From adc7d158192f9afe3a5d6e756321c0659a0add0d Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 4 Nov 2017 17:07:03 -0400 Subject: [PATCH] link module --- public/javascripts/dialogs/add_link.js | 6 +- public/javascripts/dialogs/event_log.js | 2 +- public/javascripts/dialogs/jump_to_note.js | 2 +- public/javascripts/dialogs/recent_changes.js | 2 +- public/javascripts/init.js | 62 ------------------ public/javascripts/link.js | 68 ++++++++++++++++++++ views/index.ejs | 1 + 7 files changed, 75 insertions(+), 68 deletions(-) create mode 100644 public/javascripts/link.js diff --git a/public/javascripts/dialogs/add_link.js b/public/javascripts/dialogs/add_link.js index 559c9cffa..848d0369e 100644 --- a/public/javascripts/dialogs/add_link.js +++ b/public/javascripts/dialogs/add_link.js @@ -29,7 +29,7 @@ const addLink = (function() { minLength: 0, change: () => { const val = autoCompleteEl.val(); - const noteId = getNodeIdFromLabel(val); + const noteId = link.getNodeIdFromLabel(val); if (noteId) { setDefaultLinkTitle(noteId); @@ -38,7 +38,7 @@ const addLink = (function() { // this is called when user goes through autocomplete list with keyboard // at this point the item isn't selected yet so we use supplied ui.item to see where the cursor is focus: (event, ui) => { - const noteId = getNodeIdFromLabel(ui.item.value); + const noteId = link.getNodeIdFromLabel(ui.item.value); setDefaultLinkTitle(noteId); } @@ -48,7 +48,7 @@ const addLink = (function() { formEl.submit(() => { let val = autoCompleteEl.val(); - const noteId = getNodeIdFromLabel(val); + const noteId = link.getNodeIdFromLabel(val); if (noteId) { const linkTitle = linkTitleEl.val(); diff --git a/public/javascripts/dialogs/event_log.js b/public/javascripts/dialogs/event_log.js index 3cee7c4fd..e3b0630ba 100644 --- a/public/javascripts/dialogs/event_log.js +++ b/public/javascripts/dialogs/event_log.js @@ -23,7 +23,7 @@ const eventLog = (function() { const dateTime = formatDateTime(getDateFromTS(event.date_added)); if (event.note_id) { - const noteLink = createNoteLink(event.note_id).prop('outerHTML'); + const noteLink = link.createNoteLink(event.note_id).prop('outerHTML'); event.comment = event.comment.replace('', noteLink); } diff --git a/public/javascripts/dialogs/jump_to_note.js b/public/javascripts/dialogs/jump_to_note.js index bbc45698d..f324684d5 100644 --- a/public/javascripts/dialogs/jump_to_note.js +++ b/public/javascripts/dialogs/jump_to_note.js @@ -23,7 +23,7 @@ const jumpToNote = (function() { formEl.submit(() => { const val = autoCompleteEl.val(); - const noteId = getNodeIdFromLabel(val); + const noteId = link.getNodeIdFromLabel(val); if (noteId) { getNodeByKey(noteId).setActive(); diff --git a/public/javascripts/dialogs/recent_changes.js b/public/javascripts/dialogs/recent_changes.js index 51dd6658f..2d6c5d475 100644 --- a/public/javascripts/dialogs/recent_changes.js +++ b/public/javascripts/dialogs/recent_changes.js @@ -37,7 +37,7 @@ const recentChanges = (function() { changesListEl.append($('
  • ') .append(formattedTime + ' - ') - .append(createNoteLink(change.note_id)) + .append(link.createNoteLink(change.note_id)) .append(' (').append(revLink).append(')')); } diff --git a/public/javascripts/init.js b/public/javascripts/init.js index 506552d25..61f96e8dd 100644 --- a/public/javascripts/init.js +++ b/public/javascripts/init.js @@ -86,66 +86,4 @@ $(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 diff --git a/public/javascripts/link.js b/public/javascripts/link.js new file mode 100644 index 000000000..c8836bfef --- /dev/null +++ b/public/javascripts/link.js @@ -0,0 +1,68 @@ +const link = (function() { + 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; + } + + 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(); + } + } + + // 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); + + return { + getNodeIdFromLabel, + createNoteLink + }; +})(); \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs index d4e44c403..b91ad89d8 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -282,6 +282,7 @@ +