2017-11-05 10:10:41 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const treeChanges = (function() {
|
2018-01-02 07:29:06 +08:00
|
|
|
async function moveBeforeNode(nodesToMove, beforeNode) {
|
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-01-14 07:02:41 +08:00
|
|
|
const resp = await server.put('tree/' + nodeToMove.data.note_tree_id + '/move-before/' + beforeNode.data.note_tree_id);
|
2018-01-02 11:28:19 +08:00
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
changeNode(nodeToMove, node => node.moveTo(beforeNode, 'before'));
|
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
async function moveAfterNode(nodesToMove, afterNode) {
|
2018-01-20 07:34:21 +08:00
|
|
|
nodesToMove.reverse(); // need to reverse to keep the note order
|
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-01-14 07:02:41 +08:00
|
|
|
const resp = await server.put('tree/' + nodeToMove.data.note_tree_id + '/move-after/' + afterNode.data.note_tree_id);
|
2018-01-02 11:28:19 +08:00
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
changeNode(nodeToMove, node => node.moveTo(afterNode, 'after'));
|
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
async function moveToNode(nodesToMove, toNode) {
|
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-01-14 07:02:41 +08:00
|
|
|
const resp = await server.put('tree/' + nodeToMove.data.note_tree_id + '/move-to/' + toNode.data.note_id);
|
2018-01-02 11:28:19 +08:00
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
changeNode(nodeToMove, node => {
|
|
|
|
// first expand which will force lazy load and only then move the node
|
|
|
|
// if this is not expanded before moving, then lazy load won't happen because it already contains node
|
2018-01-02 12:28:00 +08:00
|
|
|
// this doesn't work if this isn't a folder yet, that's why we expand second time below
|
2018-01-02 07:29:06 +08:00
|
|
|
toNode.setExpanded(true);
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
node.moveTo(toNode);
|
2017-12-28 11:44:27 +08:00
|
|
|
|
2018-01-02 07:29:06 +08:00
|
|
|
toNode.folder = true;
|
|
|
|
toNode.renderTitle();
|
2018-01-02 12:28:00 +08:00
|
|
|
|
|
|
|
// this expands the note in case it become the folder only after the move
|
|
|
|
toNode.setExpanded(true);
|
2018-01-02 07:29:06 +08:00
|
|
|
});
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-01-15 10:39:21 +08:00
|
|
|
async function deleteNodes(nodes) {
|
|
|
|
if (nodes.length === 0 || !confirm('Are you sure you want to delete select note(s) and all the sub-notes?')) {
|
2017-11-20 12:12:39 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-01-15 10:39:21 +08:00
|
|
|
for (const node of nodes) {
|
|
|
|
await server.remove('tree/' + node.data.note_tree_id);
|
2017-11-20 12:12:39 +08:00
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-01-15 10:39:21 +08:00
|
|
|
// following code assumes that nodes contain only top-most selected nodes - getSelectedNodes has been
|
|
|
|
// called with stopOnParent=true
|
|
|
|
let next = nodes[nodes.length - 1].getNextSibling();
|
2017-12-15 11:53:25 +08:00
|
|
|
|
|
|
|
if (!next) {
|
2018-01-15 10:39:21 +08:00
|
|
|
next = nodes[0].getPrevSibling();
|
2017-12-15 11:53:25 +08:00
|
|
|
}
|
|
|
|
|
2018-01-15 10:39:21 +08:00
|
|
|
if (!next && !isTopLevelNode(nodes[0])) {
|
|
|
|
next = nodes[0].getParent();
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2017-12-24 01:19:15 +08:00
|
|
|
if (next) {
|
|
|
|
// activate next element after this one is deleted so we don't lose focus
|
|
|
|
next.setActive();
|
|
|
|
|
|
|
|
noteTree.setCurrentNotePathToHash(next);
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2017-12-10 10:42:22 +08:00
|
|
|
noteTree.reload();
|
2017-12-27 08:54:43 +08:00
|
|
|
|
2018-01-15 10:39:21 +08:00
|
|
|
showMessage("Note(s) has been deleted.");
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2017-11-29 04:17:11 +08:00
|
|
|
async function moveNodeUpInHierarchy(node) {
|
2017-12-24 01:19:15 +08:00
|
|
|
if (isTopLevelNode(node)) {
|
2017-11-29 04:17:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-14 07:02:41 +08:00
|
|
|
const resp = await server.put('tree/' + node.data.note_tree_id + '/move-after/' + node.getParent().data.note_tree_id);
|
2018-01-02 11:28:19 +08:00
|
|
|
|
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-29 04:17:11 +08:00
|
|
|
|
2017-12-24 01:19:15 +08:00
|
|
|
if (!isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
|
2017-11-29 04:17:11 +08:00
|
|
|
node.getParent().folder = false;
|
|
|
|
node.getParent().renderTitle();
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
2017-11-29 04:17:11 +08:00
|
|
|
|
2017-12-18 05:28:13 +08:00
|
|
|
changeNode(node, node => node.moveTo(node.getParent(), 'after'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function changeNode(node, func) {
|
2017-12-24 01:19:15 +08:00
|
|
|
assertArguments(node.data.parent_note_id, node.data.note_id);
|
|
|
|
|
2017-12-20 10:40:48 +08:00
|
|
|
noteTree.removeParentChildRelation(node.data.parent_note_id, node.data.note_id);
|
2017-12-18 05:28:13 +08:00
|
|
|
|
|
|
|
func(node);
|
|
|
|
|
2017-12-24 01:19:15 +08:00
|
|
|
node.data.parent_note_id = isTopLevelNode(node) ? 'root' : node.getParent().data.note_id;
|
2017-12-18 05:28:13 +08:00
|
|
|
|
2017-12-20 10:40:48 +08:00
|
|
|
noteTree.setParentChildRelation(node.data.note_tree_id, node.data.parent_note_id, node.data.note_id);
|
2017-11-29 04:17:11 +08:00
|
|
|
|
|
|
|
noteTree.setCurrentNotePathToHash(node);
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
moveBeforeNode,
|
|
|
|
moveAfterNode,
|
|
|
|
moveToNode,
|
2018-01-15 10:39:21 +08:00
|
|
|
deleteNodes,
|
2018-01-14 07:02:41 +08:00
|
|
|
moveNodeUpInHierarchy
|
2017-11-05 10:10:41 +08:00
|
|
|
};
|
|
|
|
})();
|