2017-11-05 07:38:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-11-05 01:39:26 +08:00
|
|
|
const recentNotes = (function() {
|
2017-11-05 05:03:15 +08:00
|
|
|
const dialogEl = $("#recent-notes-dialog");
|
|
|
|
const selectBoxEl = $('#recent-notes-select-box');
|
|
|
|
const jumpToButtonEl = $('#recentNotesJumpTo');
|
|
|
|
const addLinkButtonEl = $('#recentNotesAddLink');
|
2017-11-20 05:35:35 +08:00
|
|
|
const addCurrentAsChildEl = $("#recent-notes-add-current-as-child");
|
|
|
|
const addRecentAsChildEl = $("#recent-notes-add-recent-as-child");
|
2017-11-05 05:03:15 +08:00
|
|
|
const noteDetailEl = $('#note-detail');
|
2017-11-05 01:39:26 +08:00
|
|
|
let list = [];
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 11:46:50 +08:00
|
|
|
$.ajax({
|
|
|
|
url: baseApiUrl + 'recent-notes',
|
|
|
|
type: 'GET',
|
2017-11-09 11:33:08 +08:00
|
|
|
error: () => showError("Error getting recent notes.")
|
2017-11-05 11:46:50 +08:00
|
|
|
}).then(result => {
|
2017-11-19 06:05:50 +08:00
|
|
|
list = result.map(r => r.note_tree_id);
|
2017-11-05 11:46:50 +08:00
|
|
|
});
|
|
|
|
|
2017-11-20 01:06:48 +08:00
|
|
|
function addRecentNote(notePath) {
|
2017-11-05 01:21:41 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
// we include the note into recent list only if the user stayed on the note at least 5 seconds
|
2017-11-20 01:06:48 +08:00
|
|
|
if (notePath === noteTree.getCurrentNotePath()) {
|
2017-11-05 11:46:50 +08:00
|
|
|
$.ajax({
|
2017-11-20 01:06:48 +08:00
|
|
|
url: baseApiUrl + 'recent-notes/' + encodeURIComponent(notePath),
|
2017-11-05 11:46:50 +08:00
|
|
|
type: 'PUT',
|
2017-11-09 11:33:08 +08:00
|
|
|
error: () => showError("Error setting recent notes.")
|
2017-11-05 11:46:50 +08:00
|
|
|
}).then(result => {
|
2017-11-20 01:06:48 +08:00
|
|
|
list = result.map(r => r.note_path);
|
2017-11-05 11:46:50 +08:00
|
|
|
});
|
2017-11-05 01:21:41 +08:00
|
|
|
}
|
|
|
|
}, 1500);
|
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-20 05:35:35 +08:00
|
|
|
// FIXME: this should be probably just refresh upon deletion, not explicit delete
|
2017-11-20 01:06:48 +08:00
|
|
|
function removeRecentNote(notePathIdToRemove) {
|
2017-11-05 11:46:50 +08:00
|
|
|
$.ajax({
|
2017-11-20 05:35:35 +08:00
|
|
|
url: baseApiUrl + 'recent-notes/' + encodeURIComponent(notePathIdToRemove),
|
2017-11-05 11:46:50 +08:00
|
|
|
type: 'DELETE',
|
2017-11-09 11:33:08 +08:00
|
|
|
error: () => showError("Error removing note from recent notes.")
|
2017-11-05 11:46:50 +08:00
|
|
|
}).then(result => {
|
2017-11-20 01:06:48 +08:00
|
|
|
list = result.map(r => r.note_path);
|
2017-11-05 11:46:50 +08:00
|
|
|
});
|
2017-11-05 01:39:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function showDialog() {
|
2017-11-05 05:03:15 +08:00
|
|
|
glob.activeDialog = dialogEl;
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
noteDetailEl.summernote('editor.saveRange');
|
|
|
|
|
|
|
|
dialogEl.dialog({
|
2017-11-05 01:21:41 +08:00
|
|
|
modal: true,
|
|
|
|
width: 800
|
|
|
|
});
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
selectBoxEl.find('option').remove();
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 01:21:41 +08:00
|
|
|
// remove the current note
|
2017-11-20 01:06:48 +08:00
|
|
|
const recNotes = list.filter(note => note !== noteTree.getCurrentNotePath());
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-20 01:06:48 +08:00
|
|
|
$.each(recNotes, (key, valueNotePath) => {
|
|
|
|
const noteTitle = treeUtils.getFullNameForPath(valueNotePath);
|
2017-11-05 01:21:41 +08:00
|
|
|
|
|
|
|
const option = $("<option></option>")
|
2017-11-20 01:06:48 +08:00
|
|
|
.attr("value", valueNotePath)
|
2017-09-10 00:06:15 +08:00
|
|
|
.text(noteTitle);
|
|
|
|
|
2017-11-05 01:21:41 +08:00
|
|
|
// select the first one (most recent one) by default
|
|
|
|
if (key === 0) {
|
|
|
|
option.attr("selected", "selected");
|
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
selectBoxEl.append(option);
|
2017-11-05 01:21:41 +08:00
|
|
|
});
|
|
|
|
}
|
2017-10-07 10:46:30 +08:00
|
|
|
|
2017-11-20 05:35:35 +08:00
|
|
|
function getSelectedNotePath() {
|
2017-11-05 05:03:15 +08:00
|
|
|
return selectBoxEl.find("option:selected").val();
|
2017-11-05 01:21:41 +08:00
|
|
|
}
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-05 01:21:41 +08:00
|
|
|
function setActiveNoteBasedOnRecentNotes() {
|
2017-11-20 05:35:35 +08:00
|
|
|
const notePath = getSelectedNotePath();
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-20 01:06:48 +08:00
|
|
|
noteTree.activateNode(notePath);
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
dialogEl.dialog('close');
|
2017-11-05 01:21:41 +08:00
|
|
|
}
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 01:21:41 +08:00
|
|
|
function addLinkBasedOnRecentNotes() {
|
2017-11-20 05:35:35 +08:00
|
|
|
const notePath = getSelectedNotePath();
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-20 05:35:35 +08:00
|
|
|
const linkTitle = noteTree.getNoteTitle(notePath);
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
dialogEl.dialog("close");
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
noteDetailEl.summernote('editor.restoreRange');
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
noteDetailEl.summernote('createLink', {
|
2017-11-05 01:21:41 +08:00
|
|
|
text: linkTitle,
|
2017-11-20 01:06:48 +08:00
|
|
|
url: 'app#' + notePath,
|
2017-11-05 01:21:41 +08:00
|
|
|
isNewWindow: true
|
|
|
|
});
|
|
|
|
}
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-20 05:35:35 +08:00
|
|
|
async function addAsChild(parentNotePath, childNotePath) {
|
|
|
|
const parentNoteId = treeUtils.getNoteIdFromNotePath(parentNotePath);
|
|
|
|
const childNoteId = treeUtils.getNoteIdFromNotePath(childNotePath);
|
|
|
|
|
|
|
|
await $.ajax({
|
|
|
|
url: baseApiUrl + 'tree/' + parentNoteId + '/addChild/' + childNoteId,
|
|
|
|
type: 'PUT',
|
|
|
|
error: () => showError("Error adding child.")
|
|
|
|
});
|
|
|
|
|
|
|
|
dialogEl.dialog("close");
|
|
|
|
|
|
|
|
await noteTree.reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addCurrentAsChild() {
|
|
|
|
await addAsChild(getSelectedNotePath(), noteTree.getCurrentNotePath());
|
|
|
|
}
|
|
|
|
|
|
|
|
async function addRecentAsChild() {
|
|
|
|
addAsChild(noteTree.getCurrentNotePath(), getSelectedNotePath());
|
|
|
|
}
|
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
selectBoxEl.keydown(e => {
|
2017-11-05 01:21:41 +08:00
|
|
|
const key = e.which;
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-20 05:35:35 +08:00
|
|
|
// to get keycodes use http://keycode.info/
|
2017-11-05 01:21:41 +08:00
|
|
|
if (key === 13)// the enter key code
|
|
|
|
{
|
|
|
|
setActiveNoteBasedOnRecentNotes();
|
|
|
|
}
|
|
|
|
else if (key === 76 /* l */) {
|
|
|
|
addLinkBasedOnRecentNotes();
|
|
|
|
}
|
2017-11-20 05:35:35 +08:00
|
|
|
else if (key === 67 /* c */) {
|
|
|
|
addCurrentAsChild();
|
|
|
|
}
|
|
|
|
else if (key === 82 /* r */) {
|
|
|
|
addRecentAsChild()
|
|
|
|
}
|
2017-11-05 01:21:41 +08:00
|
|
|
else {
|
|
|
|
return; // avoid prevent default
|
|
|
|
}
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-05 01:39:26 +08:00
|
|
|
$(document).bind('keydown', 'alt+q', showDialog);
|
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
selectBoxEl.dblclick(e => {
|
2017-11-05 01:21:41 +08:00
|
|
|
setActiveNoteBasedOnRecentNotes();
|
|
|
|
});
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2017-11-05 05:03:15 +08:00
|
|
|
jumpToButtonEl.click(setActiveNoteBasedOnRecentNotes);
|
|
|
|
addLinkButtonEl.click(addLinkBasedOnRecentNotes);
|
2017-11-20 05:35:35 +08:00
|
|
|
addCurrentAsChildEl.click(addCurrentAsChild);
|
|
|
|
addRecentAsChildEl.click(addRecentAsChild);
|
2017-10-02 11:07:32 +08:00
|
|
|
|
2017-11-05 01:21:41 +08:00
|
|
|
return {
|
2017-11-05 01:39:26 +08:00
|
|
|
showDialog,
|
|
|
|
addRecentNote,
|
|
|
|
removeRecentNote
|
2017-11-05 01:21:41 +08:00
|
|
|
};
|
|
|
|
})();
|