-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathkey-event.js
302 lines (243 loc) · 8.36 KB
/
key-event.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
// 1. key events
(function() {
var KeyEvent = function(data, type) {
this.keyCode = 'keyCode' in data ? data.keyCode : 0;
this.charCode = 'charCode' in data ? data.charCode : 0;
var modifiers = 'modifiers' in data ? data.modifiers : [];
this.ctrlKey = false;
this.metaKey = false;
this.altKey = false;
this.shiftKey = false;
for (var i = 0; i < modifiers.length; i++) {
this[modifiers[i] + 'Key'] = true;
}
this.type = type || 'keypress';
};
KeyEvent.prototype.toNative = function() {
var event = document.createEventObject ? document.createEventObject() : document.createEvent('Events');
if (event.initEvent) {
event.initEvent(this.type, true, true);
}
event.keyCode = this.keyCode;
event.which = this.charCode || this.keyCode;
event.shiftKey = this.shiftKey;
event.metaKey = this.metaKey;
event.altKey = this.altKey;
event.ctrlKey = this.ctrlKey;
return event;
};
KeyEvent.prototype.fire = function(element) {
var event = this.toNative();
if (element.dispatchEvent) {
element.dispatchEvent(event);
return;
}
element.fireEvent('on' + this.type, event);
};
// simulates complete key event as if the user pressed the key in the browser
// triggers a keydown, then a keypress, then a keyup
KeyEvent.simulate = function(charCode, keyCode, modifiers, element, repeat) {
if (modifiers === undefined) {
modifiers = [];
}
if (element === undefined) {
element = document;
}
if (repeat === undefined) {
repeat = 1;
}
var modifierToKeyCode = {
'shift': 16,
'ctrl': 17,
'alt': 18,
'meta': 91
};
// if the key is a modifier then take it out of the regular
// keypress/keydown
if (keyCode == 16 || keyCode == 17 || keyCode == 18 || keyCode == 91) {
repeat = 0;
}
var modifiersToInclude = [];
var keyEvents = [];
// modifiers would go down first
for (var i = 0; i < modifiers.length; i++) {
modifiersToInclude.push(modifiers[i]);
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: modifierToKeyCode[modifiers[i]],
modifiers: modifiersToInclude
}, 'keydown'));
}
// @todo factor in duration for these
while (repeat > 0) {
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: keyCode,
modifiers: modifiersToInclude
}, 'keydown'));
keyEvents.push(new KeyEvent({
charCode: charCode,
keyCode: charCode,
modifiers: modifiersToInclude
}, 'keypress'));
repeat--;
}
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: keyCode,
modifiers: modifiersToInclude
}, 'keyup'));
// now lift up the modifier keys
for (i = 0; i < modifiersToInclude.length; i++) {
var modifierKeyCode = modifierToKeyCode[modifiersToInclude[i]];
modifiersToInclude.splice(i, 1);
keyEvents.push(new KeyEvent({
charCode: 0,
keyCode: modifierKeyCode,
modifiers: modifiersToInclude
}, 'keyup'));
}
for (i = 0; i < keyEvents.length; i++) {
// console.log('firing', keyEvents[i].type, keyEvents[i].keyCode, keyEvents[i].charCode);
keyEvents[i].fire(element);
}
};
window.KeyEvent = KeyEvent;
}) ();
// 2. jquery replace
// Gary Haran => gary@talkerapp.com
// This code is released under MIT licence
(function($){
var replacer = function(finder, replacement, element, blackList) {
if (!finder || typeof replacement === 'undefined') {
return
}
var regex = (typeof finder == 'string') ? new RegExp(finder, 'g') : finder;
var childNodes = element.childNodes;
var len = childNodes.length;
var list = typeof blackList == 'undefined' ? 'html,head,style,title,link,meta,script,object,iframe,pre,a,' : blackList ;
while (len--) {
var node = childNodes[len];
if (node.nodeType === 1 && true || (list.indexOf(node.nodeName.toLowerCase()) === -1)) {
replacer(finder, replacement, node, list);
}
if (node.nodeType !== 3 || !regex.test(node.data)) {
continue;
}
var frag = (function(){
var html = node.data.replace(regex, replacement);
var wrap = document.createElement('span');
var frag = document.createDocumentFragment();
wrap.innerHTML = html;
while (wrap.firstChild) {
frag.appendChild(wrap.firstChild);
}
return frag;
})();
var parent = node.parentNode;
parent.insertBefore(frag, node);
parent.removeChild(node);
}
}
$.fn.replace = function(finder, replacement, blackList) {
return this.each(function(){
replacer(finder, replacement, $(this).get(0), blackList);
});
}
})(jQuery);
//3. Play Music
function playSound(url){
var audio = document.createElement('audio');
audio.style.display = "none";
audio.src = url;
audio.autoplay = true;
audio.onended = function(){
audio.remove() //Remove when played.
};
document.body.appendChild(audio);
}
//4. Copy CSS
(function($){
$.fn.getStyles = function(only, except) {
// the map to return with requested styles and values as KVP
var product = {};
// the style object from the DOM element we need to iterate through
var style;
// recycle the name of the style attribute
var name;
// if it's a limited list, no need to run through the entire style object
if (only && only instanceof Array) {
for (var i = 0, l = only.length; i < l; i++) {
// since we have the name already, just return via built-in .css method
name = only[i];
product[name] = this.css(name);
}
} else {
// prevent from empty selector
if (this.length) {
// otherwise, we need to get everything
var dom = this.get(0);
// standards
if (window.getComputedStyle) {
// convenience methods to turn css case ('background-image') to camel ('backgroundImage')
var pattern = /\-([a-z])/g;
var uc = function (a, b) {
return b.toUpperCase();
};
var camelize = function(string){
return string.replace(pattern, uc);
};
// make sure we're getting a good reference
if (style = window.getComputedStyle(dom, null)) {
var camel, value;
// opera doesn't give back style.length - use truthy since a 0 length may as well be skipped anyways
if (style.length) {
for (var i = 0, l = style.length; i < l; i++) {
name = style[i];
camel = camelize(name);
value = style.getPropertyValue(name);
product[camel] = value;
}
} else {
// opera
for (name in style) {
camel = camelize(name);
value = style.getPropertyValue(name) || style[name];
product[camel] = value;
}
}
}
}
// IE - first try currentStyle, then normal style object - don't bother with runtimeStyle
else if (style = dom.currentStyle) {
for (name in style) {
product[name] = style[name];
}
}
else if (style = dom.style) {
for (name in style) {
if (typeof style[name] != 'function') {
product[name] = style[name];
}
}
}
}
}
// remove any styles specified...
// be careful on blacklist - sometimes vendor-specific values aren't obvious but will be visible... e.g., excepting 'color' will still let '-webkit-text-fill-color' through, which will in fact color the text
if (except && except instanceof Array) {
for (var i = 0, l = except.length; i < l; i++) {
name = except[i];
delete product[name];
}
}
// one way out so we can process blacklist in one spot
return product;
};
// sugar - source is the selector, dom element or jQuery instance to copy from - only and except are optional
$.fn.copyCSS = function(source, only, except) {
var styles = $(source).getStyles(only, except);
this.css(styles);
return this;
};
})(jQuery);