Files
Trilium/apps/client/src/widgets/bulk_actions/abstract_bulk_action.ts

79 lines
2.3 KiB
TypeScript
Raw Normal View History

import { t } from "../../services/i18n.js";
2021-01-19 22:10:24 +01:00
import server from "../../services/server.js";
import ws from "../../services/ws.js";
2021-01-26 22:22:17 +01:00
import utils from "../../services/utils.js";
import type FAttribute from "../../entities/fattribute.js";
import { VNode } from "preact";
2021-01-19 22:10:24 +01:00
export interface ActionDefinition {
script: string;
relationName: string;
targetNoteId: string;
targetParentNoteId: string;
oldRelationName?: string;
newRelationName?: string;
newTitle?: string;
labelName?: string;
labelValue?: string;
oldLabelName?: string;
newLabelName?: string;
}
export default abstract class AbstractBulkAction {
attribute: FAttribute;
actionDef: ActionDefinition;
constructor(attribute: FAttribute, actionDef: ActionDefinition) {
2021-01-19 22:10:24 +01:00
this.attribute = attribute;
this.actionDef = actionDef;
}
render() {
try {
const $rendered = this.doRender();
if (Array.isArray($rendered)) {
$rendered
.find(".action-conf-del")
.on("click", () => this.deleteAction())
.attr("title", t("abstract_bulk_action.remove_this_search_action"));
2021-01-19 22:10:24 +01:00
utils.initHelpDropdown($rendered);
2021-01-19 22:10:24 +01:00
return $rendered;
} else {
return $rendered;
}
} catch (e: any) {
2021-01-19 22:10:24 +01:00
logError(`Failed rendering search action: ${JSON.stringify(this.attribute.dto)} with error: ${e.message} ${e.stack}`);
return null;
}
}
// to be overridden
abstract doRender(): JQuery<HTMLElement> | VNode;
2025-01-09 18:07:02 +02:00
static get actionName() {
return "";
}
2021-01-19 22:10:24 +01:00
async saveAction(data: {}) {
const actionObject = Object.assign({ name: (this.constructor as typeof AbstractBulkAction).actionName }, data);
2021-01-19 22:10:24 +01:00
await server.put(`notes/${this.attribute.noteId}/attribute`, {
attributeId: this.attribute.attributeId,
2025-01-09 18:07:02 +02:00
type: "label",
name: "action",
2021-01-19 22:10:24 +01:00
value: JSON.stringify(actionObject)
});
await ws.waitForMaxKnownEntityChangeId();
}
2021-01-26 10:48:28 +01:00
async deleteAction() {
await server.remove(`notes/${this.attribute.noteId}/attributes/${this.attribute.attributeId}`);
await ws.waitForMaxKnownEntityChangeId();
2022-06-11 23:29:52 +02:00
//await this.triggerCommand('refreshSearchDefinition');
2021-01-26 10:48:28 +01:00
}
2021-01-19 22:10:24 +01:00
}