| 
									
										
										
										
											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) { | 
					
						
							|  |  |  |                 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", | 
					
						
							|  |  |  |                 success: function(result) { | 
					
						
							|  |  |  |                  | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $("#tree").fancytree({ | 
					
						
							| 
									
										
										
										
											2017-08-22 21:23:10 -04:00
										 |  |  |             autoScroll: true, | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |             extensions: ["hotkeys"], | 
					
						
							|  |  |  |             source: notes, | 
					
						
							|  |  |  |             activate: function(event, data){ | 
					
						
							| 
									
										
										
										
											2017-08-15 21:29:12 -04:00
										 |  |  |                 const node = data.node.data; | 
					
						
							|  |  |  |                 const noteId = node.is_clone ? node.note_clone_id : node.note_id; | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 20:41:41 -04:00
										 |  |  |                 saveNoteIfChanged(() => loadNote(noteId)); | 
					
						
							| 
									
										
										
										
											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: { | 
					
						
							|  |  |  |                 keydown: { | 
					
						
							|  |  |  |                     "insert": function(node) { | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                         let parentKey = (node.getParent() === null || node.getParent().key === "root_1") ? "root" : node.getParent().key; | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         createNote(node, parentKey, 'after'); | 
					
						
							|  |  |  |                     }, | 
					
						
							| 
									
										
										
										
											2017-08-23 20:05:54 -04:00
										 |  |  |                     "ctrl+insert": function(node) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                         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(result) { | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                                 if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                                     node.getParent().folder = false; | 
					
						
							|  |  |  |                                     node.getParent().renderTitle(); | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                                 node.remove(); | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                             }); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     "shift+up": function(node) { | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                         if (node.getPrevSibling() !== null) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                             $.ajax({ | 
					
						
							|  |  |  |                                 url: baseUrl + 'notes/' + node.key + '/moveBefore/' + node.getPrevSibling().key, | 
					
						
							|  |  |  |                                 type: 'PUT', | 
					
						
							|  |  |  |                                 contentType: "application/json", | 
					
						
							|  |  |  |                                 success: function(result) { | 
					
						
							|  |  |  |                                 node.moveTo(node.getPrevSibling(), 'before'); | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                             }); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     "shift+down": function(node) { | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                         if (node.getNextSibling() !== null) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                             $.ajax({ | 
					
						
							|  |  |  |                                 url: baseUrl + 'notes/' + node.key + '/moveAfter/' + node.getNextSibling().key, | 
					
						
							|  |  |  |                                 type: 'PUT', | 
					
						
							|  |  |  |                                 contentType: "application/json", | 
					
						
							|  |  |  |                                 success: function(result) { | 
					
						
							|  |  |  |                                 node.moveTo(node.getNextSibling(), 'after'); | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                             }); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     "shift+left": function(node) { | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                         if (node.getParent() !== null) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                             $.ajax({ | 
					
						
							|  |  |  |                                 url: baseUrl + 'notes/' + node.key + '/moveAfter/' + node.getParent().key, | 
					
						
							|  |  |  |                                 type: 'PUT', | 
					
						
							|  |  |  |                                 contentType: "application/json", | 
					
						
							|  |  |  |                                 success: function(result) { | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                                 if (node.getParent() !== null && node.getParent().getChildren().length <= 1) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                                     node.getParent().folder = false; | 
					
						
							|  |  |  |                                     node.getParent().renderTitle(); | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                                 node.moveTo(node.getParent(), 'after'); | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                             }); | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                     "shift+right": function(node) { | 
					
						
							|  |  |  |                         let prevSibling = node.getPrevSibling(); | 
					
						
							|  |  |  |                          | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                         if (prevSibling !== null) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                             $.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(); | 
					
						
							|  |  |  |                                 } | 
					
						
							|  |  |  |                             }); | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2017-08-13 21:42:10 -04:00
										 |  |  |                     }, | 
					
						
							|  |  |  |                     "return": function(node) { | 
					
						
							|  |  |  |                         // doesn't work :-/
 | 
					
						
							|  |  |  |                         $('#noteDetail').summernote('focus'); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | }); |