2018-03-26 01:41:29 +08:00
|
|
|
import treeService from './tree.js';
|
2019-03-30 06:24:41 +08:00
|
|
|
import treeCache from "./tree_cache.js";
|
2018-03-26 11:25:17 +08:00
|
|
|
import server from './server.js';
|
2019-10-20 16:00:18 +08:00
|
|
|
import toastService from "./toast.js";
|
2018-03-25 23:09:17 +08:00
|
|
|
|
2018-03-24 23:18:46 +08:00
|
|
|
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
|
|
|
|
2019-04-01 03:19:10 +08:00
|
|
|
const helpText = `
|
|
|
|
<strong>Search tips</strong> - also see <button class="btn btn-sm" type="button" data-help-page="Search">complete help on search</button>
|
|
|
|
<p>
|
|
|
|
<ul>
|
|
|
|
<li>Just enter any text for full text search</li>
|
|
|
|
<li><code>@abc</code> - returns notes with label abc</li>
|
|
|
|
<li><code>@year=2019</code> - matches notes with label <code>year</code> having value <code>2019</code></li>
|
|
|
|
<li><code>@rock @pop</code> - matches notes which have both <code>rock</code> and <code>pop</code> labels</li>
|
|
|
|
<li><code>@rock or @pop</code> - only one of the labels must be present</li>
|
|
|
|
<li><code>@year<=2000</code> - numerical comparison (also >, >=, <).</li>
|
|
|
|
<li><code>@dateCreated>=MONTH-1</code> - notes created in the last month</li>
|
|
|
|
<li><code>=handler</code> - will execute script defined in <code>handler</code> relation to get results</li>
|
|
|
|
</ul>
|
|
|
|
</p>`;
|
|
|
|
|
2018-06-06 11:28:10 +08:00
|
|
|
function showSearch() {
|
2019-03-17 02:57:39 +08:00
|
|
|
$searchBox.slideDown();
|
2019-03-31 00:41:53 +08:00
|
|
|
|
|
|
|
$searchBox.tooltip({
|
|
|
|
trigger: 'focus',
|
|
|
|
html: true,
|
2019-04-01 03:19:10 +08:00
|
|
|
title: helpText,
|
2019-03-31 00:41:53 +08:00
|
|
|
placement: 'right',
|
|
|
|
delay: {
|
|
|
|
show: 500, // necessary because sliding out may cause wrong position
|
2019-04-01 03:19:10 +08:00
|
|
|
hide: 200
|
2019-03-31 00:41:53 +08:00
|
|
|
}
|
|
|
|
});
|
2019-04-01 03:19:10 +08:00
|
|
|
|
2019-11-10 00:45:22 +08:00
|
|
|
$searchInput.trigger('focus');
|
2018-06-06 11:28:10 +08:00
|
|
|
}
|
|
|
|
|
2018-06-08 07:50:16 +08:00
|
|
|
function hideSearch() {
|
|
|
|
resetSearch();
|
|
|
|
|
|
|
|
$searchResults.hide();
|
2019-03-17 02:57:39 +08:00
|
|
|
$searchBox.slideUp();
|
2018-06-08 07:50:16 +08:00
|
|
|
}
|
|
|
|
|
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-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
|
|
|
|
2019-04-01 03:19:10 +08:00
|
|
|
if (searchText.trim().length === 0) {
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.showMessage("Please enter search criteria first.");
|
2019-04-01 03:19:10 +08:00
|
|
|
|
2019-11-10 00:45:22 +08:00
|
|
|
$searchInput.trigger('focus');
|
2019-04-01 03:19:10 +08:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-05-30 02:27:48 +08:00
|
|
|
$searchBox.tooltip("hide");
|
|
|
|
|
2019-04-03 04:52:42 +08:00
|
|
|
const response = await server.get('search/' + encodeURIComponent(searchText));
|
|
|
|
|
|
|
|
if (!response.success) {
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.showError("Search failed.", 3000);
|
2019-04-03 04:52:42 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-03-14 08:02:00 +08:00
|
|
|
|
2018-06-04 08:42:25 +08:00
|
|
|
$searchResultsInner.empty();
|
|
|
|
$searchResults.show();
|
|
|
|
|
2019-04-03 04:52:42 +08:00
|
|
|
for (const result of response.results) {
|
2018-06-04 08:42:25 +08:00
|
|
|
const link = $('<a>', {
|
|
|
|
href: 'javascript:',
|
|
|
|
text: result.title
|
2018-08-15 16:14:14 +08:00
|
|
|
}).attr('data-action', 'note').attr('data-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);
|
|
|
|
}
|
2019-03-30 06:24:41 +08:00
|
|
|
|
|
|
|
// have at least some feedback which is good especially in situations
|
|
|
|
// when the result list does not change with a query
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.showMessage("Search finished successfully.");
|
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() {
|
2019-03-30 06:24:41 +08:00
|
|
|
const searchString = $searchInput.val().trim();
|
2017-11-24 10:10:37 +08:00
|
|
|
|
2019-03-30 06:24:41 +08:00
|
|
|
if (searchString.length === 0) {
|
|
|
|
alert("Write some search criteria first so there is something to save.");
|
|
|
|
return;
|
|
|
|
}
|
2018-03-26 11:25:17 +08:00
|
|
|
|
2019-03-30 06:24:41 +08:00
|
|
|
let activeNode = treeService.getActiveNode();
|
|
|
|
const parentNote = await treeCache.getNote(activeNode.data.noteId);
|
2018-03-14 08:02:00 +08:00
|
|
|
|
2019-03-30 06:24:41 +08: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 23:18:46 +08:00
|
|
|
}
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2019-03-30 07:12:32 +08:00
|
|
|
async function refreshSearch() {
|
|
|
|
const activeNode = treeService.getActiveNode();
|
|
|
|
|
|
|
|
activeNode.load(true);
|
|
|
|
activeNode.setExpanded(true);
|
|
|
|
|
2019-10-20 16:00:18 +08:00
|
|
|
toastService.showMessage("Saved search note refreshed.");
|
2019-03-30 07:12:32 +08:00
|
|
|
}
|
|
|
|
|
2019-11-20 04:11:20 +08:00
|
|
|
function searchInSubtree(noteId) {
|
|
|
|
showSearch();
|
|
|
|
|
|
|
|
$searchInput.val(`@in=${noteId} @text*=*`);
|
|
|
|
}
|
|
|
|
|
2019-01-26 05:18:34 +08:00
|
|
|
function init() {
|
2019-05-15 04:29:47 +08:00
|
|
|
const hashValue = document.location.hash ? document.location.hash.substr(1) : ""; // strip initial #
|
2019-01-26 05:18:34 +08:00
|
|
|
|
|
|
|
if (hashValue.startsWith("search=")) {
|
|
|
|
showSearch();
|
|
|
|
doSearch(hashValue.substr(7));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 00:45:22 +08:00
|
|
|
$searchInput.on('keyup',e => {
|
2018-03-24 23:18:46 +08:00
|
|
|
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) === "") {
|
2019-11-10 00:39:48 +08:00
|
|
|
$resetSearchButton.trigger('click');
|
2018-03-24 23:18:46 +08:00
|
|
|
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();
|
|
|
|
}
|
2019-01-11 04:04:06 +08:00
|
|
|
});
|
2017-12-27 08:16:04 +08:00
|
|
|
|
2019-11-10 00:39:48 +08:00
|
|
|
$doSearchButton.on('click', () => doSearch()); // keep long form because of argument
|
|
|
|
$resetSearchButton.on('click', resetSearch);
|
2017-11-24 10:10:37 +08:00
|
|
|
|
2019-11-10 00:39:48 +08:00
|
|
|
$saveSearchButton.on('click', saveSearch);
|
2018-03-24 12:54:50 +08:00
|
|
|
|
2019-11-10 00:39:48 +08:00
|
|
|
$closeSearchButton.on('click', hideSearch);
|
2018-06-08 07:50:16 +08:00
|
|
|
|
2018-03-24 23:18:46 +08:00
|
|
|
export default {
|
2018-06-06 11:28:10 +08:00
|
|
|
toggleSearch,
|
|
|
|
resetSearch,
|
|
|
|
showSearch,
|
2019-03-30 07:12:32 +08:00
|
|
|
refreshSearch,
|
2019-01-26 05:18:34 +08:00
|
|
|
doSearch,
|
2019-04-01 03:19:10 +08:00
|
|
|
init,
|
2019-11-20 04:11:20 +08:00
|
|
|
searchInSubtree,
|
2019-04-01 03:19:10 +08:00
|
|
|
getHelpText: () => helpText
|
2018-03-24 23:18:46 +08:00
|
|
|
};
|