Browse Source

Add function to controller to check the type of a unit.

Lee Morgan 5 years ago
parent
commit
9f05a5a605

+ 3 - 10
views/dashboardPage/js/classes/Recipe.js

@@ -2,12 +2,10 @@ const recipeBook = require("../strands/recipeBook.js");
 const analytics = require("../strands/analytics.js");
 
 class RecipeIngredient{
-    constructor(ingredient, quantity, unit, baseUnitMultiplier){
+    constructor(ingredient, quantity, unit){
         this._ingredient = ingredient;
         this._quantity = quantity;
         this._unit = unit;
-        //Quantity * baseUnitMultiplier will give you the quantity converted to the base unit of the ingredient (gram/meter/liter)
-        this._baseUnitMultiplier = baseUnitMultiplier;
     }
 
     get ingredient(){
@@ -22,10 +20,6 @@ class RecipeIngredient{
         this_quantity = controller.baseUnit(quantity, this._ingredient.unit);
     }
 
-    get baseUnitMultiplier(){
-        return this._baseUnitMultiplier;
-    }
-
     get unit(){
         return this._unit;
     }
@@ -34,9 +28,8 @@ class RecipeIngredient{
         return `${this._quantity.toFixed(2)} ${this._unit.toUpperCase()}`;
     }
 
-    getQuantityAsBase(){
-        return this._quantity * this._baseUnitMultiplier;
-    }
+    // getQuantityAsBase(){
+    // }
 }
 
 /*

+ 7 - 0
views/dashboardPage/js/dashboard.js

@@ -426,6 +426,13 @@ controller = {
             case "ft": return quantity * 3.2808;
             default: return quantity;
         }
+    },
+
+    unitType(unit){
+        if(["g", "kg", "oz", "lb"].includes(unit)) return "mass";
+        if(["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"].includes(unit)) return volume;
+        if(["mm", "cm", "m", "in", "ft"].includes(unit)) return "length";
+        return "other";
     }
 }
 

+ 6 - 6
views/dashboardPage/js/sidebars/newIngredient.js

@@ -26,19 +26,19 @@ let newIngredient = {
         let convertMass = document.getElementById("newIngMassConvert");
         let convertVolume = document.getElementById("newIngVolumeConvert");
         let convertLength = document.getElementById("newIngLengthConvert");
-        if(["g", "kg", "oz", "lb"].includes(select.value)){
+        if(controller.unitType(select.value) === "mass"){
             convertMass.style.display = "none";
             convertVolume.style.display = "flex";
             convertLength.style.display = "flex";
             document.getElementById("volumeConvertUnitLeft").innerText = select.value.toUpperCase();
             document.getElementById("lengthConvertUnitLeft").innerText = select.value.toUpperCase();
-        }else if(["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"].includes(select.value)){
+        }else if(controller.unittype(select.value) === "volume"){
             convertMass.style.display = "flex";
             convertVolume.style.display = "none";
             convertLength.style.display = "flex";
             document.getElementById("massConvertUnitLeft").innerText = select.value.toUpperCase();
             document.getElementById("lengthConvertUnitLeft").innerText = select.value.toUpperCase();
-        }else if(["mm", "cm", "m", "in", "ft"].includes(select.value)){
+        }else if(controller.unitType(select.value) === "length"){
             convertMass.style.display = "flex";
             convertVolume.style.display = "flex";
             convertLength.style.display = "none";
@@ -79,9 +79,9 @@ let newIngredient = {
             }
         }
 
-        if(["g", "kg", "oz", "lb"].includes(unit)) newIngredient.convert.toMass = 1;
-        if(["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"].includes(unit)) newIngredient.convert.toVolume = 1;
-        if(["mm", "cm", "m", "in", "ft"].includes(unit))newIngredient.convert.toLength = 1;
+        if(controller.unitType(unit) === "mass") newIngredient.convert.toMass = 1;
+        if(controller.unitType(unit) === "volume") newIngredient.convert.toVolume = 1;
+        if(controller.unittype(unit) === "length") newIngredient.convert.toLength = 1;
 
         if(isNaN(newIngredient.convert.toMass)) newIngredient.convert.toMass = undefined;
         if(isNaN(newIngredient.convert.toVolume)) newIngredient.convert.toVolume = undefined;