forked from IamAdamJowett/angular-click-outside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclickoutside.directive.js
108 lines (87 loc) · 4.69 KB
/
clickoutside.directive.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
/*global angular, navigator*/
(function() {
'use strict';
angular
.module('angular-click-outside', [])
.directive('clickOutside', ['$document', '$parse', '$timeout', clickOutside]);
function clickOutside($document, $parse, $timeout) {
return {
restrict: 'A',
link: function($scope, elem, attr) {
// postpone linking to next digest to allow for unique id generation
$timeout(function() {
var classList = (attr.outsideIfNot !== undefined) ? attr.outsideIfNot.split(/[ ,]+/) : [],
fn;
// add the elements id so it is not counted in the click listening
if (attr.id !== undefined) {
classList.push(attr.id);
}
function eventHandler(e) {
var i,
element,
r,
id,
classNames,
l;
// check if our element already hidden and abort if so
if (angular.element(elem).hasClass("ng-hide")) {
return;
}
// if there is no click target, no point going on
if (!e || !e.target) {
return;
}
// loop through the available elements, looking for classes in the class list that might match and so will eat
for (element = e.target; element; element = element.parentNode) {
id = element.id,
classNames = element.className,
l = classList.length;
// Unwrap SVGAnimatedString classes
if (classNames && classNames.baseVal !== undefined) {
classNames = classNames.baseVal;
}
// if there are no class names on the element clicked, skip the check
if (classNames || id) {
// console.log('classNames: ' + classNames);
// loop through the elements id's and classnames looking for exceptions
for (i = 0; i < l; i++) {
//prepare regex for class word matching
r = new RegExp('\\b' + classList[i] + '\\b');
// console.log('classList: ' + classList[i]);
// check for exact matches on id's or classes, but only if they exist in the first place
if ((id !== undefined && id === classList[i]) || (classNames && r.test(classNames))) {
// now let's exit out as it is an element that has been defined as being ignored for clicking outside
return;
}
}
}
}
// if we have got this far, then we are good to go with processing the command passed in via the click-outside attribute
$timeout(function() {
fn = $parse(attr['clickOutside']);
fn($scope);
});
}
// if the devices has a touchscreen, listen for this event
if (_hasTouch()) {
$document.on('touchstart', eventHandler);
}
// still listen for the click event even if there is touch to cater for touchscreen laptops
$document.on('click', eventHandler);
// when the scope is destroyed, clean up the documents event handlers as we don't want it hanging around
$scope.$on('$destroy', function() {
if (_hasTouch()) {
$document.off('touchstart', eventHandler);
}
$document.off('click', eventHandler);
});
// private function to attempt to figure out if we are on a touch device
function _hasTouch() {
// works on most browsers, IE10/11 and Surface
return 'ontouchstart' in window || navigator.maxTouchPoints;
};
});
}
};
}
})();