Files
KintoneAppBuilder/plugin/kintone-addins/src/actions/error-show.ts
2024-02-02 12:52:00 +09:00

97 lines
2.6 KiB
TypeScript

import { actionAddins } from ".";
import { IAction, IActionProperty, IActionNode, IContext, IActionResult } from "../types/ActionTypes";
import { ConditionTree } from '../types/Conditions';
interface IErrorShowProps {
message: string;
condition: string;
}
export class ErrorShowAction implements IAction {
name: string;
actionProps: IActionProperty[];
props: IErrorShowProps;
constructor() {
this.name = "エラー表示";
this.actionProps = [];
this.props = {
message: '',
condition: ''
}
this.register();
}
/**
* アクションの実行を呼び出す
* @param actionNode
* @param event
* @returns
*/
async process(actionNode: IActionNode, event: any, context: IContext): Promise<IActionResult> {
let result = {
canNext: true,
result: false
};
try {
this.actionProps = actionNode.actionProps;
if (!('message' in actionNode.ActionValue) && !('condition' in actionNode.ActionValue)) {
return result
}
this.props = actionNode.ActionValue as IErrorShowProps;
const conditionResult = this.getConditionResult(context);
if (conditionResult) {
event.error = this.props.message
} else {
result = {
canNext: false,
result: true
}
}
return result;
} catch (error) {
event.error = error;
console.error(error);
result.canNext = false;
return result;
}
}
/**
*
* @param context 条件式を実行する
* @returns
*/
getConditionResult(context: any): boolean {
const tree = this.getCondition(this.props.condition);
if (!tree) {
//条件を設定されていません
return true;
}
return tree.evaluate(tree.root, context);
}
/**
* @param condition 条件式ツリーを取得する
* @returns
*/
getCondition(condition: string): ConditionTree | null {
try {
const tree = new ConditionTree();
tree.fromJson(condition);
if (tree.getConditions(tree.root).length > 0) {
return tree;
} else {
return null;
}
} catch (error) {
return null;
}
}
register(): void {
actionAddins[this.name] = this;
}
}
new ErrorShowAction();