| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  | const tags = { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     1: "<b>", | 
					
						
							|  |  |  |     2: "</b>", | 
					
						
							|  |  |  |     3: "<i>", | 
					
						
							|  |  |  |     4: "</i>", | 
					
						
							|  |  |  |     5: "<u>", | 
					
						
							|  |  |  |     6: "</u>", | 
					
						
							|  |  |  |     9: "<s>", | 
					
						
							|  |  |  |     10: "</s>" | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | let noteChangeDisabled = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  | let isNoteChanged = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | function noteChanged() { | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  |     if (noteChangeDisabled) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  |     isNoteChanged = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | function saveNoteIfChanged(callback) { | 
					
						
							|  |  |  |     if (!isNoteChanged) { | 
					
						
							|  |  |  |         if (callback) { | 
					
						
							|  |  |  |             callback(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |     const note = globalCurrentNote; | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     updateNoteFromInputs(note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-05 23:48:41 -04:00
										 |  |  |     encryptNoteIfNecessary(note); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     saveNoteToServer(note, callback); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  | setInterval(saveNoteIfChanged, 5000); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $(document).ready(() => { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     $("#note-title").on('input', () => { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         noteChanged(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     $('#note-detail').summernote({ | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         airMode: true, | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  |         height: 300, | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         callbacks: { | 
					
						
							|  |  |  |             onChange: noteChanged | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-25 19:30:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // so that tab jumps from note title (which has tabindex 1)
 | 
					
						
							|  |  |  |     $(".note-editable").attr("tabindex", 2); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 19:47:46 -04:00
										 |  |  | function parseHtml(contents, note) { | 
					
						
							| 
									
										
										
										
											2017-09-27 00:04:50 -04:00
										 |  |  |     note.formatting = []; | 
					
						
							|  |  |  |     note.links = []; | 
					
						
							|  |  |  |     note.images = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     note.detail.note_text = contents; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!note.detail.encryption) { | 
					
						
							|  |  |  |         const linkRegexp = /<a[^>]+?href="[^"]*kapp#([A-Za-z0-9]{22})"[^>]*?>[^<]+?<\/a>/g; | 
					
						
							|  |  |  |         let match; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (match = linkRegexp.exec(contents)) { | 
					
						
							|  |  |  |             console.log("adding link for " + match[1]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             note.links.push({ | 
					
						
							|  |  |  |                 note_id: note.detail.note_id, | 
					
						
							|  |  |  |                 target_note_id: match[1] | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | function updateNoteFromInputs(note) { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     let contents = $('#note-detail').summernote('code'); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 19:47:46 -04:00
										 |  |  |     parseHtml(contents, note); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |     let title = $('#note-title').val(); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     getNodeByKey(note.detail.note_id).setTitle(title); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     note.detail.note_title = title; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function saveNoteToServer(note, callback) { | 
					
						
							|  |  |  |     $.ajax({ | 
					
						
							| 
									
										
										
										
											2017-09-30 10:05:12 -04:00
										 |  |  |         url: baseApiUrl + 'notes/' + note.detail.note_id, | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         type: 'PUT', | 
					
						
							|  |  |  |         data: JSON.stringify(note), | 
					
						
							|  |  |  |         contentType: "application/json", | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |         success: () => { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             isNoteChanged = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             message("Saved!"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (callback) { | 
					
						
							|  |  |  |                 callback(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |         error: () => { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             error("Error saving the note!"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | let globalCurrentNote; | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | function createNewTopLevelNote() { | 
					
						
							| 
									
										
										
										
											2017-09-06 21:34:54 -04:00
										 |  |  |     let rootNode = globalTree.fancytree("getRootNode"); | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     createNote(rootNode, "root", "into"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 22:36:56 -04:00
										 |  |  | let newNoteCreated = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 22:43:02 -04:00
										 |  |  | function createNote(node, parentKey, target, encryption) { | 
					
						
							|  |  |  |     // if encryption isn't available (user didn't enter password yet), then note is created as unencrypted
 | 
					
						
							|  |  |  |     // but this is quite weird since user doesn't see where the note is being created so it shouldn't occur often
 | 
					
						
							|  |  |  |     if (!encryption || !isEncryptionAvailable()) { | 
					
						
							|  |  |  |         encryption = 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const newNoteName = "new note"; | 
					
						
							|  |  |  |     const newNoteNameEncryptedIfNecessary = encryption > 0 ? encryptString(newNoteName) : newNoteName; | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     $.ajax({ | 
					
						
							| 
									
										
										
										
											2017-09-30 10:05:12 -04:00
										 |  |  |         url: baseApiUrl + 'notes/' + parentKey + '/children' , | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         type: 'POST', | 
					
						
							|  |  |  |         data: JSON.stringify({ | 
					
						
							| 
									
										
										
										
											2017-09-08 22:43:02 -04:00
										 |  |  |             note_title: newNoteNameEncryptedIfNecessary, | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             target: target, | 
					
						
							| 
									
										
										
										
											2017-09-08 22:43:02 -04:00
										 |  |  |             target_note_id: node.key, | 
					
						
							|  |  |  |             encryption: encryption | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         }), | 
					
						
							|  |  |  |         contentType: "application/json", | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |         success: result => { | 
					
						
							| 
									
										
										
										
											2017-09-08 23:14:42 -04:00
										 |  |  |             const newNode = { | 
					
						
							| 
									
										
										
										
											2017-09-08 22:43:02 -04:00
										 |  |  |                 title: newNoteName, | 
					
						
							|  |  |  |                 key: result.note_id, | 
					
						
							|  |  |  |                 note_id: result.note_id, | 
					
						
							| 
									
										
										
										
											2017-09-08 23:14:42 -04:00
										 |  |  |                 encryption: encryption, | 
					
						
							|  |  |  |                 extraClasses: encryption ? "encrypted" : "" | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 18:50:56 -04:00
										 |  |  |             globalAllNoteIds.push(result.note_id); | 
					
						
							| 
									
										
										
										
											2017-08-27 10:30:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 22:36:56 -04:00
										 |  |  |             newNoteCreated = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |             if (target === 'after') { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                 node.appendSibling(newNode).setActive(true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 node.addChildren(newNode).setActive(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 node.folder = true; | 
					
						
							|  |  |  |                 node.renderTitle(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             message("Created!"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-17 12:46:14 -04:00
										 |  |  | function setTreeBasedOnEncryption(note) { | 
					
						
							|  |  |  |     const node = getNodeByKey(note.detail.note_id); | 
					
						
							|  |  |  |     node.toggleClass("encrypted", note.detail.encryption > 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  | function setNoteBackgroundIfEncrypted(note) { | 
					
						
							|  |  |  |     if (note.detail.encryption > 0) { | 
					
						
							|  |  |  |         $(".note-editable").addClass("encrypted"); | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |         $("#encrypt-button").hide(); | 
					
						
							|  |  |  |         $("#decrypt-button").show(); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         $(".note-editable").removeClass("encrypted"); | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |         $("#encrypt-button").show(); | 
					
						
							|  |  |  |         $("#decrypt-button").hide(); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-08 23:41:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-17 12:46:14 -04:00
										 |  |  |     setTreeBasedOnEncryption(note); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | function loadNote(noteId) { | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     $.get(baseApiUrl + 'notes/' + noteId).then(note => { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         globalCurrentNote = note; | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 22:36:56 -04:00
										 |  |  |         if (newNoteCreated) { | 
					
						
							|  |  |  |             newNoteCreated = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |             $("#note-title").focus().select(); | 
					
						
							| 
									
										
										
										
											2017-06-13 22:36:56 -04:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-05 23:51:50 -04:00
										 |  |  |         handleEncryption(note.detail.encryption > 0, false, () => { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |             $("#note-detail-wrapper").show(); | 
					
						
							| 
									
										
										
										
											2017-09-06 22:03:53 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             // this may fal if the dialog has not been previously opened
 | 
					
						
							| 
									
										
										
										
											2017-09-05 23:48:41 -04:00
										 |  |  |             try { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |                 $("#encryption-password-dialog").dialog('close'); | 
					
						
							| 
									
										
										
										
											2017-09-05 23:48:41 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |             catch(e) {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |             $("#encryption-password").val(''); | 
					
						
							| 
									
										
										
										
											2017-09-05 23:48:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-06 23:13:39 -04:00
										 |  |  |             decryptNoteIfNecessary(note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |             $("#note-title").val(note.detail.note_title); | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             noteChangeDisabled = true; | 
					
						
							| 
									
										
										
										
											2017-06-11 21:04:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             // Clear contents and remove all stored history. This is to prevent undo from going across notes
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |             $('#note-detail').summernote('reset'); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:32:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |             $('#note-detail').summernote('code', note.detail.note_text); | 
					
						
							| 
									
										
										
										
											2017-08-15 22:32:30 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             document.location.hash = noteId; | 
					
						
							| 
									
										
										
										
											2017-08-22 22:40:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |             $(window).resize(); // to trigger resizing of editor
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             addRecentNote(noteId, note.detail.note_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             noteChangeDisabled = false; | 
					
						
							| 
									
										
										
										
											2017-09-04 21:37:55 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             setNoteBackgroundIfEncrypted(note); | 
					
						
							| 
									
										
										
										
											2017-09-04 21:28:07 -04:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | } |