import { actionAddins } from "."; import { IAction, IActionResult, IActionNode, IActionProperty, IField ,IContext, IVarName} from "../types/ActionTypes"; /** * アクションの属性定義 */ interface IDateSpecifiedProps { verNameGet:string; newYear:number; newMonth:number; newDay:number; verName:IVarName; } /** * 日付指定アクション */ export class DateSpecifiedAction implements IAction { name: string; actionProps: IActionProperty[]; props: IDateSpecifiedProps; constructor() { this.name = "日付指定"; this.actionProps = []; this.props = { verNameGet:'', newYear:0, newMonth:0, newDay:0, verName:{name:''} } this.register(); } /** * アクションの実行を呼び出す * @param actionNode * @param event * @returns */ async process(actionNode: IActionNode, event: any,context:IContext): Promise { let result = { canNext: true, result: false }; try { //属性設定を取得する this.actionProps = actionNode.actionProps; if (!('verName' in actionNode.ActionValue) && !('verNameGet' in actionNode.ActionValue) ) { return result } this.props = actionNode.ActionValue as IDateSpecifiedProps; //////////////////////////////////////////////////////////////////////////////////////////////// //本番コード開始: //取得変数の値を呼び出して代入する: const getContextVarByPath = (obj: any, path: string) => { return path.split(".").reduce((o, k) => (o || {})[k], obj); }; let verNameGetValue = getContextVarByPath(context.variables,this.props.verNameGet); //////////////////////////////////////////////////////////////////////////////////////////////// //取得変数の値Dateオブジェクトに変換: let dateObj = new Date(verNameGetValue); if(verNameGetValue === undefined || verNameGetValue === null || verNameGetValue === '' || isNaN(dateObj.getDate())){ throw new Error("Invalid time value"); } // 年の設定(newYearが設定されていない場合は、元の値を使用) dateObj.setFullYear(this.props.newYear >=1900 && this.props.newYear <=9999 ? this.props.newYear : dateObj.getFullYear()); // 月の設定(newMonthが設定されていない場合は、元の値を使用)// 月は0始まりなので、12月は11。 dateObj.setMonth(this.props.newMonth >=1 && this.props.newMonth <=12 ? this.props.newMonth-1 : dateObj.getMonth()); // 日の設定(newDayが設定されていない場合は、元の値を使用) dateObj.setDate(this.props.newDay >=1 && this.props.newDay <=31 ?this.props.newDay : dateObj.getDate()); // 変数に新しい値を設定 if(this.props.verName && this.props.verName.name!==''){ context.variables[this.props.verName.name]=dateObj.toISOString(); } //////////////////////////////////////////////////////////////////////////////////////////////// result = { canNext:true, result:true } return result; }catch(error){ context.errors.handleError(error,actionNode); result.canNext=false; return result; } }; register(): void { actionAddins[this.name] = this; } } new DateSpecifiedAction();