2 * Tabs 3 - New Wave Tabs
4 * Copyright (c) 2007 Klaus Hartl (stilbuero.de)
5 * Dual licensed under the MIT (MIT-LICENSE.txt)
6 * and GPL (GPL-LICENSE.txt) licenses.
11 // if the UI scope is not availalable, add it
14 // tabs initialization
15 $.fn.tabs = function(initial, options) {
16 if (initial && initial.constructor == Object) { // shift arguments
20 options = options || {};
22 initial = initial && initial.constructor == Number && --initial || 0;
24 return this.each(function() {
25 new $.ui.tabs(this, $.extend(options, { initial: initial }));
29 // other chainable tabs methods
30 $.each(['Add', 'Remove', 'Enable', 'Disable', 'Click', 'Load', 'Href'], function(i, method) {
31 $.fn['tabs' + method] = function() {
33 return this.each(function() {
34 var instance = $.ui.tabs.getInstance(this);
35 instance[method.toLowerCase()].apply(instance, args);
39 $.fn.tabsSelected = function() {
42 var instance = $.ui.tabs.getInstance(this[0]),
44 selected = $lis.index( $lis.filter('.' + instance.options.selectedClass)[0] );
46 return selected >= 0 ? ++selected : -1;
50 $.ui.tabs = function(el, options) {
54 this.options = $.extend({
60 cookie: null, // pass options object as expected by cookie plugin: { expires: 7, path: '/', domain: 'jquery.com', secure: true }
61 // TODO bookmarkable: $.ajaxHistory ? true : false,
63 unselect: options.unselected ? true : false,
66 spinner: 'Loading…',
82 remove: function() {},
83 enable: function() {},
84 disable: function() {},
91 tabTemplate: '<li><a href="#{href}"><span>#{text}</span></a></li>',
92 panelTemplate: '<div></div>',
95 navClass: 'ui-tabs-nav',
96 selectedClass: 'ui-tabs-selected',
97 unselectClass: 'ui-tabs-unselect',
98 disabledClass: 'ui-tabs-disabled',
99 panelClass: 'ui-tabs-panel',
100 hideClass: 'ui-tabs-hide',
101 loadingClass: 'ui-tabs-loading'
105 this.options.event += '.ui-tabs'; // namespace event
106 this.options.cookie = $.cookie && $.cookie.constructor == Function && this.options.cookie;
108 // save instance for later
109 $.data(el, $.ui.tabs.INSTANCE_KEY, this);
116 $.ui.tabs.INSTANCE_KEY = 'ui_tabs_instance';
117 $.ui.tabs.getInstance = function(el) {
118 return $.data(el, $.ui.tabs.INSTANCE_KEY);
122 $.extend($.ui.tabs.prototype, {
124 return a.title ? a.title.replace(/\s/g, '_')
125 : this.options.idPrefix + $.data(a);
127 tabify: function(init) {
129 this.$lis = $('li:has(a[href])', this.source);
130 this.$tabs = this.$lis.map(function() { return $('a', this)[0] });
131 this.$panels = $([]);
133 var self = this, o = this.options;
135 this.$tabs.each(function(i, a) {
137 if (a.hash && a.hash.replace('#', '')) { // Safari 2 reports '#' for an empty hash
138 self.$panels = self.$panels.add(a.hash);
141 else if ($(a).attr('href') != '#') { // prevent loading the page itself if href is just "#"
142 $.data(a, 'href', a.href);
143 var id = self.tabId(a);
145 self.$panels = self.$panels.add(
146 $('#' + id)[0] || $(o.panelTemplate).attr('id', id).addClass(o.panelClass)
147 .insertAfter( self.$panels[i - 1] || self.source )
152 o.disabled.push(i + 1);
158 // attach necessary classes for styling if not present
159 $(this.source).hasClass(o.navClass) || $(this.source).addClass(o.navClass);
160 this.$panels.each(function() {
162 $this.hasClass(o.panelClass) || $this.addClass(o.panelClass);
166 for (var i = 0, position; position = o.disabled[i]; i++) {
167 this.disable(position);
170 // Try to retrieve initial tab:
171 // 1. from fragment identifier in url if present
173 // 3. from selected class attribute on <li>
174 // 4. otherwise use given initial argument
175 // 5. check if tab is disabled
176 this.$tabs.each(function(i, a) {
178 if (a.hash == location.hash) {
180 // prevent page scroll to fragment
181 //if (($.browser.msie || $.browser.opera) && !o.remote) {
182 if ($.browser.msie || $.browser.opera) {
183 var $toShow = $(location.hash), toShowId = $toShow.attr('id');
184 $toShow.attr('id', '');
185 setTimeout(function() {
186 $toShow.attr('id', toShowId); // restore id
190 return false; // break
192 } else if (o.cookie) {
193 o.initial = parseInt($.cookie( $.ui.tabs.INSTANCE_KEY + $.data(self.source) )) || 0;
194 return false; // break
195 } else if ( self.$lis.eq(i).hasClass(o.selectedClass) ) {
197 return false; // break
200 var n = this.$lis.length;
201 while (this.$lis.eq(o.initial).hasClass(o.disabledClass) && n) {
202 o.initial = ++o.initial < this.$lis.length ? o.initial : 0;
205 if (!n) { // all tabs disabled, set option unselected to true
206 o.unselected = o.unselect = true;
209 // highlight selected tab
210 this.$panels.addClass(o.hideClass);
211 this.$lis.removeClass(o.selectedClass);
213 this.$panels.eq(o.initial).show().removeClass(o.hideClass); // use show and remove class to show in any case no matter how it has been hidden before
214 this.$lis.eq(o.initial).addClass(o.selectedClass);
217 // load if remote tab
218 var href = !o.unselected && $.data(this.$tabs[o.initial], 'href');
220 this.load(o.initial + 1, href);
223 // disable click if event is configured to something else
224 if (!/^click/.test(o.event)) {
225 this.$tabs.bind('click', function(e) { e.preventDefault(); });
231 var showAnim = {}, showSpeed = o.fxShowSpeed || o.fxSpeed,
232 hideAnim = {}, hideSpeed = o.fxHideSpeed || o.fxSpeed;
233 if (o.fxSlide || o.fxFade) {
235 showAnim['height'] = 'show';
236 hideAnim['height'] = 'hide';
239 showAnim['opacity'] = 'show';
240 hideAnim['opacity'] = 'hide';
245 } else { // use some kind of animation to prevent browser scrolling to the tab
246 showAnim['min-width'] = 0; // avoid opacity, causes flicker in Firefox
247 showSpeed = 1; // as little as 1 is sufficient
251 } else { // use some kind of animation to prevent browser scrolling to the tab
252 hideAnim['min-width'] = 0; // avoid opacity, causes flicker in Firefox
253 hideSpeed = 1; // as little as 1 is sufficient
257 // reset some styles to maintain print style sheets etc.
258 var resetCSS = { display: '', overflow: '', height: '' };
259 if (!$.browser.msie) { // not in IE to prevent ClearType font issue
260 resetCSS['opacity'] = '';
263 // Hide a tab, animation prevents browser scrolling to fragment,
264 // $show is optional.
265 function hideTab(clicked, $hide, $show) {
266 $hide.animate(hideAnim, hideSpeed, function() { //
267 $hide.addClass(o.hideClass).css(resetCSS); // maintain flexible height and accessibility in print etc.
268 if ($.browser.msie && hideAnim['opacity']) {
269 $hide[0].style.filter = '';
271 o.hide(clicked, $hide[0], $show && $show[0] || null);
273 showTab(clicked, $show, $hide);
278 // Show a tab, animation prevents browser scrolling to fragment,
280 function showTab(clicked, $show, $hide) {
281 if (!(o.fxSlide || o.fxFade || o.fxShow)) {
282 $show.css('display', 'block'); // prevent occasionally occuring flicker in Firefox cause by gap between showing and hiding the tab panels
284 $show.animate(showAnim, showSpeed, function() {
285 $show.removeClass(o.hideClass).css(resetCSS); // maintain flexible height and accessibility in print etc.
286 if ($.browser.msie && showAnim['opacity']) {
287 $show[0].style.filter = '';
289 o.show(clicked, $show[0], $hide && $hide[0] || null);
294 function switchTab(clicked, $li, $hide, $show) {
295 /*if (o.bookmarkable && trueClick) { // add to history only if true click occured, not a triggered click
296 $.ajaxHistory.update(clicked.hash);
298 $li.addClass(o.selectedClass)
299 .siblings().removeClass(o.selectedClass);
300 hideTab(clicked, $hide, $show);
303 // attach tab event handler, unbind to avoid duplicates from former tabifying...
304 this.$tabs.unbind(o.event).bind(o.event, function() {
306 //var trueClick = e.clientX; // add to history only if true click occured, not a triggered click
307 var $li = $(this).parents('li:eq(0)'),
308 $hide = self.$panels.filter(':visible'),
309 $show = $(this.hash);
311 // If tab is already selected and not unselectable or tab disabled or click callback returns false stop here.
312 // Check if click handler returns false last so that it is not executed for a disabled tab!
313 if (($li.hasClass(o.selectedClass) && !o.unselect) || $li.hasClass(o.disabledClass)
314 || o.click(this, $show[0], $hide[0]) === false) {
320 $.cookie($.ui.tabs.INSTANCE_KEY + $.data(self.source), self.$tabs.index(this), o.cookie);
323 // if tab may be closed
325 if ($li.hasClass(o.selectedClass)) {
326 $li.removeClass(o.selectedClass);
328 hideTab(this, $hide);
331 } else if (!$hide.length) {
333 if ($.data(this, 'href')) { // remote tab
335 self.load(self.$tabs.index(this) + 1, $.data(this, 'href'), function() {
336 $li.addClass(o.selectedClass).addClass(o.unselectClass);
340 $li.addClass(o.selectedClass).addClass(o.unselectClass);
341 showTab(this, $show);
348 // stop possibly running animations
354 // prevent scrollbar scrolling to 0 and than back in IE7, happens only if bookmarking/history is enabled
355 /*if ($.browser.msie && o.bookmarkable) {
356 var showId = this.hash.replace('#', '');
357 $show.attr('id', '');
358 setTimeout(function() {
359 $show.attr('id', showId); // restore id
363 if ($.data(this, 'href')) { // remote tab
365 self.load(self.$tabs.index(this) + 1, $.data(this, 'href'), function() {
366 switchTab(a, $li, $hide, $show);
369 switchTab(this, $li, $hide, $show);
372 // Set scrollbar to saved position - need to use timeout with 0 to prevent browser scroll to target of hash
373 /*var scrollX = window.pageXOffset || document.documentElement && document.documentElement.scrollLeft || document.body.scrollLeft || 0;
374 var scrollY = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop || 0;
375 setTimeout(function() {
376 scrollTo(scrollX, scrollY);
380 throw 'jQuery UI Tabs: Mismatching fragment identifier.';
383 // Prevent IE from keeping other link focussed when using the back button
384 // and remove dotted border from clicked link. This is controlled in modern
385 // browsers via CSS, also blur removes focus from address bar in Firefox
386 // which can become a usability and annoying problem with tabsRotate.
387 if ($.browser.msie) {
391 //return o.bookmarkable && !!trueClick; // convert trueClick == undefined to Boolean required in IE
397 add: function(url, text, position) {
399 position = position || this.$tabs.length; // append by default
401 var o = this.options,
402 $li = $(o.tabTemplate.replace(/#\{href\}/, url).replace(/#\{text\}/, text));
404 var id = url.indexOf('#') == 0 ? url.replace('#', '') : this.tabId( $('a:first-child', $li)[0] );
406 // try to find an existing element before creating a new one
407 var $panel = $('#' + id);
408 $panel = $panel.length && $panel
409 || $(o.panelTemplate).attr('id', id).addClass(o.panelClass).addClass(o.hideClass);
410 if (position >= this.$lis.length) {
411 $li.appendTo(this.source);
412 $panel.appendTo(this.source.parentNode);
414 $li.insertBefore(this.$lis[position - 1]);
415 $panel.insertBefore(this.$panels[position - 1]);
420 if (this.$tabs.length == 1) {
421 $li.addClass(o.selectedClass);
422 $panel.removeClass(o.hideClass);
423 var href = $.data(this.$tabs[0], 'href');
425 this.load(position + 1, href);
428 o.add(this.$tabs[position], this.$panels[position]); // callback
430 throw 'jQuery UI Tabs: Not enough arguments to add tab.';
433 remove: function(position) {
434 if (position && position.constructor == Number) {
435 var o = this.options, $li = this.$lis.eq(position - 1).remove(),
436 $panel = this.$panels.eq(position - 1).remove();
438 // If selected tab was removed focus tab to the right or
439 // tab to the left if last tab was removed.
440 if ($li.hasClass(o.selectedClass) && this.$tabs.length > 1) {
441 this.click(position + (position < this.$tabs.length ? 1 : -1));
444 o.remove($li.end()[0], $panel[0]); // callback
447 enable: function(position) {
448 var o = this.options, $li = this.$lis.eq(position - 1);
449 $li.removeClass(o.disabledClass);
450 if ($.browser.safari) { // fix disappearing tab (that used opacity indicating disabling) after enabling in Safari 2...
451 $li.css('display', 'inline-block');
452 setTimeout(function() {
453 $li.css('display', 'block')
456 o.enable(this.$tabs[position - 1], this.$panels[position - 1]); // callback
458 disable: function(position) {
459 var o = this.options;
460 this.$lis.eq(position - 1).addClass(o.disabledClass);
461 o.disable(this.$tabs[position - 1], this.$panels[position - 1]); // callback
463 click: function(position) {
464 this.$tabs.eq(position - 1).trigger(this.options.event);
466 load: function(position, url, callback) {
467 var self = this, o = this.options,
468 $a = this.$tabs.eq(position - 1), a = $a[0], $span = $('span', a);
471 if (url && url.constructor == Function) {
476 // set new URL or get existing
478 $.data(a, 'href', url);
480 url = $.data(a, 'href');
485 $.data(a, 'title', $span.html());
486 $span.html('<em>' + o.spinner + '</em>');
488 var finish = function() {
489 self.$tabs.filter('.' + o.loadingClass).each(function() {
490 $(this).removeClass(o.loadingClass);
492 $('span', this).html( $.data(this, 'title') );
497 var ajaxOptions = $.extend(o.ajaxOptions, {
499 success: function(r) {
502 // This callback is required because the switch has to take
503 // place after loading has completed.
504 if (callback && callback.constructor == Function) {
508 $.removeData(a, 'href'); // if loaded once do not load them again
510 o.load(self.$tabs[position - 1], self.$panels[position - 1]); // callback
514 // terminate pending requests from other tabs and restore title
518 $a.addClass(o.loadingClass);
519 setTimeout(function() { // timeout is again required in IE, "wait" for id being restored
520 self.xhr = $.ajax(ajaxOptions);
524 href: function(position, href) {
525 $.data(this.$tabs.eq(position - 1)[0], 'href', href);