-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
330 lines (270 loc) · 10.2 KB
/
index.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'use strict';
var EventEmitter2 = require( 'eventemitter2' ).EventEmitter2;
/*
dependencies
*/
/* binding */
var bindingMethod = window.addEventListener ? 'addEventListener' : 'attachEvent';
var eventPrefix = bindingMethod !== 'addEventListener' ? 'on' : '';
function bind( el, type, fn, capture ) {
el[ bindingMethod ]( eventPrefix + type, fn, capture || false );
return fn;
}
/* matching */
var vendorMatch = Element.prototype.matches || Element.prototype.webkitMatchesSelector || Element.prototype.mozMatchesSelector || Element.prototype.msMatchesSelector || Element.prototype.oMatchesSelector;
function matches( el, selector ) {
if ( !el || el.nodeType !== 1 ) {
return false;
}
if ( vendorMatch ) {
return vendorMatch.call( el, selector );
}
var nodes = document.querySelectorAll( selector, el.parentNode );
for ( var i = 0; i < nodes.length; ++i ) {
if ( nodes[ i ] === el ) {
return true;
}
}
return false;
}
/* closest */
function closest( element, selector, checkSelf, root ) {
element = checkSelf ? {parentNode: element} : element;
root = root || document;
/* Make sure `element !== document` and `element != null`
otherwise we get an illegal invocation */
while ( ( element = element.parentNode ) && element !== document ) {
if ( matches( element, selector ) ) {
return element;
}
/* After `matches` on the edge case that
the selector matches the root
(when the root is not the document) */
if (element === root) {
return;
}
}
}
/*
end dependencies
*/
function Emit() {
var self = this;
EventEmitter2.call( self );
self.validators = [];
self.touchMoveDelta = 10;
self.initialTouchPoint = null;
bind( document, 'touchstart', self.handleEvent.bind( self ) );
bind( document, 'touchmove', self.handleEvent.bind( self ) );
bind( document, 'touchend', self.handleEvent.bind( self ) );
bind( document, 'click', self.handleEvent.bind( self ) );
bind( document, 'input', self.handleEvent.bind( self ) );
bind( document, 'submit', self.handleEvent.bind( self ) );
}
Emit.prototype = Object.create( EventEmitter2.prototype );
function getTouchDelta( event, initial ) {
var deltaX = ( event.touches[ 0 ].pageX - initial.x );
var deltaY = ( event.touches[ 0 ].pageY - initial.y );
return Math.sqrt( ( deltaX * deltaX ) + ( deltaY * deltaY ) );
}
Emit.prototype.handleEvent = function( event ) {
var self = this;
var touches = event.touches;
var delta = -1;
if ( typeof event.propagationStoppedAt !== 'number' || isNaN( event.propagationStoppedAt ) ) {
event.propagationStoppedAt = 100; // highest possible value
}
switch ( event.type ) {
case 'touchstart':
self.initialTouchPoint = self.lastTouchPoint = {
x: touches && touches.length ? touches[ 0 ].pageX : 0,
y: touches && touches.length ? touches[ 0 ].pageY : 0
};
break;
case 'touchmove':
if ( touches && touches.length && self.initialTouchPoint ) {
delta = getTouchDelta( event, self.initialTouchPoint );
if ( delta > self.touchMoveDelta ) {
self.initialTouchPoint = null;
}
self.lastTouchPoint = {
x: touches[ 0 ].pageX,
y: touches[ 0 ].pageY
};
}
break;
case 'click':
case 'touchend':
case 'input':
case 'submit':
// eat any late-firing click events on touch devices
if ( event.type === 'click' && self.lastTouchPoint ) {
if ( event.touches && event.touches.length ) {
delta = getTouchDelta( event, self.lastTouchPoint );
if ( delta < self.touchMoveDelta ) {
event.preventDefault();
event.stopPropagation();
return;
}
}
}
// handle canceling touches that have moved too much
if ( event.type === 'touchend' && !self.initialTouchPoint ) {
return;
}
var selector = 'a,button,input,[data-emit]';
var originalElement = event.target || event.srcElement;
var el = originalElement;
var depth = -1;
var handled = false;
while( el && event.propagationStoppedAt > depth && ++depth < 100 ) {
event.emitTarget = el;
event.depth = depth;
if ( !el.hasAttribute( 'data-emit' ) ) {
// if it's a link, button or input and it has no emit attribute, allow the event to pass
if ( el.tagName === 'A' || el.tagName === 'BUTTON' || el.tagName === 'INPUT' ) {
return;
}
else {
el = closest( el, selector, false, document );
continue;
}
}
var forceAllowDefault = el.tagName === 'INPUT' && ( el.type === 'checkbox' || el.type === 'radio' );
var validated = true;
for ( var validatorIndex = 0; validatorIndex < self.validators.length; ++validatorIndex ) {
if ( !self.validators[ validatorIndex ].call( this, el, event ) ) {
validated = false;
break;
}
}
// eat the event if a validator failed
if ( !validated ) {
event.preventDefault();
event.stopPropagation();
event.propagationStoppedAt = depth;
el = null;
continue;
}
if ( typeof( self.validate ) === 'function' && !self.validate.call( self, el ) ) {
el = closest( el, selector, false, document );
continue;
}
if ( el.tagName === 'FORM' ) {
if ( event.type !== 'submit' ) {
el = closest( el, selector, false, document );
continue;
}
}
else if ( el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' ) {
if ( !( el.type === 'submit' || el.type === 'checkbox' || el.type === 'radio' || el.type === 'file' ) && event.type !== 'input' ) {
el = closest( el, selector, false, document );
continue;
}
}
else if ( el.tagName === 'SELECT' ) {
if ( event.type !== 'input' ) {
el = closest( el, selector, false, document );
continue;
}
}
handled |= self._emit( el, event, forceAllowDefault );
el = closest( el, selector, false, document );
}
if ( !handled ) {
self.emit( 'unhandled', event );
}
else if ( depth >= 100 ) {
throw new Error( 'Exceeded depth limit for Emit calls.' );
}
self.initialTouchPoint = null;
break;
}
};
Emit.prototype._emit = function( element, event, forceDefault ) {
var self = this;
var optionString = element.getAttribute( 'data-emit-options' );
var options = {};
var ignoreString = element.getAttribute( 'data-emit-ignore' );
var i;
if ( ignoreString && ignoreString.length ) {
var ignoredEvents = ignoreString.toLowerCase().split( ' ' );
for ( i = 0; i < ignoredEvents.length; ++i ) {
if ( event.type === ignoredEvents[ i ] ) {
return false;
}
}
}
if ( optionString && optionString.length ) {
var opts = optionString.toLowerCase().split( ' ' );
for ( i = 0; i < opts.length; ++i ) {
options[ opts[ i ] ] = true;
}
}
if ( !forceDefault && !options.allowdefault ) {
event.preventDefault();
}
if ( !options.allowpropagate ) {
event.stopPropagation();
event.propagationStoppedAt = event.depth;
}
var emissionList = element.getAttribute( 'data-emit' );
if ( !emissionList ) {
// allow for empty behaviors that catch events
return true;
}
var emissions = emissionList.split( ',' );
if ( options.debounce ) {
self.timeouts = self.timeouts || {};
if ( self.timeouts[ element ] ) {
clearTimeout( self.timeouts[ element ] );
}
(function() {
var _element = element;
var _emissions = emissions;
var _event = event;
self.timeouts[ element ] = setTimeout( function() {
_emissions.forEach( function( emission ) {
self.emit( emission, _event );
} );
clearTimeout( self.timeouts[ _element ] );
self.timeouts[ _element ] = null;
}, 250 );
} )();
return true;
}
emissions.forEach( function( emission ) {
self.emit( emission, event );
} );
return true;
};
Emit.prototype.addValidator = function( validator ) {
var self = this;
var found = false;
for ( var i = 0; i < self.validators.length; ++i ) {
if ( self.validators[ i ] === validator ) {
found = true;
break;
}
}
if ( found ) {
return false;
}
self.validators.push( validator );
return true;
};
Emit.prototype.removeValidator = function( validator ) {
var self = this;
var found = false;
for ( var i = 0; i < self.validators.length; ++i ) {
if ( self.validators[ i ] === validator ) {
self.validators.splice( i, 1 );
found = true;
break;
}
}
return found;
};
Emit.singleton = Emit.singleton || new Emit();
Emit.singleton.Emit = Emit;
module.exports = Emit.singleton;