Notes/src/public/app/dialogs/move_to.js

59 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-11-11 22:57:51 +01:00
import noteAutocompleteService from "../services/note_autocomplete.js";
import utils from "../services/utils.js";
import toastService from "../services/toast.js";
import treeCache from "../services/tree_cache.js";
2020-02-17 19:42:52 +01:00
import branchService from "../services/branches.js";
2020-02-15 10:41:21 +01:00
import treeService from "../services/tree.js";
2019-11-11 22:57:51 +01:00
const $dialog = $("#move-to-dialog");
const $form = $("#move-to-form");
const $noteAutoComplete = $("#move-to-note-autocomplete");
const $noteList = $("#move-to-note-list");
2020-02-15 10:41:21 +01:00
let movedBranchIds;
2019-11-11 22:57:51 +01:00
2020-02-15 10:41:21 +01:00
export async function showDialog(branchIds) {
movedBranchIds = branchIds;
2019-11-11 22:57:51 +01:00
utils.openDialog($dialog);
2019-11-11 22:57:51 +01:00
$noteAutoComplete.val('').trigger('focus');
$noteList.empty();
2020-02-15 10:41:21 +01:00
for (const branchId of movedBranchIds) {
const branch = treeCache.getBranch(branchId);
const note = await treeCache.getNote(branch.noteId);
2019-11-11 22:57:51 +01:00
$noteList.append($("<li>").text(note.title));
}
noteAutocompleteService.initNoteAutocomplete($noteAutoComplete);
noteAutocompleteService.showRecentNotes($noteAutoComplete);
}
async function moveNotesTo(parentBranchId) {
await branchService.moveToParentNote(movedBranchIds, parentBranchId);
2019-11-11 22:57:51 +01:00
const parentBranch = treeCache.getBranch(parentBranchId);
const parentNote = await parentBranch.getNote();
2019-11-11 22:57:51 +01:00
2020-02-15 10:41:21 +01:00
toastService.showMessage(`Selected notes have been moved into ${parentNote.title}`);
2019-11-11 22:57:51 +01:00
}
$form.on('submit', () => {
2020-05-16 22:11:09 +02:00
const notePath = $noteAutoComplete.getSelectedNotePath();
2019-11-11 22:57:51 +01:00
if (notePath) {
$dialog.modal('hide');
const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
treeCache.getBranchId(parentNoteId, noteId).then(branchId => moveNotesTo(branchId));
2019-11-11 22:57:51 +01:00
}
else {
logError("No path to move to.");
2019-11-11 22:57:51 +01:00
}
return false;
2020-05-16 22:11:09 +02:00
});