-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.tabs.js
312 lines (258 loc) · 10.7 KB
/
jquery.tabs.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/**
* tabs plugin v0.1.6.1
* http://wingsline.com
*
* Copyright 2012 Arpad Olasz
* All rights reserved.
*
* --------------------------------------------------------------
* USAGE AND REQUIREMENTS
* --------------------------------------------------------------
*
* Requirements: jQuery 1.9+
* Optional: https://github.com/jjenzz/jQuery.nearest
*
*
* Required CSS:
* --------------------------------------------------------------
*
* .tabs--content li {display:none}
* .tabs--content li.active {display:block}
*
* Required HTML:
* --------------------------------------------------------------
*
* <ol class="tabs">
* <li><a href="#tab1">Tab 1</a></li>
* <li><a href="#tab2">Tab 2</a></li>
* <li><a href="#tab3">Tab 3</a></li>
* </ol>
* <ul class="tabs--content">
* <li id="tab1">Content 1</li>
* <li id="tab2">Content 2</li>
* <li id="tab3">Content 3</li>
* </ul>
*
*
* Usage:
* --------------------------------------------------------------
*
* Initialize the plugin:
* $('.tabs').tabs();
*
* When initializing, you can pass options to the plugin:
* $('.tabs').tabs({'option1':'value', 'option2': 'value'});
*
*
* The following methods will open a tab:
*
* 1. HTML
* Insert a hidden input element into the HTML:
* <input name="openTab" value="tab1" type="hidden"/>
*
* 2. Javascript
* Add this after the plugin was initialized:
* $('.tabs').tabs('open', 'tabname');
*
* If you want to set the hash of the current URL:
* $('.tabs').tabs('open', 'langhu', true);
*
* 3. Open the first or the last tab
* $('.tabs').tabs('open', 'first');
* $('.tabs').tabs('open', 'last');
*
*
* Notes:
* --------------------------------------------------------------
*
* - the tab content must be inserted after the tab handlers
* - the plugin adds the class "active" to the li elements, so make
* sure in your style sheet you hide the li elemenets
* and show the li.active ones
*
*/
(function( $ ){
'use strict';
// namespace for events and data
// use it like 'click.' + __NS__ for the event
// and data(__NS__) for data
var __NS__ = 'tabs',
// public methods
methods = {
/**
* Initializes the plugin
* @param {object} options Options object
* @return {object} Tabs object
*/
init : function( options ) {
return this.each(function(){
var $this = $(this),
data = $this.data(__NS__);
// If the plugin hasn't been initialized yet
if ( ! data ) {
// default options
data = $.extend( {
// the class for the tabbed content container
'tabContent' : '.tabs--content',
// tab element will have this class when active
'activeTabClass' : 'active',
// content element in the container will have this class when open
'activeClass': 'active',
// if a hidden element is present in the page we attempt to open that tab
'hiddenInputName': 'openTab',
// update the browser hash
'updateHash' : false
}, options);
$(this).data(__NS__, data);
// update the validIds for the tabs
methods.update.apply($this);
// if there is a hidden element in the page open that tab
var openTab = $(':hidden[name="'+data.hiddenInputName+'"]'),
openTabArray = $(':hidden[name="'+data.hiddenInputName+'[]"]');
// if there are multiple opentab arrays
if (openTabArray.length) {
openTabArray.each(function () {
methods.open.apply($this, [$(this).val().toString()]);
});
} else if(openTab.length) {
methods.open.apply($this, [$(openTab).val().toString()]);
}
else if(window.location.hash)
{
var hash = window.location.hash.substr(1);
$.extend($this.data(__NS__), {'hash':hash});
// attempt to auto-open the tab if it is a valid id
if($.inArray(hash, data.validIds) >= 0)
{
methods.open.apply($this, [hash]);
}
}
// add the events to the li > a elements
/* jQuery 9 compatibility */
$(this).on('click.' + __NS__, 'li > a', function(e)
// $('li > a', $this).live('click.' + __NS__, function(e)
{
// update the valid tabs
methods.update.apply($this);
var aHash = this.hash.substr(1);
if(aHash) {
e.preventDefault();
if (data.updateHash) {
window.location.hash = '#' + aHash;
}
methods.open.apply($this, [aHash, e]);
}
});
}
});
}, // end init()
/**
* This function essentially updates the tab id's in case new tabs are added
* @return {object} Tabs object
*/
update : function ( ) {
return this.each(function(){
var $this = $(this),
data = $this.data(__NS__);
data.validIds = [];
$('li > a', $this).each(function()
{
var aHash = this.hash.substr(1);
if(aHash)
{
data.validIds.push(aHash);
}
});
});
},
/**
* Removes every trace of the tabs plugin from the DOM
* @return {object} Tabs object
*/
destroy : function( ) {
return this.each(function(){
var $this = $(this),
data = $this.data(__NS__);
$(window).unbind('.'+__NS__);
/* jQuery 1.9 compatibility */
$(document).off('.'+__NS__, 'li > a');
// $('li > a').die('.'+__NS__);
data[__NS__].remove();
$this.removeData(__NS__);
});
}, // end destroy()
/**
* Opens a tab, closing the others
* @param {string} hash Id of the tab to be opened
* @return {object} Tabs element
*/
open: function (hash, e) {
return this.each(function(){
var $this = $(this),
data = $this.data(__NS__),
container = jQuery().nearest ? $this.nearest(data.tabContent) : $this.next(data.tabContent);
// if the requested hash is not in the validIds we don't do nothing
if($.inArray(hash, data.validIds) < 0 && hash !== 'first' && hash !== 'last') {
return;
}
// if hash == first, we open the 1st tab
if(hash === 'first')
{
hash = data.validIds[0];
}
// if hash == last, we open the last tab
else if (hash === 'last')
{
hash = data.validIds[data.validIds.length - 1];
}
if(container.length)
{
// make the tab active
// get hash of the A element
$this.children('li').each(function (){
var a = $('a:first', this);
if(a.length)
{
var aHash = a.get(0).hash.substr(1);
if(aHash)
{
// if the hash of the a elmenet matches, we make this active
if(aHash === hash)
{
$(this).addClass(data.activeTabClass);
// update the hash only if we have hash
if(undefined !== e && window.location.hash && data.updateHash)
{
window.location.hash = '#' + aHash;
}
}
// otherwise we remove the active class from the li
else
{
$(this).removeClass(data.activeTabClass);
}
}
}
});
// show the tab content
var content = $('#' + hash, container).addClass(data.activeClass);
// trigger the onTabOpen event
setTimeout(function () {
content.trigger('onTabOpen', [$this[0], hash]);
});
// hide other tab content
container.children().not($('#'+hash)).removeClass(data.activeClass);
}
});
} // end open ()
}; // end methods
$.fn.tabs = function( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.tabs' );
}
}; // end plugin
})( jQuery );