| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | const noteTree = (function() { | 
					
						
							|  |  |  |     const treeEl = $("#tree"); | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |     const parentListEl = $("#parent-list"); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:35:44 -05:00
										 |  |  |     const parentListListEl = $("#parent-list-list"); | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 23:15:53 -05:00
										 |  |  |     let startNotePath = null; | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |     let notesTreeMap = {}; | 
					
						
							| 
									
										
										
										
											2017-11-23 19:29:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     let parentToChildren = {}; | 
					
						
							|  |  |  |     let childToParents = {}; | 
					
						
							| 
									
										
										
										
											2017-11-23 19:29:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |     let parentChildToNoteTreeId = {}; | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |     let noteIdToTitle = {}; | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |     function getNoteTreeId(parentNoteId, childNoteId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(parentNoteId, childNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |         const key = parentNoteId + "-" + childNoteId; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |         // this can return undefined and client code should deal with it somehow
 | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |         return parentChildToNoteTreeId[key]; | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |     function getNoteTitle(noteId, parentNoteId = null) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |         let title = noteIdToTitle[noteId]; | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!title) { | 
					
						
							| 
									
										
										
										
											2017-12-06 19:53:23 -05:00
										 |  |  |             throwError("Can't find title for noteId='" + noteId + "'"); | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |         if (parentNoteId !== null) { | 
					
						
							|  |  |  |             const noteTreeId = getNoteTreeId(parentNoteId, noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (noteTreeId) { | 
					
						
							|  |  |  |                 const noteTree = notesTreeMap[noteTreeId]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (noteTree.prefix) { | 
					
						
							|  |  |  |                     title = noteTree.prefix + ' - ' + title; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |         return title; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |     // note that if you want to access data like note_id or is_protected, you need to go into "data" property
 | 
					
						
							|  |  |  |     function getCurrentNode() { | 
					
						
							|  |  |  |         return treeEl.fancytree("getActiveNode"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getCurrentNotePath() { | 
					
						
							|  |  |  |         const node = getCurrentNode(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return treeUtils.getNotePath(node); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getCurrentNoteId() { | 
					
						
							|  |  |  |         const node = getCurrentNode(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return node ? node.data.note_id : null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getCurrentClones() { | 
					
						
							|  |  |  |         const noteId = getCurrentNoteId(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (noteId) { | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  |             return getNodesByNoteId(noteId); | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             return []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  |     function getNodesByNoteTreeId(noteTreeId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteTreeId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  |         const noteTree = notesTreeMap[noteTreeId]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return getNodesByNoteId(noteTree.note_id).filter(node => node.data.note_tree_id === noteTreeId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getNodesByNoteId(noteId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 22:26:40 -05:00
										 |  |  |         const list = getTree().getNodesByRef(noteId); | 
					
						
							|  |  |  |         return list ? list : []; // if no nodes with this refKey are found, fancy tree returns null
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  |     function setPrefix(noteTreeId, prefix) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteTreeId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  |         notesTreeMap[noteTreeId].prefix = prefix; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         getNodesByNoteTreeId(noteTreeId).map(node => { | 
					
						
							|  |  |  |             node.data.prefix = prefix; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             treeUtils.setNodeTitleWithPrefix(node); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 16:28:13 -05:00
										 |  |  |     function removeParentChildRelation(parentNoteId, childNoteId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(parentNoteId, childNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 16:28:13 -05:00
										 |  |  |         const key = parentNoteId + "-" + childNoteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         delete parentChildToNoteTreeId[key]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parentToChildren[parentNoteId] = parentToChildren[parentNoteId].filter(noteId => noteId !== childNoteId); | 
					
						
							|  |  |  |         childToParents[childNoteId] = childToParents[childNoteId].filter(noteId => noteId !== parentNoteId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 21:53:21 -05:00
										 |  |  |     function setParentChildRelation(noteTreeId, parentNoteId, childNoteId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteTreeId, parentNoteId, childNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 21:53:21 -05:00
										 |  |  |         const key = parentNoteId + "-" + childNoteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parentChildToNoteTreeId[key] = noteTreeId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!parentToChildren[parentNoteId]) { | 
					
						
							|  |  |  |             parentToChildren[parentNoteId] = []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parentToChildren[parentNoteId].push(childNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!childToParents[childNoteId]) { | 
					
						
							|  |  |  |             childToParents[childNoteId] = []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         childToParents[childNoteId].push(parentNoteId); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |     function prepareNoteTree(notes) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(notes); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |         parentToChildren = {}; | 
					
						
							|  |  |  |         childToParents = {}; | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |         notesTreeMap = {}; | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for (const note of notes) { | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |             notesTreeMap[note.note_tree_id] = note; | 
					
						
							| 
									
										
										
										
											2017-11-18 18:57:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |             noteIdToTitle[note.note_id] = note.note_title; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 19:58:56 -05:00
										 |  |  |             delete note.note_title; // this should not be used. Use noteIdToTitle instead
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             setParentChildRelation(note.note_tree_id, note.parent_note_id, note.note_id); | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |         return prepareNoteTreeInner('root'); | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |     function getExtraClasses(note) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(note); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |         let extraClasses = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (note.is_protected) { | 
					
						
							|  |  |  |             extraClasses += ",protected"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (childToParents[note.note_id].length > 1) { | 
					
						
							|  |  |  |             extraClasses += ",multiple-parents"; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (extraClasses.startsWith(",")) { | 
					
						
							|  |  |  |             extraClasses = extraClasses.substr(1); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return extraClasses; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |     function prepareNoteTreeInner(parentNoteId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(parentNoteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |         const childNoteIds = parentToChildren[parentNoteId]; | 
					
						
							| 
									
										
										
										
											2017-11-19 16:43:49 -05:00
										 |  |  |         if (!childNoteIds) { | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  |             messaging.logError("No children for " + parentNoteId + ". This shouldn't happen."); | 
					
						
							| 
									
										
										
										
											2017-11-19 16:43:49 -05:00
										 |  |  |             return; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  |         const noteList = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |         for (const noteId of childNoteIds) { | 
					
						
							|  |  |  |             const noteTreeId = getNoteTreeId(parentNoteId, noteId); | 
					
						
							|  |  |  |             const noteTree = notesTreeMap[noteTreeId]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const node = { | 
					
						
							|  |  |  |                 note_id: noteTree.note_id, | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |                 parent_note_id: noteTree.parent_note_id, | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |                 note_tree_id: noteTree.note_tree_id, | 
					
						
							|  |  |  |                 is_protected: noteTree.is_protected, | 
					
						
							| 
									
										
										
										
											2017-11-26 22:40:14 -05:00
										 |  |  |                 prefix: noteTree.prefix, | 
					
						
							| 
									
										
										
										
											2017-11-26 22:34:25 -05:00
										 |  |  |                 title: (noteTree.prefix ? (noteTree.prefix + " - ") : "") + noteIdToTitle[noteTree.note_id], | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |                 extraClasses: getExtraClasses(noteTree), | 
					
						
							|  |  |  |                 refKey: noteTree.note_id, | 
					
						
							|  |  |  |                 expanded: noteTree.is_expanded | 
					
						
							|  |  |  |             }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (parentToChildren[noteId] && parentToChildren[noteId].length > 0) { | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |                 node.folder = true; | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |                 if (node.expanded) { | 
					
						
							| 
									
										
										
										
											2017-11-23 20:12:39 -05:00
										 |  |  |                     node.children = prepareNoteTreeInner(noteId); | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |                     node.lazy = true; | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |             noteList.push(node); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return noteList; | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     async function activateNode(notePath) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  |         const runPath = getRunPath(notePath); | 
					
						
							|  |  |  |         const noteId = treeUtils.getNoteIdFromNotePath(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let parentNoteId = 'root'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 22:06:24 -05:00
										 |  |  |         //console.log(now(), "Run path: ", runPath);
 | 
					
						
							| 
									
										
										
										
											2017-12-09 21:53:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  |         for (const childNoteId of runPath) { | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             const node = getNodesByNoteId(childNoteId).find(node => node.data.parent_note_id === parentNoteId); | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (childNoteId === noteId) { | 
					
						
							|  |  |  |                 await node.setActive(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 await node.setExpanded(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             parentNoteId = childNoteId; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Accepts notePath and tries to resolve it. Part of the path might not be valid because of note moving (which causes | 
					
						
							|  |  |  |      * path change) or other corruption, in that case this will try to get some other valid path to the correct note. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     function getRunPath(notePath) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |         const path = notePath.split("/").reverse(); | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  |         path.push('root'); | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const effectivePath = []; | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |         let childNoteId = null; | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |         let i = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (true) { | 
					
						
							| 
									
										
										
										
											2017-11-30 00:02:32 -05:00
										 |  |  |             if (i >= path.length) { | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const parentNoteId = path[i++]; | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (childNoteId !== null) { | 
					
						
							|  |  |  |                 const parents = childToParents[childNoteId]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-26 21:00:42 -05:00
										 |  |  |                 if (!parents) { | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  |                     messaging.logError("No parents found for " + childNoteId); | 
					
						
							| 
									
										
										
										
											2017-11-26 21:00:42 -05:00
										 |  |  |                     return; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 00:02:32 -05:00
										 |  |  |                 if (!parents.includes(parentNoteId)) { | 
					
						
							| 
									
										
										
										
											2017-12-18 22:06:24 -05:00
										 |  |  |                     console.log(now(), "Did not find parent " + parentNoteId + " for child " + childNoteId); | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     if (parents.length > 0) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |                         console.log(now(), "Available parents:", parents); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 00:02:32 -05:00
										 |  |  |                         const pathToRoot = getSomeNotePath(parents[0]).split("/").reverse(); | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 00:02:32 -05:00
										 |  |  |                         for (const noteId of pathToRoot) { | 
					
						
							|  |  |  |                             effectivePath.push(noteId); | 
					
						
							|  |  |  |                         } | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 00:02:32 -05:00
										 |  |  |                         break; | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |                     } | 
					
						
							|  |  |  |                     else { | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  |                         messaging.logError("No parents, can't activate node."); | 
					
						
							| 
									
										
										
										
											2017-11-19 11:28:46 -05:00
										 |  |  |                         return; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  |             if (parentNoteId === 'root') { | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  |                 effectivePath.push(parentNoteId); | 
					
						
							|  |  |  |                 childNoteId = parentNoteId; | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-12-03 17:46:56 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return effectivePath.reverse(); | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |     function showParentList(noteId, node) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteId, node); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |         const parents = childToParents[noteId]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         if (!parents) { | 
					
						
							| 
									
										
										
										
											2017-12-06 19:53:23 -05:00
										 |  |  |             throwError("Can't find parents for noteId=" + noteId); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |         if (parents.length <= 1) { | 
					
						
							|  |  |  |             parentListEl.hide(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             parentListEl.show(); | 
					
						
							| 
									
										
										
										
											2017-12-16 21:35:44 -05:00
										 |  |  |             parentListListEl.empty(); | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             for (const parentNoteId of parents) { | 
					
						
							| 
									
										
										
										
											2017-12-06 20:11:45 -05:00
										 |  |  |                 const parentNotePath = getSomeNotePath(parentNoteId); | 
					
						
							|  |  |  |                 // this is to avoid having root notes leading '/'
 | 
					
						
							|  |  |  |                 const notePath = parentNotePath ? (parentNotePath + '/' + noteId) : noteId; | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |                 const title = getNotePathTitle(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 let item; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (node.getParent().data.note_id === parentNoteId) { | 
					
						
							|  |  |  |                     item = $("<span/>").attr("title", "Current note").append(title); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     item = link.createNoteLink(notePath, title); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-16 21:35:44 -05:00
										 |  |  |                 parentListListEl.append($("<li/>").append(item)); | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getNotePathTitle(notePath) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(notePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |         const titlePath = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |         let parentNoteId = 'root'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const noteId of notePath.split('/')) { | 
					
						
							|  |  |  |             titlePath.push(getNoteTitle(noteId, parentNoteId)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             parentNoteId = noteId; | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return titlePath.join(' / '); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function getSomeNotePath(noteId) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |         const path = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let cur = noteId; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while (cur !== 'root') { | 
					
						
							|  |  |  |             path.push(cur); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             if (!childToParents[cur]) { | 
					
						
							|  |  |  |                 throwError("Can't find parents for " + cur); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  |             cur = childToParents[cur][0]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return path.reverse().join('/'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |     async function setExpandedToServer(noteTreeId, isExpanded) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteTreeId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |         const expandedNum = isExpanded ? 1 : 0; | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |         await server.put('notes/' + noteTreeId + '/expanded/' + expandedNum); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |     function setCurrentNotePathToHash(node) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(node); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |         const currentNotePath = treeUtils.getNotePath(node); | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |         const currentNoteTreeId = node.data.note_tree_id; | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         document.location.hash = currentNotePath; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |         recentNotes.addRecentNote(currentNoteTreeId, currentNotePath); | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |     function initFancyTree(noteTree) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteTree); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |         const keybindings = { | 
					
						
							|  |  |  |             "del": node => { | 
					
						
							| 
									
										
										
										
											2017-11-04 22:10:41 -04:00
										 |  |  |                 treeChanges.deleteNode(node); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             "shift+up": node => { | 
					
						
							|  |  |  |                 const beforeNode = node.getPrevSibling(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (beforeNode !== null) { | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |                     treeChanges.moveBeforeNode(node, beforeNode); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             "shift+down": node => { | 
					
						
							|  |  |  |                 let afterNode = node.getNextSibling(); | 
					
						
							|  |  |  |                 if (afterNode !== null) { | 
					
						
							| 
									
										
										
										
											2017-12-03 10:06:53 -05:00
										 |  |  |                     treeChanges.moveAfterNode(node, afterNode); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |                 } | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             "shift+left": node => { | 
					
						
							| 
									
										
										
										
											2017-11-28 15:17:11 -05:00
										 |  |  |                 treeChanges.moveNodeUpInHierarchy(node); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             "shift+right": node => { | 
					
						
							|  |  |  |                 let toNode = node.getPrevSibling(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (toNode !== null) { | 
					
						
							| 
									
										
										
										
											2017-11-04 22:10:41 -04:00
										 |  |  |                     treeChanges.moveToNode(node, toNode); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-11-26 23:10:23 -05:00
										 |  |  |             }, | 
					
						
							|  |  |  |             "f2": node => { | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  |                 editTreePrefix.showDialog(node); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         treeEl.fancytree({ | 
					
						
							|  |  |  |             autoScroll: true, | 
					
						
							| 
									
										
										
										
											2017-11-22 19:40:06 -05:00
										 |  |  |             extensions: ["hotkeys", "filter", "dnd", "clones"], | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |             source: noteTree, | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             scrollParent: $("#tree"), | 
					
						
							|  |  |  |             activate: (event, data) => { | 
					
						
							|  |  |  |                 const node = data.node.data; | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |                 setCurrentNotePathToHash(data.node); | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 10:06:49 -05:00
										 |  |  |                 noteEditor.switchToNote(node.note_id); | 
					
						
							| 
									
										
										
										
											2017-11-21 20:04:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 showParentList(node.note_id, data.node); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             expand: (event, data) => { | 
					
						
							| 
									
										
										
										
											2017-11-22 20:29:22 -05:00
										 |  |  |                 setExpandedToServer(data.node.data.note_tree_id, true); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             collapse: (event, data) => { | 
					
						
							| 
									
										
										
										
											2017-11-22 20:29:22 -05:00
										 |  |  |                 setExpandedToServer(data.node.data.note_tree_id, false); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             init: (event, data) => { | 
					
						
							| 
									
										
										
										
											2017-12-15 21:36:21 -05:00
										 |  |  |                 const noteId = treeUtils.getNoteIdFromNotePath(startNotePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (noteIdToTitle[noteId] === undefined) { | 
					
						
							|  |  |  |                     // note doesn't exist so don't try to activate it
 | 
					
						
							|  |  |  |                     startNotePath = null; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-06 23:15:53 -05:00
										 |  |  |                 if (startNotePath) { | 
					
						
							|  |  |  |                     activateNode(startNotePath); | 
					
						
							| 
									
										
										
										
											2017-12-21 22:00:44 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                     // looks like this this doesn't work when triggered immediatelly after activating node
 | 
					
						
							|  |  |  |                     // so waiting a second helps
 | 
					
						
							|  |  |  |                     setTimeout(scrollToCurrentNote, 1000); | 
					
						
							| 
									
										
										
										
											2017-11-19 08:47:22 -05:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-11-19 16:35:35 -05:00
										 |  |  |                 else { | 
					
						
							|  |  |  |                     showAppIfHidden(); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             }, | 
					
						
							|  |  |  |             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
 | 
					
						
							| 
									
										
										
										
											2017-11-28 11:36:32 -05:00
										 |  |  |                     case 67: | 
					
						
							|  |  |  |                         if (event.ctrlKey) { // Ctrl-C
 | 
					
						
							|  |  |  |                             contextMenu.copy(node); | 
					
						
							| 
									
										
										
										
											2017-12-23 08:24:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                             showMessage("Note copied into clipboard."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |                             return false; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 88: | 
					
						
							|  |  |  |                         if (event.ctrlKey) { // Ctrl-X
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |                             contextMenu.cut(node); | 
					
						
							| 
									
										
										
										
											2017-12-23 08:24:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |                             showMessage("Note cut into clipboard."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             return false; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                     case 86: | 
					
						
							|  |  |  |                         if (event.ctrlKey) { // Ctrl-V
 | 
					
						
							|  |  |  |                             contextMenu.pasteInto(node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                             showMessage("Note pasted from clipboard into current note."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |                             return false; | 
					
						
							|  |  |  |                         } | 
					
						
							|  |  |  |                         break; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  |             }, | 
					
						
							|  |  |  |             lazyLoad: function(event, data){ | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |                 const node = data.node.data; | 
					
						
							| 
									
										
										
										
											2017-11-17 21:31:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 16:43:49 -05:00
										 |  |  |                 data.result = prepareNoteTreeInner(node.note_id); | 
					
						
							| 
									
										
										
										
											2017-11-22 19:40:06 -05:00
										 |  |  |             }, | 
					
						
							|  |  |  |             clones: { | 
					
						
							|  |  |  |                 highlightActiveClones: true | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |         treeEl.contextmenu(contextMenu.contextMenuSettings); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 19:58:56 -05:00
										 |  |  |     function getTree() { | 
					
						
							|  |  |  |         return treeEl.fancytree('getTree'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-05 09:52:28 -05:00
										 |  |  |     async function reload() { | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |         const notes = await loadTree(); | 
					
						
							| 
									
										
										
										
											2017-11-05 09:52:28 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // this will also reload the note content
 | 
					
						
							| 
									
										
										
										
											2017-11-22 19:58:56 -05:00
										 |  |  |         await getTree().reload(notes); | 
					
						
							| 
									
										
										
										
											2017-11-05 09:52:28 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 19:31:02 -05:00
										 |  |  |     function getNotePathFromAddress() { | 
					
						
							|  |  |  |         return document.location.hash.substr(1); // strip initial #
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |     function loadTree() { | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |         return server.get('tree').then(resp => { | 
					
						
							| 
									
										
										
										
											2017-12-06 23:15:53 -05:00
										 |  |  |             startNotePath = resp.start_note_path; | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (document.location.hash) { | 
					
						
							| 
									
										
										
										
											2017-12-19 19:31:02 -05:00
										 |  |  |                 startNotePath = getNotePathFromAddress(); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 20:44:06 -05:00
										 |  |  |             return prepareNoteTree(resp.notes); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |     $(() => loadTree().then(noteTree => initFancyTree(noteTree))); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function collapseTree() { | 
					
						
							|  |  |  |         treeEl.fancytree("getRootNode").visit(node => { | 
					
						
							|  |  |  |             node.setExpanded(false); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $(document).bind('keydown', 'alt+c', collapseTree); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function scrollToCurrentNote() { | 
					
						
							| 
									
										
										
										
											2017-12-21 22:00:44 -05:00
										 |  |  |         const node = getCurrentNode(); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (node) { | 
					
						
							|  |  |  |             node.makeVisible({scrollIntoView: true}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             node.setFocus(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |     function setCurrentNoteTreeBasedOnProtectedStatus() { | 
					
						
							| 
									
										
										
										
											2017-11-22 20:29:22 -05:00
										 |  |  |         getCurrentClones().map(node => node.toggleClass("protected", !!node.data.is_protected)); | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 19:39:39 -05:00
										 |  |  |     function getAutocompleteItems(parentNoteId, notePath, titlePath) { | 
					
						
							|  |  |  |         if (!parentNoteId) { | 
					
						
							|  |  |  |             parentNoteId = 'root'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!parentToChildren[parentNoteId]) { | 
					
						
							|  |  |  |             return []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!notePath) { | 
					
						
							|  |  |  |             notePath = ''; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!titlePath) { | 
					
						
							|  |  |  |             titlePath = ''; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const autocompleteItems = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         for (const childNoteId of parentToChildren[parentNoteId]) { | 
					
						
							|  |  |  |             const childNotePath = (notePath ? (notePath + '/') : '') + childNoteId; | 
					
						
							| 
									
										
										
										
											2017-11-29 22:03:03 -05:00
										 |  |  |             const childTitlePath = (titlePath ? (titlePath + ' / ') : '') + getNoteTitle(childNoteId, parentNoteId); | 
					
						
							| 
									
										
										
										
											2017-11-19 19:39:39 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             autocompleteItems.push({ | 
					
						
							| 
									
										
										
										
											2017-11-19 20:36:13 -05:00
										 |  |  |                 value: childTitlePath + ' (' + childNotePath + ')', | 
					
						
							| 
									
										
										
										
											2017-11-19 19:39:39 -05:00
										 |  |  |                 label: childTitlePath | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             const childItems = getAutocompleteItems(childNoteId, childNotePath, childTitlePath); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             for (const childItem of childItems) { | 
					
						
							|  |  |  |                 autocompleteItems.push(childItem); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return autocompleteItems; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 19:38:33 -05:00
										 |  |  |     function setNoteTitle(noteId, title) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(noteId); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 19:38:33 -05:00
										 |  |  |         noteIdToTitle[noteId] = title; | 
					
						
							| 
									
										
										
										
											2017-11-22 19:58:56 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 19:38:33 -05:00
										 |  |  |         getNodesByNoteId(noteId).map(clone => treeUtils.setNodeTitleWithPrefix(clone)); | 
					
						
							| 
									
										
										
										
											2017-11-22 19:58:56 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 19:40:47 -05:00
										 |  |  |     async function createNewTopLevelNote() { | 
					
						
							|  |  |  |         const rootNode = treeEl.fancytree("getRootNode"); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 19:40:47 -05:00
										 |  |  |         await createNote(rootNode, "root", "into"); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function createNote(node, parentNoteId, target, isProtected) { | 
					
						
							| 
									
										
										
										
											2017-12-23 11:02:38 -05:00
										 |  |  |         assertArguments(node, parentNoteId, target); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         // if isProtected 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 (!isProtected || !protected_session.isProtectedSessionAvailable()) { | 
					
						
							|  |  |  |             isProtected = false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const newNoteName = "new note"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 20:52:38 -05:00
										 |  |  |         const result = await server.post('notes/' + parentNoteId + '/children', { | 
					
						
							|  |  |  |             note_title: newNoteName, | 
					
						
							|  |  |  |             target: target, | 
					
						
							|  |  |  |             target_note_tree_id: node.data.note_tree_id, | 
					
						
							|  |  |  |             is_protected: isProtected | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const newNode = { | 
					
						
							|  |  |  |             title: newNoteName, | 
					
						
							|  |  |  |             note_id: result.note_id, | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |             parent_note_id: parentNoteId, | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |             refKey: result.note_id, | 
					
						
							|  |  |  |             note_tree_id: result.note_tree_id, | 
					
						
							|  |  |  |             is_protected: isProtected, | 
					
						
							|  |  |  |             extraClasses: isProtected ? "protected" : "" | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-09 21:53:21 -05:00
										 |  |  |         setParentChildRelation(result.note_tree_id, parentNoteId, result.note_id); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-12 22:20:03 -05:00
										 |  |  |         notesTreeMap[result.note_tree_id] = result; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         noteIdToTitle[result.note_id] = newNoteName; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 19:40:47 -05:00
										 |  |  |         noteEditor.newNoteCreated(); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (target === 'after') { | 
					
						
							|  |  |  |             node.appendSibling(newNode).setActive(true); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             node.addChildren(newNode).setActive(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             node.folder = true; | 
					
						
							|  |  |  |             node.renderTitle(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         showMessage("Created!"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  |     $(document).bind('keydown', 'ctrl+o', e => { | 
					
						
							|  |  |  |         console.log("pressed O"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const node = getCurrentNode(); | 
					
						
							| 
									
										
										
										
											2017-12-19 21:40:48 -05:00
										 |  |  |         const parentNoteId = node.data.parent_note_id; | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  |         const isProtected = treeUtils.getParentProtectedStatus(node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         createNote(node, parentNoteId, 'after', isProtected); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $(document).bind('keydown', 'ctrl+p', e => { | 
					
						
							| 
									
										
										
										
											2017-12-17 16:40:38 -05:00
										 |  |  |         const node = getCurrentNode(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         createNote(node, node.data.note_id, 'into', node.data.is_protected); | 
					
						
							| 
									
										
										
										
											2017-12-18 23:41:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $(document).bind('keydown', 'ctrl+del', e => { | 
					
						
							|  |  |  |         const node = getCurrentNode(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         treeChanges.deleteNode(node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         e.preventDefault(); | 
					
						
							| 
									
										
										
										
											2017-12-17 16:40:38 -05:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-17 16:56:30 -05:00
										 |  |  |     $(document).bind('keydown', 'ctrl+.', scrollToCurrentNote); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 19:31:02 -05:00
										 |  |  |     $(window).bind('hashchange', function() { | 
					
						
							|  |  |  |         const notePath = getNotePathFromAddress(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         activateNode(notePath); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-19 19:54:55 -05:00
										 |  |  |     if (isElectron()) { | 
					
						
							|  |  |  |         $(document).bind('keydown', 'alt+left', e => { | 
					
						
							|  |  |  |             window.history.back(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             e.preventDefault(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $(document).bind('keydown', 'alt+right', e => { | 
					
						
							|  |  |  |             window.history.forward(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             e.preventDefault(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |     return { | 
					
						
							| 
									
										
										
										
											2017-11-05 09:52:28 -05:00
										 |  |  |         reload, | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |         collapseTree, | 
					
						
							|  |  |  |         scrollToCurrentNote, | 
					
						
							| 
									
										
										
										
											2017-11-18 17:05:50 -05:00
										 |  |  |         setCurrentNoteTreeBasedOnProtectedStatus, | 
					
						
							|  |  |  |         getCurrentNode, | 
					
						
							| 
									
										
										
										
											2017-11-19 12:06:48 -05:00
										 |  |  |         activateNode, | 
					
						
							|  |  |  |         getCurrentNotePath, | 
					
						
							| 
									
										
										
										
											2017-11-19 18:16:50 -05:00
										 |  |  |         getNoteTitle, | 
					
						
							| 
									
										
										
										
											2017-11-19 19:39:39 -05:00
										 |  |  |         setCurrentNotePathToHash, | 
					
						
							| 
									
										
										
										
											2017-11-22 19:58:56 -05:00
										 |  |  |         getAutocompleteItems, | 
					
						
							| 
									
										
										
										
											2017-11-28 19:38:33 -05:00
										 |  |  |         setNoteTitle, | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         createNewTopLevelNote, | 
					
						
							| 
									
										
										
										
											2017-11-28 10:17:30 -05:00
										 |  |  |         createNote, | 
					
						
							|  |  |  |         setPrefix, | 
					
						
							| 
									
										
										
										
											2017-12-17 16:28:13 -05:00
										 |  |  |         getNotePathTitle, | 
					
						
							|  |  |  |         removeParentChildRelation, | 
					
						
							|  |  |  |         setParentChildRelation | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | })(); |