Upgrade to ExtJS 3.2.2 - Released 06/02/2010
[extjs.git] / src / ext-core / src / core / Ext.js
1 /*!
2  * Ext JS Library 3.2.2
3  * Copyright(c) 2006-2010 Ext JS, Inc.
4  * licensing@extjs.com
5  * http://www.extjs.com/license
6  */
7 // for old browsers
8 window.undefined = window.undefined;
9
10 /**
11  * @class Ext
12  * Ext core utilities and functions.
13  * @singleton
14  */
15
16 Ext = {
17     /**
18      * The version of the framework
19      * @type String
20      */
21     version : '3.2.2',
22     versionDetail : {
23         major: 3,
24         minor: 2,
25         patch: 2
26     }
27 };
28
29 /**
30  * Copies all the properties of config to obj.
31  * @param {Object} obj The receiver of the properties
32  * @param {Object} config The source of the properties
33  * @param {Object} defaults A different object that will also be applied for default values
34  * @return {Object} returns obj
35  * @member Ext apply
36  */
37 Ext.apply = function(o, c, defaults){
38     // no "this" reference for friendly out of scope calls
39     if(defaults){
40         Ext.apply(o, defaults);
41     }
42     if(o && c && typeof c == 'object'){
43         for(var p in c){
44             o[p] = c[p];
45         }
46     }
47     return o;
48 };
49
50 (function(){
51     var idSeed = 0,
52         toString = Object.prototype.toString,
53         ua = navigator.userAgent.toLowerCase(),
54         check = function(r){
55             return r.test(ua);
56         },
57         DOC = document,
58         isStrict = DOC.compatMode == "CSS1Compat",
59         isOpera = check(/opera/),
60         isChrome = check(/\bchrome\b/),
61         isWebKit = check(/webkit/),
62         isSafari = !isChrome && check(/safari/),
63         isSafari2 = isSafari && check(/applewebkit\/4/), // unique to Safari 2
64         isSafari3 = isSafari && check(/version\/3/),
65         isSafari4 = isSafari && check(/version\/4/),
66         isIE = !isOpera && check(/msie/),
67         isIE7 = isIE && check(/msie 7/),
68         isIE8 = isIE && check(/msie 8/),
69         isIE6 = isIE && !isIE7 && !isIE8,
70         isGecko = !isWebKit && check(/gecko/),
71         isGecko2 = isGecko && check(/rv:1\.8/),
72         isGecko3 = isGecko && check(/rv:1\.9/),
73         isBorderBox = isIE && !isStrict,
74         isWindows = check(/windows|win32/),
75         isMac = check(/macintosh|mac os x/),
76         isAir = check(/adobeair/),
77         isLinux = check(/linux/),
78         isSecure = /^https/i.test(window.location.protocol);
79
80     // remove css image flicker
81     if(isIE6){
82         try{
83             DOC.execCommand("BackgroundImageCache", false, true);
84         }catch(e){}
85     }
86
87     Ext.apply(Ext, {
88         /**
89          * URL to a blank file used by Ext when in secure mode for iframe src and onReady src to prevent
90          * the IE insecure content warning (<tt>'about:blank'</tt>, except for IE in secure mode, which is <tt>'javascript:""'</tt>).
91          * @type String
92          */
93         SSL_SECURE_URL : isSecure && isIE ? 'javascript:""' : 'about:blank',
94         /**
95          * True if the browser is in strict (standards-compliant) mode, as opposed to quirks mode
96          * @type Boolean
97          */
98         isStrict : isStrict,
99         /**
100          * True if the page is running over SSL
101          * @type Boolean
102          */
103         isSecure : isSecure,
104         /**
105          * True when the document is fully initialized and ready for action
106          * @type Boolean
107          */
108         isReady : false,
109
110         /**
111          * True if the {@link Ext.Fx} Class is available
112          * @type Boolean
113          * @property enableFx
114          */
115
116         /**
117          * True to automatically uncache orphaned Ext.Elements periodically (defaults to true)
118          * @type Boolean
119          */
120         enableGarbageCollector : true,
121
122         /**
123          * True to automatically purge event listeners during garbageCollection (defaults to false).
124          * @type Boolean
125          */
126         enableListenerCollection : false,
127
128         /**
129          * EXPERIMENTAL - True to cascade listener removal to child elements when an element is removed.
130          * Currently not optimized for performance.
131          * @type Boolean
132          */
133         enableNestedListenerRemoval : false,
134
135         /**
136          * Indicates whether to use native browser parsing for JSON methods.
137          * This option is ignored if the browser does not support native JSON methods.
138          * <b>Note: Native JSON methods will not work with objects that have functions.
139          * Also, property names must be quoted, otherwise the data will not parse.</b> (Defaults to false)
140          * @type Boolean
141          */
142         USE_NATIVE_JSON : false,
143
144         /**
145          * Copies all the properties of config to obj if they don't already exist.
146          * @param {Object} obj The receiver of the properties
147          * @param {Object} config The source of the properties
148          * @return {Object} returns obj
149          */
150         applyIf : function(o, c){
151             if(o){
152                 for(var p in c){
153                     if(!Ext.isDefined(o[p])){
154                         o[p] = c[p];
155                     }
156                 }
157             }
158             return o;
159         },
160
161         /**
162          * Generates unique ids. If the element already has an id, it is unchanged
163          * @param {Mixed} el (optional) The element to generate an id for
164          * @param {String} prefix (optional) Id prefix (defaults "ext-gen")
165          * @return {String} The generated Id.
166          */
167         id : function(el, prefix){
168             el = Ext.getDom(el, true) || {};
169             if (!el.id) {
170                 el.id = (prefix || "ext-gen") + (++idSeed);
171             }
172             return el.id;
173         },
174
175         /**
176          * <p>Extends one class to create a subclass and optionally overrides members with the passed literal. This method
177          * also adds the function "override()" to the subclass that can be used to override members of the class.</p>
178          * For example, to create a subclass of Ext GridPanel:
179          * <pre><code>
180 MyGridPanel = Ext.extend(Ext.grid.GridPanel, {
181     constructor: function(config) {
182
183 //      Create configuration for this Grid.
184         var store = new Ext.data.Store({...});
185         var colModel = new Ext.grid.ColumnModel({...});
186
187 //      Create a new config object containing our computed properties
188 //      *plus* whatever was in the config parameter.
189         config = Ext.apply({
190             store: store,
191             colModel: colModel
192         }, config);
193
194         MyGridPanel.superclass.constructor.call(this, config);
195
196 //      Your postprocessing here
197     },
198
199     yourMethod: function() {
200         // etc.
201     }
202 });
203 </code></pre>
204          *
205          * <p>This function also supports a 3-argument call in which the subclass's constructor is
206          * passed as an argument. In this form, the parameters are as follows:</p>
207          * <div class="mdetail-params"><ul>
208          * <li><code>subclass</code> : Function <div class="sub-desc">The subclass constructor.</div></li>
209          * <li><code>superclass</code> : Function <div class="sub-desc">The constructor of class being extended</div></li>
210          * <li><code>overrides</code> : Object <div class="sub-desc">A literal with members which are copied into the subclass's
211          * prototype, and are therefore shared among all instances of the new class.</div></li>
212          * </ul></div>
213          *
214          * @param {Function} superclass The constructor of class being extended.
215          * @param {Object} overrides <p>A literal with members which are copied into the subclass's
216          * prototype, and are therefore shared between all instances of the new class.</p>
217          * <p>This may contain a special member named <tt><b>constructor</b></tt>. This is used
218          * to define the constructor of the new class, and is returned. If this property is
219          * <i>not</i> specified, a constructor is generated and returned which just calls the
220          * superclass's constructor passing on its parameters.</p>
221          * <p><b>It is essential that you call the superclass constructor in any provided constructor. See example code.</b></p>
222          * @return {Function} The subclass constructor from the <code>overrides</code> parameter, or a generated one if not provided.
223          */
224         extend : function(){
225             // inline overrides
226             var io = function(o){
227                 for(var m in o){
228                     this[m] = o[m];
229                 }
230             };
231             var oc = Object.prototype.constructor;
232
233             return function(sb, sp, overrides){
234                 if(typeof sp == 'object'){
235                     overrides = sp;
236                     sp = sb;
237                     sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);};
238                 }
239                 var F = function(){},
240                     sbp,
241                     spp = sp.prototype;
242
243                 F.prototype = spp;
244                 sbp = sb.prototype = new F();
245                 sbp.constructor=sb;
246                 sb.superclass=spp;
247                 if(spp.constructor == oc){
248                     spp.constructor=sp;
249                 }
250                 sb.override = function(o){
251                     Ext.override(sb, o);
252                 };
253                 sbp.superclass = sbp.supr = (function(){
254                     return spp;
255                 });
256                 sbp.override = io;
257                 Ext.override(sb, overrides);
258                 sb.extend = function(o){return Ext.extend(sb, o);};
259                 return sb;
260             };
261         }(),
262
263         /**
264          * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
265          * Usage:<pre><code>
266 Ext.override(MyClass, {
267     newMethod1: function(){
268         // etc.
269     },
270     newMethod2: function(foo){
271         // etc.
272     }
273 });
274 </code></pre>
275          * @param {Object} origclass The class to override
276          * @param {Object} overrides The list of functions to add to origClass.  This should be specified as an object literal
277          * containing one or more methods.
278          * @method override
279          */
280         override : function(origclass, overrides){
281             if(overrides){
282                 var p = origclass.prototype;
283                 Ext.apply(p, overrides);
284                 if(Ext.isIE && overrides.hasOwnProperty('toString')){
285                     p.toString = overrides.toString;
286                 }
287             }
288         },
289
290         /**
291          * Creates namespaces to be used for scoping variables and classes so that they are not global.
292          * Specifying the last node of a namespace implicitly creates all other nodes. Usage:
293          * <pre><code>
294 Ext.namespace('Company', 'Company.data');
295 Ext.namespace('Company.data'); // equivalent and preferable to above syntax
296 Company.Widget = function() { ... }
297 Company.data.CustomStore = function(config) { ... }
298 </code></pre>
299          * @param {String} namespace1
300          * @param {String} namespace2
301          * @param {String} etc
302          * @return {Object} The namespace object. (If multiple arguments are passed, this will be the last namespace created)
303          * @method namespace
304          */
305         namespace : function(){
306             var o, d;
307             Ext.each(arguments, function(v) {
308                 d = v.split(".");
309                 o = window[d[0]] = window[d[0]] || {};
310                 Ext.each(d.slice(1), function(v2){
311                     o = o[v2] = o[v2] || {};
312                 });
313             });
314             return o;
315         },
316
317         /**
318          * Takes an object and converts it to an encoded URL. e.g. Ext.urlEncode({foo: 1, bar: 2}); would return "foo=1&bar=2".  Optionally, property values can be arrays, instead of keys and the resulting string that's returned will contain a name/value pair for each array value.
319          * @param {Object} o
320          * @param {String} pre (optional) A prefix to add to the url encoded string
321          * @return {String}
322          */
323         urlEncode : function(o, pre){
324             var empty,
325                 buf = [],
326                 e = encodeURIComponent;
327
328             Ext.iterate(o, function(key, item){
329                 empty = Ext.isEmpty(item);
330                 Ext.each(empty ? key : item, function(val){
331                     buf.push('&', e(key), '=', (!Ext.isEmpty(val) && (val != key || !empty)) ? (Ext.isDate(val) ? Ext.encode(val).replace(/"/g, '') : e(val)) : '');
332                 });
333             });
334             if(!pre){
335                 buf.shift();
336                 pre = '';
337             }
338             return pre + buf.join('');
339         },
340
341         /**
342          * Takes an encoded URL and and converts it to an object. Example: <pre><code>
343 Ext.urlDecode("foo=1&bar=2"); // returns {foo: "1", bar: "2"}
344 Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", false); // returns {foo: "1", bar: ["2", "3", "4"]}
345 </code></pre>
346          * @param {String} string
347          * @param {Boolean} overwrite (optional) Items of the same name will overwrite previous values instead of creating an an array (Defaults to false).
348          * @return {Object} A literal with members
349          */
350         urlDecode : function(string, overwrite){
351             if(Ext.isEmpty(string)){
352                 return {};
353             }
354             var obj = {},
355                 pairs = string.split('&'),
356                 d = decodeURIComponent,
357                 name,
358                 value;
359             Ext.each(pairs, function(pair) {
360                 pair = pair.split('=');
361                 name = d(pair[0]);
362                 value = d(pair[1]);
363                 obj[name] = overwrite || !obj[name] ? value :
364                             [].concat(obj[name]).concat(value);
365             });
366             return obj;
367         },
368
369         /**
370          * Appends content to the query string of a URL, handling logic for whether to place
371          * a question mark or ampersand.
372          * @param {String} url The URL to append to.
373          * @param {String} s The content to append to the URL.
374          * @return (String) The resulting URL
375          */
376         urlAppend : function(url, s){
377             if(!Ext.isEmpty(s)){
378                 return url + (url.indexOf('?') === -1 ? '?' : '&') + s;
379             }
380             return url;
381         },
382
383         /**
384          * Converts any iterable (numeric indices and a length property) into a true array
385          * Don't use this on strings. IE doesn't support "abc"[0] which this implementation depends on.
386          * For strings, use this instead: "abc".match(/./g) => [a,b,c];
387          * @param {Iterable} the iterable object to be turned into a true Array.
388          * @return (Array) array
389          */
390          toArray : function(){
391              return isIE ?
392                  function(a, i, j, res){
393                      res = [];
394                      for(var x = 0, len = a.length; x < len; x++) {
395                          res.push(a[x]);
396                      }
397                      return res.slice(i || 0, j || res.length);
398                  } :
399                  function(a, i, j){
400                      return Array.prototype.slice.call(a, i || 0, j || a.length);
401                  };
402          }(),
403
404         isIterable : function(v){
405             //check for array or arguments
406             if(Ext.isArray(v) || v.callee){
407                 return true;
408             }
409             //check for node list type
410             if(/NodeList|HTMLCollection/.test(toString.call(v))){
411                 return true;
412             }
413             //NodeList has an item and length property
414             //IXMLDOMNodeList has nextNode method, needs to be checked first.
415             return ((typeof v.nextNode != 'undefined' || v.item) && Ext.isNumber(v.length));
416         },
417
418         /**
419          * Iterates an array calling the supplied function.
420          * @param {Array/NodeList/Mixed} array The array to be iterated. If this
421          * argument is not really an array, the supplied function is called once.
422          * @param {Function} fn The function to be called with each item. If the
423          * supplied function returns false, iteration stops and this method returns
424          * the current <code>index</code>. This function is called with
425          * the following arguments:
426          * <div class="mdetail-params"><ul>
427          * <li><code>item</code> : <i>Mixed</i>
428          * <div class="sub-desc">The item at the current <code>index</code>
429          * in the passed <code>array</code></div></li>
430          * <li><code>index</code> : <i>Number</i>
431          * <div class="sub-desc">The current index within the array</div></li>
432          * <li><code>allItems</code> : <i>Array</i>
433          * <div class="sub-desc">The <code>array</code> passed as the first
434          * argument to <code>Ext.each</code>.</div></li>
435          * </ul></div>
436          * @param {Object} scope The scope (<code>this</code> reference) in which the specified function is executed.
437          * Defaults to the <code>item</code> at the current <code>index</code>
438          * within the passed <code>array</code>.
439          * @return See description for the fn parameter.
440          */
441         each : function(array, fn, scope){
442             if(Ext.isEmpty(array, true)){
443                 return;
444             }
445             if(!Ext.isIterable(array) || Ext.isPrimitive(array)){
446                 array = [array];
447             }
448             for(var i = 0, len = array.length; i < len; i++){
449                 if(fn.call(scope || array[i], array[i], i, array) === false){
450                     return i;
451                 };
452             }
453         },
454
455         /**
456          * Iterates either the elements in an array, or each of the properties in an object.
457          * <b>Note</b>: If you are only iterating arrays, it is better to call {@link #each}.
458          * @param {Object/Array} object The object or array to be iterated
459          * @param {Function} fn The function to be called for each iteration.
460          * The iteration will stop if the supplied function returns false, or
461          * all array elements / object properties have been covered. The signature
462          * varies depending on the type of object being interated:
463          * <div class="mdetail-params"><ul>
464          * <li>Arrays : <tt>(Object item, Number index, Array allItems)</tt>
465          * <div class="sub-desc">
466          * When iterating an array, the supplied function is called with each item.</div></li>
467          * <li>Objects : <tt>(String key, Object value, Object)</tt>
468          * <div class="sub-desc">
469          * When iterating an object, the supplied function is called with each key-value pair in
470          * the object, and the iterated object</div></li>
471          * </ul></div>
472          * @param {Object} scope The scope (<code>this</code> reference) in which the specified function is executed. Defaults to
473          * the <code>object</code> being iterated.
474          */
475         iterate : function(obj, fn, scope){
476             if(Ext.isEmpty(obj)){
477                 return;
478             }
479             if(Ext.isIterable(obj)){
480                 Ext.each(obj, fn, scope);
481                 return;
482             }else if(typeof obj == 'object'){
483                 for(var prop in obj){
484                     if(obj.hasOwnProperty(prop)){
485                         if(fn.call(scope || obj, prop, obj[prop], obj) === false){
486                             return;
487                         };
488                     }
489                 }
490             }
491         },
492
493         /**
494          * Return the dom node for the passed String (id), dom node, or Ext.Element.
495          * Optional 'strict' flag is needed for IE since it can return 'name' and
496          * 'id' elements by using getElementById.
497          * Here are some examples:
498          * <pre><code>
499 // gets dom node based on id
500 var elDom = Ext.getDom('elId');
501 // gets dom node based on the dom node
502 var elDom1 = Ext.getDom(elDom);
503
504 // If we don&#39;t know if we are working with an
505 // Ext.Element or a dom node use Ext.getDom
506 function(el){
507     var dom = Ext.getDom(el);
508     // do something with the dom node
509 }
510          * </code></pre>
511          * <b>Note</b>: the dom node to be found actually needs to exist (be rendered, etc)
512          * when this method is called to be successful.
513          * @param {Mixed} el
514          * @return HTMLElement
515          */
516         getDom : function(el, strict){
517             if(!el || !DOC){
518                 return null;
519             }
520             if (el.dom){
521                 return el.dom;
522             } else {
523                 if (typeof el == 'string') {
524                     var e = DOC.getElementById(el);
525                     // IE returns elements with the 'name' and 'id' attribute.
526                     // we do a strict check to return the element with only the id attribute
527                     if (e && isIE && strict) {
528                         if (el == e.getAttribute('id')) {
529                             return e;
530                         } else {
531                             return null;
532                         }
533                     }
534                     return e;
535                 } else {
536                     return el;
537                 }
538             }
539         },
540
541         /**
542          * Returns the current document body as an {@link Ext.Element}.
543          * @return Ext.Element The document body
544          */
545         getBody : function(){
546             return Ext.get(DOC.body || DOC.documentElement);
547         },
548         
549         /**
550          * Returns the current document body as an {@link Ext.Element}.
551          * @return Ext.Element The document body
552          */
553         getHead : function() {
554             var head;
555             
556             return function() {
557                 if (head == undefined) {
558                     head = Ext.get(DOC.getElementsByTagName("head")[0]);
559                 }
560                 
561                 return head;
562             };
563         }(),
564
565         /**
566          * Removes a DOM node from the document.
567          */
568         /**
569          * <p>Removes this element from the document, removes all DOM event listeners, and deletes the cache reference.
570          * All DOM event listeners are removed from this element. If {@link Ext#enableNestedListenerRemoval} is
571          * <code>true</code>, then DOM event listeners are also removed from all child nodes. The body node
572          * will be ignored if passed in.</p>
573          * @param {HTMLElement} node The node to remove
574          */
575         removeNode : isIE && !isIE8 ? function(){
576             var d;
577             return function(n){
578                 if(n && n.tagName != 'BODY'){
579                     (Ext.enableNestedListenerRemoval) ? Ext.EventManager.purgeElement(n, true) : Ext.EventManager.removeAll(n);
580                     d = d || DOC.createElement('div');
581                     d.appendChild(n);
582                     d.innerHTML = '';
583                     delete Ext.elCache[n.id];
584                 }
585             };
586         }() : function(n){
587             if(n && n.parentNode && n.tagName != 'BODY'){
588                 (Ext.enableNestedListenerRemoval) ? Ext.EventManager.purgeElement(n, true) : Ext.EventManager.removeAll(n);
589                 n.parentNode.removeChild(n);
590                 delete Ext.elCache[n.id];
591             }
592         },
593
594         /**
595          * <p>Returns true if the passed value is empty.</p>
596          * <p>The value is deemed to be empty if it is<div class="mdetail-params"><ul>
597          * <li>null</li>
598          * <li>undefined</li>
599          * <li>an empty array</li>
600          * <li>a zero length string (Unless the <tt>allowBlank</tt> parameter is <tt>true</tt>)</li>
601          * </ul></div>
602          * @param {Mixed} value The value to test
603          * @param {Boolean} allowBlank (optional) true to allow empty strings (defaults to false)
604          * @return {Boolean}
605          */
606         isEmpty : function(v, allowBlank){
607             return v === null || v === undefined || ((Ext.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
608         },
609
610         /**
611          * Returns true if the passed value is a JavaScript array, otherwise false.
612          * @param {Mixed} value The value to test
613          * @return {Boolean}
614          */
615         isArray : function(v){
616             return toString.apply(v) === '[object Array]';
617         },
618
619         /**
620          * Returns true if the passed object is a JavaScript date object, otherwise false.
621          * @param {Object} object The object to test
622          * @return {Boolean}
623          */
624         isDate : function(v){
625             return toString.apply(v) === '[object Date]';
626         },
627
628         /**
629          * Returns true if the passed value is a JavaScript Object, otherwise false.
630          * @param {Mixed} value The value to test
631          * @return {Boolean}
632          */
633         isObject : function(v){
634             return !!v && Object.prototype.toString.call(v) === '[object Object]';
635         },
636
637         /**
638          * Returns true if the passed value is a JavaScript 'primitive', a string, number or boolean.
639          * @param {Mixed} value The value to test
640          * @return {Boolean}
641          */
642         isPrimitive : function(v){
643             return Ext.isString(v) || Ext.isNumber(v) || Ext.isBoolean(v);
644         },
645
646         /**
647          * Returns true if the passed value is a JavaScript Function, otherwise false.
648          * @param {Mixed} value The value to test
649          * @return {Boolean}
650          */
651         isFunction : function(v){
652             return toString.apply(v) === '[object Function]';
653         },
654
655         /**
656          * Returns true if the passed value is a number. Returns false for non-finite numbers.
657          * @param {Mixed} value The value to test
658          * @return {Boolean}
659          */
660         isNumber : function(v){
661             return typeof v === 'number' && isFinite(v);
662         },
663
664         /**
665          * Returns true if the passed value is a string.
666          * @param {Mixed} value The value to test
667          * @return {Boolean}
668          */
669         isString : function(v){
670             return typeof v === 'string';
671         },
672
673         /**
674          * Returns true if the passed value is a boolean.
675          * @param {Mixed} value The value to test
676          * @return {Boolean}
677          */
678         isBoolean : function(v){
679             return typeof v === 'boolean';
680         },
681
682         /**
683          * Returns true if the passed value is an HTMLElement
684          * @param {Mixed} value The value to test
685          * @return {Boolean}
686          */
687         isElement : function(v) {
688             return v ? !!v.tagName : false;
689         },
690
691         /**
692          * Returns true if the passed value is not undefined.
693          * @param {Mixed} value The value to test
694          * @return {Boolean}
695          */
696         isDefined : function(v){
697             return typeof v !== 'undefined';
698         },
699
700         /**
701          * True if the detected browser is Opera.
702          * @type Boolean
703          */
704         isOpera : isOpera,
705         /**
706          * True if the detected browser uses WebKit.
707          * @type Boolean
708          */
709         isWebKit : isWebKit,
710         /**
711          * True if the detected browser is Chrome.
712          * @type Boolean
713          */
714         isChrome : isChrome,
715         /**
716          * True if the detected browser is Safari.
717          * @type Boolean
718          */
719         isSafari : isSafari,
720         /**
721          * True if the detected browser is Safari 3.x.
722          * @type Boolean
723          */
724         isSafari3 : isSafari3,
725         /**
726          * True if the detected browser is Safari 4.x.
727          * @type Boolean
728          */
729         isSafari4 : isSafari4,
730         /**
731          * True if the detected browser is Safari 2.x.
732          * @type Boolean
733          */
734         isSafari2 : isSafari2,
735         /**
736          * True if the detected browser is Internet Explorer.
737          * @type Boolean
738          */
739         isIE : isIE,
740         /**
741          * True if the detected browser is Internet Explorer 6.x.
742          * @type Boolean
743          */
744         isIE6 : isIE6,
745         /**
746          * True if the detected browser is Internet Explorer 7.x.
747          * @type Boolean
748          */
749         isIE7 : isIE7,
750         /**
751          * True if the detected browser is Internet Explorer 8.x.
752          * @type Boolean
753          */
754         isIE8 : isIE8,
755         /**
756          * True if the detected browser uses the Gecko layout engine (e.g. Mozilla, Firefox).
757          * @type Boolean
758          */
759         isGecko : isGecko,
760         /**
761          * True if the detected browser uses a pre-Gecko 1.9 layout engine (e.g. Firefox 2.x).
762          * @type Boolean
763          */
764         isGecko2 : isGecko2,
765         /**
766          * True if the detected browser uses a Gecko 1.9+ layout engine (e.g. Firefox 3.x).
767          * @type Boolean
768          */
769         isGecko3 : isGecko3,
770         /**
771          * True if the detected browser is Internet Explorer running in non-strict mode.
772          * @type Boolean
773          */
774         isBorderBox : isBorderBox,
775         /**
776          * True if the detected platform is Linux.
777          * @type Boolean
778          */
779         isLinux : isLinux,
780         /**
781          * True if the detected platform is Windows.
782          * @type Boolean
783          */
784         isWindows : isWindows,
785         /**
786          * True if the detected platform is Mac OS.
787          * @type Boolean
788          */
789         isMac : isMac,
790         /**
791          * True if the detected platform is Adobe Air.
792          * @type Boolean
793          */
794         isAir : isAir
795     });
796
797     /**
798      * Creates namespaces to be used for scoping variables and classes so that they are not global.
799      * Specifying the last node of a namespace implicitly creates all other nodes. Usage:
800      * <pre><code>
801 Ext.namespace('Company', 'Company.data');
802 Ext.namespace('Company.data'); // equivalent and preferable to above syntax
803 Company.Widget = function() { ... }
804 Company.data.CustomStore = function(config) { ... }
805 </code></pre>
806      * @param {String} namespace1
807      * @param {String} namespace2
808      * @param {String} etc
809      * @return {Object} The namespace object. (If multiple arguments are passed, this will be the last namespace created)
810      * @method ns
811      */
812     Ext.ns = Ext.namespace;
813 })();
814
815 Ext.ns("Ext.util", "Ext.lib", "Ext.data");
816
817 Ext.elCache = {};
818
819 /**
820  * @class Function
821  * These functions are available on every Function object (any JavaScript function).
822  */
823 Ext.apply(Function.prototype, {
824      /**
825      * Creates an interceptor function. The passed function is called before the original one. If it returns false,
826      * the original one is not called. The resulting function returns the results of the original function.
827      * The passed function is called with the parameters of the original function. Example usage:
828      * <pre><code>
829 var sayHi = function(name){
830     alert('Hi, ' + name);
831 }
832
833 sayHi('Fred'); // alerts "Hi, Fred"
834
835 // create a new function that validates input without
836 // directly modifying the original function:
837 var sayHiToFriend = sayHi.createInterceptor(function(name){
838     return name == 'Brian';
839 });
840
841 sayHiToFriend('Fred');  // no alert
842 sayHiToFriend('Brian'); // alerts "Hi, Brian"
843 </code></pre>
844      * @param {Function} fcn The function to call before the original
845      * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the passed function is executed.
846      * <b>If omitted, defaults to the scope in which the original function is called or the browser window.</b>
847      * @return {Function} The new function
848      */
849     createInterceptor : function(fcn, scope){
850         var method = this;
851         return !Ext.isFunction(fcn) ?
852                 this :
853                 function() {
854                     var me = this,
855                         args = arguments;
856                     fcn.target = me;
857                     fcn.method = method;
858                     return (fcn.apply(scope || me || window, args) !== false) ?
859                             method.apply(me || window, args) :
860                             null;
861                 };
862     },
863
864      /**
865      * Creates a callback that passes arguments[0], arguments[1], arguments[2], ...
866      * Call directly on any function. Example: <code>myFunction.createCallback(arg1, arg2)</code>
867      * Will create a function that is bound to those 2 args. <b>If a specific scope is required in the
868      * callback, use {@link #createDelegate} instead.</b> The function returned by createCallback always
869      * executes in the window scope.
870      * <p>This method is required when you want to pass arguments to a callback function.  If no arguments
871      * are needed, you can simply pass a reference to the function as a callback (e.g., callback: myFn).
872      * However, if you tried to pass a function with arguments (e.g., callback: myFn(arg1, arg2)) the function
873      * would simply execute immediately when the code is parsed. Example usage:
874      * <pre><code>
875 var sayHi = function(name){
876     alert('Hi, ' + name);
877 }
878
879 // clicking the button alerts "Hi, Fred"
880 new Ext.Button({
881     text: 'Say Hi',
882     renderTo: Ext.getBody(),
883     handler: sayHi.createCallback('Fred')
884 });
885 </code></pre>
886      * @return {Function} The new function
887     */
888     createCallback : function(/*args...*/){
889         // make args available, in function below
890         var args = arguments,
891             method = this;
892         return function() {
893             return method.apply(window, args);
894         };
895     },
896
897     /**
898      * Creates a delegate (callback) that sets the scope to obj.
899      * Call directly on any function. Example: <code>this.myFunction.createDelegate(this, [arg1, arg2])</code>
900      * Will create a function that is automatically scoped to obj so that the <tt>this</tt> variable inside the
901      * callback points to obj. Example usage:
902      * <pre><code>
903 var sayHi = function(name){
904     // Note this use of "this.text" here.  This function expects to
905     // execute within a scope that contains a text property.  In this
906     // example, the "this" variable is pointing to the btn object that
907     // was passed in createDelegate below.
908     alert('Hi, ' + name + '. You clicked the "' + this.text + '" button.');
909 }
910
911 var btn = new Ext.Button({
912     text: 'Say Hi',
913     renderTo: Ext.getBody()
914 });
915
916 // This callback will execute in the scope of the
917 // button instance. Clicking the button alerts
918 // "Hi, Fred. You clicked the "Say Hi" button."
919 btn.on('click', sayHi.createDelegate(btn, ['Fred']));
920 </code></pre>
921      * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
922      * <b>If omitted, defaults to the browser window.</b>
923      * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
924      * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
925      * if a number the args are inserted at the specified position
926      * @return {Function} The new function
927      */
928     createDelegate : function(obj, args, appendArgs){
929         var method = this;
930         return function() {
931             var callArgs = args || arguments;
932             if (appendArgs === true){
933                 callArgs = Array.prototype.slice.call(arguments, 0);
934                 callArgs = callArgs.concat(args);
935             }else if (Ext.isNumber(appendArgs)){
936                 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
937                 var applyArgs = [appendArgs, 0].concat(args); // create method call params
938                 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
939             }
940             return method.apply(obj || window, callArgs);
941         };
942     },
943
944     /**
945      * Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
946      * <pre><code>
947 var sayHi = function(name){
948     alert('Hi, ' + name);
949 }
950
951 // executes immediately:
952 sayHi('Fred');
953
954 // executes after 2 seconds:
955 sayHi.defer(2000, this, ['Fred']);
956
957 // this syntax is sometimes useful for deferring
958 // execution of an anonymous function:
959 (function(){
960     alert('Anonymous');
961 }).defer(100);
962 </code></pre>
963      * @param {Number} millis The number of milliseconds for the setTimeout call (if less than or equal to 0 the function is executed immediately)
964      * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
965      * <b>If omitted, defaults to the browser window.</b>
966      * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
967      * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
968      * if a number the args are inserted at the specified position
969      * @return {Number} The timeout id that can be used with clearTimeout
970      */
971     defer : function(millis, obj, args, appendArgs){
972         var fn = this.createDelegate(obj, args, appendArgs);
973         if(millis > 0){
974             return setTimeout(fn, millis);
975         }
976         fn();
977         return 0;
978     }
979 });
980
981 /**
982  * @class String
983  * These functions are available on every String object.
984  */
985 Ext.applyIf(String, {
986     /**
987      * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens.  Each
988      * token must be unique, and must increment in the format {0}, {1}, etc.  Example usage:
989      * <pre><code>
990 var cls = 'my-class', text = 'Some text';
991 var s = String.format('&lt;div class="{0}">{1}&lt;/div>', cls, text);
992 // s now contains the string: '&lt;div class="my-class">Some text&lt;/div>'
993      * </code></pre>
994      * @param {String} string The tokenized string to be formatted
995      * @param {String} value1 The value to replace token {0}
996      * @param {String} value2 Etc...
997      * @return {String} The formatted string
998      * @static
999      */
1000     format : function(format){
1001         var args = Ext.toArray(arguments, 1);
1002         return format.replace(/\{(\d+)\}/g, function(m, i){
1003             return args[i];
1004         });
1005     }
1006 });
1007
1008 /**
1009  * @class Array
1010  */
1011 Ext.applyIf(Array.prototype, {
1012     /**
1013      * Checks whether or not the specified object exists in the array.
1014      * @param {Object} o The object to check for
1015      * @param {Number} from (Optional) The index at which to begin the search
1016      * @return {Number} The index of o in the array (or -1 if it is not found)
1017      */
1018     indexOf : function(o, from){
1019         var len = this.length;
1020         from = from || 0;
1021         from += (from < 0) ? len : 0;
1022         for (; from < len; ++from){
1023             if(this[from] === o){
1024                 return from;
1025             }
1026         }
1027         return -1;
1028     },
1029
1030     /**
1031      * Removes the specified object from the array.  If the object is not found nothing happens.
1032      * @param {Object} o The object to remove
1033      * @return {Array} this array
1034      */
1035     remove : function(o){
1036         var index = this.indexOf(o);
1037         if(index != -1){
1038             this.splice(index, 1);
1039         }
1040         return this;
1041     }
1042 });