207 lines
6.2 KiB
JavaScript
Raw Normal View History

import utils from './utils.js';
import server from './server.js';
2019-10-20 10:00:18 +02:00
import toastService from "./toast.js";
2018-03-26 22:11:45 -04:00
import treeCache from "./tree_cache.js";
import hoistedNoteService from "./hoisted_note.js";
2019-10-18 23:19:16 +02:00
import ws from "./ws.js";
2018-01-01 22:28:19 -05:00
2020-01-26 11:41:40 +01:00
async function moveBeforeBranch(branchIdsToMove, beforeBranchId) {
2020-02-10 20:57:56 +01:00
branchIdsToMove = filterRootNote(branchIdsToMove);
2020-01-12 09:57:28 +01:00
if (beforeBranchId === 'root') {
alert('Cannot move notes before root note.');
return;
}
2020-01-12 09:57:28 +01:00
for (const branchIdToMove of branchIdsToMove) {
const resp = await server.put(`branches/${branchIdToMove}/move-before/${beforeBranchId}`);
2017-11-19 23:12:39 -05:00
if (!resp.success) {
alert(resp.message);
return;
}
}
}
2020-01-26 11:41:40 +01:00
async function moveAfterBranch(branchIdsToMove, afterBranchId) {
2020-02-10 20:57:56 +01:00
branchIdsToMove = filterRootNote(branchIdsToMove);
2020-01-12 09:57:28 +01:00
const afterNote = await treeCache.getBranch(afterBranchId).getNote();
2020-02-10 20:57:56 +01:00
if (afterNote.noteId === 'root' || afterNote.noteId === hoistedNoteService.getHoistedNoteId()) {
alert('Cannot move notes after root note.');
return;
}
2020-01-12 09:57:28 +01:00
branchIdsToMove.reverse(); // need to reverse to keep the note order
2018-01-01 22:28:19 -05:00
2020-01-12 09:57:28 +01:00
for (const branchIdToMove of branchIdsToMove) {
const resp = await server.put(`branches/${branchIdToMove}/move-after/${afterBranchId}`);
2017-11-19 23:12:39 -05:00
if (!resp.success) {
alert(resp.message);
return;
}
2017-11-04 22:10:41 -04:00
}
}
2017-11-04 22:10:41 -04:00
2020-01-26 11:41:40 +01:00
async function moveToParentNote(branchIdsToMove, newParentNoteId) {
2020-02-10 20:57:56 +01:00
branchIdsToMove = filterRootNote(branchIdsToMove);
2020-01-12 09:57:28 +01:00
for (const branchIdToMove of branchIdsToMove) {
const branchToMove = treeCache.getBranch(branchIdToMove);
2020-02-10 20:57:56 +01:00
if (branchToMove.noteId === hoistedNoteService.getHoistedNoteId()
2020-01-12 09:57:28 +01:00
|| (await branchToMove.getParentNote()).type === 'search') {
continue;
}
2020-01-12 09:57:28 +01:00
const resp = await server.put(`branches/${branchIdToMove}/move-to/${newParentNoteId}`);
2018-01-01 22:28:19 -05:00
if (!resp.success) {
alert(resp.message);
return;
}
2017-11-19 23:12:39 -05:00
}
}
2017-11-04 22:10:41 -04:00
async function deleteNotes(branchIdsToDelete) {
2020-02-10 20:57:56 +01:00
branchIdsToDelete = filterRootNote(branchIdsToDelete);
2018-08-30 22:38:34 +02:00
2020-01-12 10:35:33 +01:00
if (branchIdsToDelete.length === 0) {
return false;
}
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."));
2020-01-12 10:35:33 +01:00
const $nodeTitles = $("<ul>");
for (const branchId of branchIdsToDelete) {
const note = await treeCache.getBranch(branchId).getNote();
$nodeTitles.append($("<li>").text(note.title));
}
const $confirmText = $("<div>")
.append($("<p>").text('This will delete the following notes and their sub-notes: '))
.append($nodeTitles)
.append($deleteClonesCheckbox);
const confirmDialog = await import('../dialogs/confirm.js');
if (!await confirmDialog.confirm($confirmText)) {
2019-05-22 20:53:59 +02:00
return false;
}
const deleteClones = $deleteClonesCheckbox.find("input").is(":checked");
2019-10-18 23:19:16 +02:00
const taskId = utils.randomString(10);
2019-10-19 00:11:07 +02:00
let counter = 0;
2020-01-12 10:35:33 +01:00
for (const branchIdToDelete of branchIdsToDelete) {
2019-10-19 00:11:07 +02:00
counter++;
2020-01-12 10:35:33 +01:00
const last = counter === branchIdsToDelete.length;
2019-10-19 00:11:07 +02:00
const query = `?taskId=${taskId}&last=${last ? 'true' : 'false'}`;
2020-01-12 10:35:33 +01:00
const branch = treeCache.getBranch(branchIdToDelete);
if (deleteClones) {
2020-01-12 10:35:33 +01:00
await server.remove(`notes/${branch.noteId}` + query);
}
else {
2020-02-03 21:56:45 +01:00
await server.remove(`branches/${branchIdToDelete}` + query);
}
}
2019-05-22 20:53:59 +02:00
return true;
}
2018-01-01 22:28:19 -05:00
async function moveNodeUpInHierarchy(node) {
2020-02-10 20:57:56 +01:00
if (hoistedNoteService.isRootNode(node)
|| hoistedNoteService.isTopLevelNode(node)
|| node.getParent().data.noteType === 'search') {
return;
}
2017-11-28 15:17:11 -05:00
2018-04-01 20:33:10 -04:00
const resp = await server.put('branches/' + node.data.branchId + '/move-after/' + node.getParent().data.branchId);
2017-11-28 15:17:11 -05:00
if (!resp.success) {
alert(resp.message);
return;
}
2020-02-10 20:57:56 +01:00
if (!hoistedNoteService.isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
node.getParent().folder = false;
node.getParent().renderTitle();
}
}
2017-11-04 22:10:41 -04:00
2020-02-10 20:57:56 +01:00
function filterRootNote(branchIds) {
const hoistedNoteId = hoistedNoteService.getHoistedNoteId();
2020-01-12 09:57:28 +01:00
return branchIds.filter(branchId => {
const branch = treeCache.getBranch(branchId);
return branch.noteId !== 'root'
&& branch.noteId !== hoistedNoteId;
});
}
2019-10-18 23:19:16 +02: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 10:00:18 +02:00
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
2019-10-18 23:19:16 +02:00
} else if (message.type === 'task-progress-count') {
2019-10-20 10:00:18 +02:00
toastService.showPersistent(makeToast(message.taskId, "Delete notes in progress: " + message.progressCount));
2019-10-18 23:19:16 +02:00
} else if (message.type === 'task-succeeded') {
const toast = makeToast(message.taskId, "Delete finished successfully.");
toast.closeAfter = 5000;
2019-10-20 10:00:18 +02:00
toastService.showPersistent(toast);
2019-10-18 23:19:16 +02:00
}
});
2020-01-03 13:14:43 +01:00
ws.subscribeToMessages(async message => {
if (message.taskType !== 'undelete-notes') {
return;
}
if (message.type === 'task-error') {
toastService.closePersistent(message.taskId);
toastService.showError(message.message);
} else if (message.type === 'task-progress-count') {
toastService.showPersistent(makeToast(message.taskId, "Undeleting notes in progress: " + message.progressCount));
} else if (message.type === 'task-succeeded') {
const toast = makeToast(message.taskId, "Undeleting notes finished successfully.");
toast.closeAfter = 5000;
toastService.showPersistent(toast);
}
});
export default {
2020-01-26 11:41:40 +01:00
moveBeforeBranch,
moveAfterBranch,
moveToParentNote,
2020-02-12 22:25:52 +01:00
deleteNotes,
moveNodeUpInHierarchy
};