app-mpicker.vue 19.0 KB
Newer Older
1 2
<template>
    <div class="app-mpicker">
3 4 5
        <div class="app-mpicker-select-container" ref="appMpickerSelect">
            <el-select ref="dragSelect" :value="curValue" :placeholder="placeholder" multiple filterable remote :loading="loading" :remote-method="onSearch" size="small" @change="onSelect" @remove-tag="onRemove" :disabled="disabled || readonly"
            @blur.native.capture='blur'  @focus="focus" @visible-change="handleVisibleChange">
6
                <template v-if="loading" slot="empty">
7
                    <span class="is-empty el-icon-loading"></span>
8
                </template>
9
                <el-option v-for="item in items" :key="item[deKeyField]" :label="item[deMajorField]" :value="item[deKeyField]"></el-option>
10 11 12 13 14 15 16 17 18 19
            </el-select>
            <span class="app-mpicker-open-icon">
                <i class="el-icon-search" @click="openView"></i>
            </span>
        </div>
    </div>
</template>
<script lang = 'ts'>
import { Component, Vue, Prop, Watch } from 'vue-property-decorator';
import { Subject, Subscription } from 'rxjs';
20 21
import { LogUtil, Util } from 'ibiz-core';
import Sortable from 'sortablejs';
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

@Component({
})
export default class AppMpicker extends Vue {

    /**
     * 表单数据
     */
    @Prop() data?: any;

    /**
     * 是否禁用
     */
    @Prop() disabled?: boolean;

	/**
	 * 只读模式
	 * 
	 * @type {boolean}
	 */
	@Prop({default: false}) public readonly?: boolean;

    /**
     * 表单项值
     */
    @Prop() value?: any;

    /**
     * 值项
     */
    @Prop() valueitem?: any;

    /**
     * 局部上下文导航参数
     * 
     * @type {any}
     * @memberof AppMpicker
     */
    @Prop() public localContext!:any;

    /**
     * 局部导航参数
     * 
     * @type {any}
     * @memberof AppMpicker
     */
    @Prop() public localParam!:any;

    /**
     * 表单项名称
     */
    @Prop() name: any;
    
    /**
     * 视图上下文
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop() public context!: any;

    /**
     * 视图参数
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop() public viewparams!: any;

    /**
     * AC参数
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop({default: () => {}}) public acParams?: any;

    /**
     * 应用实体主信息属性名称
     *
     * @type {string}
     * @memberof AppMpicker
     */
    @Prop({default: 'srfmajortext'}) public deMajorField!: string;

    /**
     * 应用实体主键属性名称
     *
     * @type {string}
     * @memberof AppMpicker
     */
    @Prop({default: 'srfkey'}) public deKeyField!: string;

    /**
     * 表单服务
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop() public service?: any;

    /**
     * 打开对应的选择视图
     */
    @Prop() pickupView?: any;

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
    /**
     * 值类型
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop({ default: 'SIMPLE' })
    public valueType?: 'SIMPLE' | 'OBJECTS';

    /**
     * 值分隔符
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop({ default: ',' })
    public valueSeparator?: string;

    /**
     * 对象标识属性
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop()
    public objectIdField?: string;

    /**
     * 对象名称属性
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop()
    public objectNameField?: string;

    /**
     * 对象值属性
     *
     * @type {*}
     * @memberof AppMpicker
     */
    @Prop()
    public objectValueField?: string;

    /**
     * 占位提示
     *
     * @type {*}
     */
    @Prop({ default: '' }) public placeholder?: string;
    
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
    /**
     * 当前表单项绑定值key的集合
     */
    public curValue: any = [];

    /**
     * 所有操作过的下拉选选项
     */
    public items: Array<any> = [];

    /**
     * 选中项key-value键值对
     * 
     */
    public selectItems: Array<any> = [];

    /**
     * 下拉远程加载状态
     * 
     * @type {boolean}
     * @memberof AppMpicker
     */
    public loading: boolean = false;

    /**
     * 模态事件
     * 
     * @type {Subscription | undefined}
     * @memberof AppMpicker
     */
    public modalEvent: Subscription | undefined;

    /**
     * 监听curvalue值
     * @param newVal 
     * @param val 
     */
    @Watch('value', {immediate:true, deep: true })
    oncurvalueChange(newVal: any, val: any) {
        this.curValue = [];
        this.selectItems = [];
        if (newVal) {
            try {
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
                if (this.valueType == 'OBJECTS') {
                    this.value.forEach((item: any) => {
                        const _item = Util.deepCopy(item);
                        Object.assign(_item, {
                            [this.deKeyField]: item[this.objectIdField as string],
                            [this.deMajorField]: item[this.objectNameField as string]
                        })
                        if (this.objectValueField) {
                            Object.assign(_item, {
                                ...item[this.objectValueField]
                            })
                            delete _item[this.objectValueField]
                        }
                        if (_item[this.deKeyField]) {
                            this.selectItems.push(_item)
                        }
                    })
                } else {
                    if (this.objectIdField) {
                        let values = this.value.split(this.valueSeparator);
                        values.forEach((value: string) => {
                            this.selectItems.push({
                                [this.deKeyField]: value,
                            })
                        })
                    } else {
                        this.selectItems = JSON.parse(newVal);
                    }
                }
                this.selectItems.forEach((item: any) => {
                    this.curValue.push(item[this.deKeyField]);
                    let index = this.items.findIndex((i) => Object.is(i[this.deKeyField], item[this.deKeyField]));
                    if (index < 0) {
                        this.items.push({ [this.deMajorField]: item[this.deMajorField], [this.deKeyField]: item[this.deKeyField] });
                    }
                });
            } catch (error: any) {
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
              if(error.name === 'SyntaxError'){
                let srfkeys:any = newVal.split(',');
                let srfmajortexts:any = null;
                if(this.valueitem && this.data[this.valueitem]){
                    srfmajortexts = this.data[this.valueitem].split(',');
                }
                if(srfkeys.length && srfkeys.length > 0 && srfmajortexts.length && srfmajortexts.length > 0 && srfkeys.length == srfmajortexts.length){
                    srfkeys.forEach((id: any, index: number) => {
                        this.curValue.push(id);
                        this.selectItems.push({[this.deKeyField]: id, [this.deMajorField]: srfmajortexts[index]});
                        let _index = this.items.findIndex((i) => Object.is(i[this.deKeyField],id));
                        if (_index < 0) {
                            this.items.push({[this.deKeyField]: id, [this.deMajorField]: srfmajortexts[index]});
                        }
                    });
                }
              }
            }
        }
        this.$forceUpdate();
    }

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 330 331
    /**
     * 生命周期
     *
     * @memberof AppMpicker
     */
    public mounted() {
        this.setSort();
        if(this.objectNameField){
            this.onSearch("");
        }
    }

    /**
     * 生命周期
     * 
     * @memberof AppMpicker
     */
    public updated() {
        const appMpickerSelect = this.$refs.appMpickerSelect as HTMLElement
        if (appMpickerSelect) {
            const el = appMpickerSelect.querySelectorAll('.el-select__tags-text')
            // 获取元素内文本值,添加到title
            if (el.length > 0) {
                el.forEach((item: any) => {
                    item.title = item.innerText
                })
            }
        }
    }

    /**
     * 设置拖拽排序
     *
     * @memberof AppMpicker
     */
    public setSort() {
        const el = (this.$refs.dragSelect as any).$el.querySelectorAll('.el-select__tags > span')[0]
        Sortable.create(el, {
            ghostClass: 'sortable-ghost', 
            setData: (dataTransfer: any) => {
                dataTransfer.setData('Text', '')
            },
            onEnd: (evt: any) => {
                const targetRow = this.curValue.splice(evt.oldIndex, 1)[0]
                this.curValue.splice(evt.newIndex, 0, targetRow);
                this.onSelect(this.curValue);
            }
        })
    }

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
    /**
     * 远程执行搜索
     *
     * @param {*} query
     * @memberof AppMpicker
     */
    public onSearch(query: any) {
        // 公共参数处理
        let data: any = {};
        const bcancel: boolean = this.handlePublicParams(data);
        if (!bcancel) {
            return;
        }
        // 参数处理
        let _context = data.context;
        let _param = data.param;
348 349 350
        // TODO 防止查询参数过长
        if (query) {
            Object.assign(_param, { query });
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
        }
        // 错误信息国际化
        let error: string = (this.$t('components.appmpicker.error') as any);
        let miss: string = (this.$t('components.appmpicker.miss') as any);
        let requestException: string = (this.$t('components.appmpicker.requestexception') as any);
        if(!this.service){
            this.$throw(miss+'service','onSearch');
        } else if(!this.acParams.serviceName) {
            this.$throw(miss+'serviceName','onSearch');
        } else if(!this.acParams.interfaceName) {
            this.$throw(miss+'interfaceName','onSearch');
        } else {
          this.loading = true;
          this.service.getItems(this.acParams.serviceName,this.acParams.interfaceName, _context, _param).then((response: any) => {
              this.loading = false;
              if (!response) {
                  this.$throw(requestException,'onSearch');
              } else {
                  this.items = [...response];
              }
          }).catch((error: any) => {
              this.loading = false;
              LogUtil.log(error);
          });
        }
    }

    /**
     * 下拉选中回调
     *
     * @param {*} selects
     * @memberof AppMpicker
     */
    public onSelect(selects: any) {
        let val: Array<any> = [];
386
        let value: any = null;
387 388 389
        if (selects.length > 0) {
            selects.forEach((select: any) => {
                let index = this.items.findIndex((item) => Object.is(item[this.deKeyField], select));
390
                let item: any = {};
391
                if (index >= 0) {
392
                    item = this.items[index];
393 394 395
                } else {
                    index = this.selectItems.findIndex((item: any) => Object.is(item[this.deKeyField], select));
                    if (index >= 0) {
396 397 398 399 400 401 402 403 404 405
                        item = this.selectItems[index];
                    }
                }
                if (this.valueType == 'OBJECTS') {
                    val.push(this.handleObjectParams(item))
                } else {
                    if (this.objectIdField) {
                        val.push(item[this.deKeyField])
                    } else {
                        val.push({ [this.deKeyField]: item[this.deKeyField], [this.deMajorField]: item[this.deMajorField] })
406 407 408
                    }
                }
            });
409 410 411 412 413 414 415
            if (val.length > 0) {
                if (this.valueType == 'OBJECTS') {
                    value = val;
                } else {
                    value = this.objectIdField ? val.join(this.valueSeparator) : JSON.stringify(val);
                }
            }
416 417 418 419
            this.$emit('formitemvaluechange', { name: this.name, value: value });
        }
    }

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
    /**
     * 处理对象数据类型抛值
     * @param select 选中数据
     */
    public handleObjectParams(select: any): any {
        const object: any = {};
        if (this.objectIdField) {
            Object.assign(object, {
                [this.objectIdField]: select[this.deKeyField],
            })
        }
        if (this.objectNameField) {
            Object.assign(object, {
                [this.objectNameField]: select[this.deMajorField],
            })
        }
        if (this.objectValueField) {
            Object.assign(object, {
                [this.objectValueField]: Util.deepCopy(select)
            })
        }
        return object
    }

444 445 446 447 448 449 450 451 452 453
    /**
     * 移除标签回调
     *
     * @param {*} tag
     * @memberof AppMpicker
     */
    public onRemove(tag: any) {
        let index = this.selectItems.findIndex((item: any) => Object.is(item[this.deKeyField], tag));
        if (index >= 0) {
            this.selectItems.splice(index, 1);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
            let val: Array<any> = [];
            let value: any = null;
            this.selectItems.forEach((select: any) => {
                if (this.valueType == 'OBJECTS') {
                    val.push(this.handleObjectParams(select))
                } else {
                    if (this.objectIdField) {
                        val.push(select[this.deKeyField])
                    } else {
                        val.push({ [this.deKeyField]: select[this.deKeyField], [this.deMajorField]: select[this.deMajorField] })
                    }
                }
            })
            if (val.length > 0) {
                if (this.valueType == 'OBJECTS') {
                    value = val;
                } else {
                    value = this.objectIdField ? val.join(this.valueSeparator) : JSON.stringify(val);
                }
            }
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
            this.$emit('formitemvaluechange', { name: this.name, value: value });
        }
    }

    /**
     * 公共参数处理
     *
     * @param {*} arg
     * @returns
     * @memberof AppMpicker
     */
    public handlePublicParams(arg: any): boolean {
        if (!this.data) {
            this.$throw((this.$t('components.AppMpicker.formdataException') as any),'handlePublicParams');
            return false;
        }
        // 合并表单参数
        arg.param = this.viewparams ? JSON.parse(JSON.stringify(this.viewparams)) : {};
        arg.context = this.context ? JSON.parse(JSON.stringify(this.context)) : {};
        // 附加参数处理
        if (this.localContext && Object.keys(this.localContext).length >0) {
            let _context = this.$util.computedNavData(this.data,arg.context,arg.param,this.localContext);
            Object.assign(arg.context,_context);
        }
        if (this.localParam && Object.keys(this.localParam).length >0) {
            let _param = this.$util.computedNavData(this.data,arg.param,arg.param,this.localParam);
            Object.assign(arg.param,_param);
        }
        return true;
    }

    /**
     * 打开视图
     *
     * @returns
     * @memberof AppMpicker
     */
    public openView() {
        if (this.disabled) {
            return;
        }
        if (this.pickupView && Object.keys(this.pickupView).length > 0) {
            // 参数处理
            const view = { ...this.pickupView };
            // 公共参数处理
            let data: any = {};
            const bcancel: boolean = this.handlePublicParams(data);
            if (!bcancel) {
                return;
            }
            // 参数处理
            let _context = data.context;
            let _viewparams = data.param;
            let _selectItems = JSON.parse(JSON.stringify(this.selectItems));
            if(!Object.is(this.deKeyField,"srfkey")){
                _selectItems.forEach((item:any, index:number)=>{
                    _selectItems[index].srfkey = item[this.deKeyField];
                });
            }
            if(!Object.is(this.deMajorField,"srfmajortext")){
                _selectItems.forEach((item:any, index:number)=>{
                    _selectItems[index].srfmajortext = item[this.deMajorField];
                });
            }
            _context = Object.assign(_context, { srfparentdata: { srfparentkey: this.data[this.deKeyField] }, });
            _viewparams = Object.assign(_viewparams,{ selectedData: [..._selectItems]});
            if (view && view.viewpath) {
              _context.viewpath = view.viewpath;
            }
            let formdata = this.data;
            const modal: Subject<any> = this.$appmodal.openModal(view, _context, _viewparams)
            this.modalEvent = modal.subscribe((result: any) => {
                if (!result || !Object.is(result.ret, 'OK')) {
                    return;
                }
                let selects: Array<any> = [];
                if (result.datas && Array.isArray(result.datas)) {
                    result.datas.forEach((select: any) => {
552 553 554 555 556 557 558 559 560 561 562 563 564
                        Object.assign(select, {
                            [this.deKeyField]: select[this.deKeyField] ? select[this.deKeyField] : select.srfkey,
                            [this.deMajorField]: select[this.deMajorField] ? select[this.deMajorField] : select.srfmajortext,
                        })
                        if (this.valueType == 'OBJECTS') {
                            selects.push(this.handleObjectParams(select));
                        } else {
                            if (this.objectIdField) {
                                selects.push(select[this.deKeyField])
                            } else {
                                selects.push({ [this.deKeyField]: select[this.deKeyField], [this.deMajorField]: select[this.deMajorField] })
                            }
                        }
565 566
                        let index = this.items.findIndex((item) => Object.is(item[this.deKeyField], select[this.deKeyField]));
                        if (index < 0) {
567
                            this.items.push(select);
568 569 570 571
                        }
                    });
                }
                if (this.name && this.data) {
572 573 574 575 576 577 578 579
                    let value: any = null;
                    if (selects.length > 0) {
                        if (this.valueType == 'OBJECTS') {
                            value = selects;
                        } else {
                            value = this.objectIdField ? selects.join(this.valueSeparator) : JSON.stringify(selects);
                        }
                    }
580 581 582 583 584 585 586
                    this.$emit('formitemvaluechange', { name: this.name, value: value });
                }
            })
        }
    }

    /**
587
     * 处理下拉显示改变
588
     */
589 590 591
    public handleVisibleChange(visible: boolean) {
        if (visible) {
            this.onSearch('')
592 593 594 595 596 597 598 599 600
        }
    }

    public destroyed() {
        if (this.modalEvent) {
            this.modalEvent.unsubscribe();
        }
    }

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    /**
     * 输入框失焦
     * 
     * @memberof AppMpicker
     */
     public blur(){
        this.$emit('blur',{ name: this.name, value: this.value })
    }
    
    /**
     * 输入框聚焦
     * 
     * @memberof AppMpicker
     */
    public focus(){
        this.$emit('focus',{ name: this.name, value: this.value })
    }
618 619
}
</script>