uiaction-tool.ts 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/**
 * 界面行为工具类
 */
export class UIActionTool {

    /**
     * 处理应用上下文参数
     * 
     * @param actionTarget 数据目标
     * @param args  传入数据对象
11 12
     * @param parentContext 父上下文
     * @param parentParams  父参数
13 14
     * @param param 传入应用上下数据参数
     */
15 16
    public static handleContextParam(actionTarget: any, args: any,parentContext:any,parentParams:any, context: any) {
        return this.formatData(actionTarget, args,parentContext,parentParams,context);
17 18 19 20 21 22 23
    }

    /**
     * 处理界面行为参数
     * 
     * @param actionTarget 数据目标
     * @param args  传入数据对象
24 25
     * @param parentContext 父上下文
     * @param parentParams  父参数
26 27
     * @param param 传入界面行为附加参数
     */
28 29
    public static handleActionParam(actionTarget: any, args: any,parentContext:any,parentParams:any, params: any) {
        return this.formatData(actionTarget, args,parentContext,parentParams,params);
30 31 32 33 34 35 36 37 38
    }

    /**
     * 格式化数据
     *
     * @private
     * @static
     * @param {*} actionTarget
     * @param {*} args
39 40
     * @param parentContext
     * @param parentParams
41 42 43 44
     * @param {*} _params
     * @returns {*}
     * @memberof UIActionTool
     */
45
    private static formatData(actionTarget: any, args: any,parentContext:any,parentParams:any, _params: any): any {
46
        let _data: any = {};
47
        if (Object.is(actionTarget, 'SINGLEKEY') || Object.is(actionTarget, 'NONE')) {
48 49
            let [arg] = args;
            Object.keys(_params).forEach((name: string) => {
50
                let hasProperty = true;
51 52 53 54
                if (!name) {
                    return;
                }
                let value: string | null = _params[name];
55
                if (value && typeof(value) === 'string' && value.startsWith('%') && value.endsWith('%')) {
56
                    const key = value.substring(1, value.length - 1);
57
                    if (arg && arg.hasOwnProperty(key) && Object.is(actionTarget, 'SINGLEKEY')) {
58
                        value = (arg[key] !== null && arg[key] !== undefined) ? arg[key] : null;
59 60 61 62 63
                    } else if(parentContext && parentContext.hasOwnProperty(key)){
                        value = (parentContext[key] !== null && parentContext[key] !== undefined) ? parentContext[key] : null;
                    }else if(parentParams && parentParams.hasOwnProperty(key)){
                        value = (parentParams[key] !== null && parentParams[key] !== undefined) ? parentParams[key] : null;
                    }else {
64
                        hasProperty = false;
65 66
                    }
                }
67
                if(hasProperty){
68
                    Object.assign(_data, { [name.toLowerCase()]: value });
69
                }
70 71 72
            });
        } else if (Object.is(actionTarget, 'MULTIKEY')) {
            Object.keys(_params).forEach((name: string) => {
73
                let noPropertyNum = 0;
74 75 76 77 78
                if (!name) {
                    return;
                }
                let value: string | null = _params[name];
                let values: any[] = [];
79
                if (value && typeof(value) === 'string'  && value.startsWith('%') && value.endsWith('%')) {
80 81 82 83
                    const key = value.substring(1, value.length - 1);
                    args.forEach((arg: any) => {
                        if (arg && arg.hasOwnProperty(key)) {
                            value = (arg[key] !== null && arg[key] !== undefined) ? arg[key] : null;
84 85 86 87 88
                        }else if(parentContext && parentContext.hasOwnProperty(key)){
                            value = (parentContext[key] !== null && parentContext[key] !== undefined) ? parentContext[key] : null;
                        }else if(parentParams && parentParams.hasOwnProperty(key)){
                            value = (parentParams[key] !== null && parentParams[key] !== undefined) ? parentParams[key] : null;
                        }else {
89
                            value = null;
90
                            noPropertyNum++;
91 92 93 94
                        }
                        values.push(value);
                    });
                }
95
                if(values.length !== noPropertyNum){
96
                    Object.assign(_data, { [name.toLowerCase()]: values.length > 0 ? values.join(',') : value });
97
                }
98 99 100 101 102 103
            });
        }
        return _data;
    }

}