-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathangular-hammer.js
79 lines (66 loc) · 1.64 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
;(function (__global, angular, hammer) {
'use strict';
if (!angular) {
throw new Error("window.angular is not defined.");
}
if (!Hammer) {
throw new Error("window.Hammer is not defined.");
}
var GESTURES = [
'hold',
'tap',
'doubletap',
'drag',
'dragstart',
'dragend',
'dragup',
'dragdown',
'dragleft',
'dragright',
'swipe',
'swipeup',
'swipedown',
'swipeleft',
'swiperight',
'transform',
'transformstart',
'transformend',
'rotate',
'pinch',
'pinchin',
'pinchout',
'touch',
'release'
];
// Create clean scope
var newScope;
angular.injector(['ng']).invoke(['$rootScope', function($rootScope) {
newScope = $rootScope.$new();
}]);
// Create module
var module = angular.module('hammer', []);
GESTURES.forEach(function (gesture) {
var hammerGesture = 'hammer' + gesture[0].toUpperCase() + gesture.slice(1);
module.directive(hammerGesture, ['$parse', function ($parse) {
return function (scope, element, attr) {
var args = newScope.$eval(attr[hammerGesture]),
tapHandler,
options = null,
instance;
if (typeof args === 'undefined') {
tapHandler = $parse(attr[hammerGesture]);
} else {
tapHandler = $parse(args.fn);
delete args.fn;
options = args;
}
instance = hammer(element[0], options);
instance.on(gesture, function (e) {
scope.$apply(function () {
tapHandler(scope, { $event: e });
});
});
};
}]);
});
}(window, window.angular, window.Hammer));