From 2523f81f3b476ab1c3ea9a3deb2b5255d7f7a44c Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 12 Nov 2018 20:17:48 +0100 Subject: [PATCH] bootstrap backed replacements for JS prompt and alert --- src/public/javascripts/dialogs/info.js | 31 +++++++++++++ src/public/javascripts/dialogs/prompt.js | 37 ++++++++++++++++ .../services/note_detail_relation_map.js | 43 +++---------------- src/views/dialogs/branch_prefix.ejs | 2 +- src/views/dialogs/info.ejs | 19 ++++++++ src/views/dialogs/prompt.ejs | 24 +++++++++++ src/views/index.ejs | 2 + 7 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 src/public/javascripts/dialogs/info.js create mode 100644 src/public/javascripts/dialogs/prompt.js create mode 100644 src/views/dialogs/info.ejs create mode 100644 src/views/dialogs/prompt.ejs diff --git a/src/public/javascripts/dialogs/info.js b/src/public/javascripts/dialogs/info.js new file mode 100644 index 000000000..f64f83ce0 --- /dev/null +++ b/src/public/javascripts/dialogs/info.js @@ -0,0 +1,31 @@ +const $dialog = $("#info-dialog"); +const $infoContent = $("#info-dialog-content"); +const $okButton = $("#info-dialog-ok-button"); + +let resolve; + +function info(message) { + glob.activeDialog = $dialog; + + $infoContent.text(message); + + $dialog.modal(); + + return new Promise((res, rej) => { + resolve = res; + }); +} + +$dialog.on('shown.bs.modal', () => $okButton.trigger("focus")); + +$dialog.on("hidden.bs.modal", () => { + if (resolve) { + resolve(); + } +}); + +$okButton.click(() => $dialog.modal("hide")); + +export default { + info +} \ No newline at end of file diff --git a/src/public/javascripts/dialogs/prompt.js b/src/public/javascripts/dialogs/prompt.js new file mode 100644 index 000000000..eeb983b16 --- /dev/null +++ b/src/public/javascripts/dialogs/prompt.js @@ -0,0 +1,37 @@ +const $dialog = $("#prompt-dialog"); +const $question = $("#prompt-dialog-question"); +const $answer = $("#prompt-dialog-answer"); +const $form = $("#prompt-dialog-form"); + +let resolve; + +function ask(message, defaultValue = '') { + glob.activeDialog = $dialog; + + $question.text(message); + $answer.val(defaultValue); + + $dialog.modal(); + + return new Promise((res, rej) => { + resolve = res; + }); +} + +$dialog.on('shown.bs.modal', () => $answer.focus().select()); + +$dialog.on("hidden.bs.modal", () => { + if (resolve) { + resolve(null); + } +}); + +$form.submit(() => { + resolve($answer.val()); + + $dialog.modal('hide'); +}); + +export default { + ask +} \ No newline at end of file diff --git a/src/public/javascripts/services/note_detail_relation_map.js b/src/public/javascripts/services/note_detail_relation_map.js index be723f429..643c39e2e 100644 --- a/src/public/javascripts/services/note_detail_relation_map.js +++ b/src/public/javascripts/services/note_detail_relation_map.js @@ -5,10 +5,10 @@ import libraryLoader from "./library_loader.js"; import treeService from "./tree.js"; import contextMenuWidget from "./context_menu.js"; import infoService from "./info.js"; +import promptDialog from "../dialogs/prompt.js"; const $component = $("#note-detail-relation-map"); const $relationMapContainer = $("#relation-map-container"); -const $addChildNotesButton = $("#relation-map-add-child-notes"); const $createChildNote = $("#relation-map-create-child-note"); const $zoomInButton = $("#relation-map-zoom-in"); const $zoomOutButton = $("#relation-map-zoom-out"); @@ -62,6 +62,10 @@ function loadMapData() { } async function show() { + const result = await promptDialog.ask("Enter name of new note:", "new note"); + + alert(result); + $component.show(); await libraryLoader.requireLibrary(libraryLoader.RELATION_MAP); @@ -377,12 +381,6 @@ async function createNoteBox(id, title, x, y) { }); } -function getFreePosition() { - const maxY = mapData.notes.filter(note => !!note.y).map(note => note.y).reduce((a, b) => Math.max(a, b), 0); - - return [100, maxY + 200]; -} - async function refresh() { // delete all endpoints and connections jsPlumbInstance.deleteEveryEndpoint(); @@ -390,37 +388,6 @@ async function refresh() { await loadNotesAndRelations(); } -$addChildNotesButton.click(async () => { - const children = await server.get("notes/" + noteDetailService.getCurrentNoteId() + "/children"); - - let [curX, curY] = getFreePosition(); - - for (const child of children) { - if (mapData.notes.some(note => note.id === child.noteId)) { - // note already exists - continue; - } - - mapData.notes.push({ - id: child.noteId, - x: curX, - y: curY - }); - - if (curX > 1000) { - curX = 100; - curY += 200; - } - else { - curX += 200; - } - } - - saveData(); - - await refresh(); -}); - let clipboard = null; $createChildNote.click(async () => { diff --git a/src/views/dialogs/branch_prefix.ejs b/src/views/dialogs/branch_prefix.ejs index 36e5a44ac..edaea7042 100644 --- a/src/views/dialogs/branch_prefix.ejs +++ b/src/views/dialogs/branch_prefix.ejs @@ -17,7 +17,7 @@ diff --git a/src/views/dialogs/info.ejs b/src/views/dialogs/info.ejs new file mode 100644 index 000000000..64bfa6696 --- /dev/null +++ b/src/views/dialogs/info.ejs @@ -0,0 +1,19 @@ + diff --git a/src/views/dialogs/prompt.ejs b/src/views/dialogs/prompt.ejs new file mode 100644 index 000000000..28875553b --- /dev/null +++ b/src/views/dialogs/prompt.ejs @@ -0,0 +1,24 @@ + diff --git a/src/views/index.ejs b/src/views/index.ejs index 45e6707d9..49383918e 100644 --- a/src/views/index.ejs +++ b/src/views/index.ejs @@ -182,6 +182,8 @@ <% include dialogs/protected_session_password.ejs %> <% include dialogs/recent_changes.ejs %> <% include dialogs/sql_console.ejs %> + <% include dialogs/info.ejs %> + <% include dialogs/prompt.ejs %>