2017-11-05 07:38:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-05 07:33:39 +08:00
|
|
|
const contextMenu = (function() {
|
|
|
|
const treeEl = $("#tree");
|
2017-11-05 07:28:49 +08:00
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
let clipboardId = null;
|
|
|
|
let clipboardMode = null;
|
|
|
|
|
2017-11-05 07:33:39 +08:00
|
|
|
function pasteAfter(node) {
|
2017-11-23 12:16:54 +08:00
|
|
|
if (clipboardMode === 'cut') {
|
2017-11-24 08:29:25 +08:00
|
|
|
const subjectNode = treeUtils.getNodeByKey(clipboardId);
|
2017-10-16 08:55:38 +08:00
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
treeChanges.moveAfterNode(subjectNode, node);
|
|
|
|
}
|
|
|
|
else if (clipboardMode === 'copy') {
|
|
|
|
treeChanges.cloneNoteAfter(clipboardId, node.data.note_tree_id);
|
|
|
|
}
|
2017-11-29 00:36:32 +08:00
|
|
|
else if (clipboardId === null) {
|
|
|
|
// just do nothing
|
|
|
|
}
|
2017-11-23 12:16:54 +08:00
|
|
|
else {
|
2017-12-07 08:53:23 +08:00
|
|
|
throwError("Unrecognized clipboard mode=" + clipboardMode);
|
2017-11-23 12:16:54 +08:00
|
|
|
}
|
2017-10-16 08:55:38 +08:00
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
clipboardId = null;
|
|
|
|
clipboardMode = null;
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
2017-10-16 08:55:38 +08:00
|
|
|
|
2017-11-05 07:33:39 +08:00
|
|
|
function pasteInto(node) {
|
2017-11-23 12:16:54 +08:00
|
|
|
if (clipboardMode === 'cut') {
|
2017-11-24 08:29:25 +08:00
|
|
|
const subjectNode = treeUtils.getNodeByKey(clipboardId);
|
2017-11-23 12:16:54 +08:00
|
|
|
|
|
|
|
treeChanges.moveToNode(subjectNode, node);
|
|
|
|
}
|
|
|
|
else if (clipboardMode === 'copy') {
|
|
|
|
treeChanges.cloneNoteTo(clipboardId, node.data.note_id);
|
|
|
|
}
|
|
|
|
else {
|
2017-12-07 08:53:23 +08:00
|
|
|
throwError("Unrecognized clipboard mode=" + mode);
|
2017-11-23 12:16:54 +08:00
|
|
|
}
|
2017-10-16 08:55:38 +08:00
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
clipboardId = null;
|
|
|
|
clipboardMode = null;
|
|
|
|
}
|
2017-10-16 08:55:38 +08:00
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
function copy(node) {
|
|
|
|
clipboardId = node.data.note_id;
|
|
|
|
clipboardMode = 'copy';
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
2017-10-16 08:55:38 +08:00
|
|
|
|
2017-11-05 07:33:39 +08:00
|
|
|
function cut(node) {
|
2017-11-24 08:29:25 +08:00
|
|
|
clipboardId = node.key;
|
2017-11-23 12:16:54 +08:00
|
|
|
clipboardMode = 'cut';
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
2017-10-16 08:55:38 +08:00
|
|
|
|
2017-11-05 07:33:39 +08:00
|
|
|
const contextMenuSettings = {
|
|
|
|
delegate: "span.fancytree-title",
|
|
|
|
autoFocus: true,
|
|
|
|
menu: [
|
2017-12-24 03:35:20 +08: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-05 07:33:39 +08:00
|
|
|
{title: "----"},
|
2017-12-24 03:35:20 +08:00
|
|
|
{title: "Edit tree prefix <kbd>F2</kbd>", cmd: "editTreePrefix", uiIcon: "ui-icon-pencil"},
|
2017-11-27 11:34:25 +08:00
|
|
|
{title: "----"},
|
2017-11-15 13:04:26 +08:00
|
|
|
{title: "Protect sub-tree", cmd: "protectSubTree", uiIcon: "ui-icon-locked"},
|
|
|
|
{title: "Unprotect sub-tree", cmd: "unprotectSubTree", uiIcon: "ui-icon-unlocked"},
|
2017-11-05 07:33:39 +08:00
|
|
|
{title: "----"},
|
2017-12-24 03:35:20 +08: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 23:00:08 +08:00
|
|
|
{title: "Paste after", cmd: "pasteAfter", uiIcon: "ui-icon-clipboard"},
|
|
|
|
{title: "----"},
|
|
|
|
{title: "Collapse sub-tree <kbd>Alt+-</kbd>", cmd: "collapse-sub-tree", uiIcon: "ui-icon-minus"}
|
2017-11-05 07:33:39 +08:00
|
|
|
],
|
|
|
|
beforeOpen: (event, ui) => {
|
|
|
|
const node = $.ui.fancytree.getNode(ui.target);
|
|
|
|
// Modify menu entries depending on node status
|
2017-11-23 12:16:54 +08:00
|
|
|
treeEl.contextmenu("enableEntry", "pasteAfter", clipboardId !== null);
|
|
|
|
treeEl.contextmenu("enableEntry", "pasteInto", clipboardId !== null);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 07:33:39 +08: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-10 00:06:15 +08:00
|
|
|
|
2017-11-05 07:33:39 +08:00
|
|
|
if (ui.cmd === "insertNoteHere") {
|
2017-12-20 10:40:48 +08:00
|
|
|
const parentNoteId = node.data.parent_note_id;
|
2017-11-15 12:01:23 +08:00
|
|
|
const isProtected = treeUtils.getParentProtectedStatus(node);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-23 12:16:54 +08:00
|
|
|
noteTree.createNote(node, parentNoteId, 'after', isProtected);
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
|
|
|
else if (ui.cmd === "insertChildNote") {
|
2017-11-23 12:16:54 +08:00
|
|
|
noteTree.createNote(node, node.data.note_id, 'into');
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
2017-11-27 11:34:25 +08:00
|
|
|
else if (ui.cmd === "editTreePrefix") {
|
|
|
|
editTreePrefix.showDialog(node);
|
|
|
|
}
|
2017-11-15 13:04:26 +08:00
|
|
|
else if (ui.cmd === "protectSubTree") {
|
2017-11-23 09:46:42 +08:00
|
|
|
protected_session.protectSubTree(node.data.note_id, true);
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
2017-11-15 13:04:26 +08:00
|
|
|
else if (ui.cmd === "unprotectSubTree") {
|
2017-11-23 09:46:42 +08:00
|
|
|
protected_session.protectSubTree(node.data.note_id, false);
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
2017-11-23 12:16:54 +08:00
|
|
|
else if (ui.cmd === "copy") {
|
|
|
|
copy(node);
|
|
|
|
}
|
2017-11-05 07:33:39 +08:00
|
|
|
else if (ui.cmd === "cut") {
|
|
|
|
cut(node);
|
|
|
|
}
|
|
|
|
else if (ui.cmd === "pasteAfter") {
|
|
|
|
pasteAfter(node);
|
|
|
|
}
|
|
|
|
else if (ui.cmd === "pasteInto") {
|
|
|
|
pasteInto(node);
|
|
|
|
}
|
|
|
|
else if (ui.cmd === "delete") {
|
2017-11-05 10:10:41 +08:00
|
|
|
treeChanges.deleteNode(node);
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
2017-12-26 23:00:08 +08:00
|
|
|
else if (ui.cmd === "collapse-sub-tree") {
|
|
|
|
noteTree.collapseTree(node);
|
|
|
|
}
|
2017-11-05 07:33:39 +08:00
|
|
|
else {
|
2017-12-02 11:28:22 +08:00
|
|
|
messaging.logError("Unknown command: " + ui.cmd);
|
2017-11-05 07:33:39 +08:00
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
}
|
2017-11-05 07:33:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
pasteAfter,
|
|
|
|
pasteInto,
|
|
|
|
cut,
|
2017-11-29 00:36:32 +08:00
|
|
|
copy,
|
2017-11-05 07:33:39 +08:00
|
|
|
contextMenuSettings
|
2017-09-10 00:06:15 +08:00
|
|
|
}
|
2017-11-05 07:33:39 +08:00
|
|
|
})();
|