/
index.ts
81 lines (67 loc) 路 1.87 KB
/
index.ts
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
import Vue from 'vue'
import mixins from '../../util/mixins'
import { VOverlay } from '../../components/VOverlay'
interface options {
$el: HTMLElement
$refs: {
content?: HTMLElement
}
overlay?: InstanceType<typeof VOverlay>
}
interface DependentInstance extends Vue {
isActive?: boolean
isDependent?: boolean
}
function searchChildren (children: Vue[]): DependentInstance[] {
const results = []
for (let index = 0; index < children.length; index++) {
const child = children[index] as DependentInstance
if (child.isActive && child.isDependent) {
results.push(child)
} else {
results.push(...searchChildren(child.$children))
}
}
return results
}
/* @vue/component */
export default mixins<Vue & options>().extend({
name: 'dependent',
data () {
return {
closeDependents: true,
isActive: false,
isDependent: true,
}
},
watch: {
isActive (val) {
if (val) return
const openDependents = this.getOpenDependents()
for (let index = 0; index < openDependents.length; index++) {
openDependents[index].isActive = false
}
},
},
methods: {
getOpenDependents (): any[] {
if (this.closeDependents) return searchChildren(this.$children)
return []
},
getOpenDependentElements (): HTMLElement[] {
const result = []
const openDependents = this.getOpenDependents()
for (let index = 0; index < openDependents.length; index++) {
result.push(...openDependents[index].getClickableDependentElements())
}
return result
},
getClickableDependentElements (): HTMLElement[] {
const result = [this.$el]
if (this.$refs.content) result.push(this.$refs.content)
if (this.overlay) result.push(this.overlay.$el as HTMLElement)
result.push(...this.getOpenDependentElements())
return result
},
},
})