app-tree-picker.vue 7.9 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
<template>
    <div v-if="refviewname" class="app-tree-picker">
        <Dropdown :visible="visible" trigger="custom" style="left:0px;width: 100%" @on-clickoutside="() => {triggerMenu(false);}" >
          <Input v-model="inputValue" class="tree-input" type="text" :placeholder="placeholder ? placeholder : $t('components.appTreePicker.placeholder')" :disabled="disabled" @on-change="OnInputChange" @on-focus="()=>{triggerMenu();}" >
              <template v-slot:suffix>
                  <i v-if="inputValue && !disabled" class='el-icon-circle-close' @click="onClear"></i>
                  <Icon :type="visible ? 'ios-arrow-up' : 'ios-arrow-down'" class="icon-arrow" @click="() => {triggerMenu();}"></Icon>
              </template>
          </Input>
          <DropdownMenu slot="list">
              <component
                v-if="visible"
                :is="refviewname"
                :viewdata="viewdata"
                :viewparam="viewparam"
                :isShowButton="false"
                :viewDefaultUsage="false"
                @viewdataschange="setValue"
                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';

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

    /**
     * 视图上下文
     *
     * @type {*}
39
     * @memberof AppTreePicker
40 41 42 43 44 45 46
     */
    @Prop() public context!: any;

    /**
     * 视图参数
     *
     * @type {*}
47
     * @memberof AppTreePicker
48 49 50 51 52 53 54
     */
    @Prop() public viewparams!: any;

    /**
     * 表单数据
     *
     * @type {*}
55
     * @memberof AppTreePicker
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
     */
    @Prop() public data!: any;

    /**
     * 值
     *
     * @type {*}
     * @memberof AppTreePicker
     */
    @Prop() public value: any;

    /**
     * 是否启用
     *
     * @type {boolean}
71
     * @memberof AppTreePicker
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
     */
    @Prop({default: false}) public disabled!: boolean;
    
    /**
     * 表单状态
     *
     * @type {Subject<any>}
     * @memberof AppTreePicker
     */
    @Prop() public formState!: Subject<any>

    /**
     * 输入框值
     *
     * @type {string}
     * @memberof AppTreePicker
     */
    public inputValue: any = '';

    /**
     * 视图状态事件
     *
     * @protected
     * @type {(Subscription | undefined)}
     * @memberof SelectType
     */
    protected formStateEvent: Subscription | undefined;

    /**
     * 值项名称
     *
     * @type {string}
104
     * @memberof AppTreePicker
105 106 107 108 109 110 111
     */
    @Prop() public valueItem?: string;

    /**
     * 关联视图名称
     *
     * @type {string}
112
     * @memberof AppTreePicker
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
     */
    @Prop() public refviewname?: string;

    /**
     * 提示信息
     *
     * @type {string}
     * @memberof AppTreePicker
     */
    @Prop() public placeholder?: string;

    /**
     * 属性项名称
     *
     * @type {string}
128
     * @memberof AppTreePicker
129 130 131 132
     */
    @Prop() public name!: string;

    /**
133 134 135
     * 局部上下文导航参数
     * 
     * @type {any}
136 137
     * @memberof AppTreePicker
     */
138 139 140 141 142 143 144 145 146
    @Prop() public localContext!:any;

    /**
     * 局部导航参数
     * 
     * @type {any}
     * @memberof AppTreePicker
     */
    @Prop() public localParam!:any;
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

    /**
     * 是否忽略之变化
     *
     * @type {boolean}
     * @memberof AppTreePicker
     */
    @Prop() public ignorefieldvaluechange!: boolean;

    /**
     * 重置项
     *
     * @type {string}
     * @memberof AppTreePicker
     */
    @Prop() public refreshitems?: string;

    /**
     * 下拉显示控制变量
     *
     * @type {string}
     * @memberof AppTreePicker
     */
    public visible: boolean = false;

    /**
     * 视图参数
     *
     * @type {string}
     * @memberof AppTreePicker
     */
    public viewparam: any = JSON.stringify(this.viewparams);;
    
    /**
     * 视图上下文
     *
     * @type {string}
     * @memberof AppTreePicker
     */
    public viewdata: any = JSON.stringify(this.context);

    /**
     * 输入框change事件
     * 
     * @param $event 事件对象
     * @memberof AppTreePicker
     */
    public OnInputChange($event: any){
        let _viewdata =  Object.assign({ srfnodefilter: this.inputValue }, JSON.parse(this.viewdata)) ;
        this.viewdata = JSON.stringify(_viewdata);
    }

    /**
     * 输入框change事件
     * 
     * @param $event 事件对象
     * @memberof AppTreePicker
     */
    public triggerMenu(visible?: boolean){
        if(this.disabled){
          return;
        }
        if (!visible) {
            this.visible = !this.visible;
        } else {
            this.visible = visible;
        }
    }

    /**
     * 设置视图参数
     *
     * @memberof AppTreePicker
     */
221 222
    public setViewParam() {
        if (!this.data) {
223 224 225 226 227 228 229
            return;
        }
        let arg: any = {};
        // 合并视图上下文参数和视图参数
        let param: any = JSON.parse(JSON.stringify(this.viewparams));
        let context: any = JSON.parse(JSON.stringify(this.context));
        // 附加参数处理
230 231 232
        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);
233
        }
234 235 236
        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);
237 238 239 240 241 242 243 244 245 246
        }
        this.viewdata = JSON.stringify(context);
        this.viewparam = JSON.stringify(param);
    }

    /**
     * 监控值
     *
     * @param {*} newVal
     * @param {*} oldVal
247
     * @memberof AppTreePicker
248 249 250 251 252
     */
    @Watch('data')
    onActivedataChange(newVal: any, oldVal: any) {
        const newFormData: any = JSON.parse(newVal);
        const oldDormData: any = JSON.parse(oldVal);
253
        this.setViewParam();
254 255 256 257 258 259 260 261 262 263 264 265 266 267
        if (!this.refreshitems || this.ignorefieldvaluechange) {
            return;
        }
        if(Object.is(newFormData[this.refreshitems], oldDormData[this.refreshitems])) {
            return;
        }
        this.setValue([{srfmajortext: null, srfkey: null}]);
    }

    /**
     * 值变化
     *
     * @param {*} newVal
     * @param {*} oldVal
268
     * @memberof AppTreePicker
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
     */
    @Watch('value')
    public onValueChange(newVal: any, oldVal: any) {
        this.inputValue = newVal;
    }

    /**
     * 生命周期
     *
     * @memberof AppTreePicker
     */
    public created() {
        if(this.formState) {
            this.formStateEvent = this.formState.subscribe(({ tag, action, data }) => {
                if (Object.is('load', action)) {
284
                    this.setViewParam();
285 286 287 288 289 290 291 292
                }
            });
        }
    }

    /**
     * vue 生命周期
     *
293
     * @memberof AppTreePicker
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 332 333 334 335
     */
    public destroyed() {
        if (this.formStateEvent) {
            this.formStateEvent.unsubscribe();
        }
    }

    /**
     * 设置值
     *
     * @param {*} item
     * @memberof AppTreePicker
     */
    public setValue(item: any) {
        this.visible = false;
        if (this.valueItem) {
            this.$emit('formitemvaluechange', { name: this.valueItem, value: item[0].srfkey });
        }
        if (this.name) {
            this.$emit('formitemvaluechange', { name: this.name, value: item[0].srfmajortext });
        }
    }

    /**
     * 清除
     */
    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();
    }

}
</script>

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