| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- /**
- * A pane that displays/edits basic food objects
- */
- (function() {
- (function() {
- try {return angular.module('ieat.ui.editors')}
- catch {return angular.module('ieat.ui.editors', ['ndbDatabase','Units'])}}
- )().component('basicFoodEditor', {
- templateUrl: 'static/templates/basicFoodEditor.html',
- bindings: {
- ndbKey: '@',
- food: '<'
- },
- controller: [
- '$scope', '$q', 'NDBList', 'NDBWeight', 'Unit',
- function($scope, $q, NDBList, NDBWeight, Unit) {
- var self = this;
- $scope.units_symbol = {};
- Unit.primary(function(units) {
- units.forEach(function(elm) {
- $scope.units_symbol[elm.type] = elm.symbol;
- });
- });
- this.$onInit = function() {
- $scope.categories = NDBList.get({
- key: self.ndbKey,
- type: "g"
- });
- var getCanidateUnit = function(weight, units) {
- return units.find(function(u) {
- return u.type == "volume" &&
- (weight.msre_desc.localeCompare(u.symbol) == 0
- || u.aliases.some(function(a) {
- return weight.msre_desc.localeCompare(a) == 0;
- }));
- });
- };
-
- if (!self.food.density) {
- $q.all([
- NDBWeight.query({ndbno: self.food.ndbno}).$promise,
- Unit.query().$promise
- ]).then(function(res) {
- for (var i = 0; i < res[0].length; i++) {
- var w = res[0][i];
- var unit = getCanidateUnit(w, res[1]);
- if (unit) {
- self.food.density =
- w.gm_wgt / (w.amount * unit.conversion);
- return;
- }
- }
- }, function() {
- // TODO: Error handling
- console.err("Failed to get weight information.")
- });
- }
- if (!self.food.unit_type) {
- self.food.unit_type = "Mass";
- self.food.dry = true;
- }
- };
- }]
- });
- })();
|