Files
Trilium/apps/client/src/widgets/ribbon_widgets/search_definition.ts

105 lines
4.1 KiB
TypeScript
Raw Normal View History

import { t } from "../../services/i18n.js";
2021-01-30 15:50:46 +01:00
import server from "../../services/server.js";
2021-05-22 12:35:41 +02:00
import NoteContextAwareWidget from "../note_context_aware_widget.js";
2021-04-16 23:01:56 +02:00
import froca from "../../services/froca.js";
2021-01-30 15:50:46 +01:00
import ws from "../../services/ws.js";
import toastService from "../../services/toast.js";
2021-06-06 11:01:10 +02:00
import treeService from "../../services/tree.js";
2021-01-30 15:50:46 +01:00
import SearchString from "../search_options/search_string.js";
import FastSearch from "../search_options/fast_search.js";
import Ancestor from "../search_options/ancestor.js";
import IncludeArchivedNotes from "../search_options/include_archived_notes.js";
import OrderBy from "../search_options/order_by.js";
import SearchScript from "../search_options/search_script.js";
import Limit from "../search_options/limit.js";
import Debug from "../search_options/debug.js";
import appContext, { type EventData } from "../../components/app_context.js";
2022-06-03 17:29:08 +02:00
import bulkActionService from "../../services/bulk_action.js";
import { Dropdown } from "bootstrap";
import type FNote from "../../entities/fnote.js";
import type { AttributeType } from "../../entities/fattribute.js";
import { renderReactWidget } from "../react/react_utils.jsx";
2020-11-26 23:00:27 +01:00
const TPL = /*html*/`
<div class="">
<div class="">
2020-11-26 23:00:27 +01:00
<tr>
<td>
2021-01-08 19:57:49 +01:00
<div class="dropdown" style="display: inline-block;">
<button class="btn btn-sm dropdown-toggle action-add-toggle" type="button" id="dropdownMenuButton" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
2021-01-08 19:57:49 +01:00
<span class="bx bxs-zap"></span>
2025-01-09 18:07:02 +02:00
${t("search_definition.action")}
2021-01-08 19:57:49 +01:00
</button>
2022-06-03 17:29:08 +02:00
<div class="dropdown-menu action-list"></div>
2021-01-08 19:57:49 +01:00
</div>
</td>
</tr>
<tbody class="search-options"></tbody>
2021-01-19 22:10:24 +01:00
<tbody class="action-options"></tbody>
2020-11-26 23:00:27 +01:00
</table>
</div>
</div>`;
2025-01-09 18:07:02 +02:00
const OPTION_CLASSES = [SearchString, SearchScript, Ancestor, FastSearch, IncludeArchivedNotes, OrderBy, Limit, Debug];
2021-05-22 12:35:41 +02:00
export default class SearchDefinitionWidget extends NoteContextAwareWidget {
private $component!: JQuery<HTMLElement>;
private $actionList!: JQuery<HTMLElement>;
private $searchOptions!: JQuery<HTMLElement>;
private $searchButton!: JQuery<HTMLElement>;
private $searchAndExecuteButton!: JQuery<HTMLElement>;
private $saveToNoteButton!: JQuery<HTMLElement>;
private $actionOptions!: JQuery<HTMLElement>;
get name() {
return "searchDefinition";
}
2020-11-26 23:00:27 +01:00
doRender() {
this.$widget = $(TPL);
2021-06-13 22:55:31 +02:00
this.contentSized();
2025-01-09 18:07:02 +02:00
this.$component = this.$widget.find(".search-definition-widget");
this.$actionList = this.$widget.find(".action-list");
2022-06-03 17:29:08 +02:00
2022-06-05 23:36:46 +02:00
for (const actionGroup of bulkActionService.ACTION_GROUPS) {
this.$actionList.append($('<h6 class="dropdown-header">').append(actionGroup.title));
for (const action of actionGroup.actions) {
2025-01-09 18:07:02 +02:00
this.$actionList.append($('<a class="dropdown-item" href="#">').attr("data-action-add", action.actionName).text(action.actionTitle));
2022-06-05 23:36:46 +02:00
}
2022-06-03 17:29:08 +02:00
}
2025-01-09 18:07:02 +02:00
this.$widget.on("click", "[data-action-add]", async (event) => {
Dropdown.getOrCreateInstance(this.$widget.find(".action-add-toggle")[0]);
2021-01-25 21:24:02 +01:00
2025-01-09 18:07:02 +02:00
const actionName = $(event.target).attr("data-action-add");
2022-06-03 17:29:08 +02:00
if (this.noteId && actionName) {
await bulkActionService.addAction(this.noteId, actionName);
}
2021-01-25 21:24:02 +01:00
this.refresh();
});
2025-01-09 18:07:02 +02:00
this.$searchOptions = this.$widget.find(".search-options");
this.$actionOptions = this.$widget.find(".action-options");
2020-11-26 23:00:27 +01:00
}
async refreshWithNote(note: FNote) {
if (!this.note) {
return;
}
2022-06-03 17:29:08 +02:00
const actions = bulkActionService.parseActions(this.note);
const renderedEls = actions
.map((action) => renderReactWidget(this, action.doRender()))
.filter((e) => e) as JQuery<HTMLElement>[];
this.$actionOptions.empty().append(...renderedEls);
2025-01-09 18:07:02 +02:00
this.$searchAndExecuteButton.css("visibility", actions.length > 0 ? "visible" : "_hidden");
2020-12-04 22:57:54 +01:00
}
2020-11-26 23:00:27 +01:00
}