drbar-control-base.tsx 10.3 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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
import { IPSDEDRBar, IPSDEDRBarGroup, IPSDEDRBarItem, IPSDEDRCtrlItem, IPSDEEditForm, IPSNavigateContext, IPSNavigateParam } from '@ibiz/dynamic-model-api';
import { DrbarControlInterface, Util } from "ibiz-core";
import { Subscription } from 'rxjs';
import { MainControlBase } from "./main-control-base";

/**
 * 数据关系栏部件基类
 *
 * @export
 * @class DrbarControlBase
 * @extends {MainControlBase}
 */
export class DrbarControlBase extends MainControlBase implements DrbarControlInterface{

    /**
     * 数据关系栏部件实例对象
     * 
     * @type {IPSDEDRBar}
     * @memberof DrbarControlBase
     */
    public declare controlInstance: IPSDEDRBar;

    /**
     * 表单实例事项
     * 
     * @type {IPSDEEditForm}
     * @memberof DrbarControlBase
     */
    public formInstance?: IPSDEEditForm;

    /**
     * 表单名称
     * 
     * @type {string}
     * @memberof DrbarControlBase
     */
    public formName: string = '';

    /**
     * 数据选中项
     *
     * @type {*}
     * @memberof DrbarControlBase
     */
    public selection: any = {};

    /**
     * 关系栏数据项
     * 
     * @type {any[]}
     * @memberof DrbarControlBase
     */
    public items: any[] = [];

    /**
     * 默认打开项
     * 
     * @type {string[]}
     * @memberof DrbarControlBase
     */
    public defaultOpeneds: string[] = [];

    /**
     * 父数据
     * 
     * @type {*}
     * @memberof DrbarControlBase
     */
    public parentData: any = {};

    /**
     * 宽度
     * 
     * @type {number}
     * @memberof DrbarControlBase
     */
    public width: number = 240;

    /**
     * @description 菜单方向
     * @type {('horizontal' | 'vertical')}
     * @memberof DrbarControlBase
     */
    public menuDir: 'horizontal' | 'vertical' = 'vertical';

    /**
     * @description 显示模式(DEFAULT:默认模式,INDEXMODE:嵌入实体首页视图中)
     * @type {('DEFAULT' | 'INDEXMODE')}
     * @memberof DrbarControlBase
     */
    public showMode: 'DEFAULT' | 'INDEXMODE' = 'DEFAULT';

    /**
     * @description 菜单项数据
     * @type {any[]}
     * @memberof DrbarControlBase
     */
    public menuItems: any[] = [];

    /**
     * @description dabar部件事件
     * @type {(Subscription | undefined)}
     * @memberof DrbarControlBase
     */
    public drbarControlEvent: Subscription | undefined;

    /**
     * @description 静态参数变化
     * @param {*} newVal 新值
     * @param {*} oldVal 旧值
     * @memberof DrbarControlBase
     */
    public onStaticPropsChange(newVal: any, oldVal: any) {
        super.onStaticPropsChange(newVal, oldVal);
        this.showMode = newVal.showMode == 'INDEXMODE' ? 'INDEXMODE' : 'DEFAULT';
        this.menuDir = newVal.showMode == 'INDEXMODE' ? 'horizontal' : newVal.menuDir == 'horizontal' ? 'horizontal' : 'vertical';
    }

    /**
     * 部件模型初始化
     * 
     * @memberof DrbarControlBase
     */
    public async ctrlModelInit() {
        await super.ctrlModelInit();
        this.width = this.controlInstance.width >= 240 ? this.controlInstance.width : 240;
        this.initDrbarBasicData();
    }

    /**
     * 部件初始化
     * 
     * @memberof DrbarControlBase
     */
    public ctrlInit() {
        super.ctrlInit();
        if (this.viewState) {
            this.drbarControlEvent = this.viewState.subscribe(({ tag, action, data }: any) => {
                if (!Object.is(tag, this.name)) {
                    return;
                }
                if (Object.is('state', action)) {
                    this.handleFormChange(data);
                }
                if (Object.is('change', action)) {
                    this.selection = data;
                }
            });
        }
        this.initDefaultSelection();
    }

    /**
     * @description 初始化默认选中
     * @memberof DrbarControlBase
     */
    public initDefaultSelection() {
        if (this.items.length > 0) {
            if (this.showMode == 'INDEXMODE') {
                this.selection = this.items.length > 1 ? this.items[1] : this.items[0];
            }
        }
    }

    /**
     * 初始化关系栏数据项
     * 
     * @memberof DrbarControlBase
     */
    public initDrbarBasicData() {
        const ctrlItems: Array<IPSDEDRCtrlItem> = this.controlInstance.getPSDEDRCtrlItems() || [];
        const counterRef = this.controlInstance.getPSAppCounterRef();
        let counterService: any = undefined;
        if (counterRef) {
            counterService = Util.findElementByField(this.counterServiceArray, 'id', counterRef.id)?.service;
        }
        ctrlItems.forEach((item: IPSDEDRCtrlItem) => {
            Object.assign(item, { disabled: true, groupCodeName: (item as IPSDEDRBarItem).getPSDEDRBarGroup?.()?.id || '' });
            // let _item: any = {
            //     text: this.$tl(item.getCapPSLanguageRes()?.lanResTag, item.caption),
                
            //     id: item.name?.toLowerCase(),
            //     name: item.name?.toLowerCase(),
            //     iconcls: (item as any).getPSSysImage?.()?.cssClass,
            //     icon: (item as any).getPSSysImage?.()?.imagePath,
            //     view: item,
            //     groupCodeName: (item as IPSDEDRBarItem).getPSDEDRBarGroup?.()?.id || '',
            // }
            // if (item.counterId && counterService) {
            //     Object.assign(_item, {
            //         counter: {
            //             id: item.counterId,
            //             count: counterService?.counterData?.[item.counterId.toLowerCase()],
            //             offset: _item.groupCodeName ? [10, 19] : [-16, 19],
            //             showZero: 1
            //         }
            //     });
            // }
            this.items.push(item);
        })
        this.initMenuItems();
    }

    /**
     * @description 初始化菜单项集合
     * @memberof DrbarControlBase
     */
    public initMenuItems() {
        const groups = this.controlInstance.getPSDEDRBarGroups() || [];
        const menuItems: any[] = [];
        const noGroupItems = this.items.filter((item: any) => { return !item.groupCodeName; });
        menuItems.push.apply(menuItems, noGroupItems);
        groups.forEach((group: IPSDEDRBarGroup) => {
            const items = this.items.filter((item: any) => { return Object.is(item.groupCodeName, group.id); });
            if (items.length == 1) {
                menuItems.push(items[0]);
            } else {
                menuItems.push({
                    caption: this.$tl(group.getCapPSLanguageRes()?.lanResTag, group.caption),
                    codeName: group.id,
                    id: group.id,
                    name: group.id,
                    hidden: group.hidden,
                    items: items,
                });
            }
        });
        this.menuItems = [...menuItems];
    }

    /**
     * 获取子项
     *
     * @param {any[]} items
     * @param {string} id
     * @returns {*}
     * @memberof DrbarControlBase
     */
    public getItem(id: string): any {
        return this.items.find((item: any) => {
           return item.id == id;
        });
    }

    /**
     * 节点选中
     *
     * @param {*} $event
     * @memberof DrbarControlBase
     */
    public select(item: any) {
        if (item == this.selection.id) {
            return;
        }
        const selectItem = this.getItem(item);
        this.handleCtrlEvents('onselectionchange', { action: 'SelectionChange', data: selectItem, navData: this.navdatas }).then((res: boolean) => {
            const tempContext = Util.deepCopy(this.context);
            const tempViewParams = Util.deepCopy(this.viewparams);
            if (selectItem.getPSAppView?.()) {
                Object.assign(tempContext, { viewpath: selectItem.getPSAppView().modelPath });
            }
            Object.assign(selectItem, {
                srfnavdata: {
                    context: Object.assign(tempContext, selectItem.localContext ? selectItem.localContext : {}),
                    viewparams: Object.assign(tempViewParams, selectItem.localViewParam ? selectItem.localViewParam : {})
                }
            });
            this.ctrlEvent({
                controlname: this.controlInstance.name,
                action: 'selectionchange',
                data: selectItem
            });
        });
    }

    /**
     * 设置关系项状态
     *
     * @param {any[]} items
     * @param {boolean} state
     * @memberof DrbarControlBase
     */
    public handleFormChange(args: any) {
        if (args && Object.is(args.srfuf, '1')) {
            this.items.forEach((item: any) => {
                // 取消禁用
                item.disabled = false;
                // 设置导航参数
                const navigateContext: IPSNavigateContext[] = item.getPSNavigateContexts() || [];
                if (navigateContext && navigateContext.length > 0) {
                    const localContext = Util.formatNavParam(navigateContext);
                    let _context: any = Util.computedNavData(args, this.context, this.viewparams, localContext);
                    item.localContext = _context;
                }
                const navigateParams: IPSNavigateParam[] = item.getPSNavigateParams() || [];
                if (navigateParams && navigateParams.length > 0) {
                    const localViewParam = Util.formatNavParam(navigateParams);
                    let _param: any = Util.computedNavData(args, this.context, this.viewparams, localViewParam);
                    item.localViewParam = _param;
                }
            })
        } else {
          this.items.forEach((item: any) => {
            item.disabled = true;
          });
        }
        if (args.srfparentkey) {
          Object.assign(this.context, { srfparentkey: args.srfparentkey })
          Object.assign(this.viewparams, { srfparentkey: args.srfparentkey })
        }
        if (args.srfparentdename) {
          Object.assign(this.context, { srfparentdename: args.srfparentdename })
          Object.assign(this.viewparams, { srfparentdename: args.srfparentdename })
        }
        this.$forceUpdate();
    }

    /**
     * @description 部件销毁
     * @memberof DrbarControlBase
     */
    public ctrlDestroyed() {
        super.ctrlDestroyed();
        if (this.drbarControlEvent) {
            this.drbarControlEvent.unsubscribe();
        }
    }

}