-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_handler.js
99 lines (88 loc) · 3.21 KB
/
input_handler.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
function inputify(element, cb, setText = true) {
let input = document.createElement('input')
if(setText) input.value = element.textContent
element.textContent = ''
element.classList.add('activeInput')
element.appendChild(input)
function callCb() {
element.classList.remove('activeInput')
input.removeEventListener('keypress', callCbEnter)
cb(input.value)
}
function callCbEnter() {
if(event.which == 13) {
element.classList.remove('activeInput')
input.removeEventListener('blur', callCb)
cb(input.value)
}
}
input.addEventListener('blur', callCb)
input.addEventListener('keypress', callCbEnter)
input.addEventListener('click', event => event.stopImmediatePropagation())
setTimeout(() => {
input.focus()
input.select()
}, 0)
}
const DIRECTIONS = Object.freeze({
left: 37, up: 38, right: 39, down: 40
})
function inputifyNav(element, cb, navigate, database = undefined) {
if(element.querySelector('input')) return
function testNavigation(event) {
switch(event.which) {
case DIRECTIONS.left:
if(event.target.selectionStart != 0) break
case DIRECTIONS.right:
if(event.which == DIRECTIONS.right && event.target.selectionStart != event.target.value.length && !(event.target.selectionStart == 0 && event.target.selectionEnd == event.target.value.length)) break
case DIRECTIONS.up:
case DIRECTIONS.down:
navigate(event.which)
}
}
if(database) inputifyAutocomplete(element, cb, database)
else inputify(element, cb)
element.querySelector('input:not(:disabled)').addEventListener('keydown', testNavigation)
}
function inputifyAutocomplete(element, cb, database) {
database = database.sort()
let input = document.createElement('input')
input.style.background = 'transparent'
let hintInput = document.createElement('input')
element.classList.add('autocompleteInput')
input.value = element.textContent
hintInput.value = element.textContent
hintInput.disabled = true
element.textContent = ''
element.appendChild(hintInput)
element.appendChild(input)
function callCb() {
element.classList.remove('autocompleteInput')
input.removeEventListener('keypress', callCbEnter)
cb(hintInput.value)
}
function callCbEnter() {
if(event.which == 13) {
element.classList.remove('autocompleteInput')
input.removeEventListener('blur', callCb)
cb(hintInput.value)
}
else autoComplete()
}
function autoComplete() {
for(const name of database) {
if(input.value && name.toLowerCase().startsWith(input.value.toLowerCase())) {
hintInput.value = input.value + name.toLowerCase().split(input.value.toLowerCase())[1]
return
}
}
hintInput.value = ''
}
input.addEventListener('blur', callCb)
input.addEventListener('keyup', callCbEnter)
input.addEventListener('click', event => event.stopImmediatePropagation())
setTimeout(() => {
input.focus()
input.select()
}, 0)
}