From 40bbe380d37cfa06c53a928094426d43fc05eeaf Mon Sep 17 00:00:00 2001 From: zadam Date: Thu, 16 Jun 2022 14:08:33 +0200 Subject: [PATCH] converted move to dialog to new pattern --- src/public/app/dialogs/move_to.js | 58 ---------- src/public/app/layouts/desktop_layout.js | 4 +- .../app/services/root_command_executor.js | 5 - src/public/app/widgets/dialogs/move_to.js | 102 ++++++++++++++++++ src/views/desktop.ejs | 1 - src/views/dialogs/move_to.ejs | 31 ------ 6 files changed, 105 insertions(+), 96 deletions(-) delete mode 100644 src/public/app/dialogs/move_to.js create mode 100644 src/public/app/widgets/dialogs/move_to.js delete mode 100644 src/views/dialogs/move_to.ejs diff --git a/src/public/app/dialogs/move_to.js b/src/public/app/dialogs/move_to.js deleted file mode 100644 index 061dd57a8..000000000 --- a/src/public/app/dialogs/move_to.js +++ /dev/null @@ -1,58 +0,0 @@ -import noteAutocompleteService from "../services/note_autocomplete.js"; -import utils from "../services/utils.js"; -import toastService from "../services/toast.js"; -import froca from "../services/froca.js"; -import branchService from "../services/branches.js"; -import treeService from "../services/tree.js"; - -const $dialog = $("#move-to-dialog"); -const $form = $("#move-to-form"); -const $noteAutoComplete = $("#move-to-note-autocomplete"); -const $noteList = $("#move-to-note-list"); - -let movedBranchIds; - -export async function showDialog(branchIds) { - movedBranchIds = branchIds; - - utils.openDialog($dialog); - - $noteAutoComplete.val('').trigger('focus'); - - $noteList.empty(); - - for (const branchId of movedBranchIds) { - const branch = froca.getBranch(branchId); - const note = await froca.getNote(branch.noteId); - - $noteList.append($("
  • ").text(note.title)); - } - - noteAutocompleteService.initNoteAutocomplete($noteAutoComplete); - noteAutocompleteService.showRecentNotes($noteAutoComplete); -} - -async function moveNotesTo(parentBranchId) { - await branchService.moveToParentNote(movedBranchIds, parentBranchId); - - const parentBranch = froca.getBranch(parentBranchId); - const parentNote = await parentBranch.getNote(); - - toastService.showMessage(`Selected notes have been moved into ${parentNote.title}`); -} - -$form.on('submit', () => { - const notePath = $noteAutoComplete.getSelectedNotePath(); - - if (notePath) { - $dialog.modal('hide'); - - const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath); - froca.getBranchId(parentNoteId, noteId).then(branchId => moveNotesTo(branchId)); - } - else { - logError("No path to move to."); - } - - return false; -}); diff --git a/src/public/app/layouts/desktop_layout.js b/src/public/app/layouts/desktop_layout.js index f1757d05c..9ae5a736a 100644 --- a/src/public/app/layouts/desktop_layout.js +++ b/src/public/app/layouts/desktop_layout.js @@ -64,6 +64,7 @@ import NoteTypeChooserDialog from "../widgets/dialogs/note_type_chooser.js"; import JumpToNoteDialog from "../widgets/dialogs/jump_to_note.js"; import AddLinkDialog from "../widgets/dialogs/add_link.js"; import CloneToDialog from "../widgets/dialogs/clone_to.js"; +import MoveToDialog from "../widgets/dialogs/move_to.js"; export default class DesktopLayout { constructor(customWidgets) { @@ -202,6 +203,7 @@ export default class DesktopLayout { .child(new NoteTypeChooserDialog()) .child(new JumpToNoteDialog()) .child(new AddLinkDialog()) - .child(new CloneToDialog()); + .child(new CloneToDialog()) + .child(new MoveToDialog()); } } diff --git a/src/public/app/services/root_command_executor.js b/src/public/app/services/root_command_executor.js index 71ad2c814..65332502a 100644 --- a/src/public/app/services/root_command_executor.js +++ b/src/public/app/services/root_command_executor.js @@ -23,11 +23,6 @@ export default class RootCommandExecutor extends Component { appContext.triggerEvent("readOnlyTemporarilyDisabled", { noteContext }); } - async moveBranchIdsToCommand({branchIds}) { - const d = await import("../dialogs/move_to.js"); - d.showDialog(branchIds); - } - showOptionsCommand({openTab}) { import("../dialogs/options.js").then(d => d.showDialog(openTab)); } diff --git a/src/public/app/widgets/dialogs/move_to.js b/src/public/app/widgets/dialogs/move_to.js new file mode 100644 index 000000000..2d3ee0af0 --- /dev/null +++ b/src/public/app/widgets/dialogs/move_to.js @@ -0,0 +1,102 @@ +import noteAutocompleteService from "../../services/note_autocomplete.js"; +import utils from "../../services/utils.js"; +import toastService from "../../services/toast.js"; +import froca from "../../services/froca.js"; +import branchService from "../../services/branches.js"; +import treeService from "../../services/tree.js"; +import BasicWidget from "../basic_widget.js"; + +const TPL = ` +`; + +export default class MoveToDialog extends BasicWidget { + constructor() { + super(); + + this.movedBranchIds = null; + } + + doRender() { + this.$widget = $(TPL); + this.$dialog = this.$widget.find(".move-to-dialog"); + this.$form = this.$widget.find(".move-to-form"); + this.$noteAutoComplete = this.$widget.find(".move-to-note-autocomplete"); + this.$noteList = this.$widget.find(".move-to-note-list"); + + this.$form.on('submit', () => { + const notePath = this.$noteAutoComplete.getSelectedNotePath(); + + if (notePath) { + this.$widget.modal('hide'); + + const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath); + froca.getBranchId(parentNoteId, noteId).then(branchId => this.moveNotesTo(branchId)); + } + else { + logError("No path to move to."); + } + + return false; + }); + } + + async moveBranchIdsToEvent({branchIds}) { + this.movedBranchIds = branchIds; + + utils.openDialog(this.$widget); + + this.$noteAutoComplete.val('').trigger('focus'); + + this.$noteList.empty(); + + for (const branchId of this.movedBranchIds) { + const branch = froca.getBranch(branchId); + const note = await froca.getNote(branch.noteId); + + this.$noteList.append($("
  • ").text(note.title)); + } + + noteAutocompleteService.initNoteAutocomplete(this.$noteAutoComplete); + noteAutocompleteService.showRecentNotes(this.$noteAutoComplete); + } + + async moveNotesTo(parentBranchId) { + await branchService.moveToParentNote(this.movedBranchIds, parentBranchId); + + const parentBranch = froca.getBranch(parentBranchId); + const parentNote = await parentBranch.getNote(); + + toastService.showMessage(`Selected notes have been moved into ${parentNote.title}`); + } +} diff --git a/src/views/desktop.ejs b/src/views/desktop.ejs index ac1f348ab..0098b8d9d 100644 --- a/src/views/desktop.ejs +++ b/src/views/desktop.ejs @@ -26,7 +26,6 @@ <%- include('dialogs/info.ejs') %> <%- include('dialogs/prompt.ejs') %> <%- include('dialogs/confirm.ejs') %> -<%- include('dialogs/move_to.ejs') %> <%- include('dialogs/delete_notes.ejs') %>