diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js index cb7b543d5..d90229e07 100644 --- a/public/javascripts/note_tree.js +++ b/public/javascripts/note_tree.js @@ -305,8 +305,6 @@ const noteTree = (function() { } function initFancyTree(noteTree) { - console.log(noteTree); - const keybindings = { "insert": node => { const parentNoteId = node.data.note_pid; @@ -493,30 +491,6 @@ const noteTree = (function() { } } - function showSearch() { - $("#search-box").show(); - - $("input[name=search]").focus(); - } - - function toggleSearch() { - if ($("#search-box:hidden").length) { - showSearch(); - } - else { - resetSearch(); - - $("#search-box").hide(); - } - } - - function resetSearch() { - $("input[name=search]").val(""); - - const tree = getTree(); - tree.clearFilter(); - } - function setCurrentNoteTreeBasedOnProtectedStatus() { getCurrentClones().map(node => node.toggleClass("protected", !!node.data.is_protected)); } @@ -627,36 +601,11 @@ const noteTree = (function() { showMessage("Created!"); } - $("button#reset-search-button").click(resetSearch); - - $("input[name=search]").keyup(e => { - const searchString = $("input[name=search]").val(); - - if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchString) === "") { - $("button#reset-search-button").click(); - return; - } - - if (e && e.which === $.ui.keyCode.ENTER) { - $.get(baseApiUrl + 'notes?search=' + searchString).then(resp => { - console.log("search: ", resp); - - // Pass a string to perform case insensitive matching - getTree().filterBranches(node => { - return resp.includes(node.data.note_id); - }); - }); - } - }).focus(); - - $(document).bind('keydown', 'alt+s', showSearch); - return { getTreeLoadTime, reload, collapseTree, scrollToCurrentNote, - toggleSearch, setCurrentNoteTreeBasedOnProtectedStatus, getCurrentNode, activateNode, diff --git a/public/javascripts/search_tree.js b/public/javascripts/search_tree.js new file mode 100644 index 000000000..60279776e --- /dev/null +++ b/public/javascripts/search_tree.js @@ -0,0 +1,60 @@ +"use strict"; + +const searchTree = (function() { + const treeEl = $("#tree"); + const searchInputEl = $("input[name='search-text']"); + const resetSearchButton = $("button#reset-search-button"); + const searchBoxEl = $("#search-box"); + + resetSearchButton.click(resetSearch); + + function showSearch() { + searchBoxEl.show(); + searchInputEl.focus(); + } + + function toggleSearch() { + if (searchBoxEl.is(":hidden")) { + showSearch(); + } + else { + resetSearch(); + + searchBoxEl.hide(); + } + } + + function resetSearch() { + searchInputEl.val(""); + + getTree().clearFilter(); + } + + function getTree() { + return treeEl.fancytree('getTree'); + } + + searchInputEl.keyup(e => { + const searchText = searchInputEl.val(); + + if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") { + resetSearchButton.click(); + return; + } + + if (e && e.which === $.ui.keyCode.ENTER) { + $.get(baseApiUrl + 'notes?search=' + searchText).then(resp => { + // Pass a string to perform case insensitive matching + getTree().filterBranches(node => { + return resp.includes(node.data.note_id); + }); + }); + } + }).focus(); + + $(document).bind('keydown', 'alt+s', showSearch); + + return { + toggleSearch + }; +})(); \ No newline at end of file diff --git a/views/index.ejs b/views/index.ejs index 747b81083..73a526fdf 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -49,14 +49,14 @@ Collapse tree - + Search in notes