Procházet zdrojové kódy

Sub-ingredient quantity now stores properly.
Create function to get display of sub-ingredient quantity.
Update storage of ingredient quantities on the recipes themselves.

Lee Morgan před 5 roky
rodič
revize
547e2ddf6f

+ 1 - 1
models/ingredient.js

@@ -41,7 +41,7 @@ const IngredientSchema = new mongoose.Schema({
             ref: "Ingredient",
             required: true
         },
-        quantity: {
+        quantity: { //quantity per base unit of parent
             type: Number,
             required: true,
             min: 0

+ 1 - 1
views/dashboardPage/dashboard.ejs

@@ -7,7 +7,7 @@
         <link rel="icon" type="img/png" href="/shared/images/logo.png">
         <link rel="stylesheet" href="/dashboardPage/bundle.css">
         <link href="https://fonts.googleapis.com/css?family=Saira&display=swap" rel="stylesheet"> 
-        <!-- <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> -->
+        <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
     </head>
     <body>
         <% include ./ejs/menu.ejs %>

+ 31 - 2
views/dashboardPage/js/classes/Ingredient.js

@@ -1,14 +1,43 @@
+class SubIngredient{
+    constructor(id, ingredient, quantity, unit, parent){
+        this.id = id;
+        this._ingredient = ingredient;
+        this.quantity = quantity;
+        this.unit = unit;
+        this.parent = parent;
+    }
+
+    get ingredient(){
+        return merchant.getIngredient(this._ingredient).ingredient;
+    }
+
+    getDisplayQuantity(){
+        let mult = controller.unitMultiplier(controller.unitType(this.parent.unit), this.parent.unit);
+        return `${this.quantity * mult} ${this.unit} / ${this.parent.unit}`;
+    }
+}
+
 class Ingredient{
-    constructor(id, name, category, unitType, unit, parent, unitSize = undefined, convert){
+    constructor(id, name, category, unitType, unit, subIngredients, convert, parent, unitSize = undefined){
         this._id = id;
         this._name = name;
         this._category = category;
         this._unitType = unitType;
         this._unit = unit;
+        this._subIngredients = [];
         this._parent = parent;
         this._unitSize = unitSize;
-        this._subIngredients = [];
         this._convert = convert;
+
+        for(let i = 0; i < subIngredients.length; i++){
+            this._subIngredients.push(new SubIngredient(
+                subIngredients[i]._id,
+                subIngredients[i].ingredient,
+                subIngredients[i].quantity,
+                subIngredients[i].unit,
+                this
+            ));
+        }
     }
 
     get id(){

+ 5 - 10
views/dashboardPage/js/classes/Merchant.js

@@ -103,9 +103,10 @@ class Merchant{
                 ingredients[i].ingredient.category,
                 ingredients[i].ingredient.unitType,
                 ingredients[i].defaultUnit,
+                ingredients[i].ingredient.ingredients,
+                ingredients[i].ingredient.convert,
                 this,
                 ingredients[i].ingredient.unitSize,
-                ingredients[i].ingredient.convert
             );
 
             const merchantIngredient = new MerchantIngredient(
@@ -117,11 +118,6 @@ class Merchant{
             this._inventory.push(merchantIngredient);
         }
 
-        for(let i = 0; i < ingredients.length; i++){
-            let thisIngredient = this.getIngredient(ingredients[i].ingredient._id);
-            thisIngredient.ingredient.addIngredients(ingredients[i].ingredient.ingredients);
-        }
-
         //populate recipes
         for(let i = 0; i < recipes.length; i++){
             let ingredients = [];
@@ -258,13 +254,12 @@ class Merchant{
                 ingredient.category,
                 ingredient.unitType,
                 defaultUnit,
+                ingredient.ingredients,
+                ingredient.convert,
                 this,
-                ingredient.unitSize,
-                ingredient.convert
+                ingredient.unitSize
             );
 
-            createdIngredient.replaceIngredients(ingredient.ingredients);
-
             const merchantIngredient = new MerchantIngredient(createdIngredient, quantity, this);
             this._inventory.push(merchantIngredient);
         }

+ 13 - 14
views/dashboardPage/js/classes/Recipe.js

@@ -17,7 +17,7 @@ class RecipeIngredient{
     }
 
     set quantity(quantity){
-        this_quantity = controller.baseUnit(quantity, this._ingredient.unit);
+        this._quantity = controller.baseUnit(quantity, this._ingredient.unit);
     }
 
     get unit(){
@@ -154,23 +154,22 @@ class Recipe{
 
     calculateIngredientTotals(){
         this._ingredientTotals = {};
-        
+
         let traverseIngredient = (recipeIngredient, multiplier)=>{
-            // console.log(recipeIngredient._ingredient);
-            // let ingredient = recipeIngredient._ingredient;
-            // for(let i = 0; i < ingredient.subIngredients.length; i++){
-            //     traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i].quantity);
-            // }
-
-            // if(this._ingredientTotals[ingredient.id] === undefined){
-            //     this._ingredientTotals[ingredient.id] = multiplier * recipeIngredient.baseUnitMultiplier;
-            // }else{
-            //     this._ingredientTotals[ingredient.id] += multiplier * recipeIngredient.baseUnitMultiplier;
-            // }
+            let ingredient = recipeIngredient._ingredient;
+            for(let i = 0; i < ingredient.subIngredients.length; i++){
+                traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i].quantity);
+            }
+
+            if(this._ingredientstotals[ingredient.id] === undefined){
+                this.ingredienttotals[ingredient.id] = multiplier;
+            }else{
+                this._ingredientTotals[ingredient.id] += multiplier;
+            }
         }
 
         for(let i = 0; i < this._ingredients.length; i++){
-            traverseIngredient(this._ingredients[i], this._ingredients[i].quantity);
+            traverseIngredient(this._ingredients[i], this._ingredients[i].getQuantityAsBase());
         }
     }
 }

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

@@ -430,6 +430,50 @@ controller = {
         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";
+    },
+
+    unitMultiplier(from, to){
+        let multiplier = 1;
+        
+        switch(from){
+            case "kg": multiplier = 1000; break;
+            case "oz": multiplier = 28.3495; break;
+            case "lb": multiplier = 453.5924; break;
+            case "ml": multiplier = .001; break;
+            case "tsp": multiplier = .00492892004404; break;
+            case "tbsp": multiplier = 0.0147868184386; break;
+            case "ozfl": multiplier = 0.0295734619582; break;
+            case "cup": multiplier = 0.239998080015; break;
+            case "pt": multiplier = 0.473171193338; break;
+            case "qt": multiplier = 0.946342386675; break;
+            case "gal": multiplier = 3.7854; break;
+            case "mm": multiplier = 0.001; break;
+            case "cm": multiplier = 0.01; break;
+            case "in": multiplier = 0.025399986284; break;
+            case "ft": multiplier = 0.304803706413; break;
+        }
+
+        switch(to){
+            case "g": return multiplier;
+            case "kg": return multiplier / 1000;
+            case "oz": return multiplier / 28.3495;
+            case "lb": return multiplier / 453.5924;
+            case "ml": return multiplier * 1000;
+            case "l": return multiplier;
+            case "tsp": return multiplier * 202.8842;
+            case "tbsp": return multiplier * 67.6278;
+            case "cup": return multiplier * 4.1667;
+            case "pt": return multiplier * 2.1134;
+            case "qt": return multiplier * 1.0567
+            case "gal": return multiplier / 3.7854;
+            case "mm": return multiplier * 1000;
+            case "cm": return multiplier * 100;
+            case "m": return multiplier;
+            case "in": return multiplier * 39.3701;
+            case "ft": return multiplier * 3.2808;
+        }
+
+        return multiplier;
     }
 }
 

+ 2 - 3
views/dashboardPage/js/modal.js

@@ -281,13 +281,12 @@ let modal = {
                 let rightQuantity = parseFloat(right.children[i].children[1].children[2].children[0].value);
                 let rightUnit = right.children[i].children[1].children[2].children[1].value;
 
-                let leftBase = controller.baseUnit(leftQuantity, leftUnit);
                 let rightBase = controller.baseUnit(rightQuantity, rightUnit);
 
                 data.ingredients.push({
                     ingredient: right.children[i].ingredient.id,
-                    quantity: leftBase / rightBase,
-                    unit: right.children[i].children[1].children[0].children[1].value
+                    quantity: leftQuantity / rightBase,
+                    unit: leftUnit
                 });
             }
 

+ 1 - 1
views/dashboardPage/js/sidebars/ingredientDetails.js

@@ -29,7 +29,7 @@ let ingredientDetails = {
             let subIngredient = ingredient.ingredient.subIngredients[i];
             let button = template.cloneNode(true);
             button.children[0].innerText = subIngredient.ingredient.name;
-            button.children[1].innerText = `${subIngredient.quantity} ${subIngredient.ingredient.unit}`;
+            button.children[1].innerText = subIngredient.getDisplayQuantity();
             button.onclick = ()=>{this.display(merchant.getIngredient(subIngredient.ingredient.id))};
             subIngredientList.appendChild(button);
         }