import AbstractSearchOption from "./abstract_search_option.js";
import SpacedUpdate from "../../services/spaced_update.js";
import server from "../../services/server.js";
import shortcutService from "../../services/shortcuts.js";
import appContext from "../../components/app_context.js";
import { t } from "../../services/i18n.js";
const TPL = `
${t('search_string.title_column')} |
|
|
`;
export default class SearchString extends AbstractSearchOption {
static get optionName() { return "searchString" };
static get attributeType() { return "label" };
static async create(noteId) {
await AbstractSearchOption.setAttribute(noteId, 'label', 'searchString');
}
doRender() {
const $option = $(TPL);
this.$searchString = $option.find('.search-string');
this.$searchString.on('input', () => this.spacedUpdate.scheduleUpdate());
shortcutService.bindElShortcut(this.$searchString, 'return', async () => {
// this also in effect disallows new lines in query string.
// on one hand, this makes sense since search string is a label
// on the other hand, it could be nice for structuring long search string. It's probably a niche case though.
await this.spacedUpdate.updateNowIfNecessary();
this.triggerCommand('refreshResults');
});
this.spacedUpdate = new SpacedUpdate(async () => {
const searchString = this.$searchString.val();
appContext.lastSearchString = searchString;
await this.setAttribute('label', 'searchString', searchString);
if (this.note.title.startsWith('Search: ')) {
await server.put(`notes/${this.note.noteId}/title`, {
title: `Search: ${searchString.length < 30 ? searchString : `${searchString.substr(0, 30)}…`}`
});
}
}, 1000);
this.$searchString.val(this.note.getLabelValue('searchString'));
return $option;
}
showSearchErrorEvent({ error }) {
let tooltip = new bootstrap.Tooltip(this.$searchString, {
trigger: 'manual',
title: `${t('search_string.error', { error })}`,
placement: 'bottom'
});
tooltip.show();
setTimeout(() => tooltip.dispose(), 4000);
}
focusOnSearchDefinitionEvent() {
this.$searchString.val(this.$searchString.val().trim() || appContext.lastSearchString).focus().select();
this.spacedUpdate.scheduleUpdate();
}
}