app-picker-select-view.vue 16.7 KB
Newer Older
1 2 3
<template>
    <div class="app-picker-select-view">
        <Dropdown :visible="visible" trigger="custom" style="left:0px;width: 100%" @on-clickoutside="() => {triggerMenu(false);}" >
4
          <Input v-if="isSingleSelect" v-model="queryValue" class="tree-input" type="text" :placeholder="placeholder ? placeholder : $t('components.appPickerSelectView.placeholder')" :disabled="disabled" @on-change="OnInputChange" @on-focus="()=>{triggerMenu(true);}" >
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
              <template v-slot:suffix>
                  <i v-if="queryValue && !disabled" class='el-icon-circle-close' @click="onClear"></i>
                  <Icon :type="visible ? 'ios-arrow-up' : 'ios-arrow-down'" class="icon-arrow" @click="() => {triggerMenu();}"></Icon>
                  <icon v-if="linkview" type="ios-open-outline" @click="openLinkView"/>
              </template>
          </Input>
          <el-select v-if="!isSingleSelect" popper-class="select-no-dropdown" :value="keySet" multiple filterable remote :remote-method="($event) => {this.queryValue = $event;}" size="small" style="width:100%;" @change="onSelectChange" @focus="() => {triggerMenu(true);}" :disabled="disabled">
                <el-option v-for="(item, index) in items" :key="index" :label="item.srfmajortext" :value="item[deKeyField]"></el-option>
          </el-select>
          <DropdownMenu slot="list">
              <component
                :is="pickupView.viewname"
                :viewdata="viewdata"
                :viewparam="viewparam"
                :isShowButton="false"
                :viewDefaultUsage="false"
                @viewdataschange="onViewdatasChange"
                style="height:100%;">
              </component>
          </DropdownMenu>
        </Dropdown>
    </div>
</template>

<script lang="ts">
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
import { CreateElement } from 'vue';
import { Subject, Subscription } from 'rxjs';
import { ViewTool } from '@/utils/view-tool/view-tool';

@Component({
})
37
export default class AppPickerSelectView extends Vue {
38 39 40 41
    /**
     * 视图上下文
     *
     * @type {*}
42
     * @memberof AppPickerSelectView
43 44 45 46 47 48 49
     */
    @Prop() public context!: any;

    /**
     * 视图参数
     *
     * @type {*}
50
     * @memberof AppPickerSelectView
51 52 53 54 55 56 57
     */
    @Prop() public viewparams!: any;

    /**
     * 是否单选
     *
     * @type {*}
58
     * @memberof AppPickerSelectView
59 60 61 62 63 64 65
     */
    @Prop({default: 'true'}) public isSingleSelect!: any;
    
    /**
     * 当前多选框选中值的key集合
     *
     * @type {string}
66
     * @memberof AppPickerSelectView
67 68 69 70 71 72 73
     */
    public keySet: any = [];
    
    /**
     * 当前多选框选中项对象集合
     *
     * @type {string}
74
     * @memberof AppPickerSelectView
75 76 77 78 79 80 81
     */
    public selectItems: Array<any> = [];
    
    /**
     * 所有操作过的下拉选项对象集合
     *
     * @type {string}
82
     * @memberof AppPickerSelectView
83 84 85 86 87 88 89
     */
    public items: Array<any> = [];
    
    /**
     * 视图参数
     *
     * @type {string}
90
     * @memberof AppPickerSelectView
91 92 93 94 95 96 97
     */
    public viewparam: any = JSON.stringify(this.viewparams);
    
    /**
     * 视图上下文
     *
     * @type {string}
98
     * @memberof AppPickerSelectView
99 100 101 102 103 104 105
     */
    public viewdata: any = JSON.stringify(this.context);

    /**
     * 表单数据
     *
     * @type {*}
106
     * @memberof AppPickerSelectView
107 108 109 110 111 112 113
     */
    @Prop() public data!: any;

    /**
     * 值
     *
     * @type {*}
114
     * @memberof AppPickerSelectView
115 116 117 118 119 120 121
     */
    @Prop() public value: any;

    /**
     * 是否启用
     *
     * @type {boolean}
122
     * @memberof AppPickerSelectView
123 124 125 126 127 128 129
     */
    @Prop({default: false}) public disabled!: boolean;
    
    /**
     * 应用实体主信息属性名称
     *
     * @type {string}
130
     * @memberof AppPickerSelectView
131 132 133 134 135 136 137
     */
    @Prop({default: 'srfmajortext'}) public deMajorField!: string;

    /**
     * 应用实体主键属性名称
     *
     * @type {string}
138
     * @memberof AppPickerSelectView
139 140 141 142 143 144 145
     */
    @Prop({default: 'srfkey'}) public deKeyField!: string;

    /**
     * 输入框值(搜索值)
     *
     * @type {string}
146
     * @memberof AppPickerSelectView
147 148 149 150 151 152 153
     */
    public queryValue: any = '';

    /**
     * 值项名称
     *
     * @type {string}
154
     * @memberof AppPickerSelectView
155 156 157 158 159 160 161
     */
    @Prop() public valueitem!: string;

    /**
     * 关联视图名称
     *
     * @type {string}
162
     * @memberof AppPickerSelectView
163 164 165 166 167 168 169
     */
    @Prop() public pickupView?: any;

    /**
     * 数据链接视图参数
     *
     * @type {*}
170
     * @memberof AppPickerSelectView
171 172 173 174 175 176 177
     */
    @Prop() public linkview?: any;

    /**
     * 提示信息
     *
     * @type {string}
178
     * @memberof AppPickerSelectView
179 180 181 182 183 184 185
     */
    @Prop() public placeholder!: string;

    /**
     * 属性项名称
     *
     * @type {string}
186
     * @memberof AppPickerSelectView
187 188 189 190
     */
    @Prop() public name!: string;

    /**
191 192 193 194 195 196 197 198 199 200 201 202
     * 局部上下文导航参数
     * 
     * @type {any}
     * @memberof AppPickerSelectView
     */
    @Prop() public localContext!:any;

    /**
     * 局部导航参数
     * 
     * @type {any}
     * @memberof AppPickerSelectView
203
     */
204
    @Prop() public localParam!:any;
205 206 207 208 209

    /**
     * 下拉显示控制变量
     *
     * @type {string}
210
     * @memberof AppPickerSelectView
211 212 213 214 215 216 217
     */
    public visible: boolean = false;

    /**
     * 父视图数据
     *
     * @type {string}
218
     * @memberof AppPickerSelectView
219 220 221 222 223 224 225
     */
    public srfparentdata: any = {};

    /**
     * 输入框change事件
     * 
     * @param $event 事件对象
226
     * @memberof AppPickerSelectView
227 228 229 230 231 232 233 234 235 236
     */
    public OnInputChange($event: any){
        let _viewdata =  Object.assign({ query: this.queryValue }, JSON.parse(this.viewdata)) ;
        this.viewdata = JSON.stringify(_viewdata);
    }

    /**
     * 输入框change事件
     * 
     * @param $event 事件对象
237
     * @memberof AppPickerSelectView
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
     */
    public triggerMenu(visible?: boolean){
        if(this.disabled){
          return;
        }
        if (!visible) {
            this.visible = !this.visible;
        } else {
            this.visible = visible;
        }
    }

    /**
     * 公共参数处理
     *
     * @param {*} arg
     * @returns
255
     * @memberof AppPickerSelectView
256 257 258
     */
    public handlePublicParams(arg: any): boolean {
        if (!this.data) {
259
            this.$Notice.error({ title: (this.$t('components.appPickerSelectView.error') as any), desc: (this.$t('components.appPickerSelectView.formdataException') as any) });
260 261 262
            return false;
        }
        // 合并表单参数
263 264
        arg.param = this.viewparams ? JSON.parse(JSON.stringify(this.viewparams)) : {};
        arg.context = this.context ? JSON.parse(JSON.stringify(this.context)) : {};
265
        // 附加参数处理
266 267
        if (this.localContext && Object.keys(this.localContext).length >0) {
            let _context = this.$util.computedNavData(this.data,arg.context,arg.param,this.localContext);
268 269
            Object.assign(arg.context,_context);
        }
270 271
        if (this.localParam && Object.keys(this.localParam).length >0) {
            let _param = this.$util.computedNavData(this.data,arg.param,arg.param,this.localParam);
272 273 274 275 276 277 278 279 280 281
            Object.assign(arg.param,_param);
        }
        return true;
    }

    /**
     * 监控值
     *
     * @param {*} newVal
     * @param {*} oldVal
282
     * @memberof AppPickerSelectView
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
     */
    @Watch('data',{deep:true})
    onActivedataChange(newVal: any, oldVal: any) {
        // 公共参数处理
        let data: any = {};
        const bcancel: boolean = this.handlePublicParams(data);
        if (!bcancel) {
            return;
        }
        // 参数处理
        this.viewdata = JSON.stringify(data.context);
        this.viewparam = JSON.stringify(data.param);
    }

    /**
     * 值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
302
     * @memberof AppPickerSelectView
303 304 305 306 307 308
     */
    @Watch('value', { deep: true })
    public onValueChange(newVal: any, oldVal: any) {
        if(this.isSingleSelect){
            this.queryValue = newVal;
            if (!this.data || !this.valueitem || !this.data[this.valueitem]) {
309
                this.$Notice.error({ title: (this.$t('components.appPickerSelectView.error') as any), desc: (this.$t('components.appPickerSelectView.editor') as any)+this.name+(this.$t('components.appPickerSelectView.valueitemException') as any) });
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
            }else{
                let _viewparam = JSON.parse(this.viewparam);
                _viewparam.selectedData = [{srfkey: this.data[this.valueitem], srfmajortext: this.value }];
                this.viewparam = JSON.stringify(_viewparam);
            }
        }else{
            this.keySet = [];
            this.selectItems = [];
            if (newVal) {
                this.selectItems = JSON.parse(newVal);
                this.selectItems.forEach((item: any) => {
                    this.keySet.push(item.srfkey);
                    let index = this.items.findIndex((i) => Object.is(i.srfkey, item.srfkey));
                    if (index < 0) {
                        this.items.push({ srfmajortext : item.srfmajortext, srfkey: item.srfkey });
                    }
                });
            }
328 329 330
            let _viewparam = JSON.parse(this.viewparam);
            _viewparam.selectedData = this.selectItems;
            this.viewparam = JSON.stringify(_viewparam);
331 332 333 334 335 336 337
        }
        this.$forceUpdate();
    }

    /**
     * 生命周期
     *
338
     * @memberof AppPickerSelectView
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
     */
    public created() {
        // 公共参数处理
        let data: any = {};
        const bcancel: boolean = this.handlePublicParams(data);
        if (!bcancel) {
            return;
        }
        // 参数处理
        this.viewdata = JSON.stringify(data.context);
        this.viewparam = JSON.stringify(data.param);
    }

    /**
     * vue 生命周期
     *
355
     * @memberof AppPickerSelectView
356 357 358 359 360 361 362 363 364
     */
    public destroyed() {

    }

    /**
     * 设置值
     *
     * @param {*} item
365
     * @memberof AppPickerSelectView
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
     */
    public onViewdatasChange($event: any) {
        if($event.length == 0){
            this.onClear(null);
            return;
        }
        if(this.isSingleSelect){
             this.visible = false;
            if (this.valueitem) {
                let tempvalue = $event[0][this.deKeyField] ? $event[0][this.deKeyField] : $event[0].srfkey;
                this.$emit('formitemvaluechange', { name: this.valueitem, value: tempvalue });
            }
            if (this.name) {
                let tempvalue = $event[0][this.deMajorField] ? $event[0][this.deMajorField] : $event[0].srfmajortext;
                this.$emit('formitemvaluechange', { name: this.name, value: tempvalue });
            }
        }else{
            let selects: Array<any> = [];
            if ($event && Array.isArray($event)) {
                $event.forEach((select: any) => {
                    selects.push({ srfkey: select.srfkey, srfmajortext: select.srfmajortext });
                    let index = this.items.findIndex((item) => Object.is(item.srfkey, select.srfkey));
                    if (index < 0) {
                        this.items.push({ srfmajortext : select.srfmajortext, srfkey: select.srfkey });
                    }
                });
            }
            if (this.name) {
                let value = selects.length > 0 ? JSON.stringify(selects) : '';
                this.$emit('formitemvaluechange', { name: this.name, value: value });
            }
        }
    }

    /**
     * 清除
     */
    public onClear($event: any): void {
        if (this.valueitem) {
            this.$emit('formitemvaluechange', { name: this.valueitem, value: '' });
        }
        if (this.name) {
            this.$emit('formitemvaluechange', { name: this.name, value: '' });
        }
        this.$forceUpdate();
    }

    
    /**
     * 打开链接视图
     *
417
     * @memberof AppPickerSelectView
418 419 420
     */
    public openLinkView($event: any): void {
        if (!this.data || !this.valueitem || !this.data[this.valueitem]) {
421
            console.error({ title: (this.$t('components.appPickerSelectView.error') as any), desc: (this.$t('components.appPickerSelectView.editor') as any)+this.name+(this.$t('components.appPickerSelectView.valueitemException') as any) });
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
            return;
        }
        // 公共参数处理
        let data: any = {};
        const bcancel: boolean = this.handlePublicParams(data);
        if (!bcancel) {
            return;
        }
        // 参数处理
        let _context = data.context;
        let _param = data.param;
        Object.assign(_context, { [this.deKeyField]: this.data[this.valueitem] });
        const view = JSON.parse(JSON.stringify(this.linkview));
        const viewname2: string = this.$util.srfFilePath2(view.viewname);
        view.viewname = viewname2;

        if (Object.is(view.placement, 'INDEXVIEWTAB') || Object.is(view.placement, '')) {
            this.openIndexViewTab(view, _context, _param);
        } else if (Object.is(view.placement, 'POPOVER')) {
            this.openPopOver($event, view, _context, _param);
        } else if (Object.is(view.placement, 'POPUPMODAL')) {
            this.openPopupModal(view, _context, _param);
        } else if (view.placement.startsWith('DRAWER')) {
            this.openDrawer(view, _context, _param);
        }
    }

    /**
     * 路由模式打开视图
     *
     * @private
     * @param {string} viewpath
     * @param {*} data
455
     * @memberof AppPickerSelectView
456 457 458 459 460 461 462 463 464 465 466 467 468
     */
    private openIndexViewTab(view: any, context: any, param: any): void {
        const routePath = this.$viewTool.buildUpRoutePath(this.$route, this.context, view.deResParameters, view.parameters, [context] , param);
        this.$router.push(routePath);
    }

    /**
     * 气泡卡片模式打开
     *
     * @private
     * @param {*} $event
     * @param {*} view
     * @param {*} data
469
     * @memberof AppPickerSelectView
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
     */
    private openPopOver($event: any, view: any, context: any, param: any): void {
        let container: Subject<any> = this.$apppopover.openPop($event, view, context, param);
        container.subscribe((result: any) => {
            if (!result || !Object.is(result.ret, 'OK')) {
                return;
            }
            this.openViewClose(result);
        });
    }


    /**
     * 模态模式打开视图
     *
     * @private
     * @param {*} view
     * @param {*} data
488
     * @memberof AppPickerSelectView
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
     */
    private openPopupModal(view: any, context: any, param: any): void {
        let container: Subject<any> = this.$appmodal.openModal(view, context, param);
        container.subscribe((result: any) => {
            if (!result || !Object.is(result.ret, 'OK')) {
                return;
            }
            this.openViewClose(result);
        });
    }

    /**
     * 抽屉模式打开视图
     *
     * @private
     * @param {*} view
     * @param {*} data
506
     * @memberof AppPickerSelectView
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
     */
    private openDrawer(view: any, context: any, param: any): void {
        let container: Subject<any> = this.$appdrawer.openDrawer(view, context, param);
        container.subscribe((result: any) => {
            if (!result || !Object.is(result.ret, 'OK')) {
                return;
            }
            this.openViewClose(result);
        });
    }

    /**
     * 打开页面关闭
     *
     * @param {*} result
522
     * @memberof AppPickerSelectView
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
     */
    public openViewClose(result: any) {
        let item: any = {};
        if (result.datas && Array.isArray(result.datas)) {
            Object.assign(item, result.datas[0]);
        }

        if (this.data) {
            if (this.valueitem) {
                this.$emit('formitemvaluechange', { name: this.valueitem, value: item[this.deKeyField]?item[this.deKeyField]:item["srfkey"] });
            }
            if (this.name) {
                this.$emit('formitemvaluechange', { name: this.name, value: item[this.deMajorField]?item[this.deMajorField]:item["srfmajortext"] });
            }
        }
    }
    
    /**
     * 下拉选中回调
     *
     * @param {*} selects 当前选中的key值集合
544
     * @memberof AppPickerSelectView
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
     */
    public onSelectChange(selects: any) {
        let val: Array<any> = [];
        if (selects.length > 0) {
            selects.forEach((select: any) => {
                let index = this.items.findIndex((item) => Object.is(item[this.deKeyField], select));
                if (index >= 0) {
                    val.push(this.items[index]);
                }
            });
        }
        let value = val.length > 0 ? JSON.stringify(val) : '';
        this.$emit('formitemvaluechange', { name: this.name, value: value });
    }

}
</script>

<style lang='less'>
@import './app-picker-select-view.less';
</style>