Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / Element-more.html
1 <html>
2 <head>
3   <title>The source code</title>
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
6 </head>
7 <body  onload="prettyPrint();">
8     <pre class="prettyprint lang-js">/*!
9  * Ext JS Library 3.0.3
10  * Copyright(c) 2006-2009 Ext JS, LLC
11  * licensing@extjs.com
12  * http://www.extjs.com/license
13  */
14 /**\r
15  * @class Ext.Element\r
16  */\r
17 Ext.Element.addMethods({    \r
18     <div id="method-Ext.Element-swallowEvent"></div>/**\r
19      * Stops the specified event(s) from bubbling and optionally prevents the default action\r
20      * @param {String/Array} eventName an event / array of events to stop from bubbling\r
21      * @param {Boolean} preventDefault (optional) true to prevent the default action too\r
22      * @return {Ext.Element} this\r
23      */\r
24     swallowEvent : function(eventName, preventDefault){\r
25             var me = this;\r
26         function fn(e){\r
27             e.stopPropagation();\r
28             if(preventDefault){\r
29                 e.preventDefault();\r
30             }\r
31         }\r
32         if(Ext.isArray(eventName)){            \r
33                 Ext.each(eventName, function(e) {\r
34                  me.on(e, fn);\r
35             });\r
36             return me;\r
37         }\r
38         me.on(eventName, fn);\r
39         return me;\r
40     },\r
41     \r
42     <div id="method-Ext.Element-relayEvent"></div>/**\r
43      * Create an event handler on this element such that when the event fires and is handled by this element,\r
44      * it will be relayed to another object (i.e., fired again as if it originated from that object instead).\r
45      * @param {String} eventName The type of event to relay\r
46      * @param {Object} object Any object that extends {@link Ext.util.Observable} that will provide the context\r
47      * for firing the relayed event\r
48      */\r
49     relayEvent : function(eventName, observable){\r
50         this.on(eventName, function(e){\r
51             observable.fireEvent(eventName, e);\r
52         });\r
53     },\r
54     \r
55         <div id="method-Ext.Element-clean"></div>/**\r
56      * Removes worthless text nodes\r
57      * @param {Boolean} forceReclean (optional) By default the element\r
58      * keeps track if it has been cleaned already so\r
59      * you can call this over and over. However, if you update the element and\r
60      * need to force a reclean, you can pass true.\r
61      */\r
62     clean : function(forceReclean){\r
63         var me = this, \r
64             dom = me.dom,\r
65                 n = dom.firstChild, \r
66                 ni = -1;\r
67                 \r
68             if(Ext.Element.data(dom, 'isCleaned') && forceReclean !== true){\r
69             return me;\r
70         }      \r
71                 \r
72             while(n){\r
73                 var nx = n.nextSibling;\r
74             if(n.nodeType == 3 && !/\S/.test(n.nodeValue)){\r
75                 dom.removeChild(n);\r
76             }else{\r
77                 n.nodeIndex = ++ni;\r
78             }\r
79                 n = nx;\r
80             }\r
81         Ext.Element.data(dom, 'isCleaned', true);\r
82             return me;\r
83         },\r
84     \r
85     <div id="method-Ext.Element-load"></div>/**\r
86      * Direct access to the Updater {@link Ext.Updater#update} method. The method takes the same object\r
87      * parameter as {@link Ext.Updater#update}\r
88      * @return {Ext.Element} this\r
89      */\r
90     load : function(){\r
91         var um = this.getUpdater();\r
92         um.update.apply(um, arguments);\r
93         return this;\r
94     },\r
95 \r
96     <div id="method-Ext.Element-getUpdater"></div>/**\r
97     * Gets this element's {@link Ext.Updater Updater}\r
98     * @return {Ext.Updater} The Updater\r
99     */\r
100     getUpdater : function(){\r
101         return this.updateManager || (this.updateManager = new Ext.Updater(this));\r
102     },\r
103     \r
104         <div id="method-Ext.Element-update"></div>/**\r
105     * Update the innerHTML of this element, optionally searching for and processing scripts\r
106     * @param {String} html The new HTML\r
107     * @param {Boolean} loadScripts (optional) True to look for and process scripts (defaults to false)\r
108     * @param {Function} callback (optional) For async script loading you can be notified when the update completes\r
109     * @return {Ext.Element} this\r
110      */\r
111     update : function(html, loadScripts, callback){\r
112         html = html || "";\r
113             \r
114         if(loadScripts !== true){\r
115             this.dom.innerHTML = html;\r
116             if(Ext.isFunction(callback)){\r
117                 callback();\r
118             }\r
119             return this;\r
120         }\r
121         \r
122         var id = Ext.id(),\r
123                 dom = this.dom;\r
124 \r
125         html += '<span id="' + id + '"></span>';\r
126 \r
127         Ext.lib.Event.onAvailable(id, function(){\r
128             var DOC = document,\r
129                 hd = DOC.getElementsByTagName("head")[0],\r
130                 re = /(?:<script([^>]*)?>)((\n|\r|.)*?)(?:<\/script>)/ig,\r
131                 srcRe = /\ssrc=([\'\"])(.*?)\1/i,\r
132                 typeRe = /\stype=([\'\"])(.*?)\1/i,\r
133                 match,\r
134                 attrs,\r
135                 srcMatch,\r
136                 typeMatch,\r
137                 el,\r
138                 s;\r
139 \r
140             while((match = re.exec(html))){\r
141                 attrs = match[1];\r
142                 srcMatch = attrs ? attrs.match(srcRe) : false;\r
143                 if(srcMatch && srcMatch[2]){\r
144                    s = DOC.createElement("script");\r
145                    s.src = srcMatch[2];\r
146                    typeMatch = attrs.match(typeRe);\r
147                    if(typeMatch && typeMatch[2]){\r
148                        s.type = typeMatch[2];\r
149                    }\r
150                    hd.appendChild(s);\r
151                 }else if(match[2] && match[2].length > 0){\r
152                     if(window.execScript) {\r
153                        window.execScript(match[2]);\r
154                     } else {\r
155                        window.eval(match[2]);\r
156                     }\r
157                 }\r
158             }\r
159             el = DOC.getElementById(id);\r
160             if(el){Ext.removeNode(el);}\r
161             if(Ext.isFunction(callback)){\r
162                 callback();\r
163             }\r
164         });\r
165         dom.innerHTML = html.replace(/(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig, "");\r
166         return this;\r
167     },\r
168     \r
169     <div id="method-Ext.Element-createProxy"></div>/**\r
170      * Creates a proxy element of this element\r
171      * @param {String/Object} config The class name of the proxy element or a DomHelper config object\r
172      * @param {String/HTMLElement} renderTo (optional) The element or element id to render the proxy to (defaults to document.body)\r
173      * @param {Boolean} matchBox (optional) True to align and size the proxy to this element now (defaults to false)\r
174      * @return {Ext.Element} The new proxy element\r
175      */\r
176     createProxy : function(config, renderTo, matchBox){\r
177         config = Ext.isObject(config) ? config : {tag : "div", cls: config};\r
178 \r
179         var me = this,\r
180                 proxy = renderTo ? Ext.DomHelper.append(renderTo, config, true) :\r
181                                                    Ext.DomHelper.insertBefore(me.dom, config, true);        \r
182         \r
183         if(matchBox && me.setBox && me.getBox){ // check to make sure Element.position.js is loaded\r
184            proxy.setBox(me.getBox());\r
185         }\r
186         return proxy;\r
187     }\r
188 });\r
189 \r
190 Ext.Element.prototype.getUpdateManager = Ext.Element.prototype.getUpdater;\r
191 \r
192 // private\r
193 Ext.Element.uncache = function(el){\r
194     for(var i = 0, a = arguments, len = a.length; i < len; i++) {\r
195         if(a[i]){\r
196             delete Ext.Element.cache[a[i].id || a[i]];\r
197         }\r
198     }\r
199 };</pre>
200 </body>
201 </html>