From a87f4d8653ffb448bdd311065835c79a90c62dcd Mon Sep 17 00:00:00 2001 From: azivner Date: Fri, 25 Jan 2019 22:18:34 +0100 Subject: [PATCH] search can be triggered from URL, closes #385 --- src/public/javascripts/desktop.js | 2 ++ .../javascripts/services/search_notes.js | 12 ++++++++- src/public/javascripts/services/tree.js | 25 ++++++++++++------- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/public/javascripts/desktop.js b/src/public/javascripts/desktop.js index 65e752985..51262c8ed 100644 --- a/src/public/javascripts/desktop.js +++ b/src/public/javascripts/desktop.js @@ -121,6 +121,8 @@ $("#export-note-button").click(function () { macInit.init(); +searchNotesService.init(); // should be in front of treeService since that one manipulates address bar hash + treeService.showTree(); entrypoints.registerEntrypoints(); diff --git a/src/public/javascripts/services/search_notes.js b/src/public/javascripts/services/search_notes.js index 916391960..b65965f33 100644 --- a/src/public/javascripts/services/search_notes.js +++ b/src/public/javascripts/services/search_notes.js @@ -76,6 +76,15 @@ async function saveSearch() { await treeService.activateNote(noteId); } +function init() { + const hashValue = treeService.getHashValueFromAddress(); + + if (hashValue.startsWith("search=")) { + showSearch(); + doSearch(hashValue.substr(7)); + } +} + $searchInput.keyup(e => { const searchText = $searchInput.val(); @@ -100,5 +109,6 @@ export default { toggleSearch, resetSearch, showSearch, - doSearch + doSearch, + init }; \ No newline at end of file diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js index df9df1672..a8aa0ce71 100644 --- a/src/public/javascripts/services/tree.js +++ b/src/public/javascripts/services/tree.js @@ -483,16 +483,20 @@ async function reload() { await getTree().reload(notes); } -function getNotePathFromAddress() { - return document.location.hash.substr(1); // strip initial # +function isNotePathInAddress() { + return getHashValueFromAddress().startsWith("root"); +} + +function getHashValueFromAddress() { + return document.location.hash ? document.location.hash.substr(1) : ""; // strip initial # } async function loadTree() { const resp = await server.get('tree'); startNotePath = resp.startNotePath; - if (document.location.hash) { - startNotePath = getNotePathFromAddress(); + if (isNotePathInAddress()) { + startNotePath = getHashValueFromAddress(); } return await treeBuilder.prepareTree(resp.notes, resp.branches, resp.relations); @@ -707,12 +711,14 @@ utils.bindShortcut('ctrl+p', createNoteInto); utils.bindShortcut('ctrl+.', scrollToCurrentNote); $(window).bind('hashchange', function() { - const notePath = getNotePathFromAddress(); + if (isNotePathInAddress()) { + const notePath = getHashValueFromAddress(); - if (notePath !== '-' && getCurrentNotePath() !== notePath) { - console.debug("Switching to " + notePath + " because of hash change"); + if (notePath !== '-' && getCurrentNotePath() !== notePath) { + console.debug("Switching to " + notePath + " because of hash change"); - activateNote(notePath); + activateNote(notePath); + } } }); @@ -745,5 +751,6 @@ export default { showTree, loadTree, treeInitialized, - setExpandedToServer + setExpandedToServer, + getHashValueFromAddress }; \ No newline at end of file