| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- let ingredients = {
- isPopulated: false,
- ingredients: [],
- display: function(){
- if(!this.isPopulated){
- document.getElementById("ingredientSearch").oninput = ()=>{this.search()};
- this.populateByProperty();
- this.isPopulated = true;
- }
- },
- populateByProperty: function(){
- let categories;
- categories = merchant.categorizeIngredients();
-
- let ingredientStrand = document.getElementById("categoryList");
- let categoryTemplate = document.getElementById("categoryDiv").content.children[0];
- let ingredientTemplate = document.getElementById("ingredient").content.children[0];
- this.ingredients = [];
- while(ingredientStrand.children.length > 0){
- ingredientStrand.removeChild(ingredientStrand.firstChild);
- }
- for(let i = 0; i < categories.length; i++){
- let categoryDiv = categoryTemplate.cloneNode(true);
- categoryDiv.children[0].children[0].innerText = categories[i].name.toUpperCase();
-
- categoryDiv.children[0].onclick = ()=>{
- this.toggleCategory(categoryDiv.children[1], categoryDiv.children[0].children[1]);
- };
- categoryDiv.children[1].style.display = "none";
- ingredientStrand.appendChild(categoryDiv);
- for(let j = 0; j < categories[i].ingredients.length; j++){
- let ingredient = categories[i].ingredients[j];
- let ingredientDiv = ingredientTemplate.cloneNode(true);
- ingredientDiv.children[0].innerText = ingredient.ingredient.name;
- ingredientDiv.onclick = ()=>{
- controller.openSidebar("ingredientDetails", ingredient);
- ingredientDiv.classList.add("active");
- };
- ingredientDiv._name = ingredient.ingredient.name.toLowerCase();
- ingredientDiv._unit = ingredient.ingredient.unit.toLowerCase();
- ingredientDiv.children[2].innerText = `${ingredient.quantity.toFixed(2)} ${ingredient.ingredient.unit.toUpperCase()}`;
- categoryDiv.children[1].appendChild(ingredientDiv);
- this.ingredients.push(ingredientDiv);
- }
- }
- },
- displayIngredientsOnly: function(ingredients){
- let ingredientDiv = document.getElementById("categoryList");
- while(ingredientDiv.children.length > 0){
- ingredientDiv.removeChild(ingredientDiv.firstChild);
- }
- for(let i = 0; i < ingredients.length; i++){
- ingredientDiv.appendChild(ingredients[i]);
- }
- },
- toggleCategory: function(div, button){
- if(div.style.display === "none"){
- button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="18 15 12 9 6 15"></polyline></svg>';
- div.style.display = "flex";
- }else if(div.style.display === "flex"){
- button.innerHTML = '<svg width="32" height="32" viewBox="0 0 24 24" fill="none" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>';
- div.style.display = "none";
- }
- },
- search: function(){
- let input = document.getElementById("ingredientSearch").value.toLowerCase();
- if(input === ""){
- this.populateByProperty();
- return;
- }
- let matchingIngredients = [];
- for(let i = 0; i < this.ingredients.length; i++){
- if(this.ingredients[i]._name.includes(input)){
- matchingIngredients.push(this.ingredients[i]);
- }
- }
- this.displayIngredientsOnly(matchingIngredients);
- }
- }
- module.exports = ingredients;
|