2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
import utils from './utils.js';
|
2018-03-26 01:41:29 +08:00
|
|
|
import server from './server.js';
|
2018-03-26 09:29:35 +08:00
|
|
|
import infoService from "./info.js";
|
2018-03-27 10:11:45 +08:00
|
|
|
import treeCache from "./tree_cache.js";
|
2018-01-02 11:28:19 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function moveBeforeNode(nodesToMove, beforeNode) {
|
2018-10-22 04:42:20 +08:00
|
|
|
nodesToMove = filterRootNote(nodesToMove);
|
|
|
|
|
|
|
|
if (beforeNode.data.noteId === 'root') {
|
|
|
|
alert('Cannot move notes before root note.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-04-02 08:33:10 +08:00
|
|
|
const resp = await server.put('branches/' + nodeToMove.data.branchId + '/move-before/' + beforeNode.data.branchId);
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
2018-01-02 07:29:06 +08:00
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-03-27 10:11:45 +08:00
|
|
|
await changeNode(nodeToMove, node => node.moveTo(beforeNode, 'before'));
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-20 07:34:21 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function moveAfterNode(nodesToMove, afterNode) {
|
2018-10-22 04:42:20 +08:00
|
|
|
nodesToMove = filterRootNote(nodesToMove);
|
|
|
|
|
|
|
|
if (afterNode.data.noteId === 'root') {
|
|
|
|
alert('Cannot move notes after root note.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
nodesToMove.reverse(); // need to reverse to keep the note order
|
2018-01-02 11:28:19 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-04-02 08:33:10 +08:00
|
|
|
const resp = await server.put('branches/' + nodeToMove.data.branchId + '/move-after/' + afterNode.data.branchId);
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
2018-01-02 07:29:06 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2018-03-27 10:11:45 +08:00
|
|
|
await changeNode(nodeToMove, node => node.moveTo(afterNode, 'after'));
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function moveToNode(nodesToMove, toNode) {
|
2018-10-22 04:42:20 +08:00
|
|
|
nodesToMove = filterRootNote(nodesToMove);
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
for (const nodeToMove of nodesToMove) {
|
2018-04-02 08:33:10 +08:00
|
|
|
const resp = await server.put('branches/' + nodeToMove.data.branchId + '/move-to/' + toNode.data.noteId);
|
2018-01-02 11:28:19 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-04-02 00:03:21 +08:00
|
|
|
await changeNode(nodeToMove, async node => {
|
2018-03-25 23:09:17 +08:00
|
|
|
// 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
|
|
|
|
// this doesn't work if this isn't a folder yet, that's why we expand second time below
|
2018-04-02 00:03:21 +08:00
|
|
|
await toNode.setExpanded(true);
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
node.moveTo(toNode);
|
2017-12-28 11:44:27 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
toNode.folder = true;
|
|
|
|
toNode.renderTitle();
|
2018-01-02 12:28:00 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
// this expands the note in case it become the folder only after the move
|
2018-04-02 00:03:21 +08:00
|
|
|
await toNode.setExpanded(true);
|
2018-03-25 23:09:17 +08:00
|
|
|
});
|
2017-11-20 12:12:39 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-10-22 04:42:20 +08:00
|
|
|
function filterRootNote(nodes) {
|
|
|
|
// some operations are not possible on root notes
|
|
|
|
return nodes.filter(node => node.data.noteId !== 'root');
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function deleteNodes(nodes) {
|
2018-10-22 04:42:20 +08:00
|
|
|
nodes = filterRootNote(nodes);
|
2018-08-31 04:38:34 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (nodes.length === 0 || !confirm('Are you sure you want to delete select note(s) and all the sub-notes?')) {
|
|
|
|
return;
|
|
|
|
}
|
2017-12-15 11:53:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
for (const node of nodes) {
|
2018-04-02 08:33:10 +08:00
|
|
|
await server.remove('branches/' + node.data.branchId);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-12-15 11:53:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +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-11-20 12:12:39 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!next) {
|
|
|
|
next = nodes[0].getPrevSibling();
|
|
|
|
}
|
2017-12-24 01:19:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!next && !utils.isTopLevelNode(nodes[0])) {
|
|
|
|
next = nodes[0].getParent();
|
|
|
|
}
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (next) {
|
|
|
|
// activate next element after this one is deleted so we don't lose focus
|
|
|
|
next.setActive();
|
2017-12-27 08:54:43 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
treeService.setCurrentNotePathToHash(next);
|
2017-11-05 10:10:41 +08:00
|
|
|
}
|
|
|
|
|
2018-03-26 09:29:35 +08:00
|
|
|
infoService.showMessage("Note(s) has been deleted.");
|
2018-03-27 10:29:14 +08:00
|
|
|
|
|
|
|
await treeService.reload();
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2018-01-02 11:28:19 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function moveNodeUpInHierarchy(node) {
|
2018-10-22 04:42:20 +08:00
|
|
|
if (utils.isRootNode(node) || utils.isTopLevelNode(node)) {
|
2018-03-25 23:09:17 +08:00
|
|
|
return;
|
|
|
|
}
|
2017-11-29 04:17:11 +08:00
|
|
|
|
2018-04-02 08:33:10 +08:00
|
|
|
const resp = await server.put('branches/' + node.data.branchId + '/move-after/' + node.getParent().data.branchId);
|
2017-11-29 04:17:11 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!resp.success) {
|
|
|
|
alert(resp.message);
|
|
|
|
return;
|
2017-12-18 05:28:13 +08:00
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
if (!utils.isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
|
|
|
|
node.getParent().folder = false;
|
|
|
|
node.getParent().renderTitle();
|
|
|
|
}
|
2017-12-24 01:19:15 +08:00
|
|
|
|
2018-03-27 10:11:45 +08:00
|
|
|
await changeNode(node, node => node.moveTo(node.getParent(), 'after'));
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-12-18 05:28:13 +08:00
|
|
|
|
2018-03-27 10:11:45 +08:00
|
|
|
async function changeNode(node, func) {
|
2018-03-25 23:09:17 +08:00
|
|
|
utils.assertArguments(node.data.parentNoteId, node.data.noteId);
|
2017-12-18 05:28:13 +08:00
|
|
|
|
2018-03-27 10:11:45 +08:00
|
|
|
const childNoteId = node.data.noteId;
|
|
|
|
const oldParentNoteId = node.data.parentNoteId;
|
2017-12-18 05:28:13 +08:00
|
|
|
|
2018-04-02 00:03:21 +08:00
|
|
|
await func(node);
|
2017-11-29 04:17:11 +08:00
|
|
|
|
2018-03-27 10:11:45 +08:00
|
|
|
const newParentNoteId = node.data.parentNoteId = utils.isTopLevelNode(node) ? 'root' : node.getParent().data.noteId;
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2018-03-27 10:11:45 +08:00
|
|
|
await treeCache.moveNote(childNoteId, oldParentNoteId, newParentNoteId);
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
treeService.setCurrentNotePathToHash(node);
|
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
|
|
|
moveBeforeNode,
|
|
|
|
moveAfterNode,
|
|
|
|
moveToNode,
|
|
|
|
deleteNodes,
|
|
|
|
moveNodeUpInHierarchy
|
|
|
|
};
|