From 2a08aef885845c74f89aabe20602e31d00fb396d Mon Sep 17 00:00:00 2001 From: azivner Date: Thu, 26 Jul 2018 16:05:09 +0200 Subject: [PATCH] #129, recent notes are now visible in the jump to dialog --- .../javascripts/dialogs/jump_to_note.js | 39 +++++++------------ src/routes/api/autocomplete.js | 34 +++++++++++++++- src/views/index.ejs | 16 +++----- 3 files changed, 50 insertions(+), 39 deletions(-) diff --git a/src/public/javascripts/dialogs/jump_to_note.js b/src/public/javascripts/dialogs/jump_to_note.js index 36f699983..a03c1936e 100644 --- a/src/public/javascripts/dialogs/jump_to_note.js +++ b/src/public/javascripts/dialogs/jump_to_note.js @@ -5,8 +5,6 @@ import searchNotesService from '../services/search_notes.js'; const $dialog = $("#jump-to-note-dialog"); const $autoComplete = $("#jump-to-note-autocomplete"); -const $form = $("#jump-to-note-form"); -const $jumpToNoteButton = $("#jump-to-note-button"); const $showInFullTextButton = $("#show-in-full-text-button"); async function showDialog() { @@ -34,25 +32,22 @@ async function showDialog() { } }, focus: function(event, ui) { - return $(ui.item).val() !== 'No results'; + event.preventDefault(); }, - minLength: 2 + minLength: 0, + autoFocus: true, + select: function (event, ui) { + if (ui.item.value === 'No results') { + return false; + } + + treeService.activateNode(ui.item.value); + + $dialog.dialog('close'); + } }); -} -function getSelectedNotePath() { - const val = $autoComplete.val(); - return linkService.getNodePathFromLabel(val); -} - -function goToNote() { - const notePath = getSelectedNotePath(); - - if (notePath) { - treeService.activateNode(notePath); - - $dialog.dialog('close'); - } + $autoComplete.autocomplete("search", ""); } function showInFullText(e) { @@ -69,14 +64,6 @@ function showInFullText(e) { $dialog.dialog('close'); } -$form.submit(() => { - goToNote(); - - return false; -}); - -$jumpToNoteButton.click(goToNote); - $showInFullTextButton.click(showInFullText); $dialog.bind('keydown', 'ctrl+return', showInFullText); diff --git a/src/routes/api/autocomplete.js b/src/routes/api/autocomplete.js index 424a940f8..e26f2bdbb 100644 --- a/src/routes/api/autocomplete.js +++ b/src/routes/api/autocomplete.js @@ -1,20 +1,50 @@ "use strict"; const noteCacheService = require('../../services/note_cache'); +const repository = require('../../services/repository'); async function getAutocomplete(req) { const query = req.query.query; - const results = noteCacheService.findNotes(query); + let results; + + if (query.trim().length === 0) { + results = await getRecentNotes(); + } + else { + results = noteCacheService.findNotes(query); + } return results.map(res => { return { - value: res.title + ' (' + res.path + ')', + value: res.path, label: res.title } }); } +async function getRecentNotes() { + const recentNotes = await repository.getEntities(` + SELECT + recent_notes.* + FROM + recent_notes + JOIN branches USING(branchId) + WHERE + recent_notes.isDeleted = 0 + AND branches.isDeleted = 0 + ORDER BY + dateCreated DESC + LIMIT 200`); + + return recentNotes.map(rn => { + return { + path: rn.notePath, + title: noteCacheService.getNoteTitleForPath(rn.notePath.split('/')) + }; + }); +} + module.exports = { getAutocomplete }; \ No newline at end of file diff --git a/src/views/index.ejs b/src/views/index.ejs index 1d707d15f..eef78b766 100644 --- a/src/views/index.ejs +++ b/src/views/index.ejs @@ -302,18 +302,12 @@