From af5ecd186915494606d88c80165959b6f19696b0 Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 4 Nov 2017 13:21:41 -0400 Subject: [PATCH] refactoring recent notes into encapsulated module --- public/javascripts/note.js | 2 +- public/javascripts/recent_notes.js | 179 +++++++++++++++-------------- 2 files changed, 95 insertions(+), 86 deletions(-) diff --git a/public/javascripts/note.js b/public/javascripts/note.js index 040c1e1c3..ec04dd13d 100644 --- a/public/javascripts/note.js +++ b/public/javascripts/note.js @@ -217,7 +217,7 @@ async function loadNoteToEditor(noteId) { document.location.hash = noteId; - addRecentNote(noteId, note.detail.note_id); + recentNotes.addRecentNote(noteId, note.detail.note_id); noteChangeDisabled = false; diff --git a/public/javascripts/recent_notes.js b/public/javascripts/recent_notes.js index ebdcc7451..92d933fa2 100644 --- a/public/javascripts/recent_notes.js +++ b/public/javascripts/recent_notes.js @@ -1,103 +1,112 @@ glob.recentNotes = []; -function addRecentNote(noteTreeId, noteContentId) { - setTimeout(() => { - // we include the note into recent list only if the user stayed on the note at least 5 seconds - if (noteTreeId === glob.currentNote.detail.note_id || noteContentId === glob.currentNote.detail.note_id) { - // if it's already there, remove the note - glob.recentNotes = glob.recentNotes.filter(note => note !== noteTreeId); - - glob.recentNotes.unshift(noteTreeId); - } - }, 1500); -} - -function showRecentNotes() { - $('#note-detail').summernote('editor.saveRange'); - - $("#recent-notes-dialog").dialog({ - modal: true, - width: 800 - }); - +recentNotes = (function() { const recentNotesSelectBox = $('#recent-notes-select-box'); + const recentNotesDialog = $("#recent-notes-dialog"); + const recentNotesJumpTo = $('#recentNotesJumpTo'); + const recentNotesAddLink = $('#recentNotesAddLink'); + const noteDetail = $('#note-detail'); - recentNotesSelectBox.find('option').remove(); + function addRecentNote(noteTreeId, noteContentId) { + setTimeout(() => { + // we include the note into recent list only if the user stayed on the note at least 5 seconds + if (noteTreeId === glob.currentNote.detail.note_id || noteContentId === glob.currentNote.detail.note_id) { + // if it's already there, remove the note + glob.recentNotes = glob.recentNotes.filter(note => note !== noteTreeId); - // remove the current note - const recNotes = glob.recentNotes.filter(note => note !== glob.currentNote.detail.note_id); + glob.recentNotes.unshift(noteTreeId); + } + }, 1500); + } - $.each(recNotes, (key, valueNoteId) => { - const noteTitle = getFullName(valueNoteId); + function showRecentNotes() { + noteDetail.summernote('editor.saveRange'); - if (!noteTitle) { - return; - } + recentNotesDialog.dialog({ + modal: true, + width: 800 + }); - const option = $("") + recentNotesSelectBox.find('option').remove(); + + // remove the current note + const recNotes = glob.recentNotes.filter(note => note !== glob.currentNote.detail.note_id); + + $.each(recNotes, (key, valueNoteId) => { + const noteTitle = getFullName(valueNoteId); + + if (!noteTitle) { + return; + } + + const option = $("") .attr("value", valueNoteId) .text(noteTitle); - // select the first one (most recent one) by default - if (key === 0) { - option.attr("selected", "selected"); + // select the first one (most recent one) by default + if (key === 0) { + option.attr("selected", "selected"); + } + + recentNotesSelectBox.append(option); + }); + } + + $(document).bind('keydown', 'alt+q', showRecentNotes); + + function getSelectedNoteIdFromRecentNotes() { + return recentNotesSelectBox.find("option:selected").val(); + } + + function setActiveNoteBasedOnRecentNotes() { + const noteId = getSelectedNoteIdFromRecentNotes(); + + getNodeByKey(noteId).setActive(); + + recentNotesDialog.dialog('close'); + } + + function addLinkBasedOnRecentNotes() { + const noteId = getSelectedNoteIdFromRecentNotes(); + + const linkTitle = getNoteTitle(noteId); + + recentNotesDialog.dialog("close"); + + noteDetail.summernote('editor.restoreRange'); + + noteDetail.summernote('createLink', { + text: linkTitle, + url: 'app#' + noteId, + isNewWindow: true + }); + } + + recentNotesSelectBox.keydown(e => { + const key = e.which; + + if (key === 13)// the enter key code + { + setActiveNoteBasedOnRecentNotes(); + } + else if (key === 76 /* l */) { + addLinkBasedOnRecentNotes(); + } + else { + return; // avoid prevent default } - recentNotesSelectBox.append(option); + e.preventDefault(); }); -} -$(document).bind('keydown', 'alt+q', showRecentNotes); - -function getSelectedNoteIdFromRecentNotes() { - return $("#recent-notes-select-box option:selected").val(); -} - -function setActiveNoteBasedOnRecentNotes() { - const noteId = getSelectedNoteIdFromRecentNotes(); - - getNodeByKey(noteId).setActive(); - - $("#recent-notes-dialog").dialog('close'); -} - -function addLinkBasedOnRecentNotes() { - const noteId = getSelectedNoteIdFromRecentNotes(); - - const linkTitle = getNoteTitle(noteId); - const noteDetail = $('#note-detail'); - - $("#recent-notes-dialog").dialog("close"); - - noteDetail.summernote('editor.restoreRange'); - - noteDetail.summernote('createLink', { - text: linkTitle, - url: 'app#' + noteId, - isNewWindow: true - }); -} - -$('#recent-notes-select-box').keydown(e => { - const key = e.which; - - if (key === 13)// the enter key code - { + recentNotesSelectBox.dblclick(e => { setActiveNoteBasedOnRecentNotes(); - } - else if (key === 76 /* l */) { - addLinkBasedOnRecentNotes(); - } - else { - return; // avoid prevent default - } + }); - e.preventDefault(); -}); + recentNotesJumpTo.click(setActiveNoteBasedOnRecentNotes); + recentNotesAddLink.click(addLinkBasedOnRecentNotes); -$('#recent-notes-select-box').dblclick(e => { - setActiveNoteBasedOnRecentNotes(); -}); - -$('#recentNotesJumpTo').click(setActiveNoteBasedOnRecentNotes); -$('#recentNotesAddLink').click(addLinkBasedOnRecentNotes); \ No newline at end of file + return { + addRecentNote + }; +})(); \ No newline at end of file