Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1.  
  2. const editModeButton = document.querySelector("#activateEditModeButton");
  3. let flag = 0;
  4. console.log("flag beofre event listener is:",flag);
  5. let editState = "edit";
  6. editModeButton.addEventListener("click", (e) => {
  7. flag += 1;
  8. console.log("THE GENERAL FLAG IS:",flag);
  9. let allPTagsInsideModal = mainDiv.querySelectorAll("p");
  10. if(e.target.tagName === "BUTTON") {
  11. let button = e.target;
  12. if(flag === 1) {
  13. console.log("the flag is now",flag);
  14. allPTagsInsideModal.forEach(p => {
  15. p.addEventListener("click" , e => {
  16.  
  17. let currentBoxNumber = e.target.parentNode;
  18. console.log("THE DIV:",currentBoxNumber);
  19. //getting ref to which p tag i clicked on
  20. let toolToEdit = e.target;
  21.  
  22. let input = document.createElement("input");
  23. input.type = "text";
  24. input.value = toolToEdit.textContent;
  25.  
  26. //setting the id of the input we created to the id of the p tag i clicked on
  27. input.id = toolToEdit.id;
  28. input.className = toolToEdit.className;
  29. console.log("does it work?",input.className);
  30.  
  31.  
  32. console.log(input.className);
  33. //inserting the input tag in place of the p tag i clicked on.
  34. currentBoxNumber.insertBefore(input,toolToEdit);
  35. currentBoxNumber.removeChild(toolToEdit);
  36. button.textContent = "שמור";
  37. console.log("the flag is still in edit mode");
  38.  
  39. });
  40. });
  41. } else if(flag === 2) {
  42. console.log("it is SAFE MODE, THE FLAG IS NOW:",flag);
  43. //getting ref to all the inputs that exists in the modal after we pressed on edit.
  44. let allInputsInModal = mainDiv.querySelectorAll("input");
  45. //loop through each existing input
  46. allInputsInModal.forEach(input => {
  47. //getting ref to the parent div of the input which is the the box
  48. let currentBoxNumber = input.parentNode;
  49. // console.log(currentBoxNumber);
  50. let newInputTool = input;
  51.  
  52.  
  53. let updatedToolPTag = document.createElement("p");
  54. //setting the id of the new p element to be equal to the id of the input.
  55. updatedToolPTag.id = newInputTool.id;
  56. //i set the number of the updatedTool to be equal to the input in order to save the updatedTool with the same number field in storage.
  57.  
  58. updatedToolPTag.className = newInputTool.className;
  59. updatedToolPTag.innerHTML = newInputTool.value;
  60. console.log("my id is:",updatedToolPTag.id);
  61. console.log("the name has been changed to:",updatedToolPTag.innerHTML);
  62. let currentName = updatedToolPTag.innerHTML;
  63. let currentBox = currentBoxNumber.className;
  64. //getting just the number of the box in order to pass it to be saved in the storage.
  65. let getJustTheBoxNum = parseInt(currentBox.slice(4));
  66.  
  67. console.log("TYPE OF CLASSNAME",typeof updatedToolPTag.className);
  68. Store.editToolFromModal(updatedToolPTag.id,currentName,getJustTheBoxNum,parseInt(updatedToolPTag.className));
  69. //inserting the updated p tag in place of the input after i click on the save.
  70. // Store.editToolFromModal(newInputTool.id,updatedToolPTag.name)
  71. currentBoxNumber.insertBefore(updatedToolPTag,newInputTool);
  72. currentBoxNumber.removeChild(newInputTool);
  73. button.textContent = "הפעל מצב עריכה";
  74. flag = 1
  75.  
  76. });
  77.  
  78. } else {
  79. flag = 1
  80. console.log("IM IN THE ELSE RIGHT NOW, THE FLAG IS",flag);
  81. return;
  82.  
  83.  
  84. }
  85. }
  86. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement