This repository has been archived by the owner on Dec 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
multiEvent.js
223 lines (169 loc) · 7.17 KB
/
multiEvent.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
/*! MultiEvent 0.7.0 | Copyright (c) 2013 Ryan Fitzer | License: (http://www.opensource.org/licenses/mit-license.php) */
/*jshint laxcomma:true, asi:true */
( function ( root, factory ) {
if ( typeof define === 'function' && define.amd ) {
define(factory );
} else {
root.multiEvent = factory( root.detect );
}
}( this, function () {
var debug = false;
var behaviors = {
on: {
pointer: 'MSPointerDown',
touch: 'touchstart',
// Why not mousedown? FF on Win8 fires a both the touchstart and mousedown events.
// Not so when when the click event is used. Event.preventDefault does
// not fix the issue.
mouse: 'click'
},
off: {
pointer: 'MSPointerUp',
touch: 'touchend',
mouse: 'mouseup'
},
over: {
pointer: 'MSPointerOver',
mouse: 'mouseover'
},
out: {
pointer: 'MSPointerOut',
mouse: 'mouseout'
}
}
var detect = {
// Overly simplified. See https://github.com/Modernizr/Modernizr/blob/master/feature-detects/touchevents.js
// TODO: Replace with Modernizr.
hasTouch: function() {
if ( 'Modernizr' in window ) return Modernizr.touch;
return ( ( 'ontouchstart' in window ) || window.DocumentTouch && document instanceof DocumentTouch );
},
// Overly simplified. See https://github.com/Modernizr/Modernizr/blob/master/src/isEventSupported.js
// TODO: Replace with Modernizr.
hasEvent: function( name ) {
return ( 'on' + name.toLowerCase() in document.documentElement );
}
};
function log() {
if ( debug ) console.log( arguments );
}
/**
* Provide the proper event(s) for the current environment. Make sure to use `event#preventDefault` in the handlers to prevent multiple events from firing in hybrid enviroments.
* @param type {String|Object} Event type string or event types object
* @returns {Array} Supported events types
*/
function resolveEvents( types ) {
var events = []
, hasPointer = window.navigator.msPointerEnabled
;
var push = function( eventTypes ) {
eventTypes = eventTypes.isArray ? eventTypes : [ eventTypes ];
eventTypes.forEach( function( item ) {
events.push( item );
});
return events;
}
// Return only the pointers, if supported.
if ( types.pointer && hasPointer ) return push( types.pointer );
if ( types.touch && detect.hasTouch ) push( types.touch );
if ( types.mouse ) {
if ( !types.mouse.isArray && detect.hasEvent( types.mouse ) ) events.push( types.mouse );
else {
if ( !types.mouse.isArray ) events.push( types.mouse );
else {
types.mouse.forEach( function( item ) {
if ( detect.hasEvent( item ) ) events.push( item );
});
}
}
}
return events;
}
function MultiEvent( type, isDebug ) {
debug = isDebug;
// Did they supply their own types object?
if ( typeof type === 'object' ) {
this.types = type;
this.behavior = 'custom';
}
else {
this.types = behaviors[ type ];
this.behavior = type;
}
this.events = resolveEvents( this.types );
return this;
}
MultiEvent.prototype = {
/**
* Determine the event source.
* @param evt {Object} The event object (can also be a jQuery event object).
* @returns {Object} An object of source types with boolean values
*/
resolve: function( evt ) {
var types = this.types
, evtType = evt.type
, origEvent = evt.originalEvent || evt
;
// http://msdn.microsoft.com/en-us/library/ie/hh772103.aspx
var msSrc = origEvent.pointerType
, msTouch = origEvent.MSPOINTER_TYPE_TOUCH
, msMouse = origEvent.MSPOINTER_TYPE_MOUSE
;
// https://developer.mozilla.org/en-US/docs/DOM/event.mozInputSource
var mozSrc = origEvent.mozInputSource
, mozTouch = origEvent.MOZ_SOURCE_TOUCH
, mozMouse = origEvent.MOZ_SOURCE_MOUSE
;
this.isMatch = true;
this.isTouch = false;
this.isMouse = false;
switch( evtType ) {
case types.pointer:
log( 'switch case: pointer; event: ', types.pointer );
if ( msSrc === msTouch ) this.isTouch = true;
if ( msSrc === msMouse ) this.isMouse = true;
break;
case types.touch:
log( 'switch case: types.touch' );
this.isTouch = true;
break;
case types.mouse:
log( 'switch case: mouse; event: ', types.mouse );
// `mozSrc` can be `0` when the source could not be
// determined. If that's the case, default to `source.mouse`
if ( mozSrc ) {
log('entered mozSrc');
if ( mozSrc === mozTouch ) {
this.isTouch = true;
this.isMatch = false;
}
if ( mozSrc === mozMouse ) this.isMouse = true;
break;
}
else if ( types.mouse === 'mouseover' || types.mouse === 'mouseout' ) {
// Not perfect, but Chrome on Win8 and IOS can fire these
// events when touched. If it's a mouse, these values are
// non-zero 99.99% of the time and the event continuosly fires.
// With a touch event on IOS, these will be undefined.
// TODO: Test iPad with mouse.
if ( 'webkitMovementX' in origEvent ) {
log( 'entered webkitMovementX' );
if ( !origEvent.webkitMovementX && !origEvent.webkitMovementY ) {
this.isTouch = true;
this.isMatch = false;
break;
}
}
}
default:
log( 'switch case: default; event: ', evtType );
this.isMouse = true;
break;
}
return this;
}
}
return function( type ) {
return new MultiEvent( type );
}
}));