This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
gesture.js
825 lines (742 loc) · 27.7 KB
/
gesture.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
var HANDLERS = {};
/**
* The state of the current 'pointer'. The pointer represents the state of the current touch.
* It contains normalized x and y coordinates from DOM events,
* as well as other information abstracted from the DOM.
*/
var pointer, lastPointer, maxClickDistance = 6;
var forceSkipClickHijack = false, disableAllGestures = false;
/**
* The position of the most recent click if that click was on a label element.
* @type {{x: number, y: number}|null}
*/
var lastLabelClickPos = null;
/**
* Used to attach event listeners once when multiple ng-apps are running.
* @type {boolean}
*/
var isInitialized = false;
/**
* @ngdoc module
* @name material.core.gestures
* @description
* AngularJS Material Gesture handling for touch devices.
* This module replaced the usage of the HammerJS library.
*/
angular
.module('material.core.gestures', [])
.provider('$mdGesture', MdGestureProvider)
.factory('$$MdGestureHandler', MdGestureHandler)
.run(attachToDocument);
/**
* @ngdoc service
* @name $mdGestureProvider
* @module material.core.gestures
*
* @description
* In some scenarios on mobile devices (without jQuery), the click events should NOT be hijacked.
* `$mdGestureProvider` is used to configure the Gesture module to ignore or skip click hijacking
* on mobile devices.
*
* You can also change the max click distance, `6px` by default, if you have issues on some touch
* screens.
*
* <hljs lang="js">
* app.config(function($mdGestureProvider) {
*
* // For mobile devices without jQuery loaded, do not
* // intercept click events during the capture phase.
* $mdGestureProvider.skipClickHijack();
*
* // If hijacking clicks, you may want to change the default click distance
* $mdGestureProvider.setMaxClickDistance(12);
* });
* </hljs>
*
*/
function MdGestureProvider() { }
MdGestureProvider.prototype = {
/**
* @ngdoc method
* @name $mdGestureProvider#disableAll
*
* @description
* Disable all gesture detection. This can be beneficial to application performance
* and memory usage.
*/
disableAll: function () {
disableAllGestures = true;
},
// Publish access to setter to configure a variable BEFORE the
// $mdGesture service is instantiated...
/**
* @ngdoc method
* @name $mdGestureProvider#skipClickHijack
*
* @description
* Tell the AngularJS Material Gesture module to skip (or ignore) click hijacking on mobile devices.
*/
skipClickHijack: function() {
return forceSkipClickHijack = true;
},
/**
* @ngdoc method
* @name $mdGestureProvider#setMaxClickDistance
* @param clickDistance {string} Distance in pixels. I.e. `12px`.
* @description
* Set the max distance from the origin of the touch event to trigger touch handlers.
*/
setMaxClickDistance: function(clickDistance) {
maxClickDistance = parseInt(clickDistance);
},
/**
* $get is used to build an instance of $mdGesture
* @ngInject
*/
$get : function($$MdGestureHandler, $$rAF, $timeout, $mdUtil) {
return new MdGesture($$MdGestureHandler, $$rAF, $timeout, $mdUtil);
}
};
/**
* MdGesture factory construction function
* @ngInject
*/
function MdGesture($$MdGestureHandler, $$rAF, $timeout, $mdUtil) {
var touchActionProperty = $mdUtil.getTouchAction();
var hasJQuery = (typeof window.jQuery !== 'undefined') && (angular.element === window.jQuery);
var self = {
handler: addHandler,
register: register,
isAndroid: $mdUtil.isAndroid,
isIos: $mdUtil.isIos,
// On mobile w/out jQuery, we normally intercept clicks. Should we skip that?
isHijackingClicks: ($mdUtil.isIos || $mdUtil.isAndroid) && !hasJQuery && !forceSkipClickHijack
};
if (self.isHijackingClicks) {
self.handler('click', {
options: {
maxDistance: maxClickDistance
},
onEnd: checkDistanceAndEmit('click')
});
self.handler('focus', {
options: {
maxDistance: maxClickDistance
},
onEnd: function(ev, pointer) {
if (pointer.distance < this.state.options.maxDistance && canFocus(ev.target)) {
this.dispatchEvent(ev, 'focus', pointer);
ev.target.focus();
}
}
});
self.handler('mouseup', {
options: {
maxDistance: maxClickDistance
},
onEnd: checkDistanceAndEmit('mouseup')
});
self.handler('mousedown', {
onStart: function(ev) {
this.dispatchEvent(ev, 'mousedown');
}
});
}
function checkDistanceAndEmit(eventName) {
return function(ev, pointer) {
if (pointer.distance < this.state.options.maxDistance) {
this.dispatchEvent(ev, eventName, pointer);
}
};
}
/**
* Register an element to listen for a handler.
* This allows an element to override the default options for a handler.
* Additionally, some handlers like drag and hold only dispatch events if
* the domEvent happens inside an element that's registered to listen for these events.
*
* @see GestureHandler for how overriding of default options works.
* @example $mdGesture.register(myElement, 'drag', { minDistance: 20, horizontal: false })
*/
function register(element, handlerName, options) {
var handler = HANDLERS[handlerName.replace(/^\$md./, '')];
if (!handler) {
throw new Error('Failed to register element with handler ' + handlerName + '. ' +
'Available handlers: ' + Object.keys(HANDLERS).join(', '));
}
return handler.registerElement(element, options);
}
/*
* add a handler to $mdGesture. see below.
*/
function addHandler(name, definition) {
var handler = new $$MdGestureHandler(name);
angular.extend(handler, definition);
HANDLERS[name] = handler;
return self;
}
/**
* Register handlers. These listen to touch/start/move events, interpret them,
* and dispatch gesture events depending on options & conditions. These are all
* instances of GestureHandler.
* @see GestureHandler
*/
return self
/*
* The press handler dispatches an event on touchdown/touchend.
* It's a simple abstraction of touch/mouse/pointer start and end.
*/
.handler('press', {
onStart: function (ev, pointer) {
this.dispatchEvent(ev, '$md.pressdown');
},
onEnd: function (ev, pointer) {
this.dispatchEvent(ev, '$md.pressup');
}
})
/*
* The hold handler dispatches an event if the user keeps their finger within
* the same <maxDistance> area for <delay> ms.
* The hold handler will only run if a parent of the touch target is registered
* to listen for hold events through $mdGesture.register()
*/
.handler('hold', {
options: {
maxDistance: 6,
delay: 500
},
onCancel: function () {
$timeout.cancel(this.state.timeout);
},
onStart: function (ev, pointer) {
// For hold, require a parent to be registered with $mdGesture.register()
// Because we prevent scroll events, this is necessary.
if (!this.state.registeredParent) return this.cancel();
this.state.pos = {x: pointer.x, y: pointer.y};
this.state.timeout = $timeout(angular.bind(this, function holdDelayFn() {
this.dispatchEvent(ev, '$md.hold');
this.cancel(); // we're done!
}), this.state.options.delay, false);
},
onMove: function (ev, pointer) {
// Don't scroll while waiting for hold.
// If we don't preventDefault touchmove events here, Android will assume we don't
// want to listen to anymore touch events. It will start scrolling and stop sending
// touchmove events.
if (!touchActionProperty && ev.type === 'touchmove') ev.preventDefault();
// If the user moves greater than <maxDistance> pixels, stop the hold timer
// set in onStart
var dx = this.state.pos.x - pointer.x;
var dy = this.state.pos.y - pointer.y;
if (Math.sqrt(dx * dx + dy * dy) > this.options.maxDistance) {
this.cancel();
}
},
onEnd: function () {
this.onCancel();
}
})
/*
* The drag handler dispatches a drag event if the user holds and moves his finger greater than
* <minDistance> px in the x or y direction, depending on options.horizontal.
* The drag will be cancelled if the user moves his finger greater than <minDistance>*<cancelMultiplier> in
* the perpendicular direction. Eg if the drag is horizontal and the user moves his finger <minDistance>*<cancelMultiplier>
* pixels vertically, this handler won't consider the move part of a drag.
*/
.handler('drag', {
options: {
minDistance: 6,
horizontal: true,
cancelMultiplier: 1.5
},
/**
* @param {angular.JQLite} element where touch action styles need to be adjusted
* @param {{horizontal: boolean}=} options object whose horizontal property can specify to
* apply 'pan-y' or 'pan-x' touch actions.
*/
onSetup: function(element, options) {
if (touchActionProperty) {
// We check for horizontal to be false, because otherwise we would overwrite the default opts.
this.oldTouchAction = element[0].style[touchActionProperty];
element[0].style[touchActionProperty] = options.horizontal ? 'pan-y' : 'pan-x';
}
},
/**
* @param {angular.JQLite} element where styles need to be cleaned up
*/
onCleanup: function(element) {
if (this.oldTouchAction) {
element[0].style[touchActionProperty] = this.oldTouchAction;
} else {
element[0].style[touchActionProperty] = null;
}
},
onStart: function (ev) {
// For drag, require a parent to be registered with $mdGesture.register()
if (!this.state.registeredParent) this.cancel();
},
onMove: function (ev, pointer) {
var shouldStartDrag, shouldCancel;
// Don't scroll while deciding if this touchmove qualifies as a drag event.
// If we don't preventDefault touchmove events here, Android will assume we don't
// want to listen to anymore touch events. It will start scrolling and stop sending
// touchmove events.
if (!touchActionProperty && ev.type === 'touchmove') ev.preventDefault();
if (!this.state.dragPointer) {
if (this.state.options.horizontal) {
shouldStartDrag = Math.abs(pointer.distanceX) > this.state.options.minDistance;
shouldCancel = Math.abs(pointer.distanceY) > this.state.options.minDistance * this.state.options.cancelMultiplier;
} else {
shouldStartDrag = Math.abs(pointer.distanceY) > this.state.options.minDistance;
shouldCancel = Math.abs(pointer.distanceX) > this.state.options.minDistance * this.state.options.cancelMultiplier;
}
if (shouldStartDrag) {
// Create a new pointer representing this drag, starting at this point where the drag started.
this.state.dragPointer = makeStartPointer(ev);
updatePointerState(ev, this.state.dragPointer);
this.dispatchEvent(ev, '$md.dragstart', this.state.dragPointer);
} else if (shouldCancel) {
this.cancel();
}
} else {
this.dispatchDragMove(ev);
}
},
// Only dispatch dragmove events every frame; any more is unnecessary
dispatchDragMove: $$rAF.throttle(function (ev) {
// Make sure the drag didn't stop while waiting for the next frame
if (this.state.isRunning) {
updatePointerState(ev, this.state.dragPointer);
this.dispatchEvent(ev, '$md.drag', this.state.dragPointer);
}
}),
onEnd: function (ev, pointer) {
if (this.state.dragPointer) {
updatePointerState(ev, this.state.dragPointer);
this.dispatchEvent(ev, '$md.dragend', this.state.dragPointer);
}
}
})
/*
* The swipe handler will dispatch a swipe event if, on the end of a touch,
* the velocity and distance were high enough.
*/
.handler('swipe', {
options: {
minVelocity: 0.65,
minDistance: 10
},
onEnd: function (ev, pointer) {
var eventType;
if (Math.abs(pointer.velocityX) > this.state.options.minVelocity &&
Math.abs(pointer.distanceX) > this.state.options.minDistance) {
eventType = pointer.directionX == 'left' ? '$md.swipeleft' : '$md.swiperight';
this.dispatchEvent(ev, eventType);
}
else if (Math.abs(pointer.velocityY) > this.state.options.minVelocity &&
Math.abs(pointer.distanceY) > this.state.options.minDistance) {
eventType = pointer.directionY == 'up' ? '$md.swipeup' : '$md.swipedown';
this.dispatchEvent(ev, eventType);
}
}
});
}
/**
* MdGestureHandler
* A GestureHandler is an object which is able to dispatch custom dom events
* based on native dom {touch,pointer,mouse}{start,move,end} events.
*
* A gesture will manage its lifecycle through the start,move,end, and cancel
* functions, which are called by native dom events.
*
* A gesture has the concept of 'options' (eg. a swipe's required velocity), which can be
* overridden by elements registering through $mdGesture.register().
*/
function GestureHandler (name) {
this.name = name;
this.state = {};
}
function MdGestureHandler() {
var hasJQuery = (typeof window.jQuery !== 'undefined') && (angular.element === window.jQuery);
GestureHandler.prototype = {
options: {},
// jQuery listeners don't work with custom DOMEvents, so we have to dispatch events
// differently when jQuery is loaded
dispatchEvent: hasJQuery ? jQueryDispatchEvent : nativeDispatchEvent,
// These are overridden by the registered handler
onSetup: angular.noop,
onCleanup: angular.noop,
onStart: angular.noop,
onMove: angular.noop,
onEnd: angular.noop,
onCancel: angular.noop,
// onStart sets up a new state for the handler, which includes options from the
// nearest registered parent element of ev.target.
start: function (ev, pointer) {
if (this.state.isRunning) return;
var parentTarget = this.getNearestParent(ev.target);
// Get the options from the nearest registered parent
var parentTargetOptions = parentTarget && parentTarget.$mdGesture[this.name] || {};
this.state = {
isRunning: true,
// Override the default options with the nearest registered parent's options
options: angular.extend({}, this.options, parentTargetOptions),
// Pass in the registered parent node to the state so the onStart listener can use
registeredParent: parentTarget
};
this.onStart(ev, pointer);
},
move: function (ev, pointer) {
if (!this.state.isRunning) return;
this.onMove(ev, pointer);
},
end: function (ev, pointer) {
if (!this.state.isRunning) return;
this.state.isRunning = false;
this.onEnd(ev, pointer);
},
cancel: function (ev, pointer) {
this.onCancel(ev, pointer);
this.state = {};
},
// Find and return the nearest parent element that has been registered to
// listen for this handler via $mdGesture.register(element, 'handlerName').
getNearestParent: function (node) {
var current = node;
while (current) {
if ((current.$mdGesture || {})[this.name]) {
return current;
}
current = current.parentNode;
}
return null;
},
// Called from $mdGesture.register when an element registers itself with a handler.
// Store the options the user gave on the DOMElement itself. These options will
// be retrieved with getNearestParent when the handler starts.
registerElement: function (element, options) {
var self = this;
element[0].$mdGesture = element[0].$mdGesture || {};
element[0].$mdGesture[this.name] = options || {};
element.on('$destroy', onDestroy);
self.onSetup(element, options || {});
return onDestroy;
function onDestroy() {
delete element[0].$mdGesture[self.name];
element.off('$destroy', onDestroy);
self.onCleanup(element, options || {});
}
}
};
return GestureHandler;
/**
* Dispatch an event with jQuery
* TODO: Make sure this sends bubbling events
*
* @param srcEvent the original DOM touch event that started this.
* @param eventType the name of the custom event to send (eg 'click' or '$md.drag')
* @param eventPointer the pointer object that matches this event.
*/
function jQueryDispatchEvent(srcEvent, eventType, eventPointer) {
eventPointer = eventPointer || pointer;
var eventObj = new angular.element.Event(eventType);
eventObj.$material = true;
eventObj.pointer = eventPointer;
eventObj.srcEvent = srcEvent;
angular.extend(eventObj, {
clientX: eventPointer.x,
clientY: eventPointer.y,
screenX: eventPointer.x,
screenY: eventPointer.y,
pageX: eventPointer.x,
pageY: eventPointer.y,
ctrlKey: srcEvent.ctrlKey,
altKey: srcEvent.altKey,
shiftKey: srcEvent.shiftKey,
metaKey: srcEvent.metaKey
});
angular.element(eventPointer.target).trigger(eventObj);
}
/**
* NOTE: nativeDispatchEvent is very performance sensitive.
* @param srcEvent the original DOM touch event that started this.
* @param eventType the name of the custom event to send (eg 'click' or '$md.drag')
* @param eventPointer the pointer object that matches this event.
*/
function nativeDispatchEvent(srcEvent, eventType, eventPointer) {
eventPointer = eventPointer || pointer;
var eventObj;
if (eventType === 'click' || eventType === 'mouseup' || eventType === 'mousedown') {
if (typeof window.MouseEvent === "function") {
eventObj = new MouseEvent(eventType, {
bubbles: true,
cancelable: true,
screenX: Number(srcEvent.screenX),
screenY: Number(srcEvent.screenY),
clientX: Number(eventPointer.x),
clientY: Number(eventPointer.y),
ctrlKey: srcEvent.ctrlKey,
altKey: srcEvent.altKey,
shiftKey: srcEvent.shiftKey,
metaKey: srcEvent.metaKey,
button: srcEvent.button,
buttons: srcEvent.buttons,
relatedTarget: srcEvent.relatedTarget || null
});
} else {
eventObj = document.createEvent('MouseEvents');
// This has been deprecated
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/initMouseEvent
eventObj.initMouseEvent(
eventType, true, true, window, srcEvent.detail,
eventPointer.x, eventPointer.y, eventPointer.x, eventPointer.y,
srcEvent.ctrlKey, srcEvent.altKey, srcEvent.shiftKey, srcEvent.metaKey,
srcEvent.button, srcEvent.relatedTarget || null
);
}
} else {
if (typeof window.CustomEvent === "function") {
eventObj = new CustomEvent(eventType, {
bubbles: true,
cancelable: true,
detail: {}
});
} else {
// This has been deprecated
// https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/initCustomEvent
eventObj = document.createEvent('CustomEvent');
eventObj.initCustomEvent(eventType, true, true, {});
}
}
eventObj.$material = true;
eventObj.pointer = eventPointer;
eventObj.srcEvent = srcEvent;
eventPointer.target.dispatchEvent(eventObj);
}
}
/**
* Attach Gestures: hook document and check shouldHijack clicks
* @ngInject
*/
function attachToDocument($mdGesture, $$MdGestureHandler, $mdUtil) {
if (disableAllGestures) {
return;
}
if (!isInitialized && $mdGesture.isHijackingClicks) {
/*
* If hijack clicks is true, we preventDefault any click that wasn't
* sent by AngularJS Material. This is because on older Android & iOS, a false, or 'ghost',
* click event will be sent ~400ms after a touchend event happens.
* The only way to know if this click is real is to prevent any normal
* click events, and add a flag to events sent by material so we know not to prevent those.
*
* Two exceptions to click events that should be prevented are:
* - click events sent by the keyboard (eg form submit)
* - events that originate from an Ionic app
*/
document.addEventListener('click' , clickHijacker , true);
document.addEventListener('mouseup' , mouseInputHijacker, true);
document.addEventListener('mousedown', mouseInputHijacker, true);
document.addEventListener('focus' , mouseInputHijacker, true);
isInitialized = true;
}
function mouseInputHijacker(ev) {
var isKeyClick = !ev.clientX && !ev.clientY;
if (
!isKeyClick &&
!ev.$material &&
!ev.isIonicTap &&
!isInputEventFromLabelClick(ev) &&
(ev.type !== 'mousedown' || (!canFocus(ev.target) && !canFocus(document.activeElement)))
) {
ev.preventDefault();
ev.stopPropagation();
}
}
/**
* Ignore click events that don't come from AngularJS Material, Ionic, Input Label clicks,
* or key presses that generate click events. This helps to ignore the ghost tap events on
* older mobile browsers that get sent after a 300-400ms delay.
* @param ev MouseEvent or modified MouseEvent with $material, pointer, and other fields
*/
function clickHijacker(ev) {
var isKeyClick;
if ($mdUtil.isIos) {
isKeyClick = angular.isDefined(ev.webkitForce) && ev.webkitForce === 0;
} else {
isKeyClick = ev.clientX === 0 && ev.clientY === 0;
}
if (!isKeyClick && !ev.$material && !ev.isIonicTap && !isInputEventFromLabelClick(ev)) {
ev.preventDefault();
ev.stopPropagation();
lastLabelClickPos = null;
} else {
lastLabelClickPos = null;
if (ev.target.tagName.toLowerCase() === 'label') {
lastLabelClickPos = {x: ev.x, y: ev.y};
}
}
}
// Listen to all events to cover all platforms.
var START_EVENTS = 'mousedown touchstart pointerdown';
var MOVE_EVENTS = 'mousemove touchmove pointermove';
var END_EVENTS = 'mouseup mouseleave touchend touchcancel pointerup pointercancel';
angular.element(document)
.on(START_EVENTS, gestureStart)
.on(MOVE_EVENTS, gestureMove)
.on(END_EVENTS, gestureEnd)
// For testing
.on('$$mdGestureReset', function gestureClearCache () {
lastPointer = pointer = null;
});
/**
* When a DOM event happens, run all registered gesture handlers' lifecycle
* methods which match the DOM event.
* Eg. when a 'touchstart' event happens, runHandlers('start') will call and
* run `handler.cancel()` and `handler.start()` on all registered handlers.
*/
function runHandlers(handlerEvent, event) {
var handler;
for (var name in HANDLERS) {
handler = HANDLERS[name];
if (handler instanceof $$MdGestureHandler) {
if (handlerEvent === 'start') {
// Run cancel to reset any handlers' state
handler.cancel();
}
handler[handlerEvent](event, pointer);
}
}
}
/*
* gestureStart vets if a start event is legitimate (and not part of a 'ghost click' from iOS/Android)
* If it is legitimate, we initiate the pointer state and mark the current pointer's type
* For example, for a touchstart event, mark the current pointer as a 'touch' pointer, so mouse events
* won't effect it.
*/
function gestureStart(ev) {
// If we're already touched down, abort
if (pointer) return;
var now = +Date.now();
// iOS & old android bug: after a touch event, a click event is sent 350 ms later.
// If <400ms have passed, don't allow an event of a different type than the previous event
if (lastPointer && !typesMatch(ev, lastPointer) && (now - lastPointer.endTime < 1500)) {
return;
}
pointer = makeStartPointer(ev);
runHandlers('start', ev);
}
/**
* If a move event happens of the right type, update the pointer and run all the move handlers.
* "of the right type": if a mousemove happens but our pointer started with a touch event, do
* nothing.
*/
function gestureMove(ev) {
if (!pointer || !typesMatch(ev, pointer)) return;
updatePointerState(ev, pointer);
runHandlers('move', ev);
}
/**
* If an end event happens of the right type, update the pointer, run endHandlers, and save the
* pointer as 'lastPointer'.
*/
function gestureEnd(ev) {
if (!pointer || !typesMatch(ev, pointer)) return;
updatePointerState(ev, pointer);
pointer.endTime = +Date.now();
if (ev.type !== 'pointercancel') {
runHandlers('end', ev);
}
lastPointer = pointer;
pointer = null;
}
}
// ********************
// Module Functions
// ********************
/*
* Initiate the pointer. x, y, and the pointer's type.
*/
function makeStartPointer(ev) {
var point = getEventPoint(ev);
var startPointer = {
startTime: +Date.now(),
target: ev.target,
// 'p' for pointer events, 'm' for mouse, 't' for touch
type: ev.type.charAt(0)
};
startPointer.startX = startPointer.x = point.pageX;
startPointer.startY = startPointer.y = point.pageY;
return startPointer;
}
/*
* return whether the pointer's type matches the event's type.
* Eg if a touch event happens but the pointer has a mouse type, return false.
*/
function typesMatch(ev, pointer) {
return ev && pointer && ev.type.charAt(0) === pointer.type;
}
/**
* Gets whether the given event is an input event that was caused by clicking on an
* associated label element.
*
* This is necessary because the browser will, upon clicking on a label element, fire an
* *extra* click event on its associated input (if any). mdGesture is able to flag the label
* click as with `$material` correctly, but not the second input click.
*
* In order to determine whether an input event is from a label click, we compare the (x, y) for
* the event to the (x, y) for the most recent label click (which is cleared whenever a non-label
* click occurs). Unfortunately, there are no event properties that tie the input and the label
* together (such as relatedTarget).
*
* @param {MouseEvent} event
* @returns {boolean}
*/
function isInputEventFromLabelClick(event) {
return lastLabelClickPos
&& lastLabelClickPos.x === event.x
&& lastLabelClickPos.y === event.y;
}
/*
* Update the given pointer based upon the given DOMEvent.
* Distance, velocity, direction, duration, etc
*/
function updatePointerState(ev, pointer) {
var point = getEventPoint(ev);
var x = pointer.x = point.pageX;
var y = pointer.y = point.pageY;
pointer.distanceX = x - pointer.startX;
pointer.distanceY = y - pointer.startY;
pointer.distance = Math.sqrt(
pointer.distanceX * pointer.distanceX + pointer.distanceY * pointer.distanceY
);
pointer.directionX = pointer.distanceX > 0 ? 'right' : pointer.distanceX < 0 ? 'left' : '';
pointer.directionY = pointer.distanceY > 0 ? 'down' : pointer.distanceY < 0 ? 'up' : '';
pointer.duration = +Date.now() - pointer.startTime;
pointer.velocityX = pointer.distanceX / pointer.duration;
pointer.velocityY = pointer.distanceY / pointer.duration;
}
/**
* Normalize the point where the DOM event happened whether it's touch or mouse.
* @returns point event obj with pageX and pageY on it.
*/
function getEventPoint(ev) {
ev = ev.originalEvent || ev; // support jQuery events
return (ev.touches && ev.touches[0]) ||
(ev.changedTouches && ev.changedTouches[0]) ||
ev;
}
/** Checks whether an element can be focused. */
function canFocus(element) {
return (
!!element &&
element.getAttribute('tabindex') !== '-1' &&
!element.hasAttribute('disabled') &&
(
element.hasAttribute('tabindex') ||
element.hasAttribute('href') ||
element.isContentEditable ||
['INPUT', 'SELECT', 'BUTTON', 'TEXTAREA', 'VIDEO', 'AUDIO'].indexOf(element.nodeName) !== -1
)
);
}