forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate_event.js
More file actions
84 lines (76 loc) · 2.86 KB
/
Copy pathsimulate_event.js
File metadata and controls
84 lines (76 loc) · 2.86 KB
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
function simulate(element, eventName) {
var defaults = extend({}, defaultOptions);
var options = extend(defaults, arguments[2] || {});
var oEvent, eventType = null;
for (var name in eventMatchers) {
if (eventMatchers[name].test(eventName)) { eventType = name; break; }
}
if (!eventType) {
throw new SyntaxError('Only HTMLEvents and MouseEvents interfaces are supported');
}
if (document.createEvent) {
oEvent = document.createEvent(eventType);
if (eventType == 'HTMLEvents') {
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
}
else if (eventType === 'MouseEvents') {
oEvent.initMouseEvent(eventName, options.bubbles, options.cancelable, document.defaultView, options.detail, options.pointerX, options.pointerY, options.pointerX, options.pointerY, options.ctrlKey, options.altKey, options.shiftKey, options.metaKey, options.button, element);
} else if (eventType === 'KeyboardEvent') {
defaults = extend({}, defaultKeyboardOptions);
options = extend(defaults, arguments[2] || {});
if (oEvent.initKeyEvent) {
oEvent.initKeyEvent(eventName, options.bubbles, options.cancelable, document.defaultView, options.ctrl, options.shift, options.alt, options.meta, options.keyCode, options.charCode);
} else {
oEvent = document.createEvent("Events");
// initKeyboardEvent doesn't work property in Chrome, fudge it using plain event
oEvent.initEvent(eventName, options.bubbles, options.cancelable);
oEvent.keyCode = options.keyCode;
oEvent.which = options.keyCode;
}
}
element.dispatchEvent(oEvent);
} else {
options.clientX = options.pointerX;
options.clientY = options.pointerY;
var evt = document.createEventObject();
oEvent = extend(evt, options);
element.fireEvent('on' + eventName, oEvent);
}
return element;
}
function extend(destination, source) {
for (var property in source)
destination[property] = source[property];
return destination;
}
var eventMatchers = {
'HTMLEvents': /^(?:load|unload|abort|error|select|change|submit|reset|focus|blur|resize|scroll)$/,
'MouseEvents': /^(?:click|dblclick|mouse(?:down|up|over|move|out|wheel))$/,
'KeyboardEvent': /^(?:keydown|keypress|keyup)$/
};
var defaultOptions = {
pointerX: 16,
pointerY: 16,
detail: 0,
button: 0,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
bubbles: true,
cancelable: true
};
var defaultKeyboardOptions = {
bubbles: true,
cancelable: true,
charCode: 0,
keyCode: 0,
location: 0,
modifiers: '',
repeat: false,
locale: '',
ctrl: false,
alt: false,
shift: false,
meta: false
};