2017-11-05 07:38:50 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-03-26 01:02:39 +08:00
|
|
|
import treeService from '../services/tree_service.js';
|
|
|
|
import server from '../services/server.js';
|
|
|
|
import messaging from '../services/messaging.js';
|
2018-03-25 23:09:17 +08:00
|
|
|
|
|
|
|
const $showDialogButton = $("#recent-notes-button");
|
|
|
|
const $dialog = $("#recent-notes-dialog");
|
|
|
|
const $searchInput = $('#recent-notes-search-input');
|
|
|
|
|
|
|
|
// list of recent note paths
|
|
|
|
let list = [];
|
|
|
|
|
|
|
|
async function reload() {
|
|
|
|
const result = await server.get('recent-notes');
|
|
|
|
|
|
|
|
list = result.map(r => r.notePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addRecentNote(branchId, notePath) {
|
|
|
|
setTimeout(async () => {
|
|
|
|
// we include the note into recent list only if the user stayed on the note at least 5 seconds
|
|
|
|
if (notePath && notePath === treeService.getCurrentNotePath()) {
|
|
|
|
const result = await server.put('recent-notes/' + branchId + '/' + encodeURIComponent(notePath));
|
|
|
|
|
|
|
|
list = result.map(r => r.notePath);
|
|
|
|
}
|
|
|
|
}, 1500);
|
|
|
|
}
|
|
|
|
|
|
|
|
function showDialog() {
|
|
|
|
glob.activeDialog = $dialog;
|
|
|
|
|
|
|
|
$dialog.dialog({
|
|
|
|
modal: true,
|
|
|
|
width: 800,
|
|
|
|
height: 100,
|
|
|
|
position: { my: "center top+100", at: "top", of: window }
|
|
|
|
});
|
2018-02-06 12:50:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$searchInput.val('');
|
2017-09-10 00:06:15 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
// remove the current note
|
|
|
|
const recNotes = list.filter(note => note !== treeService.getCurrentNotePath());
|
2017-12-03 23:42:23 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$searchInput.autocomplete({
|
|
|
|
source: recNotes.map(notePath => {
|
|
|
|
let noteTitle;
|
2017-11-05 11:46:50 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
try {
|
|
|
|
noteTitle = treeService.getNotePathTitle(notePath);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
noteTitle = "[error - can't find note title]";
|
2017-11-29 09:52:38 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
messaging.logError("Could not find title for notePath=" + notePath + ", stack=" + e.stack);
|
2017-11-05 01:21:41 +08:00
|
|
|
}
|
2018-02-06 12:50:25 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
return {
|
|
|
|
label: noteTitle,
|
|
|
|
value: notePath
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
minLength: 0,
|
|
|
|
autoFocus: true,
|
|
|
|
select: function (event, ui) {
|
|
|
|
treeService.activateNode(ui.item.value);
|
|
|
|
|
|
|
|
$searchInput.autocomplete('destroy');
|
|
|
|
$dialog.dialog('close');
|
|
|
|
},
|
|
|
|
focus: function (event, ui) {
|
|
|
|
event.preventDefault();
|
|
|
|
},
|
|
|
|
close: function (event, ui) {
|
|
|
|
if (event.keyCode === 27) { // escape closes dialog
|
2018-02-10 21:44:34 +08:00
|
|
|
$searchInput.autocomplete('destroy');
|
|
|
|
$dialog.dialog('close');
|
2017-11-05 01:21:41 +08:00
|
|
|
}
|
2018-03-25 23:09:17 +08:00
|
|
|
else {
|
|
|
|
// keep autocomplete open
|
|
|
|
// we're kind of abusing autocomplete to work in a way which it's not designed for
|
|
|
|
$searchInput.autocomplete("search", "");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
create: () => $searchInput.autocomplete("search", ""),
|
|
|
|
classes: {
|
|
|
|
"ui-autocomplete": "recent-notes-autocomplete"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-10-07 10:46:30 +08:00
|
|
|
|
2018-03-26 01:13:26 +08:00
|
|
|
setTimeout(reload, 100);
|
2017-12-03 23:42:23 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$(document).bind('keydown', 'ctrl+e', e => {
|
|
|
|
showDialog();
|
2017-12-19 12:41:13 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
2017-11-05 01:39:26 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
$showDialogButton.click(showDialog);
|
2018-03-24 12:54:50 +08:00
|
|
|
|
2018-03-25 23:09:17 +08:00
|
|
|
export default {
|
|
|
|
showDialog,
|
|
|
|
addRecentNote,
|
|
|
|
reload
|
|
|
|
};
|