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 17
/
Copy pathhappen.js
154 lines (144 loc) · 6.06 KB
/
happen.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
/* global module jQuery */
!(function() {
var h = {},
events = {
mouse: ['click', 'mousedown', 'mouseup', 'mousemove',
'mouseover', 'mouseout'],
key: ['keydown', 'keyup', 'keypress'],
touch:['touchstart', 'touchmove', 'touchend']
},
s, i;
// Make inheritance bearable: clone one level of properties
function extend(child, parent) {
for (var property in parent) {
if (typeof child[property] == 'undefined') {
child[property] = parent[property];
}
}
return child;
}
// IE<9 doesn't support indexOf
function has(x, y) {
for (var i = 0; i < x.length; i++) if (x[i] == y) return true;
return false;
}
h.makeEvent = function(o) {
var evt;
if (has(events.key, o.type)) {
if (typeof Event === 'function') {
evt = new Event(o.type, { bubbles: true });
evt.keyCode = o.keyCode || 0;
evt.charCode = o.charCode || 0;
evt.shiftKey = o.shiftKey || false;
evt.metaKey = o.metaKey || false;
evt.ctrlKey = o.ctrlKey || false;
evt.altKey = o.altKey || false;
evt.relatedTarget = o.relatedTarget;
} else {
evt = document.createEvent('KeyboardEvent');
// https://developer.mozilla.org/en/DOM/event.initKeyEvent
// https://developer.mozilla.org/en/DOM/KeyboardEvent
evt[(evt.initKeyEvent) ? 'initKeyEvent'
: 'initKeyboardEvent'](
o.type, // in DOMString typeArg,
true, // in boolean canBubbleArg,
true, // in boolean cancelableArg,
null, // in nsIDOMAbstractView viewArg, Specifies UIEvent.view. This value may be null.
o.ctrlKey || false, // in boolean ctrlKeyArg,
o.altKey || false, // in boolean altKeyArg,
o.shiftKey || false, // in boolean shiftKeyArg,
o.metaKey || false, // in boolean metaKeyArg,
o.keyCode || 0, // in unsigned long keyCodeArg,
o.charCode || 0 // in unsigned long charCodeArg);
);
// Workaround for https://bugs.webkit.org/show_bug.cgi?id=16735
if (evt.ctrlKey != (o.ctrlKey || 0) ||
evt.altKey != (o.altKey || 0) ||
evt.shiftKey != (o.shiftKey || 0) ||
evt.metaKey != (o.metaKey || 0) ||
evt.keyCode != (o.keyCode || 0) ||
evt.charCode != (o.charCode || 0)) {
evt = document.createEvent('Event');
evt.initEvent(o.type, true, true);
evt.ctrlKey = o.ctrlKey || false;
evt.altKey = o.altKey || false;
evt.shiftKey = o.shiftKey || false;
evt.metaKey = o.metaKey || false;
evt.keyCode = o.keyCode || 0;
evt.charCode = o.charCode || 0;
}
}
} else {
if (typeof document.createEvent === 'undefined' &&
typeof document.createEventObject !== 'undefined') {
evt = document.createEventObject();
extend(evt, o);
} else if (typeof document.createEvent !== 'undefined') {
if (has(events.touch, o.type)) {
evt = document.createEvent('UIEvent');
evt.initUIEvent(o.type, true, true, window, o.detail || 1);
extend(evt, o);
} else {
// both MouseEvent and MouseEvents work in Chrome
evt = document.createEvent('MouseEvents');
// https://developer.mozilla.org/en/DOM/event.initMouseEvent
evt.initMouseEvent(o.type,
true, // canBubble
true, // cancelable
window, // 'AbstractView'
o.detail || 0, // click count or mousewheel detail
o.screenX || 0, // screenX
o.screenY || 0, // screenY
o.clientX || 0, // clientX
o.clientY || 0, // clientY
o.ctrlKey || 0, // ctrl
o.altKey || false, // alt
o.shiftKey || false, // shift
o.metaKey || false, // meta
o.button || false, // mouse button
o.relatedTarget // relatedTarget
);
}
}
}
return evt;
};
h.dispatchEvent = function(x, evt) {
// not ie before 9
if (typeof x.dispatchEvent !== 'undefined') {
x.dispatchEvent(evt);
} else if (typeof x.fireEvent !== 'undefined') {
x.fireEvent('on' + evt.type, evt);
}
};
h.once = function(x, o) {
h.dispatchEvent(x, h.makeEvent(o || {}));
};
for (var type in events) {
if (!events.hasOwnProperty(type)) continue;
var shortcuts = events[type];
for (i = 0; i < shortcuts.length; i++) {
s = shortcuts[i];
h[s] = (function(s) {
return function(x, o) {
h.once(x, extend(o || {}, { type: s }));
};
})(s);
}
}
h.dblclick = function(x, o) {
h.once(x, extend(o || {}, { type: 'dblclick', detail: 2 }));
};
if (typeof window !== 'undefined') window.happen = h;
if (typeof module !== 'undefined') module.exports = h;
// Provide jQuery plugin
if (typeof jQuery !== 'undefined' && jQuery.fn) {
jQuery.fn.happen = function(o) {
if (typeof o === 'string') o = { type: o };
for (var i = 0; i < this.length; i++) {
h.once(this[i], o);
}
return this;
};
}
})(this);