|
|
@@ -9,17 +9,70 @@
|
|
|
<script type="text/javascript" src="static/Food.js"></script>
|
|
|
<script type="text/javascript" src="static/units.js"></script>
|
|
|
<script type="text/javascript" src="static/searchBar.js"></script>
|
|
|
+ <script type="text/javascript" src="static/nutritionLabel.js"></script>
|
|
|
+ <base href="/ieat-2.0.0/" />
|
|
|
<script type="text/javascript">
|
|
|
var app = angular.module('recipe',
|
|
|
['ui.bootstrap', 'Food', 'ieat.ui', 'ndbDatabase',
|
|
|
- 'ieat.ui.editors']);
|
|
|
+ 'ieat.ui.editors'])
|
|
|
+ .config(['$locationProvider', function($locationProvider) {
|
|
|
+ $locationProvider.html5Mode(true);
|
|
|
+ }]);
|
|
|
app.controller('SearchController',
|
|
|
- ['$scope', '$uibModal', 'Food', 'Recipe', 'Unit', 'NDBList',
|
|
|
- function($scope, $uibModal, Food, Recipe, Unit, NDBList) {
|
|
|
- $scope.recipe = new Recipe({
|
|
|
+ ['$scope', '$location', '$uibModal', '$q',
|
|
|
+ 'Food', 'Recipe', 'Unit', 'NDBList',
|
|
|
+ function($scope, $location, $uibModal, $q,
|
|
|
+ Food, Recipe, Unit, NDBList) {
|
|
|
+ var loadRecipe = function(id) {
|
|
|
+ return Recipe.get({"id": id}, function(r) {
|
|
|
+ r.ingredients = r.ingredients.map(function(pair) {
|
|
|
+ return {
|
|
|
+ amount: pair.amount,
|
|
|
+ item: Food.get({"id": pair.item})
|
|
|
+ };
|
|
|
+ });
|
|
|
+ $scope.amount = r.result_amount;
|
|
|
+ });
|
|
|
+ };
|
|
|
+ var id = $location.search()["id"];
|
|
|
+ $scope.recipe = id? loadRecipe(id) : new Recipe({
|
|
|
steps: [],
|
|
|
ingredients: []
|
|
|
});
|
|
|
+ var ingredientSum = {calories: 0, grams: 0, nutrition: {}};
|
|
|
+ var toGrams = function (amount, food) {
|
|
|
+ switch (food.unit_type) {
|
|
|
+ case "mass":
|
|
|
+ return amount;
|
|
|
+ case "volume":
|
|
|
+ return amount * food.density;
|
|
|
+ case "count":
|
|
|
+ throw "Not yet implemented!";
|
|
|
+ //return amount * food.mass_p_unit;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ var updateNutritionInfo = function() {
|
|
|
+ var grams = 0, calories = 0, nutrition = {};
|
|
|
+ $scope.recipe.ingredients.forEach(function(i) {
|
|
|
+ grams += toGrams(i.amount, i.item);
|
|
|
+ calories += i.amount * i.item.calories_p_100;
|
|
|
+ for (var n in i.item.nutrients) {
|
|
|
+ nutrition[n] = i.amount * i.item.nutrients[n] +
|
|
|
+ (nutrition[n] || 0);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ var rGrams = toGrams($scope.recipe.result_amount, $scope.recipe);
|
|
|
+ $scope.recipe.calories_p_100 = calories / rGrams;
|
|
|
+ $scope.recipe.nutrients = {};
|
|
|
+ for (var n in nutrition) {
|
|
|
+ $scope.recipe.nutrients[n] = nutrition[n] / rGrams;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ $scope.updateResultAmount = function() {
|
|
|
+ $scope.recipe.result_amount =
|
|
|
+ $scope.unit.normalize($scope.recipe, $scope.amount);
|
|
|
+ updateNutritionInfo();
|
|
|
+ };
|
|
|
$scope.categories = NDBList.get({
|
|
|
key: "${ndbKey}",
|
|
|
type: "g"
|
|
|
@@ -32,7 +85,8 @@
|
|
|
$scope.unit = units.find(function(u) {
|
|
|
return u.symbol == "";
|
|
|
});
|
|
|
- $scope.recipe.unit_type = $scope.unit.type;
|
|
|
+ $scope.recipe.unit_type = $scope.recipe.unit_type
|
|
|
+ || $scope.unit.type;
|
|
|
$scope.unitList = units;
|
|
|
units.forEach(function(u) {
|
|
|
if (u.conversion == 1) {
|
|
|
@@ -40,6 +94,9 @@
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
+ $q.all([unitList.$promise, $scope.recipe.$promise]).then(function() {
|
|
|
+ $scope.unit = primeUnits[$scope.recipe.unit_type];
|
|
|
+ });
|
|
|
var newIngredientCtrl = [
|
|
|
'$scope', '$uibModalInstance', 'food', 'units',
|
|
|
function($scope, $uibModalInstance, food, units) {
|
|
|
@@ -66,29 +123,45 @@
|
|
|
amount: parseFloat(amount),
|
|
|
item: $item
|
|
|
});
|
|
|
+ updateNutritionInfo();
|
|
|
}, function(reason) {
|
|
|
console.debug(reason);
|
|
|
});
|
|
|
};
|
|
|
$scope.removeIngredient = function(index) {
|
|
|
$scope.recipe.ingredients.splice(index, 1);
|
|
|
+ updateNutritionInfo();
|
|
|
};
|
|
|
|
|
|
$scope.addStep = function(step) {
|
|
|
$scope.recipe.steps.push(step);
|
|
|
+
|
|
|
};
|
|
|
$scope.removeStep = function(index) {
|
|
|
$scope.recipe.steps.splice(index, 1);
|
|
|
};
|
|
|
|
|
|
- $scope.saveRecipe = function(index) {
|
|
|
- $scope.recipe.$save(function () {
|
|
|
+ $scope.saveRecipe = function() {
|
|
|
+ var recipe = new Recipe($scope.recipe);
|
|
|
+ recipe.ingredients = recipe.ingredients.map(function(pair) {
|
|
|
+ return {amount: pair.amount, item: pair.item.id};
|
|
|
+ });
|
|
|
+ recipe.$save(function () {
|
|
|
window.location = "/ieat-2.0.0";
|
|
|
}, function (err) {
|
|
|
// TODO: Proper error handling
|
|
|
console.error(err);
|
|
|
});
|
|
|
};
|
|
|
+
|
|
|
+ $scope.deleteRecipe = function() {
|
|
|
+ $scope.recipe.$delete(function () {
|
|
|
+ window.location = "/ieat-2.0.0/browseFood";
|
|
|
+ }, function (err) {
|
|
|
+ // TODO: Proper error handling
|
|
|
+ console.error(err);
|
|
|
+ });
|
|
|
+ };
|
|
|
}]);
|
|
|
</script>
|
|
|
</jsp:attribute>
|
|
|
@@ -100,14 +173,17 @@
|
|
|
<label for="recipeName">Name:</label>
|
|
|
<input id="recipeName" type="text" data-ng-model="recipe.name" />
|
|
|
</div>
|
|
|
+ <div style="float: right;">
|
|
|
+ <nutrition-label food="recipe"></nutrition-label>
|
|
|
+ </div>
|
|
|
<div>
|
|
|
<label for="amount">Creates:</label>
|
|
|
<input id="amount" type="number" data-ng-model="amount"
|
|
|
- data-ng-change="recipe.result_amount = unit.normalize(recipe, amount);" />
|
|
|
+ data-ng-change="updateResultAmount();" />
|
|
|
<select data-ng-model="unit"
|
|
|
data-ng-change="recipe.dry = unit.type != 'volume';
|
|
|
recipe.unit_type = unit.type;
|
|
|
- recipe.result_amount = unit.normalize(recipe, amount);"
|
|
|
+ updateResultAmount();"
|
|
|
data-ng-options="u as u.symbol for u in unitList">
|
|
|
</select>
|
|
|
</div>
|
|
|
@@ -128,8 +204,10 @@
|
|
|
<label for="density">Density (g/ml):</label>
|
|
|
<input id="density" type="number" data-ng-model="recipe.density" />
|
|
|
</div>
|
|
|
- <input type="button" value="Add" class="checkbox-inline"
|
|
|
+ <input type="button" value="Add" class="btn"
|
|
|
data-ng-click="saveRecipe()" />
|
|
|
+ <input type="button" value="Delete" class="btn btn-danger"
|
|
|
+ data-ng-show="recipe.id" data-ng-click="deleteRecipe()" />
|
|
|
<table style="width: 100%;">
|
|
|
<tr>
|
|
|
<td style="width: 50%;">
|