|
1 | 1 | <template>
|
2 | 2 | <div
|
3 | 3 | class="vjs__tree"
|
4 |
| - :style="{ 'background-color': treeContentBackground }" |
5 |
| - @click.stop="handleClick" |
6 |
| - @mouseover.stop="treeContentBackground = '#eee'" |
7 |
| - @mouseout.stop="treeContentBackground = 'transparent'"> |
| 4 | + :style="{ |
| 5 | + 'background-color': treeContentBackground, |
| 6 | + 'position': child ? '' : 'relative', |
| 7 | + 'margin-left': !child && existCheckbox ? '30px' : '' |
| 8 | + }" |
| 9 | + @click.stop="handleClick($event)" |
| 10 | + @mouseover.stop="handleMouseover" |
| 11 | + @mouseout.stop="handleMouseout"> |
| 12 | + |
| 13 | + <template v-if="selectable && existCheckbox" class="vjs-checkbox"> |
| 14 | + <checkbox v-model="checkboxVal" @change="handleClick($event, true)"></checkbox> |
| 15 | + </template> |
| 16 | + |
8 | 17 | <template v-if="Array.isArray(data) || isObject(data)">
|
9 | 18 | <!-- 左闭合 -->
|
10 | 19 | <brackets-left
|
|
25 | 34 | :parent-data="data"
|
26 | 35 | :data="item"
|
27 | 36 | :path="path + (Array.isArray(data) ? `[${index}]` : `.${index}`)"
|
| 37 | + :path-checked="pathChecked" |
| 38 | + :path-selectable="pathSelectable" |
| 39 | + :selectable-type="selectableType" |
28 | 40 | :index="index"
|
29 | 41 | :child="true"
|
30 | 42 | @click="handleItemClick">
|
|
51 | 63 | </template>
|
52 | 64 |
|
53 | 65 | <script>
|
| 66 | + import Checkbox from './checkbox' |
54 | 67 | import BracketsLeft from './brackets-left'
|
55 | 68 | import BracketsRight from './brackets-right'
|
56 | 69 |
|
57 | 70 | export default {
|
58 | 71 | name: 'tree',
|
59 | 72 | components: {
|
| 73 | + Checkbox, |
60 | 74 | BracketsLeft,
|
61 | 75 | BracketsRight
|
62 | 76 | },
|
|
68 | 82 | type: String,
|
69 | 83 | default: 'root'
|
70 | 84 | },
|
| 85 | + // 定义已选中的数据层级 |
| 86 | + pathChecked: { |
| 87 | + type: Array, |
| 88 | + default: () => [] |
| 89 | + }, |
| 90 | + // 定义某个数据层级是否支持选中操作 |
| 91 | + pathSelectable: { |
| 92 | + type: Function, |
| 93 | + default: () => true |
| 94 | + }, |
| 95 | + // 定义数据层级支持的选中方式, 默认所有方式均支持 |
| 96 | + selectableType: { |
| 97 | + type: String, |
| 98 | + default: 'both' // both, checkbox, tree |
| 99 | + }, |
71 | 100 | /* 外部可用 END */
|
72 | 101 | parentData: {}, // 当前树的父级数据
|
73 |
| - child: Boolean, // 是否子树 |
| 102 | + child: Boolean, // 是否子树(优化: 通过 $parent?) |
74 | 103 | index: {}
|
75 | 104 | },
|
76 | 105 | data () {
|
77 | 106 | return {
|
78 | 107 | visiable: true,
|
79 |
| - treeContentBackground: 'transparent' |
| 108 | + treeContentBackground: 'transparent', |
| 109 | + checkboxVal: this.pathChecked.includes(this.path) // 复选框的值 |
80 | 110 | }
|
81 | 111 | },
|
82 | 112 | computed: {
|
|
88 | 118 | let arr = Object.keys(this.parentData)
|
89 | 119 | return arr[arr.length - 1]
|
90 | 120 | }
|
| 121 | + }, |
| 122 | + // 当前的树是否支持选中功能 |
| 123 | + selectable () { |
| 124 | + return this.pathSelectable(this.path, this.data) |
| 125 | + }, |
| 126 | + // 存在复选框 |
| 127 | + existCheckbox () { |
| 128 | + return this.selectableType === 'both' || this.selectableType === 'checkbox' |
| 129 | + }, |
| 130 | + // 存在mouseover |
| 131 | + existMouseover () { |
| 132 | + return this.selectableType === 'both' || this.selectableType === 'tree' |
91 | 133 | }
|
92 | 134 | },
|
93 | 135 | methods: {
|
94 |
| - // 触发组件的 click 事件 |
95 |
| - handleClick () { |
96 |
| - this.$emit('click', this.path, this.data) |
| 136 | + /** |
| 137 | + * 触发组件的 click 事件 |
| 138 | + * @param {Boolean} changed 复选框值是否已改变(如果来自复选框 change 事件则已改变) |
| 139 | + */ |
| 140 | + handleClick (e, changed = false) { |
| 141 | + // 由于 checkbox 也依赖该函数, 因此通过 changed 进行排除 |
| 142 | + if (!changed && !this.existMouseover || !this.selectable) return |
| 143 | + changed || (this.checkboxVal = !this.checkboxVal) |
| 144 | + this.$emit('click', this.path, this.data, this.checkboxVal) |
97 | 145 | },
|
98 | 146 | // 处理子树触发的 click 事件, 并传递到顶层
|
99 |
| - handleItemClick (path, data) { |
100 |
| - this.$emit('click', path, data) |
| 147 | + handleItemClick (path, data, checked) { |
| 148 | + this.$emit('click', path, data, checked) |
| 149 | + }, |
| 150 | + handleMouseover () { |
| 151 | + this.existMouseover && this.selectable && (this.treeContentBackground = '#eee') |
| 152 | + }, |
| 153 | + handleMouseout () { |
| 154 | + this.existMouseover && this.selectable && (this.treeContentBackground = 'transparent') |
101 | 155 | },
|
102 | 156 | // 工具函数: 判断是否对象
|
103 | 157 | isObject (value) {
|
|
117 | 171 | </script>
|
118 | 172 |
|
119 | 173 | <style lang="less">
|
| 174 | + @content-padding: 15px; /* 树的内容区域左留空*/ |
| 175 | +
|
120 | 176 | .vjs__tree {
|
121 | 177 | font-family: "Monaco", "Menlo", "Consolas", "Bitstream Vera Sans Mono";
|
122 | 178 | .vjs__tree__content {
|
123 |
| - padding-left: 15px; |
| 179 | + padding-left: @content-padding; |
124 | 180 | border-left: 1px dotted #ccc;
|
125 | 181 | }
|
126 | 182 | .vjs__tree__node {
|
|
147 | 203 | content: "\""
|
148 | 204 | }
|
149 | 205 | }
|
| 206 | + .vjs-checkbox { |
| 207 | + position: absolute; |
| 208 | + left: -30px; |
| 209 | + } |
150 | 210 | }
|
151 | 211 | </style>
|
0 commit comments