| 
									
										
										
										
											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-11-04 17:54:27 -04:00
										 |  |  |         noteEditor.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-11-04 17:54:27 -04:00
										 |  |  |         noteEditor.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 :-/
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |         $('#note-detail').summernote('focus'); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  | glob.allNoteIds = []; | 
					
						
							|  |  |  | glob.tree = $("#tree"); | 
					
						
							|  |  |  | glob.clipboardNoteId = 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) { | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  |         glob.allNoteIds.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-11-04 00:05:08 -04:00
										 |  |  | glob.treeLoadTime; | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | function initFancyTree(notes, startNoteId) { | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  |     glob.tree.fancytree({ | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |         autoScroll: true, | 
					
						
							|  |  |  |         extensions: ["hotkeys", "filter", "dnd"], | 
					
						
							|  |  |  |         source: notes, | 
					
						
							|  |  |  |         scrollParent: $("#tree"), | 
					
						
							|  |  |  |         activate: (event, data) => { | 
					
						
							|  |  |  |             const node = data.node.data; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 17:54:27 -04:00
										 |  |  |             noteEditor.saveNoteIfChanged().then(() => noteEditor.loadNoteToEditor(node.note_id)); | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |         }, | 
					
						
							|  |  |  |         expand: (event, data) => { | 
					
						
							|  |  |  |             setExpandedToServer(data.node.key, true); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         collapse: (event, data) => { | 
					
						
							|  |  |  |             setExpandedToServer(data.node.key, false); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         init: (event, data) => { | 
					
						
							|  |  |  |             if (startNoteId) { | 
					
						
							|  |  |  |                 data.tree.activateKey(startNoteId); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-09-29 00:17:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  |             $(window).resize(); | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         hotkeys: { | 
					
						
							|  |  |  |             keydown: keybindings | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         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)
 | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         dnd: dragAndDropSetup, | 
					
						
							|  |  |  |         keydown: (event, data) => { | 
					
						
							|  |  |  |             const node = data.node; | 
					
						
							|  |  |  |             // Eat keyboard events, when a menu is open
 | 
					
						
							|  |  |  |             if ($(".contextMenu:visible").length > 0) | 
					
						
							|  |  |  |                 return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             switch (event.which) { | 
					
						
							|  |  |  |                 // Open context menu on [Space] key (simulate right click)
 | 
					
						
							|  |  |  |                 case 32: // [Space]
 | 
					
						
							|  |  |  |                     $(node.span).trigger("mousedown", { | 
					
						
							|  |  |  |                         preventDefault: true, | 
					
						
							|  |  |  |                         button: 2 | 
					
						
							|  |  |  |                     }) | 
					
						
							|  |  |  |                         .trigger("mouseup", { | 
					
						
							|  |  |  |                             preventDefault: true, | 
					
						
							|  |  |  |                             pageX: node.span.offsetLeft, | 
					
						
							|  |  |  |                             pageY: node.span.offsetTop, | 
					
						
							|  |  |  |                             button: 2 | 
					
						
							|  |  |  |                         }); | 
					
						
							|  |  |  |                     return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // Handle Ctrl-C, -X and -V
 | 
					
						
							|  |  |  |                 // case 67:
 | 
					
						
							|  |  |  |                 //     if (event.ctrlKey) { // Ctrl-C
 | 
					
						
							|  |  |  |                 //         copyPaste("copy", node);
 | 
					
						
							|  |  |  |                 //         return false;
 | 
					
						
							|  |  |  |                 //     }
 | 
					
						
							|  |  |  |                 //     break;
 | 
					
						
							|  |  |  |                 case 86: | 
					
						
							|  |  |  |                     console.log("CTRL-V"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (event.ctrlKey) { // Ctrl-V
 | 
					
						
							|  |  |  |                         pasteAfter(node); | 
					
						
							|  |  |  |                         return false; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 case 88: | 
					
						
							|  |  |  |                     console.log("CTRL-X"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     if (event.ctrlKey) { // Ctrl-X
 | 
					
						
							|  |  |  |                         cut(node); | 
					
						
							|  |  |  |                         return false; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  |     glob.tree.contextmenu(contextMenuSetup); | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function loadTree() { | 
					
						
							|  |  |  |     return $.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-11-04 18:18:55 -04:00
										 |  |  |         encryption.setEncryptionSalt(resp.password_derived_key_salt); | 
					
						
							|  |  |  |         encryption.setEncryptionSessionTimeout(resp.encryption_session_timeout); | 
					
						
							|  |  |  |         encryption.setEncryptedDataKey(resp.encrypted_data_key); | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  |         glob.treeLoadTime = resp.tree_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-11-01 22:36:26 -04:00
										 |  |  |         return { | 
					
						
							|  |  |  |             notes: notes, | 
					
						
							|  |  |  |             startNoteId: startNoteId | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-09-04 13:22:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-01 22:36:26 -04:00
										 |  |  | $(() => { | 
					
						
							|  |  |  |     loadTree().then(resp => { | 
					
						
							|  |  |  |        initFancyTree(resp.notes, resp.startNoteId); | 
					
						
							| 
									
										
										
										
											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-11-04 00:05:08 -04:00
										 |  |  |     glob.tree.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() { | 
					
						
							| 
									
										
										
										
											2017-11-04 17:54:27 -04:00
										 |  |  |     const node = getNodeByKey(noteEditor.getCurrentNoteId()); | 
					
						
							| 
									
										
										
										
											2017-09-16 11:37:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     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(""); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  |     const tree = glob.tree.fancytree("getTree"); | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  |     tree.clearFilter(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  | $("button#reset-search-button").click(resetSearch); | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-09 18:53:11 -04:00
										 |  |  | $("input[name=search]").keyup(e => { | 
					
						
							| 
									
										
										
										
											2017-10-15 20:20:07 -04:00
										 |  |  |     const searchString = $("input[name=search]").val(); | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchString) === "") { | 
					
						
							| 
									
										
										
										
											2017-10-10 20:19:16 -04:00
										 |  |  |         $("button#reset-search-button").click(); | 
					
						
							| 
									
										
										
										
											2017-10-01 09:49:50 -04:00
										 |  |  |         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
 | 
					
						
							| 
									
										
										
										
											2017-11-04 00:05:08 -04:00
										 |  |  |             const tree = glob.tree.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(); |