panel-container.ts 8.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
import { PanelDetailModel } from './panel-detail';

/**
 * 面板容器模型
 *
 * @export
 * @class PanelContainerModel
 * @extends {PanelDetailModel}
 */
export class PanelContainerModel extends PanelDetailModel {

    /**
     * 子项
     *
     * @type {string[]}
     * @memberof PanelContainerModel
     */
    public details: string[] = [];

    /**
     * 标题关闭模式
     *
     * @type {number}
     * @memberof PanelContainerModel
     */
    public titleBarCloseMode: number = 0;

    /**
     * 背景图片
     *
     * @type {*}
     * @memberof PanelContainerModel
     */
    public sysImage: any = {};

    /**
     * 数据区域类型
     * @description 值模式 [数据面板模式] {NONE:无、 LOGINFORM:登录表单、 SINGLEDATA:单项数据、 MULTIDATA:多项数据、 INHERIT:继承、 USER:用户自定义 }
     * @type {( string | 'NONE' | 'LOGINFORM' | 'SINGLEDATA' | 'MULTIDATA' | 'INHERIT' | 'USER')} 
     * @memberof PanelContainerModel
     */
    public dataRegionType: string | 'NONE' | 'LOGINFORM' | 'SINGLEDATA' | 'MULTIDATA' | 'INHERIT' | 'USER' | '' = '';

    /**
     * 数据源类型
     * @description 值模式 [数据面板源(全部)] {DEACTION:实体行为、 DEDATASET:实体集合、 DELOGIC:实体逻辑、 APPGLOBALPARAM:绑定应用全局变量、 TOPVIEWSESSIONPARAM:绑定顶级视图会话共享变量、 VIEWSESSIONPARAM:绑定当前视图会话共享变量 }
     * @type {( string | 'DEACTION' | 'DEDATASET' | 'DELOGIC' | 'APPGLOBALPARAM' | 'TOPVIEWSESSIONPARAM' | 'VIEWSESSIONPARAM')} 
     * @memberof PanelContainerModel
     */
    public dataSourceType: string | 'DEACTION' | 'DEDATASET' | 'DELOGIC' | 'APPGLOBALPARAM' | 'TOPVIEWSESSIONPARAM' | 'VIEWSESSIONPARAM' | '' = '';

    /**
     * 应用实体codeName
     *
     * @type {(string | undefined)}
     * @memberof PanelContainerModel
     */
    public appDataEntityCodeName: string | undefined = undefined;

    /**
     * 应用实体方法CodeName
     *
     * @type {(string | undefined)}
     * @memberof PanelContainerModel
     */
    public appDEMethodCodeName: string | undefined = undefined;

    /**
     * Creates an instance of PanelContainerModel.
     * @param {*} [opts={}]
     * @memberof PanelContainerModel
     */
    public constructor(opts: any = {}) {
        super(opts);
        this.details = opts.details;
        this.titleBarCloseMode = opts.titleBarCloseMode;
        this.sysImage = opts.sysImage;
        this.appDataEntityCodeName = opts.appDataEntityCodeName;
        this.appDEMethodCodeName = opts.appDEMethodCodeName;
        this.dataRegionType = opts.dataRegionType;
        this.dataSourceType = opts.dataSourceType;
    }

    /**
     * 获取容器元素样式(仅限容器元素)
     *
     * @return {*} 
     * @memberof PanelContainerModel
     */
    public getElementStyle() {
        const containerStyle = {};
        Object.assign(containerStyle, this.getFlexStyle());
        //边缘布局容器盒子大小由布局组件内部计算
        if (this.layout !== "BORDER") {
            Object.assign(containerStyle, this.getBoxSizeStyle());
        }
        Object.assign(containerStyle, this.getBoxSpacingStyle());
        Object.assign(containerStyle, this.getSpecificStyle());
        Object.assign(containerStyle, this.getBoxSelfAlignStyle());
        return containerStyle;
    }

    /**
     * 获取容器特有样式(主要包含背景图片)
     *
     * @protected
     * @memberof PanelContainerModel
     */
    protected getSpecificStyle() {
        const boxStyle = {};
        if (this.sysImage && (this.sysImage.imagePath || this.sysImage.rawContent)) {
            Object.assign(boxStyle, { background: `url('${this.sysImage.imagePath || this.sysImage.rawContent}') no-repeat 0px 0px`, 'background-size': '100% 100%' });
            if (!this.layoutWidth) {
                Object.assign(boxStyle, { width: '100%' });
            }
            if (!this.layoutHeight) {
                Object.assign(boxStyle, { height: '100%' });
            }
        }
        return boxStyle;
    }

    /**
     * 加载
     *
     * @param {*} context
     * @param {*} viewParams
     * @memberof PanelContainerModel
     */
    public async load(context: any, viewParams: any) {
        const nullData = {};
        switch (this.dataRegionType) {
            case 'LOGINFORM':
                this.data = nullData;
                break;
            case 'INHERIT':
                if (!this.parentName) {
                    if (this.type == 'VIEWLAYOUT') {
                        this.data = nullData;
                    } else {
                        this.data = this.panel.data;
                    }
                } else {
                    let parentItem = this.panel.layoutModelDetails[`${this.parentName}_${this.index}`];
                    if (!parentItem) {
                        parentItem = this.panel.layoutModelDetails[this.parentName];
                    }
                    if (parentItem) {
                        if (parentItem.dataRegionType === 'MULTIDATA' && parentItem['data'] && parentItem['data'].length > 0) {
                            this.data = parentItem.getData()[this.getIndex()];    
                        } else {
                            this.data = parentItem.getData();
                        }
                    }
                }
                break;
            case 'SINGLEDATA':
            case 'MULTIDATA':
                this.data = await this.loadData(context, viewParams);
                break;
            default:
                console.warn(`${this.dataRegionType}数据域未实现`);
                break;
        }
    }

    /**
     * 加载数据
     *
     * @param {*} context
     * @param {*} viewParams
     * @return {*} 
     * @memberof PanelContainerModel
     */
    public async loadData(context: any, viewParams: any) {
        let data = {};
        if (this.dataSourceType === 'DEACTION' || this.dataSourceType === 'DEDATASET') {
            try {
                if (this.appDataEntityCodeName && this.appDEMethodCodeName) {
                    const service = await window.entityServiceRegister.getService(this.appDataEntityCodeName);
                    if (service) {
                        if (service[this.appDEMethodCodeName] && service[this.appDEMethodCodeName] instanceof Function) {
                            const response = await service[this.appDEMethodCodeName](context, viewParams);
                            if (response && response.status === 200) {
                                data = response.data;
                            }
                        }
                    }
                }
            } catch (error) {
                console.error(`${this.dataSourceType}数据源请求数据异常`);
            }
        } else {
            console.warn(`${this.dataSourceType}数据源类型未实现`);
        }
        return data;
    }

    /**
     * 设置数据
     *
     * @param {*} args
     * @memberof PanelContainerModel
     */
    public setData(args: any) {
        const { name, value, index } = args;
        switch (this.dataRegionType) {
            case 'LOGINFORM':
                this.data[name] = value;
                break;
            case 'INHERIT':
                this.data[name] = value;
                if (this.parentName) {
                    const parentItem = this.panel.layoutModelDetails[this.parentName];
                    this.data[name] = value;
                    if (parentItem) {
                        if (parentItem.dataRegionType && parentItem.dataRegionType === "MULTIDATA") {
                            // 多数据域第一层子项传整条数据值
                            parentItem.setData({ name, value: this.data, index });
                        } else {
                            parentItem.setData({ name, value, index });
                        }
                    }
                }
                break;
            case 'SINGLEDATA':
                this.data[name] = value;
                break;
            case 'MULTIDATA':
                this.data[index] = value;
                break;
            default:
                this.data[name] = value;
                break;
        }
    }

    /**
     * 重置当前项数据
     * 
     * @memberof PanelContainerModel
     */
    public reSetData() {
        switch (this.dataRegionType) {
            case 'LOGINFORM':
            case 'SINGLEDATA':
                this.data = {};
                break;
            case 'INHERIT':
            case 'USER':
                this.data = {};
                break;
            case 'MULTIDATA':
                this.data = [];
                break;
            default:
                this.data = {};
                break;
        }
    }
}