You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

notes_app.js 7.4KB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. $(document).ready(function(){
  2. $("#addNoteDiv").on('click', function() {
  3. createForm();
  4. })
  5. $('#allNotesDiv').on('click', 'button', function() {
  6. console.log("Add")
  7. console.log(this)
  8. let buttonId = (this.id).split("_")[1];
  9. let type = (this.id).split("_")[0];
  10. if (type == 'saveNoteButton'){
  11. saveNoteObject(buttonId);
  12. } else if (type == 'editNoteButton'){
  13. saveNoteObject(buttonId, true);
  14. }
  15. console.log(buttonId)
  16. })
  17. $('#noteStorage').on('click', 'button', function() {
  18. console.log("Delete")
  19. console.log(this)
  20. let buttonId = (this.id).split("_")[1];
  21. console.log(buttonId)
  22. removeBrowserItem(buttonId);
  23. })
  24. //$('.browserElement').on('click', 'div', function() {
  25. // console.log("Load");
  26. // console.log(this);
  27. // let divId = (this.id).split("_")[1];
  28. // console.log(divId);
  29. // getSavedNote(divId);
  30. //})
  31. $("#refreshButton").on('click', function() {
  32. console.log("Refresh")
  33. updateNoteBrowser();
  34. })
  35. })
  36. let id=1;
  37. function createForm(){
  38. id++;
  39. console.log("createForm() "+id)
  40. let wrapper = document.createElement("div")
  41. wrapper.setAttribute('id', "note"+id)
  42. let inputsWrapper = document.createElement("div")
  43. inputsWrapper.setAttribute('class', "noteDiv");
  44. let input = document.createElement("input");
  45. input.setAttribute('type', "text");
  46. input.setAttribute('name',"noteTitle");
  47. input.setAttribute('id', "inputTitle"+id);
  48. input.setAttribute('placeholder', "Enter title...");
  49. input.style.marginBottom='5px';
  50. let div1 = document.createElement("div");
  51. let textarea = document.createElement("textarea");
  52. textarea.setAttribute('name',"noteText");
  53. textarea.setAttribute('id', "noteText"+id);
  54. textarea.setAttribute('placeholder', "Enter notes...");
  55. textarea.style.width='300px';
  56. textarea.style.height='150px';
  57. textarea.style.boxSizing='border-box';
  58. div1.appendChild(textarea);
  59. let div2 = document.createElement("div")
  60. div2.setAttribute('id', 'buttonDiv'+id)
  61. let btn = document.createElement("button");
  62. btn.setAttribute('type',"button");
  63. btn.setAttribute('id',"saveNoteButton_"+id);
  64. btn.innerHTML = 'Save';
  65. inputsWrapper.appendChild(input);
  66. inputsWrapper.appendChild(div1);
  67. inputsWrapper.appendChild(btn);
  68. wrapper.appendChild(inputsWrapper);
  69. document.getElementById('allNotesDiv').appendChild(wrapper);
  70. }
  71. function createFormWithSavedContent(id){
  72. console.log("createFormWithSavedContent() "+id)
  73. item = JSON.parse(localStorage.getItem('noteObj'+id))
  74. console.log(item.title);
  75. console.log(item.text);
  76. let wrapper = document.createElement("div")
  77. wrapper.setAttribute('id', "note"+id)
  78. let inputsWrapper = document.createElement("div")
  79. inputsWrapper.setAttribute('class', "noteDiv");
  80. let input = document.createElement("input");
  81. input.setAttribute('type', "text");
  82. input.setAttribute('name',"noteTitle");
  83. input.setAttribute('id', "inputTitle"+id);
  84. input.setAttribute('value', item.title);
  85. //document.getElementById("inputTitle"+id).value = item.title;
  86. input.style.marginBottom='5px';
  87. let div1 = document.createElement("div");
  88. let textarea = document.createElement("textarea");
  89. textarea.setAttribute('name',"noteText");
  90. textarea.setAttribute('id', "noteText"+id);
  91. textarea.setAttribute('value', item.text);
  92. textarea.innerHTML = item.text;
  93. textarea.style.width='300px';
  94. textarea.style.height='150px';
  95. textarea.style.boxSizing='border-box';
  96. //document.getElementById('noteText'+id).value = item.text;
  97. div1.appendChild(textarea);
  98. let div2 = document.createElement("div")
  99. div2.setAttribute('id', 'buttonDiv'+id)
  100. let btn = document.createElement("button");
  101. btn.setAttribute('type',"button");
  102. btn.setAttribute('id',"editNoteButton_"+id);
  103. btn.innerHTML = 'Save';
  104. inputsWrapper.appendChild(input);
  105. inputsWrapper.appendChild(div1);
  106. inputsWrapper.appendChild(btn);
  107. wrapper.appendChild(inputsWrapper);
  108. document.getElementById('allNotesDiv').appendChild(wrapper);
  109. }
  110. let ctr;
  111. function sleep(ms) {
  112. return new Promise(resolve => setTimeout(resolve, ms));
  113. }
  114. async function saveNoteObject(id, overwrite=false){
  115. console.log("saveNoteObject() "+id)
  116. console.log(id)
  117. enableLoadingSign(id);
  118. await sleep(10000) //sleep simulates the remote server access or similar,
  119. //so instead of sleep would be e.g. saveToRemoteServer()...
  120. //the leftover code of the async function is executed after the delay
  121. disableLoadingSign(id);
  122. if (overwrite == true){
  123. localStorage.removeItem('noteObj'+id);
  124. removeBrowserItem(id);
  125. }
  126. let noteTitle = document.getElementById('inputTitle'+id).value;
  127. let noteText = document.getElementById('noteText'+id).value;
  128. let noteObj = {
  129. idK: id,
  130. title: noteTitle,
  131. text: noteText
  132. };
  133. let noteObj_serialized = JSON.stringify(noteObj); //stringify noteObj so it is displayed properly when set to local Storage
  134. localStorage.setItem("noteObj"+id, noteObj_serialized);
  135. createBrowserItem(noteObj);
  136. console.log(noteObj_serialized)
  137. document.getElementById("note"+id).remove();
  138. }
  139. function enableLoadingSign(id){
  140. console.log("enableLoadingSign() "+id)
  141. createLoadingSign(id)
  142. document.getElementById('loadSign'+id).style.display = "block";
  143. }
  144. function disableLoadingSign(id){
  145. let sign = document.getElementById('loadSign'+id);
  146. sign.style.display = "none";
  147. sign.parentNode.removeChild(sign);
  148. }
  149. function createLoadingSign(id){
  150. console.log("createLoadingSign() "+id)
  151. let loadingSign = document.createElement('div');
  152. let loadingSignWrapper = document.createElement('div');
  153. loadingSign.setAttribute('class', 'loader')
  154. loadingSign.setAttribute('id', 'loadSign'+id);
  155. loadingSignWrapper.appendChild(loadingSign);
  156. document.getElementById("note"+id).appendChild(loadingSignWrapper);
  157. }
  158. function updateNoteBrowser(){
  159. let noteStorage = [];
  160. let keys = Object.keys(localStorage);
  161. let i = keys.length;
  162. while ( i-- ) {
  163. noteStorage.push(localStorage.getItem(keys[i]));
  164. }
  165. localStorage.clear();
  166. noteStorage.forEach(element => {
  167. createBrowserItem(JSON.parse(element))
  168. });
  169. }
  170. function createBrowserItem(item){
  171. console.log(item);
  172. let wrapperDiv = document.createElement('div');
  173. let browserItem = document.createElement('div');
  174. let button = document.createElement('button');
  175. wrapperDiv.setAttribute('class', 'browserElement');
  176. browserItem.innerHTML = item.title;
  177. button.innerHTML = "X";
  178. browserItem.setAttribute('id', "browserItem_"+item.idK);
  179. browserItem.setAttribute('onClick', "getSavedNote(this.id)");
  180. button.setAttribute('id', "deleteItem_" + item.idK);
  181. wrapperDiv.appendChild(browserItem);
  182. wrapperDiv.appendChild(button);
  183. document.getElementById('noteStorage').appendChild(wrapperDiv);
  184. }
  185. function removeBrowserItem(id){
  186. console.log("removeBrowserItem() "+id)
  187. localStorage.removeItem('noteObj'+id);
  188. document.getElementById("browserItem_"+id).remove();
  189. document.getElementById("deleteItem_"+id).remove();
  190. }
  191. function getSavedNote(id){
  192. console.log("getSavedNote() "+ id);
  193. let idTmp = id.split("_")[1];
  194. createFormWithSavedContent(idTmp);
  195. }