refactoring recent notes into encapsulated module

This commit is contained in:
azivner 2017-11-04 13:21:41 -04:00
parent 869bccea18
commit af5ecd1869
2 changed files with 95 additions and 86 deletions

View File

@ -217,7 +217,7 @@ async function loadNoteToEditor(noteId) {
document.location.hash = noteId; document.location.hash = noteId;
addRecentNote(noteId, note.detail.note_id); recentNotes.addRecentNote(noteId, note.detail.note_id);
noteChangeDisabled = false; noteChangeDisabled = false;

View File

@ -1,6 +1,13 @@
glob.recentNotes = []; glob.recentNotes = [];
function addRecentNote(noteTreeId, noteContentId) { recentNotes = (function() {
const recentNotesSelectBox = $('#recent-notes-select-box');
const recentNotesDialog = $("#recent-notes-dialog");
const recentNotesJumpTo = $('#recentNotesJumpTo');
const recentNotesAddLink = $('#recentNotesAddLink');
const noteDetail = $('#note-detail');
function addRecentNote(noteTreeId, noteContentId) {
setTimeout(() => { setTimeout(() => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds // 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 (noteTreeId === glob.currentNote.detail.note_id || noteContentId === glob.currentNote.detail.note_id) {
@ -10,18 +17,16 @@ function addRecentNote(noteTreeId, noteContentId) {
glob.recentNotes.unshift(noteTreeId); glob.recentNotes.unshift(noteTreeId);
} }
}, 1500); }, 1500);
} }
function showRecentNotes() { function showRecentNotes() {
$('#note-detail').summernote('editor.saveRange'); noteDetail.summernote('editor.saveRange');
$("#recent-notes-dialog").dialog({ recentNotesDialog.dialog({
modal: true, modal: true,
width: 800 width: 800
}); });
const recentNotesSelectBox = $('#recent-notes-select-box');
recentNotesSelectBox.find('option').remove(); recentNotesSelectBox.find('option').remove();
// remove the current note // remove the current note
@ -45,29 +50,28 @@ function showRecentNotes() {
recentNotesSelectBox.append(option); recentNotesSelectBox.append(option);
}); });
} }
$(document).bind('keydown', 'alt+q', showRecentNotes); $(document).bind('keydown', 'alt+q', showRecentNotes);
function getSelectedNoteIdFromRecentNotes() { function getSelectedNoteIdFromRecentNotes() {
return $("#recent-notes-select-box option:selected").val(); return recentNotesSelectBox.find("option:selected").val();
} }
function setActiveNoteBasedOnRecentNotes() { function setActiveNoteBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes(); const noteId = getSelectedNoteIdFromRecentNotes();
getNodeByKey(noteId).setActive(); getNodeByKey(noteId).setActive();
$("#recent-notes-dialog").dialog('close'); recentNotesDialog.dialog('close');
} }
function addLinkBasedOnRecentNotes() { function addLinkBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes(); const noteId = getSelectedNoteIdFromRecentNotes();
const linkTitle = getNoteTitle(noteId); const linkTitle = getNoteTitle(noteId);
const noteDetail = $('#note-detail');
$("#recent-notes-dialog").dialog("close"); recentNotesDialog.dialog("close");
noteDetail.summernote('editor.restoreRange'); noteDetail.summernote('editor.restoreRange');
@ -76,9 +80,9 @@ function addLinkBasedOnRecentNotes() {
url: 'app#' + noteId, url: 'app#' + noteId,
isNewWindow: true isNewWindow: true
}); });
} }
$('#recent-notes-select-box').keydown(e => { recentNotesSelectBox.keydown(e => {
const key = e.which; const key = e.which;
if (key === 13)// the enter key code if (key === 13)// the enter key code
@ -93,11 +97,16 @@ $('#recent-notes-select-box').keydown(e => {
} }
e.preventDefault(); e.preventDefault();
}); });
$('#recent-notes-select-box').dblclick(e => { recentNotesSelectBox.dblclick(e => {
setActiveNoteBasedOnRecentNotes(); setActiveNoteBasedOnRecentNotes();
}); });
$('#recentNotesJumpTo').click(setActiveNoteBasedOnRecentNotes); recentNotesJumpTo.click(setActiveNoteBasedOnRecentNotes);
$('#recentNotesAddLink').click(addLinkBasedOnRecentNotes); recentNotesAddLink.click(addLinkBasedOnRecentNotes);
return {
addRecentNote
};
})();