2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2018-03-26 11:25:17 +08:00
|
|
|
import server from './server.js';
|
2018-06-04 08:42:25 +08:00
|
|
|
import treeUtils from "./tree_utils.js";
|
2018-03-25 23:09:17 +08:00
|
|
|
|
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");
|
2018-06-04 08:42:25 +08:00
|
|
|
const $searchResults = $("#search-results");
|
|
|
|
const $searchResultsInner = $("#search-results-inner");
|
2018-06-08 07:50:16 +08:00
|
|
|
const $closeSearchButton = $("#close-search-button");
|
2018-03-24 23:18:46 +08:00
|
|
|
|
2018-06-06 11:28:10 +08:00
|
|
|
function showSearch() {
|
|
|
|
$searchBox.show();
|
|
|
|
$searchInput.focus();
|
|
|
|
}
|
|
|
|
|
2018-06-08 07:50:16 +08:00
|
|
|
function hideSearch() {
|
|
|
|
resetSearch();
|
|
|
|
|
|
|
|
$searchResults.hide();
|
|
|
|
$searchBox.hide();
|
|
|
|
}
|
|
|
|
|
2018-03-24 23:18:46 +08:00
|
|
|
function toggleSearch() {
|
|
|
|
if ($searchBox.is(":hidden")) {
|
2018-06-06 11:28:10 +08:00
|
|
|
showSearch();
|
2017-11-24 10:10:37 +08:00
|
|
|
}
|
2018-03-24 23:18:46 +08:00
|
|
|
else {
|
2018-06-08 07:50:16 +08:00
|
|
|
hideSearch();
|
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("");
|
|
|
|
}
|
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-06-06 11:28:10 +08:00
|
|
|
async function doSearch(searchText) {
|
|
|
|
if (searchText) {
|
|
|
|
$searchInput.val(searchText);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
searchText = $searchInput.val();
|
|
|
|
}
|
2018-03-14 08:02:00 +08:00
|
|
|
|
2018-06-04 08:42:25 +08:00
|
|
|
const results = await server.get('search/' + encodeURIComponent(searchText));
|
2018-03-14 08:02:00 +08:00
|
|
|
|
2018-06-04 08:42:25 +08:00
|
|
|
$searchResultsInner.empty();
|
|
|
|
$searchResults.show();
|
|
|
|
|
|
|
|
for (const result of results) {
|
|
|
|
const link = $('<a>', {
|
|
|
|
href: 'javascript:',
|
|
|
|
text: result.title
|
|
|
|
}).attr('action', 'note').attr('note-path', result.path);
|
2017-11-24 10:10:37 +08:00
|
|
|
|
2018-06-04 08:42:25 +08:00
|
|
|
const $result = $('<li>').append(link);
|
|
|
|
|
|
|
|
$searchResultsInner.append($result);
|
|
|
|
}
|
2018-03-24 23:18:46 +08:00
|
|
|
}
|
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 11:08:29 +08:00
|
|
|
|
2018-03-24 23:18:46 +08:00
|
|
|
$searchInput.keyup(e => {
|
|
|
|
const searchText = $searchInput.val();
|
2018-03-24 11:08:29 +08:00
|
|
|
|
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();
|
2017-12-27 08:16:04 +08:00
|
|
|
|
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 12:54:50 +08:00
|
|
|
|
2018-06-08 07:50:16 +08:00
|
|
|
$closeSearchButton.click(hideSearch);
|
|
|
|
|
2018-03-24 23:18:46 +08:00
|
|
|
export default {
|
2018-06-06 11:28:10 +08:00
|
|
|
toggleSearch,
|
|
|
|
resetSearch,
|
|
|
|
showSearch,
|
|
|
|
doSearch
|
2018-03-24 23:18:46 +08:00
|
|
|
};
|