| 
									
										
										
										
											2017-11-04 19:38:50 -04:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  | const contextMenu = (function() { | 
					
						
							|  |  |  |     const treeEl = $("#tree"); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |     let clipboardIds = []; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |     let clipboardMode = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-19 18:34:21 -05:00
										 |  |  |     async function pasteAfter(node) { | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         if (clipboardMode === 'cut') { | 
					
						
							| 
									
										
										
										
											2018-01-19 18:34:21 -05:00
										 |  |  |             const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey)); | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-19 18:34:21 -05:00
										 |  |  |             await treeChanges.moveAfterNode(nodes, node); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |             clipboardIds = []; | 
					
						
							|  |  |  |             clipboardMode = null; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (clipboardMode === 'copy') { | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |             for (const noteId of clipboardIds) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 await cloning.cloneNoteAfter(noteId, node.data.noteTreeId); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
 | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |         else if (clipboardIds.length === 0) { | 
					
						
							| 
									
										
										
										
											2017-11-28 11:36:32 -05:00
										 |  |  |             // just do nothing
 | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         else { | 
					
						
							| 
									
										
										
										
											2017-12-06 19:53:23 -05:00
										 |  |  |             throwError("Unrecognized clipboard mode=" + clipboardMode); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-19 18:34:21 -05:00
										 |  |  |     async function pasteInto(node) { | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         if (clipboardMode === 'cut') { | 
					
						
							| 
									
										
										
										
											2018-01-19 18:34:21 -05:00
										 |  |  |             const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey)); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-19 18:34:21 -05:00
										 |  |  |             await treeChanges.moveToNode(nodes, node); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |             clipboardIds = []; | 
					
						
							|  |  |  |             clipboardMode = null; | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else if (clipboardMode === 'copy') { | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |             for (const noteId of clipboardIds) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 await cloning.cloneNoteTo(noteId, node.data.noteId); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |             // copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (clipboardIds.length === 0) { | 
					
						
							|  |  |  |             // just do nothing
 | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2017-12-06 19:53:23 -05:00
										 |  |  |             throwError("Unrecognized clipboard mode=" + mode); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |     function copy(nodes) { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |         clipboardIds = nodes.map(node => node.data.noteId); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         clipboardMode = 'copy'; | 
					
						
							| 
									
										
										
										
											2018-01-01 22:33:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         showMessage("Note(s) have been copied into clipboard."); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |     function cut(nodes) { | 
					
						
							|  |  |  |         clipboardIds = nodes.map(node => node.key); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         clipboardMode = 'cut'; | 
					
						
							| 
									
										
										
										
											2018-01-01 22:33:21 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         showMessage("Note(s) have been cut into clipboard."); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |     const contextMenuSettings = { | 
					
						
							|  |  |  |         delegate: "span.fancytree-title", | 
					
						
							|  |  |  |         autoFocus: true, | 
					
						
							|  |  |  |         menu: [ | 
					
						
							| 
									
										
										
										
											2017-12-23 14:35:20 -05:00
										 |  |  |             {title: "Insert note here <kbd>Ctrl+O</kbd>", cmd: "insertNoteHere", uiIcon: "ui-icon-plus"}, | 
					
						
							|  |  |  |             {title: "Insert child note <kbd>Ctrl+P</kbd>", cmd: "insertChildNote", uiIcon: "ui-icon-plus"}, | 
					
						
							|  |  |  |             {title: "Delete <kbd>Ctrl+Del</kbd>", cmd: "delete", uiIcon: "ui-icon-trash"}, | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             {title: "----"}, | 
					
						
							| 
									
										
										
										
											2017-12-23 14:35:20 -05:00
										 |  |  |             {title: "Edit tree prefix <kbd>F2</kbd>", cmd: "editTreePrefix", uiIcon: "ui-icon-pencil"}, | 
					
						
							| 
									
										
										
										
											2017-11-26 22:34:25 -05:00
										 |  |  |             {title: "----"}, | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             {title: "Protect sub-tree", cmd: "protectSubTree", uiIcon: "ui-icon-locked"}, | 
					
						
							|  |  |  |             {title: "Unprotect sub-tree", cmd: "unprotectSubTree", uiIcon: "ui-icon-unlocked"}, | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             {title: "----"}, | 
					
						
							| 
									
										
										
										
											2017-12-23 14:35:20 -05:00
										 |  |  |             {title: "Copy / clone <kbd>Ctrl+C</kbd>", cmd: "copy", uiIcon: "ui-icon-copy"}, | 
					
						
							|  |  |  |             {title: "Cut <kbd>Ctrl+X</kbd>", cmd: "cut", uiIcon: "ui-icon-scissors"}, | 
					
						
							|  |  |  |             {title: "Paste into <kbd>Ctrl+V</kbd>", cmd: "pasteInto", uiIcon: "ui-icon-clipboard"}, | 
					
						
							| 
									
										
										
										
											2017-12-26 10:00:08 -05:00
										 |  |  |             {title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"}, | 
					
						
							|  |  |  |             {title: "----"}, | 
					
						
							| 
									
										
										
										
											2017-12-30 21:44:26 -05:00
										 |  |  |             {title: "Collapse sub-tree <kbd>Alt+-</kbd>", cmd: "collapse-sub-tree", uiIcon: "ui-icon-minus"}, | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  |             {title: "Force note sync", cmd: "force-note-sync", uiIcon: "ui-icon-refresh"}, | 
					
						
							| 
									
										
										
										
											2018-01-19 19:36:41 -05:00
										 |  |  |             {title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sort-alphabetically", uiIcon: " ui-icon-arrowthick-2-n-s"} | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |         ], | 
					
						
							|  |  |  |         beforeOpen: (event, ui) => { | 
					
						
							|  |  |  |             const node = $.ui.fancytree.getNode(ui.target); | 
					
						
							|  |  |  |             // Modify menu entries depending on node status
 | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |             treeEl.contextmenu("enableEntry", "pasteAfter", clipboardIds.length > 0); | 
					
						
							|  |  |  |             treeEl.contextmenu("enableEntry", "pasteInto", clipboardIds.length > 0); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             // Activate node on right-click
 | 
					
						
							|  |  |  |             node.setActive(); | 
					
						
							|  |  |  |             // Disable tree keyboard handling
 | 
					
						
							|  |  |  |             ui.menu.prevKeyboard = node.tree.options.keyboard; | 
					
						
							|  |  |  |             node.tree.options.keyboard = false; | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         close: (event, ui) => {}, | 
					
						
							|  |  |  |         select: (event, ui) => { | 
					
						
							|  |  |  |             const node = $.ui.fancytree.getNode(ui.target); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             if (ui.cmd === "insertNoteHere") { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 const parentNoteId = node.data.parentNoteId; | 
					
						
							| 
									
										
										
										
											2017-11-14 23:01:23 -05:00
										 |  |  |                 const isProtected = treeUtils.getParentProtectedStatus(node); | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |                 noteTree.createNote(node, parentNoteId, 'after', isProtected); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |             else if (ui.cmd === "insertChildNote") { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 noteTree.createNote(node, node.data.noteId, 'into'); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-26 22:34:25 -05:00
										 |  |  |             else if (ui.cmd === "editTreePrefix") { | 
					
						
							|  |  |  |                 editTreePrefix.showDialog(node); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             else if (ui.cmd === "protectSubTree") { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 protected_session.protectSubTree(node.data.noteId, true); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-15 00:04:26 -05:00
										 |  |  |             else if (ui.cmd === "unprotectSubTree") { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 protected_session.protectSubTree(node.data.noteId, false); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |             else if (ui.cmd === "copy") { | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |                 copy(noteTree.getSelectedNodes()); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             else if (ui.cmd === "cut") { | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |                 cut(noteTree.getSelectedNodes()); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |             else if (ui.cmd === "pasteAfter") { | 
					
						
							|  |  |  |                 pasteAfter(node); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (ui.cmd === "pasteInto") { | 
					
						
							|  |  |  |                 pasteInto(node); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             else if (ui.cmd === "delete") { | 
					
						
							| 
									
										
										
										
											2018-01-14 21:39:21 -05:00
										 |  |  |                 treeChanges.deleteNodes(noteTree.getSelectedNodes(true)); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-12-26 10:00:08 -05:00
										 |  |  |             else if (ui.cmd === "collapse-sub-tree") { | 
					
						
							|  |  |  |                 noteTree.collapseTree(node); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-12-30 21:44:26 -05:00
										 |  |  |             else if (ui.cmd === "force-note-sync") { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 forceNoteSync(node.data.noteId); | 
					
						
							| 
									
										
										
										
											2017-12-30 21:44:26 -05:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  |             else if (ui.cmd === "sort-alphabetically") { | 
					
						
							| 
									
										
										
										
											2018-01-28 19:30:14 -05:00
										 |  |  |                 noteTree.sortAlphabetically(node.data.noteId); | 
					
						
							| 
									
										
										
										
											2018-01-13 17:00:40 -05:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             else { | 
					
						
							| 
									
										
										
										
											2017-12-01 22:28:22 -05:00
										 |  |  |                 messaging.logError("Unknown command: " + ui.cmd); | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         pasteAfter, | 
					
						
							|  |  |  |         pasteInto, | 
					
						
							|  |  |  |         cut, | 
					
						
							| 
									
										
										
										
											2017-11-28 11:36:32 -05:00
										 |  |  |         copy, | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  |         contextMenuSettings | 
					
						
							| 
									
										
										
										
											2017-09-09 12:06:15 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-04 19:33:39 -04:00
										 |  |  | })(); |