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