Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / core / src / dom / Element.js
1 /**
2  * @class Ext.core.Element
3  * <p>Encapsulates a DOM element, adding simple DOM manipulation facilities, normalizing for browser differences.</p>
4  * <p>All instances of this class inherit the methods of {@link Ext.fx.Anim} making visual effects easily available to all DOM elements.</p>
5  * <p>Note that the events documented in this class are not Ext events, they encapsulate browser events. To
6  * access the underlying browser event, see {@link Ext.EventObject#browserEvent}. Some older
7  * browsers may not support the full range of events. Which events are supported is beyond the control of ExtJs.</p>
8  * Usage:<br>
9 <pre><code>
10 // by id
11 var el = Ext.get("my-div");
12
13 // by DOM element reference
14 var el = Ext.get(myDivElement);
15 </code></pre>
16  * <b>Animations</b><br />
17  * <p>When an element is manipulated, by default there is no animation.</p>
18  * <pre><code>
19 var el = Ext.get("my-div");
20
21 // no animation
22 el.setWidth(100);
23  * </code></pre>
24  * <p>Many of the functions for manipulating an element have an optional "animate" parameter.  This
25  * parameter can be specified as boolean (<tt>true</tt>) for default animation effects.</p>
26  * <pre><code>
27 // default animation
28 el.setWidth(100, true);
29  * </code></pre>
30  *
31  * <p>To configure the effects, an object literal with animation options to use as the Element animation
32  * configuration object can also be specified. Note that the supported Element animation configuration
33  * options are a subset of the {@link Ext.fx.Anim} animation options specific to Fx effects.  The supported
34  * Element animation configuration options are:</p>
35 <pre>
36 Option    Default   Description
37 --------- --------  ---------------------------------------------
38 {@link Ext.fx.Anim#duration duration}  .35       The duration of the animation in seconds
39 {@link Ext.fx.Anim#easing easing}    easeOut   The easing method
40 {@link Ext.fx.Anim#callback callback}  none      A function to execute when the anim completes
41 {@link Ext.fx.Anim#scope scope}     this      The scope (this) of the callback function
42 </pre>
43  *
44  * <pre><code>
45 // Element animation options object
46 var opt = {
47     {@link Ext.fx.Anim#duration duration}: 1,
48     {@link Ext.fx.Anim#easing easing}: 'elasticIn',
49     {@link Ext.fx.Anim#callback callback}: this.foo,
50     {@link Ext.fx.Anim#scope scope}: this
51 };
52 // animation with some options set
53 el.setWidth(100, opt);
54  * </code></pre>
55  * <p>The Element animation object being used for the animation will be set on the options
56  * object as "anim", which allows you to stop or manipulate the animation. Here is an example:</p>
57  * <pre><code>
58 // using the "anim" property to get the Anim object
59 if(opt.anim.isAnimated()){
60     opt.anim.stop();
61 }
62  * </code></pre>
63  * <p>Also see the <tt>{@link #animate}</tt> method for another animation technique.</p>
64  * <p><b> Composite (Collections of) Elements</b></p>
65  * <p>For working with collections of Elements, see {@link Ext.CompositeElement}</p>
66  * @constructor Create a new Element directly.
67  * @param {String/HTMLElement} element
68  * @param {Boolean} forceNew (optional) By default the constructor checks to see if there is already an instance of this element in the cache and if there is it returns the same instance. This will skip that check (useful for extending this class).
69  */
70  (function() {
71     var DOC = document,
72         EC = Ext.cache;
73
74     Ext.Element = Ext.core.Element = function(element, forceNew) {
75         var dom = typeof element == "string" ? DOC.getElementById(element) : element,
76         id;
77
78         if (!dom) {
79             return null;
80         }
81
82         id = dom.id;
83
84         if (!forceNew && id && EC[id]) {
85             // element object already exists
86             return EC[id].el;
87         }
88
89         /**
90      * The DOM element
91      * @type HTMLElement
92      */
93         this.dom = dom;
94
95         /**
96      * The DOM element ID
97      * @type String
98      */
99         this.id = id || Ext.id(dom);
100     };
101
102     var DH = Ext.core.DomHelper,
103     El = Ext.core.Element;
104
105
106     El.prototype = {
107         /**
108      * Sets the passed attributes as attributes of this element (a style attribute can be a string, object or function)
109      * @param {Object} o The object with the attributes
110      * @param {Boolean} useSet (optional) false to override the default setAttribute to use expandos.
111      * @return {Ext.core.Element} this
112      */
113         set: function(o, useSet) {
114             var el = this.dom,
115                 attr,
116                 val;
117             useSet = (useSet !== false) && !!el.setAttribute;
118
119             for (attr in o) {
120                 if (o.hasOwnProperty(attr)) {
121                     val = o[attr];
122                     if (attr == 'style') {
123                         DH.applyStyles(el, val);
124                     } else if (attr == 'cls') {
125                         el.className = val;
126                     } else if (useSet) {
127                         el.setAttribute(attr, val);
128                     } else {
129                         el[attr] = val;
130                     }
131                 }
132             }
133             return this;
134         },
135
136         //  Mouse events
137         /**
138      * @event click
139      * Fires when a mouse click is detected within the element.
140      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
141      * @param {HtmlElement} t The target of the event.
142      * @param {Object} o The options configuration passed to the {@link #addListener} call.
143      */
144         /**
145      * @event contextmenu
146      * Fires when a right click is detected within the element.
147      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
148      * @param {HtmlElement} t The target of the event.
149      * @param {Object} o The options configuration passed to the {@link #addListener} call.
150      */
151         /**
152      * @event dblclick
153      * Fires when a mouse double click is detected within the element.
154      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
155      * @param {HtmlElement} t The target of the event.
156      * @param {Object} o The options configuration passed to the {@link #addListener} call.
157      */
158         /**
159      * @event mousedown
160      * Fires when a mousedown is detected within the element.
161      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
162      * @param {HtmlElement} t The target of the event.
163      * @param {Object} o The options configuration passed to the {@link #addListener} call.
164      */
165         /**
166      * @event mouseup
167      * Fires when a mouseup is detected within the element.
168      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
169      * @param {HtmlElement} t The target of the event.
170      * @param {Object} o The options configuration passed to the {@link #addListener} call.
171      */
172         /**
173      * @event mouseover
174      * Fires when a mouseover is detected within the element.
175      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
176      * @param {HtmlElement} t The target of the event.
177      * @param {Object} o The options configuration passed to the {@link #addListener} call.
178      */
179         /**
180      * @event mousemove
181      * Fires when a mousemove is detected with the element.
182      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
183      * @param {HtmlElement} t The target of the event.
184      * @param {Object} o The options configuration passed to the {@link #addListener} call.
185      */
186         /**
187      * @event mouseout
188      * Fires when a mouseout is detected with the element.
189      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
190      * @param {HtmlElement} t The target of the event.
191      * @param {Object} o The options configuration passed to the {@link #addListener} call.
192      */
193         /**
194      * @event mouseenter
195      * Fires when the mouse enters the element.
196      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
197      * @param {HtmlElement} t The target of the event.
198      * @param {Object} o The options configuration passed to the {@link #addListener} call.
199      */
200         /**
201      * @event mouseleave
202      * Fires when the mouse leaves the element.
203      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
204      * @param {HtmlElement} t The target of the event.
205      * @param {Object} o The options configuration passed to the {@link #addListener} call.
206      */
207
208         //  Keyboard events
209         /**
210      * @event keypress
211      * Fires when a keypress is detected within the element.
212      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
213      * @param {HtmlElement} t The target of the event.
214      * @param {Object} o The options configuration passed to the {@link #addListener} call.
215      */
216         /**
217      * @event keydown
218      * Fires when a keydown is detected within the element.
219      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
220      * @param {HtmlElement} t The target of the event.
221      * @param {Object} o The options configuration passed to the {@link #addListener} call.
222      */
223         /**
224      * @event keyup
225      * Fires when a keyup is detected within the element.
226      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
227      * @param {HtmlElement} t The target of the event.
228      * @param {Object} o The options configuration passed to the {@link #addListener} call.
229      */
230
231
232         //  HTML frame/object events
233         /**
234      * @event load
235      * Fires when the user agent finishes loading all content within the element. Only supported by window, frames, objects and images.
236      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
237      * @param {HtmlElement} t The target of the event.
238      * @param {Object} o The options configuration passed to the {@link #addListener} call.
239      */
240         /**
241      * @event unload
242      * Fires when the user agent removes all content from a window or frame. For elements, it fires when the target element or any of its content has been removed.
243      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
244      * @param {HtmlElement} t The target of the event.
245      * @param {Object} o The options configuration passed to the {@link #addListener} call.
246      */
247         /**
248      * @event abort
249      * Fires when an object/image is stopped from loading before completely loaded.
250      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
251      * @param {HtmlElement} t The target of the event.
252      * @param {Object} o The options configuration passed to the {@link #addListener} call.
253      */
254         /**
255      * @event error
256      * Fires when an object/image/frame cannot be loaded properly.
257      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
258      * @param {HtmlElement} t The target of the event.
259      * @param {Object} o The options configuration passed to the {@link #addListener} call.
260      */
261         /**
262      * @event resize
263      * Fires when a document view is resized.
264      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
265      * @param {HtmlElement} t The target of the event.
266      * @param {Object} o The options configuration passed to the {@link #addListener} call.
267      */
268         /**
269      * @event scroll
270      * Fires when a document view is scrolled.
271      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
272      * @param {HtmlElement} t The target of the event.
273      * @param {Object} o The options configuration passed to the {@link #addListener} call.
274      */
275
276         //  Form events
277         /**
278      * @event select
279      * Fires when a user selects some text in a text field, including input and textarea.
280      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
281      * @param {HtmlElement} t The target of the event.
282      * @param {Object} o The options configuration passed to the {@link #addListener} call.
283      */
284         /**
285      * @event change
286      * Fires when a control loses the input focus and its value has been modified since gaining focus.
287      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
288      * @param {HtmlElement} t The target of the event.
289      * @param {Object} o The options configuration passed to the {@link #addListener} call.
290      */
291         /**
292      * @event submit
293      * Fires when a form is submitted.
294      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
295      * @param {HtmlElement} t The target of the event.
296      * @param {Object} o The options configuration passed to the {@link #addListener} call.
297      */
298         /**
299      * @event reset
300      * Fires when a form is reset.
301      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
302      * @param {HtmlElement} t The target of the event.
303      * @param {Object} o The options configuration passed to the {@link #addListener} call.
304      */
305         /**
306      * @event focus
307      * Fires when an element receives focus either via the pointing device or by tab navigation.
308      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
309      * @param {HtmlElement} t The target of the event.
310      * @param {Object} o The options configuration passed to the {@link #addListener} call.
311      */
312         /**
313      * @event blur
314      * Fires when an element loses focus either via the pointing device or by tabbing navigation.
315      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
316      * @param {HtmlElement} t The target of the event.
317      * @param {Object} o The options configuration passed to the {@link #addListener} call.
318      */
319
320         //  User Interface events
321         /**
322      * @event DOMFocusIn
323      * Where supported. Similar to HTML focus event, but can be applied to any focusable element.
324      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
325      * @param {HtmlElement} t The target of the event.
326      * @param {Object} o The options configuration passed to the {@link #addListener} call.
327      */
328         /**
329      * @event DOMFocusOut
330      * Where supported. Similar to HTML blur event, but can be applied to any focusable element.
331      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
332      * @param {HtmlElement} t The target of the event.
333      * @param {Object} o The options configuration passed to the {@link #addListener} call.
334      */
335         /**
336      * @event DOMActivate
337      * Where supported. Fires when an element is activated, for instance, through a mouse click or a keypress.
338      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
339      * @param {HtmlElement} t The target of the event.
340      * @param {Object} o The options configuration passed to the {@link #addListener} call.
341      */
342
343         //  DOM Mutation events
344         /**
345      * @event DOMSubtreeModified
346      * Where supported. Fires when the subtree is modified.
347      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
348      * @param {HtmlElement} t The target of the event.
349      * @param {Object} o The options configuration passed to the {@link #addListener} call.
350      */
351         /**
352      * @event DOMNodeInserted
353      * Where supported. Fires when a node has been added as a child of another node.
354      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
355      * @param {HtmlElement} t The target of the event.
356      * @param {Object} o The options configuration passed to the {@link #addListener} call.
357      */
358         /**
359      * @event DOMNodeRemoved
360      * Where supported. Fires when a descendant node of the element is removed.
361      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
362      * @param {HtmlElement} t The target of the event.
363      * @param {Object} o The options configuration passed to the {@link #addListener} call.
364      */
365         /**
366      * @event DOMNodeRemovedFromDocument
367      * Where supported. Fires when a node is being removed from a document.
368      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
369      * @param {HtmlElement} t The target of the event.
370      * @param {Object} o The options configuration passed to the {@link #addListener} call.
371      */
372         /**
373      * @event DOMNodeInsertedIntoDocument
374      * Where supported. Fires when a node is being inserted into a document.
375      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
376      * @param {HtmlElement} t The target of the event.
377      * @param {Object} o The options configuration passed to the {@link #addListener} call.
378      */
379         /**
380      * @event DOMAttrModified
381      * Where supported. Fires when an attribute has been modified.
382      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
383      * @param {HtmlElement} t The target of the event.
384      * @param {Object} o The options configuration passed to the {@link #addListener} call.
385      */
386         /**
387      * @event DOMCharacterDataModified
388      * Where supported. Fires when the character data has been modified.
389      * @param {Ext.EventObject} e The {@link Ext.EventObject} encapsulating the DOM event.
390      * @param {HtmlElement} t The target of the event.
391      * @param {Object} o The options configuration passed to the {@link #addListener} call.
392      */
393
394         /**
395      * The default unit to append to CSS values where a unit isn't provided (defaults to px).
396      * @type String
397      */
398         defaultUnit: "px",
399
400         /**
401      * Returns true if this element matches the passed simple selector (e.g. div.some-class or span:first-child)
402      * @param {String} selector The simple selector to test
403      * @return {Boolean} True if this element matches the selector, else false
404      */
405         is: function(simpleSelector) {
406             return Ext.DomQuery.is(this.dom, simpleSelector);
407         },
408
409         /**
410      * Tries to focus the element. Any exceptions are caught and ignored.
411      * @param {Number} defer (optional) Milliseconds to defer the focus
412      * @return {Ext.core.Element} this
413      */
414         focus: function(defer,
415                         /* private */
416                         dom) {
417             var me = this;
418             dom = dom || me.dom;
419             try {
420                 if (Number(defer)) {
421                     Ext.defer(me.focus, defer, null, [null, dom]);
422                 } else {
423                     dom.focus();
424                 }
425             } catch(e) {}
426             return me;
427         },
428
429         /**
430      * Tries to blur the element. Any exceptions are caught and ignored.
431      * @return {Ext.core.Element} this
432      */
433         blur: function() {
434             try {
435                 this.dom.blur();
436             } catch(e) {}
437             return this;
438         },
439
440         /**
441      * Returns the value of the "value" attribute
442      * @param {Boolean} asNumber true to parse the value as a number
443      * @return {String/Number}
444      */
445         getValue: function(asNumber) {
446             var val = this.dom.value;
447             return asNumber ? parseInt(val, 10) : val;
448         },
449
450         /**
451      * Appends an event handler to this element.  The shorthand version {@link #on} is equivalent.
452      * @param {String} eventName The name of event to handle.
453      * @param {Function} fn The handler function the event invokes. This function is passed
454      * the following parameters:<ul>
455      * <li><b>evt</b> : EventObject<div class="sub-desc">The {@link Ext.EventObject EventObject} describing the event.</div></li>
456      * <li><b>el</b> : HtmlElement<div class="sub-desc">The DOM element which was the target of the event.
457      * Note that this may be filtered by using the <tt>delegate</tt> option.</div></li>
458      * <li><b>o</b> : Object<div class="sub-desc">The options object from the addListener call.</div></li>
459      * </ul>
460      * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the handler function is executed.
461      * <b>If omitted, defaults to this Element.</b>.
462      * @param {Object} options (optional) An object containing handler configuration properties.
463      * This may contain any of the following properties:<ul>
464      * <li><b>scope</b> Object : <div class="sub-desc">The scope (<code><b>this</b></code> reference) in which the handler function is executed.
465      * <b>If omitted, defaults to this Element.</b></div></li>
466      * <li><b>delegate</b> String: <div class="sub-desc">A simple selector to filter the target or look for a descendant of the target. See below for additional details.</div></li>
467      * <li><b>stopEvent</b> Boolean: <div class="sub-desc">True to stop the event. That is stop propagation, and prevent the default action.</div></li>
468      * <li><b>preventDefault</b> Boolean: <div class="sub-desc">True to prevent the default action</div></li>
469      * <li><b>stopPropagation</b> Boolean: <div class="sub-desc">True to prevent event propagation</div></li>
470      * <li><b>normalized</b> Boolean: <div class="sub-desc">False to pass a browser event to the handler function instead of an Ext.EventObject</div></li>
471      * <li><b>target</b> Ext.core.Element: <div class="sub-desc">Only call the handler if the event was fired on the target Element, <i>not</i> if the event was bubbled up from a child node.</div></li>
472      * <li><b>delay</b> Number: <div class="sub-desc">The number of milliseconds to delay the invocation of the handler after the event fires.</div></li>
473      * <li><b>single</b> Boolean: <div class="sub-desc">True to add a handler to handle just the next firing of the event, and then remove itself.</div></li>
474      * <li><b>buffer</b> Number: <div class="sub-desc">Causes the handler to be scheduled to run in an {@link Ext.util.DelayedTask} delayed
475      * by the specified number of milliseconds. If the event fires again within that time, the original
476      * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</div></li>
477      * </ul><br>
478      * <p>
479      * <b>Combining Options</b><br>
480      * In the following examples, the shorthand form {@link #on} is used rather than the more verbose
481      * addListener.  The two are equivalent.  Using the options argument, it is possible to combine different
482      * types of listeners:<br>
483      * <br>
484      * A delayed, one-time listener that auto stops the event and adds a custom argument (forumId) to the
485      * options object. The options object is available as the third parameter in the handler function.<div style="margin: 5px 20px 20px;">
486      * Code:<pre><code>
487 el.on('click', this.onClick, this, {
488     single: true,
489     delay: 100,
490     stopEvent : true,
491     forumId: 4
492 });</code></pre></p>
493      * <p>
494      * <b>Attaching multiple handlers in 1 call</b><br>
495      * The method also allows for a single argument to be passed which is a config object containing properties
496      * which specify multiple handlers.</p>
497      * <p>
498      * Code:<pre><code>
499 el.on({
500     'click' : {
501         fn: this.onClick,
502         scope: this,
503         delay: 100
504     },
505     'mouseover' : {
506         fn: this.onMouseOver,
507         scope: this
508     },
509     'mouseout' : {
510         fn: this.onMouseOut,
511         scope: this
512     }
513 });</code></pre>
514      * <p>
515      * Or a shorthand syntax:<br>
516      * Code:<pre><code></p>
517 el.on({
518     'click' : this.onClick,
519     'mouseover' : this.onMouseOver,
520     'mouseout' : this.onMouseOut,
521     scope: this
522 });
523      * </code></pre></p>
524      * <p><b>delegate</b></p>
525      * <p>This is a configuration option that you can pass along when registering a handler for
526      * an event to assist with event delegation. Event delegation is a technique that is used to
527      * reduce memory consumption and prevent exposure to memory-leaks. By registering an event
528      * for a container element as opposed to each element within a container. By setting this
529      * configuration option to a simple selector, the target element will be filtered to look for
530      * a descendant of the target.
531      * For example:<pre><code>
532 // using this markup:
533 &lt;div id='elId'>
534     &lt;p id='p1'>paragraph one&lt;/p>
535     &lt;p id='p2' class='clickable'>paragraph two&lt;/p>
536     &lt;p id='p3'>paragraph three&lt;/p>
537 &lt;/div>
538 // utilize event delegation to registering just one handler on the container element:
539 el = Ext.get('elId');
540 el.on(
541     'click',
542     function(e,t) {
543         // handle click
544         console.info(t.id); // 'p2'
545     },
546     this,
547     {
548         // filter the target element to be a descendant with the class 'clickable'
549         delegate: '.clickable'
550     }
551 );
552      * </code></pre></p>
553      * @return {Ext.core.Element} this
554      */
555         addListener: function(eventName, fn, scope, options) {
556             Ext.EventManager.on(this.dom, eventName, fn, scope || this, options);
557             return this;
558         },
559
560         /**
561      * Removes an event handler from this element.  The shorthand version {@link #un} is equivalent.
562      * <b>Note</b>: if a <i>scope</i> was explicitly specified when {@link #addListener adding} the
563      * listener, the same scope must be specified here.
564      * Example:
565      * <pre><code>
566 el.removeListener('click', this.handlerFn);
567 // or
568 el.un('click', this.handlerFn);
569 </code></pre>
570      * @param {String} eventName The name of the event from which to remove the handler.
571      * @param {Function} fn The handler function to remove. <b>This must be a reference to the function passed into the {@link #addListener} call.</b>
572      * @param {Object} scope If a scope (<b><code>this</code></b> reference) was specified when the listener was added,
573      * then this must refer to the same object.
574      * @return {Ext.core.Element} this
575      */
576         removeListener: function(eventName, fn, scope) {
577             Ext.EventManager.un(this.dom, eventName, fn, scope || this);
578             return this;
579         },
580
581         /**
582      * Removes all previous added listeners from this element
583      * @return {Ext.core.Element} this
584      */
585         removeAllListeners: function() {
586             Ext.EventManager.removeAll(this.dom);
587             return this;
588         },
589
590         /**
591          * Recursively removes all previous added listeners from this element and its children
592          * @return {Ext.core.Element} this
593          */
594         purgeAllListeners: function() {
595             Ext.EventManager.purgeElement(this);
596             return this;
597         },
598
599         /**
600          * @private Test if size has a unit, otherwise appends the passed unit string, or the default for this Element.
601          * @param size {Mixed} The size to set
602          * @param units {String} The units to append to a numeric size value
603          */
604         addUnits: function(size, units) {
605
606             // Most common case first: Size is set to a number
607             if (Ext.isNumber(size)) {
608                 return size + (units || this.defaultUnit || 'px');
609             }
610
611             // Size set to a value which means "auto"
612             if (size === "" || size == "auto" || size === undefined || size === null) {
613                 return size || '';
614             }
615
616             // Otherwise, warn if it's not a valid CSS measurement
617             if (!unitPattern.test(size)) {
618                 //<debug>
619                 if (Ext.isDefined(Ext.global.console)) {
620                     Ext.global.console.warn("Warning, size detected as NaN on Element.addUnits.");
621                 }
622                 //</debug>
623                 return size || '';
624             }
625             return size;
626         },
627
628         /**
629          * Tests various css rules/browsers to determine if this element uses a border box
630          * @return {Boolean}
631          */
632         isBorderBox: function() {
633             return Ext.isBorderBox || noBoxAdjust[(this.dom.tagName || "").toLowerCase()];
634         },
635
636         /**
637          * <p>Removes this element's dom reference.  Note that event and cache removal is handled at {@link Ext#removeNode Ext.removeNode}</p>
638          */
639         remove: function() {
640             var me = this,
641             dom = me.dom;
642
643             if (dom) {
644                 delete me.dom;
645                 Ext.removeNode(dom);
646             }
647         },
648
649         /**
650          * Sets up event handlers to call the passed functions when the mouse is moved into and out of the Element.
651          * @param {Function} overFn The function to call when the mouse enters the Element.
652          * @param {Function} outFn The function to call when the mouse leaves the Element.
653          * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the functions are executed. Defaults to the Element's DOM element.
654          * @param {Object} options (optional) Options for the listener. See {@link Ext.util.Observable#addListener the <tt>options</tt> parameter}.
655          * @return {Ext.core.Element} this
656          */
657         hover: function(overFn, outFn, scope, options) {
658             var me = this;
659             me.on('mouseenter', overFn, scope || me.dom, options);
660             me.on('mouseleave', outFn, scope || me.dom, options);
661             return me;
662         },
663
664         /**
665          * Returns true if this element is an ancestor of the passed element
666          * @param {HTMLElement/String} el The element to check
667          * @return {Boolean} True if this element is an ancestor of el, else false
668          */
669         contains: function(el) {
670             return ! el ? false: Ext.core.Element.isAncestor(this.dom, el.dom ? el.dom: el);
671         },
672
673         /**
674          * Returns the value of a namespaced attribute from the element's underlying DOM node.
675          * @param {String} namespace The namespace in which to look for the attribute
676          * @param {String} name The attribute name
677          * @return {String} The attribute value
678          * @deprecated
679          */
680         getAttributeNS: function(ns, name) {
681             return this.getAttribute(name, ns);
682         },
683
684         /**
685          * Returns the value of an attribute from the element's underlying DOM node.
686          * @param {String} name The attribute name
687          * @param {String} namespace (optional) The namespace in which to look for the attribute
688          * @return {String} The attribute value
689          * @method
690          */
691         getAttribute: (Ext.isIE && !(Ext.isIE9 && document.documentMode === 9)) ?
692         function(name, ns) {
693             var d = this.dom,
694             type;
695             if(ns) {
696                 type = typeof d[ns + ":" + name];
697                 if (type != 'undefined' && type != 'unknown') {
698                     return d[ns + ":" + name] || null;
699                 }
700                 return null;
701             }
702             if (name === "for") {
703                 name = "htmlFor";
704             }
705             return d[name] || null;
706         }: function(name, ns) {
707             var d = this.dom;
708             if (ns) {
709                return d.getAttributeNS(ns, name) || d.getAttribute(ns + ":" + name);
710             }
711             return  d.getAttribute(name) || d[name] || null;
712         },
713
714         /**
715          * Update the innerHTML of this element
716          * @param {String} html The new HTML
717          * @return {Ext.core.Element} this
718          */
719         update: function(html) {
720             if (this.dom) {
721                 this.dom.innerHTML = html;
722             }
723             return this;
724         }
725     };
726
727     var ep = El.prototype;
728
729     El.addMethods = function(o) {
730         Ext.apply(ep, o);
731     };
732
733     /**
734      * Appends an event handler (shorthand for {@link #addListener}).
735      * @param {String} eventName The name of event to handle.
736      * @param {Function} fn The handler function the event invokes.
737      * @param {Object} scope (optional) The scope (<code>this</code> reference) in which the handler function is executed.
738      * @param {Object} options (optional) An object containing standard {@link #addListener} options
739      * @member Ext.core.Element
740      * @method on
741      */
742     ep.on = ep.addListener;
743
744     /**
745      * Removes an event handler from this element (see {@link #removeListener} for additional notes).
746      * @param {String} eventName The name of the event from which to remove the handler.
747      * @param {Function} fn The handler function to remove. <b>This must be a reference to the function passed into the {@link #addListener} call.</b>
748      * @param {Object} scope If a scope (<b><code>this</code></b> reference) was specified when the listener was added,
749      * then this must refer to the same object.
750      * @return {Ext.core.Element} this
751      * @member Ext.core.Element
752      * @method un
753      */
754     ep.un = ep.removeListener;
755
756     /**
757      * Removes all previous added listeners from this element
758      * @return {Ext.core.Element} this
759      * @member Ext.core.Element
760      * @method clearListeners
761      */
762     ep.clearListeners = ep.removeAllListeners;
763
764     /**
765      * Removes this element's dom reference.  Note that event and cache removal is handled at {@link Ext#removeNode Ext.removeNode}.
766      * Alias to {@link #remove}.
767      * @member Ext.core.Element
768      * @method destroy
769      */
770     ep.destroy = ep.remove;
771
772     /**
773      * true to automatically adjust width and height settings for box-model issues (default to true)
774      */
775     ep.autoBoxAdjust = true;
776
777     // private
778     var unitPattern = /\d+(px|em|%|en|ex|pt|in|cm|mm|pc)$/i,
779     docEl;
780
781     /**
782      * Retrieves Ext.core.Element objects.
783      * <p><b>This method does not retrieve {@link Ext.Component Component}s.</b> This method
784      * retrieves Ext.core.Element objects which encapsulate DOM elements. To retrieve a Component by
785      * its ID, use {@link Ext.ComponentManager#get}.</p>
786      * <p>Uses simple caching to consistently return the same object. Automatically fixes if an
787      * object was recreated with the same id via AJAX or DOM.</p>
788      * @param {Mixed} el The id of the node, a DOM Node or an existing Element.
789      * @return {Element} The Element object (or null if no matching element was found)
790      * @static
791      * @member Ext.core.Element
792      * @method get
793      */
794     El.get = function(el) {
795         var ex,
796         elm,
797         id;
798         if (!el) {
799             return null;
800         }
801         if (typeof el == "string") {
802             // element id
803             if (! (elm = DOC.getElementById(el))) {
804                 return null;
805             }
806             if (EC[el] && EC[el].el) {
807                 ex = EC[el].el;
808                 ex.dom = elm;
809             } else {
810                 ex = El.addToCache(new El(elm));
811             }
812             return ex;
813         } else if (el.tagName) {
814             // dom element
815             if (! (id = el.id)) {
816                 id = Ext.id(el);
817             }
818             if (EC[id] && EC[id].el) {
819                 ex = EC[id].el;
820                 ex.dom = el;
821             } else {
822                 ex = El.addToCache(new El(el));
823             }
824             return ex;
825         } else if (el instanceof El) {
826             if (el != docEl) {
827                 // refresh dom element in case no longer valid,
828                 // catch case where it hasn't been appended
829                 // If an el instance is passed, don't pass to getElementById without some kind of id
830                 if (Ext.isIE && (el.id == undefined || el.id == '')) {
831                     el.dom = el.dom;
832                 } else {
833                     el.dom = DOC.getElementById(el.id) || el.dom;
834                 }
835             }
836             return el;
837         } else if (el.isComposite) {
838             return el;
839         } else if (Ext.isArray(el)) {
840             return El.select(el);
841         } else if (el == DOC) {
842             // create a bogus element object representing the document object
843             if (!docEl) {
844                 var f = function() {};
845                 f.prototype = El.prototype;
846                 docEl = new f();
847                 docEl.dom = DOC;
848             }
849             return docEl;
850         }
851         return null;
852     };
853
854     El.addToCache = function(el, id) {
855         if (el) {
856             id = id || el.id;
857             EC[id] = {
858                 el: el,
859                 data: {},
860                 events: {}
861             };
862         }
863         return el;
864     };
865
866     // private method for getting and setting element data
867     El.data = function(el, key, value) {
868         el = El.get(el);
869         if (!el) {
870             return null;
871         }
872         var c = EC[el.id].data;
873         if (arguments.length == 2) {
874             return c[key];
875         } else {
876             return (c[key] = value);
877         }
878     };
879
880     // private
881     // Garbage collection - uncache elements/purge listeners on orphaned elements
882     // so we don't hold a reference and cause the browser to retain them
883     function garbageCollect() {
884         if (!Ext.enableGarbageCollector) {
885             clearInterval(El.collectorThreadId);
886         } else {
887             var eid,
888             el,
889             d,
890             o;
891
892             for (eid in EC) {
893                 if (!EC.hasOwnProperty(eid)) {
894                     continue;
895                 }
896                 o = EC[eid];
897                 if (o.skipGarbageCollection) {
898                     continue;
899                 }
900                 el = o.el;
901                 d = el.dom;
902                 // -------------------------------------------------------
903                 // Determining what is garbage:
904                 // -------------------------------------------------------
905                 // !d
906                 // dom node is null, definitely garbage
907                 // -------------------------------------------------------
908                 // !d.parentNode
909                 // no parentNode == direct orphan, definitely garbage
910                 // -------------------------------------------------------
911                 // !d.offsetParent && !document.getElementById(eid)
912                 // display none elements have no offsetParent so we will
913                 // also try to look it up by it's id. However, check
914                 // offsetParent first so we don't do unneeded lookups.
915                 // This enables collection of elements that are not orphans
916                 // directly, but somewhere up the line they have an orphan
917                 // parent.
918                 // -------------------------------------------------------
919                 if (!d || !d.parentNode || (!d.offsetParent && !DOC.getElementById(eid))) {
920                     if (d && Ext.enableListenerCollection) {
921                         Ext.EventManager.removeAll(d);
922                     }
923                     delete EC[eid];
924                 }
925             }
926             // Cleanup IE Object leaks
927             if (Ext.isIE) {
928                 var t = {};
929                 for (eid in EC) {
930                     if (!EC.hasOwnProperty(eid)) {
931                         continue;
932                     }
933                     t[eid] = EC[eid];
934                 }
935                 EC = Ext.cache = t;
936             }
937         }
938     }
939     El.collectorThreadId = setInterval(garbageCollect, 30000);
940
941     var flyFn = function() {};
942     flyFn.prototype = El.prototype;
943
944     // dom is optional
945     El.Flyweight = function(dom) {
946         this.dom = dom;
947     };
948
949     El.Flyweight.prototype = new flyFn();
950     El.Flyweight.prototype.isFlyweight = true;
951     El._flyweights = {};
952
953     /**
954      * <p>Gets the globally shared flyweight Element, with the passed node as the active element. Do not store a reference to this element -
955      * the dom node can be overwritten by other code. Shorthand of {@link Ext.core.Element#fly}</p>
956      * <p>Use this to make one-time references to DOM elements which are not going to be accessed again either by
957      * application code, or by Ext's classes. If accessing an element which will be processed regularly, then {@link Ext#get Ext.get}
958      * will be more appropriate to take advantage of the caching provided by the Ext.core.Element class.</p>
959      * @param {String/HTMLElement} el The dom node or id
960      * @param {String} named (optional) Allows for creation of named reusable flyweights to prevent conflicts
961      * (e.g. internally Ext uses "_global")
962      * @return {Element} The shared Element object (or null if no matching element was found)
963      * @member Ext.core.Element
964      * @method fly
965      */
966     El.fly = function(el, named) {
967         var ret = null;
968         named = named || '_global';
969         el = Ext.getDom(el);
970         if (el) {
971             (El._flyweights[named] = El._flyweights[named] || new El.Flyweight()).dom = el;
972             ret = El._flyweights[named];
973         }
974         return ret;
975     };
976
977     /**
978      * Retrieves Ext.core.Element objects.
979      * <p><b>This method does not retrieve {@link Ext.Component Component}s.</b> This method
980      * retrieves Ext.core.Element objects which encapsulate DOM elements. To retrieve a Component by
981      * its ID, use {@link Ext.ComponentManager#get}.</p>
982      * <p>Uses simple caching to consistently return the same object. Automatically fixes if an
983      * object was recreated with the same id via AJAX or DOM.</p>
984      * Shorthand of {@link Ext.core.Element#get}
985      * @param {Mixed} el The id of the node, a DOM Node or an existing Element.
986      * @return {Element} The Element object (or null if no matching element was found)
987      * @member Ext
988      * @method get
989      */
990     Ext.get = El.get;
991
992     /**
993      * <p>Gets the globally shared flyweight Element, with the passed node as the active element. Do not store a reference to this element -
994      * the dom node can be overwritten by other code. Shorthand of {@link Ext.core.Element#fly}</p>
995      * <p>Use this to make one-time references to DOM elements which are not going to be accessed again either by
996      * application code, or by Ext's classes. If accessing an element which will be processed regularly, then {@link Ext#get Ext.get}
997      * will be more appropriate to take advantage of the caching provided by the Ext.core.Element class.</p>
998      * @param {String/HTMLElement} el The dom node or id
999      * @param {String} named (optional) Allows for creation of named reusable flyweights to prevent conflicts
1000      * (e.g. internally Ext uses "_global")
1001      * @return {Element} The shared Element object (or null if no matching element was found)
1002      * @member Ext
1003      * @method fly
1004      */
1005     Ext.fly = El.fly;
1006
1007     // speedy lookup for elements never to box adjust
1008     var noBoxAdjust = Ext.isStrict ? {
1009         select: 1
1010     }: {
1011         input: 1,
1012         select: 1,
1013         textarea: 1
1014     };
1015     if (Ext.isIE || Ext.isGecko) {
1016         noBoxAdjust['button'] = 1;
1017     }
1018 })();