window.ingredientsStrandObj = {
isPopulated: false,
ingredients: [],
display: function(){
if(!this.isPopulated){
this.populateByProperty("category");
this.isPopulated = true;
}
},
populateByProperty: function(property){
let categories;
if(property === "category"){
categories = categorizeIngredients(merchant.inventory);
}else if(property === "unit"){
categories = unitizeIngredients(merchant.inventory);
}
let ingredientStrand = document.querySelector("#categoryList");
let categoryTemplate = document.querySelector("#categoryDiv").content.children[0];
let ingredientTemplate = document.querySelector("#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;
categoryDiv.children[0].children[1].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.name;
ingredientDiv.children[2].innerText = `${ingredient.quantity} ${ingredient.unit}`;
ingredientDiv.onclick = ()=>{ingredientDetailsComp.display(ingredient, categories[i])};
ingredientDiv._name = ingredient.name.toLowerCase();
ingredientDiv._unit = ingredient.unit.toLowerCase();
categoryDiv.children[1].appendChild(ingredientDiv);
this.ingredients.push(ingredientDiv);
}
}
},
displayIngredientsOnly: function(ingredients){
let ingredientDiv = document.querySelector("#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 = '';
div.style.display = "flex";
}else if(div.style.display === "flex"){
button.innerHTML = '';
div.style.display = "none";
}
},
search: function(){
let input = document.querySelector("#ingredientSearch").value.toLowerCase();
document.querySelector("#ingredientSelect").selectedIndex = 0;
if(input === ""){
this.populateByProperty("category");
document.querySelector("#ingredientClearButton").style.display = "none";
return;
}
let matchingIngredients = [];
for(let i = 0; i < this.ingredients.length; i++){
if(this.ingredients[i]._name.includes(input)){
matchingIngredients.push(this.ingredients[i]);
}
}
document.querySelector("#ingredientClearButton").style.display = "inline";
this.displayIngredientsOnly(matchingIngredients);
},
sort: function(sortType){
if(sortType === ""){
return;
}
document.querySelector("#ingredientSearch").value = "";
if(sortType === "category"){
this.populateByProperty("category");
return;
}
if(sortType === "unit"){
this.populateByProperty("unit");
return;
}
document.querySelector("#ingredientClearButton").style.display = "inline";
let sortedIngredients = this.ingredients.slice().sort((a, b)=> (a[sortType] > b[sortType]) ? 1 : -1);
this.displayIngredientsOnly(sortedIngredients);
},
clearSorting: function(button){
document.querySelector("#ingredientSearch").value = "";
document.querySelector("#ingredientSelect").selectedIndex = 0;
document.querySelector("#ingredientClearButton").style.display = "none";
this.populateByProperty("category");
}
}