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']");
|
2018-03-14 08:02:00 +08:00
|
|
|
const $resetSearchButton = $("#reset-search-button");
|
|
|
|
const $doSearchButton = $("#do-search-button");
|
|
|
|
const $saveSearchButton = $("#save-search-button");
|
2018-02-15 12:31:20 +08:00
|
|
|
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-03-14 08:02:00 +08:00
|
|
|
async function doSearch() {
|
|
|
|
const searchText = $searchInput.val();
|
|
|
|
|
|
|
|
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
|
|
|
|
getTree().filterBranches(node => noteIds.includes(node.data.noteId));
|
|
|
|
}
|
|
|
|
|
|
|
|
$searchInput.keyup(e => {
|
2018-02-15 12:31:20 +08:00
|
|
|
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) {
|
2018-03-14 08:02:00 +08:00
|
|
|
doSearch();
|
2017-11-24 10:10:37 +08:00
|
|
|
}
|
|
|
|
}).focus();
|
|
|
|
|
2018-03-14 08:02:00 +08:00
|
|
|
$doSearchButton.click(doSearch);
|
|
|
|
|
|
|
|
$saveSearchButton.click(() => alert("Save search"));
|
|
|
|
|
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
|
|
|
|
};
|
|
|
|
})();
|