2018-03-25 13:41:29 -04:00
|
|
|
import treeService from './tree.js';
|
2019-03-29 23:24:41 +01:00
|
|
|
import treeCache from "./tree_cache.js";
|
2018-03-25 23:25:17 -04:00
|
|
|
import server from './server.js';
|
2019-03-29 23:24:41 +01:00
|
|
|
import infoService from "./info.js";
|
2018-03-25 11:09:17 -04:00
|
|
|
|
2018-03-24 11:18:46 -04: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-03 20:42:25 -04:00
|
|
|
const $searchResults = $("#search-results");
|
|
|
|
const $searchResultsInner = $("#search-results-inner");
|
2018-06-07 19:50:16 -04:00
|
|
|
const $closeSearchButton = $("#close-search-button");
|
2018-03-24 11:18:46 -04:00
|
|
|
|
2018-06-05 23:28:10 -04:00
|
|
|
function showSearch() {
|
2019-03-16 19:57:39 +01:00
|
|
|
$searchBox.slideDown();
|
2018-06-05 23:28:10 -04:00
|
|
|
$searchInput.focus();
|
|
|
|
}
|
|
|
|
|
2018-06-07 19:50:16 -04:00
|
|
|
function hideSearch() {
|
|
|
|
resetSearch();
|
|
|
|
|
|
|
|
$searchResults.hide();
|
2019-03-16 19:57:39 +01:00
|
|
|
$searchBox.slideUp();
|
2018-06-07 19:50:16 -04:00
|
|
|
}
|
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
function toggleSearch() {
|
|
|
|
if ($searchBox.is(":hidden")) {
|
2018-06-05 23:28:10 -04:00
|
|
|
showSearch();
|
2017-11-23 21:10:37 -05:00
|
|
|
}
|
2018-03-24 11:18:46 -04:00
|
|
|
else {
|
2018-06-07 19:50:16 -04:00
|
|
|
hideSearch();
|
2017-11-23 21:10:37 -05:00
|
|
|
}
|
2018-03-24 11:18:46 -04:00
|
|
|
}
|
2017-11-23 21:10:37 -05:00
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
function resetSearch() {
|
|
|
|
$searchInput.val("");
|
|
|
|
}
|
2018-03-13 20:02:00 -04:00
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
function getTree() {
|
|
|
|
return $tree.fancytree('getTree');
|
|
|
|
}
|
2018-03-13 20:02:00 -04:00
|
|
|
|
2018-06-05 23:28:10 -04:00
|
|
|
async function doSearch(searchText) {
|
|
|
|
if (searchText) {
|
|
|
|
$searchInput.val(searchText);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
searchText = $searchInput.val();
|
|
|
|
}
|
2018-03-13 20:02:00 -04:00
|
|
|
|
2018-06-03 20:42:25 -04:00
|
|
|
const results = await server.get('search/' + encodeURIComponent(searchText));
|
2018-03-13 20:02:00 -04:00
|
|
|
|
2018-06-03 20:42:25 -04:00
|
|
|
$searchResultsInner.empty();
|
|
|
|
$searchResults.show();
|
|
|
|
|
|
|
|
for (const result of results) {
|
|
|
|
const link = $('<a>', {
|
|
|
|
href: 'javascript:',
|
|
|
|
text: result.title
|
2018-08-15 10:14:14 +02:00
|
|
|
}).attr('data-action', 'note').attr('data-note-path', result.path);
|
2017-11-23 21:10:37 -05:00
|
|
|
|
2018-06-03 20:42:25 -04:00
|
|
|
const $result = $('<li>').append(link);
|
|
|
|
|
|
|
|
$searchResultsInner.append($result);
|
|
|
|
}
|
2019-03-29 23:24:41 +01:00
|
|
|
|
|
|
|
// have at least some feedback which is good especially in situations
|
|
|
|
// when the result list does not change with a query
|
|
|
|
infoService.showMessage("Search finished successfully.");
|
2018-03-24 11:18:46 -04:00
|
|
|
}
|
2017-11-23 21:10:37 -05:00
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
async function saveSearch() {
|
2019-03-29 23:24:41 +01:00
|
|
|
const searchString = $searchInput.val().trim();
|
2017-11-23 21:10:37 -05:00
|
|
|
|
2019-03-29 23:24:41 +01:00
|
|
|
if (searchString.length === 0) {
|
|
|
|
alert("Write some search criteria first so there is something to save.");
|
|
|
|
return;
|
|
|
|
}
|
2018-03-25 23:25:17 -04:00
|
|
|
|
2019-03-29 23:24:41 +01:00
|
|
|
let activeNode = treeService.getActiveNode();
|
|
|
|
const parentNote = await treeCache.getNote(activeNode.data.noteId);
|
2018-03-13 20:02:00 -04:00
|
|
|
|
2019-03-29 23:24:41 +01:00
|
|
|
if (parentNote.type === 'search') {
|
|
|
|
activeNode = activeNode.getParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
await treeService.createNote(activeNode, activeNode.data.noteId, 'into', {
|
|
|
|
type: "search",
|
|
|
|
mime: "application/json",
|
|
|
|
title: searchString,
|
|
|
|
content: JSON.stringify({ searchString: searchString })
|
|
|
|
});
|
|
|
|
|
|
|
|
resetSearch();
|
2018-03-24 11:18:46 -04:00
|
|
|
}
|
2018-03-23 23:08:29 -04:00
|
|
|
|
2019-01-25 22:18:34 +01:00
|
|
|
function init() {
|
|
|
|
const hashValue = treeService.getHashValueFromAddress();
|
|
|
|
|
|
|
|
if (hashValue.startsWith("search=")) {
|
|
|
|
showSearch();
|
|
|
|
doSearch(hashValue.substr(7));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
$searchInput.keyup(e => {
|
|
|
|
const searchText = $searchInput.val();
|
2018-03-23 23:08:29 -04:00
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") {
|
|
|
|
$resetSearchButton.click();
|
|
|
|
return;
|
|
|
|
}
|
2018-03-13 20:02:00 -04:00
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
if (e && e.which === $.ui.keyCode.ENTER) {
|
|
|
|
doSearch();
|
|
|
|
}
|
2019-01-10 21:04:06 +01:00
|
|
|
});
|
2017-12-26 19:16:04 -05:00
|
|
|
|
2018-08-31 19:23:56 +02:00
|
|
|
$doSearchButton.click(() => doSearch()); // keep long form because of argument
|
2018-03-24 11:18:46 -04:00
|
|
|
$resetSearchButton.click(resetSearch);
|
2017-11-23 21:10:37 -05:00
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
$saveSearchButton.click(saveSearch);
|
2018-03-24 00:54:50 -04:00
|
|
|
|
2018-06-07 19:50:16 -04:00
|
|
|
$closeSearchButton.click(hideSearch);
|
|
|
|
|
2018-03-24 11:18:46 -04:00
|
|
|
export default {
|
2018-06-05 23:28:10 -04:00
|
|
|
toggleSearch,
|
|
|
|
resetSearch,
|
|
|
|
showSearch,
|
2019-01-25 22:18:34 +01:00
|
|
|
doSearch,
|
|
|
|
init
|
2018-03-24 11:18:46 -04:00
|
|
|
};
|