| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  | const keybindings = { | 
					
						
							|  |  |  |     "insert": function(node) { | 
					
						
							|  |  |  |         let parentKey = (node.getParent() === null || node.getParent().key === "root_1") ? "root" : node.getParent().key; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         createNote(node, parentKey, 'after'); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "ctrl+insert": function(node) { | 
					
						
							|  |  |  |         createNote(node, node.key, 'into'); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "del": function(node) { | 
					
						
							|  |  |  |         if (confirm('Are you sure you want to delete note "' + node.title + '"?')) { | 
					
						
							|  |  |  |             $.ajax({ | 
					
						
							|  |  |  |                 url: baseUrl + 'notes/' + node.key, | 
					
						
							|  |  |  |                 type: 'DELETE', | 
					
						
							|  |  |  |                 success: function() { | 
					
						
							|  |  |  |                     if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { | 
					
						
							|  |  |  |                         node.getParent().folder = false; | 
					
						
							|  |  |  |                         node.getParent().renderTitle(); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 10:30:32 -04:00
										 |  |  |                     delete globalNoteNames[node.key]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     // remove from recent notes
 | 
					
						
							|  |  |  |                     recentNotes = recentNotes.filter(note => note !== node.key); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 10:33:12 -04:00
										 |  |  |                     let next = node.getNextSibling(); | 
					
						
							|  |  |  |                     if (!next) { | 
					
						
							|  |  |  |                         next = node.getParent(); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |                     node.remove(); | 
					
						
							| 
									
										
										
										
											2017-08-27 10:33:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     // activate next element after this one is deleted so we don't lose focus
 | 
					
						
							|  |  |  |                     next.setActive(); | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "shift+up": function(node) { | 
					
						
							|  |  |  |         if (node.getPrevSibling() !== null) { | 
					
						
							|  |  |  |             $.ajax({ | 
					
						
							|  |  |  |                 url: baseUrl + 'notes/' + node.key + '/moveBefore/' + node.getPrevSibling().key, | 
					
						
							|  |  |  |                 type: 'PUT', | 
					
						
							|  |  |  |                 contentType: "application/json", | 
					
						
							|  |  |  |                 success: function() { | 
					
						
							|  |  |  |                     node.moveTo(node.getPrevSibling(), 'before'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "shift+down": function(node) { | 
					
						
							|  |  |  |         if (node.getNextSibling() !== null) { | 
					
						
							|  |  |  |             $.ajax({ | 
					
						
							|  |  |  |                 url: baseUrl + 'notes/' + node.key + '/moveAfter/' + node.getNextSibling().key, | 
					
						
							|  |  |  |                 type: 'PUT', | 
					
						
							|  |  |  |                 contentType: "application/json", | 
					
						
							|  |  |  |                 success: function() { | 
					
						
							|  |  |  |                     node.moveTo(node.getNextSibling(), 'after'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "shift+left": function(node) { | 
					
						
							|  |  |  |         if (node.getParent() !== null) { | 
					
						
							|  |  |  |             $.ajax({ | 
					
						
							|  |  |  |                 url: baseUrl + 'notes/' + node.key + '/moveAfter/' + node.getParent().key, | 
					
						
							|  |  |  |                 type: 'PUT', | 
					
						
							|  |  |  |                 contentType: "application/json", | 
					
						
							|  |  |  |                 success: function() { | 
					
						
							|  |  |  |                     if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { | 
					
						
							|  |  |  |                         node.getParent().folder = false; | 
					
						
							|  |  |  |                         node.getParent().renderTitle(); | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     node.moveTo(node.getParent(), 'after'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "shift+right": function(node) { | 
					
						
							|  |  |  |         let prevSibling = node.getPrevSibling(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (prevSibling !== null) { | 
					
						
							|  |  |  |             $.ajax({ | 
					
						
							|  |  |  |                 url: baseUrl + 'notes/' + node.key + '/moveTo/' + prevSibling.key, | 
					
						
							|  |  |  |                 type: 'PUT', | 
					
						
							|  |  |  |                 contentType: "application/json", | 
					
						
							|  |  |  |                 success: function(result) { | 
					
						
							|  |  |  |                     node.moveTo(prevSibling); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     prevSibling.setExpanded(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     prevSibling.folder = true; | 
					
						
							|  |  |  |                     prevSibling.renderTitle(); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     "return": function(node) { | 
					
						
							|  |  |  |         // doesn't work :-/
 | 
					
						
							|  |  |  |         $('#noteDetail').summernote('focus'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 10:30:32 -04:00
										 |  |  | const globalNoteNames = {}; | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | $(function(){ | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  |     $.get(baseUrl + 'tree').then(resp => { | 
					
						
							|  |  |  |         const notes = resp.notes; | 
					
						
							| 
									
										
										
										
											2017-08-22 21:32:03 -04:00
										 |  |  |         let startNoteId = resp.start_note_id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (document.location.hash) { | 
					
						
							|  |  |  |             startNoteId = document.location.hash.substr(1); // strip initial #
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         function copyTitle(notes) { | 
					
						
							|  |  |  |             for (let note of notes) { | 
					
						
							| 
									
										
										
										
											2017-08-27 10:30:32 -04:00
										 |  |  |                 globalNoteNames[note.note_id] = note.note_title; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                 note.title = note.note_title; | 
					
						
							| 
									
										
										
										
											2017-08-15 21:29:12 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 if (note.is_clone) { | 
					
						
							|  |  |  |                     note.title += " (clone)"; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                 note.key = note.note_id; | 
					
						
							|  |  |  |                 note.expanded = note.is_expanded; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (note.children && note.children.length > 0) { | 
					
						
							|  |  |  |                     copyTitle(note.children); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         copyTitle(notes); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function setExpanded(note_id, is_expanded) { | 
					
						
							|  |  |  |             expanded_num = is_expanded ? 1 : 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $.ajax({ | 
					
						
							|  |  |  |                 url: baseUrl + 'notes/' + note_id + '/expanded/' + expanded_num, | 
					
						
							|  |  |  |                 type: 'PUT', | 
					
						
							|  |  |  |                 contentType: "application/json", | 
					
						
							| 
									
										
										
										
											2017-08-23 20:09:28 -04:00
										 |  |  |                 success: function(result) {} | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $("#tree").fancytree({ | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  |             autoScroll: true, | 
					
						
							| 
									
										
										
										
											2017-08-28 23:10:04 -04:00
										 |  |  |             extensions: ["hotkeys", "filter"], | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             source: notes, | 
					
						
							|  |  |  |             activate: function(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
										 |  |  |             }, | 
					
						
							|  |  |  |             expand: function(event, data) { | 
					
						
							|  |  |  |                 setExpanded(data.node.key, true); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             collapse: function(event, data) { | 
					
						
							|  |  |  |                 setExpanded(data.node.key, false); | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  |             init: function(event, data) { | 
					
						
							|  |  |  |                 if (startNoteId) { | 
					
						
							|  |  |  |                     data.tree.activateKey(startNoteId); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							| 
									
										
										
										
											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-06-11 16:04:07 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2017-08-28 23:10:04 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $("input[name=search]").keyup(function (e) { | 
					
						
							|  |  |  |     let match = $(this).val(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(match) === "") { | 
					
						
							|  |  |  |         $("button#btnResetSearch").click(); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Pass a string to perform case insensitive matching
 | 
					
						
							|  |  |  |     let tree = $("#tree").fancytree("getTree"); | 
					
						
							|  |  |  |     tree.filterBranches(match); | 
					
						
							|  |  |  | }).focus(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $("button#btnResetSearch").click(function () { | 
					
						
							|  |  |  |     $("input[name=search]").val(""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     let tree = $("#tree").fancytree("getTree"); | 
					
						
							|  |  |  |     tree.clearFilter(); | 
					
						
							| 
									
										
										
										
											2017-08-29 20:58:53 -04:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function collapseTree() { | 
					
						
							|  |  |  |     $("#tree").fancytree("getRootNode").visit(function(node){ | 
					
						
							|  |  |  |         node.setExpanded(false); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | $(document).bind('keydown', 'alt+c', collapseTree); |