2 Copyright (c) 2009, 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
19 document = window.document,
23 ACTIVE_INDEX = 'activeIndex',
24 ACTIVE_TAB = 'activeTab',
25 CONTENT_EL = 'contentEl',
29 * A widget to control tabbed views.
30 * @namespace YAHOO.widget
32 * @extends YAHOO.util.Element
34 * @param {HTMLElement | String | Object} el(optional) The html
35 * element that represents the TabView, or the attribute object to use.
36 * An element will be created if none provided.
37 * @param {Object} attr (optional) A key map of the tabView's
38 * initial attributes. Ignored if first arg is attributes object.
40 TabView = function(el, attr) {
42 if (arguments.length == 1 && !YAHOO.lang.isString(el) && !el.nodeName) {
43 attr = el; // treat first arg as attr object
44 el = attr.element || null;
47 if (!el && !attr.element) { // create if we dont have one
48 el = this._createTabViewElement(attr);
50 TabView.superclass.constructor.call(this, el, attr);
53 YAHOO.extend(TabView, Y.Element, {
55 * The className to add when building from scratch.
59 CLASSNAME: 'yui-navset',
62 * The className of the HTMLElement containing the TabView's tab elements
63 * to look for when building from existing markup, or to add when building
65 * All childNodes of the tab container are treated as Tabs when building
66 * from existing markup.
67 * @property TAB_PARENT_CLASSNAME
70 TAB_PARENT_CLASSNAME: 'yui-nav',
73 * The className of the HTMLElement containing the TabView's label elements
74 * to look for when building from existing markup, or to add when building
76 * All childNodes of the content container are treated as content elements when
77 * building from existing markup.
78 * @property CONTENT_PARENT_CLASSNAME
79 * @default "nav-content"
81 CONTENT_PARENT_CLASSNAME: 'yui-content',
87 * Adds a Tab to the TabView instance.
88 * If no index is specified, the tab is added to the end of the tab list.
90 * @param {YAHOO.widget.Tab} tab A Tab instance to add.
91 * @param {Integer} index The position to add the tab.
94 addTab: function(tab, index) {
95 var tabs = this.get('tabs'),
96 before = this.getTab(index),
97 tabParent = this._tabParent,
98 contentParent = this._contentParent,
99 tabElement = tab.get(ELEMENT),
100 contentEl = tab.get(CONTENT_EL);
102 if (!tabs) { // not ready yet
103 this._queue[this._queue.length] = ['addTab', arguments];
107 index = (index === undefined) ? tabs.length : index;
110 tabParent.insertBefore(tabElement, before.get(ELEMENT));
112 tabParent.appendChild(tabElement);
115 if ( contentEl && !Dom.isAncestor(contentParent, contentEl) ) {
116 contentParent.appendChild(contentEl);
119 if ( !tab.get(ACTIVE) ) {
120 tab.set('contentVisible', false, true); /* hide if not active */
122 this.set(ACTIVE_TAB, tab, true);
126 this._initTabEvents(tab);
127 tabs.splice(index, 0, tab);
130 _initTabEvents: function(tab) {
131 tab.addListener( tab.get('activationEvent'), tab._onActivate, this, tab);
133 tab.addListener('activationEventChange', function(e) {
134 if (e.prevValue != e.newValue) {
135 tab.removeListener(e.prevValue, tab._onActivate);
136 tab.addListener(e.newValue, tab._onActivate, this, tab);
142 * Routes childNode events.
143 * @method DOMEventHandler
144 * @param {event} e The Dom event that is being handled.
147 DOMEventHandler: function(e) {
148 var target = Event.getTarget(e),
149 tabParent = this._tabParent,
150 tabs = this.get('tabs'),
156 if (Dom.isAncestor(tabParent, target) ) {
157 for (var i = 0, len = tabs.length; i < len; i++) {
158 tabEl = tabs[i].get(ELEMENT);
159 contentEl = tabs[i].get(CONTENT_EL);
161 if ( target == tabEl || Dom.isAncestor(tabEl, target) ) {
168 tab.fireEvent(e.type, e);
174 * Returns the Tab instance at the specified index.
176 * @param {Integer} index The position of the Tab.
177 * @return YAHOO.widget.Tab
179 getTab: function(index) {
180 return this.get('tabs')[index];
184 * Returns the index of given tab.
185 * @method getTabIndex
186 * @param {YAHOO.widget.Tab} tab The tab whose index will be returned.
189 getTabIndex: function(tab) {
191 tabs = this.get('tabs');
192 for (var i = 0, len = tabs.length; i < len; ++i) {
193 if (tab == tabs[i]) {
203 * Removes the specified Tab from the TabView.
205 * @param {YAHOO.widget.Tab} item The Tab instance to be removed.
208 removeTab: function(tab) {
209 var tabCount = this.get('tabs').length,
210 index = this.getTabIndex(tab);
212 if ( tab === this.get(ACTIVE_TAB) ) {
213 if (tabCount > 1) { // select another tab
214 if (index + 1 === tabCount) { // if last, activate previous
215 this.set(ACTIVE_INDEX, index - 1);
216 } else { // activate next tab
217 this.set(ACTIVE_INDEX, index + 1);
219 } else { // no more tabs
220 this.set(ACTIVE_TAB, null);
224 this._tabParent.removeChild( tab.get(ELEMENT) );
225 this._contentParent.removeChild( tab.get(CONTENT_EL) );
226 this._configs.tabs.value.splice(index, 1);
228 tab.fireEvent('remove', { type: 'remove', tabview: this });
232 * Provides a readable name for the TabView instance.
236 toString: function() {
237 var name = this.get('id') || this.get('tagName');
238 return "TabView " + name;
242 * The transiton to use when switching between tabs.
243 * @method contentTransition
245 contentTransition: function(newTab, oldTab) {
247 newTab.set('contentVisible', true);
250 oldTab.set('contentVisible', false);
255 * setAttributeConfigs TabView specific properties.
256 * @method initAttributes
257 * @param {Object} attr Hash of initial attributes
259 initAttributes: function(attr) {
260 TabView.superclass.initAttributes.call(this, attr);
262 if (!attr.orientation) {
263 attr.orientation = 'top';
266 var el = this.get(ELEMENT);
268 if (!Dom.hasClass(el, this.CLASSNAME)) {
269 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] || this._createTabParent();
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] || this._createContentParent();
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);
318 if (value === 'bottom') {
319 this.appendChild(this._tabParent);
325 * The index of the tab currently active.
326 * @attribute activeIndex
329 this.setAttributeConfig(ACTIVE_INDEX, {
330 value: attr.activeIndex,
331 method: function(value) {
333 validator: function(value) {
335 if (value && this.getTab(value).get('disabled')) { // cannot activate if disabled
343 * The tab currently active.
344 * @attribute activeTab
345 * @type YAHOO.widget.Tab
347 this.setAttributeConfig(ACTIVE_TAB, {
348 value: attr.activeTab,
349 method: function(tab) {
350 var activeTab = this.get(ACTIVE_TAB);
353 tab.set(ACTIVE, true);
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) {
368 if (value && value.get('disabled')) { // cannot activate if disabled
375 this.on('activeTabChange', this._onActiveTabChange);
376 this.on('activeIndexChange', this._onActiveIndexChange);
378 YAHOO.log('attributes initialized', 'info', 'TabView');
379 if ( this._tabParent ) {
383 // Due to delegation we add all DOM_EVENTS to the TabView container
384 // but IE will leak when unsupported events are added, so remove these
385 this.DOM_EVENTS.submit = false;
386 this.DOM_EVENTS.focus = false;
387 this.DOM_EVENTS.blur = false;
389 for (var type in this.DOM_EVENTS) {
390 if ( YAHOO.lang.hasOwnProperty(this.DOM_EVENTS, type) ) {
391 this.addListener.call(this, type, this.DOMEventHandler);
397 * Removes selected state from the given tab if it is the activeTab
398 * @method deselectTab
399 * @param {Int} index The tab index to deselect
401 deselectTab: function(index) {
402 if (this.getTab(index) === this.get('activeTab')) {
403 this.set('activeTab', null);
408 * Makes the tab at the given index the active tab
410 * @param {Int} index The tab index to be made active
412 selectTab: function(index) {
413 this.set('activeTab', this.getTab(index));
416 _onActiveTabChange: function(e) {
417 var activeIndex = this.get(ACTIVE_INDEX),
418 newIndex = this.getTabIndex(e.newValue);
420 if (activeIndex !== newIndex) {
421 if (!(this.set(ACTIVE_INDEX, newIndex)) ) { // NOTE: setting
422 // revert if activeIndex update fails (cancelled via beforeChange)
423 this.set(ACTIVE_TAB, e.prevValue);
428 _onActiveIndexChange: function(e) {
429 // no set if called from ActiveTabChange event
430 if (e.newValue !== this.getTabIndex(this.get(ACTIVE_TAB))) {
431 if (!(this.set(ACTIVE_TAB, this.getTab(e.newValue))) ) { // NOTE: setting
432 // revert if activeTab update fails (cancelled via beforeChange)
433 this.set(ACTIVE_INDEX, e.prevValue);
439 * Creates Tab instances from a collection of HTMLElements.
444 _initTabs: function() {
445 var tabs = Dom.getChildren(this._tabParent),
446 contentElements = Dom.getChildren(this._contentParent),
447 activeIndex = this.get(ACTIVE_INDEX),
452 for (var i = 0, len = tabs.length; i < len; ++i) {
455 if (contentElements[i]) {
456 attr.contentEl = contentElements[i];
459 tab = new YAHOO.widget.Tab(tabs[i], attr);
462 if (tab.hasClass(tab.ACTIVE_CLASSNAME) ) {
467 this.set(ACTIVE_TAB, this.getTab(activeIndex));
469 this._configs.activeTab.value = active; // dont invoke method
470 this._configs.activeIndex.value = this.getTabIndex(active);
474 _createTabViewElement: function(attr) {
475 var el = document.createElement('div');
477 if ( this.CLASSNAME ) {
478 el.className = this.CLASSNAME;
481 YAHOO.log('TabView Dom created', 'info', 'TabView');
485 _createTabParent: function(attr) {
486 var el = document.createElement('ul');
488 if ( this.TAB_PARENT_CLASSNAME ) {
489 el.className = this.TAB_PARENT_CLASSNAME;
492 this.get(ELEMENT).appendChild(el);
497 _createContentParent: function(attr) {
498 var el = document.createElement('div');
500 if ( this.CONTENT_PARENT_CLASSNAME ) {
501 el.className = this.CONTENT_PARENT_CLASSNAME;
504 this.get(ELEMENT).appendChild(el);
511 YAHOO.widget.TabView = TabView;
521 ACTIVE_TAB = 'activeTab',
523 LABEL_EL = 'labelEl',
525 CONTENT_EL = 'contentEl',
527 CACHE_DATA = 'cacheData',
528 DATA_SRC = 'dataSrc',
529 DATA_LOADED = 'dataLoaded',
530 DATA_TIMEOUT = 'dataTimeout',
531 LOAD_METHOD = 'loadMethod',
532 POST_DATA = 'postData',
533 DISABLED = 'disabled',
536 * A representation of a Tab's label and content.
537 * @namespace YAHOO.widget
539 * @extends YAHOO.util.Element
541 * @param element {HTMLElement | String} (optional) The html element that
542 * represents the TabView. An element will be created if none provided.
543 * @param {Object} properties A key map of initial properties
545 Tab = function(el, attr) {
547 if (arguments.length == 1 && !Lang.isString(el) && !el.nodeName) {
552 if (!el && !attr.element) {
553 el = this._createTabElement(attr);
557 success: function(o) {
558 this.set(CONTENT, o.responseText);
560 failure: function(o) {
564 Tab.superclass.constructor.call(this, el, attr);
566 this.DOM_EVENTS = {}; // delegating to tabView
569 YAHOO.extend(Tab, YAHOO.util.Element, {
571 * The default tag name for a Tab's inner element.
572 * @property LABEL_INNER_TAGNAME
579 * The class name applied to active tabs.
580 * @property ACTIVE_CLASSNAME
582 * @default "selected"
584 ACTIVE_CLASSNAME: 'selected',
587 * The class name applied to active tabs.
588 * @property ACTIVE_CLASSNAME
590 * @default "selected"
592 HIDDEN_CLASSNAME: 'yui-hidden',
595 * The title applied to active tabs.
596 * @property ACTIVE_TITLE
600 ACTIVE_TITLE: 'active',
603 * The class name applied to disabled tabs.
604 * @property DISABLED_CLASSNAME
606 * @default "disabled"
608 DISABLED_CLASSNAME: DISABLED,
611 * The class name applied to dynamic tabs while loading.
612 * @property LOADING_CLASSNAME
614 * @default "disabled"
616 LOADING_CLASSNAME: 'loading',
619 * Provides a reference to the connection request object when data is
620 * loaded dynamically.
621 * @property dataConnection
624 dataConnection: null,
627 * Object containing success and failure callbacks for loading data.
628 * @property loadHandler
636 * Provides a readable name for the tab.
640 toString: function() {
641 var el = this.get(ELEMENT),
642 id = el.id || el.tagName;
647 * setAttributeConfigs TabView specific properties.
648 * @method initAttributes
649 * @param {Object} attr Hash of initial attributes
651 initAttributes: function(attr) {
653 Tab.superclass.initAttributes.call(this, attr);
656 * The event that triggers the tab's activation.
657 * @attribute activationEvent
660 this.setAttributeConfig('activationEvent', {
661 value: attr.activationEvent || 'click'
665 * The element that contains the tab's label.
669 this.setAttributeConfig(LABEL_EL, {
670 value: attr[LABEL_EL] || this._getLabelEl(),
671 method: function(value) {
672 value = Dom.get(value);
673 var current = this.get(LABEL_EL);
676 if (current == value) {
677 return false; // already set
680 current.parentNode.replaceChild(value, current);
681 this.set(LABEL, value.innerHTML);
687 * The tab's label text (or innerHTML).
691 this.setAttributeConfig(LABEL, {
692 value: attr.label || this._getLabel(),
693 method: function(value) {
694 var labelEl = this.get(LABEL_EL);
695 if (!labelEl) { // create if needed
696 this.set(LABEL_EL, this._createLabelEl());
699 labelEl.innerHTML = value;
704 * The HTMLElement that contains the tab's content.
705 * @attribute contentEl
708 this.setAttributeConfig(CONTENT_EL, {
709 value: attr[CONTENT_EL] || document.createElement('div'),
710 method: function(value) {
711 value = Dom.get(value);
712 var current = this.get(CONTENT_EL);
715 if (current === value) {
716 return false; // already set
718 if (!this.get('selected')) {
719 Dom.addClass(value, 'yui-hidden');
721 current.parentNode.replaceChild(value, current);
722 this.set(CONTENT, value.innerHTML);
732 this.setAttributeConfig(CONTENT, {
733 value: attr[CONTENT],
734 method: function(value) {
735 this.get(CONTENT_EL).innerHTML = value;
740 * The tab's data source, used for loading content dynamically.
744 this.setAttributeConfig(DATA_SRC, {
749 * Whether or not content should be reloaded for every view.
750 * @attribute cacheData
754 this.setAttributeConfig(CACHE_DATA, {
755 value: attr.cacheData || false,
756 validator: Lang.isBoolean
760 * The method to use for the data request.
761 * @attribute loadMethod
765 this.setAttributeConfig(LOAD_METHOD, {
766 value: attr.loadMethod || 'GET',
767 validator: Lang.isString
771 * Whether or not any data has been loaded from the server.
772 * @attribute dataLoaded
775 this.setAttributeConfig(DATA_LOADED, {
777 validator: Lang.isBoolean,
782 * Number if milliseconds before aborting and calling failure handler.
783 * @attribute dataTimeout
787 this.setAttributeConfig(DATA_TIMEOUT, {
788 value: attr.dataTimeout || null,
789 validator: Lang.isNumber
793 * Arguments to pass when POST method is used
794 * @attribute postData
797 this.setAttributeConfig(POST_DATA, {
798 value: attr.postData || null
802 * Whether or not the tab is currently active.
803 * If a dataSrc is set for the tab, the content will be loaded from
808 this.setAttributeConfig('active', {
809 value: attr.active || this.hasClass(this.ACTIVE_CLASSNAME),
810 method: function(value) {
811 if (value === true) {
812 this.addClass(this.ACTIVE_CLASSNAME);
813 this.set('title', this.ACTIVE_TITLE);
815 this.removeClass(this.ACTIVE_CLASSNAME);
816 this.set('title', '');
819 validator: function(value) {
820 return Lang.isBoolean(value) && !this.get(DISABLED) ;
825 * Whether or not the tab is disabled.
826 * @attribute disabled
829 this.setAttributeConfig(DISABLED, {
830 value: attr.disabled || this.hasClass(this.DISABLED_CLASSNAME),
831 method: function(value) {
832 if (value === true) {
833 Dom.addClass(this.get(ELEMENT), this.DISABLED_CLASSNAME);
835 Dom.removeClass(this.get(ELEMENT), this.DISABLED_CLASSNAME);
838 validator: Lang.isBoolean
842 * The href of the tab's anchor element.
847 this.setAttributeConfig('href', {
849 this.getElementsByTagName('a')[0].getAttribute('href', 2) || '#',
850 method: function(value) {
851 this.getElementsByTagName('a')[0].href = value;
853 validator: Lang.isString
857 * The Whether or not the tab's content is visible.
858 * @attribute contentVisible
862 this.setAttributeConfig('contentVisible', {
863 value: attr.contentVisible,
864 method: function(value) {
866 Dom.removeClass(this.get(CONTENT_EL), this.HIDDEN_CLASSNAME);
868 if ( this.get(DATA_SRC) ) {
869 // load dynamic content unless already loading or loaded and caching
870 if ( !this._loading && !(this.get(DATA_LOADED) && this.get(CACHE_DATA)) ) {
875 Dom.addClass(this.get(CONTENT_EL), this.HIDDEN_CLASSNAME);
878 validator: Lang.isBoolean
880 YAHOO.log('attributes initialized', 'info', 'Tab');
883 _dataConnect: function() {
885 YAHOO.log('YAHOO.util.Connect dependency not met',
890 Dom.addClass(this.get(CONTENT_EL).parentNode, this.LOADING_CLASSNAME);
891 this._loading = true;
892 this.dataConnection = Y.Connect.asyncRequest(
893 this.get(LOAD_METHOD),
896 success: function(o) {
897 YAHOO.log('content loaded successfully', 'info', 'Tab');
898 this.loadHandler.success.call(this, o);
899 this.set(DATA_LOADED, true);
900 this.dataConnection = null;
901 Dom.removeClass(this.get(CONTENT_EL).parentNode,
902 this.LOADING_CLASSNAME);
903 this._loading = false;
905 failure: function(o) {
906 YAHOO.log('loading failed: ' + o.statusText, 'error', 'Tab');
907 this.loadHandler.failure.call(this, o);
908 this.dataConnection = null;
909 Dom.removeClass(this.get(CONTENT_EL).parentNode,
910 this.LOADING_CLASSNAME);
911 this._loading = false;
914 timeout: this.get(DATA_TIMEOUT)
920 _createTabElement: function(attr) {
921 var el = document.createElement('li'),
922 a = document.createElement('a'),
923 label = attr.label || null,
924 labelEl = attr.labelEl || null;
926 a.href = attr.href || '#'; // TODO: Use Dom.setAttribute?
929 if (labelEl) { // user supplied labelEl
930 if (!label) { // user supplied label
931 label = this._getLabel();
934 labelEl = this._createLabelEl();
937 a.appendChild(labelEl);
939 YAHOO.log('creating Tab Dom', 'info', 'Tab');
943 _getLabelEl: function() {
944 return this.getElementsByTagName(this.LABEL_TAGNAME)[0];
947 _createLabelEl: function() {
948 var el = document.createElement(this.LABEL_TAGNAME);
953 _getLabel: function() {
954 var el = this.get(LABEL_EL);
963 _onActivate: function(e, tabview) {
968 Y.Event.preventDefault(e);
969 if (tab === tabview.get(ACTIVE_TAB)) {
970 silent = true; // dont fire activeTabChange if already active
972 tabview.set(ACTIVE_TAB, tab, silent);
978 * Fires when a tab is removed from the tabview
981 * @param {Event} An event object with fields for "type" ("remove")
982 * and "tabview" (the tabview instance it was removed from)
985 YAHOO.widget.Tab = Tab;
988 YAHOO.register("tabview", YAHOO.widget.TabView, {version: "2.7.0", build: "1799"});