2018-03-25 11:09:17 -04:00
|
|
|
import utils from './utils.js';
|
2018-03-25 13:41:29 -04:00
|
|
|
import server from './server.js';
|
2019-10-20 10:00:18 +02:00
|
|
|
import toastService from "./toast.js";
|
2021-04-16 23:01:56 +02:00
|
|
|
import froca from "./froca.js";
|
2019-04-16 21:40:04 +02:00
|
|
|
import hoistedNoteService from "./hoisted_note.js";
|
2019-10-18 23:19:16 +02:00
|
|
|
import ws from "./ws.js";
|
2022-12-01 13:07:23 +01:00
|
|
|
import appContext from "../components/app_context.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);
|
2021-02-18 22:30:55 +01:00
|
|
|
branchIdsToMove = filterSearchBranches(branchIdsToMove);
|
2018-10-21 22:42:20 +02:00
|
|
|
|
2022-12-01 10:16:57 +01:00
|
|
|
if (['root', 'lb_root', 'lb_availablelaunchers', 'lb_visiblelaunchers'].includes(beforeBranchId)) {
|
2022-11-25 15:29:57 +01:00
|
|
|
toastService.showError('Cannot move notes here.');
|
2018-10-21 22:42:20 +02:00
|
|
|
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
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
2018-01-01 18:29:06 -05:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
}
|
2018-01-19 18:34:21 -05:00
|
|
|
|
2020-01-26 11:41:40 +01:00
|
|
|
async function moveAfterBranch(branchIdsToMove, afterBranchId) {
|
2020-02-10 20:57:56 +01:00
|
|
|
branchIdsToMove = filterRootNote(branchIdsToMove);
|
2021-02-18 22:30:55 +01:00
|
|
|
branchIdsToMove = filterSearchBranches(branchIdsToMove);
|
2020-01-12 09:57:28 +01:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
const afterNote = await froca.getBranch(afterBranchId).getNote();
|
2018-10-21 22:42:20 +02:00
|
|
|
|
2022-11-25 15:29:57 +01:00
|
|
|
const forbiddenNoteIds = [
|
|
|
|
'root',
|
|
|
|
hoistedNoteService.getHoistedNoteId(),
|
|
|
|
'lb_root',
|
2022-12-01 10:16:57 +01:00
|
|
|
'lb_availablelaunchers',
|
|
|
|
'lb_visiblelaunchers'
|
2022-11-25 15:29:57 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
if (forbiddenNoteIds.includes(afterNote.noteId)) {
|
|
|
|
toastService.showError('Cannot move notes here.');
|
2018-10-21 22:42:20 +02:00
|
|
|
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
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
2018-01-01 18:29:06 -05:00
|
|
|
}
|
2017-11-04 22:10:41 -04:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-04 22:10:41 -04:00
|
|
|
|
2020-05-30 10:30:21 +02:00
|
|
|
async function moveToParentNote(branchIdsToMove, newParentBranchId) {
|
2022-11-25 15:29:57 +01:00
|
|
|
if (newParentBranchId === 'lb_root') {
|
|
|
|
toastService.showError('Cannot move notes here.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:57:56 +01:00
|
|
|
branchIdsToMove = filterRootNote(branchIdsToMove);
|
2020-01-12 09:57:28 +01:00
|
|
|
|
|
|
|
for (const branchIdToMove of branchIdsToMove) {
|
2021-04-16 22:57:37 +02:00
|
|
|
const branchToMove = froca.getBranch(branchIdToMove);
|
2018-10-21 22:42:20 +02:00
|
|
|
|
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') {
|
2019-11-04 22:41:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-05-30 10:30:21 +02:00
|
|
|
const resp = await server.put(`branches/${branchIdToMove}/move-to/${newParentBranchId}`);
|
2018-01-01 22:28:19 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-11-19 23:12:39 -05:00
|
|
|
}
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2017-11-04 22:10:41 -04:00
|
|
|
|
2022-09-15 23:09:24 +02:00
|
|
|
async function deleteNotes(branchIdsToDelete, forceDeleteAllClones = false) {
|
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) {
|
2019-08-17 02:49:08 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-16 14:38:09 +02:00
|
|
|
let proceed, deleteAllClones, eraseNotes;
|
2021-08-21 20:53:45 +02:00
|
|
|
|
|
|
|
if (utils.isMobile()) {
|
|
|
|
proceed = true;
|
|
|
|
deleteAllClones = false;
|
|
|
|
}
|
|
|
|
else {
|
2022-06-16 19:41:29 +02:00
|
|
|
({proceed, deleteAllClones, eraseNotes} = await new Promise(res =>
|
2022-09-15 23:09:24 +02:00
|
|
|
appContext.triggerCommand('showDeleteNotesDialog', {branchIdsToDelete, callback: res, forceDeleteAllClones})));
|
2021-08-21 20:53:45 +02:00
|
|
|
}
|
2021-03-14 19:54:40 +01:00
|
|
|
|
2021-03-18 23:23:35 +01:00
|
|
|
if (!proceed) {
|
|
|
|
return false;
|
2020-01-12 10:35:33 +01:00
|
|
|
}
|
|
|
|
|
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;
|
2021-09-16 14:38:09 +02:00
|
|
|
const query = `?taskId=${taskId}&eraseNotes=${eraseNotes ? 'true' : 'false'}&last=${last ? 'true' : 'false'}`;
|
2019-10-19 00:11:07 +02:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
const branch = froca.getBranch(branchIdToDelete);
|
2020-01-12 10:35:33 +01:00
|
|
|
|
2021-03-18 23:42:30 +01:00
|
|
|
if (deleteAllClones) {
|
2020-01-12 10:35:33 +01:00
|
|
|
await server.remove(`notes/${branch.noteId}` + query);
|
2019-05-20 21:50:01 +02:00
|
|
|
}
|
2019-09-01 20:57:25 +02:00
|
|
|
else {
|
2020-02-03 21:56:45 +01:00
|
|
|
await server.remove(`branches/${branchIdToDelete}` + query);
|
2019-09-01 20:57:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 19:47:44 +02:00
|
|
|
if (eraseNotes) {
|
|
|
|
utils.reloadFrontendApp("erasing notes requires reload");
|
|
|
|
}
|
|
|
|
|
2019-05-22 20:53:59 +02:00
|
|
|
return true;
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
2018-01-01 22:28:19 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
async function moveNodeUpInHierarchy(node) {
|
2021-03-06 20:23:29 +01:00
|
|
|
if (hoistedNoteService.isHoistedNode(node)
|
2020-02-10 20:57:56 +01:00
|
|
|
|| hoistedNoteService.isTopLevelNode(node)
|
2019-11-04 22:41:06 +01:00
|
|
|
|| node.getParent().data.noteType === 'search') {
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
|
|
|
}
|
2017-11-28 15:17:11 -05:00
|
|
|
|
2022-08-07 13:23:03 +02:00
|
|
|
const targetBranchId = node.getParent().data.branchId;
|
|
|
|
const branchIdToMove = node.data.branchId;
|
|
|
|
|
|
|
|
const resp = await server.put(`branches/${branchIdToMove}/move-after/${targetBranchId}`);
|
2017-11-28 15:17:11 -05:00
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2018-03-25 11:09:17 -04:00
|
|
|
return;
|
2017-12-17 16:28:13 -05:00
|
|
|
}
|
|
|
|
|
2020-02-10 20:57:56 +01:00
|
|
|
if (!hoistedNoteService.isTopLevelNode(node) && node.getParent().getChildren().length <= 1) {
|
2018-03-25 11:09:17 -04:00
|
|
|
node.getParent().folder = false;
|
2021-08-24 22:59:51 +02:00
|
|
|
node.getParent().renderTitle();
|
2018-03-25 11:09:17 -04:00
|
|
|
}
|
|
|
|
}
|
2017-11-04 22:10:41 -04:00
|
|
|
|
2021-02-18 22:30:55 +01:00
|
|
|
function filterSearchBranches(branchIds) {
|
|
|
|
return branchIds.filter(branchId => !branchId.startsWith('virt-'));
|
|
|
|
}
|
|
|
|
|
2020-02-10 20:57:56 +01:00
|
|
|
function filterRootNote(branchIds) {
|
|
|
|
const hoistedNoteId = hoistedNoteService.getHoistedNoteId();
|
2019-04-16 21:40:04 +02:00
|
|
|
|
2020-01-12 09:57:28 +01:00
|
|
|
return branchIds.filter(branchId => {
|
2021-04-16 22:57:37 +02:00
|
|
|
const branch = froca.getBranch(branchId);
|
2020-01-12 09:57:28 +01:00
|
|
|
|
|
|
|
return branch.noteId !== 'root'
|
|
|
|
&& branch.noteId !== hoistedNoteId;
|
|
|
|
});
|
2019-04-16 21:40:04 +02:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-24 11:39:44 +02:00
|
|
|
if (message.type === 'taskError') {
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.closePersistent(message.taskId);
|
|
|
|
toastService.showError(message.message);
|
2021-04-24 11:39:44 +02:00
|
|
|
} else if (message.type === 'taskProgressCount') {
|
2019-10-20 10:00:18 +02:00
|
|
|
toastService.showPersistent(makeToast(message.taskId, "Delete notes in progress: " + message.progressCount));
|
2021-04-24 11:39:44 +02:00
|
|
|
} else if (message.type === 'taskSucceeded') {
|
2019-10-18 23:19:16 +02:00
|
|
|
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 => {
|
2021-04-24 11:39:44 +02:00
|
|
|
if (message.taskType !== 'undeleteNotes') {
|
2020-01-03 13:14:43 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-24 11:39:44 +02:00
|
|
|
if (message.type === 'taskError') {
|
2020-01-03 13:14:43 +01:00
|
|
|
toastService.closePersistent(message.taskId);
|
|
|
|
toastService.showError(message.message);
|
2021-04-24 11:39:44 +02:00
|
|
|
} else if (message.type === 'taskProgressCount') {
|
2020-01-03 13:14:43 +01:00
|
|
|
toastService.showPersistent(makeToast(message.taskId, "Undeleting notes in progress: " + message.progressCount));
|
2021-04-24 11:39:44 +02:00
|
|
|
} else if (message.type === 'taskSucceeded') {
|
2020-01-03 13:14:43 +01:00
|
|
|
const toast = makeToast(message.taskId, "Undeleting notes finished successfully.");
|
|
|
|
toast.closeAfter = 5000;
|
|
|
|
|
|
|
|
toastService.showPersistent(toast);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-27 23:39:46 +01:00
|
|
|
async function cloneNoteToBranch(childNoteId, parentBranchId, prefix) {
|
|
|
|
const resp = await server.put(`notes/${childNoteId}/clone-to-branch/${parentBranchId}`, {
|
|
|
|
prefix: prefix
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2021-12-27 23:39:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cloneNoteToNote(childNoteId, parentNoteId, prefix) {
|
|
|
|
const resp = await server.put(`notes/${childNoteId}/clone-to-note/${parentNoteId}`, {
|
2020-02-17 19:42:52 +01:00
|
|
|
prefix: prefix
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2020-02-17 19:42:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// beware that first arg is noteId and second is branchId!
|
|
|
|
async function cloneNoteAfter(noteId, afterBranchId) {
|
|
|
|
const resp = await server.put('notes/' + noteId + '/clone-after/' + afterBranchId);
|
|
|
|
|
|
|
|
if (!resp.success) {
|
2022-08-24 23:20:05 +02:00
|
|
|
toastService.showError(resp.message);
|
2020-02-17 19:42:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 11:09:17 -04:00
|
|
|
export default {
|
2020-01-26 11:41:40 +01:00
|
|
|
moveBeforeBranch,
|
|
|
|
moveAfterBranch,
|
|
|
|
moveToParentNote,
|
2020-02-12 22:25:52 +01:00
|
|
|
deleteNotes,
|
2020-02-17 19:42:52 +01:00
|
|
|
moveNodeUpInHierarchy,
|
|
|
|
cloneNoteAfter,
|
2021-12-27 23:39:46 +01:00
|
|
|
cloneNoteToBranch,
|
|
|
|
cloneNoteToNote,
|
2020-05-30 10:30:21 +02:00
|
|
|
};
|