4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
17 window["undefined"] = window["undefined"];
21 * Roo core utilities and functions.
26 * Copies all the properties of config to obj.
27 * @param {Object} obj The receiver of the properties
28 * @param {Object} config The source of the properties
29 * @param {Object} defaults A different object that will also be applied for default values
30 * @return {Object} returns obj
35 Roo.apply = function(o, c, defaults){
37 // no "this" reference for friendly out of scope calls
38 Roo.apply(o, defaults);
40 if(o && c && typeof c == 'object'){
51 var ua = navigator.userAgent.toLowerCase();
53 var isStrict = document.compatMode == "CSS1Compat",
54 isOpera = ua.indexOf("opera") > -1,
55 isSafari = (/webkit|khtml/).test(ua),
56 isIE = ua.indexOf("msie") > -1,
57 isIE7 = ua.indexOf("msie 7") > -1,
58 isGecko = !isSafari && ua.indexOf("gecko") > -1,
59 isBorderBox = isIE && !isStrict,
60 isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1),
61 isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1),
62 isLinux = (ua.indexOf("linux") != -1),
63 isSecure = window.location.href.toLowerCase().indexOf("https") === 0;
65 // remove css image flicker
68 document.execCommand("BackgroundImageCache", false, true);
74 * True if the browser is in strict mode
79 * True if the page is running over SSL
84 * True when the document is fully initialized and ready for action
90 * True to automatically uncache orphaned Roo.Elements periodically (defaults to true)
93 enableGarbageCollector : true,
96 * True to automatically purge event listeners after uncaching an element (defaults to false).
97 * Note: this only happens if enableGarbageCollector is true.
100 enableListenerCollection:false,
103 * URL to a blank file used by Roo when in secure mode for iframe src and onReady src to prevent
104 * the IE insecure content warning (defaults to javascript:false).
107 SSL_SECURE_URL : "javascript:false",
110 * URL to a 1x1 transparent gif image used by Roo to create inline icons with CSS background images. (Defaults to
111 * "http://Roojs.com/s.gif" and you should change this to a URL on your server).
114 BLANK_IMAGE_URL : "http:/"+"/localhost/s.gif",
116 emptyFn : function(){},
119 * Copies all the properties of config to obj if they don't already exist.
120 * @param {Object} obj The receiver of the properties
121 * @param {Object} config The source of the properties
122 * @return {Object} returns obj
124 applyIf : function(o, c){
127 if(typeof o[p] == "undefined"){ o[p] = c[p]; }
134 * Applies event listeners to elements by selectors when the document is ready.
135 * The event name is specified with an @ suffix.
138 // add a listener for click on all anchors in element with id foo
139 '#foo a@click' : function(e, t){
143 // add the same listener to multiple selectors (separated by comma BEFORE the @)
144 '#foo a, #bar span.some-class@mouseover' : function(){
149 * @param {Object} obj The list of behaviors to apply
151 addBehaviors : function(o){
153 Roo.onReady(function(){
158 var cache = {}; // simple cache for applying multiple behaviors to same selector does query multiple times
160 var parts = b.split('@');
161 if(parts[1]){ // for Object prototype breakers
164 cache[s] = Roo.select(s);
166 cache[s].on(parts[1], o[b]);
173 * Generates unique ids. If the element already has an id, it is unchanged
174 * @param {String/HTMLElement/Element} el (optional) The element to generate an id for
175 * @param {String} prefix (optional) Id prefix (defaults "Roo-gen")
176 * @return {String} The generated Id.
178 id : function(el, prefix){
179 prefix = prefix || "roo-gen";
181 var id = prefix + (++idSeed);
182 return el ? (el.id ? el.id : (el.id = id)) : id;
187 * Extends one class with another class and optionally overrides members with the passed literal. This class
188 * also adds the function "override()" to the class that can be used to override
189 * members on an instance.
190 * @param {Object} subclass The class inheriting the functionality
191 * @param {Object} superclass The class being extended
192 * @param {Object} overrides (optional) A literal with members
197 var io = function(o){
202 return function(sb, sp, overrides){
203 if(typeof sp == 'object'){ // eg. prototype, rather than function constructor..
206 sb = function(){sp.apply(this, arguments);};
208 var F = function(){}, sbp, spp = sp.prototype;
210 sbp = sb.prototype = new F();
214 if(spp.constructor == Object.prototype.constructor){
219 sb.override = function(o){
223 Roo.override(sb, overrides);
229 * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
231 Roo.override(MyClass, {
232 newMethod1: function(){
235 newMethod2: function(foo){
240 * @param {Object} origclass The class to override
241 * @param {Object} overrides The list of functions to add to origClass. This should be specified as an object literal
242 * containing one or more methods.
245 override : function(origclass, overrides){
247 var p = origclass.prototype;
248 for(var method in overrides){
249 p[method] = overrides[method];
254 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
256 Roo.namespace('Company', 'Company.data');
257 Company.Widget = function() { ... }
258 Company.data.CustomStore = function(config) { ... }
260 * @param {String} namespace1
261 * @param {String} namespace2
262 * @param {String} etc
265 namespace : function(){
266 var a=arguments, o=null, i, j, d, rt;
267 for (i=0; i<a.length; ++i) {
271 eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
272 for (j=1; j<d.length; ++j) {
273 o[d[j]]=o[d[j]] || {};
279 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
281 Roo.factory({ xns: Roo.data, xtype : 'Store', .....});
282 Roo.factory(conf, Roo.data);
284 * @param {String} classname
285 * @param {String} namespace (optional)
289 factory : function(c, ns)
291 // no xtype, no ns or c.xns - or forced off by c.xns
292 if (!c.xtype || (!ns && !c.xns) || (c.xns === false)) { // not enough info...
295 ns = c.xns ? c.xns : ns; // if c.xns is set, then use that..
296 if (c.constructor == ns[c.xtype]) {// already created...
300 if (Roo.debug) console.log("Roo.Factory(" + c.xtype + ")");
301 var ret = new ns[c.xtype](c);
305 c.xns = false; // prevent recursion..
309 * Logs to console if it can.
311 * @param {String|Object} string
316 if ((typeof(console) == 'undefined') || typeof(console.log) == 'undefined')) {
323 * Takes an object and converts it to an encoded URL. e.g. Roo.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.
327 urlEncode : function(o){
333 var ov = o[key], k = encodeURIComponent(key);
334 var type = typeof ov;
335 if(type == 'undefined'){
337 }else if(type != "function" && type != "object"){
338 buf.push(k, "=", encodeURIComponent(ov), "&");
339 }else if(ov instanceof Array){
341 for(var i = 0, len = ov.length; i < len; i++) {
342 buf.push(k, "=", encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
354 * Takes an encoded URL and and converts it to an object. e.g. Roo.urlDecode("foo=1&bar=2"); would return {foo: 1, bar: 2} or Roo.urlDecode("foo=1&bar=2&bar=3&bar=4", true); would return {foo: 1, bar: [2, 3, 4]}.
355 * @param {String} string
356 * @param {Boolean} overwrite (optional) Items of the same name will overwrite previous values instead of creating an an array (Defaults to false).
357 * @return {Object} A literal with members
359 urlDecode : function(string, overwrite){
360 if(!string || !string.length){
364 var pairs = string.split('&');
365 var pair, name, value;
366 for(var i = 0, len = pairs.length; i < len; i++){
367 pair = pairs[i].split('=');
368 name = decodeURIComponent(pair[0]);
369 value = decodeURIComponent(pair[1]);
370 if(overwrite !== true){
371 if(typeof obj[name] == "undefined"){
373 }else if(typeof obj[name] == "string"){
374 obj[name] = [obj[name]];
375 obj[name].push(value);
377 obj[name].push(value);
387 * Iterates an array calling the passed function with each item, stopping if your function returns false. If the
388 * passed array is not really an array, your function is called once with it.
389 * The supplied function is called with (Object item, Number index, Array allItems).
390 * @param {Array/NodeList/Mixed} array
391 * @param {Function} fn
392 * @param {Object} scope
394 each : function(array, fn, scope){
395 if(typeof array.length == "undefined" || typeof array == "string"){
398 for(var i = 0, len = array.length; i < len; i++){
399 if(fn.call(scope || array[i], array[i], i, array) === false){ return i; };
404 combine : function(){
405 var as = arguments, l = as.length, r = [];
406 for(var i = 0; i < l; i++){
408 if(a instanceof Array){
410 }else if(a.length !== undefined && !a.substr){
411 r = r.concat(Array.prototype.slice.call(a, 0));
420 * Escapes the passed string for use in a regular expression
421 * @param {String} str
424 escapeRe : function(s) {
425 return s.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1");
429 callback : function(cb, scope, args, delay){
430 if(typeof cb == "function"){
432 cb.defer(delay, scope, args || []);
434 cb.apply(scope, args || []);
440 * Return the dom node for the passed string (id), dom node, or Roo.Element
441 * @param {String/HTMLElement/Roo.Element} el
442 * @return HTMLElement
444 getDom : function(el){
448 return el.dom ? el.dom : (typeof el == 'string' ? document.getElementById(el) : el);
452 * Shorthand for {@link Roo.ComponentMgr#get}
454 * @return Roo.Component
456 getCmp : function(id){
457 return Roo.ComponentMgr.get(id);
460 num : function(v, defaultValue){
461 if(typeof v != 'number'){
467 destroy : function(){
468 for(var i = 0, a = arguments, len = a.length; i < len; i++) {
472 as.removeAllListeners();
476 if(typeof as.purgeListeners == 'function'){
479 if(typeof as.destroy == 'function'){
486 // inpired by a similar function in mootools library
488 * Returns the type of object that is passed in. If the object passed in is null or undefined it
489 * return false otherwise it returns one of the following values:<ul>
490 * <li><b>string</b>: If the object passed is a string</li>
491 * <li><b>number</b>: If the object passed is a number</li>
492 * <li><b>boolean</b>: If the object passed is a boolean value</li>
493 * <li><b>function</b>: If the object passed is a function reference</li>
494 * <li><b>object</b>: If the object passed is an object</li>
495 * <li><b>array</b>: If the object passed is an array</li>
496 * <li><b>regexp</b>: If the object passed is a regular expression</li>
497 * <li><b>element</b>: If the object passed is a DOM Element</li>
498 * <li><b>nodelist</b>: If the object passed is a DOM NodeList</li>
499 * <li><b>textnode</b>: If the object passed is a DOM text node and contains something other than whitespace</li>
500 * <li><b>whitespace</b>: If the object passed is a DOM text node and contains only whitespace</li>
501 * @param {Mixed} object
505 if(o === undefined || o === null){
512 if(t == 'object' && o.nodeName) {
514 case 1: return 'element';
515 case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
518 if(t == 'object' || t == 'function') {
519 switch(o.constructor) {
520 case Array: return 'array';
521 case RegExp: return 'regexp';
523 if(typeof o.length == 'number' && typeof o.item == 'function') {
531 * Returns true if the passed value is null, undefined or an empty string (optional).
532 * @param {Mixed} value The value to test
533 * @param {Boolean} allowBlank (optional) Pass true if an empty string is not considered empty
536 isEmpty : function(v, allowBlank){
537 return v === null || v === undefined || (!allowBlank ? v === '' : false);
551 isBorderBox : isBorderBox,
553 isWindows : isWindows,
560 * By default, Ext intelligently decides whether floating elements should be shimmed. If you are using flash,
561 * you may want to set this to true.
564 useShims : ((isIE && !isIE7) || (isGecko && isMac))
570 Roo.namespace("Roo", "Roo.util", "Roo.grid", "Roo.dd", "Roo.tree", "Roo.data",
571 "Roo.form", "Roo.menu", "Roo.state", "Roo.lib", "Roo.layout", "Roo.app", "Roo.ux");