import BasicWidget from "./basic_widget.js";
import treeService from "../services/tree.js";
import treeCache from "../services/tree_cache.js";
import toastService from "../services/toast.js";
import appContext from "../services/app_context.js";
const helpText = `
Search tips - also see complete help on search
Just enter any text for full text search
@abc
- returns notes with label abc
@year=2019
- matches notes with label year
having value 2019
@rock @pop
- matches notes which have both rock
and pop
labels
@rock or @pop
- only one of the labels must be present
@year<=2000
- numerical comparison (also >, >=, <).
@dateCreated>=MONTH-1
- notes created in the last month
=handler
- will execute script defined in handler
relation to get results
`;
const TPL = `
`;
export default class SearchBoxWidget extends BasicWidget {
doRender() {
this.$widget = $(TPL);
this.$searchBox = this.$widget;
this.$closeSearchButton = this.$widget.find(".close-search-button");
this.$searchInput = this.$widget.find("input[name='search-text']");
this.$resetSearchButton = this.$widget.find(".reset-search-button");
this.$doSearchButton = this.$widget.find(".do-search-button");
this.$saveSearchButton = this.$widget.find(".save-search-button");
this.$searchInput.on('keyup',e => {
const searchText = this.$searchInput.val();
if (e && e.which === $.ui.keyCode.ESCAPE || $.trim(searchText) === "") {
this.$resetSearchButton.trigger('click');
return;
}
if (e && e.which === $.ui.keyCode.ENTER) {
this.doSearch();
}
});
this.$doSearchButton.on('click', () => this.doSearch()); // keep long form because of argument
this.$resetSearchButton.on('click', () => this.resetSearchListener());
this.$saveSearchButton.on('click', () => this.saveSearch());
this.$closeSearchButton.on('click', () => this.trigger('hideSearch'));
return this.$widget;
}
doSearch(searchText) {
if (searchText) {
this.$searchInput.val(searchText);
}
else {
searchText = this.$searchInput.val();
}
if (searchText.trim().length === 0) {
toastService.showMessage("Please enter search criteria first.");
this.$searchInput.trigger('focus');
return;
}
this.trigger('searchForResults', {
searchText: this.$searchInput.val()
});
this.$searchBox.tooltip("hide");
}
async saveSearch() {
const searchString = this.$searchInput.val().trim();
if (searchString.length === 0) {
alert("Write some search criteria first so there is something to save.");
return;
}
let activeNode = appContext.getMainNoteTree().getActiveNode();
const parentNote = await treeCache.getNote(activeNode.data.noteId);
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 })
});
this.resetSearchListener();
}
showSearchListener() {
this.$searchBox.slideDown();
this.$searchBox.tooltip({
trigger: 'focus',
html: true,
title: helpText,
placement: 'right',
delay: {
show: 500, // necessary because sliding out may cause wrong position
hide: 200
}
});
this.$searchInput.trigger('focus');
}
hideSearchListener() {
this.resetSearchListener();
this.$searchBox.slideUp();
this.trigger('hideSearchResults');
}
toggleSearchListener() {
if (this.$searchBox.is(":hidden")) {
this.showSearchListener();
}
else {
this.hideSearchListener();
}
}
searchNotesListener() {
this.toggleSearchListener();
}
resetSearchListener() {
this.$searchInput.val("");
}
searchInSubtreeListener({noteId}) {
noteId = noteId || appContext.getActiveTabNoteId();
this.toggle(true);
this.$searchInput.val(`@in=${noteId} @text*=*`);
}
}