Notes/src/public/app/services/branches.js

223 lines
6.3 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";
2021-04-16 23:01:56 +02:00
import froca from "./froca.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);
branchIdsToMove = filterSearchBranches(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);
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();
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-05-30 10:30:21 +02:00
async function moveToParentNote(branchIdsToMove, newParentBranchId) {
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);
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-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
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;
}
2021-09-16 14:38:09 +02:00
let proceed, deleteAllClones, eraseNotes;
if (utils.isMobile()) {
proceed = true;
deleteAllClones = false;
}
else {
const deleteNotesDialog = await import("../dialogs/delete_notes.js");
2021-09-16 14:38:09 +02:00
({proceed, deleteAllClones, eraseNotes} = await deleteNotesDialog.showDialog(branchIdsToDelete));
}
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
if (deleteAllClones) {
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) {
if (hoistedNoteService.isHoistedNode(node)
2020-02-10 20:57:56 +01:00
|| 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;
2021-08-24 22:59:51 +02:00
node.getParent().renderTitle();
}
}
2017-11-04 22:10:41 -04: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();
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-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);
}
});
async function cloneNoteTo(childNoteId, parentBranchId, prefix) {
const resp = await server.put(`notes/${childNoteId}/clone-to/${parentBranchId}`, {
2020-02-17 19:42:52 +01:00
prefix: prefix
});
if (!resp.success) {
alert(resp.message);
}
}
// 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) {
alert(resp.message);
}
}
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,
cloneNoteTo
2020-05-30 10:30:21 +02:00
};