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,
64 isTouch = 'ontouchstart' in window || window.DocumentTouch && document instanceof DocumentTouch;
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
89 * Turn on debugging output (currently only the factory uses this)
96 * True to automatically uncache orphaned Roo.Elements periodically (defaults to true)
99 enableGarbageCollector : true,
102 * True to automatically purge event listeners after uncaching an element (defaults to false).
103 * Note: this only happens if enableGarbageCollector is true.
106 enableListenerCollection:false,
109 * URL to a blank file used by Roo when in secure mode for iframe src and onReady src to prevent
110 * the IE insecure content warning (defaults to javascript:false).
113 SSL_SECURE_URL : "javascript:false",
116 * URL to a 1x1 transparent gif image used by Roo to create inline icons with CSS background images. (Defaults to
117 * "http://Roojs.com/s.gif" and you should change this to a URL on your server).
120 BLANK_IMAGE_URL : "http:/"+"/localhost/s.gif",
122 emptyFn : function(){},
125 * Copies all the properties of config to obj if they don't already exist.
126 * @param {Object} obj The receiver of the properties
127 * @param {Object} config The source of the properties
128 * @return {Object} returns obj
130 applyIf : function(o, c){
133 if(typeof o[p] == "undefined"){ o[p] = c[p]; }
140 * Applies event listeners to elements by selectors when the document is ready.
141 * The event name is specified with an @ suffix.
144 // add a listener for click on all anchors in element with id foo
145 '#foo a@click' : function(e, t){
149 // add the same listener to multiple selectors (separated by comma BEFORE the @)
150 '#foo a, #bar span.some-class@mouseover' : function(){
155 * @param {Object} obj The list of behaviors to apply
157 addBehaviors : function(o){
159 Roo.onReady(function(){
164 var cache = {}; // simple cache for applying multiple behaviors to same selector does query multiple times
166 var parts = b.split('@');
167 if(parts[1]){ // for Object prototype breakers
170 cache[s] = Roo.select(s);
172 cache[s].on(parts[1], o[b]);
179 * Generates unique ids. If the element already has an id, it is unchanged
180 * @param {String/HTMLElement/Element} el (optional) The element to generate an id for
181 * @param {String} prefix (optional) Id prefix (defaults "Roo-gen")
182 * @return {String} The generated Id.
184 id : function(el, prefix){
185 prefix = prefix || "roo-gen";
187 var id = prefix + (++idSeed);
188 return el ? (el.id ? el.id : (el.id = id)) : id;
193 * Extends one class with another class and optionally overrides members with the passed literal. This class
194 * also adds the function "override()" to the class that can be used to override
195 * members on an instance.
196 * @param {Object} subclass The class inheriting the functionality
197 * @param {Object} superclass The class being extended
198 * @param {Object} overrides (optional) A literal with members
203 var io = function(o){
208 return function(sb, sp, overrides){
209 if(typeof sp == 'object'){ // eg. prototype, rather than function constructor..
212 sb = function(){sp.apply(this, arguments);};
214 var F = function(){}, sbp, spp = sp.prototype;
216 sbp = sb.prototype = new F();
220 if(spp.constructor == Object.prototype.constructor){
225 sb.override = function(o){
229 Roo.override(sb, overrides);
235 * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
237 Roo.override(MyClass, {
238 newMethod1: function(){
241 newMethod2: function(foo){
246 * @param {Object} origclass The class to override
247 * @param {Object} overrides The list of functions to add to origClass. This should be specified as an object literal
248 * containing one or more methods.
251 override : function(origclass, overrides){
253 var p = origclass.prototype;
254 for(var method in overrides){
255 p[method] = overrides[method];
260 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
262 Roo.namespace('Company', 'Company.data');
263 Company.Widget = function() { ... }
264 Company.data.CustomStore = function(config) { ... }
266 * @param {String} namespace1
267 * @param {String} namespace2
268 * @param {String} etc
271 namespace : function(){
272 var a=arguments, o=null, i, j, d, rt;
273 for (i=0; i<a.length; ++i) {
277 eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
278 for (j=1; j<d.length; ++j) {
279 o[d[j]]=o[d[j]] || {};
285 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
287 Roo.factory({ xns: Roo.data, xtype : 'Store', .....});
288 Roo.factory(conf, Roo.data);
290 * @param {String} classname
291 * @param {String} namespace (optional)
295 factory : function(c, ns)
297 // no xtype, no ns or c.xns - or forced off by c.xns
298 if (!c.xtype || (!ns && !c.xns) || (c.xns === false)) { // not enough info...
301 ns = c.xns ? c.xns : ns; // if c.xns is set, then use that..
302 if (c.constructor == ns[c.xtype]) {// already created...
305 Roo.log("Roo.Factory(" + c.xtype + ")");
307 if (Roo.debug) Roo.log("Roo.Factory(" + c.xtype + ")");
308 var ret = new ns[c.xtype](c);
312 c.xns = false; // prevent recursion..
316 * Logs to console if it can.
318 * @param {String|Object} string
323 if ((typeof(console) == 'undefined') || (typeof(console.log) == 'undefined')) {
330 * 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.
334 urlEncode : function(o){
340 var ov = o[key], k = Roo.encodeURIComponent(key);
341 var type = typeof ov;
342 if(type == 'undefined'){
344 }else if(type != "function" && type != "object"){
345 buf.push(k, "=", Roo.encodeURIComponent(ov), "&");
346 }else if(ov instanceof Array){
348 for(var i = 0, len = ov.length; i < len; i++) {
349 buf.push(k, "=", Roo.encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
360 * Safe version of encodeURIComponent
361 * @param {String} data
365 encodeURIComponent : function (data)
368 return encodeURIComponent(data);
369 } catch(e) {} // should be an uri encode error.
371 if (data == '' || data == null){
374 // http://stackoverflow.com/questions/2596483/unicode-and-uri-encoding-decoding-and-escaping-in-javascript
375 function nibble_to_hex(nibble){
376 var chars = '0123456789ABCDEF';
377 return chars.charAt(nibble);
379 data = data.toString();
381 for(var i=0; i<data.length; i++){
382 var c = data.charCodeAt(i);
383 var bs = new Array();
386 bs[0] = 0xF0 | ((c & 0x1C0000) >>> 18);
387 bs[1] = 0x80 | ((c & 0x3F000) >>> 12);
388 bs[2] = 0x80 | ((c & 0xFC0) >>> 6);
389 bs[3] = 0x80 | (c & 0x3F);
390 }else if (c > 0x800){
392 bs[0] = 0xE0 | ((c & 0xF000) >>> 12);
393 bs[1] = 0x80 | ((c & 0xFC0) >>> 6);
394 bs[2] = 0x80 | (c & 0x3F);
397 bs[0] = 0xC0 | ((c & 0x7C0) >>> 6);
398 bs[1] = 0x80 | (c & 0x3F);
403 for(var j=0; j<bs.length; j++){
405 var hex = nibble_to_hex((b & 0xF0) >>> 4)
406 + nibble_to_hex(b &0x0F);
415 * 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]}.
416 * @param {String} string
417 * @param {Boolean} overwrite (optional) Items of the same name will overwrite previous values instead of creating an an array (Defaults to false).
418 * @return {Object} A literal with members
420 urlDecode : function(string, overwrite){
421 if(!string || !string.length){
425 var pairs = string.split('&');
426 var pair, name, value;
427 for(var i = 0, len = pairs.length; i < len; i++){
428 pair = pairs[i].split('=');
429 name = decodeURIComponent(pair[0]);
430 value = decodeURIComponent(pair[1]);
431 if(overwrite !== true){
432 if(typeof obj[name] == "undefined"){
434 }else if(typeof obj[name] == "string"){
435 obj[name] = [obj[name]];
436 obj[name].push(value);
438 obj[name].push(value);
448 * Iterates an array calling the passed function with each item, stopping if your function returns false. If the
449 * passed array is not really an array, your function is called once with it.
450 * The supplied function is called with (Object item, Number index, Array allItems).
451 * @param {Array/NodeList/Mixed} array
452 * @param {Function} fn
453 * @param {Object} scope
455 each : function(array, fn, scope){
456 if(typeof array.length == "undefined" || typeof array == "string"){
459 for(var i = 0, len = array.length; i < len; i++){
460 if(fn.call(scope || array[i], array[i], i, array) === false){ return i; };
465 combine : function(){
466 var as = arguments, l = as.length, r = [];
467 for(var i = 0; i < l; i++){
469 if(a instanceof Array){
471 }else if(a.length !== undefined && !a.substr){
472 r = r.concat(Array.prototype.slice.call(a, 0));
481 * Escapes the passed string for use in a regular expression
482 * @param {String} str
485 escapeRe : function(s) {
486 return s.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1");
490 callback : function(cb, scope, args, delay){
491 if(typeof cb == "function"){
493 cb.defer(delay, scope, args || []);
495 cb.apply(scope, args || []);
501 * Return the dom node for the passed string (id), dom node, or Roo.Element
502 * @param {String/HTMLElement/Roo.Element} el
503 * @return HTMLElement
505 getDom : function(el){
509 return el.dom ? el.dom : (typeof el == 'string' ? document.getElementById(el) : el);
513 * Shorthand for {@link Roo.ComponentMgr#get}
515 * @return Roo.Component
517 getCmp : function(id){
518 return Roo.ComponentMgr.get(id);
521 num : function(v, defaultValue){
522 if(typeof v != 'number'){
528 destroy : function(){
529 for(var i = 0, a = arguments, len = a.length; i < len; i++) {
533 as.removeAllListeners();
537 if(typeof as.purgeListeners == 'function'){
540 if(typeof as.destroy == 'function'){
547 // inpired by a similar function in mootools library
549 * Returns the type of object that is passed in. If the object passed in is null or undefined it
550 * return false otherwise it returns one of the following values:<ul>
551 * <li><b>string</b>: If the object passed is a string</li>
552 * <li><b>number</b>: If the object passed is a number</li>
553 * <li><b>boolean</b>: If the object passed is a boolean value</li>
554 * <li><b>function</b>: If the object passed is a function reference</li>
555 * <li><b>object</b>: If the object passed is an object</li>
556 * <li><b>array</b>: If the object passed is an array</li>
557 * <li><b>regexp</b>: If the object passed is a regular expression</li>
558 * <li><b>element</b>: If the object passed is a DOM Element</li>
559 * <li><b>nodelist</b>: If the object passed is a DOM NodeList</li>
560 * <li><b>textnode</b>: If the object passed is a DOM text node and contains something other than whitespace</li>
561 * <li><b>whitespace</b>: If the object passed is a DOM text node and contains only whitespace</li>
562 * @param {Mixed} object
566 if(o === undefined || o === null){
573 if(t == 'object' && o.nodeName) {
575 case 1: return 'element';
576 case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
579 if(t == 'object' || t == 'function') {
580 switch(o.constructor) {
581 case Array: return 'array';
582 case RegExp: return 'regexp';
584 if(typeof o.length == 'number' && typeof o.item == 'function') {
592 * Returns true if the passed value is null, undefined or an empty string (optional).
593 * @param {Mixed} value The value to test
594 * @param {Boolean} allowBlank (optional) Pass true if an empty string is not considered empty
597 isEmpty : function(v, allowBlank){
598 return v === null || v === undefined || (!allowBlank ? v === '' : false);
612 isBorderBox : isBorderBox,
614 isWindows : isWindows,
623 * By default, Ext intelligently decides whether floating elements should be shimmed. If you are using flash,
624 * you may want to set this to true.
627 useShims : ((isIE && !isIE7) || (isGecko && isMac)),
632 * Selects a single element as a Roo Element
633 * This is about as close as you can get to jQuery's $('do crazy stuff')
634 * @param {String} selector The selector/xpath query
635 * @param {Node} root (optional) The start of the query (defaults to document).
636 * @return {Roo.Element}
638 selectNode : function(selector, root)
640 var node = Roo.DomQuery.selectNode(selector,root);
641 return node ? Roo.get(node) : new Roo.Element(false);
649 Roo.namespace("Roo", "Roo.util", "Roo.grid", "Roo.dd", "Roo.tree", "Roo.data",
650 "Roo.form", "Roo.menu", "Roo.state", "Roo.lib", "Roo.layout", "Roo.app", "Roo.ux");