newRecipe.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. let newRecipe = {
  2. unchosen: [],
  3. display: function(){
  4. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  5. for(let i = 0; i < merchant.ingredients.length; i++){
  6. this.unchosen.push(merchant.ingredients[i].ingredient);
  7. }
  8. this.populateChoices();
  9. },
  10. populateChoices: function(){
  11. this.unchosen.sort((a, b) => (a.name > b.name) ? 1 : -1);
  12. let list = document.getElementById("recipeChoicesList");
  13. while(list.children.length > 0){
  14. list.removeChild(list.firstChild);
  15. }
  16. for(let i = 0; i < this.unchosen.length; i++){
  17. let ingredient = document.createElement("button");
  18. ingredient.innerText = this.unchosen[i].name;
  19. ingredient.classList.add("choosable");
  20. ingredient.onclick = ()=>{
  21. this.add(this.unchosen[i]);
  22. this.unchosen.splice(i, 1);
  23. this.populateChoices();
  24. };
  25. list.appendChild(ingredient);
  26. }
  27. },
  28. add: function(ingredient){
  29. let element = document.getElementById("newRecipeChosenIngredient").content.children[0].cloneNode(true);
  30. element.children[0].children[0].innerText = ingredient.name;
  31. element.children[0].children[1].onclick = ()=>{
  32. this.unchosen.push(ingredient);
  33. element.parentElement.removeChild(element);
  34. this.populateChoices();
  35. };
  36. element.children[1].placeholder = `UNIT (${ingredient.unit.toUpperCase()})`;
  37. document.getElementById("newRecipeChosenList").appendChild(element);
  38. },
  39. };
  40. module.exports = newRecipe;