-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular.hammer.js
631 lines (541 loc) · 20.5 KB
/
angular.hammer.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
// ---- Angular Hammer ----
// Copyright (c) 2015 Ryan S Mullins <[email protected]>
// Licensed under the MIT Software License
//
// (fairly heavy) modifications by James Wilson <[email protected]>
//
(function (angular, Hammer) {
'use strict';
// Checking to make sure Hammer and Angular are defined
if (typeof angular === 'undefined') {
throw Error("angular-hammer: AngularJS (angular) is undefined but is necessary.");
}
if (typeof Hammer === 'undefined') {
throw Error("angular-hammer: HammerJS (Hammer) is undefined but is necessary.");
}
/**
* Mapping of the gesture event names with the Angular attribute directive
* names. Follows the form: <directiveName>:<eventName>.
*
* @type {Array}
*/
var gestureTypes = [
'hmCustom:custom',
'hmSwipe:swipe',
'hmSwipeleft:swipeleft',
'hmSwiperight:swiperight',
'hmSwipeup:swipeup',
'hmSwipedown:swipedown',
'hmPan:pan',
'hmPanstart:panstart',
'hmPanmove:panmove',
'hmPanend:panend',
'hmPancancel:pancancel',
'hmPanleft:panleft',
'hmPanright:panright',
'hmPanup:panup',
'hmPandown:pandown',
'hmPress:press',
'hmPressup:pressup',
'hmRotate:rotate',
'hmRotatestart:rotatestart',
'hmRotatemove:rotatemove',
'hmRotateend:rotateend',
'hmRotatecancel:rotatecancel',
'hmPinch:pinch',
'hmPinchstart:pinchstart',
'hmPinchmove:pinchmove',
'hmPinchend:pinchend',
'hmPinchcancel:pinchcancel',
'hmPinchin:pinchin',
'hmPinchout:pinchout',
'hmTap:tap',
'hmDoubletap:doubletap'
];
// ---- Module Definition ----
/**
* @module hmTouchEvents
* @description Angular.js module for adding Hammer.js event listeners to HTML
* elements using attribute directives
* @requires angular
* @requires hammer
*/
var NAME = 'hmTouchEvents';
var hmTouchEvents = angular.module('hmTouchEvents', []);
/**
* Provides a common interface for configuring global manager and recognizer
* options. Allows things like tap duration etc to be defaulted globally and
* overridden on a per-directive basis as needed.
*
* @return {Object} functions to add manager and recognizer options.
*/
hmTouchEvents.provider(NAME, function(){
var self = this;
var defaultRecognizerOpts = false;
var recognizerOptsHash = {};
var managerOpts = {};
//
// In order to use the Hamme rpresets provided, we need
// to map the recognizer fn to some name:
//
var recognizerFnToName = {};
recognizerFnToName[ Hammer.Tap.toString() ] = "tap";
recognizerFnToName[ Hammer.Pan.toString() ] = "pan";
recognizerFnToName[ Hammer.Pinch.toString() ] = "pinch";
recognizerFnToName[ Hammer.Press.toString() ] = "press";
recognizerFnToName[ Hammer.Rotate.toString() ] = "rotate";
recognizerFnToName[ Hammer.Swipe.toString() ] = "swipe";
//
// normalize opts, setting its name as it should be keyed by
// and any must-have options. currently only doubletap is treated
// specially. each _name leads to a new recognizer.
//
function normalizeRecognizerOptions(opts){
opts = angular.copy(opts);
if(opts.event){
if(opts.event == "doubletap"){
opts.type = "tap";
if(!opts.taps) opts.taps = 2;
opts._name = "doubletap";
} else {
opts._name = false;
}
} else {
opts._name = opts.type || false;
}
return opts;
}
//
// create default opts for some eventName.
// again, treat doubletap specially.
//
function defaultOptionsForEvent(eventName){
if(eventName == "custom"){
throw Error(NAME+"Provider: no defaults exist for custom events");
}
var ty = getRecognizerTypeFromeventName(eventName);
return normalizeRecognizerOptions(
eventName == "doubletap"
? {type:ty, event:"doubletap"}
: {type:ty}
);
}
//
// Make use of presets from Hammer.defaults.preset array
// in angular-hammer events.
//
self.applyHammerPresets = function(){
var hammerPresets = Hammer.defaults.preset;
//add every preset that, when normalized, has a _name.
//this precludes most custom events.
angular.forEach(hammerPresets, function(presetArr){
var data = presetArr[1];
if(!data.type) data.type = recognizerFnToName[presetArr[0]];
data = normalizeRecognizerOptions(data);
if(!data._name) return;
recognizerOptsHash[data._name] = data;
});
}
//
// Add a manager option (key/val to extend or object to set all):
//
self.addManagerOption = function(name, val){
if(typeof name == "object"){
angular.extend(managerOpts, name);
}
else {
managerOpts[name] = val;
}
}
//
// Add a recognizer option:
//
self.addRecognizerOption = function(val){
if(Array.isArray(val)){
for(var i = 0; i < val.length; i++) self.addRecognizerOption(val[i]);
return;
}
if(typeof val !== "object"){
throw Error(NAME+"Provider: addRecognizerOption: should be object or array of objects");
}
val = normalizeRecognizerOptions(val);
//hash by name if present, else if no event name,
//set as defaults.
if(val._name){
recognizerOptsHash[val.type] = val;
} else if(!val.event){
defaultRecognizerOpts = val;
}
}
//provide an interface to this that the hm-* directives use
//to extend their recognizer/manager opts.
self.$get = function(){
return {
extendWithDefaultManagerOpts: function(opts){
if(typeof opts != "object"){
opts = {};
} else {
opts = angular.copy(opts);
}
for(var name in managerOpts) {
if(!opts[name]) opts[name] = angular.copy(managerOpts[name]);
}
return opts;
},
extendWithDefaultRecognizerOpts: function(eventName, opts){
if(typeof opts !== "object"){
opts = [];
}
if(!Array.isArray(opts)){
opts = [opts];
}
//dont apply anything if this is custom event
//(beyond normalizing opts to an array):
if(eventName == "custom") return opts;
var recognizerType = getRecognizerTypeFromeventName(eventName);
var specificOpts = recognizerOptsHash[eventName] || recognizerOptsHash[recognizerType];
//get the last opt provided that matches the type or eventName
//that we have. normalizing removes any eventnames we dont care about
//(everything but doubletap at the moment).
var foundOpt;
var isExactMatch = false;
var defaults = angular.extend({}, defaultRecognizerOpts || {}, specificOpts || {});
opts.forEach(function(opt){
if(!opt.event && !opt.type){
return angular.extend(defaults, opt);
}
if(isExactMatch){
return;
}
//more specific wins over less specific.
if(opt.event == eventName){
foundOpt = opt;
isExactMatch = true;
} else if(!opt.event && opt.type == recognizerType){
foundOpt = opt;
}
});
if(!foundOpt) foundOpt = defaultOptionsForEvent(eventName);
else foundOpt = normalizeRecognizerOptions(foundOpt);
return [angular.extend(defaults, foundOpt)];
}
};
};
});
/**
* Iterates through each gesture type mapping and creates a directive for
* each of the
*
* @param {String} type Mapping in the form of <directiveName>:<eventName>
* @return None
*/
angular.forEach(gestureTypes, function (type) {
var directive = type.split(':'),
directiveName = directive[0],
eventName = directive[1];
hmTouchEvents.directive(directiveName, ['$parse', '$window', '$document', NAME, function ($parse, $window, $document, defaultEvents) {
return {
restrict: 'A',
scope: false,
link: function (scope, element, attrs) {
////////////////////////////////////////////////////////////////////////
//console.log(element.children()[0]); //
// //
//var eleme = angular.element(element); //
//console.log(angular.element(eleme.children())); //
//var target = $document[0].getElementsByClassName('target'); //
//console.log(target[0],'--target');
//console.log(attrs.class);
//var elem2 = angular.element(eleme.children()[0]); //
//console.log(elem2.contents()); //
//elem2.css("background-color", "#aa929f"); //
// //
//eleme.css("color","blue"); //
////////////////////////////////////////////////////////////////////////
// Check for Hammer and required functionality.
// error if they arent found as unexpected behaviour otherwise
if (!Hammer || !$window.addEventListener) {
throw Error(NAME+": window.Hammer or window.addEventListener not found, can't add event "+directiveName);
}
var hammer = element.data('hammer'),
managerOpts = defaultEvents.extendWithDefaultManagerOpts( scope.$eval(attrs.hmManagerOptions) ),
recognizerOpts = defaultEvents.extendWithDefaultRecognizerOpts( eventName, scope.$eval(attrs.hmRecognizerOptions) );
// Check for a manager, make one if needed and destroy it when
// the scope is destroyed
if (!hammer) {
hammer = new Hammer.Manager(element[0], managerOpts);
element.data('hammer', hammer);
scope.$on('$destroy', function(){
hammer.destroy();
});
}
// Obtain and wrap our handler function to do a couple of bits for
// us if options provided.
var handlerExpr = $parse(attrs[directiveName]).bind(null,scope);
var handler = function (event) {
event.element = element;
scope.event = event;
// Default invokeApply to true, overridden by recognizer option
var invokeApply = true;
var recognizer = hammer.get(getRecognizerTypeFromeventName(event.type));
if (recognizer) {
var opts = recognizer.options;
if (opts.preventDefault) {
event.preventDefault();
}
if (opts.stopPropagation) {
event.srcEvent.stopPropagation();
}
invokeApply = angular.isUndefined(opts.invokeApply) || opts.invokeApply;
}
if (invokeApply) {
scope.$apply(function(){
handlerExpr({ '$event': event });
});
} else {
handlerExpr({ '$event': event });
}
};
// The recognizer options are normalized to an array. This array
// contains whatever events we wish to add (our prior extending
// takes care of that), but we do a couple of specific things
// depending on this directive so that events play nice together.
angular.forEach(recognizerOpts, function (options) {
if(eventName !== 'custom'){
if (eventName === 'doubletap' && hammer.get('tap')) {
options.recognizeWith = 'tap';
}
else if (options.type == "pan" && hammer.get('swipe')) {
options.recognizeWith = 'swipe';
}
else if (options.type == "pinch" && hammer.get('rotate')) {
options.recognizeWith = 'rotate';
}
}
//add the recognizer with these options:
setupRecognizerWithOptions(
hammer,
applyManagerOptions(managerOpts, options),
element
);
//if custom there may be multiple events to apply, which
//we do here. else, we'll only ever add one.
hammer.on(eventName, handler);
});
}
};
}]);
});
// ---- Private Functions -----
/**
* Adds a gesture recognizer to a given manager. The type of recognizer to
* add is determined by the value of the options.type property.
*
* @param {Object} manager Hammer.js manager object assigned to an element
* @param {String} type Options that define the recognizer to add
* @return {Object} Reference to the new gesture recognizer, if
* successful, null otherwise.
*/
function addRecognizer (manager, name) {
if (manager === undefined || name === undefined) { return null; }
var recognizer;
if (name.indexOf('pan') > -1) {
recognizer = new Hammer.Pan();
} else if (name.indexOf('pinch') > -1) {
recognizer = new Hammer.Pinch();
} else if (name.indexOf('press') > -1) {
recognizer = new Hammer.Press();
} else if (name.indexOf('rotate') > -1) {
recognizer = new Hammer.Rotate();
} else if (name.indexOf('swipe') > -1) {
recognizer = new Hammer.Swipe();
} else {
recognizer = new Hammer.Tap();
}
manager.add(recognizer);
return recognizer;
}
/**
* Applies certain manager options to individual recognizer options.
*
* @param {Object} managerOpts Manager options
* @param {Object} recognizerOpts Recognizer options
* @return None
*/
function applyManagerOptions (managerOpts, recognizerOpts) {
if (managerOpts) {
recognizerOpts.preventGhosts = managerOpts.preventGhosts;
}
return recognizerOpts;
}
/**
* Extracts the type of recognizer that should be instantiated from a given
* event name. Used only when no recognizer options are provided.
*
* @param {String} eventName Name to derive the recognizer type from
* @return {string} Type of recognizer that fires events with that name
*/
function getRecognizerTypeFromeventName (eventName) {
if (eventName.indexOf('pan') > -1) {
return 'pan';
} else if (eventName.indexOf('pinch') > -1) {
return 'pinch';
} else if (eventName.indexOf('press') > -1) {
return 'press';
} else if (eventName.indexOf('rotate') > -1) {
return 'rotate';
} else if (eventName.indexOf('swipe') > -1) {
return 'swipe';
} else if (eventName.indexOf('tap') > -1) {
return 'tap';
} else {
return "custom";
}
}
/**
* Applies the passed options object to the appropriate gesture recognizer.
* Recognizers are created if they do not already exist. See the README for a
* description of the options object that can be passed to this function.
*
* @param {Object} manager Hammer.js manager object assigned to an element
* @param {Object} options Options applied to a recognizer managed by manager
* @return None
*/
function setupRecognizerWithOptions (manager, options, element) {
if (manager == null || options == null || options.type == null) {
return console.error('ERROR: Angular Hammer could not setup the' +
' recognizer. Values of the passed manager and options: ', manager, options);
}
var recognizer = manager.get(options._name);
if (!recognizer) {
recognizer = addRecognizer(manager, options._name);
}
if (!options.directions) {
if (options._name === 'pan' || options._name === 'swipe') {
options.directions = 'DIRECTION_ALL';
} else if (options._name.indexOf('left') > -1) {
options.directions = 'DIRECTION_LEFT';
} else if (options._name.indexOf('right') > -1) {
options.directions = 'DIRECTION_RIGHT';
} else if (options._name.indexOf('up') > -1) {
options.directions = 'DIRECTION_UP';
} else if (options._name.indexOf('down') > -1) {
options.directions = 'DIRECTION_DOWN';
} else {
options.directions = '';
}
}
options.direction = parseDirections(options.directions);
recognizer.set(options);
if (typeof options.recognizeWith === 'string') {
var recognizeWithRecognizer;
if (manager.get(options.recognizeWith) == null){
recognizeWithRecognizer = addRecognizer(manager, options.recognizeWith);
}
if (recognizeWithRecognizer != null) {
recognizer.recognizeWith(recognizeWithRecognizer);
}
}
if (typeof options.dropRecognizeWith === 'string' &&
manager.get(options.dropRecognizeWith) != null) {
recognizer.dropRecognizeWith(manager.get(options.dropRecognizeWith));
}
if (typeof options.requireFailure === 'string') {
var requireFailureRecognizer;
if (manager.get(options.requireFailure) == null){
requireFailureRecognizer = addRecognizer(manager, {type:options.requireFailure});
}
if (requireFailureRecognizer != null) {
recognizer.requireFailure(requireFailureRecognizer);
}
}
if (typeof options.dropRequireFailure === 'string' &&
manager.get(options.dropRequireFailure) != null) {
recognizer.dropRequireFailure(manager.get(options.dropRequireFailure));
}
if (options.preventGhosts === true && element != null) {
preventGhosts(element);
}
}
/**
* Parses the value of the directions property of any Angular Hammer options
* object and converts them into the standard Hammer.js directions values.
*
* @param {String} dirs Direction names separated by '|' characters
* @return {Number} Hammer.js direction value
*/
function parseDirections (dirs) {
var directions = 0;
angular.forEach(dirs.split('|'), function (direction) {
if (Hammer.hasOwnProperty(direction)) {
directions = directions | Hammer[direction];
}
});
return directions;
}
// ---- Preventing Ghost Clicks ----
/**
* Modified from: https://gist.github.com/jtangelder/361052976f044200ea17
*
* Prevent click events after a touchend.
*
* Inspired/copy-paste from this article of Google by Ryan Fioravanti
* https://developers.google.com/mobile/articles/fast_buttons#ghost
*/
function preventGhosts (element) {
if (!element) { return; }
var coordinates = [],
threshold = 25,
timeout = 2500;
if ('ontouchstart' in window) {
element[0].addEventListener('touchstart', resetCoordinates, true);
element[0].addEventListener('touchend', registerCoordinates, true);
element[0].addEventListener('click', preventGhostClick, true);
element[0].addEventListener('mouseup', preventGhostClick, true);
}
/**
* prevent clicks if they're in a registered XY region
* @param {MouseEvent} ev
*/
function preventGhostClick (ev) {
for (var i = 0; i < coordinates.length; i++) {
var x = coordinates[i][0];
var y = coordinates[i][1];
// within the range, so prevent the click
if (Math.abs(ev.clientX - x) < threshold &&
Math.abs(ev.clientY - y) < threshold) {
ev.stopPropagation();
ev.preventDefault();
break;
}
}
}
/**
* reset the coordinates array
*/
function resetCoordinates () {
coordinates = [];
}
/**
* remove the first coordinates set from the array
*/
function popCoordinates () {
coordinates.splice(0, 1);
}
/**
* if it is an final touchend, we want to register it's place
* @param {TouchEvent} ev
*/
function registerCoordinates (ev) {
// touchend is triggered on every releasing finger
// changed touches always contain the removed touches on a touchend
// the touches object might contain these also at some browsers (firefox os)
// so touches - changedTouches will be 0 or lower, like -1, on the final touchend
if(ev.touches.length - ev.changedTouches.length <= 0) {
var touch = ev.changedTouches[0];
coordinates.push([touch.clientX, touch.clientY]);
setTimeout(popCoordinates, timeout);
}
}
}
})(angular, Hammer);