ibiz-wf-exp-bar.ts 6.2 KB
Newer Older
ibizdev's avatar
ibizdev committed
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
/**
 * 工作流树导航部件
 *
 * @class IBizWFExpBar
 * @extends {IBizControl}
 */
class IBizWFExpBar extends IBizControl {

    /**
     * 展开数据项
     *
     * @type {Array<string>}
     * @memberof IBizWFExpBar
     */
    public expandItems: Array<string> = [];

    /**
     * 导航树部件是否收缩,默认展开
     *
     * @type {boolean}
     * @memberof IBizWFExpBar
     */
    public isCollapsed: boolean = true;

    /**
     * 导航菜单数据项
     * 
     * @type {Array<any>}
     * @memberof IBizWFExpBarService
     */
    public items: Array<any> = [];

    /**
     * 选中菜单项
     * 
     * @type {*}
     * @memberof IBizWFExpBarService
     */
    public selectItem: any = {};

    /**
     * 计数器
     *
     * @type {IBizUICounterService}
     * @memberof IBizWFExpBarService
     */
    public UICounter: IBizUICounter = null;

    /**
     * Creates an instance of IBizWFExpBar.
     * 创建 IBizWFExpBar 实例
     * 
     * @param {*} [otps={}]
     * @memberof IBizWFExpBar
     */
    constructor(otps: any = {}) {
        super(otps);

        if (this.getViewController()) {
            const viewController = this.getViewController();
            viewController.on(IBizViewController.INITED).subscribe(() => {
                this.UICounter = viewController.uicounters.get(this.getUICounterName());
                this.onCounterChanged(this.items);
                this.UICounter.on(IBizUICounter.COUNTERCHANGED).subscribe((data) => {
                    this.onCounterChanged(this.items);
                });
            });
        }
    }

    /**
     * 加载导航树数据
     * 
     * @param {*} _opt 
     * @memberof IBizWFExpBar
     */
    public load(_opt: any): void {
        let opts: any = {};
        Object.assign(opts, _opt);
        Object.assign(opts, { srfaction: 'fetch', srfctrlid: this.getName() });

        this.iBizHttp.post(this.getBackendUrl(), opts).subscribe((result) => {
            if (result.ret === 0) {
                // this.items = result.items;
                this.onCounterChanged(result.items);
                this.formarItems(result.items);
                this.items = [...result.items];
                this.fire(IBizWFExpBar.LOADED, this.items[0]);
            }
        }, error => {
            console.log(error);
        });
    }

    /**
     * 格式化数据项
     *
     * @param {*} _items
     * @returns {*}
     * @memberof IBizWFExpBar
     */
    public formarItems(_items: any): any {
        _items.forEach(item => {
            item.disabled = false;
            item.class = 'wfbadge';
            if (item.items) {
                this.expandItems.push(item.id);
                this.formarItems(item.items);
            }
        });
    }

    /**
     * 菜单项选中处理
     *
     * @param {*} [item={}]
     * @returns {void}
     * @memberof IBizWFExpBar
     */
    public selection(item: any = {}): void {
        if (item.items && item.items.length > 0) {
            return;
        }

        if (Object.is(item.id, this.selectItem.id)) {
            return;
        }
        this.selectItem = {};
        Object.assign(this.selectItem, item);

        this.fire(IBizWFExpBar.SELECTIONCHANGE, this.selectItem);
    }


    /**
     * 菜单节点选中处理
     * 
     * @param {*} [item={}] 
     * @memberof IBizWFExpBar
     */
    public expandedAndSelectSubMenu(item: any = {}): void {
        if (Object.is(item.id, this.selectItem.id)) {
            return;
        }
        this.selectItem = {};
        Object.assign(this.selectItem, item);

        this.fire(IBizWFExpBar.SELECTIONCHANGE, this.selectItem);
    }

    /**
     * 获取计数器名称
     * 在发布器中重写
     * 
     * @returns {string} 
     * @memberof IBizWFExpBar
     */
    public getUICounterName(): string {
        return undefined;
    }

    /**
     * 设置选中项
     *
     * @param {*} [item={}]
     * @memberof IBizWFExpBar
     */
    public setSelectItem(item: any = {}): void {
        if (item) {
            this.selectItem = {};
            Object.assign(this.selectItem, item);
        }
    }

    /**
     * 计数器值变化
     *
     * @returns {void}
     * @memberof IBizWFExpBar
     */
    public onCounterChanged(items: Array<any>): void {
        if (!this.UICounter) {
            return;
        }
        const data = this.UICounter.getData();
        if (!data) {
            return;
        }
        let bNeedReSelect: boolean = this.itemSelect(items, data);

        if (bNeedReSelect) {
            this.selectItem = {};
            Object.assign(this.selectItem, this.items[0]);
            this.fire(IBizWFExpBar.SELECTIONCHANGE, this.selectItem);
        }
    }

    /**
     * 选中项
     *
     * @param {Array<any>} items
     * @param {*} [data={}]
     * @returns {boolean}
     * @memberof IBizWFExpBar
     */
    public itemSelect(items: Array<any>, data: any = {}): boolean {
        let bNeedReSelect: boolean = false;
        items.forEach(item => {
            let counterid = item.counterid;
            let countermode = item.countermode;

            item.show = true;
            let count = data[counterid];
            if (!count) {
                count = 0;
            }
            if (count === 0 && countermode && countermode === 1) {
                item.show = false;
                // 判断是否选中列,如果是则重置选中
                if (this.selectItem && Object.is(this.selectItem.id, item.id)) {
                    bNeedReSelect = true;
                }
            }

            item.counterdata = count;
            if (item.items) {
                bNeedReSelect = this.itemSelect(item.items, data);
            }
        });
        return bNeedReSelect;
    }

    /**
     * 获取数据项
     *
     * @returns {Array<any>}
     * @memberof IBizWFExpBar
     */
    public getItems(): Array<any> {
        return this.items;
    }

    /*****************事件声明************************/
    /**
     * 选择变化
     *
     * @static
     * @memberof IBizWFExpBar
     */
    public static SELECTIONCHANGE = "SELECTIONCHANGE";

    /**
     * 加载完成
     *
     * @static
     * @memberof IBizWFExpBar
     */
    public static LOADED = 'LOADED';
}