addRecipe.jsp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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">
  9. var app = angular.module('recipe', ['ui.bootstrap', 'ieat.ui.editors']);
  10. app.directive('myOnEnter', function () {
  11. return function (scope, element, attrs) {
  12. element.bind("keydown keypress", function (event) {
  13. if(event.which === 13) {
  14. scope.$apply(function (){
  15. scope.$eval(attrs.myOnEnter);
  16. });
  17. event.preventDefault();
  18. }
  19. });
  20. };
  21. });
  22. app.controller('SearchController', ['$scope', 'Food', 'Recipe',
  23. function($scope, Food, Recipe) {
  24. $scope.recipe = new Recipe({
  25. steps: [],
  26. ingredients: []
  27. });
  28. $scope.queryFoods = function(q) {
  29. return Food.query({query: q}).$promise;
  30. };
  31. $scope.addIngredient = function($item) {
  32. $scope.recipe.ingredients.push({
  33. amount: parseFloat($scope.nextAmount),
  34. item: $item
  35. });
  36. };
  37. $scope.removeIngredient = function(index) {
  38. $scope.recipe.ingredients.splice(index, 1);
  39. };
  40. $scope.addStep = function(step) {
  41. $scope.recipe.steps.push(step);
  42. };
  43. $scope.removeStep = function(index) {
  44. $scope.recipe.steps.splice(index, 1);
  45. };
  46. $scope.saveRecipe = function(index) {
  47. $scope.recipe.$save(function () {
  48. window.location = "/ieat-2.0.0";
  49. }, function (err) {
  50. // TODO: Proper error handling
  51. console.error(err);
  52. });
  53. };
  54. }]);
  55. </script>
  56. </jsp:attribute>
  57. <jsp:body>
  58. <div class="section container" data-ng-app="recipe" data-ng-controller="SearchController">
  59. <h2>Add Recipe</h2>
  60. <label for="recipeName">Name:</label>
  61. <input id="recipeName" type="text" data-ng-model="recipe.name" />
  62. <input type="button" value="Add" class="checkbox-inline" data-ng-click="saveRecipe()" />
  63. <table style="width: 100%;">
  64. <tr>
  65. <td style="width: 50%;">
  66. <ol>
  67. <li data-ng-repeat="step in recipe.steps track by $index">
  68. {{step}}
  69. <span class="glyphicon glyphicon-remove" data-ng-click="removeStep($index);"></span>
  70. </li>
  71. </ol>
  72. </td>
  73. <td style="width: 50%;" colspan="2">
  74. <ul>
  75. <li data-ng-repeat="ingredient in recipe.ingredients">
  76. {{ingredient.amount}}{{ingredient.item.default_unit}}
  77. {{ingredient.item.name}}
  78. <span class="glyphicon glyphicon-remove" data-ng-click="removeIngredient($index);"></span>
  79. </li>
  80. </ul>
  81. </td>
  82. </tr>
  83. <tr>
  84. <td>
  85. <input type="text"
  86. style="width: 100%;"
  87. placeholder="Next step..."
  88. data-ng-model="nextStep"
  89. data-my-on-enter="addStep(nextStep); nextStep='';" />
  90. </td>
  91. <td>
  92. <input type="text"
  93. class="form-control"
  94. size="3"
  95. data-ng-model="nextAmount" />
  96. </td>
  97. <td>
  98. <input type="text"
  99. class="form-control"
  100. placeholder="Next ingredient..."
  101. data-ng-model="nextIngredient"
  102. data-uib-typeahead="food.name for food in queryFoods($viewValue)"
  103. data-typeahead-editable="false"
  104. data-typeahead-min-length="3"
  105. data-typeahead-on-select="addIngredient($item)"
  106. data-typeahead-loading="loadingFoods"
  107. data-typeahead-no-results="ingredientNotFound" />
  108. <i ng-show="loadingFoods" class="glyphicon glyphicon-refresh"></i>
  109. <div ng-show="ingredientNotFound">
  110. <i class="glyphicon glyphicon-remove"></i> No Results Found
  111. </div>
  112. </td>
  113. </tr>
  114. </table>
  115. </jsp:body>
  116. </t:template>