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
|
/**
* @copyright Copyright (c) 2020 John Molakvoæ <skjnldsv@protonmail.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
const arrowNavigation = {
data() {
return {
focused: null,
}
},
methods: {
/**
* Functions to implement by the user of this mixin
getFocusableList() {
return this.$el.querySelectorAll('li.acli_wrapper .acli')
},
focusCancel() {
return this.abortSearch()
},
isFocused() {
return this.isSearching
},
*/
mountArrowNavigation() {
document.addEventListener('keydown', (event) => {
// https://www.w3.org/WAI/GL/wiki/Using_ARIA_menus
if (this.isFocused()) {
// If arrow down, focus next result
if (event.key === 'ArrowDown') {
this.focusNext(event)
}
// If arrow up, focus prev result
if (event.key === 'ArrowUp') {
this.focusPrev(event)
}
// Reset search
if (event.key === 'Escape') {
this.focusCancel()
}
}
})
},
/**
* If we have items already, open first one
*/
onInputEnter() {
const items = this.getFocusableList()
if (items.length) {
items[0].click()
}
},
/**
* If none already focused, focus the first rendered result
* @param {Event} event the keydown event
*/
focusInitialise(event) {
if (this.focused === null) {
this.focusFirst()
}
},
/**
* Focus the first item if any
* @param {Event} event the keydown event
*/
focusFirst(event) {
const items = this.getFocusableList()
if (items && items.length > 0) {
if (event) {
event.preventDefault()
}
this.focused = 0
this.focusIndex(this.focused)
}
},
/**
* Focus the next item if any
* @param {Event} event the keydown event
*/
focusNext(event) {
if (this.focused === null) {
this.focusFirst(event)
return
}
const items = this.getFocusableList()
// If we're not focusing the last, focus the next one
if (items && items.length > 0) {
event.preventDefault()
if (this.focused + 1 >= items.length) {
// When we are out of scope, reset the focus to the last item
this.focused = items.length - 1
} else {
this.focused++
}
this.focusIndex(this.focused)
}
},
/**
* Focus the previous item if any
* @param {Event} event the keydown event
*/
focusPrev(event) {
if (this.focused === null) {
this.focusFirst(event)
return
}
const items = this.getFocusableList()
// If we're not focusing the first, focus the previous one
if (items && items.length > 0 && this.focused > 0) {
event.preventDefault()
if (this.focused > items.length) {
// When we are out of scope, reset the focus to the last item
this.focused = items.length - 1
} else {
this.focused--
}
this.focusIndex(this.focused)
}
},
/**
* Focus the specified item index if it exists
* @param {number} index the item index
*/
focusIndex(index) {
const items = this.getFocusableList()
if (items && items[index]) {
items[index].focus()
}
},
/**
* Set the current focused element based on the target
* @param {Event} event the focus event
*/
setFocusedIndex(event) {
const entry = event.target
const items = this.getFocusableList()
const index = [...items].findIndex(search => search === entry)
if (index > -1) {
// let's not use focusIndex as the entry is already focused
this.focused = index
}
},
},
}
export default arrowNavigation
|