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;
addRecentNote(noteId, note.detail.note_id);
recentNotes.addRecentNote(noteId, note.detail.note_id);
noteChangeDisabled = false;

View File

@ -1,5 +1,12 @@
glob.recentNotes = [];
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(() => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds
@ -13,15 +20,13 @@ function addRecentNote(noteTreeId, noteContentId) {
}
function showRecentNotes() {
$('#note-detail').summernote('editor.saveRange');
noteDetail.summernote('editor.saveRange');
$("#recent-notes-dialog").dialog({
recentNotesDialog.dialog({
modal: true,
width: 800
});
const recentNotesSelectBox = $('#recent-notes-select-box');
recentNotesSelectBox.find('option').remove();
// remove the current note
@ -50,7 +55,7 @@ function showRecentNotes() {
$(document).bind('keydown', 'alt+q', showRecentNotes);
function getSelectedNoteIdFromRecentNotes() {
return $("#recent-notes-select-box option:selected").val();
return recentNotesSelectBox.find("option:selected").val();
}
function setActiveNoteBasedOnRecentNotes() {
@ -58,16 +63,15 @@ function setActiveNoteBasedOnRecentNotes() {
getNodeByKey(noteId).setActive();
$("#recent-notes-dialog").dialog('close');
recentNotesDialog.dialog('close');
}
function addLinkBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes();
const linkTitle = getNoteTitle(noteId);
const noteDetail = $('#note-detail');
$("#recent-notes-dialog").dialog("close");
recentNotesDialog.dialog("close");
noteDetail.summernote('editor.restoreRange');
@ -78,7 +82,7 @@ function addLinkBasedOnRecentNotes() {
});
}
$('#recent-notes-select-box').keydown(e => {
recentNotesSelectBox.keydown(e => {
const key = e.which;
if (key === 13)// the enter key code
@ -95,9 +99,14 @@ $('#recent-notes-select-box').keydown(e => {
e.preventDefault();
});
$('#recent-notes-select-box').dblclick(e => {
recentNotesSelectBox.dblclick(e => {
setActiveNoteBasedOnRecentNotes();
});
$('#recentNotesJumpTo').click(setActiveNoteBasedOnRecentNotes);
$('#recentNotesAddLink').click(addLinkBasedOnRecentNotes);
recentNotesJumpTo.click(setActiveNoteBasedOnRecentNotes);
recentNotesAddLink.click(addLinkBasedOnRecentNotes);
return {
addRecentNote
};
})();