base.js 6.61 KB
Newer Older
wangdanlei's avatar
wangdanlei 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
export default {
  data() {
    return {

    }
  },
  computed: {

  },
  watch: {

  },
  methods: {
    changeCheck(data, node) {
      const isChecked = data.customCheck
      const findIndex = this.modelArr.findIndex(d => d.id === data.targetId)
      findIndex > -1 && this.$set(this.modelArr[findIndex], 'checked', isChecked)
      if (isChecked) {
        this.$set(data, 'indeterminate', false)
        // 判断是否有下级节点,有的话,遍历下级,都选中
        if (!data.showSwitch && data.children && data.children.length) {
          this.setChildrenCheck(data.children, true)
        }
      } else {
        this.$set(data, 'checked', isChecked)
        if (!data.showSwitch && data.children && data.children.length) {
          this.setChildrenCheck(data.children, false)
        }
        if (data.showSwitch && findIndex > -1) {
          this.remove(data)
        }
      }
      // 判断是否有父节点
      if (node.parent) {
        this.setParentCheck(node.parent, isChecked)
      }
      this.loadCheckedModel(isChecked)
    },
    // 设置子节点的选中状态
    setChildrenCheck(arr, isChecked) {
      arr.forEach((item, index) => {
        this.$set(arr[index], 'customCheck', isChecked)
        const findIndex = this.modelArr.findIndex(d => d.id === item.targetId)
        findIndex > -1 && this.$set(this.modelArr[findIndex], 'checked', isChecked)
        item.children && this.setChildrenCheck(item.children, isChecked)

        if (!isChecked && item.showSwitch && findIndex > -1) {
          this.$set(item, 'checked', isChecked)
          this.remove(item)
        }
      })
    },
    // 设置父节点的选中状态
    // true: 子级有一个选中,则父级选中,false:父节点下的子节点是都未选中的状态,则父级取消勾选
    setParentCheck(parentNode, isChecked) {
      if (parentNode.childNodes && parentNode.childNodes.length) {
        const count = parentNode.childNodes.filter(r => r.data.customCheck || r.data.indeterminate).length
        if (count === 0) {
          this.$set(parentNode.data, 'customCheck', false)
          this.$set(parentNode.data, 'indeterminate', false)
        } else if (count === parentNode.childNodes.length) {
          // 全选
          this.$set(parentNode.data, 'customCheck', true)
          this.$set(parentNode.data, 'indeterminate', false)
        } else {
          // 半选
          this.$set(parentNode.data, 'indeterminate', true)
        }
      }
      if (parentNode.parent) {
        this.setParentCheck(parentNode.parent, isChecked)
      }
    },
    // 高亮匹配的文字
    highLightTreeNode() {
      this.$nextTick(() => {
        const treeNode = this.$refs.modelTree.$el
        const spans = treeNode.getElementsByClassName('change-text')

        if (spans.length > 0) {
          for (let i = 0; i < spans.length; i++) {
            var labelText = spans[i].textContent
            const reg = this.inputVal
              .replace(/\(/g, '\\(')
              .replace(/\)/g, '\\)')
              .replace(/\./g, '\\.')
            const allVal = labelText.match(new RegExp(reg, 'ig'))
            if (allVal) {
              const newAllVal = [...new Set(allVal)]
              for (let j = 0; j < newAllVal.length; j++) {
                if (newAllVal[j]) {
                  const tp = newAllVal[j]
                    .replace(/\(/g, '\\(')
                    .replace(/\)/g, '\\)')
                    .replace(/\./g, '\\.')
                  labelText = labelText.replace(
                    new RegExp(tp, 'g'),
                    '*' + newAllVal[j] + '*'
                  )
                }
              }
              for (let k = 0; k < allVal.length; k++) {
                if (allVal[k]) {
                  labelText = labelText.replace(
                    '*' + allVal[k] + '*',
                    '<span class="highlight-text">' + allVal[k] + '</span>'
                  )
                }
              }
            }
            spans[i].innerHTML = labelText
          }
        }
      })
    },
    // 展开匹配的节点
    expandToMatchNode() {
      const node = this.filterTool.nodes[this.filterTool.currentIndex]
      let currentNode = node.parent
      while (currentNode) {
        currentNode.expanded = true
        currentNode = currentNode.parent
      }
      this.$refs.modelTree.setCurrentKey(node.key)
      this.highLightTreeNode()

      // 保持滚动条内容可见
      this.$nextTick(() => {
        const treeEl = document.getElementById('tree')
        const el = this.$refs.modelTree.$el.getElementsByClassName('is-current')[0]
        let parentNode = el.parentNode
        let offsetParent = el.offsetParent
        let offsetTop = el.offsetTop || 0

        while (parentNode && parentNode !== treeEl) {
          if (offsetParent !== parentNode.offsetParent) {
            offsetTop += parentNode.offsetTop || 0
          } else {
            offsetParent = parentNode.offsetParent
          }
          parentNode = parentNode.parentNode
        }

        treeEl.scrollTop = offsetTop - treeEl.clientHeight * 0.5 + 10
      })
    },
    // 查询下一个
    findNext() {
      if (!this.inputVal) {
        return
      }
      this.findAll().then(() => {
        if (this.filterTool.nodes.length < 1) {
          return
        }
        if (++this.filterTool.currentIndex >= this.filterTool.nodes.length) {
          this.filterTool.currentIndex = 0
        }
        this.expandToMatchNode()
      })
    },
    // 查询上一个
    findPrev() {
      if (!this.inputVal) {
        return
      }
      this.findAll().then(() => {
        if (this.filterTool.nodes.length < 1) {
          return
        }
        if (--this.filterTool.currentIndex < 0) {
          this.filterTool.currentIndex = this.filterTool.nodes.length - 1
        }
        this.expandToMatchNode()
      })
    },
    // 查找所有满足条件的节点,先从服务端装载所有BOM结构
    findAll() {
      return new Promise((resolve) => {
        this.findMatchNodes()
        resolve()
      })
    },
    // 查找节点
    findMatchNodes() {
      let nodeMap = this.$refs.modelTree.store.nodesMap
      nodeMap = Object.values(nodeMap)
      this.filterTool.nodes = nodeMap.filter(node => node.label.toUpperCase().includes(this.inputVal.toUpperCase()))
    },
    expandAllFun(bool) {
      var nodes = this.$refs.modelTree.store._getAllNodes()
      this.expandAll = bool
      this.setNodeExpanded(nodes, bool)
      this.highLightTreeNode()
    },
    setNodeExpanded(nodes, bool) {
      nodes.forEach(Node => {
        if (bool) {
          Node.expand()
        } else {
          Node.collapse()
        }
      })
    }
  }
}