/src/main/resources/org/apache/struts2/static/element/element-debug.js

http://struts2yuiplugin.googlecode.com/ · JavaScript · 1072 lines · 488 code · 149 blank · 435 comment · 97 complexity · 3acfcab5df53a14e44ed9d02904ea682 MD5 · raw file

  1. /*
  2. Copyright (c) 2009, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.net/yui/license.txt
  5. version: 2.7.0
  6. */
  7. /**
  8. * Provides Attribute configurations.
  9. * @namespace YAHOO.util
  10. * @class Attribute
  11. * @constructor
  12. * @param hash {Object} The intial Attribute.
  13. * @param {YAHOO.util.AttributeProvider} The owner of the Attribute instance.
  14. */
  15. YAHOO.util.Attribute = function(hash, owner) {
  16. if (owner) {
  17. this.owner = owner;
  18. this.configure(hash, true);
  19. }
  20. };
  21. YAHOO.util.Attribute.prototype = {
  22. /**
  23. * The name of the attribute.
  24. * @property name
  25. * @type String
  26. */
  27. name: undefined,
  28. /**
  29. * The value of the attribute.
  30. * @property value
  31. * @type String
  32. */
  33. value: null,
  34. /**
  35. * The owner of the attribute.
  36. * @property owner
  37. * @type YAHOO.util.AttributeProvider
  38. */
  39. owner: null,
  40. /**
  41. * Whether or not the attribute is read only.
  42. * @property readOnly
  43. * @type Boolean
  44. */
  45. readOnly: false,
  46. /**
  47. * Whether or not the attribute can only be written once.
  48. * @property writeOnce
  49. * @type Boolean
  50. */
  51. writeOnce: false,
  52. /**
  53. * The attribute's initial configuration.
  54. * @private
  55. * @property _initialConfig
  56. * @type Object
  57. */
  58. _initialConfig: null,
  59. /**
  60. * Whether or not the attribute's value has been set.
  61. * @private
  62. * @property _written
  63. * @type Boolean
  64. */
  65. _written: false,
  66. /**
  67. * A function to call when setting the attribute's value.
  68. * The method receives the new value as the first arg and the attribute name as the 2nd
  69. * @property method
  70. * @type Function
  71. */
  72. method: null,
  73. /**
  74. * The function to use when setting the attribute's value.
  75. * The setter receives the new value as the first arg and the attribute name as the 2nd
  76. * The return value of the setter replaces the value passed to set().
  77. * @property setter
  78. * @type Function
  79. */
  80. setter: null,
  81. /**
  82. * The function to use when getting the attribute's value.
  83. * The getter receives the new value as the first arg and the attribute name as the 2nd
  84. * The return value of the getter will be used as the return from get().
  85. * @property getter
  86. * @type Function
  87. */
  88. getter: null,
  89. /**
  90. * The validator to use when setting the attribute's value.
  91. * @property validator
  92. * @type Function
  93. * @return Boolean
  94. */
  95. validator: null,
  96. /**
  97. * Retrieves the current value of the attribute.
  98. * @method getValue
  99. * @return {any} The current value of the attribute.
  100. */
  101. getValue: function() {
  102. var val = this.value;
  103. if (this.getter) {
  104. val = this.getter.call(this.owner, this.name);
  105. }
  106. return val;
  107. },
  108. /**
  109. * Sets the value of the attribute and fires beforeChange and change events.
  110. * @method setValue
  111. * @param {Any} value The value to apply to the attribute.
  112. * @param {Boolean} silent If true the change events will not be fired.
  113. * @return {Boolean} Whether or not the value was set.
  114. */
  115. setValue: function(value, silent) {
  116. var beforeRetVal,
  117. owner = this.owner,
  118. name = this.name;
  119. var event = {
  120. type: name,
  121. prevValue: this.getValue(),
  122. newValue: value
  123. };
  124. if (this.readOnly || ( this.writeOnce && this._written) ) {
  125. YAHOO.log( 'setValue ' + name + ', ' + value +
  126. ' failed: read only', 'error', 'Attribute');
  127. return false; // write not allowed
  128. }
  129. if (this.validator && !this.validator.call(owner, value) ) {
  130. YAHOO.log( 'setValue ' + name + ', ' + value +
  131. ' validation failed', 'error', 'Attribute');
  132. return false; // invalid value
  133. }
  134. if (!silent) {
  135. beforeRetVal = owner.fireBeforeChangeEvent(event);
  136. if (beforeRetVal === false) {
  137. YAHOO.log('setValue ' + name +
  138. ' cancelled by beforeChange event', 'info', 'Attribute');
  139. return false;
  140. }
  141. }
  142. if (this.setter) {
  143. value = this.setter.call(owner, value, this.name);
  144. if (value === undefined) {
  145. YAHOO.log('setter for ' + this.name + ' returned undefined', 'warn', 'Attribute');
  146. }
  147. }
  148. if (this.method) {
  149. this.method.call(owner, value, this.name);
  150. }
  151. this.value = value; // TODO: set before calling setter/method?
  152. this._written = true;
  153. event.type = name;
  154. if (!silent) {
  155. this.owner.fireChangeEvent(event);
  156. }
  157. return true;
  158. },
  159. /**
  160. * Allows for configuring the Attribute's properties.
  161. * @method configure
  162. * @param {Object} map A key-value map of Attribute properties.
  163. * @param {Boolean} init Whether or not this should become the initial config.
  164. */
  165. configure: function(map, init) {
  166. map = map || {};
  167. if (init) {
  168. this._written = false; // reset writeOnce
  169. }
  170. this._initialConfig = this._initialConfig || {};
  171. for (var key in map) {
  172. if ( map.hasOwnProperty(key) ) {
  173. this[key] = map[key];
  174. if (init) {
  175. this._initialConfig[key] = map[key];
  176. }
  177. }
  178. }
  179. },
  180. /**
  181. * Resets the value to the initial config value.
  182. * @method resetValue
  183. * @return {Boolean} Whether or not the value was set.
  184. */
  185. resetValue: function() {
  186. return this.setValue(this._initialConfig.value);
  187. },
  188. /**
  189. * Resets the attribute config to the initial config state.
  190. * @method resetConfig
  191. */
  192. resetConfig: function() {
  193. this.configure(this._initialConfig, true);
  194. },
  195. /**
  196. * Resets the value to the current value.
  197. * Useful when values may have gotten out of sync with actual properties.
  198. * @method refresh
  199. * @return {Boolean} Whether or not the value was set.
  200. */
  201. refresh: function(silent) {
  202. this.setValue(this.value, silent);
  203. }
  204. };
  205. (function() {
  206. var Lang = YAHOO.util.Lang;
  207. /*
  208. Copyright (c) 2006, Yahoo! Inc. All rights reserved.
  209. Code licensed under the BSD License:
  210. http://developer.yahoo.net/yui/license.txt
  211. */
  212. /**
  213. * Provides and manages YAHOO.util.Attribute instances
  214. * @namespace YAHOO.util
  215. * @class AttributeProvider
  216. * @uses YAHOO.util.EventProvider
  217. */
  218. YAHOO.util.AttributeProvider = function() {};
  219. YAHOO.util.AttributeProvider.prototype = {
  220. /**
  221. * A key-value map of Attribute configurations
  222. * @property _configs
  223. * @protected (may be used by subclasses and augmentors)
  224. * @private
  225. * @type {Object}
  226. */
  227. _configs: null,
  228. /**
  229. * Returns the current value of the attribute.
  230. * @method get
  231. * @param {String} key The attribute whose value will be returned.
  232. * @return {Any} The current value of the attribute.
  233. */
  234. get: function(key){
  235. this._configs = this._configs || {};
  236. var config = this._configs[key];
  237. if (!config || !this._configs.hasOwnProperty(key)) {
  238. YAHOO.log(key + ' not found', 'error', 'AttributeProvider');
  239. return null;
  240. }
  241. return config.getValue();
  242. },
  243. /**
  244. * Sets the value of a config.
  245. * @method set
  246. * @param {String} key The name of the attribute
  247. * @param {Any} value The value to apply to the attribute
  248. * @param {Boolean} silent Whether or not to suppress change events
  249. * @return {Boolean} Whether or not the value was set.
  250. */
  251. set: function(key, value, silent){
  252. this._configs = this._configs || {};
  253. var config = this._configs[key];
  254. if (!config) {
  255. YAHOO.log('set failed: ' + key + ' not found',
  256. 'error', 'AttributeProvider');
  257. return false;
  258. }
  259. return config.setValue(value, silent);
  260. },
  261. /**
  262. * Returns an array of attribute names.
  263. * @method getAttributeKeys
  264. * @return {Array} An array of attribute names.
  265. */
  266. getAttributeKeys: function(){
  267. this._configs = this._configs;
  268. var keys = [], key;
  269. for (key in this._configs) {
  270. if ( Lang.hasOwnProperty(this._configs, key) &&
  271. !Lang.isUndefined(this._configs[key]) ) {
  272. keys[keys.length] = key;
  273. }
  274. }
  275. return keys;
  276. },
  277. /**
  278. * Sets multiple attribute values.
  279. * @method setAttributes
  280. * @param {Object} map A key-value map of attributes
  281. * @param {Boolean} silent Whether or not to suppress change events
  282. */
  283. setAttributes: function(map, silent){
  284. for (var key in map) {
  285. if ( Lang.hasOwnProperty(map, key) ) {
  286. this.set(key, map[key], silent);
  287. }
  288. }
  289. },
  290. /**
  291. * Resets the specified attribute's value to its initial value.
  292. * @method resetValue
  293. * @param {String} key The name of the attribute
  294. * @param {Boolean} silent Whether or not to suppress change events
  295. * @return {Boolean} Whether or not the value was set
  296. */
  297. resetValue: function(key, silent){
  298. this._configs = this._configs || {};
  299. if (this._configs[key]) {
  300. this.set(key, this._configs[key]._initialConfig.value, silent);
  301. return true;
  302. }
  303. return false;
  304. },
  305. /**
  306. * Sets the attribute's value to its current value.
  307. * @method refresh
  308. * @param {String | Array} key The attribute(s) to refresh
  309. * @param {Boolean} silent Whether or not to suppress change events
  310. */
  311. refresh: function(key, silent) {
  312. this._configs = this._configs || {};
  313. var configs = this._configs;
  314. key = ( ( Lang.isString(key) ) ? [key] : key ) ||
  315. this.getAttributeKeys();
  316. for (var i = 0, len = key.length; i < len; ++i) {
  317. if (configs.hasOwnProperty(key[i])) {
  318. this._configs[key[i]].refresh(silent);
  319. }
  320. }
  321. },
  322. /**
  323. * Adds an Attribute to the AttributeProvider instance.
  324. * @method register
  325. * @param {String} key The attribute's name
  326. * @param {Object} map A key-value map containing the
  327. * attribute's properties.
  328. * @deprecated Use setAttributeConfig
  329. */
  330. register: function(key, map) {
  331. this.setAttributeConfig(key, map);
  332. },
  333. /**
  334. * Returns the attribute's properties.
  335. * @method getAttributeConfig
  336. * @param {String} key The attribute's name
  337. * @private
  338. * @return {object} A key-value map containing all of the
  339. * attribute's properties.
  340. */
  341. getAttributeConfig: function(key) {
  342. this._configs = this._configs || {};
  343. var config = this._configs[key] || {};
  344. var map = {}; // returning a copy to prevent overrides
  345. for (key in config) {
  346. if ( Lang.hasOwnProperty(config, key) ) {
  347. map[key] = config[key];
  348. }
  349. }
  350. return map;
  351. },
  352. /**
  353. * Sets or updates an Attribute instance's properties.
  354. * @method setAttributeConfig
  355. * @param {String} key The attribute's name.
  356. * @param {Object} map A key-value map of attribute properties
  357. * @param {Boolean} init Whether or not this should become the intial config.
  358. */
  359. setAttributeConfig: function(key, map, init) {
  360. this._configs = this._configs || {};
  361. map = map || {};
  362. if (!this._configs[key]) {
  363. map.name = key;
  364. this._configs[key] = this.createAttribute(map);
  365. } else {
  366. this._configs[key].configure(map, init);
  367. }
  368. },
  369. /**
  370. * Sets or updates an Attribute instance's properties.
  371. * @method configureAttribute
  372. * @param {String} key The attribute's name.
  373. * @param {Object} map A key-value map of attribute properties
  374. * @param {Boolean} init Whether or not this should become the intial config.
  375. * @deprecated Use setAttributeConfig
  376. */
  377. configureAttribute: function(key, map, init) {
  378. this.setAttributeConfig(key, map, init);
  379. },
  380. /**
  381. * Resets an attribute to its intial configuration.
  382. * @method resetAttributeConfig
  383. * @param {String} key The attribute's name.
  384. * @private
  385. */
  386. resetAttributeConfig: function(key){
  387. this._configs = this._configs || {};
  388. this._configs[key].resetConfig();
  389. },
  390. // wrapper for EventProvider.subscribe
  391. // to create events on the fly
  392. subscribe: function(type, callback) {
  393. this._events = this._events || {};
  394. if ( !(type in this._events) ) {
  395. this._events[type] = this.createEvent(type);
  396. }
  397. YAHOO.util.EventProvider.prototype.subscribe.apply(this, arguments);
  398. },
  399. on: function() {
  400. this.subscribe.apply(this, arguments);
  401. },
  402. addListener: function() {
  403. this.subscribe.apply(this, arguments);
  404. },
  405. /**
  406. * Fires the attribute's beforeChange event.
  407. * @method fireBeforeChangeEvent
  408. * @param {String} key The attribute's name.
  409. * @param {Obj} e The event object to pass to handlers.
  410. */
  411. fireBeforeChangeEvent: function(e) {
  412. var type = 'before';
  413. type += e.type.charAt(0).toUpperCase() + e.type.substr(1) + 'Change';
  414. e.type = type;
  415. return this.fireEvent(e.type, e);
  416. },
  417. /**
  418. * Fires the attribute's change event.
  419. * @method fireChangeEvent
  420. * @param {String} key The attribute's name.
  421. * @param {Obj} e The event object to pass to the handlers.
  422. */
  423. fireChangeEvent: function(e) {
  424. e.type += 'Change';
  425. return this.fireEvent(e.type, e);
  426. },
  427. createAttribute: function(map) {
  428. return new YAHOO.util.Attribute(map, this);
  429. }
  430. };
  431. YAHOO.augment(YAHOO.util.AttributeProvider, YAHOO.util.EventProvider);
  432. })();
  433. (function() {
  434. // internal shorthand
  435. var Dom = YAHOO.util.Dom,
  436. AttributeProvider = YAHOO.util.AttributeProvider;
  437. /**
  438. * Element provides an wrapper object to simplify adding
  439. * event listeners, using dom methods, and managing attributes.
  440. * @module element
  441. * @namespace YAHOO.util
  442. * @requires yahoo, dom, event
  443. */
  444. /**
  445. * Element provides an wrapper object to simplify adding
  446. * event listeners, using dom methods, and managing attributes.
  447. * @class Element
  448. * @uses YAHOO.util.AttributeProvider
  449. * @constructor
  450. * @param el {HTMLElement | String} The html element that
  451. * represents the Element.
  452. * @param {Object} map A key-value map of initial config names and values
  453. */
  454. var Element = function(el, map) {
  455. this.init.apply(this, arguments);
  456. };
  457. Element.DOM_EVENTS = {
  458. 'click': true,
  459. 'dblclick': true,
  460. 'keydown': true,
  461. 'keypress': true,
  462. 'keyup': true,
  463. 'mousedown': true,
  464. 'mousemove': true,
  465. 'mouseout': true,
  466. 'mouseover': true,
  467. 'mouseup': true,
  468. 'focus': true,
  469. 'blur': true,
  470. 'submit': true,
  471. 'change': true
  472. };
  473. Element.prototype = {
  474. /**
  475. * Dom events supported by the Element instance.
  476. * @property DOM_EVENTS
  477. * @type Object
  478. */
  479. DOM_EVENTS: null,
  480. DEFAULT_HTML_SETTER: function(value, key) {
  481. var el = this.get('element');
  482. if (el) {
  483. el[key] = value;
  484. }
  485. },
  486. DEFAULT_HTML_GETTER: function(key) {
  487. var el = this.get('element'),
  488. val;
  489. if (el) {
  490. val = el[key];
  491. }
  492. return val;
  493. },
  494. /**
  495. * Wrapper for HTMLElement method.
  496. * @method appendChild
  497. * @param {YAHOO.util.Element || HTMLElement} child The element to append.
  498. * @return {HTMLElement} The appended DOM element.
  499. */
  500. appendChild: function(child) {
  501. child = child.get ? child.get('element') : child;
  502. return this.get('element').appendChild(child);
  503. },
  504. /**
  505. * Wrapper for HTMLElement method.
  506. * @method getElementsByTagName
  507. * @param {String} tag The tagName to collect
  508. * @return {HTMLCollection} A collection of DOM elements.
  509. */
  510. getElementsByTagName: function(tag) {
  511. return this.get('element').getElementsByTagName(tag);
  512. },
  513. /**
  514. * Wrapper for HTMLElement method.
  515. * @method hasChildNodes
  516. * @return {Boolean} Whether or not the element has childNodes
  517. */
  518. hasChildNodes: function() {
  519. return this.get('element').hasChildNodes();
  520. },
  521. /**
  522. * Wrapper for HTMLElement method.
  523. * @method insertBefore
  524. * @param {HTMLElement} element The HTMLElement to insert
  525. * @param {HTMLElement} before The HTMLElement to insert
  526. * the element before.
  527. * @return {HTMLElement} The inserted DOM element.
  528. */
  529. insertBefore: function(element, before) {
  530. element = element.get ? element.get('element') : element;
  531. before = (before && before.get) ? before.get('element') : before;
  532. return this.get('element').insertBefore(element, before);
  533. },
  534. /**
  535. * Wrapper for HTMLElement method.
  536. * @method removeChild
  537. * @param {HTMLElement} child The HTMLElement to remove
  538. * @return {HTMLElement} The removed DOM element.
  539. */
  540. removeChild: function(child) {
  541. child = child.get ? child.get('element') : child;
  542. return this.get('element').removeChild(child);
  543. },
  544. /**
  545. * Wrapper for HTMLElement method.
  546. * @method replaceChild
  547. * @param {HTMLElement} newNode The HTMLElement to insert
  548. * @param {HTMLElement} oldNode The HTMLElement to replace
  549. * @return {HTMLElement} The replaced DOM element.
  550. */
  551. replaceChild: function(newNode, oldNode) {
  552. newNode = newNode.get ? newNode.get('element') : newNode;
  553. oldNode = oldNode.get ? oldNode.get('element') : oldNode;
  554. return this.get('element').replaceChild(newNode, oldNode);
  555. },
  556. /**
  557. * Registers Element specific attributes.
  558. * @method initAttributes
  559. * @param {Object} map A key-value map of initial attribute configs
  560. */
  561. initAttributes: function(map) {
  562. },
  563. /**
  564. * Adds a listener for the given event. These may be DOM or
  565. * customEvent listeners. Any event that is fired via fireEvent
  566. * can be listened for. All handlers receive an event object.
  567. * @method addListener
  568. * @param {String} type The name of the event to listen for
  569. * @param {Function} fn The handler to call when the event fires
  570. * @param {Any} obj A variable to pass to the handler
  571. * @param {Object} scope The object to use for the scope of the handler
  572. */
  573. addListener: function(type, fn, obj, scope) {
  574. var el = this.get('element') || this.get('id');
  575. scope = scope || this;
  576. var self = this;
  577. if (!this._events[type]) { // create on the fly
  578. if (el && this.DOM_EVENTS[type]) {
  579. YAHOO.util.Event.addListener(el, type, function(e) {
  580. if (e.srcElement && !e.target) { // supplement IE with target
  581. e.target = e.srcElement;
  582. }
  583. self.fireEvent(type, e);
  584. }, obj, scope);
  585. }
  586. this.createEvent(type, this);
  587. }
  588. return YAHOO.util.EventProvider.prototype.subscribe.apply(this, arguments); // notify via customEvent
  589. },
  590. /**
  591. * Alias for addListener
  592. * @method on
  593. * @param {String} type The name of the event to listen for
  594. * @param {Function} fn The function call when the event fires
  595. * @param {Any} obj A variable to pass to the handler
  596. * @param {Object} scope The object to use for the scope of the handler
  597. */
  598. on: function() {
  599. return this.addListener.apply(this, arguments);
  600. },
  601. /**
  602. * Alias for addListener
  603. * @method subscribe
  604. * @param {String} type The name of the event to listen for
  605. * @param {Function} fn The function call when the event fires
  606. * @param {Any} obj A variable to pass to the handler
  607. * @param {Object} scope The object to use for the scope of the handler
  608. */
  609. subscribe: function() {
  610. return this.addListener.apply(this, arguments);
  611. },
  612. /**
  613. * Remove an event listener
  614. * @method removeListener
  615. * @param {String} type The name of the event to listen for
  616. * @param {Function} fn The function call when the event fires
  617. */
  618. removeListener: function(type, fn) {
  619. return this.unsubscribe.apply(this, arguments);
  620. },
  621. /**
  622. * Wrapper for Dom method.
  623. * @method addClass
  624. * @param {String} className The className to add
  625. */
  626. addClass: function(className) {
  627. Dom.addClass(this.get('element'), className);
  628. },
  629. /**
  630. * Wrapper for Dom method.
  631. * @method getElementsByClassName
  632. * @param {String} className The className to collect
  633. * @param {String} tag (optional) The tag to use in
  634. * conjunction with class name
  635. * @return {Array} Array of HTMLElements
  636. */
  637. getElementsByClassName: function(className, tag) {
  638. return Dom.getElementsByClassName(className, tag,
  639. this.get('element') );
  640. },
  641. /**
  642. * Wrapper for Dom method.
  643. * @method hasClass
  644. * @param {String} className The className to add
  645. * @return {Boolean} Whether or not the element has the class name
  646. */
  647. hasClass: function(className) {
  648. return Dom.hasClass(this.get('element'), className);
  649. },
  650. /**
  651. * Wrapper for Dom method.
  652. * @method removeClass
  653. * @param {String} className The className to remove
  654. */
  655. removeClass: function(className) {
  656. return Dom.removeClass(this.get('element'), className);
  657. },
  658. /**
  659. * Wrapper for Dom method.
  660. * @method replaceClass
  661. * @param {String} oldClassName The className to replace
  662. * @param {String} newClassName The className to add
  663. */
  664. replaceClass: function(oldClassName, newClassName) {
  665. return Dom.replaceClass(this.get('element'),
  666. oldClassName, newClassName);
  667. },
  668. /**
  669. * Wrapper for Dom method.
  670. * @method setStyle
  671. * @param {String} property The style property to set
  672. * @param {String} value The value to apply to the style property
  673. */
  674. setStyle: function(property, value) {
  675. return Dom.setStyle(this.get('element'), property, value); // TODO: always queuing?
  676. },
  677. /**
  678. * Wrapper for Dom method.
  679. * @method getStyle
  680. * @param {String} property The style property to retrieve
  681. * @return {String} The current value of the property
  682. */
  683. getStyle: function(property) {
  684. return Dom.getStyle(this.get('element'), property);
  685. },
  686. /**
  687. * Apply any queued set calls.
  688. * @method fireQueue
  689. */
  690. fireQueue: function() {
  691. var queue = this._queue;
  692. for (var i = 0, len = queue.length; i < len; ++i) {
  693. this[queue[i][0]].apply(this, queue[i][1]);
  694. }
  695. },
  696. /**
  697. * Appends the HTMLElement into either the supplied parentNode.
  698. * @method appendTo
  699. * @param {HTMLElement | Element} parentNode The node to append to
  700. * @param {HTMLElement | Element} before An optional node to insert before
  701. * @return {HTMLElement} The appended DOM element.
  702. */
  703. appendTo: function(parent, before) {
  704. parent = (parent.get) ? parent.get('element') : Dom.get(parent);
  705. this.fireEvent('beforeAppendTo', {
  706. type: 'beforeAppendTo',
  707. target: parent
  708. });
  709. before = (before && before.get) ?
  710. before.get('element') : Dom.get(before);
  711. var element = this.get('element');
  712. if (!element) {
  713. YAHOO.log('appendTo failed: element not available',
  714. 'error', 'Element');
  715. return false;
  716. }
  717. if (!parent) {
  718. YAHOO.log('appendTo failed: parent not available',
  719. 'error', 'Element');
  720. return false;
  721. }
  722. if (element.parent != parent) {
  723. if (before) {
  724. parent.insertBefore(element, before);
  725. } else {
  726. parent.appendChild(element);
  727. }
  728. }
  729. YAHOO.log(element + 'appended to ' + parent);
  730. this.fireEvent('appendTo', {
  731. type: 'appendTo',
  732. target: parent
  733. });
  734. return element;
  735. },
  736. get: function(key) {
  737. var configs = this._configs || {},
  738. el = configs.element; // avoid loop due to 'element'
  739. if (el && !configs[key] && !YAHOO.lang.isUndefined(el.value[key]) ) {
  740. this._setHTMLAttrConfig(key);
  741. }
  742. return AttributeProvider.prototype.get.call(this, key);
  743. },
  744. setAttributes: function(map, silent) {
  745. // set based on configOrder
  746. var done = {},
  747. configOrder = this._configOrder;
  748. // set based on configOrder
  749. for (var i = 0, len = configOrder.length; i < len; ++i) {
  750. if (map[configOrder[i]] !== undefined) {
  751. done[configOrder[i]] = true;
  752. this.set(configOrder[i], map[configOrder[i]], silent);
  753. }
  754. }
  755. // unconfigured (e.g. Dom attributes)
  756. for (var att in map) {
  757. if (map.hasOwnProperty(att) && !done[att]) {
  758. this.set(att, map[att], silent);
  759. }
  760. }
  761. },
  762. set: function(key, value, silent) {
  763. var el = this.get('element');
  764. if (!el) {
  765. this._queue[this._queue.length] = ['set', arguments];
  766. if (this._configs[key]) {
  767. this._configs[key].value = value; // so "get" works while queueing
  768. }
  769. return;
  770. }
  771. // set it on the element if not configured and is an HTML attribute
  772. if ( !this._configs[key] && !YAHOO.lang.isUndefined(el[key]) ) {
  773. this._setHTMLAttrConfig(key);
  774. }
  775. return AttributeProvider.prototype.set.apply(this, arguments);
  776. },
  777. setAttributeConfig: function(key, map, init) {
  778. this._configOrder.push(key);
  779. AttributeProvider.prototype.setAttributeConfig.apply(this, arguments);
  780. },
  781. createEvent: function(type, scope) {
  782. this._events[type] = true;
  783. return AttributeProvider.prototype.createEvent.apply(this, arguments);
  784. },
  785. init: function(el, attr) {
  786. this._initElement(el, attr);
  787. },
  788. destroy: function() {
  789. var el = this.get('element');
  790. YAHOO.util.Event.purgeElement(el, true); // purge DOM listeners recursively
  791. this.unsubscribeAll(); // unsubscribe all custom events
  792. if (el && el.parentNode) {
  793. el.parentNode.removeChild(el); // pull from the DOM
  794. }
  795. // revert initial configs
  796. this._queue = [];
  797. this._events = {};
  798. this._configs = {};
  799. this._configOrder = [];
  800. },
  801. _initElement: function(el, attr) {
  802. this._queue = this._queue || [];
  803. this._events = this._events || {};
  804. this._configs = this._configs || {};
  805. this._configOrder = [];
  806. attr = attr || {};
  807. attr.element = attr.element || el || null;
  808. var isReady = false; // to determine when to init HTMLElement and content
  809. var DOM_EVENTS = Element.DOM_EVENTS;
  810. this.DOM_EVENTS = this.DOM_EVENTS || {};
  811. for (var event in DOM_EVENTS) {
  812. if (DOM_EVENTS.hasOwnProperty(event)) {
  813. this.DOM_EVENTS[event] = DOM_EVENTS[event];
  814. }
  815. }
  816. if (typeof attr.element === 'string') { // register ID for get() access
  817. this._setHTMLAttrConfig('id', { value: attr.element });
  818. }
  819. if (Dom.get(attr.element)) {
  820. isReady = true;
  821. this._initHTMLElement(attr);
  822. this._initContent(attr);
  823. }
  824. YAHOO.util.Event.onAvailable(attr.element, function() {
  825. if (!isReady) { // otherwise already done
  826. this._initHTMLElement(attr);
  827. }
  828. this.fireEvent('available', { type: 'available', target: Dom.get(attr.element) });
  829. }, this, true);
  830. YAHOO.util.Event.onContentReady(attr.element, function() {
  831. if (!isReady) { // otherwise already done
  832. this._initContent(attr);
  833. }
  834. this.fireEvent('contentReady', { type: 'contentReady', target: Dom.get(attr.element) });
  835. }, this, true);
  836. },
  837. _initHTMLElement: function(attr) {
  838. /**
  839. * The HTMLElement the Element instance refers to.
  840. * @attribute element
  841. * @type HTMLElement
  842. */
  843. this.setAttributeConfig('element', {
  844. value: Dom.get(attr.element),
  845. readOnly: true
  846. });
  847. },
  848. _initContent: function(attr) {
  849. this.initAttributes(attr);
  850. this.setAttributes(attr, true);
  851. this.fireQueue();
  852. },
  853. /**
  854. * Sets the value of the property and fires beforeChange and change events.
  855. * @private
  856. * @method _setHTMLAttrConfig
  857. * @param {YAHOO.util.Element} element The Element instance to
  858. * register the config to.
  859. * @param {String} key The name of the config to register
  860. * @param {Object} map A key-value map of the config's params
  861. */
  862. _setHTMLAttrConfig: function(key, map) {
  863. var el = this.get('element');
  864. map = map || {};
  865. map.name = key;
  866. map.setter = map.setter || this.DEFAULT_HTML_SETTER;
  867. map.getter = map.getter || this.DEFAULT_HTML_GETTER;
  868. map.value = map.value || el[key];
  869. this._configs[key] = new YAHOO.util.Attribute(map, this);
  870. }
  871. };
  872. /**
  873. * Fires when the Element's HTMLElement can be retrieved by Id.
  874. * <p>See: <a href="#addListener">Element.addListener</a></p>
  875. * <p><strong>Event fields:</strong><br>
  876. * <code>&lt;String&gt; type</code> available<br>
  877. * <code>&lt;HTMLElement&gt;
  878. * target</code> the HTMLElement bound to this Element instance<br>
  879. * <p><strong>Usage:</strong><br>
  880. * <code>var handler = function(e) {var target = e.target};<br>
  881. * myTabs.addListener('available', handler);</code></p>
  882. * @event available
  883. */
  884. /**
  885. * Fires when the Element's HTMLElement subtree is rendered.
  886. * <p>See: <a href="#addListener">Element.addListener</a></p>
  887. * <p><strong>Event fields:</strong><br>
  888. * <code>&lt;String&gt; type</code> contentReady<br>
  889. * <code>&lt;HTMLElement&gt;
  890. * target</code> the HTMLElement bound to this Element instance<br>
  891. * <p><strong>Usage:</strong><br>
  892. * <code>var handler = function(e) {var target = e.target};<br>
  893. * myTabs.addListener('contentReady', handler);</code></p>
  894. * @event contentReady
  895. */
  896. /**
  897. * Fires before the Element is appended to another Element.
  898. * <p>See: <a href="#addListener">Element.addListener</a></p>
  899. * <p><strong>Event fields:</strong><br>
  900. * <code>&lt;String&gt; type</code> beforeAppendTo<br>
  901. * <code>&lt;HTMLElement/Element&gt;
  902. * target</code> the HTMLElement/Element being appended to
  903. * <p><strong>Usage:</strong><br>
  904. * <code>var handler = function(e) {var target = e.target};<br>
  905. * myTabs.addListener('beforeAppendTo', handler);</code></p>
  906. * @event beforeAppendTo
  907. */
  908. /**
  909. * Fires after the Element is appended to another Element.
  910. * <p>See: <a href="#addListener">Element.addListener</a></p>
  911. * <p><strong>Event fields:</strong><br>
  912. * <code>&lt;String&gt; type</code> appendTo<br>
  913. * <code>&lt;HTMLElement/Element&gt;
  914. * target</code> the HTMLElement/Element being appended to
  915. * <p><strong>Usage:</strong><br>
  916. * <code>var handler = function(e) {var target = e.target};<br>
  917. * myTabs.addListener('appendTo', handler);</code></p>
  918. * @event appendTo
  919. */
  920. YAHOO.augment(Element, AttributeProvider);
  921. YAHOO.util.Element = Element;
  922. })();
  923. YAHOO.register("element", YAHOO.util.Element, {version: "2.7.0", build: "1799"});