3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
11 Ext.ns("Ext.grid", "Ext.list", "Ext.dd", "Ext.tree", "Ext.form", "Ext.menu",
12 "Ext.state", "Ext.layout", "Ext.app", "Ext.ux", "Ext.chart", "Ext.direct");
14 * Namespace alloted for extensions to the framework.
19 Ext.apply(Ext, function(){
26 * A reusable empty function
30 emptyFn : function(){},
33 * URL to a 1x1 transparent gif image used by Ext to create inline icons with CSS background images.
34 * In older versions of IE, this defaults to "http://extjs.com/s.gif" and you should change this to a URL on your server.
35 * For other browsers it uses an inline data URL.
38 BLANK_IMAGE_URL : Ext.isIE6 || Ext.isIE7 || Ext.isAir ?
39 'http:/' + '/www.extjs.com/s.gif' :
40 'data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==',
42 extendX : function(supr, fn){
43 return Ext.extend(supr, fn(supr.prototype));
47 * Returns the current HTML document object as an {@link Ext.Element}.
48 * @return Ext.Element The document
51 return Ext.get(document);
55 * Utility method for validating that a value is numeric, returning the specified default value if it is not.
56 * @param {Mixed} value Should be a number, but any type will be handled appropriately
57 * @param {Number} defaultValue The value to return if the original value is non-numeric
58 * @return {Number} Value, if numeric, else defaultValue
60 num : function(v, defaultValue){
61 v = Number(Ext.isEmpty(v) || Ext.isArray(v) || typeof v == 'boolean' || (typeof v == 'string' && v.trim().length == 0) ? NaN : v);
62 return isNaN(v) ? defaultValue : v;
66 * <p>Utility method for returning a default value if the passed value is empty.</p>
67 * <p>The value is deemed to be empty if it is<div class="mdetail-params"><ul>
70 * <li>an empty array</li>
71 * <li>a zero length string (Unless the <tt>allowBlank</tt> parameter is <tt>true</tt>)</li>
73 * @param {Mixed} value The value to test
74 * @param {Mixed} defaultValue The value to return if the original value is empty
75 * @param {Boolean} allowBlank (optional) true to allow zero length strings to qualify as non-empty (defaults to false)
76 * @return {Mixed} value, if non-empty, else defaultValue
78 value : function(v, defaultValue, allowBlank){
79 return Ext.isEmpty(v, allowBlank) ? defaultValue : v;
83 * Escapes the passed string for use in a regular expression
87 escapeRe : function(s) {
88 return s.replace(/([-.*+?^${}()|[\]\/\\])/g, "\\$1");
91 sequence : function(o, name, fn, scope){
92 o[name] = o[name].createSequence(fn, scope);
96 * Applies event listeners to elements by selectors when the document is ready.
97 * The event name is specified with an <tt>@</tt> suffix.
100 // add a listener for click on all anchors in element with id foo
101 '#foo a@click' : function(e, t){
105 // add the same listener to multiple selectors (separated by comma BEFORE the @)
106 '#foo a, #bar span.some-class@mouseover' : function(){
111 * @param {Object} obj The list of behaviors to apply
113 addBehaviors : function(o){
115 Ext.onReady(function(){
119 var cache = {}, // simple cache for applying multiple behaviors to same selector does query multiple times
124 if ((parts = b.split('@'))[1]) { // for Object prototype breakers
127 cache[s] = Ext.select(s);
129 cache[s].on(parts[1], o[b]);
137 * Utility method for getting the width of the browser scrollbar. This can differ depending on
138 * operating system settings, such as the theme or font size.
139 * @param {Boolean} force (optional) true to force a recalculation of the value.
140 * @return {Number} The width of the scrollbar.
142 getScrollBarWidth: function(force){
147 if(force === true || scrollWidth === null){
148 // Append our div, do our calculation and then remove it
149 var div = Ext.getBody().createChild('<div class="x-hide-offsets" style="width:100px;height:50px;overflow:hidden;"><div style="height:200px;"></div></div>'),
150 child = div.child('div', true);
151 var w1 = child.offsetWidth;
152 div.setStyle('overflow', (Ext.isWebKit || Ext.isGecko) ? 'auto' : 'scroll');
153 var w2 = child.offsetWidth;
155 // Need to add 2 to ensure we leave enough space
156 scrollWidth = w1 - w2 + 2;
163 combine : function(){
164 var as = arguments, l = as.length, r = [];
165 for(var i = 0; i < l; i++){
169 }else if(a.length !== undefined && !a.substr){
170 r = r.concat(Array.prototype.slice.call(a, 0));
179 * Copies a set of named properties fom the source object to the destination object.
180 * <p>example:<pre><code>
181 ImageComponent = Ext.extend(Ext.BoxComponent, {
182 initComponent: function() {
183 this.autoEl = { tag: 'img' };
184 MyComponent.superclass.initComponent.apply(this, arguments);
185 this.initialBox = Ext.copyTo({}, this.initialConfig, 'x,y,width,height');
189 * @param {Object} dest The destination object.
190 * @param {Object} source The source object.
191 * @param {Array/String} names Either an Array of property names, or a comma-delimited list
192 * of property names to copy.
193 * @return {Object} The modified object.
195 copyTo : function(dest, source, names){
196 if(typeof names == 'string'){
197 names = names.split(/[,;\s]/);
199 Ext.each(names, function(name){
200 if(source.hasOwnProperty(name)){
201 dest[name] = source[name];
208 * Attempts to destroy any objects passed to it by removing all event listeners, removing them from the
209 * DOM (if applicable) and calling their destroy functions (if available). This method is primarily
210 * intended for arguments of type {@link Ext.Element} and {@link Ext.Component}, but any subclass of
211 * {@link Ext.util.Observable} can be passed in. Any number of elements and/or components can be
212 * passed into this function in a single call as separate arguments.
213 * @param {Mixed} arg1 An {@link Ext.Element}, {@link Ext.Component}, or an Array of either of these to destroy
214 * @param {Mixed} arg2 (optional)
215 * @param {Mixed} etc... (optional)
217 destroy : function(){
218 Ext.each(arguments, function(arg){
220 if(Ext.isArray(arg)){
221 this.destroy.apply(this, arg);
222 }else if(typeof arg.destroy == 'function'){
232 * Attempts to destroy and then remove a set of named properties of the passed object.
233 * @param {Object} o The object (most likely a Component) who's properties you wish to destroy.
234 * @param {Mixed} arg1 The name of the property to destroy and remove from the object.
235 * @param {Mixed} etc... More property names to destroy and remove.
237 destroyMembers : function(o, arg1, arg2, etc){
238 for(var i = 1, a = arguments, len = a.length; i < len; i++) {
239 Ext.destroy(o[a[i]]);
245 * Creates a copy of the passed Array with falsy values removed.
246 * @param {Array/NodeList} arr The Array from which to remove falsy values.
247 * @return {Array} The new, compressed Array.
249 clean : function(arr){
251 Ext.each(arr, function(v){
260 * Creates a copy of the passed Array, filtered to contain only unique values.
261 * @param {Array} arr The Array to filter
262 * @return {Array} The new Array containing unique values.
264 unique : function(arr){
268 Ext.each(arr, function(v) {
278 * Recursively flattens into 1-d Array. Injects Arrays inline.
279 * @param {Array} arr The array to flatten
280 * @return {Array} The new, flattened array.
282 flatten : function(arr){
284 function rFlatten(a) {
285 Ext.each(a, function(v) {
294 return rFlatten(arr);
298 * Returns the minimum value in the Array.
299 * @param {Array|NodeList} arr The Array from which to select the minimum value.
300 * @param {Function} comp (optional) a function to perform the comparision which determines minimization.
301 * If omitted the "<" operator will be used. Note: gt = 1; eq = 0; lt = -1
302 * @return {Object} The minimum value in the Array.
304 min : function(arr, comp){
306 comp = comp || function(a,b){ return a < b ? -1 : 1; };
307 Ext.each(arr, function(v) {
308 ret = comp(ret, v) == -1 ? ret : v;
314 * Returns the maximum value in the Array
315 * @param {Array|NodeList} arr The Array from which to select the maximum value.
316 * @param {Function} comp (optional) a function to perform the comparision which determines maximization.
317 * If omitted the ">" operator will be used. Note: gt = 1; eq = 0; lt = -1
318 * @return {Object} The maximum value in the Array.
320 max : function(arr, comp){
322 comp = comp || function(a,b){ return a > b ? 1 : -1; };
323 Ext.each(arr, function(v) {
324 ret = comp(ret, v) == 1 ? ret : v;
330 * Calculates the mean of the Array
331 * @param {Array} arr The Array to calculate the mean value of.
332 * @return {Number} The mean.
334 mean : function(arr){
335 return arr.length > 0 ? Ext.sum(arr) / arr.length : undefined;
339 * Calculates the sum of the Array
340 * @param {Array} arr The Array to calculate the sum value of.
341 * @return {Number} The sum.
345 Ext.each(arr, function(v) {
352 * Partitions the set into two sets: a true set and a false set.
357 Ext.partition([true, false, true, true, false]); // [[true, true, true], [false, false]]
363 return val.className == "class1"
366 // true are those paragraph elements with a className of "class1",
367 // false set are those that do not have that className.
369 * @param {Array|NodeList} arr The array to partition
370 * @param {Function} truth (optional) a function to determine truth. If this is omitted the element
371 * itself must be able to be evaluated for its truthfulness.
372 * @return {Array} [true<Array>,false<Array>]
374 partition : function(arr, truth){
376 Ext.each(arr, function(v, i, a) {
377 ret[ (truth && truth(v, i, a)) || (!truth && v) ? 0 : 1].push(v);
383 * Invokes a method on each item in an Array.
386 Ext.invoke(Ext.query("p"), "getAttribute", "id");
387 // [el1.getAttribute("id"), el2.getAttribute("id"), ..., elN.getAttribute("id")]
389 * @param {Array|NodeList} arr The Array of items to invoke the method on.
390 * @param {String} methodName The method name to invoke.
391 * @param {...*} args Arguments to send into the method invocation.
392 * @return {Array} The results of invoking the method on each item in the array.
394 invoke : function(arr, methodName){
396 args = Array.prototype.slice.call(arguments, 2);
397 Ext.each(arr, function(v,i) {
398 if (v && typeof v[methodName] == 'function') {
399 ret.push(v[methodName].apply(v, args));
408 * Plucks the value of a property from each item in the Array
411 Ext.pluck(Ext.query("p"), "className"); // [el1.className, el2.className, ..., elN.className]
413 * @param {Array|NodeList} arr The Array of items to pluck the value from.
414 * @param {String} prop The property name to pluck from each element.
415 * @return {Array} The value from each item in the Array.
417 pluck : function(arr, prop){
419 Ext.each(arr, function(v) {
426 * <p>Zips N sets together.</p>
429 Ext.zip([1,2,3],[4,5,6]); // [[1,4],[2,5],[3,6]]
436 return "$" + a + "" + b + "." + c
438 ); // ["$+12.43", "$-10.15", "$+22.96"]
440 * @param {Arrays|NodeLists} arr This argument may be repeated. Array(s) to contribute values.
441 * @param {Function} zipper (optional) The last item in the argument list. This will drive how the items are zipped together.
442 * @return {Array} The zipped set.
445 var parts = Ext.partition(arguments, function( val ){ return typeof val != 'function'; }),
448 len = Ext.max(Ext.pluck(arrs, "length")),
451 for (var i = 0; i < len; i++) {
454 ret[i] = fn.apply(fn, Ext.pluck(arrs, i));
456 for (var j = 0, aLen = arrs.length; j < aLen; j++){
457 ret[i].push( arrs[j][i] );
465 * This is shorthand reference to {@link Ext.ComponentMgr#get}.
466 * Looks up an existing {@link Ext.Component Component} by {@link Ext.Component#id id}
467 * @param {String} id The component {@link Ext.Component#id id}
468 * @return Ext.Component The Component, <tt>undefined</tt> if not found, or <tt>null</tt> if a
471 getCmp : function(id){
472 return Ext.ComponentMgr.get(id);
476 * By default, Ext intelligently decides whether floating elements should be shimmed. If you are using flash,
477 * you may want to set this to true.
480 useShims: E.isIE6 || (E.isMac && E.isGecko2),
482 // inpired by a similar function in mootools library
484 * Returns the type of object that is passed in. If the object passed in is null or undefined it
485 * return false otherwise it returns one of the following values:<div class="mdetail-params"><ul>
486 * <li><b>string</b>: If the object passed is a string</li>
487 * <li><b>number</b>: If the object passed is a number</li>
488 * <li><b>boolean</b>: If the object passed is a boolean value</li>
489 * <li><b>date</b>: If the object passed is a Date object</li>
490 * <li><b>function</b>: If the object passed is a function reference</li>
491 * <li><b>object</b>: If the object passed is an object</li>
492 * <li><b>array</b>: If the object passed is an array</li>
493 * <li><b>regexp</b>: If the object passed is a regular expression</li>
494 * <li><b>element</b>: If the object passed is a DOM Element</li>
495 * <li><b>nodelist</b>: If the object passed is a DOM NodeList</li>
496 * <li><b>textnode</b>: If the object passed is a DOM text node and contains something other than whitespace</li>
497 * <li><b>whitespace</b>: If the object passed is a DOM text node and contains only whitespace</li>
499 * @param {Mixed} object
503 if(o === undefined || o === null){
510 if(t == 'object' && o.nodeName) {
512 case 1: return 'element';
513 case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
516 if(t == 'object' || t == 'function') {
517 switch(o.constructor) {
518 case Array: return 'array';
519 case RegExp: return 'regexp';
520 case Date: return 'date';
522 if(typeof o.length == 'number' && typeof o.item == 'function') {
529 intercept : function(o, name, fn, scope){
530 o[name] = o[name].createInterceptor(fn, scope);
534 callback : function(cb, scope, args, delay){
535 if(typeof cb == 'function'){
537 cb.defer(delay, scope, args || []);
539 cb.apply(scope, args || []);
548 * These functions are available on every Function object (any JavaScript function).
550 Ext.apply(Function.prototype, {
552 * Create a combined function call sequence of the original function + the passed function.
553 * The resulting function returns the results of the original function.
554 * The passed fcn is called with the parameters of the original function. Example usage:
556 var sayHi = function(name){
557 alert('Hi, ' + name);
560 sayHi('Fred'); // alerts "Hi, Fred"
562 var sayGoodbye = sayHi.createSequence(function(name){
563 alert('Bye, ' + name);
566 sayGoodbye('Fred'); // both alerts show
568 * @param {Function} fcn The function to sequence
569 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the passed function is executed.
570 * <b>If omitted, defaults to the scope in which the original function is called or the browser window.</b>
571 * @return {Function} The new function
573 createSequence : function(fcn, scope){
575 return (typeof fcn != 'function') ?
578 var retval = method.apply(this || window, arguments);
579 fcn.apply(scope || this || window, arguments);
588 * These functions are available as static methods on the JavaScript String object.
590 Ext.applyIf(String, {
593 * Escapes the passed string for ' and \
594 * @param {String} string The string to escape
595 * @return {String} The escaped string
598 escape : function(string) {
599 return string.replace(/('|\\)/g, "\\$1");
603 * Pads the left side of a string with a specified character. This is especially useful
604 * for normalizing number and date strings. Example usage:
606 var s = String.leftPad('123', 5, '0');
607 // s now contains the string: '00123'
609 * @param {String} string The original string
610 * @param {Number} size The total length of the output string
611 * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
612 * @return {String} The padded string
615 leftPad : function (val, size, ch) {
616 var result = String(val);
620 while (result.length < size) {
621 result = ch + result;
628 * Utility function that allows you to easily switch a string between two alternating values. The passed value
629 * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
630 * they are already different, the first value passed in is returned. Note that this method returns the new value
631 * but does not change the current string.
633 // alternate sort directions
634 sort = sort.toggle('ASC', 'DESC');
636 // instead of conditional logic:
637 sort = (sort == 'ASC' ? 'DESC' : 'ASC');
639 * @param {String} value The value to compare to the current string
640 * @param {String} other The new value to use if the string already equals the first value passed in
641 * @return {String} The new value
643 String.prototype.toggle = function(value, other){
644 return this == value ? other : value;
648 * Trims whitespace from either end of a string, leaving spaces within the string intact. Example:
651 alert('-' + s + '-'); //alerts "- foo bar -"
652 alert('-' + s.trim() + '-'); //alerts "-foo bar-"
654 * @return {String} The trimmed string
656 String.prototype.trim = function(){
657 var re = /^\s+|\s+$/g;
658 return function(){ return this.replace(re, ""); };
661 // here to prevent dependency on Date.js
663 Returns the number of milliseconds between this date and date
664 @param {Date} date (optional) Defaults to now
665 @return {Number} The diff in milliseconds
666 @member Date getElapsed
668 Date.prototype.getElapsed = function(date) {
669 return Math.abs((date || new Date()).getTime()-this.getTime());
676 Ext.applyIf(Number.prototype, {
678 * Checks whether or not the current number is within a desired range. If the number is already within the
679 * range it is returned, otherwise the min or max value is returned depending on which side of the range is
680 * exceeded. Note that this method returns the constrained value but does not change the current number.
681 * @param {Number} min The minimum number in the range
682 * @param {Number} max The maximum number in the range
683 * @return {Number} The constrained value if outside the range, otherwise the current value
685 constrain : function(min, max){
686 return Math.min(Math.max(this, min), max);