/
Keyboard.js
242 lines (193 loc) · 5.84 KB
/
Keyboard.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
/*
---
script: Keyboard.js
name: Keyboard
description: KeyboardEvents used to intercept events on a class for keyboard and format modifiers in a specific order so as to make alt+shift+c the same as shift+alt+c.
license: MIT-style license
authors:
- Perrin Westrich
- Aaron Newton
- Scott Kyle
requires:
- Core/Events
- Core/Options
- Core/Element.Event
- Element.Event.Pseudos.Keys
provides: [Keyboard]
...
*/
(function(){
var Keyboard = this.Keyboard = new Class({
Extends: Events,
Implements: [Options],
options: {/*
onActivate: function(){},
onDeactivate: function(){},*/
defaultEventType: 'keydown',
active: false,
manager: null,
events: {},
nonParsedEvents: ['activate', 'deactivate', 'onactivate', 'ondeactivate', 'changed', 'onchanged']
},
initialize: function(options){
if (options && options.manager){
this._manager = options.manager;
delete options.manager;
}
this.setOptions(options);
this._setup();
},
addEvent: function(type, fn, internal){
return this.parent(Keyboard.parse(type, this.options.defaultEventType, this.options.nonParsedEvents), fn, internal);
},
removeEvent: function(type, fn){
return this.parent(Keyboard.parse(type, this.options.defaultEventType, this.options.nonParsedEvents), fn);
},
toggleActive: function(){
return this[this.isActive() ? 'deactivate' : 'activate']();
},
activate: function(instance){
if (instance){
if (instance.isActive()) return this;
//if we're stealing focus, store the last keyboard to have it so the relinquish command works
if (this._activeKB && instance != this._activeKB){
this.previous = this._activeKB;
this.previous.fireEvent('deactivate');
}
//if we're enabling a child, assign it so that events are now passed to it
this._activeKB = instance.fireEvent('activate');
Keyboard.manager.fireEvent('changed');
} else if (this._manager){
//else we're enabling ourselves, we must ask our parent to do it for us
this._manager.activate(this);
}
return this;
},
isActive: function(){
return this._manager ? (this._manager._activeKB == this) : (Keyboard.manager == this);
},
deactivate: function(instance){
if (instance){
if (instance === this._activeKB){
this._activeKB = null;
instance.fireEvent('deactivate');
Keyboard.manager.fireEvent('changed');
}
} else if (this._manager){
this._manager.deactivate(this);
}
return this;
},
relinquish: function(){
if (this.isActive() && this._manager && this._manager.previous) this._manager.activate(this._manager.previous);
else this.deactivate();
return this;
},
//management logic
manage: function(instance){
if (instance._manager) instance._manager.drop(instance);
this._instances.push(instance);
instance._manager = this;
if (!this._activeKB) this.activate(instance);
return this;
},
drop: function(instance){
instance.relinquish();
this._instances.erase(instance);
if (this._activeKB == instance){
if (this.previous && this._instances.contains(this.previous)) this.activate(this.previous);
else this._activeKB = this._instances[0];
}
return this;
},
trace: function(){
Keyboard.trace(this);
},
each: function(fn){
Keyboard.each(this, fn);
},
/*
PRIVATE METHODS
*/
_instances: [],
_disable: function(instance){
if (this._activeKB == instance) this._activeKB = null;
},
_setup: function(){
this.addEvents(this.options.events);
//if this is the root manager, nothing manages it
if (Keyboard.manager && !this._manager) Keyboard.manager.manage(this);
if (this.options.active) this.activate();
else this.relinquish();
},
_handle: function(event, type){
//Keyboard.stop(event) prevents key propagation
if (event.preventKeyboardPropagation) return;
var bubbles = !!this._manager;
if (bubbles && this._activeKB){
this._activeKB._handle(event, type);
if (event.preventKeyboardPropagation) return;
}
this.fireEvent(type, event);
if (!bubbles && this._activeKB) this._activeKB._handle(event, type);
}
});
var parsed = {};
var modifiers = ['shift', 'control', 'alt', 'meta'];
var regex = /^(?:shift|control|ctrl|alt|meta)$/;
Keyboard.parse = function(type, eventType, ignore){
if (ignore && ignore.contains(type.toLowerCase())) return type;
type = type.toLowerCase().replace(/^(keyup|keydown):/, function($0, $1){
eventType = $1;
return '';
});
if (!parsed[type]){
var key, mods = {};
type.split('+').each(function(part){
if (regex.test(part)) mods[part] = true;
else key = part;
});
mods.control = mods.control || mods.ctrl; // allow both control and ctrl
var keys = [];
modifiers.each(function(mod){
if (mods[mod]) keys.push(mod);
});
if (key) keys.push(key);
parsed[type] = keys.join('+');
}
return eventType + ':keys(' + parsed[type] + ')';
};
Keyboard.each = function(keyboard, fn){
var current = keyboard || Keyboard.manager;
while (current){
fn.run(current);
current = current._activeKB;
}
};
Keyboard.stop = function(event){
event.preventKeyboardPropagation = true;
};
Keyboard.manager = new Keyboard({
active: true
});
Keyboard.trace = function(keyboard){
keyboard = keyboard || Keyboard.manager;
var hasConsole = window.console && console.log;
if (hasConsole) console.log('the following items have focus: ');
Keyboard.each(keyboard, function(current){
if (hasConsole) console.log(document.id(current.widget) || current.wiget || current);
});
};
var handler = function(event){
var keys = [];
modifiers.each(function(mod){
if (event[mod]) keys.push(mod);
});
if (!regex.test(event.key)) keys.push(event.key);
Keyboard.manager._handle(event, event.type + ':keys(' + keys.join('+') + ')');
};
document.addEvents({
'keyup': handler,
'keydown': handler
});
}).call(this);