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