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