-
Notifications
You must be signed in to change notification settings - Fork 9
/
angular-deep-blur.js
60 lines (52 loc) · 1.96 KB
/
angular-deep-blur.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
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([ 'module', 'angular' ], function (module, angular) {
module.exports = factory(angular);
});
} else if (typeof module === 'object') {
module.exports = factory(require('angular'));
} else {
if (!root.mp) {
root.mp = {};
}
root.mp.deepBlur = factory(root.angular);
}
}(this, function (angular) {
'use strict';
function containsDom(parent, dom) {
while (dom) {
if (dom === parent) {
return true;
}
dom = dom.parentNode;
}
return false;
}
return angular.module('mp.deepBlur', [])
// child links must use tabindex=0 to capture focus in Webkit and avoiding triggering blur
.directive('deepBlur', [ '$timeout', function ($timeout) {
return {
restrict: 'A',
controller: [ '$scope', '$element', '$attrs', function ($scope, $element, $attrs) {
var leaveExpr = $attrs.deepBlur,
dom = $element[0];
function onBlur(e) {
// e.relatedTarget for Chrome
// document.activeElement for IE 11
var targetElement = e.relatedTarget || document.activeElement;
if (!containsDom(dom, targetElement)) {
$timeout(function () {
$scope.$apply(leaveExpr);
}, 10);
}
}
if (dom.addEventListener) {
dom.addEventListener('blur', onBlur, true);
} else {
dom.attachEvent('onfocusout', onBlur); // For IE8
}
} ]
};
} ])
.name; // pass back as dependency name
}));