| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- /**
- * 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', 'NDBNutrDef', 'Unit',
- function($scope, $q, NDBList, NDBWeight, NDBNutrDef, Unit) {
- var self = this;
- $scope.units_symbol = {};
- Unit.primary(function(units) {
- units.forEach(function(elm) {
- $scope.units_symbol[elm.type] = elm.symbol;
- });
- });
- NDBNutrDef.query(function(data) {
- $scope.nutrients = data.reduce(function(r, n) {
- r[n.nutr_no] = {name: n.nutr_desc, unit: n.unit};
- return r;
- }, {});
- });
- this.$onInit = function() {
- $scope.categories = NDBList.get({
- key: self.ndbKey,
- type: "g"
- });
- var getCanidateUnit = function(weight, units) {
- return units.find(function(u) {
- var w = weight.msre_desc.toLowerCase();
- return u.type == "volume" &&
- (w == u.symbol.toLowerCase()
- || u.aliases.some(function(a) {
- return w == a.toLowerCase();
- }));
- });
- };
-
- 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;
- }
- };
- }]
- });
- })();
|