fuchengshen
2022-07-09 3a25c05b5ab837714f30469cdcb1e2a77d8d7f6c
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
<template>
  <mars-pannel :visible="true" right="10" top="10" height="46">
    <layer-state />
  </mars-pannel>
 
  <mars-pannel :visible="true" right="10" top="60" width="250">
    <mars-tree checkable :tree-data="treeData" @check="checkedChange" v-model:checkedKeys="checkedKeys" v-model:expandedKeys="expandedKeys">
      <template #title="{ title }">
        <span class="tree-style" :title="title">{{ title }}</span>
      </template>
    </mars-tree>
  </mars-pannel>
</template>
<script lang="ts" setup>
import LayerState from "@mars/components/mars-sample/layer-state.vue"
import { nextTick, ref } from "vue"
import * as mapWork from "./map.js"
 
const treeData = ref<any[]>([
  {
    title: "全部",
    key: 0,
    id: -1,
    children: []
  }
])
 
const checkedKeys = ref<string[]>([])
const expandedKeys = ref<any[]>([])
expandedKeys.value.push(0)
let layersObj: any = {}
 
mapWork.treeEvent.on("tree", (event: any) => {
  initTree(event.data)
})
 
const checkedChange = (_keys: string[], checkedNodes: any) => {
  const show = checkedNodes.checked
  const entity = layersObj[checkedNodes.node.uuid]
 
  if (checkedNodes.node.id === -1) {
    Object.keys(layersObj).forEach((k) => {
      const layer = layersObj[k]
      layer.graphic.show = show
    })
    return
  }
 
  if (!show) {
    entity.graphic.show = show
  } else {
    entity.graphic.show = show
    entity.graphic.flyTo()
  }
}
 
function initTree(dataItems: any) {
  // 重置上一次的树状数据
  treeData.value[0].children = []
  layersObj = {}
 
  const children: any[] = []
  const dataKeys: any = []
 
  // 遍历出所有的树状数据
  for (let i = 0; i < dataItems.length; i++) {
    const layer = dataItems[i]
    if (layer) {
      const key = "01-" + Math.random()
      children.push({
        title: layer.airportName,
        key: key,
        uuid: layer.graphic.uuid
      })
 
      if (layer.graphic.show) {
        dataKeys.push(key)
      }
      layersObj[layer.graphic.uuid] = layer
    }
  }
  treeData.value[0].children = children
 
  nextTick(() => {
    checkedKeys.value = dataKeys
  })
}
</script>