forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngEventDirs.js
75 lines (62 loc) · 2.08 KB
/
ngEventDirs.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
'use strict';
/* global ngTouch: false, POINTER_EVENTS: false, getPointerEventNames: false */
/*
* A collection of directives that allows creation of custom event handlers that are defined as
* angular expressions and are compiled and executed within the current scope.
*/
var ngTouchEventDirectives = {};
// Duplicate from the ng module...
var forceAsyncEvents = {
'blur': true,
'focus': true
};
// Duplicated from jqLite.
var SPECIAL_CHARS_REGEXP = /([\:\-\_]+(.))/g;
var MOZ_HACK_REGEXP = /^moz([A-Z])/;
/**
* Converts snake_case to camelCase.
* Also there is special case for Moz prefix starting with upper case letter.
* @param name Name to normalize
*/
function camelCase(name) {
return name.
replace(SPECIAL_CHARS_REGEXP, function(_, separator, letter, offset) {
return offset ? letter.toUpperCase() : letter;
}).
replace(MOZ_HACK_REGEXP, 'Moz$1');
}
angular.forEach(Object.keys(POINTER_EVENTS.mouse),
function (eventType) {
var eventName = POINTER_EVENTS.mouse[eventType];
var directiveName = camelCase('ng-' + eventName);
ngTouch.config(['$provide', function ($provide) {
$provide.decorator(directiveName + 'Directive', ['$delegate', function ($delegate) {
// drop the default mouse directives
$delegate.shift();
return $delegate;
}]);
}]);
ngTouchEventDirectives[directiveName] = ['$parse', '$rootScope', function ($parse, $rootScope) {
return {
restrict: 'A',
compile: function ($element, attr) {
var fn = $parse(attr[directiveName]);
return function ngEventHandler(scope, element) {
//
element.on(getPointerEventNames(eventType), function (event) {
var callback = function () {
fn(scope, {$event: event});
};
if (forceAsyncEvents[eventName] && $rootScope.$$phase) {
scope.$evalAsync(callback);
} else {
scope.$apply(callback);
}
});
};
}
};
}];
}
);
ngTouch.directive(ngTouchEventDirectives);