-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathangular-tab-trap.js
67 lines (56 loc) · 2.17 KB
/
angular-tab-trap.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
(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.tabTrap = factory(root.angular);
}
}(this, function (angular) {
'use strict';
return angular.module('mp.tabTrap', []).directive('tabTrap', [ '$window', function ($window) {
// Introduce custom elements for IE8
$window.document.createElement('tab-trap');
var tmpl = ''
+ '<button type="button"></button>'
+ '<div ng-transclude></div>'
+ '<button type="button"></button>'
;
return {
restrict: 'AE',
template: tmpl,
transclude: true,
link: {
pre: function ($scope) {
},
post: function ($scope, $element, $attributes, ngModel) {
var buttonStyle = {
position: 'absolute',
left: '-9999px'
};
var children = $element.children(),
focusLeader = children.eq(0),
transclusionPoint = children.eq(1),
focusTrailer = children.eq(2);
$element.css('position', 'relative');
focusLeader.css(buttonStyle);
focusTrailer.css(buttonStyle);
children.on('focus', function () {
var potentiallyTabbableChildren = transclusionPoint[0].querySelectorAll('input, button, select, textarea, a[href], *[tabindex]');
var tabbables = Array.prototype.filter.call(potentiallyTabbableChildren, function (item) {
return item.tabIndex >= 0;
});
if (tabbables[0]) {
tabbables[0].focus();
}
});
}
}
};
}]);
}));