2019-11-05 23:11:25 +01:00
|
|
|
import noteAutocompleteService from "../services/note_autocomplete.js";
|
|
|
|
import utils from "../services/utils.js";
|
2020-01-25 09:56:08 +01:00
|
|
|
import treeService from "../services/tree.js";
|
2019-11-05 23:11:25 +01:00
|
|
|
import toastService from "../services/toast.js";
|
2021-04-16 22:57:37 +02:00
|
|
|
import froca from "../services/tree_cache.js";
|
2020-02-17 19:42:52 +01:00
|
|
|
import branchService from "../services/branches.js";
|
2019-11-05 23:11:25 +01:00
|
|
|
|
|
|
|
const $dialog = $("#clone-to-dialog");
|
|
|
|
const $form = $("#clone-to-form");
|
|
|
|
const $noteAutoComplete = $("#clone-to-note-autocomplete");
|
|
|
|
const $clonePrefix = $("#clone-prefix");
|
2019-11-10 22:06:43 +01:00
|
|
|
const $noteList = $("#clone-to-note-list");
|
2019-11-05 23:11:25 +01:00
|
|
|
|
2019-11-10 22:06:43 +01:00
|
|
|
let clonedNoteIds;
|
2019-11-05 23:11:25 +01:00
|
|
|
|
2019-11-10 22:06:43 +01:00
|
|
|
export async function showDialog(noteIds) {
|
|
|
|
clonedNoteIds = [];
|
2019-11-05 23:11:25 +01:00
|
|
|
|
2019-11-10 22:06:43 +01:00
|
|
|
for (const noteId of noteIds) {
|
|
|
|
if (!clonedNoteIds.includes(noteId)) {
|
|
|
|
clonedNoteIds.push(noteId);
|
|
|
|
}
|
2019-11-05 23:11:25 +01:00
|
|
|
}
|
|
|
|
|
2020-02-09 10:00:13 +01:00
|
|
|
utils.openDialog($dialog);
|
2019-11-05 23:11:25 +01:00
|
|
|
|
2019-11-09 17:45:22 +01:00
|
|
|
$noteAutoComplete.val('').trigger('focus');
|
2019-11-05 23:11:25 +01:00
|
|
|
|
2019-11-10 22:06:43 +01:00
|
|
|
$noteList.empty();
|
|
|
|
|
|
|
|
for (const noteId of clonedNoteIds) {
|
2021-04-16 22:57:37 +02:00
|
|
|
const note = await froca.getNote(noteId);
|
2019-11-10 22:06:43 +01:00
|
|
|
|
|
|
|
$noteList.append($("<li>").text(note.title));
|
|
|
|
}
|
|
|
|
|
2019-11-05 23:11:25 +01:00
|
|
|
noteAutocompleteService.initNoteAutocomplete($noteAutoComplete);
|
|
|
|
noteAutocompleteService.showRecentNotes($noteAutoComplete);
|
|
|
|
}
|
|
|
|
|
2019-11-10 22:06:43 +01:00
|
|
|
async function cloneNotesTo(notePath) {
|
2020-05-31 22:33:02 +02:00
|
|
|
const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
|
2021-04-16 22:57:37 +02:00
|
|
|
const targetBranchId = await froca.getBranchId(parentNoteId, noteId);
|
2019-11-10 22:06:43 +01:00
|
|
|
|
|
|
|
for (const cloneNoteId of clonedNoteIds) {
|
2020-05-31 22:33:02 +02:00
|
|
|
await branchService.cloneNoteTo(cloneNoteId, targetBranchId, $clonePrefix.val());
|
2019-11-10 22:06:43 +01:00
|
|
|
|
2021-04-16 22:57:37 +02:00
|
|
|
const clonedNote = await froca.getNote(cloneNoteId);
|
|
|
|
const targetNote = await froca.getBranch(targetBranchId).getNote();
|
2019-11-10 22:06:43 +01:00
|
|
|
|
|
|
|
toastService.showMessage(`Note "${clonedNote.title}" has been cloned into ${targetNote.title}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:45:22 +01:00
|
|
|
$form.on('submit', () => {
|
2020-05-16 22:11:09 +02:00
|
|
|
const notePath = $noteAutoComplete.getSelectedNotePath();
|
2019-11-05 23:11:25 +01:00
|
|
|
|
|
|
|
if (notePath) {
|
|
|
|
$dialog.modal('hide');
|
|
|
|
|
2019-11-10 22:06:43 +01:00
|
|
|
cloneNotesTo(notePath);
|
2019-11-05 23:11:25 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-10-12 21:05:34 +02:00
|
|
|
logError("No path to clone to.");
|
2019-11-05 23:11:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2020-05-16 22:11:09 +02:00
|
|
|
});
|