| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import treeService from './tree.js'; | 
					
						
							|  |  |  | import cloningService from './cloning.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | import exportService from './export.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  | import messagingService from './messaging.js'; | 
					
						
							|  |  |  | import protectedSessionService from './protected_session.js'; | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  | import treeChangesService from './branches.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | import treeUtils from './tree_utils.js'; | 
					
						
							| 
									
										
										
										
											2018-04-01 20:50:58 -04:00
										 |  |  | import branchPrefixDialog from '../dialogs/branch_prefix.js'; | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  | import infoService from "./info.js"; | 
					
						
							| 
									
										
										
										
											2018-03-25 22:37:02 -04:00
										 |  |  | import treeCache from "./tree_cache.js"; | 
					
						
							| 
									
										
										
										
											2018-04-06 18:46:29 -04:00
										 |  |  | import syncService from "./sync.js"; | 
					
						
							| 
									
										
										
										
											2017-11-04 19:28:49 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | const $tree = $("#tree"); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | let clipboardIds = []; | 
					
						
							|  |  |  | let clipboardMode = null; | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function pasteAfter(node) { | 
					
						
							|  |  |  |     if (clipboardMode === 'cut') { | 
					
						
							|  |  |  |         const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey)); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |         await treeChangesService.moveAfterNode(nodes, node); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         clipboardIds = []; | 
					
						
							|  |  |  |         clipboardMode = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (clipboardMode === 'copy') { | 
					
						
							|  |  |  |         for (const noteId of clipboardIds) { | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             await cloningService.cloneNoteAfter(noteId, node.data.branchId); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04: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-04 19:33:39 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |         infoService.throwError("Unrecognized clipboard mode=" + clipboardMode); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-10-15 20:55:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | async function pasteInto(node) { | 
					
						
							|  |  |  |     if (clipboardMode === 'cut') { | 
					
						
							|  |  |  |         const nodes = clipboardIds.map(nodeKey => treeUtils.getNodeByKey(nodeKey)); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |         await treeChangesService.moveToNode(nodes, node); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         clipboardIds = []; | 
					
						
							|  |  |  |         clipboardMode = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (clipboardMode === 'copy') { | 
					
						
							|  |  |  |         for (const noteId of clipboardIds) { | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             await cloningService.cloneNoteTo(noteId, node.data.noteId); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |         // copy will keep clipboardIds and clipboardMode so it's possible to paste into multiple places
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else if (clipboardIds.length === 0) { | 
					
						
							|  |  |  |         // just do nothing
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |         infoService.throwError("Unrecognized clipboard mode=" + mode); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function copy(nodes) { | 
					
						
							|  |  |  |     clipboardIds = nodes.map(node => node.data.noteId); | 
					
						
							|  |  |  |     clipboardMode = 'copy'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |     infoService.showMessage("Note(s) have been copied into clipboard."); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function cut(nodes) { | 
					
						
							|  |  |  |     clipboardIds = nodes.map(node => node.key); | 
					
						
							|  |  |  |     clipboardMode = 'cut'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-25 21:29:35 -04:00
										 |  |  |     infoService.showMessage("Note(s) have been cut into clipboard."); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-01 17:41:28 -04:00
										 |  |  | const contextMenuOptions = { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |     delegate: "span.fancytree-title", | 
					
						
							|  |  |  |     autoFocus: true, | 
					
						
							|  |  |  |     menu: [ | 
					
						
							|  |  |  |         {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"}, | 
					
						
							|  |  |  |         {title: "----"}, | 
					
						
							| 
									
										
										
										
											2018-04-01 20:50:58 -04:00
										 |  |  |         {title: "Edit branch prefix <kbd>F2</kbd>", cmd: "editBranchPrefix", uiIcon: "ui-icon-pencil"}, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         {title: "----"}, | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |         {title: "Protect branch", cmd: "protectBranch", uiIcon: "ui-icon-locked"}, | 
					
						
							|  |  |  |         {title: "Unprotect branch", cmd: "unprotectBranch", uiIcon: "ui-icon-unlocked"}, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         {title: "----"}, | 
					
						
							|  |  |  |         {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"}, | 
					
						
							|  |  |  |         {title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"}, | 
					
						
							|  |  |  |         {title: "----"}, | 
					
						
							| 
									
										
										
										
											2018-08-28 15:59:37 +02:00
										 |  |  |         {title: "Export subtree", cmd: "exportSubTree", uiIcon: " ui-icon-arrowthick-1-ne", children: [ | 
					
						
							|  |  |  |             {title: "Native Tar", cmd: "exportSubTreeToTar"}, | 
					
						
							|  |  |  |             {title: "OPML", cmd: "exportSubTreeToOpml"} | 
					
						
							| 
									
										
										
										
											2018-05-27 12:26:34 -04:00
										 |  |  |         ]}, | 
					
						
							| 
									
										
										
										
											2018-05-29 20:32:13 -04:00
										 |  |  |         {title: "Import into branch (tar, opml)", cmd: "importBranch", uiIcon: "ui-icon-arrowthick-1-sw"}, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         {title: "----"}, | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |         {title: "Collapse branch <kbd>Alt+-</kbd>", cmd: "collapseBranch", uiIcon: "ui-icon-minus"}, | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         {title: "Force note sync", cmd: "forceNoteSync", uiIcon: "ui-icon-refresh"}, | 
					
						
							|  |  |  |         {title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sortAlphabetically", uiIcon: " ui-icon-arrowthick-2-n-s"} | 
					
						
							|  |  |  |     ], | 
					
						
							| 
									
										
										
										
											2018-03-25 14:49:20 -04:00
										 |  |  |     beforeOpen: async (event, ui) => { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         const node = $.ui.fancytree.getNode(ui.target); | 
					
						
							| 
									
										
										
										
											2018-04-06 18:49:37 -04:00
										 |  |  |         const branch = await treeCache.getBranch(node.data.branchId); | 
					
						
							| 
									
										
										
										
											2018-03-25 22:37:02 -04:00
										 |  |  |         const note = await treeCache.getNote(node.data.noteId); | 
					
						
							|  |  |  |         const parentNote = await treeCache.getNote(branch.parentNoteId); | 
					
						
							| 
									
										
										
										
											2018-05-26 16:16:34 -04:00
										 |  |  |         const isNotRoot = note.noteId !== 'root'; | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Modify menu entries depending on node status
 | 
					
						
							| 
									
										
										
										
											2018-05-26 16:16:34 -04:00
										 |  |  |         $tree.contextmenu("enableEntry", "insertNoteHere", isNotRoot && parentNote.type !== 'search'); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         $tree.contextmenu("enableEntry", "insertChildNote", note.type !== 'search'); | 
					
						
							| 
									
										
										
										
											2018-06-04 23:21:45 -04:00
										 |  |  |         $tree.contextmenu("enableEntry", "delete", isNotRoot && parentNote.type !== 'search'); | 
					
						
							| 
									
										
										
										
											2018-05-26 16:16:34 -04:00
										 |  |  |         $tree.contextmenu("enableEntry", "copy", isNotRoot); | 
					
						
							|  |  |  |         $tree.contextmenu("enableEntry", "cut", isNotRoot); | 
					
						
							|  |  |  |         $tree.contextmenu("enableEntry", "pasteAfter", clipboardIds.length > 0 && isNotRoot && parentNote.type !== 'search'); | 
					
						
							|  |  |  |         $tree.contextmenu("enableEntry", "pasteInto", clipboardIds.length > 0 && note.type !== 'search'); | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |         $tree.contextmenu("enableEntry", "importBranch", note.type !== 'search'); | 
					
						
							| 
									
										
										
										
											2018-08-28 15:59:37 +02:00
										 |  |  |         $tree.contextmenu("enableEntry", "exportSubTree", note.type !== 'search'); | 
					
						
							| 
									
										
										
										
											2018-07-26 08:58:20 +02:00
										 |  |  |         $tree.contextmenu("enableEntry", "editBranchPrefix", isNotRoot && parentNote.type !== 'search'); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (ui.cmd === "insertNoteHere") { | 
					
						
							|  |  |  |             const parentNoteId = node.data.parentNoteId; | 
					
						
							|  |  |  |             const isProtected = treeUtils.getParentProtectedStatus(node); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             treeService.createNote(node, parentNoteId, 'after', isProtected); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (ui.cmd === "insertChildNote") { | 
					
						
							|  |  |  |             treeService.createNote(node, node.data.noteId, 'into'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-01 20:50:58 -04:00
										 |  |  |         else if (ui.cmd === "editBranchPrefix") { | 
					
						
							|  |  |  |             branchPrefixDialog.showDialog(node); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |         else if (ui.cmd === "protectBranch") { | 
					
						
							|  |  |  |             protectedSessionService.protectBranch(node.data.noteId, true); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |         else if (ui.cmd === "unprotectBranch") { | 
					
						
							|  |  |  |             protectedSessionService.protectBranch(node.data.noteId, false); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         else if (ui.cmd === "copy") { | 
					
						
							|  |  |  |             copy(treeService.getSelectedNodes()); | 
					
						
							| 
									
										
										
										
											2018-01-01 17:59:59 -05:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         else if (ui.cmd === "cut") { | 
					
						
							|  |  |  |             cut(treeService.getSelectedNodes()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (ui.cmd === "pasteAfter") { | 
					
						
							|  |  |  |             pasteAfter(node); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (ui.cmd === "pasteInto") { | 
					
						
							|  |  |  |             pasteInto(node); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (ui.cmd === "delete") { | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             treeChangesService.deleteNodes(treeService.getSelectedNodes(true)); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-28 15:59:37 +02:00
										 |  |  |         else if (ui.cmd === "exportSubTreeToTar") { | 
					
						
							|  |  |  |             exportService.exportSubTree(node.data.noteId, 'tar'); | 
					
						
							| 
									
										
										
										
											2018-05-27 12:26:34 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-28 15:59:37 +02:00
										 |  |  |         else if (ui.cmd === "exportSubTreeToOpml") { | 
					
						
							|  |  |  |             exportService.exportSubTree(node.data.noteId, 'opml'); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |         else if (ui.cmd === "importBranch") { | 
					
						
							|  |  |  |             exportService.importBranch(node.data.noteId); | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-04-01 20:33:10 -04:00
										 |  |  |         else if (ui.cmd === "collapseBranch") { | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  |             treeService.collapseTree(node); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (ui.cmd === "forceNoteSync") { | 
					
						
							|  |  |  |             syncService.forceNoteSync(node.data.noteId); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else if (ui.cmd === "sortAlphabetically") { | 
					
						
							|  |  |  |             treeService.sortAlphabetically(node.data.noteId); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2018-03-25 13:41:29 -04:00
										 |  |  |             messagingService.logError("Unknown command: " + ui.cmd); | 
					
						
							| 
									
										
										
										
											2017-11-22 23:16:54 -05:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default { | 
					
						
							|  |  |  |     pasteAfter, | 
					
						
							|  |  |  |     pasteInto, | 
					
						
							|  |  |  |     cut, | 
					
						
							|  |  |  |     copy, | 
					
						
							| 
									
										
										
										
											2018-04-01 17:41:28 -04:00
										 |  |  |     contextMenuOptions | 
					
						
							| 
									
										
										
										
											2018-03-25 11:09:17 -04:00
										 |  |  | }; |