|
1 |
| -!(function(context, $) { |
2 |
| - var h = {}; |
| 1 | +!(function(context) { |
| 2 | + var h = {}, |
| 3 | + events = { |
| 4 | + mouse: ['click', 'mousedown', 'mouseup', 'mousemove', |
| 5 | + 'mouseover', 'mouseout'], |
| 6 | + key: ['keydown', 'keyup', 'keypress'] |
| 7 | + }, |
| 8 | + s, i; |
3 | 9 |
|
4 | 10 | // Make inheritance bearable: clone one level of properties
|
5 | 11 | function extend(child, parent) {
|
|
11 | 17 | return child;
|
12 | 18 | }
|
13 | 19 |
|
14 |
| - h.once = function(x, o) { |
15 |
| - var evt; |
| 20 | + // IE<9 doesn't support indexOf |
| 21 | + function has(x, y) { |
| 22 | + for (var i = 0; i < x.length; i++) if (x[i] == y) return true; |
| 23 | + return false; |
| 24 | + } |
16 | 25 |
|
17 |
| - if (o.type.slice(0, 3) === 'key') { |
| 26 | + h.makeEvent = function(o) { |
| 27 | + var evt; |
| 28 | + if (has(events.key, o.type)) { |
18 | 29 | if (typeof Event === 'function') {
|
19 | 30 | evt = new Event(o.type);
|
20 | 31 | evt.keyCode = o.keyCode || 0;
|
|
59 | 70 | }
|
60 | 71 | }
|
61 | 72 | } else {
|
62 |
| - evt = document.createEvent('MouseEvents'); |
63 |
| - // https://developer.mozilla.org/en/DOM/event.initMouseEvent |
64 |
| - evt.initMouseEvent(o.type, |
65 |
| - true, // canBubble |
66 |
| - true, // cancelable |
67 |
| - window, // 'AbstractView' |
68 |
| - o.clicks || 0, // click count |
69 |
| - o.screenX || 0, // screenX |
70 |
| - o.screenY || 0, // screenY |
71 |
| - o.clientX || 0, // clientX |
72 |
| - o.clientY || 0, // clientY |
73 |
| - o.ctrlKey || 0, // ctrl |
74 |
| - o.altKey || false, // alt |
75 |
| - o.shiftKey || false, // shift |
76 |
| - o.metaKey || false, // meta |
77 |
| - o.button || false, // mouse button |
78 |
| - null // relatedTarget |
79 |
| - ); |
| 73 | + if (typeof document.createEvent === 'undefined' && |
| 74 | + typeof document.createEventObject !== 'undefined') { |
| 75 | + evt = document.createEventObject(); |
| 76 | + extend(evt, o); |
| 77 | + } else if (typeof document.createEvent !== 'undefined') { |
| 78 | + // both MouseEvent and MouseEvents work in Chrome |
| 79 | + evt = document.createEvent('MouseEvents'); |
| 80 | + // https://developer.mozilla.org/en/DOM/event.initMouseEvent |
| 81 | + evt.initMouseEvent(o.type, |
| 82 | + true, // canBubble |
| 83 | + true, // cancelable |
| 84 | + window, // 'AbstractView' |
| 85 | + o.detail || 0, // click count or mousewheel detail |
| 86 | + o.screenX || 0, // screenX |
| 87 | + o.screenY || 0, // screenY |
| 88 | + o.clientX || 0, // clientX |
| 89 | + o.clientY || 0, // clientY |
| 90 | + o.ctrlKey || 0, // ctrl |
| 91 | + o.altKey || false, // alt |
| 92 | + o.shiftKey || false, // shift |
| 93 | + o.metaKey || false, // meta |
| 94 | + o.button || false, // mouse button |
| 95 | + null // relatedTarget |
| 96 | + ); |
| 97 | + } |
80 | 98 | }
|
| 99 | + return evt; |
| 100 | + }; |
81 | 101 |
|
82 |
| - x.dispatchEvent(evt); |
| 102 | + h.dispatchEvent = function(x, evt) { |
| 103 | + // not ie before 9 |
| 104 | + if (typeof x.dispatchEvent !== 'undefined') { |
| 105 | + x.dispatchEvent(evt); |
| 106 | + } else if (typeof x.fireEvent !== 'undefined') { |
| 107 | + x.fireEvent('on' + evt.type, evt); |
| 108 | + } |
83 | 109 | };
|
84 | 110 |
|
85 |
| - var shortcuts = ['click', 'mousedown', 'mouseup', 'mousemove', |
86 |
| - 'mouseover', 'mouseout', 'keydown', 'keyup', 'keypress'], |
87 |
| - s, i = 0; |
| 111 | + h.once = function(x, o) { |
| 112 | + h.dispatchEvent(x, h.makeEvent(o || {})); |
| 113 | + }; |
88 | 114 |
|
89 |
| - while (s = shortcuts[i++]) { |
90 |
| - h[s] = (function(s) { |
91 |
| - return function(x, o) { |
92 |
| - h.once(x, extend(o || {}, { type: s })); |
93 |
| - }; |
94 |
| - })(s); |
| 115 | + for (var type in events) { |
| 116 | + if (!events.hasOwnProperty(type)) continue; |
| 117 | + var shortcuts = events[type]; |
| 118 | + for (i = 0; i < shortcuts.length; i++) { |
| 119 | + s = shortcuts[i]; |
| 120 | + h[s] = (function(s) { |
| 121 | + return function(x, o) { |
| 122 | + h.once(x, extend(o || {}, { type: s })); |
| 123 | + }; |
| 124 | + })(s); |
| 125 | + } |
95 | 126 | }
|
96 | 127 |
|
97 | 128 | h.dblclick = function(x, o) {
|
98 |
| - h.once(x, extend(o || {}, { |
99 |
| - type: 'dblclick', |
100 |
| - clicks: 2 |
101 |
| - })); |
| 129 | + h.once(x, extend(o || {}, { type: 'dblclick', detail: 2 })); |
102 | 130 | };
|
103 | 131 |
|
104 |
| - this.happen = h; |
105 |
| - |
106 |
| - // Export for nodejs |
107 |
| - if (typeof module !== 'undefined') { |
108 |
| - module.exports = this.happen; |
109 |
| - } |
| 132 | + if (typeof window !== 'undefined') window.happen = h; |
| 133 | + if (typeof module !== 'undefined') module.exports = h; |
110 | 134 |
|
111 | 135 | // Provide jQuery plugin
|
112 |
| - if ($ && $.fn) { |
113 |
| - $.fn.happen = function(o) { |
114 |
| - if (typeof o === 'string') { |
115 |
| - o = { type: o }; |
116 |
| - } |
| 136 | + if (typeof jQuery !== 'undefined' && jQuery.fn) { |
| 137 | + jQuery.fn.happen = function(o) { |
| 138 | + if (typeof o === 'string') o = { type: o }; |
117 | 139 | for (var i = 0; i < this.length; i++) {
|
118 | 140 | happen.once(this[i], o);
|
119 | 141 | }
|
120 | 142 | return this;
|
121 | 143 | };
|
122 | 144 | }
|
123 |
| -})(this, (typeof jQuery !== 'undefined') ? jQuery : null); |
| 145 | +})(this); |
0 commit comments