9
9
<div slot =" scroll" class =" tree" >
10
10
<component-instance
11
11
v-for =" instance in instances"
12
+ ref =" instances"
12
13
:key =" instance.id"
13
14
:instance =" instance"
14
15
:depth =" 0" >
@@ -25,6 +26,7 @@ import ComponentInstance from './ComponentInstance.vue'
25
26
import keyNavMixin from ' ../../mixins/key-nav'
26
27
27
28
export default {
29
+ mixins: [keyNavMixin],
28
30
components: {
29
31
ScrollPane,
30
32
ActionHeader,
@@ -33,11 +35,11 @@ export default {
33
35
props: {
34
36
instances: Array
35
37
},
36
- mixins: [keyNavMixin],
37
38
methods: {
38
39
filterInstances (e ) {
39
40
bridge .send (' filter-instances' , e .target .value )
40
41
},
42
+
41
43
onKeyNav (dir ) {
42
44
// somewhat hacky key navigation, but it works!
43
45
const currentEl = this .$el .querySelector (' .instance.selected' )
@@ -54,43 +56,38 @@ export default {
54
56
}
55
57
} else if (dir === ' right' ) {
56
58
if (current .expanded && current .$children .length ) {
57
- current = findByOffset (current, 1 )
59
+ current = this . findByOffset (current, 1 )
58
60
current .select ()
59
61
} else {
60
62
current .expand ()
61
63
}
62
64
} else if (dir === ' up' ) {
63
- current = findByOffset (current, - 1 )
65
+ current = this . findByOffset (current, - 1 )
64
66
current .select ()
65
67
} else {
66
- current = findByOffset (current, 1 )
68
+ current = this . findByOffset (current, 1 )
67
69
current .select ()
68
70
}
71
+ },
72
+
73
+ findByOffset (current , offset ) {
74
+ const all = getAllInstances (this .$refs .instances )
75
+ const index = all .indexOf (current) + offset
76
+ if (index < 0 ) {
77
+ return all[0 ]
78
+ } else if (index >= all .length ) {
79
+ return all[all .length - 1 ]
80
+ } else {
81
+ return all[index]
82
+ }
69
83
}
70
84
}
71
85
}
72
86
73
- function getAllInstances () {
74
- const nodes = [... document .querySelectorAll (' .instance' )]
75
- return nodes .map (n => n .__vue__ )
76
- }
77
-
78
- function findByOffset (current , offset ) {
79
- const all = getAllInstances ()
80
- let currentIndex = - 1
81
- all .forEach ((el , index ) => {
82
- if (current === el) {
83
- currentIndex = index
84
- }
85
- })
86
- offset = currentIndex + offset
87
- if (offset < 0 ) {
88
- return all[0 ]
89
- } else if (offset >= all .length ) {
90
- return all[all .length - 1 ]
91
- } else {
92
- return all[offset]
93
- }
87
+ function getAllInstances (list ) {
88
+ return Array .prototype .concat .apply ([], list .map (instance => {
89
+ return [instance, ... getAllInstances (instance .$children )]
90
+ }))
94
91
}
95
92
</script >
96
93
0 commit comments