browseFood.jsp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <%@page contentType="text/html" pageEncoding="UTF-8"%>
  2. <%@taglib prefix="t" tagdir="/WEB-INF/tags" %>
  3. <t:template>
  4. <jsp:attribute name="title">Manage Food</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('ingredients', ['basicFoodEditor', 'ui.bootstrap']);
  10. app.controller('SearchController', ['$scope', '$timeout', '$uibModal', 'Food', 'BasicFood', 'Recipe',
  11. function($scope, $timeout, $uibModal, Food, BasicFood, Recipe) {
  12. $scope.searchTerm = "";
  13. $scope.searchOffset = 1;
  14. $scope.searchSize = "10";
  15. var searchTimeout = false;
  16. var searchFn = function() {
  17. Food.query({
  18. "query": $scope.searchTerm
  19. }, function(data) {
  20. $scope.searchResults = data;
  21. }, function (err) {
  22. console.error(err);
  23. });
  24. };
  25. $scope.searchResults = searchFn();
  26. $scope.$watchGroup(['searchTerm', 'searchOffset'], function(newValues, oldValues, scope) {
  27. if (searchTimeout)
  28. {
  29. $timeout.cancel(searchTimeout);
  30. }
  31. if ($scope.searchTerm.length >= 3)
  32. {
  33. searchTimeout = $timeout(searchFn, 100);
  34. }
  35. });
  36. function getResource(type) {
  37. switch (type) {
  38. case "BasicFood":
  39. return BasicFood;
  40. case "Recipe":
  41. return Recipe;
  42. default:
  43. throw "Unrecognized food type: "+type;
  44. }
  45. }
  46. $scope.promptWindow = function(id, type) {
  47. var item = getResource(type).get({"id": id});
  48. $uibModal.open({
  49. // TODO: Figure out what these are and how they work
  50. //ariaLabelledBy: 'modal-title',
  51. //ariaDescribedBy: 'modal-body',
  52. templateUrl: 'static/templates/editBasicFood.html',
  53. controller: 'BasicFoodEditorController',
  54. size: "md",
  55. resolve: {
  56. foodData: function() {return item;}
  57. }
  58. }).result.then(searchFn);
  59. };
  60. }]);
  61. </script>
  62. </jsp:attribute>
  63. <jsp:body>
  64. <div class="section container" data-ng-app="ingredients" data-ng-controller="SearchController">
  65. <h2>Ingredients</h2>
  66. <div class="form-group">
  67. <label for="search">Search: </label>
  68. <input type="text" class="form-control input-sm" id="search" data-ng-model="searchTerm" />
  69. </div>
  70. <table class="table table-striped table-hover table-responsive">
  71. <tr>
  72. <th class="col-md-1"> </th>
  73. <th class="col-md-6">Name</th>
  74. <th class="col-md-3">Group</th>
  75. <th class="col-md-3">Calories</th>
  76. </tr>
  77. <tr data-ng-repeat="item in searchResults | limitTo:searchSize:searchSize*(searchOffset-1)"
  78. data-ng-click="item.checked = !item.checked">
  79. <th>
  80. <input type="button" value="Edit" class="checkbox-inline" data-ng-click="promptWindow(item.id, item.type)" />
  81. </th>
  82. <td>{{ ::item.name }}</td>
  83. <td>{{ ::item.food_group }}</td>
  84. <td>{{ ::item.calories_p_100 }}</td>
  85. </tr>
  86. </table>
  87. <div class="form-group col-xs-5">
  88. <ul class="pagination-sm"
  89. style="margin: 0;"
  90. data-uib-pagination=""
  91. data-boundary-links="true"
  92. force-ellipses="true"
  93. data-total-items="searchResults.length"
  94. data-ng-model="searchOffset"
  95. data-items-per-page="searchSize"></ul>
  96. </div>
  97. <div class="form-group form-group-sm col-xs-2 pull-right">
  98. <select id="sizeSelector" class="form-control input-sm" data-ng-model="searchSize">
  99. <option>10</option>
  100. <option>20</option>
  101. <option>30</option>
  102. <option>40</option>
  103. </select>
  104. </div>
  105. </div>
  106. </jsp:body>
  107. </t:template>