/* * jquery ui tabs * * copyright (c) 2007, 2008 klaus hartl (stilbuero.de) * dual licensed under the mit (mit-license.txt) * and gpl (gpl-license.txt) licenses. * * http://docs.jquery.com/ui/tabs * * depends: * ui.core.js * * revision: $id: ui.tabs.js 5547 2008-05-10 08:33:38z klaus.hartl $ */ ;(function($) { $.widget("ui.tabs", { init: function() { this.options.event += '.tabs'; // namespace event // create tabs this.tabify(true); }, setdata: function(key, value) { if ((/^selected/).test(key)) this.select(value); else { this.options[key] = value; this.tabify(); } }, length: function() { return this.$tabs.length; }, tabid: function(a) { return a.title && a.title.replace(/\s/g, '_').replace(/[^a-za-z0-9\-_:\.]/g, '') || this.options.idprefix + $.data(a); }, ui: function(tab, panel) { return { instance: this, options: this.options, tab: tab, panel: panel }; }, tabify: function(init) { this.$lis = $('li:has(a[href])', this.element); this.$tabs = this.$lis.map(function() { return $('a', this)[0]; }); this.$panels = $([]); var self = this, o = this.options; this.$tabs.each(function(i, a) { // inline tab if (a.hash && a.hash.replace('#', '')) // safari 2 reports '#' for an empty hash self.$panels = self.$panels.add(a.hash); // remote tab else if ($(a).attr('href') != '#') { // prevent loading the page itself if href is just "#" $.data(a, 'href.tabs', a.href); // required for restore on destroy $.data(a, 'load.tabs', a.href); // mutable var id = self.tabid(a); a.href = '#' + id; var $panel = $('#' + id); if (!$panel.length) { $panel = $(o.paneltemplate).attr('id', id).addclass(o.panelclass) .insertafter( self.$panels[i - 1] || self.element ); $panel.data('destroy.tabs', true); } self.$panels = self.$panels.add( $panel ); } // invalid tab href else o.disabled.push(i + 1); }); if (init) { // attach necessary classes for styling if not present this.element.hasclass(o.navclass) || this.element.addclass(o.navclass); this.$panels.each(function() { var $this = $(this); $this.hasclass(o.panelclass) || $this.addclass(o.panelclass); }); // selected tab // use "selected" option or try to retrieve: // 1. from fragment identifier in url // 2. from cookie // 3. from selected class attribute on
  • if (o.selected === undefined) { if (location.hash) { this.$tabs.each(function(i, a) { if (a.hash == location.hash) { o.selected = i; // prevent page scroll to fragment if ($.browser.msie || $.browser.opera) { // && !o.remote var $toshow = $(location.hash), toshowid = $toshow.attr('id'); $toshow.attr('id', ''); settimeout(function() { $toshow.attr('id', toshowid); // restore id }, 500); } scrollto(0, 0); return false; // break } }); } else if (o.cookie) { var index = parseint($.cookie('ui-tabs' + $.data(self.element)),10); if (index && self.$tabs[index]) o.selected = index; } else if (self.$lis.filter('.' + o.selectedclass).length) o.selected = self.$lis.index( self.$lis.filter('.' + o.selectedclass)[0] ); } o.selected = o.selected === null || o.selected !== undefined ? o.selected : 0; // first tab selected by default // take disabling tabs via class attribute from html // into account and update option properly. // a selected tab cannot become disabled. o.disabled = $.unique(o.disabled.concat( $.map(this.$lis.filter('.' + o.disabledclass), function(n, i) { return self.$lis.index(n); } ) )).sort(); if ($.inarray(o.selected, o.disabled) != -1) o.disabled.splice($.inarray(o.selected, o.disabled), 1); // highlight selected tab this.$panels.addclass(o.hideclass); this.$lis.removeclass(o.selectedclass); if (o.selected !== null) { this.$panels.eq(o.selected).show().removeclass(o.hideclass); // use show and remove class to show in any case no matter how it has been hidden before this.$lis.eq(o.selected).addclass(o.selectedclass); // seems to be expected behavior that the show callback is fired var onshow = function() { $(self.element).triggerhandler('tabsshow', [self.ui(self.$tabs[o.selected], self.$panels[o.selected])], o.show); }; // load if remote tab if ($.data(this.$tabs[o.selected], 'load.tabs')) this.load(o.selected, onshow); // just trigger show event else onshow(); } // clean up to avoid memory leaks in certain versions of ie 6 $(window).bind('unload', function() { self.$tabs.unbind('.tabs'); self.$lis = self.$tabs = self.$panels = null; }); } // disable tabs for (var i = 0, li; li = this.$lis[i]; i++) $(li)[$.inarray(i, o.disabled) != -1 && !$(li).hasclass(o.selectedclass) ? 'addclass' : 'removeclass'](o.disabledclass); // reset cache if switching from cached to not cached if (o.cache === false) this.$tabs.removedata('cache.tabs'); // set up animations var hidefx, showfx, basefx = { 'min-width': 0, duration: 1 }, baseduration = 'normal'; if (o.fx && o.fx.constructor == array) hidefx = o.fx[0] || basefx, showfx = o.fx[1] || basefx; else hidefx = showfx = o.fx || basefx; // reset some styles to maintain print style sheets etc. var resetcss = { display: '', overflow: '', height: '' }; if (!$.browser.msie) // not in ie to prevent cleartype font issue resetcss.opacity = ''; // hide a tab, animation prevents browser scrolling to fragment, // $show is optional. function hidetab(clicked, $hide, $show) { $hide.animate(hidefx, hidefx.duration || baseduration, function() { // $hide.addclass(o.hideclass).css(resetcss); // maintain flexible height and accessibility in print etc. if ($.browser.msie && hidefx.opacity) $hide[0].style.filter = ''; if ($show) showtab(clicked, $show, $hide); }); } // show a tab, animation prevents browser scrolling to fragment, // $hide is optional. function showtab(clicked, $show, $hide) { if (showfx === basefx) $show.css('display', 'block'); // prevent occasionally occuring flicker in firefox cause by gap between showing and hiding the tab panels $show.animate(showfx, showfx.duration || baseduration, function() { $show.removeclass(o.hideclass).css(resetcss); // maintain flexible height and accessibility in print etc. if ($.browser.msie && showfx.opacity) $show[0].style.filter = ''; // callback $(self.element).triggerhandler('tabsshow', [self.ui(clicked, $show[0])], o.show); }); } // switch a tab function switchtab(clicked, $li, $hide, $show) { /*if (o.bookmarkable && trueclick) { // add to history only if true click occured, not a triggered click $.ajaxhistory.update(clicked.hash); }*/ $li.addclass(o.selectedclass) .siblings().removeclass(o.selectedclass); hidetab(clicked, $hide, $show); } // attach tab event handler, unbind to avoid duplicates from former tabifying... this.$tabs.unbind('.tabs').bind(o.event, function() { //var trueclick = e.clientx; // add to history only if true click occured, not a triggered click var $li = $(this).parents('li:eq(0)'), $hide = self.$panels.filter(':visible'), $show = $(this.hash); // if tab is already selected and not unselectable or tab disabled or // or is already loading or click callback returns false stop here. // check if click handler returns false last so that it is not executed // for a disabled or loading tab! if (($li.hasclass(o.selectedclass) && !o.unselect) || $li.hasclass(o.disabledclass) || $(this).hasclass(o.loadingclass) || $(self.element).triggerhandler('tabsselect', [self.ui(this, $show[0])], o.select) === false ) { this.blur(); return false; } self.options.selected = self.$tabs.index(this); // if tab may be closed if (o.unselect) { if ($li.hasclass(o.selectedclass)) { self.options.selected = null; $li.removeclass(o.selectedclass); self.$panels.stop(); hidetab(this, $hide); this.blur(); return false; } else if (!$hide.length) { self.$panels.stop(); var a = this; self.load(self.$tabs.index(this), function() { $li.addclass(o.selectedclass).addclass(o.unselectclass); showtab(a, $show); }); this.blur(); return false; } } if (o.cookie) $.cookie('ui-tabs' + $.data(self.element), self.options.selected, o.cookie); // stop possibly running animations self.$panels.stop(); // show new tab if ($show.length) { // prevent scrollbar scrolling to 0 and than back in ie7, happens only if bookmarking/history is enabled /*if ($.browser.msie && o.bookmarkable) { var showid = this.hash.replace('#', ''); $show.attr('id', ''); settimeout(function() { $show.attr('id', showid); // restore id }, 0); }*/ var a = this; self.load(self.$tabs.index(this), $hide.length ? function() { switchtab(a, $li, $hide, $show); } : function() { $li.addclass(o.selectedclass); showtab(a, $show); } ); // set scrollbar to saved position - need to use timeout with 0 to prevent browser scroll to target of hash /*var scrollx = window.pagexoffset || document.documentelement && document.documentelement.scrollleft || document.body.scrollleft || 0; var scrolly = window.pageyoffset || document.documentelement && document.documentelement.scrolltop || document.body.scrolltop || 0; settimeout(function() { scrollto(scrollx, scrolly); }, 0);*/ } else throw 'jquery ui tabs: mismatching fragment identifier.'; // prevent ie from keeping other link focussed when using the back button // and remove dotted border from clicked link. this is controlled in modern // browsers via css, also blur removes focus from address bar in firefox // which can become a usability and annoying problem with tabsrotate. if ($.browser.msie) this.blur(); //return o.bookmarkable && !!trueclick; // convert trueclick == undefined to boolean required in ie return false; }); // disable click if event is configured to something else if (!(/^click/).test(o.event)) this.$tabs.bind('click.tabs', function() { return false; }); }, add: function(url, label, index) { if (index == undefined) index = this.$tabs.length; // append by default var o = this.options; var $li = $(o.tabtemplate.replace(/#\{href\}/, url).replace(/#\{label\}/, label)); $li.data('destroy.tabs', true); var id = url.indexof('#') == 0 ? url.replace('#', '') : this.tabid( $('a:first-child', $li)[0] ); // try to find an existing element before creating a new one var $panel = $('#' + id); if (!$panel.length) { $panel = $(o.paneltemplate).attr('id', id) .addclass(o.panelclass).addclass(o.hideclass); $panel.data('destroy.tabs', true); } if (index >= this.$lis.length) { $li.appendto(this.element); $panel.appendto(this.element[0].parentnode); } else { $li.insertbefore(this.$lis[index]); $panel.insertbefore(this.$panels[index]); } o.disabled = $.map(o.disabled, function(n, i) { return n >= index ? ++n : n }); this.tabify(); if (this.$tabs.length == 1) { $li.addclass(o.selectedclass); $panel.removeclass(o.hideclass); var href = $.data(this.$tabs[0], 'load.tabs'); if (href) this.load(index, href); } // callback this.element.triggerhandler('tabsadd', [this.ui(this.$tabs[index], this.$panels[index])], o.add ); }, remove: function(index) { var o = this.options, $li = this.$lis.eq(index).remove(), $panel = this.$panels.eq(index).remove(); // if selected tab was removed focus tab to the right or // in case the last tab was removed the tab to the left. if ($li.hasclass(o.selectedclass) && this.$tabs.length > 1) this.select(index + (index + 1 < this.$tabs.length ? 1 : -1)); o.disabled = $.map($.grep(o.disabled, function(n, i) { return n != index; }), function(n, i) { return n >= index ? --n : n }); this.tabify(); // callback this.element.triggerhandler('tabsremove', [this.ui($li.find('a')[0], $panel[0])], o.remove ); }, enable: function(index) { var o = this.options; if ($.inarray(index, o.disabled) == -1) return; var $li = this.$lis.eq(index).removeclass(o.disabledclass); if ($.browser.safari) { // fix disappearing tab (that used opacity indicating disabling) after enabling in safari 2... $li.css('display', 'inline-block'); settimeout(function() { $li.css('display', 'block'); }, 0); } o.disabled = $.grep(o.disabled, function(n, i) { return n != index; }); // callback this.element.triggerhandler('tabsenable', [this.ui(this.$tabs[index], this.$panels[index])], o.enable ); }, disable: function(index) { var self = this, o = this.options; if (index != o.selected) { // cannot disable already selected tab this.$lis.eq(index).addclass(o.disabledclass); o.disabled.push(index); o.disabled.sort(); // callback this.element.triggerhandler('tabsdisable', [this.ui(this.$tabs[index], this.$panels[index])], o.disable ); } }, select: function(index) { if (typeof index == 'string') index = this.$tabs.index( this.$tabs.filter('[href$=' + index + ']')[0] ); this.$tabs.eq(index).trigger(this.options.event); }, load: function(index, callback) { // callback is for internal usage only var self = this, o = this.options, $a = this.$tabs.eq(index), a = $a[0], bypasscache = callback == undefined || callback === false, url = $a.data('load.tabs'); callback = callback || function() {}; // no remote or from cache - just finish with callback if (!url || !bypasscache && $.data(a, 'cache.tabs')) { callback(); return; } // load remote from here on var inner = function(parent) { var $parent = $(parent), $inner = $parent.find('*:last'); return $inner.length && $inner || $parent; }; var cleanup = function() { self.$tabs.filter('.' + o.loadingclass).removeclass(o.loadingclass) .each(function() { if (o.spinner) inner(this).parent().html(inner(this).data('label.tabs')); }); self.xhr = null; }; if (o.spinner) { var label = inner(a).html(); inner(a).wrapinner('') .find('em').data('label.tabs', label).html(o.spinner); } var ajaxoptions = $.extend({}, o.ajaxoptions, { url: url, success: function(r, s) { $(a.hash).html(r); cleanup(); if (o.cache) $.data(a, 'cache.tabs', true); // if loaded once do not load them again // callbacks $(self.element).triggerhandler('tabsload', [self.ui(self.$tabs[index], self.$panels[index])], o.load ); o.ajaxoptions.success && o.ajaxoptions.success(r, s); // this callback is required because the switch has to take // place after loading has completed. call last in order to // fire load before show callback... callback(); } }); if (this.xhr) { // terminate pending requests from other tabs and restore tab label this.xhr.abort(); cleanup(); } $a.addclass(o.loadingclass); settimeout(function() { // timeout is again required in ie, "wait" for id being restored self.xhr = $.ajax(ajaxoptions); }, 0); }, url: function(index, url) { this.$tabs.eq(index).removedata('cache.tabs').data('load.tabs', url); }, destroy: function() { var o = this.options; this.element.unbind('.tabs') .removeclass(o.navclass).removedata('tabs'); this.$tabs.each(function() { var href = $.data(this, 'href.tabs'); if (href) this.href = href; var $this = $(this).unbind('.tabs'); $.each(['href', 'load', 'cache'], function(i, prefix) { $this.removedata(prefix + '.tabs'); }); }); this.$lis.add(this.$panels).each(function() { if ($.data(this, 'destroy.tabs')) $(this).remove(); else $(this).removeclass([o.selectedclass, o.unselectclass, o.disabledclass, o.panelclass, o.hideclass].join(' ')); }); } }); $.ui.tabs.defaults = { // basic setup unselect: false, event: 'click', disabled: [], cookie: null, // e.g. { expires: 7, path: '/', domain: 'jquery.com', secure: true } // todo history: false, // ajax spinner: 'loading…', cache: false, idprefix: 'ui-tabs-', ajaxoptions: {}, // animations fx: null, // e.g. { height: 'toggle', opacity: 'toggle', duration: 200 } // templates tabtemplate: '
  • #{label}
  • ', paneltemplate: '
    ', // css classes navclass: 'ui-tabs-nav', selectedclass: 'ui-tabs-selected', unselectclass: 'ui-tabs-unselect', disabledclass: 'ui-tabs-disabled', panelclass: 'ui-tabs-panel', hideclass: 'ui-tabs-hide', loadingclass: 'ui-tabs-loading' }; $.ui.tabs.getter = "length"; /* * tabs extensions */ /* * rotate */ $.extend($.ui.tabs.prototype, { rotation: null, rotate: function(ms, continuing) { continuing = continuing || false; var self = this, t = this.options.selected; function start() { self.rotation = setinterval(function() { t = ++t < self.$tabs.length ? t : 0; self.select(t); }, ms); } function stop(e) { if (!e || e.clientx) { // only in case of a true click clearinterval(self.rotation); } } // start interval if (ms) { start(); if (!continuing) this.$tabs.bind(this.options.event, stop); else this.$tabs.bind(this.options.event, function() { stop(); t = self.options.selected; start(); }); } // stop interval else { stop(); this.$tabs.unbind(this.options.event, stop); } } }); })(jquery);