addRecipe.jsp 8.2 KB

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