/
jquery.accTabs.js
245 lines (210 loc) · 8.9 KB
/
jquery.accTabs.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*!
* jQuery Accessible Tabs
*
* @description: Creates accessible tabs - a single content area with multiple panels
* @source: https://github.com/nomensa/jquery.accessible-tabs.git
* @version: '0.1.2'
*
* @author: Nomensa
* @license: licenced under MIT - http://opensource.org/licenses/mit-license.php
*/
(function($, window, document, undefined) {
'use strict';
var pluginName,
defaults,
counter = 0;
pluginName = 'accTabs';
defaults = {
// Specify which tab to open by default using 0-based position
defaultTab: 0,
// Callback when the plugin is created
callbackCreate: function() {},
// Callback when the plugin is destroyed
callbackDestroy: function() {},
// A class applied to the target element
containerClass: 'js-tabs',
// A class applied to the active tab control
controlActiveClass: 'js-tabs_control-item--active',
// An explanation of how the tabs operate, which is prepended to the tabs content
controlsText: 'Use the tab and enter or arrow keys to move between tabs',
// Class to apply to the controls text element
controlsTextClass: 'js-tabs_control-text',
// A class applied to the active panel
panelActiveClass: 'js-tabs_panel--active',
// Class to apply the controls list
tabControlsClass: 'js-tabs_control',
// Ids for tab controls should start with the following string
tabControlId: 'js-tabs_control-item--',
// Class to be applied to the tab panel
tabPanelClass: 'js-tabs_panel',
// Ids for tab panels should start with the following string
tabPanelId: 'js-tabs_panel--',
// Callback when the tab is activated
callbackTabActivated: function() {}
};
function AccTabs(element, options) {
/*
Constructor function for the tabs plugin
*/
var self = this;
self.element = $(element);
// Combine user options with default options
self.options = $.extend({}, defaults, options);
function init() {
/*
Our init function to create an instance of the plugin
*/
self.controlsWrapper = $('<ul class="' + self.options.tabControlsClass + '" role="tablist" />');
// Create a control for each tab panel
$('> div', self.element).each(function(index, value) {
var tabId = self.options.tabControlId + counter + index,
panelId = self.options.tabPanelId + counter + index,
heading = $($(value).prev()),
headingContent = heading.html(),
liMarkup = $('<li role="presentation">' +
'<button aria-selected="false" data-controls="' + panelId + '" id="' + tabId + '" role="tab">' +
headingContent +
'</button>' +
'</li>');
// Hide heading that has been used for the control button text
heading.hide();
// Bind event handlers
$('button', liMarkup)
.click(createHandleClick(self))
.keydown(createHandleKeyDown(self));
self.controlsWrapper.append(liMarkup);
});
// Prepend the constructed controls
self.element.prepend(self.controlsWrapper);
// Add classes and attributes to tab panels
$('> div', self.element).each(function(index, value) {
$(value)
.addClass(self.options.tabPanelClass)
.attr({
'aria-hidden': 'true',
'aria-labelledby': self.options.tabControlId + counter + index,
'id': self.options.tabPanelId + counter + index,
'role': 'tabpanel'
})
.hide();
});
// Add explanatory control text
self.controlsTextWrapper = $('<p class="' + self.options.controlsTextClass + '">' + self.options.controlsText + '</p>');
self.element.prepend(self.controlsTextWrapper);
// Activate the default tab
self.activateTab($('button', self.controlsWrapper).eq(self.options.defaultTab));
// Add the active class
self.element.addClass(self.options.containerClass);
// Increment counter for unique ID's
counter++;
self.options.callbackCreate();
}
function createHandleClick() {
/*
Create the click event handle
*/
self.handleClick = function(event) {
event.preventDefault();
self.activateTab(this);
self.options.callbackTabActivated();
};
return self.handleClick;
}
function createHandleKeyDown() {
/*
Create the keydown event handle
*/
self.handleKeyDown = function(event) {
switch (event.which) {
// arrow left or up
case 37:
case 38:
event.preventDefault();
// Allow us to loop through the controls
if ($(this).parent().prev().length !== 0) {
$(this).parent().prev().find('> button').focus().click();
} else {
$(self.controlsWrapper).find('li:last > button').focus().click();
}
break;
// arrow right or down
case 39:
case 40:
event.preventDefault();
// Allow us to loop through the controls
if ($(this).parent().next().length !== 0) {
$(this).parent().next().find('> button').focus().click();
} else {
$(self.controlsWrapper).find('li:first > button').focus().click();
}
break;
}
};
return self.handleKeyDown;
}
init();
}
AccTabs.prototype.activateTab = function(tab) {
/*
Public method for activating a tab
*/
// Checks if the tab is already active before trying to activate it
if ($(tab).attr('aria-selected') === 'false') {
var activeTabClass = this.options.controlActiveClass,
tabPanelId = '#' + $(tab).attr('data-controls');
// Reset state if another tab is active
if ($('[aria-selected="true"]', this.controlsWrapper).length !== 0) {
$('[aria-selected="true"]', this.controlsWrapper)
.attr('aria-selected', 'false')
.parent('li').removeClass(activeTabClass);
// Hide inactive panels
$('> [aria-hidden="false"]', this.element)
.attr('aria-hidden', 'true')
.hide()
// Remove active class
.removeClass(this.options.panelActiveClass);
}
// Update state of newly selected tab control
$(tab, this.element).attr('aria-selected', 'true');
$(tab, this.element).parent('li').addClass(activeTabClass);
// Update state of newly selected tab panel
$(tabPanelId, this.element)
.attr('aria-hidden', 'false')
.show()
// Add active class
.addClass(this.options.panelActiveClass);
}
};
AccTabs.prototype.rebuild = function() {
/*
Public method for rebuild the plugin and options
*/
return new AccTabs(this.element, this.options);
};
AccTabs.prototype.destroy = function () {
/*
Public method for return the DOM back to its initial state
*/
var self = this;
this.element.removeClass(this.options.containerClass);
$('> .' + this.options.controlsTextClass, this.element).remove();
$('> .' + this.options.tabControlsClass, this.element).remove();
$('> div', this.element).prev().removeAttr('style');
$('> div', this.element).each(function(index, value) {
$(value)
.removeAttr('aria-hidden aria-labelledby id role style')
.removeClass(self.options.tabPanelClass);
});
this.options.callbackDestroy();
};
$.fn[pluginName] = function(options) {
/*
Guards against multiple instantiations
*/
return this.each(function() {
if (!$.data(this, 'plugin_' + pluginName)) {
$.data(this, 'plugin_' + pluginName, new AccTabs(this, options));
}
});
};
})(jQuery, window, document);