addRecipe.jsp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
  3. <t:template>
  4. <jsp:attribute name="title">Add Recipe</jsp:attribute>
  5. <jsp:attribute name="head">
  6. <script type="text/javascript" src="static/ndbDatabase.js"></script>
  7. <script type="text/javascript" src="static/basicFoodEditor.js"></script>
  8. <script type="text/javascript" src="static/Food.js"></script>
  9. <script type="text/javascript" src="static/units.js"></script>
  10. <script type="text/javascript" src="static/searchBar.js"></script>
  11. <script type="text/javascript">
  12. var app = angular.module('recipe',
  13. ['ui.bootstrap', 'Food', 'ieat.ui.editors']);
  14. app.directive('myOnEnter', function () {
  15. return function (scope, element, attrs) {
  16. element.bind("keydown keypress", function (event) {
  17. if(event.which === 13) {
  18. scope.$apply(function (){
  19. scope.$eval(attrs.myOnEnter);
  20. });
  21. event.preventDefault();
  22. }
  23. });
  24. };
  25. });
  26. app.controller('SearchController',
  27. ['$scope', '$uibModal', 'Food', 'Recipe', 'Unit',
  28. function($scope, $uibModal, Food, Recipe, Unit) {
  29. $scope.recipe = new Recipe({
  30. steps: [],
  31. ingredients: []
  32. });
  33. $scope.queryFoods = function(q) {
  34. return Food.query({query: q}).$promise;
  35. };
  36. var primeUnits = $scope.primeUnits = {};
  37. var unitList = Unit.query(function(units) {
  38. units.forEach(function(u) {
  39. if (u.conversion == 1) {
  40. $scope.primeUnits[u.type] = u;
  41. }
  42. });
  43. });
  44. var newIngredientCtrl = [
  45. '$scope', '$uibModalInstance', 'food', 'units',
  46. function($scope, $uibModalInstance, food, units) {
  47. $scope.food = food.name;
  48. $scope.units = units;
  49. $scope.unit = primeUnits[food.unit_type];
  50. $scope.submit = $uibModalInstance.close;
  51. $scope.dismiss = $uibModalInstance.dismiss;
  52. }];
  53. $scope.addIngredient = function($item) {
  54. $uibModal.open({
  55. // TODO: Figure out what these are and how they work
  56. //ariaLabelledBy: 'modal-title',
  57. //ariaDescribedBy: 'modal-body',
  58. templateUrl: "newIngredient",
  59. resolve: {
  60. food: function() {return $item;},
  61. units: function() {return unitList;}
  62. },
  63. controller: newIngredientCtrl,
  64. size: "md"
  65. }).result.then(function(amount) {
  66. $scope.recipe.ingredients.push({
  67. amount: parseFloat(amount),
  68. item: $item
  69. });
  70. }, function(reason) {
  71. console.debug(reason);
  72. });
  73. };
  74. $scope.removeIngredient = function(index) {
  75. $scope.recipe.ingredients.splice(index, 1);
  76. };
  77. $scope.addStep = function(step) {
  78. $scope.recipe.steps.push(step);
  79. };
  80. $scope.removeStep = function(index) {
  81. $scope.recipe.steps.splice(index, 1);
  82. };
  83. $scope.saveRecipe = function(index) {
  84. $scope.recipe.$save(function () {
  85. window.location = "/ieat-2.0.0";
  86. }, function (err) {
  87. // TODO: Proper error handling
  88. console.error(err);
  89. });
  90. };
  91. }]);
  92. </script>
  93. </jsp:attribute>
  94. <jsp:body>
  95. <div class="section container" data-ng-app="recipe" data-ng-controller="SearchController">
  96. <h2>Add Recipe</h2>
  97. <label for="recipeName">Name:</label>
  98. <input id="recipeName" type="text" data-ng-model="recipe.name" />
  99. <input type="button" value="Add" class="checkbox-inline" data-ng-click="saveRecipe()" />
  100. <table style="width: 100%;">
  101. <tr>
  102. <td style="width: 50%;">
  103. <ol>
  104. <li data-ng-repeat="step in recipe.steps track by $index">
  105. {{step}}
  106. <span class="glyphicon glyphicon-remove" data-ng-click="removeStep($index);"></span>
  107. </li>
  108. </ol>
  109. </td>
  110. <td style="width: 50%;" colspan="2">
  111. <ul>
  112. <li data-ng-repeat="ingredient in recipe.ingredients">
  113. {{::ingredient.amount}}
  114. {{primeUnits[ingredient.item.unit_type].symbol}}
  115. {{::ingredient.item.name}}
  116. <span class="glyphicon glyphicon-remove"
  117. data-ng-click="removeIngredient($index);"></span>
  118. </li>
  119. </ul>
  120. </td>
  121. </tr>
  122. <tr>
  123. <td>
  124. <input type="text"
  125. style="width: 100%;"
  126. placeholder="Next step..."
  127. data-ng-model="nextStep"
  128. data-my-on-enter="addStep(nextStep); nextStep='';" />
  129. </td>
  130. <td>
  131. <input type="text"
  132. class="form-control"
  133. placeholder="Next ingredient..."
  134. data-ng-model="nextIngredient"
  135. data-uib-typeahead="food.name for food in queryFoods($viewValue)"
  136. data-typeahead-editable="false"
  137. data-typeahead-min-length="3"
  138. data-typeahead-on-select="addIngredient($item)"
  139. data-typeahead-loading="loadingFoods"
  140. data-typeahead-no-results="ingredientNotFound" />
  141. <i ng-show="loadingFoods" class="glyphicon glyphicon-refresh"></i>
  142. <div ng-show="ingredientNotFound">
  143. <i class="glyphicon glyphicon-remove"></i> No Results Found
  144. </div>
  145. </td>
  146. </tr>
  147. </table>
  148. <script type="text/ng-template" id="newIngredient">
  149. <div style="width: 100%; text-align: center; padding: 0 1em 1em 0;">
  150. <span style="font-weight: bold;">{{::food}}: </span>
  151. <input type="number" min="0" required="required"
  152. data-ng-model="amount" />
  153. <select required="required" data-ng-model="unit"
  154. data-ng-options="u.name for u in units">
  155. </select>
  156. <button type="button"
  157. class="btn btn-success"
  158. data-ng-click="submit(unit.normalize(amount));">Submit</button>
  159. <button type="button" class="btn" data-ng-click="dismiss();">
  160. Cancel
  161. </button>
  162. </div>
  163. </script>
  164. </jsp:body>
  165. </t:template>