-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
301 lines (279 loc) · 9.21 KB
/
index.js
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
const customSelect = function (element, overrides){
// SETUP
// /////////////////////////////////
// assign names to things we'll need to use more than once
const defaults = {
inputSelector: 'input',
listSelector: 'ul',
optionSelector: 'li',
statusSelector: '[aria-live="polite"]'
};
const options = Object.assign({},
defaults,
overrides
);
const csSelector = document.querySelector(element) // the input, svg and ul as a group
const csInput = csSelector.querySelector(options.inputSelector)
const csList = csSelector.querySelector(options.listSelector)
const csOptions = csList.querySelectorAll(options.optionSelector)
// const csIcons = csSelector.querySelectorAll('svg')
const csStatus = document.querySelector(options.statusSelector)
const aOptions = Array.from(csOptions)
// when JS is loaded, set up our starting point
// if JS fails to load, the custom select remains a plain text input
// create and set start point for the state tracker
let csState = "initial"
// inform assistive tech (screen readers) of the names & roles of the elements in our group
csSelector.setAttribute('role', 'combobox')
csSelector.setAttribute('aria-haspopup', 'listbox')
csSelector.setAttribute('aria-owns', 'custom-select-list') // container owns the list...
csInput.setAttribute('aria-autocomplete', 'both')
csInput.setAttribute('aria-controls', 'custom-select-list') // ...but the input controls it
csList.setAttribute('role', 'listbox')
csOptions.forEach(function(option) {
option.setAttribute('role', 'option')
option.setAttribute('tabindex', "-1") // make li elements keyboard focusable by script only
})
// EVENTS
// /////////////////////////////////
csSelector.addEventListener('click', function(e) {
const currentFocus = findFocus()
switch(csState) {
case 'initial' : // if state = initial, toggleOpen and set state to opened
toggleList('Open')
setState('opened')
break
case 'opened':
// if state = opened and focus on input, toggleShut and set state to initial
if (currentFocus === csInput) {
toggleList('Shut')
setState('initial')
} else if (currentFocus.tagName === 'LI') {
// if state = opened and focus on list, makeChoice, toggleShut and set state to closed
makeChoice(currentFocus)
toggleList('Shut')
setState('closed')
}
break
case 'filtered':
// if state = filtered and focus on list, makeChoice and set state to closed
if (currentFocus.tagName === 'LI') {
makeChoice(currentFocus)
toggleList('Shut')
setState('closed')
} // if state = filtered and focus on input, do nothing (wait for next user input)
break
case 'closed': // if state = closed, toggleOpen and set state to filtered? or opened?
toggleList('Open')
setState('filtered')
break
}
})
csSelector.addEventListener('keyup', function(e) {
doKeyAction(e.key)
})
document.addEventListener('click', function(e) {
if (!e.target.closest('#myCustomSelect')) {
// click outside of the custom group
toggleList('Shut')
setState('initial')
}
})
// FUNCTIONS
// /////////////////////////////////
function toggleList(whichWay) {
if (whichWay === 'Open') {
csList.classList.remove('hidden-all')
csSelector.setAttribute('aria-expanded', 'true')
} else { // === 'Shut'
csList.classList.add('hidden-all')
csSelector.setAttribute('aria-expanded', 'false')
}
}
function findFocus() {
const focusPoint = document.activeElement
return focusPoint
}
function moveFocus(fromHere, toThere) {
// grab the currently showing options, which might have been filtered
const aCurrentOptions = aOptions.filter(function(option) {
if (option.style.display === '') {
return true
}
})
// don't move if all options have been filtered out
if (aCurrentOptions.length === 0) {
return
}
if (toThere === 'input') {
csInput.focus()
}
// possible start points
switch(fromHere) {
case csInput:
if (toThere === 'forward') {
aCurrentOptions[0].focus()
} else if (toThere === 'back') {
aCurrentOptions[aCurrentOptions.length - 1].focus()
}
break
case csOptions[0]:
if (toThere === 'forward') {
aCurrentOptions[1].focus()
} else if (toThere === 'back') {
csInput.focus()
}
break
case csOptions[csOptions.length - 1]:
if (toThere === 'forward') {
aCurrentOptions[0].focus()
} else if (toThere === 'back') {
aCurrentOptions[aCurrentOptions.length - 2].focus()
}
break
default: // middle list or filtered items
const currentItem = findFocus()
const whichOne = aCurrentOptions.indexOf(currentItem)
if (toThere === 'forward') {
const nextOne = aCurrentOptions[whichOne + 1]
nextOne.focus()
} else if (toThere === 'back' && whichOne > 0) {
const previousOne = aCurrentOptions[whichOne - 1]
previousOne.focus()
} else { // if whichOne = 0
csInput.focus()
}
break
}
}
function doFilter() {
const terms = csInput.value
const aFilteredOptions = aOptions.filter(function(option) {
if (option.innerText.toUpperCase().startsWith(terms.toUpperCase())) {
return true
}
})
csOptions.forEach(option => option.style.display = "none")
aFilteredOptions.forEach(function(option) {
option.style.display = ""
})
setState('filtered')
updateStatus(aFilteredOptions.length)
}
function updateStatus(howMany) {
csStatus.textContent = howMany + " options available."
}
function makeChoice(whichOption) {
const optionTitle = whichOption.querySelector('strong')
csInput.value = optionTitle.textContent
moveFocus(document.activeElement, 'input')
// update aria-selected, if using
}
function setState(newState) {
switch (newState) {
case 'initial':
csState = 'initial'
break
case 'opened':
csState = 'opened'
break
case 'filtered':
csState = 'filtered'
break
case 'closed':
csState = 'closed'
}
// console.log({csState})
}
function doKeyAction(whichKey) {
const currentFocus = findFocus()
switch(whichKey) {
case 'Enter':
if (csState === 'initial') {
// if state = initial, toggleOpen and set state to opened
toggleList('Open')
setState('opened')
} else if (csState === 'opened' && currentFocus.tagName === 'LI') {
// if state = opened and focus on list, makeChoice and set state to closed
makeChoice(currentFocus)
toggleList('Shut')
setState('closed')
} else if (csState === 'opened' && currentFocus === csInput) {
// if state = opened and focus on input, close it
toggleList('Shut')
setState('closed')
} else if (csState === 'filtered' && currentFocus.tagName === 'LI') {
// if state = filtered and focus on list, makeChoice and set state to closed
makeChoice(currentFocus)
toggleList('Shut')
setState('closed')
} else if (csState === 'filtered' && currentFocus === csInput) {
// if state = filtered and focus on input, set state to opened
toggleList('Open')
setState('opened')
} else { // i.e. csState is closed, or csState is opened/filtered but other focus point?
// if state = closed, set state to filtered? i.e. open but keep existing input?
toggleList('Open')
setState('filtered')
}
break
case 'Escape':
// if state = initial, do nothing
// if state = opened or filtered, set state to initial
// if state = closed, do nothing
if (csState === 'opened' || csState === 'filtered') {
toggleList('Shut')
setState('initial')
}
break
case 'ArrowDown':
if (csState === 'initial' || csState === 'closed') {
// if state = initial or closed, set state to opened and moveFocus to first
toggleList('Open')
moveFocus(csInput, 'forward')
setState('opened')
} else {
// if state = opened and focus on input, moveFocus to first
// if state = opened and focus on list, moveFocus to next/first
// if state = filtered and focus on input, moveFocus to first
// if state = filtered and focus on list, moveFocus to next/first
toggleList('Open')
moveFocus(currentFocus, 'forward')
}
break
case 'ArrowUp':
if (csState === 'initial' || csState === 'closed') {
// if state = initial, set state to opened and moveFocus to last
// if state = closed, set state to opened and moveFocus to last
toggleList('Open')
moveFocus(csInput, 'back')
setState('opened')
} else {
// if state = opened and focus on input, moveFocus to last
// if state = opened and focus on list, moveFocus to prev/last
// if state = filtered and focus on input, moveFocus to last
// if state = filtered and focus on list, moveFocus to prev/last
moveFocus(currentFocus, 'back')
}
break
default:
if (csState === 'initial') {
// if state = initial, toggle open, doFilter and set state to filtered
toggleList('Open')
doFilter()
setState('filtered')
} else if (csState === 'opened') {
// if state = opened, doFilter and set state to filtered
doFilter()
setState('filtered')
} else if (csState === 'closed') {
// if state = closed, doFilter and set state to filtered
doFilter()
setState('filtered')
} else { // already filtered
doFilter()
}
break
}
}
};