From 385d97a9b344aa092761ad98b620817f4df862b7 Mon Sep 17 00:00:00 2001 From: azivner Date: Thu, 16 Aug 2018 21:02:42 +0200 Subject: [PATCH] recent notes now don't display current note, unification of autocomplete source handling --- src/public/javascripts/dialogs/add_link.js | 21 +-------- .../javascripts/dialogs/jump_to_note.js | 25 +++-------- .../javascripts/services/note_autocomplete.js | 44 +++++++++++-------- src/routes/api/autocomplete.js | 8 ++-- 4 files changed, 39 insertions(+), 59 deletions(-) diff --git a/src/public/javascripts/dialogs/add_link.js b/src/public/javascripts/dialogs/add_link.js index 90d793269..659ebec7c 100644 --- a/src/public/javascripts/dialogs/add_link.js +++ b/src/public/javascripts/dialogs/add_link.js @@ -2,8 +2,8 @@ import cloningService from '../services/cloning.js'; import linkService from '../services/link.js'; import noteDetailService from '../services/note_detail.js'; import treeUtils from '../services/tree_utils.js'; -import server from "../services/server.js"; import noteDetailText from "../services/note_detail_text.js"; +import noteAutocompleteService from "../services/note_autocomplete.js"; const $dialog = $("#add-link-dialog"); const $form = $("#add-link-form"); @@ -55,24 +55,7 @@ async function showDialog() { } await $autoComplete.autocomplete({ - source: async function(request, response) { - const result = await server.get('autocomplete?query=' + encodeURIComponent(request.term)); - - if (result.length > 0) { - response(result.map(row => { - return { - label: row.label, - value: row.label + ' (' + row.value + ')' - } - })); - } - else { - response([{ - label: "No results", - value: "No results" - }]); - } - }, + source: noteAutocompleteService.autocompleteSource, minLength: 0, change: async (event, ui) => { if (!ui.item) { diff --git a/src/public/javascripts/dialogs/jump_to_note.js b/src/public/javascripts/dialogs/jump_to_note.js index df211d42c..39715b1e6 100644 --- a/src/public/javascripts/dialogs/jump_to_note.js +++ b/src/public/javascripts/dialogs/jump_to_note.js @@ -1,6 +1,7 @@ import treeService from '../services/tree.js'; -import server from '../services/server.js'; import searchNotesService from '../services/search_notes.js'; +import noteautocompleteService from '../services/note_autocomplete.js'; +import linkService from "../services/link.js"; const $dialog = $("#jump-to-note-dialog"); const $autoComplete = $("#jump-to-note-autocomplete"); @@ -19,22 +20,8 @@ async function showDialog() { }); await $autoComplete.autocomplete({ - source: async function(request, response) { - const result = await server.get('autocomplete?query=' + encodeURIComponent(request.term)); - - if (result.length > 0) { - response(result); - } - else { - response([{ - label: "No results", - value: "No results" - }]); - } - }, - focus: function(event, ui) { - event.preventDefault(); - }, + source: noteautocompleteService.autocompleteSource, + focus: event => event.preventDefault(), minLength: 0, autoFocus: true, select: function (event, ui) { @@ -42,7 +29,9 @@ async function showDialog() { return false; } - treeService.activateNode(ui.item.value); + const notePath = linkService.getNotePathFromLabel(ui.item.value); + + treeService.activateNode(notePath); $dialog.dialog('close'); } diff --git a/src/public/javascripts/services/note_autocomplete.js b/src/public/javascripts/services/note_autocomplete.js index ffa4325b4..2a4b9a8be 100644 --- a/src/public/javascripts/services/note_autocomplete.js +++ b/src/public/javascripts/services/note_autocomplete.js @@ -1,4 +1,26 @@ import server from "./server.js"; +import noteDetailService from "./note_detail.js"; + +async function autocompleteSource(request, response) { + const result = await server.get('autocomplete' + + '?query=' + encodeURIComponent(request.term) + + '¤tNoteId=' + noteDetailService.getCurrentNoteId()); + + if (result.length > 0) { + response(result.map(row => { + return { + label: row.label, + value: row.label + ' (' + row.value + ')' + } + })); + } + else { + response([{ + label: "No results", + value: "No results" + }]); + } +} async function initNoteAutocomplete($el) { if (!$el.hasClass("ui-autocomplete-input")) { @@ -12,24 +34,7 @@ async function initNoteAutocomplete($el) { await $el.autocomplete({ appendTo: $el.parent().parent(), - source: async function (request, response) { - const result = await server.get('autocomplete?query=' + encodeURIComponent(request.term)); - - if (result.length > 0) { - response(result.map(row => { - return { - label: row.label, - value: row.label + ' (' + row.value + ')' - } - })); - } - else { - response([{ - label: "No results", - value: "No results" - }]); - } - }, + source: autocompleteSource, minLength: 0, change: function (event, ui) { $el.trigger("change"); @@ -50,5 +55,6 @@ ko.bindingHandlers.noteAutocomplete = { }; export default { - initNoteAutocomplete + initNoteAutocomplete, + autocompleteSource } \ No newline at end of file diff --git a/src/routes/api/autocomplete.js b/src/routes/api/autocomplete.js index e26f2bdbb..e51b8395a 100644 --- a/src/routes/api/autocomplete.js +++ b/src/routes/api/autocomplete.js @@ -5,11 +5,12 @@ const repository = require('../../services/repository'); async function getAutocomplete(req) { const query = req.query.query; + const currentNoteId = req.query.currentNoteId || 'none'; let results; if (query.trim().length === 0) { - results = await getRecentNotes(); + results = await getRecentNotes(currentNoteId); } else { results = noteCacheService.findNotes(query); @@ -23,7 +24,7 @@ async function getAutocomplete(req) { }); } -async function getRecentNotes() { +async function getRecentNotes(currentNoteId) { const recentNotes = await repository.getEntities(` SELECT recent_notes.* @@ -33,9 +34,10 @@ async function getRecentNotes() { WHERE recent_notes.isDeleted = 0 AND branches.isDeleted = 0 + AND branches.noteId != ? ORDER BY dateCreated DESC - LIMIT 200`); + LIMIT 200`, [currentNoteId]); return recentNotes.map(rn => { return {