| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- /**
- * Provides interface for unit apis.
- */
- (function() {
- var restResource = function($resource) {
- return function() {
- arguments[2]["create"] = {
- method: "PUT"
- };
- arguments[2]["update"] = {
- method: "POST"
- };
- var res = $resource.apply(null, arguments);
- res.prototype.$save = function() {
- return this[this.id? "$update":"$create"].apply(this, arguments);
- };
- return res;
- };
- };
- function normalizeFromMass(amountG, food) {
- switch (food.unit_type) {
- case "volume":
- return amountG / food.density;
- case "count":
- throw "Not yet implemented!";
- //return amountG / food.mass_p_unit;
- }
- }
- function normalizeFromVolume(amountMl, food) {
- switch (food.unit_type) {
- case "mass":
- return amountMl * food.density;
- case "count":
- throw "Not yet implemented!";
- //return amountMl * food.density / food.mass_p_unit;
- }
- }
- function normalizeFromCount(amountUnit, food) {
- switch (food.unit_type) {
- case "mass":
- throw "Not yet implemented!";
- //return amountUnit * food.mass_p_unit;
- case "volume":
- throw "Not yet implemented!";
- //return amountUnit * food.mass_p_unit / food.density;
- }
- }
-
- angular.module('Units', ['ngResource'])
- /*
- * Collection of unit apis.
- *
- * Methods:
- * get: Gets information for a unit by id
- * query: Get a list of all units
- * queryType: Get a list of all units of a type
- * delete: Deletes a unit with an id
- * create: Creates a unit
- * update: Updates a unit
- * normalize: Converts from the given unit amount to the primary unit
- * save: Creates a new unit or updates an existing unit
- */
- .factory('Unit', ['$resource', function($resource) {
- var res = restResource($resource)("unit/:id", {id: "@id"}, {
- "queryType": {
- method: 'GET',
- isArray: true,
- url: "unit/type/:type"
- },
- "primary": {
- method: 'GET',
- isArray: true,
- url: "unit/primary/:type"
- }
- });
- res.prototype.normalize = function(food, amount) {
- var normalizedToType = amount * this.conversion;
- if (food.unit_type == this.type) {
- return normalizedToType;
- }
- else {
- switch (this.type) {
- case "mass":
- return normalizeFromMass(normalizedToType, food);
- case "volume":
- return normalizeFromVolume(normalizedToType, food);
- case "count":
- return normalizeFromCount(normalizedToType, food);
- }
- }
- };
- res.prototype.toGrams = function(food, amount) {
- var normalized = amount * this.conversion;
- switch (this.type) {
- case "mass":
- return normalized;
- case "volume":
- return normalized * food.density;
- case "count":
- throw "Not yet implemented!";
- //return amountUnit * food.mass_p_unit;
- }
- };
- return res;
- }]);
- })();
|