-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelect.js
333 lines (316 loc) · 9.99 KB
/
Select.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* eslint-env browser */
/* global Popper */
var Select = function (input, store, options = {allowFreeText: true, realSelect: false}) {
if (!(input instanceof HTMLInputElement)) {
throw new Error('Not an Input element')
}
input.classList.add('select')
input.setAttribute('autocomplete', 'off')
let popper = null
let originalValue = input.value
let obj = new Proxy(this, {
get: function (obj, prop) {
switch (prop) {
case 'value':
if (!input.dataset.value) {
return input.value ? input.value : ''
} else {
return input.dataset.value
}
case 'domNode':
return input
default:
return obj[prop] ? obj[prop] : ''
}
},
set: function (obj, prop, value) {
switch (prop) {
case 'value':
if (!value) { break }
input.dataset.loading = '1'
store.get(value).then((entry) => {
if (entry) {
this.lastEntry = entry
input.value = entry.label
this.value = input.value
input.dataset.value = entry.value
} else {
if (options.allowFreeText) {
input.value = value
} else {
if (!this.lastEntry) {
this.value = ''
input.value = ''
} else {
this.value = this.lastEntry.value
input.value = this.lastEntry.label
}
}
}
input.dataset.loading = '0'
})
break
default:
input[prop] = value
}
}
})
var list = document.createElement('DIV')
list.classList.add('dropdown')
/* taken from https://stackoverflow.com/posts/35173443/revisions */
var focusNextElement = function (current = null) {
if (!current) {
current = document.activeElement
}
//add all elements we want to include in our selection
let focussableElements = 'a:not([disabled]), button:not([disabled]), input[type=text]:not([disabled]), [tabindex]:not([disabled]):not([tabindex="-1"])';
if (document.activeElement && document.activeElement.form) {
let focussable = Array.prototype.filter.call(document.activeElement.form.querySelectorAll(focussableElements),
function (element) {
//check for visibility while always include the current activeElement
return element.offsetWidth > 0 || element.offsetHeight > 0 || element === document.activeElement
});
let index = focussable.indexOf(current);
if (index > -1) {
let element = focussable[index + 1] || focussable[0]
element.focus()
}
}
}
var select = (target, dontMessValue = false) => {
if (!dontMessValue) {
input.value = target.textContent
}
input.dataset.value = target.dataset.value
target.dataset.hover = '1'
window.requestAnimationFrame(() => {
if (target.parentNode) {
target.parentNode.scrollTop = target.offsetTop - Math.floor(target.parentNode.clientHeight / 2)
}
})
let ev = new Event('change')
ev.value = input.dataset.value
this.Events.dispatchEvent(ev)
}
this.Events = new EventTarget()
var move = (k) => {
let current = null
for (let i = list.firstElementChild; i; i = i.nextElementSibling) {
if (i.dataset.hover === '1') {
current = i
break
}
}
let set = null
let reset = null
let moveStep = 1
let wrapTarget = null
switch (k) {
case 'PageDown':
if (list.firstElementChild) {
moveStep = Math.floor(list.getBoundingClientRect().height / list.firstElementChild.getBoundingClientRect().height)
}
wrapTarget = list.lastElementChild
/* Fall through */
case 'ArrowDown':
if (!wrapTarget) {
wrapTarget = list.firstElementChild
}
if (current === null) {
current = list.firstElementChild
moveStep--
}
reset = current
for (let i = 0; i < moveStep && current; i++) {
current = current.nextElementSibling
}
if (current) {
set = current
} else {
set = wrapTarget
}
break
case 'PageUp':
if (list.firstElementChild) {
moveStep = Math.floor(list.getBoundingClientRect().height / list.firstElementChild.getBoundingClientRect().height)
}
wrapTarget = list.firstElementChild
/* Fall through */
case 'ArrowUp':
if (!wrapTarget) {
wrapTarget = list.lastElementChild
}
if (current === null) {
current = list.lastElementChild
moveStep--
}
reset = current
for (let i = 0; i < moveStep && current; i++) {
current = current.previousElementSibling
}
if (current) {
set = current
} else {
set = wrapTarget
}
break
}
if (set) {
set.dataset.hover = '1'
if (set.getBoundingClientRect().bottom > list.getBoundingClientRect().bottom) {
set.scrollIntoView()
} else if (set.getBoundingClientRect().top < list.getBoundingClientRect().top) {
set.scrollIntoView(false)
}
if (!options.allowFreeText) {
select(set, true)
}
}
if (reset && set !== reset) {
reset.dataset.hover = '0'
}
}
var degenerate = (event) => {
if (popper) { popper.destroy(); popper = null }
if (list.parentNode) {
window.requestAnimationFrame(() => list.parentNode.removeChild(list))
}
}
var handleTab = (event) => {
switch (event.key) {
case 'Enter':
event.stopPropagation()
event.preventDefault()
return
case 'Tab':
for (let n = list.firstElementChild; n; n = n.nextElementSibling) {
if (n.dataset.hover === '1') {
select(n)
break
}
}
degenerate()
break
}
}
var generate = (event) => {
let noValQuery = false
if (event.type === 'focus') {
input.setSelectionRange(0, input.value.length)
noValQuery = true
}
switch (event.key) {
case 'Tab': return
case 'Enter':
for (let n = list.firstElementChild; n; n = n.nextElementSibling) {
if (n.dataset.hover === '1') {
/* select item in the list and block some on keyup enter that send form if
* listener setup as so (if setup before this one it has no effet)
*/
event.stopPropagation()
select(n)
degenerate()
focusNextElement(input)
}
}
return
case 'ArrowUp':
case 'ArrowDown':
case 'PageUp':
case 'PageDown':
event.preventDefault()
return move(event.key)
case 'ArrowLeft':
case 'ArrowRight':
case 'Escape':
case 'Alt':
case 'AltGraph':
return
case 'Backspace':
case 'Delete':
if (input.value.length === 0 && !options.realSelect) {
return degenerate()
}
}
/* avoid generating another list over the old one, triggering a nasty visual bug when you select
* an item the list disappear and nothing is selected
*/
if (!popper) {
window.requestAnimationFrame((event) => {
if (!list.parentNode) {
input.parentNode.insertBefore(list, input.nextSiblingElement)
}
popper = new Popper(input, list, { removeOnDestroy: true, positionFixed: true, placement: 'bottom-start' })
})
}
let currentValue = input.value
if (noValQuery) { currentValue = '' }
store.query(currentValue).then((data) => {
if (currentValue !== input.value && !noValQuery) { return }
let frag = document.createDocumentFragment()
if (data.length < 1) {
degenerate()
} else {
window.requestAnimationFrame(() => {
list.innerHTML = ''
})
let noValQuerySelectedElement = null
data.forEach((entry) => {
let s = document.createElement('DIV')
s.dataset.value = entry.value
s.innerHTML = entry.label
if (noValQuery && input.dataset.value === s.dataset.value) {
noValQuerySelectedElement = s
}
s.addEventListener('mouseover', (event) => {
for (let i = list.firstElementChild; i; i = i.nextElementSibling) {
if (i !== event.target) {
i.dataset.hover = '0'
}
}
event.target.dataset.hover = '1'
})
s.addEventListener('mousedown', (event) => {
event.preventDefault()
select(event.target)
degenerate()
focusNextElement(input)
})
frag.appendChild(s)
})
window.requestAnimationFrame(() => {
list.appendChild(frag)
if (noValQuerySelectedElement) {
select(noValQuerySelectedElement, true)
} else if (!options.allowFreeText) {
select(list.firstElementChild, true)
}
})
}
})
}
/* must catch onchange and redispatch a new onchange for form listening on that
* event. Not doing so lead, in some cases, to strange behavior
*/
input.addEventListener('change', (event) => {
if (event.detail && event.detail.resent) { return }
event.stopPropagation()
if (input.dataset.value) {
store.get(input.dataset.value).then((entry) => {
if (entry) {
input.value = entry.label
}
})
}
input.dispatchEvent(new CustomEvent('change', {detail: {resent: true}}))
}, {capture: true})
input.addEventListener('blur', degenerate)
input.addEventListener('keyup', generate, {capture: true})
input.addEventListener('keydown', handleTab, {capture: true})
input.addEventListener('focus', generate)
obj.value = originalValue
return obj
}
Select.prototype.addEventListener = function (type, callback, options = {}) {
this.Events.addEventListener(type, callback, options)
}