2017-11-24 10:10:37 +08:00
|
|
|
"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 toggleSearch() {
|
|
|
|
if (searchBoxEl.is(":hidden")) {
|
2017-12-27 08:16:04 +08:00
|
|
|
searchBoxEl.show();
|
|
|
|
searchInputEl.focus();
|
2017-11-24 10:10:37 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
resetSearch();
|
|
|
|
|
|
|
|
searchBoxEl.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetSearch() {
|
|
|
|
searchInputEl.val("");
|
|
|
|
|
|
|
|
getTree().clearFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTree() {
|
|
|
|
return treeEl.fancytree('getTree');
|
|
|
|
}
|
|
|
|
|
2018-01-16 09:54:22 +08:00
|
|
|
searchInputEl.keyup(async e => {
|
2017-11-24 10:10:37 +08:00
|
|
|
const searchText = searchInputEl.val();
|
|
|
|
|
|
|
|
if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") {
|
|
|
|
resetSearchButton.click();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e && e.which === $.ui.keyCode.ENTER) {
|
2018-01-16 09:54:22 +08:00
|
|
|
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
|
2018-01-29 08:30:14 +08:00
|
|
|
getTree().filterBranches(node => noteIds.includes(node.data.noteId));
|
2017-11-24 10:10:37 +08:00
|
|
|
}
|
|
|
|
}).focus();
|
|
|
|
|
2018-01-14 12:03:17 +08:00
|
|
|
$(document).bind('keydown', 'ctrl+s', e => {
|
2017-12-27 08:16:04 +08:00
|
|
|
toggleSearch();
|
|
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2017-11-24 10:10:37 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
toggleSearch
|
|
|
|
};
|
|
|
|
})();
|