From 819ae7c4c007b1a759aa9c8c054d3788ffd201d4 Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 4 Nov 2017 17:03:15 -0400 Subject: [PATCH] more generic handling of links to note --- public/javascripts/dialogs/add_link.js | 47 +------------- public/javascripts/dialogs/event_log.js | 13 +--- public/javascripts/dialogs/jump_to_note.js | 2 + public/javascripts/dialogs/note_history.js | 21 ++++--- public/javascripts/dialogs/recent_changes.js | 32 ++++------ public/javascripts/dialogs/recent_notes.js | 38 +++++------ public/javascripts/dialogs/settings.js | 2 + public/javascripts/init.js | 66 +++++++++++++++++++- 8 files changed, 118 insertions(+), 103 deletions(-) diff --git a/public/javascripts/dialogs/add_link.js b/public/javascripts/dialogs/add_link.js index 4ae3ea2da..559c9cffa 100644 --- a/public/javascripts/dialogs/add_link.js +++ b/public/javascripts/dialogs/add_link.js @@ -6,6 +6,8 @@ const addLink = (function() { const linkTitleEl = $("#link-title"); function showDialog() { + glob.activeDialog = dialogEl; + noteDetailEl.summernote('editor.saveRange'); dialogEl.dialog({ @@ -65,51 +67,6 @@ const addLink = (function() { return false; }); - // when click on link popup, in case of internal link, just go the the referenced note instead of default behavior - // of opening the link in new window/tab - $(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote); - $(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote); - - function goToInternalNote(e, callback) { - const targetUrl = $(e.target).attr("href"); - - const noteId = getNoteIdFromLink(targetUrl); - - if (noteId !== null) { - getNodeByKey(noteId).setActive(); - - // this is quite ugly hack, but it seems like we can't close the tooltip otherwise - $("[role='tooltip']").remove(); - - e.preventDefault(); - - if (callback) { - callback(); - } - } - } - - function getNoteIdFromLink(url) { - const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url); - - if (noteIdMatch === null) { - return null; - } - else { - return noteIdMatch[1]; - } - } - - function getNodeIdFromLabel(label) { - const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label); - - if (noteIdMatch !== null) { - return noteIdMatch[1]; - } - - return null; - } - $(document).bind('keydown', 'alt+l', showDialog); return { diff --git a/public/javascripts/dialogs/event_log.js b/public/javascripts/dialogs/event_log.js index bbc7ac8b8..3cee7c4fd 100644 --- a/public/javascripts/dialogs/event_log.js +++ b/public/javascripts/dialogs/event_log.js @@ -3,6 +3,8 @@ const eventLog = (function() { const listEl = $("#event-log-list"); async function showDialog() { + glob.activeDialog = dialogEl; + dialogEl.dialog({ modal: true, width: 800, @@ -21,10 +23,7 @@ const eventLog = (function() { const dateTime = formatDateTime(getDateFromTS(event.date_added)); if (event.note_id) { - const noteLink = $("", { - href: 'app#' + event.note_id, - text: getFullName(event.note_id) - }).prop('outerHTML'); + const noteLink = createNoteLink(event.note_id).prop('outerHTML'); event.comment = event.comment.replace('', noteLink); } @@ -35,12 +34,6 @@ const eventLog = (function() { } } - $(document).on('click', '#event-log-dialog a', e => { - goToInternalNote(e, () => { - dialogEl.dialog('close'); - }); - }); - return { showDialog }; diff --git a/public/javascripts/dialogs/jump_to_note.js b/public/javascripts/dialogs/jump_to_note.js index 47056bf80..bbc45698d 100644 --- a/public/javascripts/dialogs/jump_to_note.js +++ b/public/javascripts/dialogs/jump_to_note.js @@ -4,6 +4,8 @@ const jumpToNote = (function() { const formEl = $("#jump-to-note-form"); function showDialog() { + glob.activeDialog = dialogEl; + autoCompleteEl.val(''); dialogEl.dialog({ diff --git a/public/javascripts/dialogs/note_history.js b/public/javascripts/dialogs/note_history.js index a3264cce4..02b92e8c5 100644 --- a/public/javascripts/dialogs/note_history.js +++ b/public/javascripts/dialogs/note_history.js @@ -10,12 +10,9 @@ const noteHistory = (function() { await showNoteHistoryDialog(glob.currentNote.detail.note_id); } - // weird hack because browser doesn't like we're returning promise and displays promise page - function showNoteHistoryDialogNotAsync(noteId, noteHistoryId) { - showNoteHistoryDialog(noteId, noteHistoryId); - } - async function showNoteHistoryDialog(noteId, noteHistoryId) { + glob.activeDialog = dialogEl; + dialogEl.dialog({ modal: true, width: 800, @@ -68,9 +65,17 @@ const noteHistory = (function() { contentEl.html(noteText); }); + $(document).on('click', "a[action='note-history']", event => { + const linkEl = $(event.target); + const noteId = linkEl.attr('note-id'); + const noteHistoryId = linkEl.attr('note-history-id'); + + showNoteHistoryDialog(noteId, noteHistoryId); + + return false; + }); + return { - showCurrentNoteHistory, - showNoteHistoryDialog, - showNoteHistoryDialogNotAsync + showCurrentNoteHistory }; })(); \ No newline at end of file diff --git a/public/javascripts/dialogs/recent_changes.js b/public/javascripts/dialogs/recent_changes.js index 4f1df5776..51dd6658f 100644 --- a/public/javascripts/dialogs/recent_changes.js +++ b/public/javascripts/dialogs/recent_changes.js @@ -1,8 +1,10 @@ const recentChanges = (function() { - const dialog = $("#recent-changes-dialog"); + const dialogEl = $("#recent-changes-dialog"); async function showDialog() { - dialog.dialog({ + glob.activeDialog = dialogEl; + + dialogEl.dialog({ modal: true, width: 800, height: 700 @@ -14,7 +16,7 @@ const recentChanges = (function() { error: () => error("Error getting recent changes.") }); - dialog.html(''); + dialogEl.html(''); const groupedByDate = groupByDate(result); @@ -26,23 +28,20 @@ const recentChanges = (function() { for (const change of dayChanges) { const formattedTime = formatTime(getDateFromTS(change.date_modified_to)); - const noteLink = $("", { - href: 'app#' + change.note_id, - text: change.note_title - }); - const revLink = $("", { - href: "javascript: noteHistory.showNoteHistoryDialogNotAsync('" + change.note_id + "', '" + change.note_history_id + "');", + href: 'javascript:', text: 'rev' - }); + }).attr('action', 'note-history') + .attr('note-id', change.note_id) + .attr('note-history-id', change.note_history_id); changesListEl.append($('
  • ') .append(formattedTime + ' - ') - .append(noteLink) + .append(createNoteLink(change.note_id)) .append(' (').append(revLink).append(')')); } - dialog.append(dayEl); + dialogEl.append(dayEl); } } @@ -51,8 +50,6 @@ const recentChanges = (function() { const dayCache = {}; for (const row of result) { - row.note_title = getFullName(row.note_id); - let dateDay = getDateFromTS(row.date_modified_to); dateDay.setHours(0); dateDay.setMinutes(0); @@ -77,15 +74,8 @@ const recentChanges = (function() { return groupedByDate; } - $(document).bind('keydown', 'alt+r', showDialog); - $(document).on('click', '#recent-changes-dialog a', e => { - goToInternalNote(e, () => { - dialog.dialog('close'); - }); - }); - return { showDialog }; diff --git a/public/javascripts/dialogs/recent_notes.js b/public/javascripts/dialogs/recent_notes.js index ce8831cfe..b741f36b3 100644 --- a/public/javascripts/dialogs/recent_notes.js +++ b/public/javascripts/dialogs/recent_notes.js @@ -1,9 +1,9 @@ const recentNotes = (function() { - const dialog = $("#recent-notes-dialog"); - const selectBox = $('#recent-notes-select-box'); - const jumpToButton = $('#recentNotesJumpTo'); - const addLinkButton = $('#recentNotesAddLink'); - const noteDetail = $('#note-detail'); + const dialogEl = $("#recent-notes-dialog"); + const selectBoxEl = $('#recent-notes-select-box'); + const jumpToButtonEl = $('#recentNotesJumpTo'); + const addLinkButtonEl = $('#recentNotesAddLink'); + const noteDetailEl = $('#note-detail'); let list = []; function addRecentNote(noteTreeId, noteContentId) { @@ -23,14 +23,16 @@ const recentNotes = (function() { } function showDialog() { - noteDetail.summernote('editor.saveRange'); + glob.activeDialog = dialogEl; - dialog.dialog({ + noteDetailEl.summernote('editor.saveRange'); + + dialogEl.dialog({ modal: true, width: 800 }); - selectBox.find('option').remove(); + selectBoxEl.find('option').remove(); // remove the current note const recNotes = list.filter(note => note !== glob.currentNote.detail.note_id); @@ -51,12 +53,12 @@ const recentNotes = (function() { option.attr("selected", "selected"); } - selectBox.append(option); + selectBoxEl.append(option); }); } function getSelectedNoteIdFromRecentNotes() { - return selectBox.find("option:selected").val(); + return selectBoxEl.find("option:selected").val(); } function setActiveNoteBasedOnRecentNotes() { @@ -64,7 +66,7 @@ const recentNotes = (function() { getNodeByKey(noteId).setActive(); - dialog.dialog('close'); + dialogEl.dialog('close'); } function addLinkBasedOnRecentNotes() { @@ -72,18 +74,18 @@ const recentNotes = (function() { const linkTitle = getNoteTitle(noteId); - dialog.dialog("close"); + dialogEl.dialog("close"); - noteDetail.summernote('editor.restoreRange'); + noteDetailEl.summernote('editor.restoreRange'); - noteDetail.summernote('createLink', { + noteDetailEl.summernote('createLink', { text: linkTitle, url: 'app#' + noteId, isNewWindow: true }); } - selectBox.keydown(e => { + selectBoxEl.keydown(e => { const key = e.which; if (key === 13)// the enter key code @@ -102,12 +104,12 @@ const recentNotes = (function() { $(document).bind('keydown', 'alt+q', showDialog); - selectBox.dblclick(e => { + selectBoxEl.dblclick(e => { setActiveNoteBasedOnRecentNotes(); }); - jumpToButton.click(setActiveNoteBasedOnRecentNotes); - addLinkButton.click(addLinkBasedOnRecentNotes); + jumpToButtonEl.click(setActiveNoteBasedOnRecentNotes); + addLinkButtonEl.click(addLinkBasedOnRecentNotes); return { showDialog, diff --git a/public/javascripts/dialogs/settings.js b/public/javascripts/dialogs/settings.js index 144f8f8ef..ad3cb64a6 100644 --- a/public/javascripts/dialogs/settings.js +++ b/public/javascripts/dialogs/settings.js @@ -9,6 +9,8 @@ const settings = (function() { } async function showDialog() { + glob.activeDialog = dialogEl; + const settings = await $.ajax({ url: baseApiUrl + 'settings', type: 'GET', diff --git a/public/javascripts/init.js b/public/javascripts/init.js index c22b3ef33..506552d25 100644 --- a/public/javascripts/init.js +++ b/public/javascripts/init.js @@ -1,4 +1,6 @@ -glob = {}; +glob = { + activeDialog: null +}; // hot keys are active also inside inputs and content editables jQuery.hotkeys.options.filterInputAcceptingElements = true; @@ -84,4 +86,66 @@ $(document).tooltip({ function isElectron() { return window && window.process && window.process.type; +} + +// when click on link popup, in case of internal link, just go the the referenced note instead of default behavior +// of opening the link in new window/tab +$(document).on('click', "a[action='note']", goToInternalNote); +$(document).on('click', 'div.popover-content a, div.ui-tooltip-content', goToInternalNote); +$(document).on('dblclick', '.note-editable a, div.ui-tooltip-content', goToInternalNote); + +function goToInternalNote(e) { + const linkEl = $(e.target); + let noteId = linkEl.attr("note-id"); + + if (!noteId) { + noteId = getNoteIdFromLink(linkEl.attr('href')); + } + + if (noteId) { + getNodeByKey(noteId).setActive(); + + // this is quite ugly hack, but it seems like we can't close the tooltip otherwise + $("[role='tooltip']").remove(); + + if (glob.activeDialog) { + try { + glob.activeDialog.dialog('close'); + } + catch (e) {} + } + + e.preventDefault(); + } +} + +function getNoteIdFromLink(url) { + const noteIdMatch = /app#([A-Za-z0-9]{12})/.exec(url); + + if (noteIdMatch === null) { + return null; + } + else { + return noteIdMatch[1]; + } +} + +function getNodeIdFromLabel(label) { + const noteIdMatch = / \(([A-Za-z0-9]{12})\)/.exec(label); + + if (noteIdMatch !== null) { + return noteIdMatch[1]; + } + + return null; +} + +function createNoteLink(noteId) { + const noteLink = $("", { + href: 'javascript:', + text: getFullName(noteId) + }).attr('action', 'note') + .attr('note-id', noteId); + + return noteLink; } \ No newline at end of file