trilium/src/public/javascripts/services/search_tree.js

76 lines
1.7 KiB
JavaScript
Raw Normal View History

import treeService from './tree.js';
2018-03-26 11:25:17 +08:00
import server from './server.js';
2018-03-24 23:18:46 +08:00
const $tree = $("#tree");
const $searchInput = $("input[name='search-text']");
const $resetSearchButton = $("#reset-search-button");
const $doSearchButton = $("#do-search-button");
const $saveSearchButton = $("#save-search-button");
const $searchBox = $("#search-box");
function toggleSearch() {
if ($searchBox.is(":hidden")) {
$searchBox.show();
$searchInput.focus();
2017-11-24 10:10:37 +08:00
}
2018-03-24 23:18:46 +08:00
else {
resetSearch();
2017-11-24 10:10:37 +08:00
2018-03-24 23:18:46 +08:00
$searchBox.hide();
2017-11-24 10:10:37 +08:00
}
2018-03-24 23:18:46 +08:00
}
2017-11-24 10:10:37 +08:00
2018-03-24 23:18:46 +08:00
function resetSearch() {
$searchInput.val("");
2017-11-24 10:10:37 +08:00
2018-03-24 23:18:46 +08:00
getTree().clearFilter();
}
2018-03-14 08:02:00 +08:00
2018-03-24 23:18:46 +08:00
function getTree() {
return $tree.fancytree('getTree');
}
2018-03-14 08:02:00 +08:00
2018-03-24 23:18:46 +08:00
async function doSearch() {
const searchText = $searchInput.val();
2018-03-14 08:02:00 +08:00
2018-03-24 23:18:46 +08:00
const noteIds = await server.get('search/' + encodeURIComponent(searchText));
2018-03-14 08:02:00 +08:00
2018-03-24 23:18:46 +08:00
for (const noteId of noteIds) {
2018-03-25 09:39:15 +08:00
await treeService.expandToNote(noteId, {noAnimation: true, noEvents: true});
2018-03-24 23:18:46 +08:00
}
2017-11-24 10:10:37 +08:00
2018-03-24 23:18:46 +08:00
// Pass a string to perform case insensitive matching
getTree().filterBranches(node => noteIds.includes(node.data.noteId));
}
2017-11-24 10:10:37 +08:00
2018-03-24 23:18:46 +08:00
async function saveSearch() {
const {noteId} = await server.post('search/' + encodeURIComponent($searchInput.val()));
2017-11-24 10:10:37 +08:00
2018-03-26 11:25:17 +08:00
resetSearch();
2018-03-25 09:39:15 +08:00
await treeService.reload();
2018-03-14 08:02:00 +08:00
2018-03-25 09:39:15 +08:00
await treeService.activateNode(noteId);
2018-03-24 23:18:46 +08:00
}
2018-03-24 23:18:46 +08:00
$searchInput.keyup(e => {
const searchText = $searchInput.val();
2018-03-24 23:18:46 +08:00
if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") {
$resetSearchButton.click();
return;
}
2018-03-14 08:02:00 +08:00
2018-03-24 23:18:46 +08:00
if (e && e.which === $.ui.keyCode.ENTER) {
doSearch();
}
}).focus();
2018-03-24 23:18:46 +08:00
$doSearchButton.click(doSearch);
$resetSearchButton.click(resetSearch);
2017-11-24 10:10:37 +08:00
2018-03-24 23:18:46 +08:00
$saveSearchButton.click(saveSearch);
2018-03-24 23:18:46 +08:00
export default {
toggleSearch
};