2024-07-29 10:23:32 +08:00
|
|
|
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";
|
2025-01-13 23:18:10 +02:00
|
|
|
import type FAttribute from "../../entities/fattribute.js";
|
2025-08-08 23:23:07 +03:00
|
|
|
import { VNode } from "preact";
|
2021-01-19 22:10:24 +01:00
|
|
|
|
2025-08-08 23:23:07 +03:00
|
|
|
export interface ActionDefinition {
|
2024-12-21 09:29:50 +02:00
|
|
|
script: string;
|
2024-12-21 14:39:36 +02:00
|
|
|
relationName: string;
|
|
|
|
|
targetNoteId: string;
|
2024-12-21 15:04:33 +02:00
|
|
|
targetParentNoteId: string;
|
2024-12-21 15:03:45 +02:00
|
|
|
oldRelationName?: string;
|
|
|
|
|
newRelationName?: string;
|
2024-12-21 15:04:33 +02:00
|
|
|
newTitle?: string;
|
2024-12-21 15:05:41 +02:00
|
|
|
labelName?: string;
|
|
|
|
|
labelValue?: string;
|
|
|
|
|
oldLabelName?: string;
|
|
|
|
|
newLabelName?: string;
|
2024-12-21 09:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
2025-08-08 23:23:07 +03:00
|
|
|
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
|
|
|
|
2025-08-08 23:23:07 +03:00
|
|
|
utils.initHelpDropdown($rendered);
|
2021-01-19 22:10:24 +01:00
|
|
|
|
2025-08-08 23:23:07 +03:00
|
|
|
return $rendered;
|
|
|
|
|
} else {
|
|
|
|
|
return $rendered;
|
|
|
|
|
}
|
2024-12-21 09:26:37 +02:00
|
|
|
} 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 00:26:47 +08:00
|
|
|
// to be overridden
|
2025-08-08 23:23:07 +03:00
|
|
|
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
|
|
|
|
2024-12-21 09:26:37 +02:00
|
|
|
async saveAction(data: {}) {
|
2024-12-21 09:29:50 +02:00
|
|
|
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
|
|
|
}
|