2 Copyright (c) 2008, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
10 * The tabview module provides a widget for managing content bound to tabs.
12 * @requires yahoo, dom, event, element
16 * A widget to control tabbed views.
17 * @namespace YAHOO.widget
19 * @extends YAHOO.util.Element
21 * @param {HTMLElement | String | Object} el(optional) The html
22 * element that represents the TabView, or the attribute object to use.
23 * An element will be created if none provided.
24 * @param {Object} attr (optional) A key map of the tabView's
25 * initial attributes. Ignored if first arg is attributes object.
27 YAHOO.widget.TabView = function(el, attr) {
29 if (arguments.length == 1 && !YAHOO.lang.isString(el) && !el.nodeName) {
30 attr = el; // treat first arg as attr object
31 el = attr.element || null;
34 if (!el && !attr.element) { // create if we dont have one
35 el = _createTabViewElement.call(this, attr);
37 YAHOO.widget.TabView.superclass.constructor.call(this, el, attr);
40 YAHOO.extend(YAHOO.widget.TabView, YAHOO.util.Element);
42 var proto = YAHOO.widget.TabView.prototype;
43 var Dom = YAHOO.util.Dom;
44 var Event = YAHOO.util.Event;
45 var Tab = YAHOO.widget.Tab;
49 * The className to add when building from scratch.
53 proto.CLASSNAME = 'yui-navset';
56 * The className of the HTMLElement containing the TabView's tab elements
57 * to look for when building from existing markup, or to add when building
59 * All childNodes of the tab container are treated as Tabs when building
60 * from existing markup.
61 * @property TAB_PARENT_CLASSNAME
64 proto.TAB_PARENT_CLASSNAME = 'yui-nav';
67 * The className of the HTMLElement containing the TabView's label elements
68 * to look for when building from existing markup, or to add when building
70 * All childNodes of the content container are treated as content elements when
71 * building from existing markup.
72 * @property CONTENT_PARENT_CLASSNAME
73 * @default "nav-content"
75 proto.CONTENT_PARENT_CLASSNAME = 'yui-content';
77 proto._tabParent = null;
78 proto._contentParent = null;
81 * Adds a Tab to the TabView instance.
82 * If no index is specified, the tab is added to the end of the tab list.
84 * @param {YAHOO.widget.Tab} tab A Tab instance to add.
85 * @param {Integer} index The position to add the tab.
88 proto.addTab = function(tab, index) {
89 var tabs = this.get('tabs');
90 if (!tabs) { // not ready yet
91 this._queue[this._queue.length] = ['addTab', arguments];
95 index = (index === undefined) ? tabs.length : index;
97 var before = this.getTab(index);
100 var el = this.get('element');
101 var tabParent = this._tabParent;
102 var contentParent = this._contentParent;
104 var tabElement = tab.get('element');
105 var contentEl = tab.get('contentEl');
108 tabParent.insertBefore(tabElement, before.get('element'));
110 tabParent.appendChild(tabElement);
113 if ( contentEl && !Dom.isAncestor(contentParent, contentEl) ) {
114 contentParent.appendChild(contentEl);
117 if ( !tab.get('active') ) {
118 tab.set('contentVisible', false, true); /* hide if not active */
120 this.set('activeTab', tab, true);
124 var activate = function(e) {
125 YAHOO.util.Event.preventDefault(e);
128 if (this == self.get('activeTab')) {
129 silent = true; // dont fire activeTabChange if already active
131 self.set('activeTab', this, silent);
134 tab.addListener( tab.get('activationEvent'), activate);
136 tab.addListener('activationEventChange', function(e) {
137 if (e.prevValue != e.newValue) {
138 tab.removeListener(e.prevValue, activate);
139 tab.addListener(e.newValue, activate);
143 tabs.splice(index, 0, tab);
147 * Routes childNode events.
148 * @method DOMEventHandler
149 * @param {event} e The Dom event that is being handled.
152 proto.DOMEventHandler = function(e) {
153 var el = this.get('element');
154 var target = YAHOO.util.Event.getTarget(e);
155 var tabParent = this._tabParent;
157 if (Dom.isAncestor(tabParent, target) ) {
161 var tabs = this.get('tabs');
163 for (var i = 0, len = tabs.length; i < len; i++) {
164 tabEl = tabs[i].get('element');
165 contentEl = tabs[i].get('contentEl');
167 if ( target == tabEl || Dom.isAncestor(tabEl, target) ) {
174 tab.fireEvent(e.type, e);
180 * Returns the Tab instance at the specified index.
182 * @param {Integer} index The position of the Tab.
183 * @return YAHOO.widget.Tab
185 proto.getTab = function(index) {
186 return this.get('tabs')[index];
190 * Returns the index of given tab.
191 * @method getTabIndex
192 * @param {YAHOO.widget.Tab} tab The tab whose index will be returned.
195 proto.getTabIndex = function(tab) {
197 var tabs = this.get('tabs');
198 for (var i = 0, len = tabs.length; i < len; ++i) {
199 if (tab == tabs[i]) {
209 * Removes the specified Tab from the TabView.
211 * @param {YAHOO.widget.Tab} item The Tab instance to be removed.
214 proto.removeTab = function(tab) {
215 var tabCount = this.get('tabs').length;
217 var index = this.getTabIndex(tab);
218 var nextIndex = index + 1;
219 if ( tab == this.get('activeTab') ) { // select next tab
221 if (index + 1 == tabCount) {
222 this.set('activeIndex', index - 1);
224 this.set('activeIndex', index + 1);
229 this._tabParent.removeChild( tab.get('element') );
230 this._contentParent.removeChild( tab.get('contentEl') );
231 this._configs.tabs.value.splice(index, 1);
236 * Provides a readable name for the TabView instance.
240 proto.toString = function() {
241 var name = this.get('id') || this.get('tagName');
242 return "TabView " + name;
246 * The transiton to use when switching between tabs.
247 * @method contentTransition
249 proto.contentTransition = function(newTab, oldTab) {
250 newTab.set('contentVisible', true);
251 oldTab.set('contentVisible', false);
255 * setAttributeConfigs TabView specific properties.
256 * @method initAttributes
257 * @param {Object} attr Hash of initial attributes
259 proto.initAttributes = function(attr) {
260 YAHOO.widget.TabView.superclass.initAttributes.call(this, attr);
262 if (!attr.orientation) {
263 attr.orientation = 'top';
266 var el = this.get('element');
268 if (!YAHOO.util.Dom.hasClass(el, this.CLASSNAME)) {
269 YAHOO.util.Dom.addClass(el, this.CLASSNAME);
273 * The Tabs belonging to the TabView instance.
277 this.setAttributeConfig('tabs', {
283 * The container of the tabView's label elements.
284 * @property _tabParent
289 this.getElementsByClassName(this.TAB_PARENT_CLASSNAME,
290 'ul' )[0] || _createTabParent.call(this);
293 * The container of the tabView's content elements.
294 * @property _contentParent
298 this._contentParent =
299 this.getElementsByClassName(this.CONTENT_PARENT_CLASSNAME,
300 'div')[0] || _createContentParent.call(this);
303 * How the Tabs should be oriented relative to the TabView.
304 * @attribute orientation
308 this.setAttributeConfig('orientation', {
309 value: attr.orientation,
310 method: function(value) {
311 var current = this.get('orientation');
312 this.addClass('yui-navset-' + value);
314 if (current != value) {
315 this.removeClass('yui-navset-' + current);
320 this.appendChild(this._tabParent);
327 * The index of the tab currently active.
328 * @attribute activeIndex
331 this.setAttributeConfig('activeIndex', {
332 value: attr.activeIndex,
333 method: function(value) {
334 this.set('activeTab', this.getTab(value));
336 validator: function(value) {
337 return !this.getTab(value).get('disabled'); // cannot activate if disabled
342 * The tab currently active.
343 * @attribute activeTab
344 * @type YAHOO.widget.Tab
346 this.setAttributeConfig('activeTab', {
347 value: attr.activeTab,
348 method: function(tab) {
349 var activeTab = this.get('activeTab');
352 tab.set('active', true);
353 this._configs['activeIndex'].value = this.getTabIndex(tab); // keep in sync
356 if (activeTab && activeTab != tab) {
357 activeTab.set('active', false);
360 if (activeTab && tab != activeTab) { // no transition if only 1
361 this.contentTransition(tab, activeTab);
363 tab.set('contentVisible', true);
366 validator: function(value) {
367 return !value.get('disabled'); // cannot activate if disabled
371 if ( this._tabParent ) {
372 _initTabs.call(this);
375 // Due to delegation we add all DOM_EVENTS to the TabView container
376 // but IE will leak when unsupported events are added, so remove these
377 this.DOM_EVENTS.submit = false;
378 this.DOM_EVENTS.focus = false;
379 this.DOM_EVENTS.blur = false;
381 for (var type in this.DOM_EVENTS) {
382 if ( YAHOO.lang.hasOwnProperty(this.DOM_EVENTS, type) ) {
383 this.addListener.call(this, type, this.DOMEventHandler);
389 * Creates Tab instances from a collection of HTMLElements.
394 var _initTabs = function() {
399 var el = this.get('element');
400 var tabs = _getChildNodes(this._tabParent);
401 var contentElements = _getChildNodes(this._contentParent);
403 for (var i = 0, len = tabs.length; i < len; ++i) {
406 if (contentElements[i]) {
407 attr.contentEl = contentElements[i];
410 tab = new YAHOO.widget.Tab(tabs[i], attr);
413 if (tab.hasClass(tab.ACTIVE_CLASSNAME) ) {
414 this._configs.activeTab.value = tab; // dont invoke method
415 this._configs.activeIndex.value = this.getTabIndex(tab);
420 var _createTabViewElement = function(attr) {
421 var el = document.createElement('div');
423 if ( this.CLASSNAME ) {
424 el.className = this.CLASSNAME;
430 var _createTabParent = function(attr) {
431 var el = document.createElement('ul');
433 if ( this.TAB_PARENT_CLASSNAME ) {
434 el.className = this.TAB_PARENT_CLASSNAME;
437 this.get('element').appendChild(el);
442 var _createContentParent = function(attr) {
443 var el = document.createElement('div');
445 if ( this.CONTENT_PARENT_CLASSNAME ) {
446 el.className = this.CONTENT_PARENT_CLASSNAME;
449 this.get('element').appendChild(el);
454 var _getChildNodes = function(el) {
456 var childNodes = el.childNodes;
458 for (var i = 0, len = childNodes.length; i < len; ++i) {
459 if (childNodes[i].nodeType == 1) {
460 nodes[nodes.length] = childNodes[i];
469 var Dom = YAHOO.util.Dom,
470 Event = YAHOO.util.Event;
473 * A representation of a Tab's label and content.
474 * @namespace YAHOO.widget
476 * @extends YAHOO.util.Element
478 * @param element {HTMLElement | String} (optional) The html element that
479 * represents the TabView. An element will be created if none provided.
480 * @param {Object} properties A key map of initial properties
482 var Tab = function(el, attr) {
484 if (arguments.length == 1 && !YAHOO.lang.isString(el) && !el.nodeName) {
489 if (!el && !attr.element) {
490 el = _createTabElement.call(this, attr);
494 success: function(o) {
495 this.set('content', o.responseText);
497 failure: function(o) {
501 Tab.superclass.constructor.call(this, el, attr);
503 this.DOM_EVENTS = {}; // delegating to tabView
506 YAHOO.extend(Tab, YAHOO.util.Element);
507 var proto = Tab.prototype;
510 * The default tag name for a Tab's inner element.
511 * @property LABEL_INNER_TAGNAME
515 proto.LABEL_TAGNAME = 'em';
518 * The class name applied to active tabs.
519 * @property ACTIVE_CLASSNAME
521 * @default "selected"
523 proto.ACTIVE_CLASSNAME = 'selected';
526 * The title applied to active tabs.
527 * @property ACTIVE_TITLE
531 proto.ACTIVE_TITLE = 'active';
534 * The class name applied to disabled tabs.
535 * @property DISABLED_CLASSNAME
537 * @default "disabled"
539 proto.DISABLED_CLASSNAME = 'disabled';
542 * The class name applied to dynamic tabs while loading.
543 * @property LOADING_CLASSNAME
545 * @default "disabled"
547 proto.LOADING_CLASSNAME = 'loading';
550 * Provides a reference to the connection request object when data is
551 * loaded dynamically.
552 * @property dataConnection
555 proto.dataConnection = null;
558 * Object containing success and failure callbacks for loading data.
559 * @property loadHandler
562 proto.loadHandler = null;
564 proto._loading = false;
567 * Provides a readable name for the tab.
571 proto.toString = function() {
572 var el = this.get('element');
573 var id = el.id || el.tagName;
578 * setAttributeConfigs TabView specific properties.
579 * @method initAttributes
580 * @param {Object} attr Hash of initial attributes
582 proto.initAttributes = function(attr) {
584 Tab.superclass.initAttributes.call(this, attr);
586 var el = this.get('element');
589 * The event that triggers the tab's activation.
590 * @attribute activationEvent
593 this.setAttributeConfig('activationEvent', {
594 value: attr.activationEvent || 'click'
598 * The element that contains the tab's label.
602 this.setAttributeConfig('labelEl', {
603 value: attr.labelEl || _getlabelEl.call(this),
604 method: function(value) {
605 var current = this.get('labelEl');
608 if (current == value) {
609 return false; // already set
612 this.replaceChild(value, current);
613 } else if (el.firstChild) { // ensure label is firstChild by default
614 this.insertBefore(value, el.firstChild);
616 this.appendChild(value);
622 * The tab's label text (or innerHTML).
626 this.setAttributeConfig('label', {
627 value: attr.label || _getLabel.call(this),
628 method: function(value) {
629 var labelEl = this.get('labelEl');
630 if (!labelEl) { // create if needed
631 this.set('labelEl', _createlabelEl.call(this));
634 _setLabel.call(this, value);
639 * The HTMLElement that contains the tab's content.
640 * @attribute contentEl
643 this.setAttributeConfig('contentEl', {
644 value: attr.contentEl || document.createElement('div'),
645 method: function(value) {
646 var current = this.get('contentEl');
649 if (current == value) {
650 return false; // already set
652 this.replaceChild(value, current);
662 this.setAttributeConfig('content', {
664 method: function(value) {
665 this.get('contentEl').innerHTML = value;
669 var _dataLoaded = false;
672 * The tab's data source, used for loading content dynamically.
676 this.setAttributeConfig('dataSrc', {
681 * Whether or not content should be reloaded for every view.
682 * @attribute cacheData
686 this.setAttributeConfig('cacheData', {
687 value: attr.cacheData || false,
688 validator: YAHOO.lang.isBoolean
692 * The method to use for the data request.
693 * @attribute loadMethod
697 this.setAttributeConfig('loadMethod', {
698 value: attr.loadMethod || 'GET',
699 validator: YAHOO.lang.isString
703 * Whether or not any data has been loaded from the server.
704 * @attribute dataLoaded
707 this.setAttributeConfig('dataLoaded', {
709 validator: YAHOO.lang.isBoolean,
714 * Number if milliseconds before aborting and calling failure handler.
715 * @attribute dataTimeout
719 this.setAttributeConfig('dataTimeout', {
720 value: attr.dataTimeout || null,
721 validator: YAHOO.lang.isNumber
725 * Whether or not the tab is currently active.
726 * If a dataSrc is set for the tab, the content will be loaded from
731 this.setAttributeConfig('active', {
732 value: attr.active || this.hasClass(this.ACTIVE_CLASSNAME),
733 method: function(value) {
734 if (value === true) {
735 this.addClass(this.ACTIVE_CLASSNAME);
736 this.set('title', this.ACTIVE_TITLE);
738 this.removeClass(this.ACTIVE_CLASSNAME);
739 this.set('title', '');
742 validator: function(value) {
743 return YAHOO.lang.isBoolean(value) && !this.get('disabled') ;
748 * Whether or not the tab is disabled.
749 * @attribute disabled
752 this.setAttributeConfig('disabled', {
753 value: attr.disabled || this.hasClass(this.DISABLED_CLASSNAME),
754 method: function(value) {
755 if (value === true) {
756 Dom.addClass(this.get('element'), this.DISABLED_CLASSNAME);
758 Dom.removeClass(this.get('element'), this.DISABLED_CLASSNAME);
761 validator: YAHOO.lang.isBoolean
765 * The href of the tab's anchor element.
770 this.setAttributeConfig('href', {
772 this.getElementsByTagName('a')[0].getAttribute('href', 2) || '#',
773 method: function(value) {
774 this.getElementsByTagName('a')[0].href = value;
776 validator: YAHOO.lang.isString
780 * The Whether or not the tab's content is visible.
781 * @attribute contentVisible
785 this.setAttributeConfig('contentVisible', {
786 value: attr.contentVisible,
787 method: function(value) {
789 this.get('contentEl').style.display = 'block';
791 if ( this.get('dataSrc') ) {
792 // load dynamic content unless already loading or loaded and caching
793 if ( !this._loading && !(this.get('dataLoaded') && this.get('cacheData')) ) {
794 _dataConnect.call(this);
798 this.get('contentEl').style.display = 'none';
801 validator: YAHOO.lang.isBoolean
805 var _createTabElement = function(attr) {
806 var el = document.createElement('li');
807 var a = document.createElement('a');
809 a.href = attr.href || '#';
813 var label = attr.label || null;
814 var labelEl = attr.labelEl || null;
816 if (labelEl) { // user supplied labelEl
817 if (!label) { // user supplied label
818 label = _getLabel.call(this, labelEl);
821 labelEl = _createlabelEl.call(this);
824 a.appendChild(labelEl);
829 var _getlabelEl = function() {
830 return this.getElementsByTagName(this.LABEL_TAGNAME)[0];
833 var _createlabelEl = function() {
834 var el = document.createElement(this.LABEL_TAGNAME);
838 var _setLabel = function(label) {
839 var el = this.get('labelEl');
840 el.innerHTML = label;
843 var _getLabel = function() {
845 el = this.get('labelEl');
854 var _dataConnect = function() {
855 if (!YAHOO.util.Connect) {
859 Dom.addClass(this.get('contentEl').parentNode, this.LOADING_CLASSNAME);
860 this._loading = true;
861 this.dataConnection = YAHOO.util.Connect.asyncRequest(
862 this.get('loadMethod'),
865 success: function(o) {
866 this.loadHandler.success.call(this, o);
867 this.set('dataLoaded', true);
868 this.dataConnection = null;
869 Dom.removeClass(this.get('contentEl').parentNode,
870 this.LOADING_CLASSNAME);
871 this._loading = false;
873 failure: function(o) {
874 this.loadHandler.failure.call(this, o);
875 this.dataConnection = null;
876 Dom.removeClass(this.get('contentEl').parentNode,
877 this.LOADING_CLASSNAME);
878 this._loading = false;
881 timeout: this.get('dataTimeout')
886 YAHOO.widget.Tab = Tab;
889 YAHOO.register("tabview", YAHOO.widget.TabView, {version: "2.5.2", build: "1076"});