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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. $(document).ready(function(){
  2. $("#addNoteDiv").on('click', function() {
  3. // Listener für den Plus Button zum hinzufügen von Note Forms.
  4. console.log("Add")
  5. createForm();
  6. })
  7. $('#allNotesDiv').on('click', 'button', async function() {
  8. // Listener für den Save Button zum speichern von Note Forms.
  9. console.log("Save")
  10. let buttonId = (this.id).split("_")[1];
  11. let type = (this.id).split("_")[0];
  12. try {
  13. if (type == 'saveNoteButton'){
  14. // Speichern einer neuen Note.
  15. await saveNoteObject(buttonId);
  16. } else if (type == 'editNoteButton'){
  17. // Editieren einer gespeicherten Note.
  18. await saveNoteObject(buttonId, true);
  19. }
  20. } catch(err){
  21. alert(err);
  22. }
  23. })
  24. $('#noteStorage').on('click', 'button', async function() {
  25. // Listener für den Delete Button zum Löschen einer Note aus dem Note Browser und Local Storage.
  26. console.log("Delete")
  27. let buttonId = (this.id).split("_")[1];
  28. try {
  29. await removeBrowserItem(buttonId);
  30. } catch(err) {
  31. alert(err);
  32. }
  33. })
  34. $("#refreshButton").on('click', async function() {
  35. // Listener für den Refresh Button zum aktualisieren der gespeicherten Notes.
  36. console.log("Refresh")
  37. createLoadingSign(id="", append="noteStorage");
  38. enableLoadingSign(id="");
  39. await sleep(2000);
  40. disableLoadingSign(id="");
  41. try {
  42. await refreshBrowser();
  43. } catch(err){
  44. alert(err);
  45. }
  46. })
  47. })
  48. let id=1;
  49. function createForm(){
  50. // Erstellt eine NoteForm und initialisiert die OnClick-Listener auf den Buttons der Form
  51. id++;
  52. console.log("createForm() "+id)
  53. let wrapper = document.createElement("div");
  54. wrapper.setAttribute('id', "note"+id);
  55. wrapper.style.margin = '2.5px';
  56. let inputsWrapper = document.createElement("div");
  57. inputsWrapper.setAttribute('class', "noteDiv");
  58. let inputDiv = document.createElement("div");
  59. let input = document.createElement("input");
  60. let deleteButton = document.createElement('button');
  61. input.setAttribute('type', "text");
  62. input.setAttribute('name',"noteTitle");
  63. input.setAttribute('id', "inputTitle"+id);
  64. input.setAttribute('placeholder', "Enter title...");
  65. input.style.marginBottom='5px';
  66. deleteButton.style.marginLeft = '2px';
  67. deleteButton.innerHTML = "X";
  68. deleteButton.setAttribute('id', "deleteForm_" + id);
  69. deleteButton.setAttribute('onClick', "deleteForm(this.id)");
  70. deleteButton.setAttribute('class', "right");
  71. inputDiv.appendChild(input);
  72. inputDiv.appendChild(deleteButton);
  73. let textAreaDiv = document.createElement("div");
  74. let textarea = document.createElement("textarea");
  75. textarea.setAttribute('name',"noteText");
  76. textarea.setAttribute('id', "noteText"+id);
  77. textarea.setAttribute('placeholder', "Enter notes...");
  78. textarea.style.width='300px';
  79. textarea.style.height='150px';
  80. textarea.style.boxSizing='border-box';
  81. textAreaDiv.appendChild(textarea);
  82. let saveNoteButton = document.createElement("button");
  83. saveNoteButton.setAttribute('type',"button");
  84. saveNoteButton.setAttribute('id',"saveNoteButton_"+id);
  85. saveNoteButton.innerHTML = 'Save';
  86. inputsWrapper.appendChild(inputDiv);
  87. inputsWrapper.appendChild(textAreaDiv);
  88. inputsWrapper.appendChild(saveNoteButton);
  89. wrapper.appendChild(inputsWrapper);
  90. document.getElementById('allNotesDiv').appendChild(wrapper);
  91. }
  92. function deleteForm(idName){
  93. // Aktiviert durch OnClickListener auf dem X-Button innerhalb der Form zum Schließen der Form
  94. console.log("deleteForm() " + id);
  95. id = idName.split("_")[1];
  96. document.getElementById("note"+id).remove();
  97. }
  98. function createFormWithSavedContent(item, id){
  99. // Holt eine gespeicherte Form aus dem Speicher und erstellt erneut eine NoteForm und initialisiert die OnClick-Listener auf den Buttons der Form
  100. console.log("createFormWithSavedContent() "+ id)
  101. let wrapper = document.createElement("div")
  102. wrapper.setAttribute('id', "note"+id)
  103. let inputsWrapper = document.createElement("div")
  104. inputsWrapper.setAttribute('class', "noteDiv");
  105. let inputDiv = document.createElement("div");
  106. let input = document.createElement("input");
  107. let deleteButton = document.createElement('button');
  108. input.setAttribute('type', "text");
  109. input.setAttribute('name',"noteTitle");
  110. input.setAttribute('id', "inputTitle"+id);
  111. input.setAttribute('value', item.title);
  112. input.style.marginBottom='5px';
  113. deleteButton.style.marginLeft = '2px';
  114. deleteButton.innerHTML = "X";
  115. deleteButton.setAttribute('id', "deleteForm_" + id);
  116. deleteButton.setAttribute('onClick', "deleteForm(this.id)");
  117. inputDiv.appendChild(input);
  118. inputDiv.appendChild(deleteButton);
  119. let textAreaDiv = document.createElement("div");
  120. let textarea = document.createElement("textarea");
  121. textarea.setAttribute('name',"noteText");
  122. textarea.setAttribute('id', "noteText"+id);
  123. textarea.setAttribute('value', item.text);
  124. textarea.innerHTML = item.text;
  125. textarea.style.width='300px';
  126. textarea.style.height='150px';
  127. textarea.style.boxSizing='border-box';
  128. textAreaDiv.appendChild(textarea);
  129. let editNoteButton = document.createElement("button");
  130. editNoteButton.setAttribute('type',"button");
  131. editNoteButton.setAttribute('id',"editNoteButton_"+id);
  132. editNoteButton.innerHTML = 'Save';
  133. inputsWrapper.appendChild(inputDiv);
  134. inputsWrapper.appendChild(textAreaDiv);
  135. inputsWrapper.appendChild(editNoteButton);
  136. wrapper.appendChild(inputsWrapper);
  137. document.getElementById('allNotesDiv').appendChild(wrapper);
  138. }
  139. let ctr;
  140. async function sleep(ms) {
  141. // Best Practice Variante zum pausieren von Code
  142. // Soll hier Prozesse simulieren die bei synchronem Code zu I/O-Blocking führen würden
  143. console.log("sleep() " + ms);
  144. try {
  145. return new Promise(resolve => setTimeout(resolve, ms));
  146. }
  147. catch (alert) {
  148. return alert(alert);
  149. }
  150. }
  151. async function removeNote(id){
  152. // Löscht eine Note aus dem Note Browser
  153. console.log("removeNote() " + id);
  154. return new Promise((resolve, reject) => {
  155. try {
  156. localStorage.removeItem('noteObj' + id);
  157. removeBrowserItem(id);
  158. resolve();
  159. } catch(err) {
  160. reject(err);
  161. }
  162. });
  163. }
  164. async function saveNote(id){
  165. console.log("saveNote() " + id);
  166. return new Promise((resolve, reject) => {
  167. try {
  168. let noteTitle = document.getElementById('inputTitle' + id).value;
  169. let noteText = document.getElementById('noteText' + id).value;
  170. let noteObj = {
  171. idK: id,
  172. title: noteTitle,
  173. text: noteText
  174. };
  175. let noteObj_serialized = JSON.stringify(noteObj); //stringify noteObj so it is displayed properly when set to local Storage
  176. localStorage.setItem("noteObj" + id, noteObj_serialized);
  177. resolve(noteObj);
  178. } catch(err) {
  179. reject(err);
  180. }
  181. });
  182. }
  183. async function refreshBrowser(){
  184. console.log("refreshBrowser()");
  185. return new Promise((resolve, reject) => {
  186. try {
  187. let noteStorage = [];
  188. let keys = Object.keys(localStorage);
  189. let i = keys.length;
  190. while (i--) {
  191. noteStorage.push(localStorage.getItem(keys[i]));
  192. }
  193. let parentNode = document.getElementById('noteStorage');
  194. parentNode.innerHTML = '';
  195. noteStorage.forEach(element => {
  196. createBrowserItem(JSON.parse(element));
  197. });
  198. resolve();
  199. } catch(err) {
  200. reject(err);
  201. }
  202. });
  203. }
  204. async function saveNoteObject(id, overwrite=false){
  205. console.log("saveNoteObject() " + id + " " + overwrite);
  206. createLoadingSign(id, append="note")
  207. enableLoadingSign(id);
  208. await sleep(10000) //sleep() simulates the delay that would occur when there is a remote server access or similar,
  209. //so instead of sleep would be e.g. saveToRemoteServer()...
  210. //the leftover code of the async function is executed after the delay
  211. disableLoadingSign(id);
  212. try {
  213. if (overwrite == true){
  214. await removeNote(id);
  215. }
  216. let obj = await saveNote(id);
  217. createBrowserItem(obj);
  218. } catch(err){
  219. alert(err)
  220. }
  221. document.getElementById("note"+id).remove();
  222. }
  223. function enableLoadingSign(id){
  224. console.log("enableLoadingSign() " + id);
  225. let target = 'loadSign'+id;
  226. document.getElementById(target).style.display = "block";
  227. }
  228. function disableLoadingSign(id){
  229. console.log("disableLoadingSign() " + id);
  230. let target = 'loadSign'+id;
  231. let sign = document.getElementById(target);
  232. sign.style.display = "none";
  233. sign.parentNode.removeChild(sign);
  234. }
  235. function createLoadingSign(id, append){
  236. console.log("createLoadingSign() " + id);
  237. let target = 'loadSign'+id;
  238. let appendTo = append+id;
  239. let loadingSign = document.createElement('div');
  240. let loadingSignWrapper = document.createElement('div');
  241. loadingSign.setAttribute('class', 'loader')
  242. loadingSign.setAttribute('id', target);
  243. loadingSignWrapper.appendChild(loadingSign);
  244. document.getElementById(appendTo).appendChild(loadingSignWrapper);
  245. }
  246. function createBrowserItem(item){
  247. console.log("createBrowserItem() " + item);
  248. let wrapperDiv = document.createElement('div');
  249. let browserItem = document.createElement('div');
  250. let deleteButton = document.createElement('button');
  251. wrapperDiv.setAttribute('class', 'browserElement');
  252. browserItem.innerHTML = item.title;
  253. deleteButton.style.marginLeft = '2px';
  254. deleteButton.innerHTML = "X";
  255. browserItem.setAttribute('id', "browserItem_"+item.idK);
  256. browserItem.setAttribute('onClick', "getSavedNote(this.id)");
  257. deleteButton.setAttribute('id', "deleteItem_" + item.idK);
  258. wrapperDiv.appendChild(browserItem);
  259. wrapperDiv.appendChild(deleteButton);
  260. document.getElementById('noteStorage').appendChild(wrapperDiv);
  261. }
  262. async function removeBrowserItem(id){
  263. console.log("removeBrowserItem() " + id);
  264. return new Promise((resolve, reject) => {
  265. try {
  266. localStorage.removeItem('noteObj' + id);
  267. document.getElementById("browserItem_" + id).remove();
  268. document.getElementById("deleteItem_" + id).remove();
  269. resolve();
  270. } catch(err) {
  271. reject(err);
  272. }
  273. });
  274. }
  275. async function getItemFromStorage(idTmp){
  276. console.log("getItemFromStorage() " + idTmp);
  277. return new Promise((resolve, reject) => {
  278. try {
  279. let item = JSON.parse(localStorage.getItem('noteObj'+idTmp));
  280. resolve(item);
  281. } catch (err) {
  282. reject(err);
  283. }
  284. });
  285. }
  286. async function getSavedNote(id){
  287. console.log("getSavedNote() "+ id);
  288. createLoadingSign("", "allNotesDiv");
  289. enableLoadingSign("");
  290. await sleep(5000);
  291. disableLoadingSign("");
  292. let idTmp = id.split("_")[1];
  293. try {
  294. let item = await getItemFromStorage(idTmp);
  295. createFormWithSavedContent(item, idTmp);
  296. } catch(err) {
  297. alert(err)
  298. }
  299. }