DeSelectTree.vue 13.4 KB
Newer Older
Tippi.Rao 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 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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 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
<template>

  <el-tree-select
    v-if="element.options!== null && element.options.attrs!==null && show"
    ref="deSelectTree"
    v-model="value"
    popover-class="test-class-wrap"
    :data="data"
    :select-params="selectParams"
    :tree-params="treeParams"
    :filter-node-method="_filterFun"
    :tree-render-fun="_renderFun"
    :custom-style="customStyle"
    :popper-append-to-body="inScreen"
    @searchFun="_searchFun"
    @node-click="changeNode"
    @removeTag="changeNodeIds"
    @check="changeCheckNode"
    @select-clear="selectClear"
    @onFocus="onFocus"
    @treeCheckChange="handleElTagStyle"
  />

</template>

<script>
import { linkMappingFieldValues, mappingFieldValues } from '@/api/dataset/dataset'
import bus from '@/utils/bus'
import { isSameVueObj } from '@/utils'
import { getLinkToken, getToken } from '@/utils/auth'
import ElTreeSelect from '@/components/elTreeSelect'
import customInput from '@/components/widget/deWidget/customInput'
import { textSelectWidget } from '@/components/widget/deWidget/serviceNameFn.js'

export default {
  components: { ElTreeSelect },
  mixins: [customInput],
  props: {
    canvasId: {
      type: String,
      required: true
    },
    element: {
      type: Object,
      default: () => {
      }
    },
    inDraw: {
      type: Boolean,
      default: true
    },
    inScreen: {
      type: Boolean,
      required: false,
      default: true
    },
    size: String,
    isRelation: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      show: true,
      selectOptionWidth: 0,
      data: [],
      // eslint-disable-next-line
      value: this.isSingle ? '' : [],
      selectParams: {
        clearable: true,
        placeholder: this.$t(this.element.options.attrs.placeholder)
      },
      treeParams: {
        showParent: true,
        clickParent: true,
        filterable: true,
        // 只想要子节点,不需要父节点
        leafOnly: false,
        includeHalfChecked: false,
        'check-strictly': false,
        'default-expand-all': false,
        'expand-on-click-node': false,
        'render-content': this._renderFun,
        data: [],
        props: {
          children: 'children',
          label: 'text',
          rootId: 'root',
          disabled: 'disabled',
          parentId: 'pid',
          value: 'id'
        }
      }
    }
  },
  computed: {
    operator() {
      return this.element.options.attrs.multiple ? 'in' : 'eq'
    },
    defaultValueStr() {
      if (!this.element || !this.element.options || !this.element.options.value) return ''
      return this.element.options.value.toString()
    },
    viewIds() {
      if (!this.element || !this.element.options || !this.element.options.attrs.viewIds) return ''
      return this.element.options.attrs.viewIds.toString()
    },
    manualModify() {
      return !!this.element.options.manualModify
    },
    panelInfo() {
      return this.$store.state.panel.panelInfo
    },
    isSingle() {
      return this.element.options.attrs.multiple
    },
    customStyle() {
      const { brColor, wordColor, innerBgColor } = this.element.style
      return { brColor, wordColor, innerBgColor }
    }
  },

  watch: {
    'viewIds': function(value, old) {
      if (typeof value === 'undefined' || value === old) return
      this.setCondition()
    },
    'defaultValueStr': function(value, old) {
      if (value === old) return
      this.value = this.fillValueDerfault()
      this.changeValue(value)
    },
    'element.options.attrs.fieldId': function(value, old) {
      if (value === null || typeof value === 'undefined' || value === old) return
      this.data = []

      let method = mappingFieldValues
      const token = this.$store.getters.token || getToken()
      const linkToken = this.$store.getters.linkToken || getLinkToken()
      if (!token && linkToken) {
        method = linkMappingFieldValues
      }
      const param = { fieldIds: this.element.options.attrs.fieldId.split(','), sort: this.element.options.attrs.sort }
      if (this.panelInfo.proxy) {
        param.userId = this.panelInfo.proxy
      }
      this.element.options.attrs.fieldId &&
      this.element.options.attrs.fieldId.length > 0 &&
      method(param).then(res => {
        this.data = this.optionData(res.data)
        this.$nextTick(() => {
          this.$refs.deSelectTree && this.$refs.deSelectTree.treeDataUpdateFun(this.data)
        })
      })
      this.element.options.value = ''
    },
    'element.options.attrs.multiple': function(value, old) {
      if (typeof old === 'undefined' || value === old) return

      if (!this.inDraw) {
        this.value = value ? [] : null
        this.element.options.value = ''
      }
      this.show = false
      this.$nextTick(() => {
        // this.value = value ? [] : null

        this.show = true
        this.$nextTick(() => {
          const defaultV = this.element.options.value === null ? '' : this.element.options.value.toString()
          if (value) {
            if (defaultV === null || typeof defaultV === 'undefined' || defaultV === '' || defaultV === '[object Object]') {
              this.value = []
            } else {
              this.value = defaultV.split(',')
            }
          } else {
            if (defaultV === null || typeof defaultV === 'undefined' || defaultV === '' || defaultV === '[object Object]') {
              this.value = ''
            } else {
              this.value = defaultV.split(',')[0]
            }
          }
          this.$refs.deSelectTree && this.$refs.deSelectTree.treeDataUpdateFun(this.data)
        })
      })
    },
    'element.options.attrs.sort': function(value, old) {
      if (value === null || typeof value === 'undefined' || value === old || isSameVueObj(value, old)) return
      this.data = []

      let method = mappingFieldValues
      const token = this.$store.getters.token || getToken()
      const linkToken = this.$store.getters.linkToken || getLinkToken()
      if (!token && linkToken) {
        method = linkMappingFieldValues
      }
      const param = { fieldIds: this.element.options.attrs.fieldId.split(','), sort: this.element.options.attrs.sort }
      if (this.panelInfo.proxy) {
        param.userId = this.panelInfo.proxy
      }
      this.element.options.attrs.fieldId &&
      this.element.options.attrs.fieldId.length > 0 &&
      method(param).then(res => {
        this.data = this.optionData(res.data)
        this.$nextTick(() => {
          this.$refs.deSelectTree && this.$refs.deSelectTree.treeDataUpdateFun(this.data)
        })
      })
      this.element.options.value = ''
    }

  },
  created() {
    if (!this.element.options.attrs.sort) {
      this.element.options.attrs.sort = {}
    }
    this.initLoad()
  },
  mounted() {
    if (this.inDraw) {
      bus.$on('reset-default-value', this.resetDefaultValue)
    }
  },
  beforeDestroy() {
    bus.$off('reset-default-value', this.resetDefaultValue)
  },

  methods: {
    clearHandler() {
      this.value = this.element.options.attrs.multiple ? [] : null
      this.$refs.deSelectTree && this.$refs.deSelectTree.resetSelectAll && this.$refs.deSelectTree.resetSelectAll()
    },
    resetDefaultValue(id) {
      if (this.inDraw && this.manualModify && this.element.id === id) {
        this.value = this.fillValueDerfault()
        this.changeValue(this.value)
      }
    },
    onFocus() {
      this.$nextTick(() => {
        this.handleCoustomStyle()
      })
    },
    handleElTagStyle() {
      setTimeout(() => {
        textSelectWidget(this.$refs.deSelectTree.$refs.select.$el, this.element.style)
      }, 50)
    },
    selectClear() {
      this.changeValue(this.value)
    },
    changeNode(data, node) {
      this.changeValue(this.value)
    },
    changeCheckNode(data, obj) {
      const { checkedKeys } = obj
      if (checkedKeys) this.value = checkedKeys
      this.changeValue(this.value)
    },
    changeNodeIds(ids) {
      this.value = ids
      this.changeValue(this.value)
    },
    initLoad() {
      this.value = this.fillValueDerfault()
      this.data = []
      this.initOptions()
      if (this.element.options.value) {
        this.value = this.fillValueDerfault()
        this.changeValue(this.value)
      }
    },
    refreshLoad() {
      this.initOptions()
    },
    initOptions() {
      this.data = []
      if (this.element.options.attrs.fieldId) {
        let method = mappingFieldValues
        const token = this.$store.getters.token || getToken()
        const linkToken = this.$store.getters.linkToken || getLinkToken()
        if (!token && linkToken) {
          method = linkMappingFieldValues
        }
        method({
          fieldIds: this.element.options.attrs.fieldId.split(','),
          sort: this.element.options.attrs.sort
        }).then(res => {
          this.data = this.optionData(res.data)
          this.$nextTick(() => {
            this.$refs.deSelectTree && this.$refs.deSelectTree.treeDataUpdateFun(this.data)
          })
        })
      }
    },
    changeValue(value) {
      if (!this.inDraw) {
        if (value === null) {
          this.element.options.value = ''
        } else {
          this.element.options.value = Array.isArray(value) ? value.join() : value
        }
        this.element.options.manualModify = false
      } else {
        this.element.options.manualModify = true
      }
      this.setCondition()
    },

    getCondition() {
      const val = this.formatFilterValue()

      const param = {
        canvasId: this.canvasId,
        component: this.element,
        value: val,
        operator: this.operator,
        isTree: true
      }
      return param
    },

    setCondition() {
      const param = this.getCondition()
      !this.isRelation && this.inDraw && this.$store.commit('addViewFilter', param)
    },
    formatFilterValue() {
      const SEPARATOR = '-de-'
      if (this.value === null) return []
      if (Array.isArray(this.value)) {
        const results = []
        const duplicateMap = {}
        this.value.forEach(item => {
          const links = item.split(SEPARATOR)
          let temp = ''
          for (let index = 0; index < links.length; index++) {
            const isLast = index === (links.length - 1)
            const isFirst = index === 0
            const node = links[index]

            temp += ((isFirst ? '' : SEPARATOR) + node)
            if (duplicateMap[temp] && !isLast) {
              delete duplicateMap[temp]
            }
          }

          duplicateMap[item] = true
        })
        for (const key in duplicateMap) {
          if (Object.hasOwnProperty.call(duplicateMap, key) && duplicateMap[key]) {
            const node = key.replaceAll(SEPARATOR, ',')
            results.push(node)
          }
        }
        return results
        // return this.value
      }
      const result = this.value.split(',').map(v => v.replaceAll(SEPARATOR, ','))
      return result
    },

    fillValueDerfault() {
      const defaultV = this.element.options.value === null ? '' : this.element.options.value.toString()
      if (this.element.options.attrs.multiple) {
        if (defaultV === null || typeof defaultV === 'undefined' || defaultV === '' || defaultV === '[object Object]') return []
        return defaultV.split(',')
      } else {
        if (defaultV === null || typeof defaultV === 'undefined' || defaultV === '' || defaultV === '[object Object]') return null
        return defaultV.split(',')[0]
      }
    },
    optionData(data) {
      if (!data) return null

      return data.filter(item => !!item)
    },

    /* 下面是树的渲染方法 */

    _filterFun(value, data, node) {
      if (!value) return true
Tippi.Rao committed
384
      return data.id.toString().toLocaleUpperCase().indexOf(value.toString().toLocaleUpperCase()) !== -1
Tippi.Rao committed
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 417 418 419 420 421 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 455
    },
    // 树点击
    _nodeClickFun(data, node, vm) {
    },
    // 树过滤
    _searchFun(value) {
      // 自行判断 是走后台查询,还是前端过滤
      this.$refs.deSelectTree.filterFun(value)
      // 后台查询
      // this.$refs.deSelectTree.treeDataUpdateFun(treeData);
    },
    // 自定义render
    _renderFun(h, { node, data, store }) {
      const { props, clickParent } = this.treeParams
      return (
        <span
          class={['custom-tree-node', !clickParent && data[props.children] && data[props.children].length ? 'disabled' : null]}
        >
          <span>{node.label}</span>
        </span>
      )
    }

  }

}
</script>

<style lang="scss">
.test-class-wrap {
  background: var(--BgSelectTreeColor, #FFFFFF) !important;
  border-color: var(--BrSelectTreeColor, #E4E7ED) !important;

  .popper__arrow,
  .popper__arrow::after {
    display: none !important;
  }

  .el-tree {
    background: var(--BgSelectTreeColor, #FFFFFF) !important;
    color: var(--SelectTreeColor, #606266) !important;

    .el-tree-node.is-current {
      background-color: rgb(245, 247, 250, .5) !important;
    }

    .el-tree-node:focus > .el-tree-node__content {
      background-color: rgb(245, 247, 250, .5) !important;
    }

    .el-tree-node__content:hover {
      background-color: rgb(245, 247, 250, .5) !important;
    }
  }

  .el-input-group--append {
    .el-input__inner {
      background: var(--BgSelectTreeColor, #FFFFFF) !important;
      color: var(--SelectTreeColor, #606266) !important;
      border: 1px solid var(--BrSelectTreeColor, #606266) !important;
    }

    .el-input-group__append {
      background: var(--BgSelectTreeColor, #FFFFFF) !important;
      color: var(--SelectTreeColor, #606266) !important;
      border: 1px solid var(--BrSelectTreeColor, #606266) !important;
      border-left: none;
    }
  }
}
</style>