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';
|
2019-10-20 16:00:18 +08:00
|
|
|
import toastService from "./toast.js";
|
2018-03-27 10:11:45 +08:00
|
|
|
import treeCache from "./tree_cache.js";
|
2019-06-16 17:12:07 +08:00
|
|
|
import treeUtils from "./tree_utils.js";
|
2019-04-17 03:40:04 +08:00
|
|
|
import hoistedNoteService from "./hoisted_note.js";
|
2019-05-21 03:50:01 +08:00
|
|
|
import noteDetailService from "./note_detail.js";
|
2019-10-19 05:19:16 +08:00
|
|
|
import ws from "./ws.js";
|
2018-01-02 11:28:19 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function moveBeforeNode(nodesToMove, beforeNode) {
|
2019-04-17 03:40:04 +08:00
|
|
|
nodesToMove = await filterRootNote(nodesToMove);
|
2018-10-22 04:42:20 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-03-19 04:59:53 +08:00
|
|
|
await changeNode(
|
|
|
|
node => node.moveTo(beforeNode, 'before'),
|
2019-11-05 03:20:21 +08:00
|
|
|
nodeToMove);
|
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) {
|
2019-04-17 03:40:04 +08:00
|
|
|
nodesToMove = await filterRootNote(nodesToMove);
|
2018-10-22 04:42:20 +08:00
|
|
|
|
2019-04-17 03:40:04 +08:00
|
|
|
if (afterNode.data.noteId === 'root' || afterNode.data.noteId === await hoistedNoteService.getHoistedNoteId()) {
|
2018-10-22 04:42:20 +08:00
|
|
|
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
|
|
|
|
2019-03-19 04:59:53 +08:00
|
|
|
await changeNode(
|
|
|
|
node => node.moveTo(afterNode, 'after'),
|
2019-11-05 03:20:21 +08:00
|
|
|
nodeToMove);
|
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) {
|
2019-04-17 03:40:04 +08:00
|
|
|
nodesToMove = await filterRootNote(nodesToMove);
|
2018-10-22 04:42:20 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
for (const nodeToMove of nodesToMove) {
|
2019-11-05 05:41:06 +08:00
|
|
|
if (nodeToMove.data.noteId === await hoistedNoteService.getHoistedNoteId()
|
|
|
|
|| nodeToMove.getParent().data.noteType === 'search') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-03-19 04:59:53 +08:00
|
|
|
await changeNode(async 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
|
|
|
|
// this doesn't work if this isn't a folder yet, that's why we expand second time below
|
|
|
|
await toNode.setExpanded(true);
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2019-03-19 04:59:53 +08:00
|
|
|
node.moveTo(toNode);
|
|
|
|
}, nodeToMove);
|
2017-11-20 12:12:39 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2019-10-27 04:50:46 +08:00
|
|
|
async function getNextNode(nodes) {
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
if (!next) {
|
|
|
|
next = nodes[0].getPrevSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!next && !await hoistedNoteService.isRootNode(nodes[0])) {
|
|
|
|
next = nodes[0].getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return treeUtils.getNotePath(next);
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
async function deleteNodes(nodes) {
|
2019-04-17 03:40:04 +08:00
|
|
|
nodes = await filterRootNote(nodes);
|
2018-08-31 04:38:34 +08:00
|
|
|
|
2019-08-17 14:49:08 +08:00
|
|
|
if (nodes.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-09-02 02:57:25 +08:00
|
|
|
const $deleteClonesCheckbox = $('<div class="form-check">')
|
|
|
|
.append($('<input type="checkbox" class="form-check-input" id="delete-clones-checkbox">'))
|
|
|
|
.append($('<label for="delete-clones-checkbox">')
|
|
|
|
.text("delete also all note clones")
|
|
|
|
.attr("title", "all clones of selected notes will be deleted and as such the whole note will be deleted."));
|
|
|
|
const $nodeTitles = $("<ul>").append(...nodes.map(node => $("<li>").text(node.title)));
|
|
|
|
const $confirmText = $("<div>")
|
|
|
|
.append($("<p>").text('This will delete the following notes and their sub-notes: '))
|
|
|
|
.append($nodeTitles)
|
|
|
|
.append($deleteClonesCheckbox);
|
2019-08-17 14:49:08 +08:00
|
|
|
|
2019-08-21 03:40:47 +08:00
|
|
|
const confirmDialog = await import('../dialogs/confirm.js');
|
|
|
|
|
2019-09-02 02:57:25 +08:00
|
|
|
if (!await confirmDialog.confirm($confirmText)) {
|
2019-05-23 02:53:59 +08:00
|
|
|
return false;
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-12-15 11:53:25 +08:00
|
|
|
|
2019-09-02 02:57:25 +08:00
|
|
|
const deleteClones = $deleteClonesCheckbox.find("input").is(":checked");
|
|
|
|
|
2019-10-19 05:19:16 +08:00
|
|
|
const taskId = utils.randomString(10);
|
|
|
|
|
2019-10-19 06:11:07 +08:00
|
|
|
let counter = 0;
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
for (const node of nodes) {
|
2019-10-19 06:11:07 +08:00
|
|
|
counter++;
|
|
|
|
|
|
|
|
const last = counter === nodes.length;
|
|
|
|
const query = `?taskId=${taskId}&last=${last ? 'true' : 'false'}`;
|
|
|
|
|
2019-09-02 02:57:25 +08:00
|
|
|
if (deleteClones) {
|
2019-10-19 06:11:07 +08:00
|
|
|
await server.remove(`notes/${node.data.noteId}` + query);
|
2019-05-21 03:50:01 +08:00
|
|
|
|
|
|
|
noteDetailService.noteDeleted(node.data.noteId);
|
|
|
|
}
|
2019-09-02 02:57:25 +08:00
|
|
|
else {
|
2019-10-19 06:11:07 +08:00
|
|
|
const {noteDeleted} = await server.remove(`branches/${node.data.branchId}` + query);
|
2019-09-02 02:57:25 +08:00
|
|
|
|
|
|
|
if (noteDeleted) {
|
|
|
|
noteDetailService.noteDeleted(node.data.noteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 04:50:46 +08:00
|
|
|
const nextNotePath = await getNextNode(nodes);
|
2017-12-15 11:53:25 +08:00
|
|
|
|
2019-11-04 01:33:43 +08:00
|
|
|
const noteIds = Array.from(new Set(nodes.map(node => node.getParent().data.noteId)));
|
2017-11-20 12:12:39 +08:00
|
|
|
|
2019-10-27 04:50:46 +08:00
|
|
|
await treeService.reloadNotes(noteIds, nextNotePath);
|
2019-03-19 06:03:41 +08:00
|
|
|
|
2019-05-23 02:53:59 +08:00
|
|
|
return true;
|
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) {
|
2019-11-05 05:41:06 +08:00
|
|
|
if (await hoistedNoteService.isRootNode(node)
|
|
|
|
|| await hoistedNoteService.isTopLevelNode(node)
|
|
|
|
|| node.getParent().data.noteType === 'search') {
|
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
|
|
|
}
|
|
|
|
|
2019-04-17 03:40:04 +08:00
|
|
|
if (!hoistedNoteService.isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
|
2018-03-25 23:09:17 +08:00
|
|
|
node.getParent().folder = false;
|
|
|
|
node.getParent().renderTitle();
|
|
|
|
}
|
2017-12-24 01:19:15 +08:00
|
|
|
|
2019-03-19 04:59:53 +08:00
|
|
|
await changeNode(
|
|
|
|
node => node.moveTo(node.getParent(), 'after'),
|
|
|
|
node);
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-12-18 05:28:13 +08:00
|
|
|
|
2019-11-05 03:20:21 +08:00
|
|
|
async function changeNode(func, node) {
|
2019-03-19 04:59:53 +08:00
|
|
|
utils.assertArguments(func, node);
|
2017-12-18 05:28:13 +08:00
|
|
|
|
2018-03-27 10:11:45 +08:00
|
|
|
const childNoteId = node.data.noteId;
|
2019-03-19 04:59:53 +08:00
|
|
|
const thisOldParentNode = node.getParent();
|
2017-12-18 05:28:13 +08:00
|
|
|
|
2019-03-19 04:59:53 +08:00
|
|
|
// this will move the node the user is directly operating on to the desired location
|
|
|
|
// note that there might be other instances of this note in the tree and those are updated through
|
|
|
|
// force reloading. We could simplify our lives by just reloading this one as well, but that leads
|
|
|
|
// to flickering and not good user experience. Current solution leads to no-flicker experience in most
|
|
|
|
// cases (since cloning is not used that often) and correct for multi-clone use cases
|
2018-04-02 00:03:21 +08:00
|
|
|
await func(node);
|
2017-11-29 04:17:11 +08:00
|
|
|
|
2019-03-19 04:59:53 +08:00
|
|
|
const thisNewParentNode = node.getParent();
|
|
|
|
|
|
|
|
node.data.parentNoteId = thisNewParentNode.data.noteId;
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-10-26 15:51:08 +08:00
|
|
|
await treeCache.reloadNotes([childNoteId]);
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2019-03-19 05:33:19 +08:00
|
|
|
await treeService.checkFolderStatus(thisOldParentNode);
|
|
|
|
await treeService.checkFolderStatus(thisNewParentNode);
|
2019-03-19 04:59:53 +08:00
|
|
|
|
|
|
|
if (!thisNewParentNode.isExpanded()) {
|
|
|
|
// this expands the note in case it become the folder only after the move
|
|
|
|
await thisNewParentNode.setExpanded(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const newParentNode of treeService.getNodesByNoteId(thisNewParentNode.data.noteId)) {
|
|
|
|
if (newParentNode.key === thisNewParentNode.key) {
|
|
|
|
// this one has been handled above specifically
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
newParentNode.load(true); // force reload to show up new note
|
2019-03-19 05:10:10 +08:00
|
|
|
|
2019-03-19 05:33:19 +08:00
|
|
|
await treeService.checkFolderStatus(newParentNode);
|
2019-03-19 04:59:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const oldParentNode of treeService.getNodesByNoteId(thisOldParentNode.data.noteId)) {
|
|
|
|
if (oldParentNode.key === thisOldParentNode.key) {
|
|
|
|
// this one has been handled above specifically
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
await oldParentNode.load(true); // force reload to show up new note
|
|
|
|
|
2019-03-19 05:33:19 +08:00
|
|
|
await treeService.checkFolderStatus(oldParentNode);
|
2019-03-19 04:59:53 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
}
|
2017-11-05 10:10:41 +08:00
|
|
|
|
2019-04-17 03:40:04 +08:00
|
|
|
async function filterRootNote(nodes) {
|
|
|
|
const hoistedNoteId = await hoistedNoteService.getHoistedNoteId();
|
|
|
|
|
|
|
|
return nodes.filter(node =>
|
|
|
|
node.data.noteId !== 'root'
|
|
|
|
&& node.data.noteId !== hoistedNoteId);
|
|
|
|
}
|
|
|
|
|
2019-10-19 05:19:16 +08:00
|
|
|
function makeToast(id, message) {
|
|
|
|
return {
|
|
|
|
id: id,
|
|
|
|
title: "Delete status",
|
|
|
|
message: message,
|
|
|
|
icon: "trash"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.subscribeToMessages(async message => {
|
|
|
|
if (message.taskType !== 'delete-notes') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.type === 'task-error') {
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.closePersistent(message.taskId);
|
|
|
|
toastService.showError(message.message);
|
2019-10-19 05:19:16 +08:00
|
|
|
} else if (message.type === 'task-progress-count') {
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.showPersistent(makeToast(message.taskId, "Delete notes in progress: " + message.progressCount));
|
2019-10-19 05:19:16 +08:00
|
|
|
} else if (message.type === 'task-succeeded') {
|
|
|
|
const toast = makeToast(message.taskId, "Delete finished successfully.");
|
|
|
|
toast.closeAfter = 5000;
|
|
|
|
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.showPersistent(toast);
|
2019-10-19 05:19:16 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
|
|
|
moveBeforeNode,
|
|
|
|
moveAfterNode,
|
|
|
|
moveToNode,
|
|
|
|
deleteNodes,
|
|
|
|
moveNodeUpInHierarchy
|
|
|
|
};
|