Mercurial > hg > cmmr2012-drupal-site
comparison core/misc/tabbingmanager.js @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 /** | |
2 * DO NOT EDIT THIS FILE. | |
3 * See the following change record for more information, | |
4 * https://www.drupal.org/node/2815083 | |
5 * @preserve | |
6 **/ | |
7 | |
8 (function ($, Drupal) { | |
9 function TabbingManager() { | |
10 this.stack = []; | |
11 } | |
12 | |
13 $.extend(TabbingManager.prototype, { | |
14 constrain: function constrain(elements) { | |
15 var il = this.stack.length; | |
16 for (var i = 0; i < il; i++) { | |
17 this.stack[i].deactivate(); | |
18 } | |
19 | |
20 var $elements = $(elements).find(':tabbable').addBack(':tabbable'); | |
21 | |
22 var tabbingContext = new TabbingContext({ | |
23 level: this.stack.length, | |
24 $tabbableElements: $elements | |
25 }); | |
26 | |
27 this.stack.push(tabbingContext); | |
28 | |
29 tabbingContext.activate(); | |
30 | |
31 $(document).trigger('drupalTabbingConstrained', tabbingContext); | |
32 | |
33 return tabbingContext; | |
34 }, | |
35 release: function release() { | |
36 var toActivate = this.stack.length - 1; | |
37 while (toActivate >= 0 && this.stack[toActivate].released) { | |
38 toActivate--; | |
39 } | |
40 | |
41 this.stack.splice(toActivate + 1); | |
42 | |
43 if (toActivate >= 0) { | |
44 this.stack[toActivate].activate(); | |
45 } | |
46 }, | |
47 activate: function activate(tabbingContext) { | |
48 var $set = tabbingContext.$tabbableElements; | |
49 var level = tabbingContext.level; | |
50 | |
51 var $disabledSet = $(':tabbable').not($set); | |
52 | |
53 tabbingContext.$disabledElements = $disabledSet; | |
54 | |
55 var il = $disabledSet.length; | |
56 for (var i = 0; i < il; i++) { | |
57 this.recordTabindex($disabledSet.eq(i), level); | |
58 } | |
59 | |
60 $disabledSet.prop('tabindex', -1).prop('autofocus', false); | |
61 | |
62 var $hasFocus = $set.filter('[autofocus]').eq(-1); | |
63 | |
64 if ($hasFocus.length === 0) { | |
65 $hasFocus = $set.eq(0); | |
66 } | |
67 $hasFocus.trigger('focus'); | |
68 }, | |
69 deactivate: function deactivate(tabbingContext) { | |
70 var $set = tabbingContext.$disabledElements; | |
71 var level = tabbingContext.level; | |
72 var il = $set.length; | |
73 for (var i = 0; i < il; i++) { | |
74 this.restoreTabindex($set.eq(i), level); | |
75 } | |
76 }, | |
77 recordTabindex: function recordTabindex($el, level) { | |
78 var tabInfo = $el.data('drupalOriginalTabIndices') || {}; | |
79 tabInfo[level] = { | |
80 tabindex: $el[0].getAttribute('tabindex'), | |
81 autofocus: $el[0].hasAttribute('autofocus') | |
82 }; | |
83 $el.data('drupalOriginalTabIndices', tabInfo); | |
84 }, | |
85 restoreTabindex: function restoreTabindex($el, level) { | |
86 var tabInfo = $el.data('drupalOriginalTabIndices'); | |
87 if (tabInfo && tabInfo[level]) { | |
88 var data = tabInfo[level]; | |
89 if (data.tabindex) { | |
90 $el[0].setAttribute('tabindex', data.tabindex); | |
91 } else { | |
92 $el[0].removeAttribute('tabindex'); | |
93 } | |
94 if (data.autofocus) { | |
95 $el[0].setAttribute('autofocus', 'autofocus'); | |
96 } | |
97 | |
98 if (level === 0) { | |
99 $el.removeData('drupalOriginalTabIndices'); | |
100 } else { | |
101 var levelToDelete = level; | |
102 while (tabInfo.hasOwnProperty(levelToDelete)) { | |
103 delete tabInfo[levelToDelete]; | |
104 levelToDelete++; | |
105 } | |
106 $el.data('drupalOriginalTabIndices', tabInfo); | |
107 } | |
108 } | |
109 } | |
110 }); | |
111 | |
112 function TabbingContext(options) { | |
113 $.extend(this, { | |
114 level: null, | |
115 | |
116 $tabbableElements: $(), | |
117 | |
118 $disabledElements: $(), | |
119 | |
120 released: false, | |
121 | |
122 active: false | |
123 }, options); | |
124 } | |
125 | |
126 $.extend(TabbingContext.prototype, { | |
127 release: function release() { | |
128 if (!this.released) { | |
129 this.deactivate(); | |
130 this.released = true; | |
131 Drupal.tabbingManager.release(this); | |
132 | |
133 $(document).trigger('drupalTabbingContextReleased', this); | |
134 } | |
135 }, | |
136 activate: function activate() { | |
137 if (!this.active && !this.released) { | |
138 this.active = true; | |
139 Drupal.tabbingManager.activate(this); | |
140 | |
141 $(document).trigger('drupalTabbingContextActivated', this); | |
142 } | |
143 }, | |
144 deactivate: function deactivate() { | |
145 if (this.active) { | |
146 this.active = false; | |
147 Drupal.tabbingManager.deactivate(this); | |
148 | |
149 $(document).trigger('drupalTabbingContextDeactivated', this); | |
150 } | |
151 } | |
152 }); | |
153 | |
154 if (Drupal.tabbingManager) { | |
155 return; | |
156 } | |
157 | |
158 Drupal.tabbingManager = new TabbingManager(); | |
159 })(jQuery, Drupal); |