addRecipe.jsp 6.6 KB

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