| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  | const keybindings = { | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     "insert": node => { | 
					
						
							| 
									
										
										
										
											2017-09-04 13:27:54 -04:00
										 |  |  |         const parentKey = getParentKey(node); | 
					
						
							| 
									
										
										
										
											2017-09-08 22:43:02 -04:00
										 |  |  |         const encryption = getParentEncryption(node); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-08 22:43:02 -04:00
										 |  |  |         createNote(node, parentKey, 'after', encryption); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     "ctrl+insert": node => { | 
					
						
							| 
									
										
										
										
											2017-09-08 22:43:02 -04:00
										 |  |  |         createNote(node, node.key, 'into', node.data.encryption); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     "del": node => { | 
					
						
							| 
									
										
										
										
											2017-09-04 13:22:24 -04:00
										 |  |  |         deleteNode(node); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     "shift+up": node => { | 
					
						
							| 
									
										
										
										
											2017-09-04 12:02:34 -04:00
										 |  |  |         const beforeNode = node.getPrevSibling(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (beforeNode !== null) { | 
					
						
							|  |  |  |             moveBeforeNode(node, beforeNode); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     "shift+down": node => { | 
					
						
							| 
									
										
										
										
											2017-09-04 12:02:34 -04:00
										 |  |  |         let afterNode = node.getNextSibling(); | 
					
						
							|  |  |  |         if (afterNode !== null) { | 
					
						
							|  |  |  |             moveAfterNode(node, afterNode); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     "shift+left": node => { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         moveNodeUp(node); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     "shift+right": node => { | 
					
						
							| 
									
										
										
										
											2017-09-04 12:02:34 -04:00
										 |  |  |         let toNode = node.getPrevSibling(); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 12:02:34 -04:00
										 |  |  |         if (toNode !== null) { | 
					
						
							|  |  |  |             moveToNode(node, toNode); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     "return": node => { | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |         // doesn't work :-/
 | 
					
						
							|  |  |  |         $('#noteDetail').summernote('focus'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 18:50:56 -04:00
										 |  |  | let globalAllNoteIds = []; | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | const globalTree = $("#tree"); | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | let globalClipboardNoteId = null; | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | function prepareNoteTree(notes) { | 
					
						
							|  |  |  |     for (const note of notes) { | 
					
						
							|  |  |  |         globalAllNoteIds.push(note.note_id); | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         if (note.encryption > 0) { | 
					
						
							|  |  |  |             note.title = "[encrypted]"; | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             note.extraClasses = "encrypted"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             note.title = note.note_title; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (note.is_clone) { | 
					
						
							|  |  |  |                 note.title += " (clone)"; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         note.key = note.note_id; | 
					
						
							|  |  |  |         note.expanded = note.is_expanded; | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         if (note.children && note.children.length > 0) { | 
					
						
							|  |  |  |             prepareNoteTree(note.children); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | function setExpandedToServer(note_id, is_expanded) { | 
					
						
							|  |  |  |     expanded_num = is_expanded ? 1 : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $.ajax({ | 
					
						
							| 
									
										
										
										
											2017-09-30 10:05:12 -04:00
										 |  |  |         url: baseApiUrl + 'notes/' + note_id + '/expanded/' + expanded_num, | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         type: 'PUT', | 
					
						
							|  |  |  |         contentType: "application/json", | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |         success: result => {} | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-04 13:22:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-12 21:43:24 -04:00
										 |  |  | let globalEncryptionSalt; | 
					
						
							| 
									
										
										
										
											2017-09-12 23:04:17 -04:00
										 |  |  | let globalEncryptionSessionTimeout; | 
					
						
							| 
									
										
										
										
											2017-09-16 23:21:46 -04:00
										 |  |  | let globalEncryptedDataKey; | 
					
						
							| 
									
										
										
										
											2017-09-29 00:17:28 -04:00
										 |  |  | let globalFullLoadTime; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | setInterval(() => { | 
					
						
							| 
									
										
										
										
											2017-09-30 19:45:57 -04:00
										 |  |  |     $.ajax({ | 
					
						
							|  |  |  |         url: baseApiUrl + 'audit/' + globalFullLoadTime, | 
					
						
							|  |  |  |         type: 'GET', | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |         success: resp => { | 
					
						
							| 
									
										
										
										
											2017-09-30 19:45:57 -04:00
										 |  |  |             if (resp.changed) { | 
					
						
							|  |  |  |                 window.location.reload(true); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         statusCode: { | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |             401: () => { | 
					
						
							| 
									
										
										
										
											2017-09-30 19:45:57 -04:00
										 |  |  |                 // if the user got logged out then we should display the page
 | 
					
						
							|  |  |  |                 // here we do that by reloading which will force the redirect if the user is really logged out
 | 
					
						
							|  |  |  |                 window.location.reload(true); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-09-29 00:17:28 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-09-30 19:45:57 -04:00
										 |  |  | }, 10 * 1000); | 
					
						
							| 
									
										
										
										
											2017-09-12 21:43:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $(() => { | 
					
						
							| 
									
										
										
										
											2017-09-30 10:05:12 -04:00
										 |  |  |     $.get(baseApiUrl + 'tree').then(resp => { | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  |         const notes = resp.notes; | 
					
						
							| 
									
										
										
										
											2017-08-22 21:32:03 -04:00
										 |  |  |         let startNoteId = resp.start_note_id; | 
					
						
							| 
									
										
										
										
											2017-09-16 23:21:46 -04:00
										 |  |  |         globalEncryptionSalt = resp.password_derived_key_salt; | 
					
						
							| 
									
										
										
										
											2017-09-12 23:04:17 -04:00
										 |  |  |         globalEncryptionSessionTimeout = resp.encryption_session_timeout; | 
					
						
							| 
									
										
										
										
											2017-09-16 23:21:46 -04:00
										 |  |  |         globalEncryptedDataKey = resp.encrypted_data_key; | 
					
						
							| 
									
										
										
										
											2017-09-29 00:17:28 -04:00
										 |  |  |         globalFullLoadTime = resp.full_load_time; | 
					
						
							| 
									
										
										
										
											2017-08-22 21:32:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-28 23:16:36 -04:00
										 |  |  |         // add browser ID header to all AJAX requests
 | 
					
						
							|  |  |  |         $.ajaxSetup({ | 
					
						
							|  |  |  |             headers: { 'x-browser-id': resp.browser_id } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-22 21:32:03 -04:00
										 |  |  |         if (document.location.hash) { | 
					
						
							|  |  |  |             startNoteId = document.location.hash.substr(1); // strip initial #
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         prepareNoteTree(notes); | 
					
						
							| 
									
										
										
										
											2017-09-08 23:14:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 15:08:17 -04:00
										 |  |  |         globalTree.fancytree({ | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  |             autoScroll: true, | 
					
						
							| 
									
										
										
										
											2017-09-04 12:02:34 -04:00
										 |  |  |             extensions: ["hotkeys", "filter", "dnd"], | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             source: notes, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |             activate: (event, data) => { | 
					
						
							| 
									
										
										
										
											2017-08-15 21:29:12 -04:00
										 |  |  |                 const node = data.node.data; | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 21:43:02 -04:00
										 |  |  |                 saveNoteIfChanged(() => loadNote(node.note_id)); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |             expand: (event, data) => { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |                 setExpandedToServer(data.node.key, true); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |             collapse: (event, data) => { | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |                 setExpandedToServer(data.node.key, false); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |             init: (event, data) => { | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  |                 if (startNoteId) { | 
					
						
							|  |  |  |                     data.tree.activateKey(startNoteId); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-09-05 23:48:41 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $(window).resize(); | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             hotkeys: { | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |                 keydown: keybindings | 
					
						
							| 
									
										
										
										
											2017-08-28 23:10:04 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             filter: { | 
					
						
							|  |  |  |                 autoApply: true,   // Re-apply last filter if lazy data is loaded
 | 
					
						
							|  |  |  |                 autoExpand: true, // Expand all branches that contain matches while filtered
 | 
					
						
							|  |  |  |                 counter: false,     // Show a badge with number of matching child nodes near parent icons
 | 
					
						
							|  |  |  |                 fuzzy: false,      // Match single characters in order, e.g. 'fb' will match 'FooBar'
 | 
					
						
							|  |  |  |                 hideExpandedCounter: true,  // Hide counter badge if parent is expanded
 | 
					
						
							|  |  |  |                 hideExpanders: false,       // Hide expanders if all child nodes are hidden by filter
 | 
					
						
							|  |  |  |                 highlight: true,   // Highlight matches by wrapping inside <mark> tags
 | 
					
						
							|  |  |  |                 leavesOnly: false, // Match end nodes only
 | 
					
						
							|  |  |  |                 nodata: true,      // Display a 'no data' status node if result is empty
 | 
					
						
							|  |  |  |                 mode: "hide"       // Grayout unmatched nodes (pass "hide" to remove unmatched node instead)
 | 
					
						
							| 
									
										
										
										
											2017-09-04 12:02:34 -04:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |             dnd: dragAndDropSetup | 
					
						
							| 
									
										
										
										
											2017-09-04 13:22:24 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         globalTree.contextmenu(contextMenuSetup); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-28 23:10:04 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 20:58:53 -04:00
										 |  |  | function collapseTree() { | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |     globalTree.fancytree("getRootNode").visit(node => { | 
					
						
							| 
									
										
										
										
											2017-08-29 20:58:53 -04:00
										 |  |  |         node.setExpanded(false); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  | $(document).bind('keydown', 'alt+c', collapseTree); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 11:37:50 -04:00
										 |  |  | function scrollToCurrentNote() { | 
					
						
							|  |  |  |     const node = getNodeByKey(globalCurrentNote.detail.note_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (node) { | 
					
						
							|  |  |  |         node.makeVisible({scrollIntoView: true}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         node.setFocus(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  | function showSearch() { | 
					
						
							| 
									
										
										
										
											2017-10-01 08:47:26 -04:00
										 |  |  |     $("#search-box").show(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $("input[name=search]").focus(); | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $(document).bind('keydown', 'alt+s', showSearch); | 
					
						
							| 
									
										
										
										
											2017-10-01 08:47:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | function toggleSearch() { | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  |     if ($("#search-box:hidden").length) { | 
					
						
							|  |  |  |         showSearch(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							|  |  |  |         resetSearch(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $("#search-box").hide(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-01 08:47:26 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  | function resetSearch() { | 
					
						
							|  |  |  |     $("input[name=search]").val(""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const tree = globalTree.fancytree("getTree"); | 
					
						
							|  |  |  |     tree.clearFilter(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $("button#btnResetSearch").click(resetSearch); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $("input[name=search]").keyup(e => { | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  |     const searchString = $(this).val(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchString) === "") { | 
					
						
							|  |  |  |         $("button#btnResetSearch").click(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (e && e.which === $.ui.keyCode.ENTER) { | 
					
						
							|  |  |  |         $.get(baseApiUrl + 'notes?search=' + searchString).then(resp => { | 
					
						
							|  |  |  |             console.log("search: ", resp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Pass a string to perform case insensitive matching
 | 
					
						
							|  |  |  |             const tree = globalTree.fancytree("getTree"); | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  |             tree.filterBranches(node => { | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  |                 return resp.includes(node.data.note_id); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }).focus(); |