-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathunidragger.js
289 lines (235 loc) · 7.76 KB
/
unidragger.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
/*!
* Unidragger v3.0.1
* Draggable base class
* MIT license
*/
( function( window, factory ) {
// universal module definition
if ( typeof module == 'object' && module.exports ) {
// CommonJS
module.exports = factory(
window,
require('ev-emitter'),
);
} else {
// browser global
window.Unidragger = factory(
window,
window.EvEmitter,
);
}
}( typeof window != 'undefined' ? window : this, function factory( window, EvEmitter ) {
function Unidragger() {}
// inherit EvEmitter
let proto = Unidragger.prototype = Object.create( EvEmitter.prototype );
// ----- bind start ----- //
// trigger handler methods for events
proto.handleEvent = function( event ) {
let method = 'on' + event.type;
if ( this[ method ] ) {
this[ method ]( event );
}
};
let startEvent, activeEvents;
if ( 'ontouchstart' in window ) {
// HACK prefer Touch Events as you can preventDefault on touchstart to
// disable scroll in iOS & mobile Chrome metafizzy/flickity#1177
startEvent = 'touchstart';
activeEvents = [ 'touchmove', 'touchend', 'touchcancel' ];
} else if ( window.PointerEvent ) {
// Pointer Events
startEvent = 'pointerdown';
activeEvents = [ 'pointermove', 'pointerup', 'pointercancel' ];
} else {
// mouse events
startEvent = 'mousedown';
activeEvents = [ 'mousemove', 'mouseup' ];
}
// prototype so it can be overwriteable by Flickity
proto.touchActionValue = 'none';
proto.bindHandles = function() {
this._bindHandles( 'addEventListener', this.touchActionValue );
};
proto.unbindHandles = function() {
this._bindHandles( 'removeEventListener', '' );
};
/**
* Add or remove start event
* @param {String} bindMethod - addEventListener or removeEventListener
* @param {String} touchAction - value for touch-action CSS property
*/
proto._bindHandles = function( bindMethod, touchAction ) {
this.handles.forEach( ( handle ) => {
handle[ bindMethod ]( startEvent, this );
handle[ bindMethod ]( 'click', this );
// touch-action: none to override browser touch gestures. metafizzy/flickity#540
if ( window.PointerEvent ) handle.style.touchAction = touchAction;
} );
};
proto.bindActivePointerEvents = function() {
activeEvents.forEach( ( eventName ) => {
window.addEventListener( eventName, this );
} );
};
proto.unbindActivePointerEvents = function() {
activeEvents.forEach( ( eventName ) => {
window.removeEventListener( eventName, this );
} );
};
// ----- event handler helpers ----- //
// trigger method with matching pointer
proto.withPointer = function( methodName, event ) {
if ( event.pointerId === this.pointerIdentifier ) {
this[ methodName ]( event, event );
}
};
// trigger method with matching touch
proto.withTouch = function( methodName, event ) {
let touch;
for ( let changedTouch of event.changedTouches ) {
if ( changedTouch.identifier === this.pointerIdentifier ) {
touch = changedTouch;
}
}
if ( touch ) this[ methodName ]( event, touch );
};
// ----- start event ----- //
proto.onmousedown = function( event ) {
this.pointerDown( event, event );
};
proto.ontouchstart = function( event ) {
this.pointerDown( event, event.changedTouches[0] );
};
proto.onpointerdown = function( event ) {
this.pointerDown( event, event );
};
// nodes that have text fields
const cursorNodes = [ 'TEXTAREA', 'INPUT', 'SELECT', 'OPTION' ];
// input types that do not have text fields
const clickTypes = [ 'radio', 'checkbox', 'button', 'submit', 'image', 'file' ];
/**
* any time you set `event, pointer` it refers to:
* @param {Event} event
* @param {Event | Touch} pointer
*/
proto.pointerDown = function( event, pointer ) {
// dismiss multi-touch taps, right clicks, and clicks on text fields
let isCursorNode = cursorNodes.includes( event.target.nodeName );
let isClickType = clickTypes.includes( event.target.type );
let isOkayElement = !isCursorNode || isClickType;
let isOkay = !this.isPointerDown && !event.button && isOkayElement;
if ( !isOkay ) return;
this.isPointerDown = true;
// save pointer identifier to match up touch events
this.pointerIdentifier = pointer.pointerId !== undefined ?
// pointerId for pointer events, touch.indentifier for touch events
pointer.pointerId : pointer.identifier;
// track position for move
this.pointerDownPointer = {
pageX: pointer.pageX,
pageY: pointer.pageY,
};
this.bindActivePointerEvents();
this.emitEvent( 'pointerDown', [ event, pointer ] );
};
// ----- move ----- //
proto.onmousemove = function( event ) {
this.pointerMove( event, event );
};
proto.onpointermove = function( event ) {
this.withPointer( 'pointerMove', event );
};
proto.ontouchmove = function( event ) {
this.withTouch( 'pointerMove', event );
};
proto.pointerMove = function( event, pointer ) {
let moveVector = {
x: pointer.pageX - this.pointerDownPointer.pageX,
y: pointer.pageY - this.pointerDownPointer.pageY,
};
this.emitEvent( 'pointerMove', [ event, pointer, moveVector ] );
// start drag if pointer has moved far enough to start drag
let isDragStarting = !this.isDragging && this.hasDragStarted( moveVector );
if ( isDragStarting ) this.dragStart( event, pointer );
if ( this.isDragging ) this.dragMove( event, pointer, moveVector );
};
// condition if pointer has moved far enough to start drag
proto.hasDragStarted = function( moveVector ) {
return Math.abs( moveVector.x ) > 3 || Math.abs( moveVector.y ) > 3;
};
// ----- drag ----- //
proto.dragStart = function( event, pointer ) {
this.isDragging = true;
this.isPreventingClicks = true; // set flag to prevent clicks
this.emitEvent( 'dragStart', [ event, pointer ] );
};
proto.dragMove = function( event, pointer, moveVector ) {
this.emitEvent( 'dragMove', [ event, pointer, moveVector ] );
};
// ----- end ----- //
proto.onmouseup = function( event ) {
this.pointerUp( event, event );
};
proto.onpointerup = function( event ) {
this.withPointer( 'pointerUp', event );
};
proto.ontouchend = function( event ) {
this.withTouch( 'pointerUp', event );
};
proto.pointerUp = function( event, pointer ) {
this.pointerDone();
this.emitEvent( 'pointerUp', [ event, pointer ] );
if ( this.isDragging ) {
this.dragEnd( event, pointer );
} else {
// pointer didn't move enough for drag to start
this.staticClick( event, pointer );
}
};
proto.dragEnd = function( event, pointer ) {
this.isDragging = false; // reset flag
// re-enable clicking async
setTimeout( () => delete this.isPreventingClicks );
this.emitEvent( 'dragEnd', [ event, pointer ] );
};
// triggered on pointer up & pointer cancel
proto.pointerDone = function() {
this.isPointerDown = false;
delete this.pointerIdentifier;
this.unbindActivePointerEvents();
this.emitEvent('pointerDone');
};
// ----- cancel ----- //
proto.onpointercancel = function( event ) {
this.withPointer( 'pointerCancel', event );
};
proto.ontouchcancel = function( event ) {
this.withTouch( 'pointerCancel', event );
};
proto.pointerCancel = function( event, pointer ) {
this.pointerDone();
this.emitEvent( 'pointerCancel', [ event, pointer ] );
};
// ----- click ----- //
// handle all clicks and prevent clicks when dragging
proto.onclick = function( event ) {
if ( this.isPreventingClicks ) event.preventDefault();
};
// triggered after pointer down & up with no/tiny movement
proto.staticClick = function( event, pointer ) {
// ignore emulated mouse up clicks
let isMouseup = event.type === 'mouseup';
if ( isMouseup && this.isIgnoringMouseUp ) return;
this.emitEvent( 'staticClick', [ event, pointer ] );
// set flag for emulated clicks 300ms after touchend
if ( isMouseup ) {
this.isIgnoringMouseUp = true;
// reset flag after 400ms
setTimeout( () => {
delete this.isIgnoringMouseUp;
}, 400 );
}
};
// ----- ----- //
return Unidragger;
} ) );