This repository has been archived by the owner on May 10, 2022. It is now read-only.
forked from randallb/angular-hammer
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathangular-hammer.js
84 lines (74 loc) · 2.56 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
/*
* angular-hammer v1.2.4
* (c) 2013 Monospaced http://monospaced.com
* License: MIT
*/
(function(window, angular, Hammer){
var hmTouchEvents = angular.module('hmTouchEvents', []),
hmGestures = ['hmHold:hold',
'hmTap:tap',
'hmPress:press',
'hmDoubletap:doubletap',
'hmDrag:drag',
'hmDragstart:dragstart',
'hmDragend:dragend',
'hmDragup:dragup',
'hmDragdown:dragdown',
'hmDragleft:dragleft',
'hmDragright:dragright',
'hmSwipe:swipe',
'hmSwipeup:swipeup',
'hmSwipedown:swipedown',
'hmSwipeleft:swipeleft',
'hmSwiperight:swiperight',
'hmTransform:transform',
'hmTransformstart:transformstart',
'hmTransformend:transformend',
'hmRotate:rotate',
'hmPinch:pinch',
'hmPinchin:pinchin',
'hmPinchout:pinchout',
'hmTouch:touch',
'hmRelease:release'];
angular.forEach(hmGestures, function(name){
var directive = name.split(':'),
directiveName = directive[0],
eventName = directive[1];
hmTouchEvents.directive(directiveName, ['$parse', '$window', function($parse, $window){
return {
restrict: 'A, C',
link: function(scope, element, attr) {
var expr = $parse(attr[directiveName]),
fn = function(event){
scope.$apply(function() {
expr(scope, {$event: event});
});
},
opts = $parse(attr['hmOptions'])(scope, {}),
hammer;
if (typeof Hammer === 'undefined' || !$window.addEventListener) {
// fallback to mouse events where appropriate
if (directiveName === 'hmTap') {
element.bind('click', fn);
}
if (directiveName === 'hmDoubletap') {
element.bind('dblclick', fn);
}
return;
}
// don't create multiple Hammer instances per element
if (!(hammer = element.data('hammer'))) {
hammer = Hammer(element[0], opts);
element.data('hammer', hammer);
}
// bind Hammer touch event
hammer.on(eventName, fn);
// unbind Hammer touch event
scope.$on('$destroy', function(){
hammer.off(eventName, fn);
});
}
};
}]);
});
})(window, window.angular, window.Hammer);