2 Copyright (c) 2006, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 isArray: function(val) { // frames lose type, so test constructor string
9 if (val.constructor && val.constructor.toString().indexOf('Array') > -1) {
12 return YAHOO.util.Lang.isObject(val) && val.constructor == Array;
16 isBoolean: function(val) {
17 return typeof val == 'boolean';
20 isFunction: function(val) {
21 return typeof val == 'function';
24 isNull: function(val) {
28 isNumber: function(val) {
32 isObject: function(val) {
33 return typeof val == 'object' || YAHOO.util.Lang.isFunction(val);
36 isString: function(val) {
37 return typeof val == 'string';
40 isUndefined: function(val) {
41 return typeof val == 'undefined';
46 * Provides Attribute configurations.
47 * @namespace YAHOO.util
50 * @param hash {Object} The intial Attribute.
51 * @param {YAHOO.util.AttributeProvider} The owner of the Attribute instance.
54 YAHOO.util.Attribute = function(hash, owner) {
57 this.configure(hash, true);
61 YAHOO.util.Attribute.prototype = {
63 * The name of the attribute.
70 * The value of the attribute.
77 * The owner of the attribute.
79 * @type YAHOO.util.AttributeProvider
84 * Whether or not the attribute is read only.
91 * Whether or not the attribute can only be written once.
98 * The attribute's initial configuration.
100 * @property _initialConfig
103 _initialConfig: null,
106 * Whether or not the attribute's value has been set.
114 * The method to use when setting the attribute's value.
115 * The method recieves the new value as the only argument.
122 * The validator to use when setting the attribute's value.
123 * @property validator
130 * Retrieves the current value of the attribute.
132 * @return {any} The current value of the attribute.
134 getValue: function() {
139 * Sets the value of the attribute and fires beforeChange and change events.
141 * @param {Any} value The value to apply to the attribute.
142 * @param {Boolean} silent If true the change events will not be fired.
143 * @return {Boolean} Whether or not the value was set.
145 setValue: function(value, silent) {
147 var owner = this.owner;
148 var name = this.name;
152 prevValue: this.getValue(),
156 if (this.readOnly || ( this.writeOnce && this._written) ) {
157 return false; // write not allowed
160 if (this.validator && !this.validator.call(owner, value) ) {
161 return false; // invalid value
165 beforeRetVal = owner.fireBeforeChangeEvent(event);
166 if (beforeRetVal === false) {
167 YAHOO.log('setValue ' + name +
168 'cancelled by beforeChange event', 'info', 'Attribute');
174 this.method.call(owner, value);
178 this._written = true;
183 this.owner.fireChangeEvent(event);
190 * Allows for configuring the Attribute's properties.
192 * @param {Object} map A key-value map of Attribute properties.
193 * @param {Boolean} init Whether or not this should become the initial config.
195 configure: function(map, init) {
197 this._written = false; // reset writeOnce
198 this._initialConfig = this._initialConfig || {};
200 for (var key in map) {
201 if ( key && map.hasOwnProperty(key) ) {
202 this[key] = map[key];
204 this._initialConfig[key] = map[key];
211 * Resets the value to the initial config value.
213 * @return {Boolean} Whether or not the value was set.
215 resetValue: function() {
216 return this.setValue(this._initialConfig.value);
220 * Resets the attribute config to the initial config state.
221 * @method resetConfig
223 resetConfig: function() {
224 this.configure(this._initialConfig);
228 * Resets the value to the current value.
229 * Useful when values may have gotten out of sync with actual properties.
231 * @return {Boolean} Whether or not the value was set.
233 refresh: function(silent) {
234 this.setValue(this.value, silent);
239 var Lang = YAHOO.util.Lang;
242 Copyright (c) 2006, Yahoo! Inc. All rights reserved.
243 Code licensed under the BSD License:
244 http://developer.yahoo.net/yui/license.txt
248 * Provides and manages YAHOO.util.Attribute instances
249 * @namespace YAHOO.util
250 * @class AttributeProvider
251 * @uses YAHOO.util.EventProvider
253 YAHOO.util.AttributeProvider = function() {};
255 YAHOO.util.AttributeProvider.prototype = {
258 * A key-value map of Attribute configurations
260 * @protected (may be used by subclasses and augmentors)
266 * Returns the current value of the attribute.
268 * @param {String} key The attribute whose value will be returned.
271 var configs = this._configs || {};
272 var config = configs[key];
275 YAHOO.log(key + ' not found', 'error', 'AttributeProvider');
283 * Sets the value of a config.
285 * @param {String} key The name of the attribute
286 * @param {Any} value The value to apply to the attribute
287 * @param {Boolean} silent Whether or not to suppress change events
288 * @return {Boolean} Whether or not the value was set.
290 set: function(key, value, silent){
291 var configs = this._configs || {};
292 var config = configs[key];
295 YAHOO.log('set failed: ' + key + ' not found',
296 'error', 'AttributeProvider');
300 return config.setValue(value, silent);
304 * Returns an array of attribute names.
305 * @method getAttributeKeys
306 * @return {Array} An array of attribute names.
308 getAttributeKeys: function(){
309 var configs = this._configs;
312 for (var key in configs) {
313 config = configs[key];
314 if ( configs.hasOwnProperty(key) &&
315 !Lang.isUndefined(config) ) {
316 keys[keys.length] = key;
324 * Sets multiple attribute values.
325 * @method setAttributes
326 * @param {Object} map A key-value map of attributes
327 * @param {Boolean} silent Whether or not to suppress change events
329 setAttributes: function(map, silent){
330 for (var key in map) {
331 if ( map.hasOwnProperty(key) ) {
332 this.set(key, map[key], silent);
338 * Resets the specified attribute's value to its initial value.
340 * @param {String} key The name of the attribute
341 * @param {Boolean} silent Whether or not to suppress change events
342 * @return {Boolean} Whether or not the value was set
344 resetValue: function(key, silent){
345 var configs = this._configs || {};
347 this.set(key, configs[key]._initialConfig.value, silent);
354 * Sets the attribute's value to its current value.
356 * @param {String | Array} key The attribute(s) to refresh
357 * @param {Boolean} silent Whether or not to suppress change events
359 refresh: function(key, silent){
360 var configs = this._configs;
362 key = ( ( Lang.isString(key) ) ? [key] : key ) ||
363 this.getAttributeKeys();
365 for (var i = 0, len = key.length; i < len; ++i) {
366 if ( // only set if there is a value and not null
368 ! Lang.isUndefined(configs[key[i]].value) &&
369 ! Lang.isNull(configs[key[i]].value) ) {
370 configs[key[i]].refresh(silent);
376 * Adds an Attribute to the AttributeProvider instance.
378 * @param {String} key The attribute's name
379 * @param {Object} map A key-value map containing the
380 * attribute's properties.
382 register: function(key, map) {
383 this._configs = this._configs || {};
385 if (this._configs[key]) { // dont override
390 this._configs[key] = new YAHOO.util.Attribute(map, this);
395 * Returns the attribute's properties.
396 * @method getAttributeConfig
397 * @param {String} key The attribute's name
399 * @return {object} A key-value map containing all of the
400 * attribute's properties.
402 getAttributeConfig: function(key) {
403 var configs = this._configs || {};
404 var config = configs[key] || {};
405 var map = {}; // returning a copy to prevent overrides
407 for (key in config) {
408 if ( config.hasOwnProperty(key) ) {
409 map[key] = config[key];
417 * Sets or updates an Attribute instance's properties.
418 * @method configureAttribute
419 * @param {String} key The attribute's name.
420 * @param {Object} map A key-value map of attribute properties
421 * @param {Boolean} init Whether or not this should become the intial config.
423 configureAttribute: function(key, map, init) {
424 var configs = this._configs || {};
427 YAHOO.log('unable to configure, ' + key + ' not found',
428 'error', 'AttributeProvider');
432 configs[key].configure(map, init);
436 * Resets an attribute to its intial configuration.
437 * @method resetAttributeConfig
438 * @param {String} key The attribute's name.
441 resetAttributeConfig: function(key){
442 var configs = this._configs || {};
443 configs[key].resetConfig();
447 * Fires the attribute's beforeChange event.
448 * @method fireBeforeChangeEvent
449 * @param {String} key The attribute's name.
450 * @param {Obj} e The event object to pass to handlers.
452 fireBeforeChangeEvent: function(e) {
454 type += e.type.charAt(0).toUpperCase() + e.type.substr(1) + 'Change';
456 return this.fireEvent(e.type, e);
460 * Fires the attribute's change event.
461 * @method fireChangeEvent
462 * @param {String} key The attribute's name.
463 * @param {Obj} e The event object to pass to the handlers.
465 fireChangeEvent: function(e) {
467 return this.fireEvent(e.type, e);
471 YAHOO.augment(YAHOO.util.AttributeProvider, YAHOO.util.EventProvider);
475 // internal shorthand
476 var Dom = YAHOO.util.Dom,
477 Lang = YAHOO.util.Lang,
478 EventPublisher = YAHOO.util.EventPublisher,
479 AttributeProvider = YAHOO.util.AttributeProvider;
482 * Element provides an interface to an HTMLElement's attributes and common
483 * methods. Other commonly used attributes are added as well.
484 * @namespace YAHOO.util
486 * @uses YAHOO.util.AttributeProvider
488 * @param el {HTMLElement | String} The html element that
489 * represents the Element.
490 * @param {Object} map A key-value map of initial config names and values
492 YAHOO.util.Element = function(el, map) {
493 if (arguments.length) {
498 YAHOO.util.Element.prototype = {
500 * Dom events supported by the Element instance.
501 * @property DOM_EVENTS
507 * Wrapper for HTMLElement method.
508 * @method appendChild
509 * @param {Boolean} deep Whether or not to do a deep clone
511 appendChild: function(child) {
512 child = child.get ? child.get('element') : child;
513 this.get('element').appendChild(child);
517 * Wrapper for HTMLElement method.
518 * @method getElementsByTagName
519 * @param {String} tag The tagName to collect
521 getElementsByTagName: function(tag) {
522 return this.get('element').getElementsByTagName(tag);
526 * Wrapper for HTMLElement method.
527 * @method hasChildNodes
528 * @return {Boolean} Whether or not the element has childNodes
530 hasChildNodes: function() {
531 return this.get('element').hasChildNodes();
535 * Wrapper for HTMLElement method.
536 * @method insertBefore
537 * @param {HTMLElement} element The HTMLElement to insert
538 * @param {HTMLElement} before The HTMLElement to insert
539 * the element before.
541 insertBefore: function(element, before) {
542 element = element.get ? element.get('element') : element;
543 before = (before && before.get) ? before.get('element') : before;
545 this.get('element').insertBefore(element, before);
549 * Wrapper for HTMLElement method.
550 * @method removeChild
551 * @param {HTMLElement} child The HTMLElement to remove
553 removeChild: function(child) {
554 child = child.get ? child.get('element') : child;
555 this.get('element').removeChild(child);
560 * Wrapper for HTMLElement method.
561 * @method replaceChild
562 * @param {HTMLElement} newNode The HTMLElement to insert
563 * @param {HTMLElement} oldNode The HTMLElement to replace
565 replaceChild: function(newNode, oldNode) {
566 newNode = newNode.get ? newNode.get('element') : newNode;
567 oldNode = oldNode.get ? oldNode.get('element') : oldNode;
568 return this.get('element').replaceChild(newNode, oldNode);
573 * Registers Element specific attributes.
574 * @method initAttributes
575 * @param {Object} map A key-value map of initial attribute configs
577 initAttributes: function(map) {
579 var element = Dom.get(map.element) || null;
582 * The HTMLElement the Element instance refers to.
586 this.register('element', {
593 * Adds a listener for the given event. These may be DOM or
594 * customEvent listeners. Any event that is fired via fireEvent
595 * can be listened for. All handlers receive an event object.
596 * @method addListener
597 * @param {String} type The name of the event to listen for
598 * @param {Function} fn The handler to call when the event fires
599 * @param {Any} obj A variable to pass to the handler
600 * @param {Object} scope The object to use for the scope of the handler
602 addListener: function(type, fn, obj, scope) {
603 var el = this.get('element');
604 var scope = scope || this;
606 el = this.get('id') || el;
608 if (!this._events[type]) { // create on the fly
609 if ( this.DOM_EVENTS[type] ) {
610 YAHOO.util.Event.addListener(el, type, function(e) {
611 if (e.srcElement && !e.target) { // supplement IE with target
612 e.target = e.srcElement;
614 this.fireEvent(type, e);
618 this.createEvent(type, this);
619 this._events[type] = true;
622 this.subscribe.apply(this, arguments); // notify via customEvent
627 * Alias for addListener
629 * @param {String} type The name of the event to listen for
630 * @param {Function} fn The function call when the event fires
631 * @param {Any} obj A variable to pass to the handler
632 * @param {Object} scope The object to use for the scope of the handler
634 on: function() { this.addListener.apply(this, arguments); },
638 * Remove an event listener
639 * @method removeListener
640 * @param {String} type The name of the event to listen for
641 * @param {Function} fn The function call when the event fires
643 removeListener: function(type, fn) {
644 this.unsubscribe.apply(this, arguments);
648 * Wrapper for Dom method.
650 * @param {String} className The className to add
652 addClass: function(className) {
653 Dom.addClass(this.get('element'), className);
657 * Wrapper for Dom method.
658 * @method getElementsByClassName
659 * @param {String} className The className to collect
660 * @param {String} tag (optional) The tag to use in
661 * conjunction with class name
662 * @return {Array} Array of HTMLElements
664 getElementsByClassName: function(className, tag) {
665 return Dom.getElementsByClassName(className, tag,
666 this.get('element') );
670 * Wrapper for Dom method.
672 * @param {String} className The className to add
673 * @return {Boolean} Whether or not the element has the class name
675 hasClass: function(className) {
676 return Dom.hasClass(this.get('element'), className);
680 * Wrapper for Dom method.
681 * @method removeClass
682 * @param {String} className The className to remove
684 removeClass: function(className) {
685 return Dom.removeClass(this.get('element'), className);
689 * Wrapper for Dom method.
690 * @method replaceClass
691 * @param {String} oldClassName The className to replace
692 * @param {String} newClassName The className to add
694 replaceClass: function(oldClassName, newClassName) {
695 return Dom.replaceClass(this.get('element'),
696 oldClassName, newClassName);
700 * Wrapper for Dom method.
702 * @param {String} property The style property to set
703 * @param {String} value The value to apply to the style property
705 setStyle: function(property, value) {
706 return Dom.setStyle(this.get('element'), property, value);
710 * Wrapper for Dom method.
712 * @param {String} property The style property to retrieve
713 * @return {String} The current value of the property
715 getStyle: function(property) {
716 return Dom.getStyle(this.get('element'), property);
720 * Apply any queued set calls.
723 fireQueue: function() {
724 var queue = this._queue;
725 for (var i = 0, len = queue.length; i < len; ++i) {
726 this[queue[i][0]].apply(this, queue[i][1]);
731 * Appends the HTMLElement into either the supplied parentNode.
733 * @param {HTMLElement | Element} parentNode The node to append to
734 * @param {HTMLElement | Element} before An optional node to insert before
736 appendTo: function(parent, before) {
737 parent = (parent.get) ? parent.get('element') : Dom.get(parent);
739 before = (before && before.get) ?
740 before.get('element') : Dom.get(before);
741 var element = this.get('element');
743 var newAddition = !Dom.inDocument(element);
746 YAHOO.log('appendTo failed: element not available',
752 YAHOO.log('appendTo failed: parent not available',
757 if (element.parent != parent) {
759 parent.insertBefore(element, before);
761 parent.appendChild(element);
765 YAHOO.log(element + 'appended to ' + parent);
768 return false; // note return; no refresh if in document
771 // if a new addition, refresh HTMLElement any applied attributes
772 var keys = this.getAttributeKeys();
774 for (var key in keys) { // only refresh HTMLElement attributes
775 if ( !Lang.isUndefined(element[key]) ) {
782 var configs = this._configs || {};
783 var el = configs.element; // avoid loop due to 'element'
784 if (el && !configs[key] && !Lang.isUndefined(el.value[key]) ) {
785 return el.value[key];
788 return AttributeProvider.prototype.get.call(this, key);
791 set: function(key, value, silent) {
792 var el = this.get('element');
794 this._queue[this._queue.length] = ['set', arguments];
798 // set it on the element if not a property
799 if ( !this._configs[key] && !Lang.isUndefined(el[key]) ) {
800 _registerHTMLAttr.call(this, key);
803 return AttributeProvider.prototype.set.apply(this, arguments);
806 register: function(key) { // protect html attributes
807 var configs = this._configs || {};
808 var element = this.get('element') || null;
810 if ( element && !Lang.isUndefined(element[key]) ) {
811 YAHOO.log(key + ' is reserved for ' + element,
816 return AttributeProvider.prototype.register.apply(this, arguments);
819 configureAttribute: function(property, map, init) { // protect html attributes
820 var el = this.get('element');
822 this._queue[this._queue.length] = ['configureAttribute', arguments];
826 if (!this._configs[property] && !Lang.isUndefined(el[property]) ) {
827 _registerHTMLAttr.call(this, property, map);
830 return AttributeProvider.prototype.configureAttribute.apply(this, arguments);
833 getAttributeKeys: function() {
834 var el = this.get('element');
835 var keys = AttributeProvider.prototype.getAttributeKeys.call(this);
837 //add any unconfigured element keys
838 for (var key in el) {
839 if (!this._configs[key]) {
840 keys[key] = keys[key] || el[key];
847 init: function(el, attr) {
848 this._queue = this._queue || [];
849 this._events = this._events || {};
850 this._configs = this._configs || {};
852 attr.element = attr.element || el || null;
866 var readyHandler = function() {
867 this.initAttributes(attr);
869 this.setAttributes(attr, true);
871 this.fireEvent('contentReady', {
872 type: 'contentReady',
877 if ( Lang.isString(el) ) {
878 _registerHTMLAttr.call(this, 'id', { value: el });
879 YAHOO.util.Event.onAvailable(el, function() {
880 attr.element = Dom.get(el);
881 this.fireEvent('available', {
887 YAHOO.util.Event.onContentReady(el, function() {
888 readyHandler.call(this);
891 readyHandler.call(this);
897 * Sets the value of the property and fires beforeChange and change events.
899 * @method _registerHTMLAttr
900 * @param {YAHOO.util.Element} element The Element instance to
901 * register the config to.
902 * @param {String} key The name of the config to register
903 * @param {Object} map A key-value map of the config's params
905 var _registerHTMLAttr = function(key, map) {
906 var el = this.get('element');
909 map.method = map.method || function(value) {
912 map.value = map.value || el[key];
913 this._configs[key] = new YAHOO.util.Attribute(map, this);
917 * Fires when the Element's HTMLElement can be retrieved by Id.
918 * <p>See: <a href="#addListener">Element.addListener</a></p>
919 * <p><strong>Event fields:</strong><br>
920 * <code><String> type</code> available<br>
921 * <code><HTMLElement>
922 * target</code> the HTMLElement bound to this Element instance<br>
923 * <p><strong>Usage:</strong><br>
924 * <code>var handler = function(e) {var target = e.target};<br>
925 * myTabs.addListener('available', handler);</code></p>
930 * Fires when the Element's HTMLElement subtree is rendered.
931 * <p>See: <a href="#addListener">Element.addListener</a></p>
932 * <p><strong>Event fields:</strong><br>
933 * <code><String> type</code> contentReady<br>
934 * <code><HTMLElement>
935 * target</code> the HTMLElement bound to this Element instance<br>
936 * <p><strong>Usage:</strong><br>
937 * <code>var handler = function(e) {var target = e.target};<br>
938 * myTabs.addListener('contentReady', handler);</code></p>
939 * @event contentReady
943 YAHOO.augment(YAHOO.util.Element, AttributeProvider);
947 var Dom = YAHOO.util.Dom,
948 Event = YAHOO.util.Event,
949 Lang = YAHOO.util.Lang;
952 * A representation of a Tab's label and content.
953 * @namespace YAHOO.widget
955 * @extends YAHOO.util.Element
957 * @param element {HTMLElement | String} (optional) The html element that
958 * represents the TabView. An element will be created if none provided.
959 * @param {Object} properties A key map of initial properties
961 var Tab = function(el, attr) {
963 if (arguments.length == 1 && !Lang.isString(el) && !el.nodeName) {
968 if (!el && !attr.element) {
969 el = _createTabElement.call(this, attr);
973 success: function(o) {
974 this.set('content', o.responseText);
976 failure: function(o) {
977 YAHOO.log('loading failed: ' + o.statusText,
982 Tab.superclass.constructor.call(this, el, attr);
984 this.DOM_EVENTS = {}; // delegating to tabView
987 YAHOO.extend(Tab, YAHOO.util.Element);
988 var proto = Tab.prototype;
991 * The default tag name for a Tab's inner element.
992 * @property LABEL_INNER_TAGNAME
996 proto.LABEL_TAGNAME = 'em';
999 * The class name applied to active tabs.
1000 * @property ACTIVE_CLASSNAME
1004 proto.ACTIVE_CLASSNAME = 'selected';
1007 * The class name applied to disabled tabs.
1008 * @property DISABLED_CLASSNAME
1010 * @default "disabled"
1012 proto.DISABLED_CLASSNAME = 'disabled';
1015 * The class name applied to dynamic tabs while loading.
1016 * @property LOADING_CLASSNAME
1018 * @default "disabled"
1020 proto.LOADING_CLASSNAME = 'loading';
1023 * Provides a reference to the connection request object when data is
1024 * loaded dynamically.
1025 * @property dataConnection
1028 proto.dataConnection = null;
1031 * Object containing success and failure callbacks for loading data.
1032 * @property loadHandler
1035 proto.loadHandler = null;
1038 * Provides a readable name for the tab.
1042 proto.toString = function() {
1043 var el = this.get('element');
1044 var id = el.id || el.tagName;
1049 * Registers TabView specific properties.
1050 * @method initAttributes
1051 * @param {Object} attr Hash of initial attributes
1053 proto.initAttributes = function(attr) {
1055 Tab.superclass.initAttributes.call(this, attr);
1057 var el = this.get('element');
1060 * The event that triggers the tab's activation.
1061 * @config activationEvent
1064 this.register('activationEvent', {
1065 value: attr.activationEvent || 'click'
1069 * The element that contains the tab's label.
1073 this.register('labelEl', {
1074 value: attr.labelEl || _getlabelEl.call(this),
1075 method: function(value) {
1076 var current = this.get('labelEl');
1079 if (current == value) {
1080 return false; // already set
1083 this.replaceChild(value, current);
1084 } else if (el.firstChild) { // ensure label is firstChild by default
1085 this.insertBefore(value, el.firstChild);
1087 this.appendChild(value);
1093 * The tab's label text (or innerHTML).
1097 this.register('label', {
1098 value: attr.label || _getLabel.call(this),
1099 method: function(value) {
1100 var labelEl = this.get('labelEl');
1101 if (!labelEl) { // create if needed
1102 this.set('labelEl', _createlabelEl.call(this));
1105 _setLabel.call(this, value);
1110 * The HTMLElement that contains the tab's content.
1114 this.register('contentEl', { // TODO: apply className?
1115 value: attr.contentEl || document.createElement('div'),
1116 method: function(value) {
1117 var current = this.get('contentEl');
1120 if (current == value) {
1121 return false; // already set
1123 this.replaceChild(value, current);
1129 * The tab's content.
1133 this.register('content', {
1134 value: attr.content, // TODO: what about existing?
1135 method: function(value) {
1136 this.get('contentEl').innerHTML = value;
1140 var _dataLoaded = false;
1143 * The tab's data source, used for loading content dynamically.
1147 this.register('dataSrc', {
1152 * Whether or not content should be reloaded for every view.
1157 this.register('cacheData', {
1158 value: attr.cacheData || false,
1159 validator: Lang.isBoolean
1163 * The method to use for the data request.
1164 * @config loadMethod
1168 this.register('loadMethod', {
1169 value: attr.loadMethod || 'GET',
1170 validator: Lang.isString
1174 * Whether or not any data has been loaded from the server.
1175 * @config dataLoaded
1178 this.register('dataLoaded', {
1180 validator: Lang.isBoolean,
1185 * Number if milliseconds before aborting and calling failure handler.
1186 * @config dataTimeout
1190 this.register('dataTimeout', {
1191 value: attr.dataTimeout || null,
1192 validator: Lang.isNumber
1196 * Whether or not the tab is currently active.
1197 * If a dataSrc is set for the tab, the content will be loaded from
1202 this.register('active', {
1203 value: attr.active || this.hasClass(this.ACTIVE_CLASSNAME),
1204 method: function(value) {
1205 if (value === true) {
1206 this.addClass(this.ACTIVE_CLASSNAME);
1207 this.set('title', 'active');
1209 this.removeClass(this.ACTIVE_CLASSNAME);
1210 this.set('title', '');
1213 validator: function(value) {
1214 return Lang.isBoolean(value) && !this.get('disabled') ;
1219 * Whether or not the tab is disabled.
1223 this.register('disabled', {
1224 value: attr.disabled || this.hasClass(this.DISABLED_CLASSNAME),
1225 method: function(value) {
1226 if (value === true) {
1227 Dom.addClass(this.get('element'), this.DISABLED_CLASSNAME);
1229 Dom.removeClass(this.get('element'), this.DISABLED_CLASSNAME);
1232 validator: Lang.isBoolean
1236 * The href of the tab's anchor element.
1241 this.register('href', {
1242 value: attr.href || '#',
1243 method: function(value) {
1244 this.getElementsByTagName('a')[0].href = value;
1246 validator: Lang.isString
1250 * The Whether or not the tab's content is visible.
1251 * @config contentVisible
1255 this.register('contentVisible', {
1256 value: attr.contentVisible,
1257 method: function(value) {
1258 if (value == true) {
1259 this.get('contentEl').style.display = 'block';
1261 if ( this.get('dataSrc') ) {
1262 // load dynamic content unless already loaded and caching
1263 if ( !this.get('dataLoaded') || !this.get('cacheData') ) {
1264 _dataConnect.call(this);
1268 this.get('contentEl').style.display = 'none';
1271 validator: Lang.isBoolean
1275 var _createTabElement = function(attr) {
1276 var el = document.createElement('li');
1277 var a = document.createElement('a');
1279 a.href = attr.href || '#';
1283 var label = attr.label || null;
1284 var labelEl = attr.labelEl || null;
1286 if (labelEl) { // user supplied labelEl
1287 if (!label) { // user supplied label
1288 label = _getLabel.call(this, labelEl);
1291 labelEl = _createlabelEl.call(this);
1294 a.appendChild(labelEl);
1299 var _getlabelEl = function() {
1300 return this.getElementsByTagName(this.LABEL_TAGNAME)[0];
1303 var _createlabelEl = function() {
1304 var el = document.createElement(this.LABEL_TAGNAME);
1308 var _setLabel = function(label) {
1309 var el = this.get('labelEl');
1310 el.innerHTML = label;
1313 var _getLabel = function() {
1315 el = this.get('labelEl');
1321 return el.innerHTML;
1324 var _dataConnect = function() {
1325 if (!YAHOO.util.Connect) {
1326 YAHOO.log('YAHOO.util.Connect dependency not met',
1331 Dom.addClass(this.get('contentEl').parentNode, this.LOADING_CLASSNAME);
1333 this.dataConnection = YAHOO.util.Connect.asyncRequest(
1334 this.get('loadMethod'),
1335 this.get('dataSrc'),
1337 success: function(o) {
1338 this.loadHandler.success.call(this, o);
1339 this.set('dataLoaded', true);
1340 this.dataConnection = null;
1341 Dom.removeClass(this.get('contentEl').parentNode,
1342 this.LOADING_CLASSNAME);
1344 failure: function(o) {
1345 this.loadHandler.failure.call(this, o);
1346 this.dataConnection = null;
1347 Dom.removeClass(this.get('contentEl').parentNode,
1348 this.LOADING_CLASSNAME);
1351 timeout: this.get('dataTimeout')
1356 YAHOO.widget.Tab = Tab;
1359 * Fires before the active state is changed.
1360 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1361 * <p>If handler returns false, the change will be cancelled, and the value will not
1363 * <p><strong>Event fields:</strong><br>
1364 * <code><String> type</code> beforeActiveChange<br>
1365 * <code><Boolean>
1366 * prevValue</code> the current value<br>
1367 * <code><Boolean>
1368 * newValue</code> the new value</p>
1369 * <p><strong>Usage:</strong><br>
1370 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1371 * myTabs.addListener('beforeActiveChange', handler);</code></p>
1372 * @event beforeActiveChange
1376 * Fires after the active state is changed.
1377 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1378 * <p><strong>Event fields:</strong><br>
1379 * <code><String> type</code> activeChange<br>
1380 * <code><Boolean>
1381 * prevValue</code> the previous value<br>
1382 * <code><Boolean>
1383 * newValue</code> the updated value</p>
1384 * <p><strong>Usage:</strong><br>
1385 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1386 * myTabs.addListener('activeChange', handler);</code></p>
1387 * @event activeChange
1391 * Fires before the tab label is changed.
1392 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1393 * <p>If handler returns false, the change will be cancelled, and the value will not
1395 * <p><strong>Event fields:</strong><br>
1396 * <code><String> type</code> beforeLabelChange<br>
1397 * <code><String>
1398 * prevValue</code> the current value<br>
1399 * <code><String>
1400 * newValue</code> the new value</p>
1401 * <p><strong>Usage:</strong><br>
1402 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1403 * myTabs.addListener('beforeLabelChange', handler);</code></p>
1404 * @event beforeLabelChange
1408 * Fires after the tab label is changed.
1409 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1410 * <p><strong>Event fields:</strong><br>
1411 * <code><String> type</code> labelChange<br>
1412 * <code><String>
1413 * prevValue</code> the previous value<br>
1414 * <code><String>
1415 * newValue</code> the updated value</p>
1416 * <p><strong>Usage:</strong><br>
1417 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1418 * myTabs.addListener('labelChange', handler);</code></p>
1419 * @event labelChange
1423 * Fires before the tab content is changed.
1424 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1425 * <p>If handler returns false, the change will be cancelled, and the value will not
1427 * <p><strong>Event fields:</strong><br>
1428 * <code><String> type</code> beforeContentChange<br>
1429 * <code><String>
1430 * prevValue</code> the current value<br>
1431 * <code><String>
1432 * newValue</code> the new value</p>
1433 * <p><strong>Usage:</strong><br>
1434 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1435 * myTabs.addListener('beforeContentChange', handler);</code></p>
1436 * @event beforeContentChange
1440 * Fires after the tab content is changed.
1441 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1442 * <p><strong>Event fields:</strong><br>
1443 * <code><String> type</code> contentChange<br>
1444 * <code><String>
1445 * prevValue</code> the previous value<br>
1446 * <code><Boolean>
1447 * newValue</code> the updated value</p>
1448 * <p><strong>Usage:</strong><br>
1449 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1450 * myTabs.addListener('contentChange', handler);</code></p>
1451 * @event contentChange
1458 * The tabview module provides a widget for managing content bound to tabs.
1460 * @requires yahoo, dom, event
1464 * A widget to control tabbed views.
1465 * @namespace YAHOO.widget
1467 * @extends YAHOO.util.Element
1469 * @param {HTMLElement | String | Object} el(optional) The html
1470 * element that represents the TabView, or the attribute object to use.
1471 * An element will be created if none provided.
1472 * @param {Object} attr (optional) A key map of the tabView's
1473 * initial attributes. Ignored if first arg is attributes object.
1475 YAHOO.widget.TabView = function(el, attr) {
1477 if (arguments.length == 1 && !Lang.isString(el) && !el.nodeName) {
1478 attr = el; // treat first arg as attr object
1479 el = attr.element || null;
1482 if (!el && !attr.element) { // create if we dont have one
1483 el = _createTabViewElement.call(this, attr);
1485 YAHOO.widget.TabView.superclass.constructor.call(this, el, attr);
1488 YAHOO.extend(YAHOO.widget.TabView, YAHOO.util.Element);
1490 var proto = YAHOO.widget.TabView.prototype;
1491 var Dom = YAHOO.util.Dom;
1492 var Lang = YAHOO.util.Lang;
1493 var Event = YAHOO.util.Event;
1494 var Tab = YAHOO.widget.Tab;
1498 * The className to add when building from scratch.
1499 * @property CLASSNAME
1502 proto.CLASSNAME = 'yui-navset';
1505 * The className of the HTMLElement containing the TabView's tab elements
1506 * to look for when building from existing markup, or to add when building
1508 * All childNodes of the tab container are treated as Tabs when building
1509 * from existing markup.
1510 * @property TAB_PARENT_CLASSNAME
1513 proto.TAB_PARENT_CLASSNAME = 'yui-nav';
1516 * The className of the HTMLElement containing the TabView's label elements
1517 * to look for when building from existing markup, or to add when building
1519 * All childNodes of the content container are treated as content elements when
1520 * building from existing markup.
1521 * @property CONTENT_PARENT_CLASSNAME
1522 * @default "nav-content"
1524 proto.CONTENT_PARENT_CLASSNAME = 'yui-content';
1526 proto._tabParent = null;
1527 proto._contentParent = null;
1530 * Adds a Tab to the TabView instance.
1531 * If no index is specified, the tab is added to the end of the tab list.
1533 * @param {YAHOO.widget.Tab} tab A Tab instance to add.
1534 * @param {Integer} index The position to add the tab.
1537 proto.addTab = function(tab, index) {
1538 var tabs = this.get('tabs');
1539 if (!tabs) { // not ready yet
1540 this._queue[this._queue.length] = ['addTab', arguments];
1544 index = (index === undefined) ? tabs.length : index;
1546 var before = this.getTab(index);
1549 var el = this.get('element');
1550 var tabParent = this._tabParent;
1551 var contentParent = this._contentParent;
1553 var tabElement = tab.get('element');
1554 var contentEl = tab.get('contentEl');
1557 tabParent.insertBefore(tabElement, before.get('element'));
1559 tabParent.appendChild(tabElement);
1562 if ( contentEl && !Dom.isAncestor(contentParent, contentEl) ) {
1563 contentParent.appendChild(contentEl);
1566 if ( !tab.get('active') ) {
1567 tab.set('contentVisible', false, true); /* hide if not active */
1569 this.set('activeTab', tab, true);
1573 var activate = function(e) {
1574 YAHOO.util.Event.preventDefault(e);
1575 self.set('activeTab', this);
1578 tab.addListener( tab.get('activationEvent'), activate);
1580 tab.addListener('activationEventChange', function(e) {
1581 if (e.prevValue != e.newValue) {
1582 tab.removeListener(e.prevValue, activate);
1583 tab.addListener(e.newValue, activate);
1587 tabs.splice(index, 0, tab);
1591 * Routes childNode events.
1592 * @method DOMEventHandler
1593 * @param {event} e The Dom event that is being handled.
1596 proto.DOMEventHandler = function(e) {
1597 var el = this.get('element');
1598 var target = YAHOO.util.Event.getTarget(e);
1599 var tabParent = this._tabParent;
1601 if (Dom.isAncestor(tabParent, target) ) {
1605 var tabs = this.get('tabs');
1607 for (var i = 0, len = tabs.length; i < len; i++) {
1608 tabEl = tabs[i].get('element');
1609 contentEl = tabs[i].get('contentEl');
1611 if ( target == tabEl || Dom.isAncestor(tabEl, target) ) {
1613 break; // note break
1618 tab.fireEvent(e.type, e);
1624 * Returns the Tab instance at the specified index.
1626 * @param {Integer} index The position of the Tab.
1627 * @return YAHOO.widget.Tab
1629 proto.getTab = function(index) {
1630 return this.get('tabs')[index];
1634 * Returns the index of given tab.
1635 * @method getTabIndex
1636 * @param {YAHOO.widget.Tab} tab The tab whose index will be returned.
1639 proto.getTabIndex = function(tab) {
1641 var tabs = this.get('tabs');
1642 for (var i = 0, len = tabs.length; i < len; ++i) {
1643 if (tab == tabs[i]) {
1653 * Removes the specified Tab from the TabView.
1655 * @param {YAHOO.widget.Tab} item The Tab instance to be removed.
1658 proto.removeTab = function(tab) {
1659 var tabCount = this.get('tabs').length;
1661 var index = this.getTabIndex(tab);
1662 var nextIndex = index + 1;
1663 if ( tab == this.get('activeTab') ) { // select next tab
1665 if (index + 1 == tabCount) {
1666 this.set('activeIndex', index - 1);
1668 this.set('activeIndex', index + 1);
1673 this._tabParent.removeChild( tab.get('element') );
1674 this._contentParent.removeChild( tab.get('contentEl') );
1675 this._configs.tabs.value.splice(index, 1);
1680 * Provides a readable name for the TabView instance.
1684 proto.toString = function() {
1685 var name = this.get('id') || this.get('tagName');
1686 return "TabView " + name;
1690 * The transiton to use when switching between tabs.
1691 * @method contentTransition
1693 proto.contentTransition = function(newTab, oldTab) {
1694 newTab.set('contentVisible', true);
1695 oldTab.set('contentVisible', false);
1699 * Registers TabView specific properties.
1700 * @method initAttributes
1701 * @param {Object} attr Hash of initial attributes
1703 proto.initAttributes = function(attr) {
1704 YAHOO.widget.TabView.superclass.initAttributes.call(this, attr);
1706 if (!attr.orientation) {
1707 attr.orientation = 'top';
1710 var el = this.get('element');
1713 * The Tabs belonging to the TabView instance.
1717 this.register('tabs', {
1723 * The container of the tabView's label elements.
1724 * @property _tabParent
1729 this.getElementsByClassName(this.TAB_PARENT_CLASSNAME,
1730 'ul' )[0] || _createTabParent.call(this);
1733 * The container of the tabView's content elements.
1734 * @property _contentParent
1738 this._contentParent =
1739 this.getElementsByClassName(this.CONTENT_PARENT_CLASSNAME,
1740 'div')[0] || _createContentParent.call(this);
1743 * How the Tabs should be oriented relative to the TabView.
1744 * @config orientation
1748 this.register('orientation', {
1749 value: attr.orientation,
1750 method: function(value) {
1751 var current = this.get('orientation');
1752 this.addClass('yui-navset-' + value);
1754 if (current != value) {
1755 this.removeClass('yui-navset-' + current);
1760 this.appendChild(this._tabParent);
1767 * The index of the tab currently active.
1768 * @config activeIndex
1771 this.register('activeIndex', {
1772 value: attr.activeIndex,
1773 method: function(value) {
1774 this.set('activeTab', this.getTab(value));
1776 validator: function(value) {
1777 return !this.getTab(value).get('disabled'); // cannot activate if disabled
1782 * The tab currently active.
1784 * @type YAHOO.widget.Tab
1786 this.register('activeTab', {
1787 value: attr.activeTab,
1788 method: function(tab) {
1789 var activeTab = this.get('activeTab');
1792 tab.set('active', true);
1795 if (activeTab && activeTab != tab) {
1796 activeTab.set('active', false);
1799 if (activeTab && tab != activeTab) { // no transition if only 1
1800 this.contentTransition(tab, activeTab);
1802 tab.set('contentVisible', true);
1805 validator: function(value) {
1806 return !value.get('disabled'); // cannot activate if disabled
1810 if ( this._tabParent ) {
1811 _initTabs.call(this);
1814 for (var type in this.DOM_EVENTS) {
1815 if ( this.DOM_EVENTS.hasOwnProperty(type) ) {
1816 this.addListener.call(this, type, this.DOMEventHandler);
1822 * Creates Tab instances from a collection of HTMLElements.
1823 * @method createTabs
1825 * @param {Array|HTMLCollection} elements The elements to use for Tabs.
1828 var _initTabs = function() {
1833 var el = this.get('element');
1834 var tabs = _getChildNodes(this._tabParent);
1835 var contentElements = _getChildNodes(this._contentParent);
1837 for (var i = 0, len = tabs.length; i < len; ++i) {
1840 if (contentElements[i]) {
1841 attr.contentEl = contentElements[i];
1844 tab = new YAHOO.widget.Tab(tabs[i], attr);
1847 if (tab.hasClass(tab.ACTIVE_CLASSNAME) ) {
1848 this._configs.activeTab.value = tab; // dont invoke method
1853 var _createTabViewElement = function(attr) {
1854 var el = document.createElement('div');
1856 if ( this.CLASSNAME ) {
1857 el.className = this.CLASSNAME;
1863 var _createTabParent = function(attr) {
1864 var el = document.createElement('ul');
1866 if ( this.TAB_PARENT_CLASSNAME ) {
1867 el.className = this.TAB_PARENT_CLASSNAME;
1870 this.get('element').appendChild(el);
1875 var _createContentParent = function(attr) {
1876 var el = document.createElement('div');
1878 if ( this.CONTENT_PARENT_CLASSNAME ) {
1879 el.className = this.CONTENT_PARENT_CLASSNAME;
1882 this.get('element').appendChild(el);
1887 var _getChildNodes = function(el) {
1889 var childNodes = el.childNodes;
1891 for (var i = 0, len = childNodes.length; i < len; ++i) {
1892 if (childNodes[i].nodeType == 1) {
1893 nodes[nodes.length] = childNodes[i];
1901 * Fires before the activeTab is changed.
1902 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1903 * <p>If handler returns false, the change will be cancelled, and the value will not
1905 * <p><strong>Event fields:</strong><br>
1906 * <code><String> type</code> beforeActiveTabChange<br>
1907 * <code><<a href="YAHOO.widget.Tab.html">YAHOO.widget.Tab</a>>
1908 * prevValue</code> the currently active tab<br>
1909 * <code><<a href="YAHOO.widget.Tab.html">YAHOO.widget.Tab</a>>
1910 * newValue</code> the tab to be made active</p>
1911 * <p><strong>Usage:</strong><br>
1912 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1913 * myTabs.addListener('beforeActiveTabChange', handler);</code></p>
1914 * @event beforeActiveTabChange
1918 * Fires after the activeTab is changed.
1919 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1920 * <p><strong>Event fields:</strong><br>
1921 * <code><String> type</code> activeTabChange<br>
1922 * <code><<a href="YAHOO.widget.Tab.html">YAHOO.widget.Tab</a>>
1923 * prevValue</code> the formerly active tab<br>
1924 * <code><<a href="YAHOO.widget.Tab.html">YAHOO.widget.Tab</a>>
1925 * newValue</code> the new active tab</p>
1926 * <p><strong>Usage:</strong><br>
1927 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1928 * myTabs.addListener('activeTabChange', handler);</code></p>
1929 * @event activeTabChange
1933 * Fires before the orientation is changed.
1934 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1935 * <p>If handler returns false, the change will be cancelled, and the value will not
1937 * <p><strong>Event fields:</strong><br>
1938 * <code><String> type</code> beforeOrientationChange<br>
1939 * <code><String>
1940 * prevValue</code> the current orientation<br>
1941 * <code><String>
1942 * newValue</code> the new orientation to be applied</p>
1943 * <p><strong>Usage:</strong><br>
1944 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1945 * myTabs.addListener('beforeOrientationChange', handler);</code></p>
1946 * @event beforeOrientationChange
1950 * Fires after the orientation is changed.
1951 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1952 * <p><strong>Event fields:</strong><br>
1953 * <code><String> type</code> orientationChange<br>
1954 * <code><String>
1955 * prevValue</code> the former orientation<br>
1956 * <code><String>
1957 * newValue</code> the new orientation</p>
1958 * <p><strong>Usage:</strong><br>
1959 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1960 * myTabs.addListener('orientationChange', handler);</code></p>
1961 * @event orientationChange