-
Notifications
You must be signed in to change notification settings - Fork 138
/
LayoutToolTree.tsx
219 lines (194 loc) · 6.67 KB
/
LayoutToolTree.tsx
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
/**
* @license
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://github.com/IDuxFE/idux/blob/main/LICENSE
*/
import { type PropType, computed, defineComponent, inject } from 'vue'
import { type VKey } from '@idux/cdk/utils'
import { IxIcon } from '@idux/components/icon'
import { IxTree, type TreeDragDropOptions, type TreeProps } from '@idux/components/tree'
import { useKey } from '@idux/components/utils'
import { proTableToken } from '../token'
import { type ProTableColumn, type ProTableColumnBase } from '../types'
import { getColumnTitle, loopColumns } from '../utils'
export default defineComponent({
props: {
columns: { type: Array as PropType<ProTableColumn[]>, required: true },
searchValue: { type: String, default: undefined },
title: { type: String, default: undefined },
onDrop: { type: Function, required: true },
onFixedChange: { type: Function, required: true },
onVisibleChange: { type: Function, required: true },
},
setup(props) {
const key = useKey()
const { locale, mergedPrefixCls, mergedColumnMap } = inject(proTableToken)!
const checkedKeys = computed(() => getCheckedKeys(props.columns))
const onCheck = (checked: boolean, column: ProTableColumn) => {
column.visible = checked
loopColumns(column.children, child => {
child.visible = checked
})
if (checked) {
const map = mergedColumnMap.value
let currColumn = column
while (currColumn?.parentKey) {
const parent = map.get(currColumn.parentKey)
if (parent && parent.visible === false) {
parent.visible = undefined
}
currColumn = parent
}
}
props.onVisibleChange()
}
const onDrop = (options: TreeDragDropOptions) => {
const { dragNode, dropNode, dropType } = options
const dragKey = dragNode?.key
const dropKey = dropNode?.key
if (!dragKey || !dropKey) {
return
}
const columns = getDropColumns(props.columns, dragKey, dropKey, dropType)
props.onDrop!(key, columns)
}
const onFixedChange = (fixed: 'start' | 'end' | undefined, column: ProTableColumn) => {
column.fixed = fixed
loopColumns(column.children, child => {
child.fixed = fixed
})
props.onFixedChange()
}
return () => {
const dataSource = props.columns.filter(column => column.layoutable !== false)
// 不要显示空状态
if (dataSource.length === 0) {
return
}
const { searchValue, title } = props
const { startPin, endPin, noPin } = locale.table.layout
const treeProps: TreeProps = {
blocked: true,
cascaderStrategy: 'all',
checkable: true,
checkedKeys: checkedKeys.value,
draggable: true,
dataSource,
disabled: disableColumn,
empty: '',
childrenKey: 'children',
getKey: 'key',
labelKey: 'title',
searchValue,
onCheck,
onDrop,
}
const treeSlots = {
label: ({ node }: { node: ProTableColumn }) => getColumnTitle(node, locale.table),
suffix: ({ node }: { node: ProTableColumn }) => {
if (node.changeFixed === false || node.parentKey != null) {
return null
}
const { fixed } = node
if (fixed === 'start') {
return [
<IxIcon name="vertical-align-center" title={noPin} onClick={() => onFixedChange(undefined, node)} />,
<IxIcon
name="vertical-align-top"
rotate={180}
title={endPin}
onClick={() => onFixedChange('end', node)}
/>,
]
}
if (fixed === 'end') {
return [
<IxIcon name="vertical-align-top" title={startPin} onClick={() => onFixedChange('start', node)} />,
<IxIcon name="vertical-align-center" title={noPin} onClick={() => onFixedChange(undefined, node)} />,
]
}
return [
<IxIcon name="vertical-align-top" title={startPin} onClick={() => onFixedChange('start', node)} />,
<IxIcon name="vertical-align-top" rotate={180} title={endPin} onClick={() => onFixedChange('end', node)} />,
]
},
}
const prefixCls = `${mergedPrefixCls.value}-layout-tool-tree`
return (
<div class={prefixCls}>
{title && <div class={`${prefixCls}-title`}>{title}</div>}
<IxTree {...treeProps} v-slots={treeSlots} />
</div>
)
}
},
})
function disableColumn(column: ProTableColumn) {
const { changeIndex, changeVisible } = column
if (changeIndex !== false && changeVisible !== false) {
return false
}
return { check: changeVisible === false, drag: changeIndex === false }
}
function getCheckedKeys(columns: ProTableColumn[]) {
const keys: VKey[] = []
findDisplayKeys(columns, keys)
return keys
}
function findDisplayKeys(columns: ProTableColumn[], keys: VKey[]) {
for (let index = 0; index < columns.length; index++) {
const column = columns[index]
if (column.visible === false) {
continue
}
if ('children' in column && column.children) {
findDisplayKeys(column.children, keys)
} else {
keys.push(column.key!)
}
}
}
function getDropColumns(columns: ProTableColumn[], dragKey: VKey, dropKey: VKey, dropType: string | undefined) {
const newColumns = [...columns]
// 原数据中移除被拖拽的节点
let dragColumn: ProTableColumn
findTargetColumn(newColumns, dragKey, (column, index, columns) => {
dragColumn = column
columns.splice(index, 1)
})
if (dropType === 'inside') {
findTargetColumn(newColumns, dropKey, item => {
// 添加到头部
;(item as ProTableColumnBase).children!.unshift(dragColumn)
})
} else {
let targetIndex: number
let targetColumns: ProTableColumn[] = []
findTargetColumn(newColumns, dropKey, (_, index, columns) => {
targetIndex = index
targetColumns = columns
})
if (dropType === 'before') {
targetColumns.splice(targetIndex!, 0, dragColumn!)
} else {
targetColumns.splice(targetIndex! + 1, 0, dragColumn!)
}
}
return newColumns
}
function findTargetColumn(
columns: ProTableColumn[],
key: VKey,
callback: (column: ProTableColumn, index: number, columns: ProTableColumn[]) => void,
) {
for (let index = 0; index < columns.length; index++) {
const column = columns[index]
if (column.key! === key) {
return callback(column, index, columns)
}
if ('children' in column && column.children) {
findTargetColumn(column.children, key, callback)
}
}
}