editRecipe.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. module.exports = {
  2. unused: [],
  3. display: function(recipe){
  4. document.getElementById("sidebarDiv").classList.add("sidebarWide");
  5. document.getElementById("editRecipeTitle").innerText = recipe.name;
  6. document.getElementById("editRecipeSearch").onchange = ()=>{this.search()};
  7. document.getElementById("editRecipeSubmit").onclick = ()=>{this.submit()};
  8. let used = document.getElementById("editRecipeUsed");
  9. let unused = document.getElementById("editRecipeUnused");
  10. let template = document.getElementById("editRecipeInputItem").content.children[0];
  11. let tempList = [];
  12. for(let i = 0; i < recipe.ingredients.length; i++){
  13. tempList.push(recipe.ingredients[i].ingredient.id);
  14. let ingredient = template.cloneNode(true);
  15. ingredient.ingredient = recipe.ingredients[i].ingredient;
  16. ingredient.children[0].children[0].innerText = recipe.ingredients[i].ingredient.name;
  17. ingredient.children[0].children[1].onclick = ()=>{this.removeIngredient(ingredient)};
  18. ingredient.children[1].children[0].value = recipe.ingredients[i].quantity;
  19. ingredient.children[1].children[1].value = recipe.ingredients[i].unit;
  20. used.appendChild(ingredient);
  21. }
  22. for(let i = 0; i < merchant.inventory.length; i++){
  23. if(tempList.includes(merchant.inventory[i].ingredient.id)) continue;
  24. this.unused.push(merchant.inventory[i].ingredient);
  25. }
  26. this.displayUnused();
  27. },
  28. displayUnused: function(){
  29. let container = document.getElementById("editRecipeUnused");
  30. this.unused.sort((a, b) => (a.name > b.name) ? 1 : -1);
  31. while(container.children.length > 0){
  32. container.removeChild(container.firstChild);
  33. }
  34. for(let i = 0; i < this.unused.length; i++){
  35. let button = document.createElement("button");
  36. button.innerText = this.unused[i].name;
  37. button.classList.add("choosable");
  38. button.classList.add("selection");
  39. button.ingredient = this.unused[i];
  40. button.onclick = ()=>{this.addIngredient(button)};
  41. container.appendChild(button);
  42. }
  43. },
  44. addIngredient: function(ingredient){
  45. for(let i = 0; i < this.unused.length; i++){
  46. if(this.unused[i] === ingredient.ingredient){
  47. this.unused.splice(i, 1);
  48. break;
  49. }
  50. }
  51. let unused = document.getElementById("editRecipeUnused");
  52. unused.removeChild(ingredient);
  53. let used = document.getElementById("editRecipeUsed");
  54. let newItem = document.getElementById("editRecipeInputItem").content.children[0].cloneNode(true);
  55. newItem.ingredient = ingredient.ingredient;
  56. newItem.children[0].children[0].innerText = ingredient.ingredient.name;
  57. newItem.children[0].children[1].onclick = ()=>{this.removeIngredient(newItem)};
  58. used.appendChild(newItem);
  59. },
  60. removeIngredient: function(ingredient){
  61. let used = document.getElementById("editRecipeUsed");
  62. this.unused.push(ingredient.ingredient);
  63. used.removeChild(ingredient);
  64. this.displayUnused();
  65. },
  66. search: function(){
  67. },
  68. submit: function(){
  69. }
  70. }