From 7ea23586fe04814a9780e70dd0b7808e6620d98d Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 15 Jan 2018 20:54:22 -0500 Subject: [PATCH] improvements to search, fixing issue #1 --- public/javascripts/note_tree.js | 15 ++++++++++++--- public/javascripts/search_tree.js | 16 +++++++++------- routes/api/notes.js | 15 ++++++--------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js index 6e6a4c0c3..52df4b19b 100644 --- a/public/javascripts/note_tree.js +++ b/public/javascripts/note_tree.js @@ -200,7 +200,7 @@ const noteTree = (function() { return noteList; } - async function activateNode(notePath) { + async function expandToNote(notePath, expandOpts) { assertArguments(notePath); const runPath = getRunPath(notePath); @@ -213,14 +213,22 @@ const noteTree = (function() { const node = getNodesByNoteId(childNoteId).find(node => node.data.parent_note_id === parentNoteId); if (childNoteId === noteId) { - await node.setActive(); + return node; } else { - await node.setExpanded(); + await node.setExpanded(true, expandOpts); } parentNoteId = childNoteId; } + } + + async function activateNode(notePath) { + assertArguments(notePath); + + const node = await expandToNote(notePath); + + await node.setActive(); clearSelectedNodes(); } @@ -841,6 +849,7 @@ const noteTree = (function() { setNoteTreeBackgroundBasedOnProtectedStatus, setProtected, getCurrentNode, + expandToNote, activateNode, getCurrentNotePath, getNoteTitle, diff --git a/public/javascripts/search_tree.js b/public/javascripts/search_tree.js index 976a48025..15fd304b7 100644 --- a/public/javascripts/search_tree.js +++ b/public/javascripts/search_tree.js @@ -30,7 +30,7 @@ const searchTree = (function() { return treeEl.fancytree('getTree'); } - searchInputEl.keyup(e => { + searchInputEl.keyup(async e => { const searchText = searchInputEl.val(); if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") { @@ -39,12 +39,14 @@ const searchTree = (function() { } if (e && e.which === $.ui.keyCode.ENTER) { - server.get('notes?search=' + searchText).then(resp => { - // Pass a string to perform case insensitive matching - getTree().filterBranches(node => { - return resp.includes(node.data.note_id); - }); - }); + const noteIds = await server.get('notes?search=' + encodeURIComponent(searchText)); + + for (const noteId of noteIds) { + await noteTree.expandToNote(noteId, {noAnimation: true, noEvents: true}); + } + + // Pass a string to perform case insensitive matching + getTree().filterBranches(node => noteIds.includes(node.data.note_id)); } }).focus(); diff --git a/routes/api/notes.js b/routes/api/notes.js index e59a61aba..fded5a9ff 100644 --- a/routes/api/notes.js +++ b/routes/api/notes.js @@ -6,6 +6,7 @@ const auth = require('../../services/auth'); const sql = require('../../services/sql'); const notes = require('../../services/notes'); const log = require('../../services/log'); +const utils = require('../../services/utils'); const protected_session = require('../../services/protected_session'); const data_encryption = require('../../services/data_encryption'); const tree = require('../../services/tree'); @@ -59,17 +60,13 @@ router.put('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { })); router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => { - const search = '%' + req.query.search + '%'; + const search = '%' + utils.sanitizeSql(req.query.search) + '%'; - const result = await sql.getAll("SELECT note_id FROM notes WHERE note_title LIKE ? OR note_text LIKE ?", [search, search]); + // searching in protected notes is pointless because of encryption + const noteIds = await sql.getFirstColumn(`SELECT note_id FROM notes + WHERE is_deleted = 0 AND is_protected = 0 AND (note_title LIKE ? OR note_text LIKE ?)`, [search, search]); - const noteIdList = []; - - for (const res of result) { - noteIdList.push(res.note_id); - } - - res.send(noteIdList); + res.send(noteIds); })); router.put('/:noteId/sort', auth.checkApiAuth, wrap(async (req, res, next) => {