Notes/public/javascripts/dialogs/recent_notes.js

138 lines
3.7 KiB
JavaScript
Raw Normal View History

"use strict";
2017-11-04 13:39:26 -04:00
const recentNotes = (function() {
2017-11-04 17:03:15 -04:00
const dialogEl = $("#recent-notes-dialog");
const selectBoxEl = $('#recent-notes-select-box');
const jumpToButtonEl = $('#recentNotesJumpTo');
const addLinkButtonEl = $('#recentNotesAddLink');
const noteDetailEl = $('#note-detail');
2017-11-04 13:39:26 -04:00
let list = [];
2017-11-04 23:46:50 -04:00
$.ajax({
url: baseApiUrl + 'recent-notes',
type: 'GET',
2017-11-08 22:33:08 -05:00
error: () => showError("Error getting recent notes.")
2017-11-04 23:46:50 -04:00
}).then(result => {
list = result.map(r => r.note_tree_id);
2017-11-04 23:46:50 -04:00
});
function addRecentNote(noteTreeId) {
setTimeout(() => {
// we include the note into recent list only if the user stayed on the note at least 5 seconds
if (noteTreeId === noteEditor.getCurrentNoteId()) {
2017-11-04 23:46:50 -04:00
$.ajax({
url: baseApiUrl + 'recent-notes/' + noteTreeId,
type: 'PUT',
2017-11-08 22:33:08 -05:00
error: () => showError("Error setting recent notes.")
2017-11-04 23:46:50 -04:00
}).then(result => {
list = result.map(r => r.note_tree_id);
2017-11-04 23:46:50 -04:00
});
}
}, 1500);
}
function removeRecentNote(noteTreeIdToRemove) {
2017-11-04 23:46:50 -04:00
$.ajax({
url: baseApiUrl + 'recent-notes/' + noteTreeIdToRemove,
2017-11-04 23:46:50 -04:00
type: 'DELETE',
2017-11-08 22:33:08 -05:00
error: () => showError("Error removing note from recent notes.")
2017-11-04 23:46:50 -04:00
}).then(result => {
list = result.map(r => r.note_tree_id);
2017-11-04 23:46:50 -04:00
});
2017-11-04 13:39:26 -04:00
}
function showDialog() {
2017-11-04 17:03:15 -04:00
glob.activeDialog = dialogEl;
2017-11-04 17:03:15 -04:00
noteDetailEl.summernote('editor.saveRange');
dialogEl.dialog({
modal: true,
width: 800
});
2017-11-04 17:03:15 -04:00
selectBoxEl.find('option').remove();
// remove the current note
2017-11-04 17:54:27 -04:00
const recNotes = list.filter(note => note !== noteEditor.getCurrentNoteId());
$.each(recNotes, (key, valueNoteTreeId) => {
const noteTitle = treeUtils.getFullName(valueNoteTreeId);
if (!noteTitle) {
return;
}
const option = $("<option></option>")
.attr("value", valueNoteTreeId)
.text(noteTitle);
// select the first one (most recent one) by default
if (key === 0) {
option.attr("selected", "selected");
}
2017-11-04 17:03:15 -04:00
selectBoxEl.append(option);
});
}
2017-10-06 22:46:30 -04:00
function getSelectedNoteIdFromRecentNotes() {
2017-11-04 17:03:15 -04:00
return selectBoxEl.find("option:selected").val();
}
function setActiveNoteBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes();
2017-11-04 22:18:36 -04:00
treeUtils.activateNode(noteId);
2017-11-04 17:03:15 -04:00
dialogEl.dialog('close');
}
function addLinkBasedOnRecentNotes() {
const noteId = getSelectedNoteIdFromRecentNotes();
2017-11-04 22:18:36 -04:00
const linkTitle = treeUtils.getNoteTitle(noteId);
2017-11-04 17:03:15 -04:00
dialogEl.dialog("close");
2017-11-04 17:03:15 -04:00
noteDetailEl.summernote('editor.restoreRange');
2017-11-04 17:03:15 -04:00
noteDetailEl.summernote('createLink', {
text: linkTitle,
url: 'app#' + noteId,
isNewWindow: true
});
}
2017-11-04 17:03:15 -04:00
selectBoxEl.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
}
e.preventDefault();
});
2017-11-04 13:39:26 -04:00
$(document).bind('keydown', 'alt+q', showDialog);
2017-11-04 17:03:15 -04:00
selectBoxEl.dblclick(e => {
setActiveNoteBasedOnRecentNotes();
});
2017-11-04 17:03:15 -04:00
jumpToButtonEl.click(setActiveNoteBasedOnRecentNotes);
addLinkButtonEl.click(addLinkBasedOnRecentNotes);
return {
2017-11-04 13:39:26 -04:00
showDialog,
addRecentNote,
removeRecentNote
};
})();