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 = (function() {
66 document.createEvent("TouchEvent");
73 // remove css image flicker
76 document.execCommand("BackgroundImageCache", false, true);
82 * True if the browser is in strict mode
87 * True if the page is running over SSL
92 * True when the document is fully initialized and ready for action
97 * Turn on debugging output (currently only the factory uses this)
104 * True to automatically uncache orphaned Roo.Elements periodically (defaults to true)
107 enableGarbageCollector : true,
110 * True to automatically purge event listeners after uncaching an element (defaults to false).
111 * Note: this only happens if enableGarbageCollector is true.
114 enableListenerCollection:false,
117 * URL to a blank file used by Roo when in secure mode for iframe src and onReady src to prevent
118 * the IE insecure content warning (defaults to javascript:false).
121 SSL_SECURE_URL : "javascript:false",
124 * URL to a 1x1 transparent gif image used by Roo to create inline icons with CSS background images. (Defaults to
125 * "http://Roojs.com/s.gif" and you should change this to a URL on your server).
128 BLANK_IMAGE_URL : "http:/"+"/localhost/s.gif",
130 emptyFn : function(){},
133 * Copies all the properties of config to obj if they don't already exist.
134 * @param {Object} obj The receiver of the properties
135 * @param {Object} config The source of the properties
136 * @return {Object} returns obj
138 applyIf : function(o, c){
141 if(typeof o[p] == "undefined"){ o[p] = c[p]; }
148 * Applies event listeners to elements by selectors when the document is ready.
149 * The event name is specified with an @ suffix.
152 // add a listener for click on all anchors in element with id foo
153 '#foo a@click' : function(e, t){
157 // add the same listener to multiple selectors (separated by comma BEFORE the @)
158 '#foo a, #bar span.some-class@mouseover' : function(){
163 * @param {Object} obj The list of behaviors to apply
165 addBehaviors : function(o){
167 Roo.onReady(function(){
172 var cache = {}; // simple cache for applying multiple behaviors to same selector does query multiple times
174 var parts = b.split('@');
175 if(parts[1]){ // for Object prototype breakers
178 cache[s] = Roo.select(s);
180 cache[s].on(parts[1], o[b]);
187 * Generates unique ids. If the element already has an id, it is unchanged
188 * @param {String/HTMLElement/Element} el (optional) The element to generate an id for
189 * @param {String} prefix (optional) Id prefix (defaults "Roo-gen")
190 * @return {String} The generated Id.
192 id : function(el, prefix){
193 prefix = prefix || "roo-gen";
195 var id = prefix + (++idSeed);
196 return el ? (el.id ? el.id : (el.id = id)) : id;
201 * Extends one class with another class and optionally overrides members with the passed literal. This class
202 * also adds the function "override()" to the class that can be used to override
203 * members on an instance.
204 * @param {Object} subclass The class inheriting the functionality
205 * @param {Object} superclass The class being extended
206 * @param {Object} overrides (optional) A literal with members
211 var io = function(o){
216 return function(sb, sp, overrides){
217 if(typeof sp == 'object'){ // eg. prototype, rather than function constructor..
220 sb = function(){sp.apply(this, arguments);};
222 var F = function(){}, sbp, spp = sp.prototype;
224 sbp = sb.prototype = new F();
228 if(spp.constructor == Object.prototype.constructor){
233 sb.override = function(o){
237 Roo.override(sb, overrides);
243 * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
245 Roo.override(MyClass, {
246 newMethod1: function(){
249 newMethod2: function(foo){
254 * @param {Object} origclass The class to override
255 * @param {Object} overrides The list of functions to add to origClass. This should be specified as an object literal
256 * containing one or more methods.
259 override : function(origclass, overrides){
261 var p = origclass.prototype;
262 for(var method in overrides){
263 p[method] = overrides[method];
268 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
270 Roo.namespace('Company', 'Company.data');
271 Company.Widget = function() { ... }
272 Company.data.CustomStore = function(config) { ... }
274 * @param {String} namespace1
275 * @param {String} namespace2
276 * @param {String} etc
279 namespace : function(){
280 var a=arguments, o=null, i, j, d, rt;
281 for (i=0; i<a.length; ++i) {
285 eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
286 for (j=1; j<d.length; ++j) {
287 o[d[j]]=o[d[j]] || {};
293 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
295 Roo.factory({ xns: Roo.data, xtype : 'Store', .....});
296 Roo.factory(conf, Roo.data);
298 * @param {String} classname
299 * @param {String} namespace (optional)
303 factory : function(c, ns)
305 // no xtype, no ns or c.xns - or forced off by c.xns
306 if (!c.xtype || (!ns && !c.xns) || (c.xns === false)) { // not enough info...
309 ns = c.xns ? c.xns : ns; // if c.xns is set, then use that..
310 if (c.constructor == ns[c.xtype]) {// already created...
314 if (Roo.debug) Roo.log("Roo.Factory(" + c.xtype + ")");
315 var ret = new ns[c.xtype](c);
319 c.xns = false; // prevent recursion..
323 * Logs to console if it can.
325 * @param {String|Object} string
330 if ((typeof(console) == 'undefined') || (typeof(console.log) == 'undefined')) {
337 * 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.
341 urlEncode : function(o){
347 var ov = o[key], k = Roo.encodeURIComponent(key);
348 var type = typeof ov;
349 if(type == 'undefined'){
351 }else if(type != "function" && type != "object"){
352 buf.push(k, "=", Roo.encodeURIComponent(ov), "&");
353 }else if(ov instanceof Array){
355 for(var i = 0, len = ov.length; i < len; i++) {
356 buf.push(k, "=", Roo.encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
367 * Safe version of encodeURIComponent
368 * @param {String} data
372 encodeURIComponent : function (data)
375 return encodeURIComponent(data);
376 } catch(e) {} // should be an uri encode error.
378 if (data == '' || data == null){
381 // http://stackoverflow.com/questions/2596483/unicode-and-uri-encoding-decoding-and-escaping-in-javascript
382 function nibble_to_hex(nibble){
383 var chars = '0123456789ABCDEF';
384 return chars.charAt(nibble);
386 data = data.toString();
388 for(var i=0; i<data.length; i++){
389 var c = data.charCodeAt(i);
390 var bs = new Array();
393 bs[0] = 0xF0 | ((c & 0x1C0000) >>> 18);
394 bs[1] = 0x80 | ((c & 0x3F000) >>> 12);
395 bs[2] = 0x80 | ((c & 0xFC0) >>> 6);
396 bs[3] = 0x80 | (c & 0x3F);
397 }else if (c > 0x800){
399 bs[0] = 0xE0 | ((c & 0xF000) >>> 12);
400 bs[1] = 0x80 | ((c & 0xFC0) >>> 6);
401 bs[2] = 0x80 | (c & 0x3F);
404 bs[0] = 0xC0 | ((c & 0x7C0) >>> 6);
405 bs[1] = 0x80 | (c & 0x3F);
410 for(var j=0; j<bs.length; j++){
412 var hex = nibble_to_hex((b & 0xF0) >>> 4)
413 + nibble_to_hex(b &0x0F);
422 * 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]}.
423 * @param {String} string
424 * @param {Boolean} overwrite (optional) Items of the same name will overwrite previous values instead of creating an an array (Defaults to false).
425 * @return {Object} A literal with members
427 urlDecode : function(string, overwrite){
428 if(!string || !string.length){
432 var pairs = string.split('&');
433 var pair, name, value;
434 for(var i = 0, len = pairs.length; i < len; i++){
435 pair = pairs[i].split('=');
436 name = decodeURIComponent(pair[0]);
437 value = decodeURIComponent(pair[1]);
438 if(overwrite !== true){
439 if(typeof obj[name] == "undefined"){
441 }else if(typeof obj[name] == "string"){
442 obj[name] = [obj[name]];
443 obj[name].push(value);
445 obj[name].push(value);
455 * Iterates an array calling the passed function with each item, stopping if your function returns false. If the
456 * passed array is not really an array, your function is called once with it.
457 * The supplied function is called with (Object item, Number index, Array allItems).
458 * @param {Array/NodeList/Mixed} array
459 * @param {Function} fn
460 * @param {Object} scope
462 each : function(array, fn, scope){
463 if(typeof array.length == "undefined" || typeof array == "string"){
466 for(var i = 0, len = array.length; i < len; i++){
467 if(fn.call(scope || array[i], array[i], i, array) === false){ return i; };
472 combine : function(){
473 var as = arguments, l = as.length, r = [];
474 for(var i = 0; i < l; i++){
476 if(a instanceof Array){
478 }else if(a.length !== undefined && !a.substr){
479 r = r.concat(Array.prototype.slice.call(a, 0));
488 * Escapes the passed string for use in a regular expression
489 * @param {String} str
492 escapeRe : function(s) {
493 return s.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1");
497 callback : function(cb, scope, args, delay){
498 if(typeof cb == "function"){
500 cb.defer(delay, scope, args || []);
502 cb.apply(scope, args || []);
508 * Return the dom node for the passed string (id), dom node, or Roo.Element
509 * @param {String/HTMLElement/Roo.Element} el
510 * @return HTMLElement
512 getDom : function(el){
516 return el.dom ? el.dom : (typeof el == 'string' ? document.getElementById(el) : el);
520 * Shorthand for {@link Roo.ComponentMgr#get}
522 * @return Roo.Component
524 getCmp : function(id){
525 return Roo.ComponentMgr.get(id);
528 num : function(v, defaultValue){
529 if(typeof v != 'number'){
535 destroy : function(){
536 for(var i = 0, a = arguments, len = a.length; i < len; i++) {
540 as.removeAllListeners();
544 if(typeof as.purgeListeners == 'function'){
547 if(typeof as.destroy == 'function'){
554 // inpired by a similar function in mootools library
556 * Returns the type of object that is passed in. If the object passed in is null or undefined it
557 * return false otherwise it returns one of the following values:<ul>
558 * <li><b>string</b>: If the object passed is a string</li>
559 * <li><b>number</b>: If the object passed is a number</li>
560 * <li><b>boolean</b>: If the object passed is a boolean value</li>
561 * <li><b>function</b>: If the object passed is a function reference</li>
562 * <li><b>object</b>: If the object passed is an object</li>
563 * <li><b>array</b>: If the object passed is an array</li>
564 * <li><b>regexp</b>: If the object passed is a regular expression</li>
565 * <li><b>element</b>: If the object passed is a DOM Element</li>
566 * <li><b>nodelist</b>: If the object passed is a DOM NodeList</li>
567 * <li><b>textnode</b>: If the object passed is a DOM text node and contains something other than whitespace</li>
568 * <li><b>whitespace</b>: If the object passed is a DOM text node and contains only whitespace</li>
569 * @param {Mixed} object
573 if(o === undefined || o === null){
580 if(t == 'object' && o.nodeName) {
582 case 1: return 'element';
583 case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
586 if(t == 'object' || t == 'function') {
587 switch(o.constructor) {
588 case Array: return 'array';
589 case RegExp: return 'regexp';
591 if(typeof o.length == 'number' && typeof o.item == 'function') {
599 * Returns true if the passed value is null, undefined or an empty string (optional).
600 * @param {Mixed} value The value to test
601 * @param {Boolean} allowBlank (optional) Pass true if an empty string is not considered empty
604 isEmpty : function(v, allowBlank){
605 return v === null || v === undefined || (!allowBlank ? v === '' : false);
619 isBorderBox : isBorderBox,
621 isWindows : isWindows,
630 * By default, Ext intelligently decides whether floating elements should be shimmed. If you are using flash,
631 * you may want to set this to true.
634 useShims : ((isIE && !isIE7) || (isGecko && isMac)),
639 * Selects a single element as a Roo Element
640 * This is about as close as you can get to jQuery's $('do crazy stuff')
641 * @param {String} selector The selector/xpath query
642 * @param {Node} root (optional) The start of the query (defaults to document).
643 * @return {Roo.Element}
645 selectNode : function(selector, root)
647 var node = Roo.DomQuery.selectNode(selector,root);
648 return node ? Roo.get(node) : new Roo.Element(false);
656 Roo.namespace("Roo", "Roo.util", "Roo.grid", "Roo.dd", "Roo.tree", "Roo.data",
657 "Roo.form", "Roo.menu", "Roo.state", "Roo.lib", "Roo.layout",
660 "Roo.bootstrap.dash");