contextTree.vue 4.12 KB
<template>
  <div class="context-tree-cmp">
    <el-form :model="form">
      <el-form-item label="上下文类型">
        <el-select v-model="form.app" placeholder="选择上下文类型进行查询" @change="onAppChange">
          <el-option
            v-for="item in appList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div class="root">
      <span>Root</span>
      <span class="add-root" @click="addRootNode" />
    </div>
    <div class="tree-wrap">
      <DeeDefaultTree
        :key="cmpKey"
        ref="defTree"
        type-name="DxContext"
        :base="treeBase"
        :tree-obj="treeTools"
        @handerTreeFun="onTreeClick"
        @nodeClick="onNodeClick"
      />
    </div>
  </div>
</template>

<script>
// import _get from 'lodash.get'
export default {
  name: 'ContextTree',
  props: {
    appList: {
      type: Array,
      default: () => []
    },
    stateOption: {
      type: Array,
      default: () => []
    }
  },
  data() {
    const that = this
    return {
      form: {
        app: 1
      },
      cmpKey: 0,
      treeBase: {
        isPaging: true,
        isShowSerach: false,
        isAutoLoadData: true,
        pageSize: 50,
        searchItemsParams: null,
        url: '/DxContext/getAllContext',
        // 查询根节点的时候默认给的 parentId
        defaultParentId: 0,
        labelWord: (data, node) => {
          const names = []
          const subType = (that.appList || []).find(m => m.value === data.subTypeName)
          data.name && names.push(data.name)
          subType && names.push(subType.label)
          if (that.stateOption.length > 0 || data.state) {
            const item = that.stateOption.find(m => m.value === data.state)
            item && names.push(item.label)
          }
          return names.join(' | ')
        }
      },
      treeTools: {
        nodeTools: [
          {
            icon: '/icons/add-L.png',
            name: '添加',
            btnValue: 'addNode',
            obscure: '!node.data.operationFlag'
            // keyName: 'addNode',
            // key: 'addNode'
          },
          {
            icon: '/icons/edit-L.png',
            name: '编辑',
            btnValue: 'editNode',
            obscure: '!node.data.operationFlag'
          },
          {
            icon: '/icons/delete-L.png',
            name: '删除',
            btnValue: 'delNode',
            obscure: '!node.data.operationFlag'
          }
        ]
      }
    }
  },
  methods: {
    updateTree(data, type) {
      if (type && type === 'MODIFY') {
        this.$refs.defTree.updateTreeNode(data)
      } else {
        this.cmpKey = ~~(!this.cmpKey)
      }
    },
    onAppChange(val) {
      let params = null
      if (val !== 1) {
        params = [{ fieldName: 'subTypeName', operator: 'EQ', value: val }]
      }
      this.treeBase = {
        ...this.treeBase,
        searchItemsParams: params ? JSON.stringify(params) : ''
      }
    },
    onTreeClick(item, data, node) {
      this[item.btnValue](data, node)
    },
    onNodeClick(data) {
      this.$emit('getList', data)
    },
    addRootNode() {
      this.$emit('handleEditGroup', false, null)
    },
    addNode(data) {
      this.$emit('handleEditGroup', false, data)
    },
    editNode(data) {
      this.$emit('handleEditGroup', true, data)
    },
    delNode(data) {
      this.$emit('handleDelGroup', data)
    }
  }
}
</script>

<style lang="scss">
.context-tree-cmp {
  height: 100%;
  .el-form-item {
    margin-bottom: 0;
  }
  .el-select {
    width:calc(100% - 82px)
  }
  .tree-wrap {
    height: calc(100% - 75px);
    overflow-y: auto;
  }
  .root{
    display: flex;
    justify-content: space-between;
    padding:0 8px;
    height: 40px;
    line-height: 40px;
    // &:hover .add-root{
    //   visibility: visible;
    // }
    .add-root{
      display: inline-block;
      width:14px;
      height:14px;
      margin: auto 4px;
      background-size: cover;
      background-position: center;
      background-image: url('/icons/add-L.png');
      // visibility: hidden;
    }
  }
}
</style>