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