90 lines
2.8 KiB
TypeScript
90 lines
2.8 KiB
TypeScript
import { actionAddins } from ".";
|
|
import { IAction,IActionResult, IActionNode, IActionProperty, IField, IContext } from "../types/ActionTypes";
|
|
|
|
/**
|
|
* アクションの属性定義
|
|
*/
|
|
interface HalfWidthProps{
|
|
field:IField
|
|
}
|
|
/**
|
|
* 半角チェックアクション
|
|
*/
|
|
export class HalfWidthAction implements IAction{
|
|
name: string;
|
|
actionProps: IActionProperty[];
|
|
props:HalfWidthProps;
|
|
constructor(){
|
|
this.name="半角チェック"; /* pgadminのnameと同様 */
|
|
this.actionProps=[];
|
|
this.props={
|
|
field:{code:''}
|
|
}
|
|
//アクションを登録する
|
|
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 (!('field' in actionNode.ActionValue) ) {
|
|
return result
|
|
}
|
|
this.props = actionNode.ActionValue as HalfWidthProps;
|
|
//条件式の計算結果を取得
|
|
const record = event.record;
|
|
const value = record[this.props.field.code]?.value;
|
|
//条件分岐
|
|
//未入力時は何も処理をせず終了
|
|
if(value===undefined || value===''){
|
|
return result;
|
|
}
|
|
//全角が含まれていた場合保存処理中止(エラー処理)
|
|
if(!this.containsHalfWidthChars(value)){
|
|
//エラー時に出力される文字設定
|
|
record[this.props.field.code].error="全角が含まれています";
|
|
//次の処理を中止する値設定
|
|
result.canNext=false;
|
|
return result;
|
|
}
|
|
//半角の場合問題なく実行
|
|
//resultプロパティ指定
|
|
result= {
|
|
canNext:true,
|
|
result:true
|
|
}
|
|
return result;
|
|
//例外処理
|
|
}catch(error){
|
|
event.error=error;
|
|
console.error(error);
|
|
result.canNext=false;
|
|
return result;
|
|
}
|
|
}
|
|
// 全て全角の文字列の場合はtrue、そうでない場合はfalse
|
|
containsHalfWidthChars(text: string): boolean {
|
|
|
|
const checkRegex = "^[\x01-\x7E\uFF61-\uFF9F]+$";
|
|
//正規表現オブジェクト生成
|
|
const halfWidthRegex = new RegExp(checkRegex);
|
|
|
|
//正規表現チェック
|
|
return halfWidthRegex.test(text);
|
|
}
|
|
//戻り値を持たないためvoid型
|
|
register(): void {
|
|
actionAddins[this.name]=this;
|
|
}
|
|
}
|
|
new HalfWidthAction(); |