| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- /**
- * 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);
- }
- }
- };
- return res;
- }]);
- })();
|