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) {
172 this.method.call(owner, value);
176 this._written = true;
181 this.owner.fireChangeEvent(event);
188 * Allows for configuring the Attribute's properties.
190 * @param {Object} map A key-value map of Attribute properties.
191 * @param {Boolean} init Whether or not this should become the initial config.
193 configure: function(map, init) {
195 this._written = false; // reset writeOnce
196 this._initialConfig = this._initialConfig || {};
198 for (var key in map) {
199 if ( key && map.hasOwnProperty(key) ) {
200 this[key] = map[key];
202 this._initialConfig[key] = map[key];
209 * Resets the value to the initial config value.
211 * @return {Boolean} Whether or not the value was set.
213 resetValue: function() {
214 return this.setValue(this._initialConfig.value);
218 * Resets the attribute config to the initial config state.
219 * @method resetConfig
221 resetConfig: function() {
222 this.configure(this._initialConfig);
226 * Resets the value to the current value.
227 * Useful when values may have gotten out of sync with actual properties.
229 * @return {Boolean} Whether or not the value was set.
231 refresh: function(silent) {
232 this.setValue(this.value, silent);
237 var Lang = YAHOO.util.Lang;
240 Copyright (c) 2006, Yahoo! Inc. All rights reserved.
241 Code licensed under the BSD License:
242 http://developer.yahoo.net/yui/license.txt
246 * Provides and manages YAHOO.util.Attribute instances
247 * @namespace YAHOO.util
248 * @class AttributeProvider
249 * @uses YAHOO.util.EventProvider
251 YAHOO.util.AttributeProvider = function() {};
253 YAHOO.util.AttributeProvider.prototype = {
256 * A key-value map of Attribute configurations
258 * @protected (may be used by subclasses and augmentors)
264 * Returns the current value of the attribute.
266 * @param {String} key The attribute whose value will be returned.
269 var configs = this._configs || {};
270 var config = configs[key];
280 * Sets the value of a config.
282 * @param {String} key The name of the attribute
283 * @param {Any} value The value to apply to the attribute
284 * @param {Boolean} silent Whether or not to suppress change events
285 * @return {Boolean} Whether or not the value was set.
287 set: function(key, value, silent){
288 var configs = this._configs || {};
289 var config = configs[key];
295 return config.setValue(value, silent);
299 * Returns an array of attribute names.
300 * @method getAttributeKeys
301 * @return {Array} An array of attribute names.
303 getAttributeKeys: function(){
304 var configs = this._configs;
307 for (var key in configs) {
308 config = configs[key];
309 if ( configs.hasOwnProperty(key) &&
310 !Lang.isUndefined(config) ) {
311 keys[keys.length] = key;
319 * Sets multiple attribute values.
320 * @method setAttributes
321 * @param {Object} map A key-value map of attributes
322 * @param {Boolean} silent Whether or not to suppress change events
324 setAttributes: function(map, silent){
325 for (var key in map) {
326 if ( map.hasOwnProperty(key) ) {
327 this.set(key, map[key], silent);
333 * Resets the specified attribute's value to its initial value.
335 * @param {String} key The name of the attribute
336 * @param {Boolean} silent Whether or not to suppress change events
337 * @return {Boolean} Whether or not the value was set
339 resetValue: function(key, silent){
340 var configs = this._configs || {};
342 this.set(key, configs[key]._initialConfig.value, silent);
349 * Sets the attribute's value to its current value.
351 * @param {String | Array} key The attribute(s) to refresh
352 * @param {Boolean} silent Whether or not to suppress change events
354 refresh: function(key, silent){
355 var configs = this._configs;
357 key = ( ( Lang.isString(key) ) ? [key] : key ) ||
358 this.getAttributeKeys();
360 for (var i = 0, len = key.length; i < len; ++i) {
361 if ( // only set if there is a value and not null
363 ! Lang.isUndefined(configs[key[i]].value) &&
364 ! Lang.isNull(configs[key[i]].value) ) {
365 configs[key[i]].refresh(silent);
371 * Adds an Attribute to the AttributeProvider instance.
373 * @param {String} key The attribute's name
374 * @param {Object} map A key-value map containing the
375 * attribute's properties.
377 register: function(key, map) {
378 this._configs = this._configs || {};
380 if (this._configs[key]) { // dont override
385 this._configs[key] = new YAHOO.util.Attribute(map, this);
390 * Returns the attribute's properties.
391 * @method getAttributeConfig
392 * @param {String} key The attribute's name
394 * @return {object} A key-value map containing all of the
395 * attribute's properties.
397 getAttributeConfig: function(key) {
398 var configs = this._configs || {};
399 var config = configs[key] || {};
400 var map = {}; // returning a copy to prevent overrides
402 for (key in config) {
403 if ( config.hasOwnProperty(key) ) {
404 map[key] = config[key];
412 * Sets or updates an Attribute instance's properties.
413 * @method configureAttribute
414 * @param {String} key The attribute's name.
415 * @param {Object} map A key-value map of attribute properties
416 * @param {Boolean} init Whether or not this should become the intial config.
418 configureAttribute: function(key, map, init) {
419 var configs = this._configs || {};
425 configs[key].configure(map, init);
429 * Resets an attribute to its intial configuration.
430 * @method resetAttributeConfig
431 * @param {String} key The attribute's name.
434 resetAttributeConfig: function(key){
435 var configs = this._configs || {};
436 configs[key].resetConfig();
440 * Fires the attribute's beforeChange event.
441 * @method fireBeforeChangeEvent
442 * @param {String} key The attribute's name.
443 * @param {Obj} e The event object to pass to handlers.
445 fireBeforeChangeEvent: function(e) {
447 type += e.type.charAt(0).toUpperCase() + e.type.substr(1) + 'Change';
449 return this.fireEvent(e.type, e);
453 * Fires the attribute's change event.
454 * @method fireChangeEvent
455 * @param {String} key The attribute's name.
456 * @param {Obj} e The event object to pass to the handlers.
458 fireChangeEvent: function(e) {
460 return this.fireEvent(e.type, e);
464 YAHOO.augment(YAHOO.util.AttributeProvider, YAHOO.util.EventProvider);
468 // internal shorthand
469 var Dom = YAHOO.util.Dom,
470 Lang = YAHOO.util.Lang,
471 EventPublisher = YAHOO.util.EventPublisher,
472 AttributeProvider = YAHOO.util.AttributeProvider;
475 * Element provides an interface to an HTMLElement's attributes and common
476 * methods. Other commonly used attributes are added as well.
477 * @namespace YAHOO.util
479 * @uses YAHOO.util.AttributeProvider
481 * @param el {HTMLElement | String} The html element that
482 * represents the Element.
483 * @param {Object} map A key-value map of initial config names and values
485 YAHOO.util.Element = function(el, map) {
486 if (arguments.length) {
491 YAHOO.util.Element.prototype = {
493 * Dom events supported by the Element instance.
494 * @property DOM_EVENTS
500 * Wrapper for HTMLElement method.
501 * @method appendChild
502 * @param {Boolean} deep Whether or not to do a deep clone
504 appendChild: function(child) {
505 child = child.get ? child.get('element') : child;
506 this.get('element').appendChild(child);
510 * Wrapper for HTMLElement method.
511 * @method getElementsByTagName
512 * @param {String} tag The tagName to collect
514 getElementsByTagName: function(tag) {
515 return this.get('element').getElementsByTagName(tag);
519 * Wrapper for HTMLElement method.
520 * @method hasChildNodes
521 * @return {Boolean} Whether or not the element has childNodes
523 hasChildNodes: function() {
524 return this.get('element').hasChildNodes();
528 * Wrapper for HTMLElement method.
529 * @method insertBefore
530 * @param {HTMLElement} element The HTMLElement to insert
531 * @param {HTMLElement} before The HTMLElement to insert
532 * the element before.
534 insertBefore: function(element, before) {
535 element = element.get ? element.get('element') : element;
536 before = (before && before.get) ? before.get('element') : before;
538 this.get('element').insertBefore(element, before);
542 * Wrapper for HTMLElement method.
543 * @method removeChild
544 * @param {HTMLElement} child The HTMLElement to remove
546 removeChild: function(child) {
547 child = child.get ? child.get('element') : child;
548 this.get('element').removeChild(child);
553 * Wrapper for HTMLElement method.
554 * @method replaceChild
555 * @param {HTMLElement} newNode The HTMLElement to insert
556 * @param {HTMLElement} oldNode The HTMLElement to replace
558 replaceChild: function(newNode, oldNode) {
559 newNode = newNode.get ? newNode.get('element') : newNode;
560 oldNode = oldNode.get ? oldNode.get('element') : oldNode;
561 return this.get('element').replaceChild(newNode, oldNode);
566 * Registers Element specific attributes.
567 * @method initAttributes
568 * @param {Object} map A key-value map of initial attribute configs
570 initAttributes: function(map) {
572 var element = Dom.get(map.element) || null;
575 * The HTMLElement the Element instance refers to.
579 this.register('element', {
586 * Adds a listener for the given event. These may be DOM or
587 * customEvent listeners. Any event that is fired via fireEvent
588 * can be listened for. All handlers receive an event object.
589 * @method addListener
590 * @param {String} type The name of the event to listen for
591 * @param {Function} fn The handler to call when the event fires
592 * @param {Any} obj A variable to pass to the handler
593 * @param {Object} scope The object to use for the scope of the handler
595 addListener: function(type, fn, obj, scope) {
596 var el = this.get('element');
597 var scope = scope || this;
599 el = this.get('id') || el;
601 if (!this._events[type]) { // create on the fly
602 if ( this.DOM_EVENTS[type] ) {
603 YAHOO.util.Event.addListener(el, type, function(e) {
604 if (e.srcElement && !e.target) { // supplement IE with target
605 e.target = e.srcElement;
607 this.fireEvent(type, e);
611 this.createEvent(type, this);
612 this._events[type] = true;
615 this.subscribe.apply(this, arguments); // notify via customEvent
620 * Alias for addListener
622 * @param {String} type The name of the event to listen for
623 * @param {Function} fn The function call when the event fires
624 * @param {Any} obj A variable to pass to the handler
625 * @param {Object} scope The object to use for the scope of the handler
627 on: function() { this.addListener.apply(this, arguments); },
631 * Remove an event listener
632 * @method removeListener
633 * @param {String} type The name of the event to listen for
634 * @param {Function} fn The function call when the event fires
636 removeListener: function(type, fn) {
637 this.unsubscribe.apply(this, arguments);
641 * Wrapper for Dom method.
643 * @param {String} className The className to add
645 addClass: function(className) {
646 Dom.addClass(this.get('element'), className);
650 * Wrapper for Dom method.
651 * @method getElementsByClassName
652 * @param {String} className The className to collect
653 * @param {String} tag (optional) The tag to use in
654 * conjunction with class name
655 * @return {Array} Array of HTMLElements
657 getElementsByClassName: function(className, tag) {
658 return Dom.getElementsByClassName(className, tag,
659 this.get('element') );
663 * Wrapper for Dom method.
665 * @param {String} className The className to add
666 * @return {Boolean} Whether or not the element has the class name
668 hasClass: function(className) {
669 return Dom.hasClass(this.get('element'), className);
673 * Wrapper for Dom method.
674 * @method removeClass
675 * @param {String} className The className to remove
677 removeClass: function(className) {
678 return Dom.removeClass(this.get('element'), className);
682 * Wrapper for Dom method.
683 * @method replaceClass
684 * @param {String} oldClassName The className to replace
685 * @param {String} newClassName The className to add
687 replaceClass: function(oldClassName, newClassName) {
688 return Dom.replaceClass(this.get('element'),
689 oldClassName, newClassName);
693 * Wrapper for Dom method.
695 * @param {String} property The style property to set
696 * @param {String} value The value to apply to the style property
698 setStyle: function(property, value) {
699 return Dom.setStyle(this.get('element'), property, value);
703 * Wrapper for Dom method.
705 * @param {String} property The style property to retrieve
706 * @return {String} The current value of the property
708 getStyle: function(property) {
709 return Dom.getStyle(this.get('element'), property);
713 * Apply any queued set calls.
716 fireQueue: function() {
717 var queue = this._queue;
718 for (var i = 0, len = queue.length; i < len; ++i) {
719 this[queue[i][0]].apply(this, queue[i][1]);
724 * Appends the HTMLElement into either the supplied parentNode.
726 * @param {HTMLElement | Element} parentNode The node to append to
727 * @param {HTMLElement | Element} before An optional node to insert before
729 appendTo: function(parent, before) {
730 parent = (parent.get) ? parent.get('element') : Dom.get(parent);
732 before = (before && before.get) ?
733 before.get('element') : Dom.get(before);
734 var element = this.get('element');
736 var newAddition = !Dom.inDocument(element);
746 if (element.parent != parent) {
748 parent.insertBefore(element, before);
750 parent.appendChild(element);
756 return false; // note return; no refresh if in document
759 // if a new addition, refresh HTMLElement any applied attributes
760 var keys = this.getAttributeKeys();
762 for (var key in keys) { // only refresh HTMLElement attributes
763 if ( !Lang.isUndefined(element[key]) ) {
770 var configs = this._configs || {};
771 var el = configs.element; // avoid loop due to 'element'
772 if (el && !configs[key] && !Lang.isUndefined(el.value[key]) ) {
773 return el.value[key];
776 return AttributeProvider.prototype.get.call(this, key);
779 set: function(key, value, silent) {
780 var el = this.get('element');
782 this._queue[this._queue.length] = ['set', arguments];
786 // set it on the element if not a property
787 if ( !this._configs[key] && !Lang.isUndefined(el[key]) ) {
788 _registerHTMLAttr.call(this, key);
791 return AttributeProvider.prototype.set.apply(this, arguments);
794 register: function(key) { // protect html attributes
795 var configs = this._configs || {};
796 var element = this.get('element') || null;
798 if ( element && !Lang.isUndefined(element[key]) ) {
802 return AttributeProvider.prototype.register.apply(this, arguments);
805 configureAttribute: function(property, map, init) { // protect html attributes
806 var el = this.get('element');
808 this._queue[this._queue.length] = ['configureAttribute', arguments];
812 if (!this._configs[property] && !Lang.isUndefined(el[property]) ) {
813 _registerHTMLAttr.call(this, property, map);
816 return AttributeProvider.prototype.configureAttribute.apply(this, arguments);
819 getAttributeKeys: function() {
820 var el = this.get('element');
821 var keys = AttributeProvider.prototype.getAttributeKeys.call(this);
823 //add any unconfigured element keys
824 for (var key in el) {
825 if (!this._configs[key]) {
826 keys[key] = keys[key] || el[key];
833 init: function(el, attr) {
834 this._queue = this._queue || [];
835 this._events = this._events || {};
836 this._configs = this._configs || {};
838 attr.element = attr.element || el || null;
852 var readyHandler = function() {
853 this.initAttributes(attr);
855 this.setAttributes(attr, true);
857 this.fireEvent('contentReady', {
858 type: 'contentReady',
863 if ( Lang.isString(el) ) {
864 _registerHTMLAttr.call(this, 'id', { value: el });
865 YAHOO.util.Event.onAvailable(el, function() {
866 attr.element = Dom.get(el);
867 this.fireEvent('available', {
873 YAHOO.util.Event.onContentReady(el, function() {
874 readyHandler.call(this);
877 readyHandler.call(this);
883 * Sets the value of the property and fires beforeChange and change events.
885 * @method _registerHTMLAttr
886 * @param {YAHOO.util.Element} element The Element instance to
887 * register the config to.
888 * @param {String} key The name of the config to register
889 * @param {Object} map A key-value map of the config's params
891 var _registerHTMLAttr = function(key, map) {
892 var el = this.get('element');
895 map.method = map.method || function(value) {
898 map.value = map.value || el[key];
899 this._configs[key] = new YAHOO.util.Attribute(map, this);
903 * Fires when the Element's HTMLElement can be retrieved by Id.
904 * <p>See: <a href="#addListener">Element.addListener</a></p>
905 * <p><strong>Event fields:</strong><br>
906 * <code><String> type</code> available<br>
907 * <code><HTMLElement>
908 * target</code> the HTMLElement bound to this Element instance<br>
909 * <p><strong>Usage:</strong><br>
910 * <code>var handler = function(e) {var target = e.target};<br>
911 * myTabs.addListener('available', handler);</code></p>
916 * Fires when the Element's HTMLElement subtree is rendered.
917 * <p>See: <a href="#addListener">Element.addListener</a></p>
918 * <p><strong>Event fields:</strong><br>
919 * <code><String> type</code> contentReady<br>
920 * <code><HTMLElement>
921 * target</code> the HTMLElement bound to this Element instance<br>
922 * <p><strong>Usage:</strong><br>
923 * <code>var handler = function(e) {var target = e.target};<br>
924 * myTabs.addListener('contentReady', handler);</code></p>
925 * @event contentReady
928 YAHOO.augment(YAHOO.util.Element, AttributeProvider);
932 var Dom = YAHOO.util.Dom,
933 Event = YAHOO.util.Event,
934 Lang = YAHOO.util.Lang;
937 * A representation of a Tab's label and content.
938 * @namespace YAHOO.widget
940 * @extends YAHOO.util.Element
942 * @param element {HTMLElement | String} (optional) The html element that
943 * represents the TabView. An element will be created if none provided.
944 * @param {Object} properties A key map of initial properties
946 var Tab = function(el, attr) {
948 if (arguments.length == 1 && !Lang.isString(el) && !el.nodeName) {
953 if (!el && !attr.element) {
954 el = _createTabElement.call(this, attr);
958 success: function(o) {
959 this.set('content', o.responseText);
961 failure: function(o) {
965 Tab.superclass.constructor.call(this, el, attr);
967 this.DOM_EVENTS = {}; // delegating to tabView
970 YAHOO.extend(Tab, YAHOO.util.Element);
971 var proto = Tab.prototype;
974 * The default tag name for a Tab's inner element.
975 * @property LABEL_INNER_TAGNAME
979 proto.LABEL_TAGNAME = 'em';
982 * The class name applied to active tabs.
983 * @property ACTIVE_CLASSNAME
987 proto.ACTIVE_CLASSNAME = 'selected';
990 * The class name applied to disabled tabs.
991 * @property DISABLED_CLASSNAME
993 * @default "disabled"
995 proto.DISABLED_CLASSNAME = 'disabled';
998 * The class name applied to dynamic tabs while loading.
999 * @property LOADING_CLASSNAME
1001 * @default "disabled"
1003 proto.LOADING_CLASSNAME = 'loading';
1006 * Provides a reference to the connection request object when data is
1007 * loaded dynamically.
1008 * @property dataConnection
1011 proto.dataConnection = null;
1014 * Object containing success and failure callbacks for loading data.
1015 * @property loadHandler
1018 proto.loadHandler = null;
1021 * Provides a readable name for the tab.
1025 proto.toString = function() {
1026 var el = this.get('element');
1027 var id = el.id || el.tagName;
1032 * Registers TabView specific properties.
1033 * @method initAttributes
1034 * @param {Object} attr Hash of initial attributes
1036 proto.initAttributes = function(attr) {
1038 Tab.superclass.initAttributes.call(this, attr);
1040 var el = this.get('element');
1043 * The event that triggers the tab's activation.
1044 * @config activationEvent
1047 this.register('activationEvent', {
1048 value: attr.activationEvent || 'click'
1052 * The element that contains the tab's label.
1056 this.register('labelEl', {
1057 value: attr.labelEl || _getlabelEl.call(this),
1058 method: function(value) {
1059 var current = this.get('labelEl');
1062 if (current == value) {
1063 return false; // already set
1066 this.replaceChild(value, current);
1067 } else if (el.firstChild) { // ensure label is firstChild by default
1068 this.insertBefore(value, el.firstChild);
1070 this.appendChild(value);
1076 * The tab's label text (or innerHTML).
1080 this.register('label', {
1081 value: attr.label || _getLabel.call(this),
1082 method: function(value) {
1083 var labelEl = this.get('labelEl');
1084 if (!labelEl) { // create if needed
1085 this.set('labelEl', _createlabelEl.call(this));
1088 _setLabel.call(this, value);
1093 * The HTMLElement that contains the tab's content.
1097 this.register('contentEl', { // TODO: apply className?
1098 value: attr.contentEl || document.createElement('div'),
1099 method: function(value) {
1100 var current = this.get('contentEl');
1103 if (current == value) {
1104 return false; // already set
1106 this.replaceChild(value, current);
1112 * The tab's content.
1116 this.register('content', {
1117 value: attr.content, // TODO: what about existing?
1118 method: function(value) {
1119 this.get('contentEl').innerHTML = value;
1123 var _dataLoaded = false;
1126 * The tab's data source, used for loading content dynamically.
1130 this.register('dataSrc', {
1135 * Whether or not content should be reloaded for every view.
1140 this.register('cacheData', {
1141 value: attr.cacheData || false,
1142 validator: Lang.isBoolean
1146 * The method to use for the data request.
1147 * @config loadMethod
1151 this.register('loadMethod', {
1152 value: attr.loadMethod || 'GET',
1153 validator: Lang.isString
1157 * Whether or not any data has been loaded from the server.
1158 * @config dataLoaded
1161 this.register('dataLoaded', {
1163 validator: Lang.isBoolean,
1168 * Number if milliseconds before aborting and calling failure handler.
1169 * @config dataTimeout
1173 this.register('dataTimeout', {
1174 value: attr.dataTimeout || null,
1175 validator: Lang.isNumber
1179 * Whether or not the tab is currently active.
1180 * If a dataSrc is set for the tab, the content will be loaded from
1185 this.register('active', {
1186 value: attr.active || this.hasClass(this.ACTIVE_CLASSNAME),
1187 method: function(value) {
1188 if (value === true) {
1189 this.addClass(this.ACTIVE_CLASSNAME);
1190 this.set('title', 'active');
1192 this.removeClass(this.ACTIVE_CLASSNAME);
1193 this.set('title', '');
1196 validator: function(value) {
1197 return Lang.isBoolean(value) && !this.get('disabled') ;
1202 * Whether or not the tab is disabled.
1206 this.register('disabled', {
1207 value: attr.disabled || this.hasClass(this.DISABLED_CLASSNAME),
1208 method: function(value) {
1209 if (value === true) {
1210 Dom.addClass(this.get('element'), this.DISABLED_CLASSNAME);
1212 Dom.removeClass(this.get('element'), this.DISABLED_CLASSNAME);
1215 validator: Lang.isBoolean
1219 * The href of the tab's anchor element.
1224 this.register('href', {
1225 value: attr.href || '#',
1226 method: function(value) {
1227 this.getElementsByTagName('a')[0].href = value;
1229 validator: Lang.isString
1233 * The Whether or not the tab's content is visible.
1234 * @config contentVisible
1238 this.register('contentVisible', {
1239 value: attr.contentVisible,
1240 method: function(value) {
1241 if (value == true) {
1242 this.get('contentEl').style.display = 'block';
1244 if ( this.get('dataSrc') ) {
1245 // load dynamic content unless already loaded and caching
1246 if ( !this.get('dataLoaded') || !this.get('cacheData') ) {
1247 _dataConnect.call(this);
1251 this.get('contentEl').style.display = 'none';
1254 validator: Lang.isBoolean
1258 var _createTabElement = function(attr) {
1259 var el = document.createElement('li');
1260 var a = document.createElement('a');
1262 a.href = attr.href || '#';
1266 var label = attr.label || null;
1267 var labelEl = attr.labelEl || null;
1269 if (labelEl) { // user supplied labelEl
1270 if (!label) { // user supplied label
1271 label = _getLabel.call(this, labelEl);
1274 labelEl = _createlabelEl.call(this);
1277 a.appendChild(labelEl);
1282 var _getlabelEl = function() {
1283 return this.getElementsByTagName(this.LABEL_TAGNAME)[0];
1286 var _createlabelEl = function() {
1287 var el = document.createElement(this.LABEL_TAGNAME);
1291 var _setLabel = function(label) {
1292 var el = this.get('labelEl');
1293 el.innerHTML = label;
1296 var _getLabel = function() {
1298 el = this.get('labelEl');
1304 return el.innerHTML;
1307 var _dataConnect = function() {
1308 if (!YAHOO.util.Connect) {
1312 Dom.addClass(this.get('contentEl').parentNode, this.LOADING_CLASSNAME);
1314 this.dataConnection = YAHOO.util.Connect.asyncRequest(
1315 this.get('loadMethod'),
1316 this.get('dataSrc'),
1318 success: function(o) {
1319 this.loadHandler.success.call(this, o);
1320 this.set('dataLoaded', true);
1321 this.dataConnection = null;
1322 Dom.removeClass(this.get('contentEl').parentNode,
1323 this.LOADING_CLASSNAME);
1325 failure: function(o) {
1326 this.loadHandler.failure.call(this, o);
1327 this.dataConnection = null;
1328 Dom.removeClass(this.get('contentEl').parentNode,
1329 this.LOADING_CLASSNAME);
1332 timeout: this.get('dataTimeout')
1337 YAHOO.widget.Tab = Tab;
1340 * Fires before the active state is changed.
1341 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1342 * <p>If handler returns false, the change will be cancelled, and the value will not
1344 * <p><strong>Event fields:</strong><br>
1345 * <code><String> type</code> beforeActiveChange<br>
1346 * <code><Boolean>
1347 * prevValue</code> the current value<br>
1348 * <code><Boolean>
1349 * newValue</code> the new value</p>
1350 * <p><strong>Usage:</strong><br>
1351 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1352 * myTabs.addListener('beforeActiveChange', handler);</code></p>
1353 * @event beforeActiveChange
1357 * Fires after the active state is changed.
1358 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1359 * <p><strong>Event fields:</strong><br>
1360 * <code><String> type</code> activeChange<br>
1361 * <code><Boolean>
1362 * prevValue</code> the previous value<br>
1363 * <code><Boolean>
1364 * newValue</code> the updated value</p>
1365 * <p><strong>Usage:</strong><br>
1366 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1367 * myTabs.addListener('activeChange', handler);</code></p>
1368 * @event activeChange
1372 * Fires before the tab label is changed.
1373 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1374 * <p>If handler returns false, the change will be cancelled, and the value will not
1376 * <p><strong>Event fields:</strong><br>
1377 * <code><String> type</code> beforeLabelChange<br>
1378 * <code><String>
1379 * prevValue</code> the current value<br>
1380 * <code><String>
1381 * newValue</code> the new value</p>
1382 * <p><strong>Usage:</strong><br>
1383 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1384 * myTabs.addListener('beforeLabelChange', handler);</code></p>
1385 * @event beforeLabelChange
1389 * Fires after the tab label is changed.
1390 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1391 * <p><strong>Event fields:</strong><br>
1392 * <code><String> type</code> labelChange<br>
1393 * <code><String>
1394 * prevValue</code> the previous value<br>
1395 * <code><String>
1396 * newValue</code> the updated value</p>
1397 * <p><strong>Usage:</strong><br>
1398 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1399 * myTabs.addListener('labelChange', handler);</code></p>
1400 * @event labelChange
1404 * Fires before the tab content is changed.
1405 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1406 * <p>If handler returns false, the change will be cancelled, and the value will not
1408 * <p><strong>Event fields:</strong><br>
1409 * <code><String> type</code> beforeContentChange<br>
1410 * <code><String>
1411 * prevValue</code> the current value<br>
1412 * <code><String>
1413 * newValue</code> the new value</p>
1414 * <p><strong>Usage:</strong><br>
1415 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1416 * myTabs.addListener('beforeContentChange', handler);</code></p>
1417 * @event beforeContentChange
1421 * Fires after the tab content is changed.
1422 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1423 * <p><strong>Event fields:</strong><br>
1424 * <code><String> type</code> contentChange<br>
1425 * <code><String>
1426 * prevValue</code> the previous value<br>
1427 * <code><Boolean>
1428 * newValue</code> the updated value</p>
1429 * <p><strong>Usage:</strong><br>
1430 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1431 * myTabs.addListener('contentChange', handler);</code></p>
1432 * @event contentChange
1439 * The tabview module provides a widget for managing content bound to tabs.
1441 * @requires yahoo, dom, event
1445 * A widget to control tabbed views.
1446 * @namespace YAHOO.widget
1448 * @extends YAHOO.util.Element
1450 * @param {HTMLElement | String | Object} el(optional) The html
1451 * element that represents the TabView, or the attribute object to use.
1452 * An element will be created if none provided.
1453 * @param {Object} attr (optional) A key map of the tabView's
1454 * initial attributes. Ignored if first arg is attributes object.
1456 YAHOO.widget.TabView = function(el, attr) {
1458 if (arguments.length == 1 && !Lang.isString(el) && !el.nodeName) {
1459 attr = el; // treat first arg as attr object
1460 el = attr.element || null;
1463 if (!el && !attr.element) { // create if we dont have one
1464 el = _createTabViewElement.call(this, attr);
1466 YAHOO.widget.TabView.superclass.constructor.call(this, el, attr);
1469 YAHOO.extend(YAHOO.widget.TabView, YAHOO.util.Element);
1471 var proto = YAHOO.widget.TabView.prototype;
1472 var Dom = YAHOO.util.Dom;
1473 var Lang = YAHOO.util.Lang;
1474 var Event = YAHOO.util.Event;
1475 var Tab = YAHOO.widget.Tab;
1479 * The className to add when building from scratch.
1480 * @property CLASSNAME
1483 proto.CLASSNAME = 'yui-navset';
1486 * The className of the HTMLElement containing the TabView's tab elements
1487 * to look for when building from existing markup, or to add when building
1489 * All childNodes of the tab container are treated as Tabs when building
1490 * from existing markup.
1491 * @property TAB_PARENT_CLASSNAME
1494 proto.TAB_PARENT_CLASSNAME = 'yui-nav';
1497 * The className of the HTMLElement containing the TabView's label elements
1498 * to look for when building from existing markup, or to add when building
1500 * All childNodes of the content container are treated as content elements when
1501 * building from existing markup.
1502 * @property CONTENT_PARENT_CLASSNAME
1503 * @default "nav-content"
1505 proto.CONTENT_PARENT_CLASSNAME = 'yui-content';
1507 proto._tabParent = null;
1508 proto._contentParent = null;
1511 * Adds a Tab to the TabView instance.
1512 * If no index is specified, the tab is added to the end of the tab list.
1514 * @param {YAHOO.widget.Tab} tab A Tab instance to add.
1515 * @param {Integer} index The position to add the tab.
1518 proto.addTab = function(tab, index) {
1519 var tabs = this.get('tabs');
1520 if (!tabs) { // not ready yet
1521 this._queue[this._queue.length] = ['addTab', arguments];
1525 index = (index === undefined) ? tabs.length : index;
1527 var before = this.getTab(index);
1530 var el = this.get('element');
1531 var tabParent = this._tabParent;
1532 var contentParent = this._contentParent;
1534 var tabElement = tab.get('element');
1535 var contentEl = tab.get('contentEl');
1538 tabParent.insertBefore(tabElement, before.get('element'));
1540 tabParent.appendChild(tabElement);
1543 if ( contentEl && !Dom.isAncestor(contentParent, contentEl) ) {
1544 contentParent.appendChild(contentEl);
1547 if ( !tab.get('active') ) {
1548 tab.set('contentVisible', false, true); /* hide if not active */
1550 this.set('activeTab', tab, true);
1554 var activate = function(e) {
1555 YAHOO.util.Event.preventDefault(e);
1556 self.set('activeTab', this);
1559 tab.addListener( tab.get('activationEvent'), activate);
1561 tab.addListener('activationEventChange', function(e) {
1562 if (e.prevValue != e.newValue) {
1563 tab.removeListener(e.prevValue, activate);
1564 tab.addListener(e.newValue, activate);
1568 tabs.splice(index, 0, tab);
1572 * Routes childNode events.
1573 * @method DOMEventHandler
1574 * @param {event} e The Dom event that is being handled.
1577 proto.DOMEventHandler = function(e) {
1578 var el = this.get('element');
1579 var target = YAHOO.util.Event.getTarget(e);
1580 var tabParent = this._tabParent;
1582 if (Dom.isAncestor(tabParent, target) ) {
1586 var tabs = this.get('tabs');
1588 for (var i = 0, len = tabs.length; i < len; i++) {
1589 tabEl = tabs[i].get('element');
1590 contentEl = tabs[i].get('contentEl');
1592 if ( target == tabEl || Dom.isAncestor(tabEl, target) ) {
1594 break; // note break
1599 tab.fireEvent(e.type, e);
1605 * Returns the Tab instance at the specified index.
1607 * @param {Integer} index The position of the Tab.
1608 * @return YAHOO.widget.Tab
1610 proto.getTab = function(index) {
1611 return this.get('tabs')[index];
1615 * Returns the index of given tab.
1616 * @method getTabIndex
1617 * @param {YAHOO.widget.Tab} tab The tab whose index will be returned.
1620 proto.getTabIndex = function(tab) {
1622 var tabs = this.get('tabs');
1623 for (var i = 0, len = tabs.length; i < len; ++i) {
1624 if (tab == tabs[i]) {
1634 * Removes the specified Tab from the TabView.
1636 * @param {YAHOO.widget.Tab} item The Tab instance to be removed.
1639 proto.removeTab = function(tab) {
1640 var tabCount = this.get('tabs').length;
1642 var index = this.getTabIndex(tab);
1643 var nextIndex = index + 1;
1644 if ( tab == this.get('activeTab') ) { // select next tab
1646 if (index + 1 == tabCount) {
1647 this.set('activeIndex', index - 1);
1649 this.set('activeIndex', index + 1);
1654 this._tabParent.removeChild( tab.get('element') );
1655 this._contentParent.removeChild( tab.get('contentEl') );
1656 this._configs.tabs.value.splice(index, 1);
1661 * Provides a readable name for the TabView instance.
1665 proto.toString = function() {
1666 var name = this.get('id') || this.get('tagName');
1667 return "TabView " + name;
1671 * The transiton to use when switching between tabs.
1672 * @method contentTransition
1674 proto.contentTransition = function(newTab, oldTab) {
1675 newTab.set('contentVisible', true);
1676 oldTab.set('contentVisible', false);
1680 * Registers TabView specific properties.
1681 * @method initAttributes
1682 * @param {Object} attr Hash of initial attributes
1684 proto.initAttributes = function(attr) {
1685 YAHOO.widget.TabView.superclass.initAttributes.call(this, attr);
1687 if (!attr.orientation) {
1688 attr.orientation = 'top';
1691 var el = this.get('element');
1694 * The Tabs belonging to the TabView instance.
1698 this.register('tabs', {
1704 * The container of the tabView's label elements.
1705 * @property _tabParent
1710 this.getElementsByClassName(this.TAB_PARENT_CLASSNAME,
1711 'ul' )[0] || _createTabParent.call(this);
1714 * The container of the tabView's content elements.
1715 * @property _contentParent
1719 this._contentParent =
1720 this.getElementsByClassName(this.CONTENT_PARENT_CLASSNAME,
1721 'div')[0] || _createContentParent.call(this);
1724 * How the Tabs should be oriented relative to the TabView.
1725 * @config orientation
1729 this.register('orientation', {
1730 value: attr.orientation,
1731 method: function(value) {
1732 var current = this.get('orientation');
1733 this.addClass('yui-navset-' + value);
1735 if (current != value) {
1736 this.removeClass('yui-navset-' + current);
1741 this.appendChild(this._tabParent);
1748 * The index of the tab currently active.
1749 * @config activeIndex
1752 this.register('activeIndex', {
1753 value: attr.activeIndex,
1754 method: function(value) {
1755 this.set('activeTab', this.getTab(value));
1757 validator: function(value) {
1758 return !this.getTab(value).get('disabled'); // cannot activate if disabled
1763 * The tab currently active.
1765 * @type YAHOO.widget.Tab
1767 this.register('activeTab', {
1768 value: attr.activeTab,
1769 method: function(tab) {
1770 var activeTab = this.get('activeTab');
1773 tab.set('active', true);
1776 if (activeTab && activeTab != tab) {
1777 activeTab.set('active', false);
1780 if (activeTab && tab != activeTab) { // no transition if only 1
1781 this.contentTransition(tab, activeTab);
1783 tab.set('contentVisible', true);
1786 validator: function(value) {
1787 return !value.get('disabled'); // cannot activate if disabled
1791 if ( this._tabParent ) {
1792 _initTabs.call(this);
1795 for (var type in this.DOM_EVENTS) {
1796 if ( this.DOM_EVENTS.hasOwnProperty(type) ) {
1797 this.addListener.call(this, type, this.DOMEventHandler);
1803 * Creates Tab instances from a collection of HTMLElements.
1804 * @method createTabs
1806 * @param {Array|HTMLCollection} elements The elements to use for Tabs.
1809 var _initTabs = function() {
1814 var el = this.get('element');
1815 var tabs = _getChildNodes(this._tabParent);
1816 var contentElements = _getChildNodes(this._contentParent);
1818 for (var i = 0, len = tabs.length; i < len; ++i) {
1821 if (contentElements[i]) {
1822 attr.contentEl = contentElements[i];
1825 tab = new YAHOO.widget.Tab(tabs[i], attr);
1828 if (tab.hasClass(tab.ACTIVE_CLASSNAME) ) {
1829 this._configs.activeTab.value = tab; // dont invoke method
1834 var _createTabViewElement = function(attr) {
1835 var el = document.createElement('div');
1837 if ( this.CLASSNAME ) {
1838 el.className = this.CLASSNAME;
1844 var _createTabParent = function(attr) {
1845 var el = document.createElement('ul');
1847 if ( this.TAB_PARENT_CLASSNAME ) {
1848 el.className = this.TAB_PARENT_CLASSNAME;
1851 this.get('element').appendChild(el);
1856 var _createContentParent = function(attr) {
1857 var el = document.createElement('div');
1859 if ( this.CONTENT_PARENT_CLASSNAME ) {
1860 el.className = this.CONTENT_PARENT_CLASSNAME;
1863 this.get('element').appendChild(el);
1868 var _getChildNodes = function(el) {
1870 var childNodes = el.childNodes;
1872 for (var i = 0, len = childNodes.length; i < len; ++i) {
1873 if (childNodes[i].nodeType == 1) {
1874 nodes[nodes.length] = childNodes[i];
1882 * Fires before the activeTab is changed.
1883 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1884 * <p>If handler returns false, the change will be cancelled, and the value will not
1886 * <p><strong>Event fields:</strong><br>
1887 * <code><String> type</code> beforeActiveTabChange<br>
1888 * <code><<a href="YAHOO.widget.Tab.html">YAHOO.widget.Tab</a>>
1889 * prevValue</code> the currently active tab<br>
1890 * <code><<a href="YAHOO.widget.Tab.html">YAHOO.widget.Tab</a>>
1891 * newValue</code> the tab to be made active</p>
1892 * <p><strong>Usage:</strong><br>
1893 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1894 * myTabs.addListener('beforeActiveTabChange', handler);</code></p>
1895 * @event beforeActiveTabChange
1899 * Fires after the activeTab is changed.
1900 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1901 * <p><strong>Event fields:</strong><br>
1902 * <code><String> type</code> activeTabChange<br>
1903 * <code><<a href="YAHOO.widget.Tab.html">YAHOO.widget.Tab</a>>
1904 * prevValue</code> the formerly active tab<br>
1905 * <code><<a href="YAHOO.widget.Tab.html">YAHOO.widget.Tab</a>>
1906 * newValue</code> the new active tab</p>
1907 * <p><strong>Usage:</strong><br>
1908 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1909 * myTabs.addListener('activeTabChange', handler);</code></p>
1910 * @event activeTabChange
1914 * Fires before the orientation is changed.
1915 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1916 * <p>If handler returns false, the change will be cancelled, and the value will not
1918 * <p><strong>Event fields:</strong><br>
1919 * <code><String> type</code> beforeOrientationChange<br>
1920 * <code><String>
1921 * prevValue</code> the current orientation<br>
1922 * <code><String>
1923 * newValue</code> the new orientation to be applied</p>
1924 * <p><strong>Usage:</strong><br>
1925 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1926 * myTabs.addListener('beforeOrientationChange', handler);</code></p>
1927 * @event beforeOrientationChange
1931 * Fires after the orientation is changed.
1932 * <p>See: <a href="YAHOO.util.Element.html#addListener">Element.addListener</a></p>
1933 * <p><strong>Event fields:</strong><br>
1934 * <code><String> type</code> orientationChange<br>
1935 * <code><String>
1936 * prevValue</code> the former orientation<br>
1937 * <code><String>
1938 * newValue</code> the new orientation</p>
1939 * <p><strong>Usage:</strong><br>
1940 * <code>var handler = function(e) {var previous = e.prevValue};<br>
1941 * myTabs.addListener('orientationChange', handler);</code></p>
1942 * @event orientationChange