forked from IamAdamJowett/angular-click-outside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clickoutside.directive.js
121 lines (102 loc) · 5.15 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
109
110
111
112
113
114
115
116
117
118
119
120
121
/*global angular, navigator*/
(function() {
'use strict';
angular
.module('angular-click-outside', [])
.directive('clickOutside', [
'$document', '$parse', '$timeout',
clickOutside
]);
/**
* @ngdoc directive
* @name angular-click-outside.directive:clickOutside
* @description Directive to add click outside capabilities to DOM elements
* @requires $document
* @requires $parse
* @requires $timeout
**/
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;
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) {
// check if the element is the same element the directive is attached to and exit if so (props @CosticaPuntaru)
if (element === elem[0]) {
return;
}
// now we have done the initial checks, start gathering id's and classes
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) {
// 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');
// check for exact matches on id's or classes, but only if they exist in the first place
if ((id !== undefined && r.test(id)) || (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, { event: e });
});
}
// if the devices has a touchscreen, listen for this event
/*if (_hasTouch()) {
$document.on('touchstart', function () {
setTimeout(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);
});
/**
* @description Private function to attempt to figure out if we are on a touch device
* @private
**/
function _hasTouch() {
// works on most browsers, IE10/11 and Surface
return 'ontouchstart' in window || navigator.maxTouchPoints;
};
});
}
};
}
})();