@@ -41,13 +41,16 @@ export default {
41
41
},
42
42
43
43
onKeyNav (dir ) {
44
- // somewhat hacky key navigation, but it works!
45
- const currentEl = this .$el .querySelector (' .instance.selected' )
46
- let current = currentEl && currentEl .__vue__
44
+ const all = getAllInstances (this .$refs .instances )
45
+ if (! all .length ) {
46
+ return
47
+ }
48
+
49
+ const { current , currentIndex } = findCurrent (all, i => i .selected )
47
50
if (! current) {
48
- current = this .$children [0 ]
49
- current .select ()
51
+ return
50
52
}
53
+
51
54
if (dir === ' left' ) {
52
55
if (current .expanded ) {
53
56
current .collapse ()
@@ -56,29 +59,14 @@ export default {
56
59
}
57
60
} else if (dir === ' right' ) {
58
61
if (current .expanded && current .$children .length ) {
59
- current = this .findByOffset (current, 1 )
60
- current .select ()
62
+ findByIndex (all, currentIndex + 1 ).select ()
61
63
} else {
62
64
current .expand ()
63
65
}
64
66
} else if (dir === ' up' ) {
65
- current = this .findByOffset (current, - 1 )
66
- current .select ()
67
- } else {
68
- current = this .findByOffset (current, 1 )
69
- current .select ()
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 ]
67
+ findByIndex (all, currentIndex - 1 ).select ()
80
68
} else {
81
- return all[index]
69
+ findByIndex ( all, currentIndex + 1 ). select ()
82
70
}
83
71
}
84
72
}
@@ -89,6 +77,31 @@ function getAllInstances (list) {
89
77
return [instance, ... getAllInstances (instance .$children )]
90
78
}))
91
79
}
80
+
81
+ function findCurrent (all , check ) {
82
+ for (let i = 0 ; i < all .length ; i++ ) {
83
+ if (check (all[i])) {
84
+ return {
85
+ current: all[i],
86
+ currentIndex: i
87
+ }
88
+ }
89
+ }
90
+ return {
91
+ current: null ,
92
+ currentIndex: - 1
93
+ }
94
+ }
95
+
96
+ function findByIndex (all , index ) {
97
+ if (index < 0 ) {
98
+ return all[0 ]
99
+ } else if (index >= all .length ) {
100
+ return all[all .length - 1 ]
101
+ } else {
102
+ return all[index]
103
+ }
104
+ }
92
105
</script >
93
106
94
107
<style lang="stylus">
0 commit comments