2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 * Provides Attribute configurations.
9 * @namespace YAHOO.util
12 * @param hash {Object} The intial Attribute.
13 * @param {YAHOO.util.AttributeProvider} The owner of the Attribute instance.
16 YAHOO.util.Attribute = function(hash, owner) {
19 this.configure(hash, true);
23 YAHOO.util.Attribute.prototype = {
25 * The name of the attribute.
32 * The value of the attribute.
39 * The owner of the attribute.
41 * @type YAHOO.util.AttributeProvider
46 * Whether or not the attribute is read only.
53 * Whether or not the attribute can only be written once.
60 * The attribute's initial configuration.
62 * @property _initialConfig
68 * Whether or not the attribute's value has been set.
76 * A function to call when setting the attribute's value.
77 * The method receives the new value as the first arg and the attribute name as the 2nd
84 * The function to use when setting the attribute's value.
85 * The setter receives the new value as the first arg and the attribute name as the 2nd
86 * The return value of the setter replaces the value passed to set().
93 * The function to use when getting the attribute's value.
94 * The getter receives the new value as the first arg and the attribute name as the 2nd
95 * The return value of the getter will be used as the return from get().
102 * The validator to use when setting the attribute's value.
103 * @property validator
110 * Retrieves the current value of the attribute.
112 * @return {any} The current value of the attribute.
114 getValue: function() {
115 var val = this.value;
118 val = this.getter.call(this.owner, this.name);
125 * Sets the value of the attribute and fires beforeChange and change events.
127 * @param {Any} value The value to apply to the attribute.
128 * @param {Boolean} silent If true the change events will not be fired.
129 * @return {Boolean} Whether or not the value was set.
131 setValue: function(value, silent) {
138 prevValue: this.getValue(),
142 if (this.readOnly || ( this.writeOnce && this._written) ) {
143 YAHOO.log( 'setValue ' + name + ', ' + value +
144 ' failed: read only', 'error', 'Attribute');
145 return false; // write not allowed
148 if (this.validator && !this.validator.call(owner, value) ) {
149 YAHOO.log( 'setValue ' + name + ', ' + value +
150 ' validation failed', 'error', 'Attribute');
151 return false; // invalid value
155 beforeRetVal = owner.fireBeforeChangeEvent(event);
156 if (beforeRetVal === false) {
157 YAHOO.log('setValue ' + name +
158 ' cancelled by beforeChange event', 'info', 'Attribute');
164 value = this.setter.call(owner, value, this.name);
165 if (value === undefined) {
166 YAHOO.log('setter for ' + this.name + ' returned undefined', 'warn', 'Attribute');
171 this.method.call(owner, value, this.name);
174 this.value = value; // TODO: set before calling setter/method?
175 this._written = true;
180 this.owner.fireChangeEvent(event);
187 * Allows for configuring the Attribute's properties.
189 * @param {Object} map A key-value map of Attribute properties.
190 * @param {Boolean} init Whether or not this should become the initial config.
192 configure: function(map, init) {
196 this._written = false; // reset writeOnce
199 this._initialConfig = this._initialConfig || {};
201 for (var key in map) {
202 if ( map.hasOwnProperty(key) ) {
203 this[key] = map[key];
205 this._initialConfig[key] = map[key];
212 * Resets the value to the initial config value.
214 * @return {Boolean} Whether or not the value was set.
216 resetValue: function() {
217 return this.setValue(this._initialConfig.value);
221 * Resets the attribute config to the initial config state.
222 * @method resetConfig
224 resetConfig: function() {
225 this.configure(this._initialConfig, true);
229 * Resets the value to the current value.
230 * Useful when values may have gotten out of sync with actual properties.
232 * @return {Boolean} Whether or not the value was set.
234 refresh: function(silent) {
235 this.setValue(this.value, silent);
240 var Lang = YAHOO.util.Lang;
243 Copyright (c) 2006, Yahoo! Inc. All rights reserved.
244 Code licensed under the BSD License:
245 http://developer.yahoo.net/yui/license.txt
249 * Provides and manages YAHOO.util.Attribute instances
250 * @namespace YAHOO.util
251 * @class AttributeProvider
252 * @uses YAHOO.util.EventProvider
254 YAHOO.util.AttributeProvider = function() {};
256 YAHOO.util.AttributeProvider.prototype = {
259 * A key-value map of Attribute configurations
261 * @protected (may be used by subclasses and augmentors)
267 * Returns the current value of the attribute.
269 * @param {String} key The attribute whose value will be returned.
270 * @return {Any} The current value of the attribute.
273 this._configs = this._configs || {};
274 var config = this._configs[key];
276 if (!config || !this._configs.hasOwnProperty(key)) {
277 YAHOO.log(key + ' not found', 'error', 'AttributeProvider');
281 return config.getValue();
285 * Sets the value of a config.
287 * @param {String} key The name of the attribute
288 * @param {Any} value The value to apply to the attribute
289 * @param {Boolean} silent Whether or not to suppress change events
290 * @return {Boolean} Whether or not the value was set.
292 set: function(key, value, silent){
293 this._configs = this._configs || {};
294 var config = this._configs[key];
297 YAHOO.log('set failed: ' + key + ' not found',
298 'error', 'AttributeProvider');
302 return config.setValue(value, silent);
306 * Returns an array of attribute names.
307 * @method getAttributeKeys
308 * @return {Array} An array of attribute names.
310 getAttributeKeys: function(){
311 this._configs = this._configs;
314 for (key in this._configs) {
315 if ( Lang.hasOwnProperty(this._configs, key) &&
316 !Lang.isUndefined(this._configs[key]) ) {
317 keys[keys.length] = key;
325 * Sets multiple attribute values.
326 * @method setAttributes
327 * @param {Object} map A key-value map of attributes
328 * @param {Boolean} silent Whether or not to suppress change events
330 setAttributes: function(map, silent){
331 for (var key in map) {
332 if ( Lang.hasOwnProperty(map, key) ) {
333 this.set(key, map[key], silent);
339 * Resets the specified attribute's value to its initial value.
341 * @param {String} key The name of the attribute
342 * @param {Boolean} silent Whether or not to suppress change events
343 * @return {Boolean} Whether or not the value was set
345 resetValue: function(key, silent){
346 this._configs = this._configs || {};
347 if (this._configs[key]) {
348 this.set(key, this._configs[key]._initialConfig.value, silent);
355 * Sets the attribute's value to its current value.
357 * @param {String | Array} key The attribute(s) to refresh
358 * @param {Boolean} silent Whether or not to suppress change events
360 refresh: function(key, silent) {
361 this._configs = this._configs || {};
362 var configs = this._configs;
364 key = ( ( Lang.isString(key) ) ? [key] : key ) ||
365 this.getAttributeKeys();
367 for (var i = 0, len = key.length; i < len; ++i) {
368 if (configs.hasOwnProperty(key[i])) {
369 this._configs[key[i]].refresh(silent);
375 * Adds an Attribute to the AttributeProvider instance.
377 * @param {String} key The attribute's name
378 * @param {Object} map A key-value map containing the
379 * attribute's properties.
380 * @deprecated Use setAttributeConfig
382 register: function(key, map) {
383 this.setAttributeConfig(key, map);
388 * Returns the attribute's properties.
389 * @method getAttributeConfig
390 * @param {String} key The attribute's name
392 * @return {object} A key-value map containing all of the
393 * attribute's properties.
395 getAttributeConfig: function(key) {
396 this._configs = this._configs || {};
397 var config = this._configs[key] || {};
398 var map = {}; // returning a copy to prevent overrides
400 for (key in config) {
401 if ( Lang.hasOwnProperty(config, key) ) {
402 map[key] = config[key];
410 * Sets or updates an Attribute instance's properties.
411 * @method setAttributeConfig
412 * @param {String} key The attribute's name.
413 * @param {Object} map A key-value map of attribute properties
414 * @param {Boolean} init Whether or not this should become the intial config.
416 setAttributeConfig: function(key, map, init) {
417 this._configs = this._configs || {};
419 if (!this._configs[key]) {
421 this._configs[key] = this.createAttribute(map);
423 this._configs[key].configure(map, init);
428 * Sets or updates an Attribute instance's properties.
429 * @method configureAttribute
430 * @param {String} key The attribute's name.
431 * @param {Object} map A key-value map of attribute properties
432 * @param {Boolean} init Whether or not this should become the intial config.
433 * @deprecated Use setAttributeConfig
435 configureAttribute: function(key, map, init) {
436 this.setAttributeConfig(key, map, init);
440 * Resets an attribute to its intial configuration.
441 * @method resetAttributeConfig
442 * @param {String} key The attribute's name.
445 resetAttributeConfig: function(key){
446 this._configs = this._configs || {};
447 this._configs[key].resetConfig();
450 // wrapper for EventProvider.subscribe
451 // to create events on the fly
452 subscribe: function(type, callback) {
453 this._events = this._events || {};
455 if ( !(type in this._events) ) {
456 this._events[type] = this.createEvent(type);
459 YAHOO.util.EventProvider.prototype.subscribe.apply(this, arguments);
463 this.subscribe.apply(this, arguments);
466 addListener: function() {
467 this.subscribe.apply(this, arguments);
471 * Fires the attribute's beforeChange event.
472 * @method fireBeforeChangeEvent
473 * @param {String} key The attribute's name.
474 * @param {Obj} e The event object to pass to handlers.
476 fireBeforeChangeEvent: function(e) {
478 type += e.type.charAt(0).toUpperCase() + e.type.substr(1) + 'Change';
480 return this.fireEvent(e.type, e);
484 * Fires the attribute's change event.
485 * @method fireChangeEvent
486 * @param {String} key The attribute's name.
487 * @param {Obj} e The event object to pass to the handlers.
489 fireChangeEvent: function(e) {
491 return this.fireEvent(e.type, e);
494 createAttribute: function(map) {
495 return new YAHOO.util.Attribute(map, this);
499 YAHOO.augment(YAHOO.util.AttributeProvider, YAHOO.util.EventProvider);
503 // internal shorthand
504 var Dom = YAHOO.util.Dom,
505 AttributeProvider = YAHOO.util.AttributeProvider;
508 * Element provides an wrapper object to simplify adding
509 * event listeners, using dom methods, and managing attributes.
511 * @namespace YAHOO.util
512 * @requires yahoo, dom, event
516 * Element provides an wrapper object to simplify adding
517 * event listeners, using dom methods, and managing attributes.
519 * @uses YAHOO.util.AttributeProvider
521 * @param el {HTMLElement | String} The html element that
522 * represents the Element.
523 * @param {Object} map A key-value map of initial config names and values
525 var Element = function(el, map) {
526 this.init.apply(this, arguments);
529 Element.DOM_EVENTS = {
546 Element.prototype = {
548 * Dom events supported by the Element instance.
549 * @property DOM_EVENTS
554 DEFAULT_HTML_SETTER: function(value, key) {
555 var el = this.get('element');
562 DEFAULT_HTML_GETTER: function(key) {
563 var el = this.get('element'),
574 * Wrapper for HTMLElement method.
575 * @method appendChild
576 * @param {YAHOO.util.Element || HTMLElement} child The element to append.
577 * @return {HTMLElement} The appended DOM element.
579 appendChild: function(child) {
580 child = child.get ? child.get('element') : child;
581 return this.get('element').appendChild(child);
585 * Wrapper for HTMLElement method.
586 * @method getElementsByTagName
587 * @param {String} tag The tagName to collect
588 * @return {HTMLCollection} A collection of DOM elements.
590 getElementsByTagName: function(tag) {
591 return this.get('element').getElementsByTagName(tag);
595 * Wrapper for HTMLElement method.
596 * @method hasChildNodes
597 * @return {Boolean} Whether or not the element has childNodes
599 hasChildNodes: function() {
600 return this.get('element').hasChildNodes();
604 * Wrapper for HTMLElement method.
605 * @method insertBefore
606 * @param {HTMLElement} element The HTMLElement to insert
607 * @param {HTMLElement} before The HTMLElement to insert
608 * the element before.
609 * @return {HTMLElement} The inserted DOM element.
611 insertBefore: function(element, before) {
612 element = element.get ? element.get('element') : element;
613 before = (before && before.get) ? before.get('element') : before;
615 return this.get('element').insertBefore(element, before);
619 * Wrapper for HTMLElement method.
620 * @method removeChild
621 * @param {HTMLElement} child The HTMLElement to remove
622 * @return {HTMLElement} The removed DOM element.
624 removeChild: function(child) {
625 child = child.get ? child.get('element') : child;
626 return this.get('element').removeChild(child);
630 * Wrapper for HTMLElement method.
631 * @method replaceChild
632 * @param {HTMLElement} newNode The HTMLElement to insert
633 * @param {HTMLElement} oldNode The HTMLElement to replace
634 * @return {HTMLElement} The replaced DOM element.
636 replaceChild: function(newNode, oldNode) {
637 newNode = newNode.get ? newNode.get('element') : newNode;
638 oldNode = oldNode.get ? oldNode.get('element') : oldNode;
639 return this.get('element').replaceChild(newNode, oldNode);
644 * Registers Element specific attributes.
645 * @method initAttributes
646 * @param {Object} map A key-value map of initial attribute configs
648 initAttributes: function(map) {
652 * Adds a listener for the given event. These may be DOM or
653 * customEvent listeners. Any event that is fired via fireEvent
654 * can be listened for. All handlers receive an event object.
655 * @method addListener
656 * @param {String} type The name of the event to listen for
657 * @param {Function} fn The handler to call when the event fires
658 * @param {Any} obj A variable to pass to the handler
659 * @param {Object} scope The object to use for the scope of the handler
661 addListener: function(type, fn, obj, scope) {
662 var el = this.get('element') || this.get('id');
663 scope = scope || this;
666 if (!this._events[type]) { // create on the fly
667 if (el && this.DOM_EVENTS[type]) {
668 YAHOO.util.Event.addListener(el, type, function(e) {
669 if (e.srcElement && !e.target) { // supplement IE with target
670 e.target = e.srcElement;
672 self.fireEvent(type, e);
675 this.createEvent(type, this);
678 return YAHOO.util.EventProvider.prototype.subscribe.apply(this, arguments); // notify via customEvent
683 * Alias for addListener
685 * @param {String} type The name of the event to listen for
686 * @param {Function} fn The function call when the event fires
687 * @param {Any} obj A variable to pass to the handler
688 * @param {Object} scope The object to use for the scope of the handler
691 return this.addListener.apply(this, arguments);
695 * Alias for addListener
697 * @param {String} type The name of the event to listen for
698 * @param {Function} fn The function call when the event fires
699 * @param {Any} obj A variable to pass to the handler
700 * @param {Object} scope The object to use for the scope of the handler
702 subscribe: function() {
703 return this.addListener.apply(this, arguments);
707 * Remove an event listener
708 * @method removeListener
709 * @param {String} type The name of the event to listen for
710 * @param {Function} fn The function call when the event fires
712 removeListener: function(type, fn) {
713 return this.unsubscribe.apply(this, arguments);
717 * Wrapper for Dom method.
719 * @param {String} className The className to add
721 addClass: function(className) {
722 Dom.addClass(this.get('element'), className);
726 * Wrapper for Dom method.
727 * @method getElementsByClassName
728 * @param {String} className The className to collect
729 * @param {String} tag (optional) The tag to use in
730 * conjunction with class name
731 * @return {Array} Array of HTMLElements
733 getElementsByClassName: function(className, tag) {
734 return Dom.getElementsByClassName(className, tag,
735 this.get('element') );
739 * Wrapper for Dom method.
741 * @param {String} className The className to add
742 * @return {Boolean} Whether or not the element has the class name
744 hasClass: function(className) {
745 return Dom.hasClass(this.get('element'), className);
749 * Wrapper for Dom method.
750 * @method removeClass
751 * @param {String} className The className to remove
753 removeClass: function(className) {
754 return Dom.removeClass(this.get('element'), className);
758 * Wrapper for Dom method.
759 * @method replaceClass
760 * @param {String} oldClassName The className to replace
761 * @param {String} newClassName The className to add
763 replaceClass: function(oldClassName, newClassName) {
764 return Dom.replaceClass(this.get('element'),
765 oldClassName, newClassName);
769 * Wrapper for Dom method.
771 * @param {String} property The style property to set
772 * @param {String} value The value to apply to the style property
774 setStyle: function(property, value) {
775 return Dom.setStyle(this.get('element'), property, value); // TODO: always queuing?
779 * Wrapper for Dom method.
781 * @param {String} property The style property to retrieve
782 * @return {String} The current value of the property
784 getStyle: function(property) {
785 return Dom.getStyle(this.get('element'), property);
789 * Apply any queued set calls.
792 fireQueue: function() {
793 var queue = this._queue;
794 for (var i = 0, len = queue.length; i < len; ++i) {
795 this[queue[i][0]].apply(this, queue[i][1]);
800 * Appends the HTMLElement into either the supplied parentNode.
802 * @param {HTMLElement | Element} parentNode The node to append to
803 * @param {HTMLElement | Element} before An optional node to insert before
804 * @return {HTMLElement} The appended DOM element.
806 appendTo: function(parent, before) {
807 parent = (parent.get) ? parent.get('element') : Dom.get(parent);
809 this.fireEvent('beforeAppendTo', {
810 type: 'beforeAppendTo',
815 before = (before && before.get) ?
816 before.get('element') : Dom.get(before);
817 var element = this.get('element');
820 YAHOO.log('appendTo failed: element not available',
826 YAHOO.log('appendTo failed: parent not available',
831 if (element.parent != parent) {
833 parent.insertBefore(element, before);
835 parent.appendChild(element);
839 YAHOO.log(element + 'appended to ' + parent);
841 this.fireEvent('appendTo', {
850 var configs = this._configs || {},
851 el = configs.element; // avoid loop due to 'element'
853 if (el && !configs[key] && !YAHOO.lang.isUndefined(el.value[key]) ) {
854 this._setHTMLAttrConfig(key);
857 return AttributeProvider.prototype.get.call(this, key);
860 setAttributes: function(map, silent) {
861 // set based on configOrder
863 configOrder = this._configOrder;
865 // set based on configOrder
866 for (var i = 0, len = configOrder.length; i < len; ++i) {
867 if (map[configOrder[i]] !== undefined) {
868 done[configOrder[i]] = true;
869 this.set(configOrder[i], map[configOrder[i]], silent);
873 // unconfigured (e.g. Dom attributes)
874 for (var att in map) {
875 if (map.hasOwnProperty(att) && !done[att]) {
876 this.set(att, map[att], silent);
881 set: function(key, value, silent) {
882 var el = this.get('element');
884 this._queue[this._queue.length] = ['set', arguments];
885 if (this._configs[key]) {
886 this._configs[key].value = value; // so "get" works while queueing
892 // set it on the element if not configured and is an HTML attribute
893 if ( !this._configs[key] && !YAHOO.lang.isUndefined(el[key]) ) {
894 this._setHTMLAttrConfig(key);
897 return AttributeProvider.prototype.set.apply(this, arguments);
900 setAttributeConfig: function(key, map, init) {
901 this._configOrder.push(key);
902 AttributeProvider.prototype.setAttributeConfig.apply(this, arguments);
905 createEvent: function(type, scope) {
906 this._events[type] = true;
907 return AttributeProvider.prototype.createEvent.apply(this, arguments);
910 init: function(el, attr) {
911 this._initElement(el, attr);
914 destroy: function() {
915 var el = this.get('element');
916 YAHOO.util.Event.purgeElement(el, true); // purge DOM listeners recursively
917 this.unsubscribeAll(); // unsubscribe all custom events
919 if (el && el.parentNode) {
920 el.parentNode.removeChild(el); // pull from the DOM
923 // revert initial configs
927 this._configOrder = [];
930 _initElement: function(el, attr) {
931 this._queue = this._queue || [];
932 this._events = this._events || {};
933 this._configs = this._configs || {};
934 this._configOrder = [];
936 attr.element = attr.element || el || null;
938 var isReady = false; // to determine when to init HTMLElement and content
940 var DOM_EVENTS = Element.DOM_EVENTS;
941 this.DOM_EVENTS = this.DOM_EVENTS || {};
943 for (var event in DOM_EVENTS) {
944 if (DOM_EVENTS.hasOwnProperty(event)) {
945 this.DOM_EVENTS[event] = DOM_EVENTS[event];
949 if (typeof attr.element === 'string') { // register ID for get() access
950 this._setHTMLAttrConfig('id', { value: attr.element });
953 if (Dom.get(attr.element)) {
955 this._initHTMLElement(attr);
956 this._initContent(attr);
959 YAHOO.util.Event.onAvailable(attr.element, function() {
960 if (!isReady) { // otherwise already done
961 this._initHTMLElement(attr);
964 this.fireEvent('available', { type: 'available', target: Dom.get(attr.element) });
967 YAHOO.util.Event.onContentReady(attr.element, function() {
968 if (!isReady) { // otherwise already done
969 this._initContent(attr);
971 this.fireEvent('contentReady', { type: 'contentReady', target: Dom.get(attr.element) });
975 _initHTMLElement: function(attr) {
977 * The HTMLElement the Element instance refers to.
981 this.setAttributeConfig('element', {
982 value: Dom.get(attr.element),
987 _initContent: function(attr) {
988 this.initAttributes(attr);
989 this.setAttributes(attr, true);
995 * Sets the value of the property and fires beforeChange and change events.
997 * @method _setHTMLAttrConfig
998 * @param {YAHOO.util.Element} element The Element instance to
999 * register the config to.
1000 * @param {String} key The name of the config to register
1001 * @param {Object} map A key-value map of the config's params
1003 _setHTMLAttrConfig: function(key, map) {
1004 var el = this.get('element');
1008 map.setter = map.setter || this.DEFAULT_HTML_SETTER;
1009 map.getter = map.getter || this.DEFAULT_HTML_GETTER;
1011 map.value = map.value || el[key];
1012 this._configs[key] = new YAHOO.util.Attribute(map, this);
1017 * Fires when the Element's HTMLElement can be retrieved by Id.
1018 * <p>See: <a href="#addListener">Element.addListener</a></p>
1019 * <p><strong>Event fields:</strong><br>
1020 * <code><String> type</code> available<br>
1021 * <code><HTMLElement>
1022 * target</code> the HTMLElement bound to this Element instance<br>
1023 * <p><strong>Usage:</strong><br>
1024 * <code>var handler = function(e) {var target = e.target};<br>
1025 * myTabs.addListener('available', handler);</code></p>
1030 * Fires when the Element's HTMLElement subtree is rendered.
1031 * <p>See: <a href="#addListener">Element.addListener</a></p>
1032 * <p><strong>Event fields:</strong><br>
1033 * <code><String> type</code> contentReady<br>
1034 * <code><HTMLElement>
1035 * target</code> the HTMLElement bound to this Element instance<br>
1036 * <p><strong>Usage:</strong><br>
1037 * <code>var handler = function(e) {var target = e.target};<br>
1038 * myTabs.addListener('contentReady', handler);</code></p>
1039 * @event contentReady
1043 * Fires before the Element is appended to another Element.
1044 * <p>See: <a href="#addListener">Element.addListener</a></p>
1045 * <p><strong>Event fields:</strong><br>
1046 * <code><String> type</code> beforeAppendTo<br>
1047 * <code><HTMLElement/Element>
1048 * target</code> the HTMLElement/Element being appended to
1049 * <p><strong>Usage:</strong><br>
1050 * <code>var handler = function(e) {var target = e.target};<br>
1051 * myTabs.addListener('beforeAppendTo', handler);</code></p>
1052 * @event beforeAppendTo
1056 * Fires after the Element is appended to another Element.
1057 * <p>See: <a href="#addListener">Element.addListener</a></p>
1058 * <p><strong>Event fields:</strong><br>
1059 * <code><String> type</code> appendTo<br>
1060 * <code><HTMLElement/Element>
1061 * target</code> the HTMLElement/Element being appended to
1062 * <p><strong>Usage:</strong><br>
1063 * <code>var handler = function(e) {var target = e.target};<br>
1064 * myTabs.addListener('appendTo', handler);</code></p>
1068 YAHOO.augment(Element, AttributeProvider);
1069 YAHOO.util.Element = Element;
1072 YAHOO.register("element", YAHOO.util.Element, {version: "2.7.0", build: "1799"});