This repository has been archived by the owner on Aug 21, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinput-stack.js
154 lines (126 loc) · 3.27 KB
/
input-stack.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
var watch = require('observ/watch')
var Observ = require('observ')
module.exports = InputStack
function InputStack(){
var keysDown = null
var stack = []
var release = null
function grab(){
var input = this
var index = stack.indexOf(input)
if (!stack.length || index < stack.length - 1){
release&&release()
if (stack.length){
stack[stack.length-1].set([])
}
if (~index){
stack.splice(index, 1)
}
stack.push(input)
release = activate(input)
}
}
function close(){
var input = this
if (stack.length){
var index = stack.indexOf(input)
if (index === stack.length-1){
release&&release()
stack.pop()
release = activate(stack[stack.length-1])
}
}
input.set([])
}
function activate(input) {
if (input) {
var releaseInput = watch(getKeysDown(), input.set)
input.active.set(true)
return function() {
releaseInput()
input.active.set(false)
}
}
}
function next(){
if (stack.length > 1){
var input = stack.pop()
release&&release()
input.set([])
input.active.set(false)
stack.unshift(input)
release = activate(stack[stack.length-1])
}
}
function swap(){
if (stack.length > 1){
var input = stack.pop()
release&&release()
input.set([])
input.active.set(false)
stack.splice(-1, 0, input)
release = activate(stack[stack.length-1])
}
}
function getKeysDown(){
return keysDown = keysDown || KeysDown()
}
function handleKey(e) {
var el = document.activeElement
if (stack.length) {
if (!el || (!shouldIgnore(el) && el.contentEditable !== 'true')){
if ((e.keyCode === 9 || e.keyCode === 13) && e.type === 'keydown') { // tab or enter
e.preventDefault()
next()
} else if (e.keyCode === 18) { // alt
e.preventDefault()
swap()
}
}
}
}
document.addEventListener('keydown', handleKey, false)
document.addEventListener('keyup', handleKey, false)
return function(){
var input = Observ([])
input.active = Observ()
input.grab = grab.bind(input)
input.close = close.bind(input)
input.grab()
return input
}
}
function shouldIgnore(el){
return (el.nodeName === 'INPUT' && el.type !== 'range' && el.type !== 'checkbox') ||
el.nodeName === 'TEXTAREA'
}
function KeysDown(){
var obs = Observ([])
function handleEvent(e){
var el = document.activeElement
if (!el || (!shouldIgnore(el) && el.contentEditable !== 'true')){
var index = obs().indexOf(e.keyCode)
if (e.type === 'keydown'){
if (!~index){
var val = obs().concat()
val.push(e.keyCode)
obs.set(val)
e.preventDefault()
}
} else if (e.type === 'keyup'){
if (~index){
var val = obs().concat()
val.splice(index, 1)
obs.set(val)
}
}
}
}
document.addEventListener('keydown', handleEvent, false)
document.addEventListener('keyup', handleEvent, false)
obs.destroy = function(){
document.removeEventListener('keydown', handleEvent, false)
document.removeEventListener('keyup', handleEvent, false)
}
return obs
}