101 lines
4.0 KiB
JavaScript
Raw Normal View History

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";
import { Tooltip } from "bootstrap";
const TPL = `
<tr>
2025-01-09 18:07:02 +02:00
<td class="title-column">${t("search_string.title_column")}</td>
<td>
2025-01-09 18:07:02 +02:00
<textarea class="form-control search-string" placeholder="${t("search_string.placeholder")}"></textarea>
</td>
2021-01-26 14:10:34 +01:00
<td class="button-column">
<div class="dropdown help-dropdown">
<span class="bx bx-help-circle icon-action" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false"></span>
2021-01-26 15:54:41 +01:00
<div class="dropdown-menu dropdown-menu-right p-4">
2025-01-09 18:07:02 +02:00
<strong>${t("search_string.search_syntax")}</strong> - ${t("search_string.also_see")} <a href="#" data-help-page="search.html">${t("search_string.complete_help")}</a>
<ul style="marigin-bottom: 0;">
2025-01-09 18:07:02 +02:00
<li>${t("search_string.full_text_search")}</li>
<li><code>#abc</code> - ${t("search_string.label_abc")}</li>
<li><code>#year = 2019</code> - ${t("search_string.label_year")}</li>
<li><code>#rock #pop</code> - ${t("search_string.label_rock_pop")}</li>
<li><code>#rock or #pop</code> - ${t("search_string.label_rock_or_pop")}</li>
<li><code>#year &lt;= 2000</code> - ${t("search_string.label_year_comparison")}</li>
<li><code>note.dateCreated >= MONTH-1</code> - ${t("search_string.label_date_created")}</li>
</ul>
2021-02-13 23:38:31 +01:00
</div>
</div>
2021-01-26 10:42:55 +01:00
<span class="bx bx-x icon-action search-option-del"></span>
</td>
</tr>`;
export default class SearchString extends AbstractSearchOption {
2025-01-09 18:07:02 +02:00
static get optionName() {
return "searchString";
}
static get attributeType() {
return "label";
}
static async create(noteId) {
2025-01-09 18:07:02 +02:00
await AbstractSearchOption.setAttribute(noteId, "label", "searchString");
}
doRender() {
const $option = $(TPL);
2025-01-09 18:07:02 +02:00
this.$searchString = $option.find(".search-string");
this.$searchString.on("input", () => this.spacedUpdate.scheduleUpdate());
2025-01-09 18:07:02 +02:00
shortcutService.bindElShortcut(this.$searchString, "return", async () => {
// this also in effect disallows new lines in query string.
2023-06-30 11:18:34 +02:00
// 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();
2025-01-09 18:07:02 +02:00
this.triggerCommand("refreshResults");
});
this.spacedUpdate = new SpacedUpdate(async () => {
2021-01-25 21:24:02 +01:00
const searchString = this.$searchString.val();
appContext.lastSearchString = searchString;
2025-01-09 18:07:02 +02:00
await this.setAttribute("label", "searchString", searchString);
2024-10-25 21:04:13 +03:00
if (this.note.title.startsWith(t("search_string.search_prefix"))) {
2022-06-13 22:38:59 +02:00
await server.put(`notes/${this.note.noteId}/title`, {
2024-10-25 21:04:13 +03:00
title: `${t("search_string.search_prefix")} ${searchString.length < 30 ? searchString : `${searchString.substr(0, 30)}`}`
});
}
}, 1000);
2025-01-09 18:07:02 +02:00
this.$searchString.val(this.note.getLabelValue("searchString"));
return $option;
}
2021-01-25 21:24:02 +01:00
showSearchErrorEvent({ error }) {
let tooltip = new Tooltip(this.$searchString, {
2025-01-09 18:07:02 +02:00
trigger: "manual",
title: `${t("search_string.error", { error })}`,
placement: "bottom"
});
tooltip.show();
setTimeout(() => tooltip.dispose(), 4000);
}
2021-01-25 21:24:02 +01:00
focusOnSearchDefinitionEvent() {
2025-01-09 18:07:02 +02:00
this.$searchString
.val(this.$searchString.val().trim() || appContext.lastSearchString)
.focus()
.select();
this.spacedUpdate.scheduleUpdate();
2021-01-25 21:24:02 +01:00
}
}