basicFoodEditor.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * A pane that displays/edits basic food objects
  3. */
  4. (function() {
  5. (function() {
  6. try {return angular.module('ieat.ui.editors')}
  7. catch {return angular.module('ieat.ui.editors', ['ndbDatabase','Units'])}}
  8. )().component('basicFoodEditor', {
  9. templateUrl: 'static/templates/basicFoodEditor.html',
  10. bindings: {
  11. ndbKey: '@',
  12. food: '<'
  13. },
  14. controller: [
  15. '$scope', '$q', 'NDBList', 'NDBWeight', 'Unit',
  16. function($scope, $q, NDBList, NDBWeight, Unit) {
  17. var self = this;
  18. $scope.units_symbol = {};
  19. Unit.primary(function(units) {
  20. units.forEach(function(elm) {
  21. $scope.units_symbol[elm.type] = elm.symbol;
  22. });
  23. });
  24. this.$onInit = function() {
  25. $scope.categories = NDBList.get({
  26. key: self.ndbKey,
  27. type: "g"
  28. });
  29. var getCanidateUnit = function(weight, units) {
  30. return units.find(function(u) {
  31. return u.type == "volume" &&
  32. (weight.msre_desc.localeCompare(u.symbol) == 0
  33. || u.aliases.some(function(a) {
  34. return weight.msre_desc.localeCompare(a) == 0;
  35. }));
  36. });
  37. };
  38. if (!self.food.density) {
  39. $q.all([
  40. NDBWeight.query({ndbno: self.food.ndbno}).$promise,
  41. Unit.query().$promise
  42. ]).then(function(res) {
  43. for (var i = 0; i < res[0].length; i++) {
  44. var w = res[0][i];
  45. var unit = getCanidateUnit(w, res[1]);
  46. if (unit) {
  47. self.food.density =
  48. w.gm_wgt / (w.amount * unit.conversion);
  49. return;
  50. }
  51. }
  52. }, function() {
  53. // TODO: Error handling
  54. console.err("Failed to get weight information.")
  55. });
  56. }
  57. if (!self.food.unit_type) {
  58. self.food.unit_type = "Mass";
  59. self.food.dry = true;
  60. }
  61. };
  62. }]
  63. });
  64. })();