trilium/src/public/javascripts/search_tree.js

62 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-11-24 10:10:37 +08:00
"use strict";
const searchTree = (function() {
2018-02-15 12:31:20 +08:00
const $tree = $("#tree");
const $searchInput = $("input[name='search-text']");
const $resetSearchButton = $("button#reset-search-button");
const $searchBox = $("#search-box");
2017-11-24 10:10:37 +08:00
2018-02-15 12:31:20 +08:00
$resetSearchButton.click(resetSearch);
2017-11-24 10:10:37 +08:00
function toggleSearch() {
2018-02-15 12:31:20 +08:00
if ($searchBox.is(":hidden")) {
$searchBox.show();
$searchInput.focus();
2017-11-24 10:10:37 +08:00
}
else {
resetSearch();
2018-02-15 12:31:20 +08:00
$searchBox.hide();
2017-11-24 10:10:37 +08:00
}
}
function resetSearch() {
2018-02-15 12:31:20 +08:00
$searchInput.val("");
2017-11-24 10:10:37 +08:00
getTree().clearFilter();
}
function getTree() {
2018-02-15 12:31:20 +08:00
return $tree.fancytree('getTree');
2017-11-24 10:10:37 +08:00
}
2018-02-15 12:31:20 +08:00
$searchInput.keyup(async e => {
const searchText = $searchInput.val();
2017-11-24 10:10:37 +08:00
if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") {
2018-02-15 12:31:20 +08:00
$resetSearchButton.click();
2017-11-24 10:10:37 +08:00
return;
}
if (e && e.which === $.ui.keyCode.ENTER) {
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();
$(document).bind('keydown', 'ctrl+s', e => {
toggleSearch();
e.preventDefault();
});
2017-11-24 10:10:37 +08:00
return {
toggleSearch
};
})();