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 isFirefox = ua.indexOf("firefox") > -1,
57 isIE = ua.indexOf("msie") > -1,
58 isIE7 = ua.indexOf("msie 7") > -1,
59 isIE11 = /trident.*rv\:11\./.test(ua),
60 isEdge = ua.indexOf("edge") > -1,
61 isGecko = !isSafari && ua.indexOf("gecko") > -1,
62 isBorderBox = isIE && !isStrict,
63 isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1),
64 isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1),
65 isLinux = (ua.indexOf("linux") != -1),
66 isSecure = window.location.href.toLowerCase().indexOf("https") === 0,
67 isIOS = /iphone|ipad/.test(ua),
68 isAndroid = /android/.test(ua),
69 isTouch = (function() {
71 if (ua.indexOf('chrome') != -1 && ua.indexOf('android') == -1) {
72 window.addEventListener('touchstart', function __set_has_touch__ () {
74 window.removeEventListener('touchstart', __set_has_touch__);
76 return false; // no touch on chrome!?
78 document.createEvent("TouchEvent");
85 // remove css image flicker
88 document.execCommand("BackgroundImageCache", false, true);
94 * True if the browser is in strict mode
99 * True if the page is running over SSL
104 * True when the document is fully initialized and ready for action
109 * Turn on debugging output (currently only the factory uses this)
116 * True to automatically uncache orphaned Roo.Elements periodically (defaults to true)
119 enableGarbageCollector : true,
122 * True to automatically purge event listeners after uncaching an element (defaults to false).
123 * Note: this only happens if enableGarbageCollector is true.
126 enableListenerCollection:false,
129 * URL to a blank file used by Roo when in secure mode for iframe src and onReady src to prevent
130 * the IE insecure content warning (defaults to javascript:false).
133 SSL_SECURE_URL : "javascript:false",
136 * URL to a 1x1 transparent gif image used by Roo to create inline icons with CSS background images. (Defaults to
137 * "http://Roojs.com/s.gif" and you should change this to a URL on your server).
140 BLANK_IMAGE_URL : "http:/"+"/localhost/s.gif",
142 emptyFn : function(){},
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
150 applyIf : function(o, c){
153 if(typeof o[p] == "undefined"){ o[p] = c[p]; }
160 * Applies event listeners to elements by selectors when the document is ready.
161 * The event name is specified with an @ suffix.
164 // add a listener for click on all anchors in element with id foo
165 '#foo a@click' : function(e, t){
169 // add the same listener to multiple selectors (separated by comma BEFORE the @)
170 '#foo a, #bar span.some-class@mouseover' : function(){
175 * @param {Object} obj The list of behaviors to apply
177 addBehaviors : function(o){
179 Roo.onReady(function(){
184 var cache = {}; // simple cache for applying multiple behaviors to same selector does query multiple times
186 var parts = b.split('@');
187 if(parts[1]){ // for Object prototype breakers
190 cache[s] = Roo.select(s);
192 cache[s].on(parts[1], o[b]);
199 * Generates unique ids. If the element already has an id, it is unchanged
200 * @param {String/HTMLElement/Element} el (optional) The element to generate an id for
201 * @param {String} prefix (optional) Id prefix (defaults "Roo-gen")
202 * @return {String} The generated Id.
204 id : function(el, prefix){
205 prefix = prefix || "roo-gen";
207 var id = prefix + (++idSeed);
208 return el ? (el.id ? el.id : (el.id = id)) : id;
213 * Extends one class with another class and optionally overrides members with the passed literal. This class
214 * also adds the function "override()" to the class that can be used to override
215 * members on an instance.
216 * @param {Object} subclass The class inheriting the functionality
217 * @param {Object} superclass The class being extended
218 * @param {Object} overrides (optional) A literal with members
223 var io = function(o){
228 return function(sb, sp, overrides){
229 if(typeof sp == 'object'){ // eg. prototype, rather than function constructor..
232 sb = function(){sp.apply(this, arguments);};
234 var F = function(){}, sbp, spp = sp.prototype;
236 sbp = sb.prototype = new F();
240 if(spp.constructor == Object.prototype.constructor){
245 sb.override = function(o){
249 Roo.override(sb, overrides);
255 * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
257 Roo.override(MyClass, {
258 newMethod1: function(){
261 newMethod2: function(foo){
266 * @param {Object} origclass The class to override
267 * @param {Object} overrides The list of functions to add to origClass. This should be specified as an object literal
268 * containing one or more methods.
271 override : function(origclass, overrides){
273 var p = origclass.prototype;
274 for(var method in overrides){
275 p[method] = overrides[method];
280 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
282 Roo.namespace('Company', 'Company.data');
283 Company.Widget = function() { ... }
284 Company.data.CustomStore = function(config) { ... }
286 * @param {String} namespace1
287 * @param {String} namespace2
288 * @param {String} etc
291 namespace : function(){
292 var a=arguments, o=null, i, j, d, rt;
293 for (i=0; i<a.length; ++i) {
297 eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
298 for (j=1; j<d.length; ++j) {
299 o[d[j]]=o[d[j]] || {};
305 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
307 Roo.factory({ xns: Roo.data, xtype : 'Store', .....});
308 Roo.factory(conf, Roo.data);
310 * @param {String} classname
311 * @param {String} namespace (optional)
315 factory : function(c, ns)
317 // no xtype, no ns or c.xns - or forced off by c.xns
318 if (!c.xtype || (!ns && !c.xns) || (c.xns === false)) { // not enough info...
321 ns = c.xns ? c.xns : ns; // if c.xns is set, then use that..
322 if (c.constructor == ns[c.xtype]) {// already created...
326 if (Roo.debug) { Roo.log("Roo.Factory(" + c.xtype + ")"); }
327 var ret = new ns[c.xtype](c);
331 c.xns = false; // prevent recursion..
335 * Logs to console if it can.
337 * @param {String|Object} string
342 if ((typeof(console) == 'undefined') || (typeof(console.log) == 'undefined')) {
346 if(window.location.href.indexOf("localhost") == -1) {
354 * 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.
358 urlEncode : function(o){
364 var ov = o[key], k = Roo.encodeURIComponent(key);
365 var type = typeof ov;
366 if(type == 'undefined'){
368 }else if(type != "function" && type != "object"){
369 buf.push(k, "=", Roo.encodeURIComponent(ov), "&");
370 }else if(ov instanceof Array){
372 for(var i = 0, len = ov.length; i < len; i++) {
373 buf.push(k, "=", Roo.encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
384 * Safe version of encodeURIComponent
385 * @param {String} data
389 encodeURIComponent : function (data)
392 return encodeURIComponent(data);
393 } catch(e) {} // should be an uri encode error.
395 if (data == '' || data == null){
398 // http://stackoverflow.com/questions/2596483/unicode-and-uri-encoding-decoding-and-escaping-in-javascript
399 function nibble_to_hex(nibble){
400 var chars = '0123456789ABCDEF';
401 return chars.charAt(nibble);
403 data = data.toString();
405 for(var i=0; i<data.length; i++){
406 var c = data.charCodeAt(i);
407 var bs = new Array();
410 bs[0] = 0xF0 | ((c & 0x1C0000) >>> 18);
411 bs[1] = 0x80 | ((c & 0x3F000) >>> 12);
412 bs[2] = 0x80 | ((c & 0xFC0) >>> 6);
413 bs[3] = 0x80 | (c & 0x3F);
414 }else if (c > 0x800){
416 bs[0] = 0xE0 | ((c & 0xF000) >>> 12);
417 bs[1] = 0x80 | ((c & 0xFC0) >>> 6);
418 bs[2] = 0x80 | (c & 0x3F);
421 bs[0] = 0xC0 | ((c & 0x7C0) >>> 6);
422 bs[1] = 0x80 | (c & 0x3F);
427 for(var j=0; j<bs.length; j++){
429 var hex = nibble_to_hex((b & 0xF0) >>> 4)
430 + nibble_to_hex(b &0x0F);
439 * 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]}.
440 * @param {String} string
441 * @param {Boolean} overwrite (optional) Items of the same name will overwrite previous values instead of creating an an array (Defaults to false).
442 * @return {Object} A literal with members
444 urlDecode : function(string, overwrite){
445 if(!string || !string.length){
449 var pairs = string.split('&');
450 var pair, name, value;
451 for(var i = 0, len = pairs.length; i < len; i++){
452 pair = pairs[i].split('=');
453 name = decodeURIComponent(pair[0]);
454 value = decodeURIComponent(pair[1]);
455 if(overwrite !== true){
456 if(typeof obj[name] == "undefined"){
458 }else if(typeof obj[name] == "string"){
459 obj[name] = [obj[name]];
460 obj[name].push(value);
462 obj[name].push(value);
472 * Iterates an array calling the passed function with each item, stopping if your function returns false. If the
473 * passed array is not really an array, your function is called once with it.
474 * The supplied function is called with (Object item, Number index, Array allItems).
475 * @param {Array/NodeList/Mixed} array
476 * @param {Function} fn
477 * @param {Object} scope
479 each : function(array, fn, scope){
480 if(typeof array.length == "undefined" || typeof array == "string"){
483 for(var i = 0, len = array.length; i < len; i++){
484 if(fn.call(scope || array[i], array[i], i, array) === false){ return i; };
489 combine : function(){
490 var as = arguments, l = as.length, r = [];
491 for(var i = 0; i < l; i++){
493 if(a instanceof Array){
495 }else if(a.length !== undefined && !a.substr){
496 r = r.concat(Array.prototype.slice.call(a, 0));
505 * Escapes the passed string for use in a regular expression
506 * @param {String} str
509 escapeRe : function(s) {
510 return s.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1");
514 callback : function(cb, scope, args, delay){
515 if(typeof cb == "function"){
517 cb.defer(delay, scope, args || []);
519 cb.apply(scope, args || []);
525 * Return the dom node for the passed string (id), dom node, or Roo.Element
526 * @param {String/HTMLElement/Roo.Element} el
527 * @return HTMLElement
529 getDom : function(el){
533 return el.dom ? el.dom : (typeof el == 'string' ? document.getElementById(el) : el);
537 * Shorthand for {@link Roo.ComponentMgr#get}
539 * @return Roo.Component
541 getCmp : function(id){
542 return Roo.ComponentMgr.get(id);
545 num : function(v, defaultValue){
546 if(typeof v != 'number'){
552 destroy : function(){
553 for(var i = 0, a = arguments, len = a.length; i < len; i++) {
557 as.removeAllListeners();
561 if(typeof as.purgeListeners == 'function'){
564 if(typeof as.destroy == 'function'){
571 // inpired by a similar function in mootools library
573 * Returns the type of object that is passed in. If the object passed in is null or undefined it
574 * return false otherwise it returns one of the following values:<ul>
575 * <li><b>string</b>: If the object passed is a string</li>
576 * <li><b>number</b>: If the object passed is a number</li>
577 * <li><b>boolean</b>: If the object passed is a boolean value</li>
578 * <li><b>function</b>: If the object passed is a function reference</li>
579 * <li><b>object</b>: If the object passed is an object</li>
580 * <li><b>array</b>: If the object passed is an array</li>
581 * <li><b>regexp</b>: If the object passed is a regular expression</li>
582 * <li><b>element</b>: If the object passed is a DOM Element</li>
583 * <li><b>nodelist</b>: If the object passed is a DOM NodeList</li>
584 * <li><b>textnode</b>: If the object passed is a DOM text node and contains something other than whitespace</li>
585 * <li><b>whitespace</b>: If the object passed is a DOM text node and contains only whitespace</li>
586 * @param {Mixed} object
590 if(o === undefined || o === null){
597 if(t == 'object' && o.nodeName) {
599 case 1: return 'element';
600 case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
603 if(t == 'object' || t == 'function') {
604 switch(o.constructor) {
605 case Array: return 'array';
606 case RegExp: return 'regexp';
608 if(typeof o.length == 'number' && typeof o.item == 'function') {
616 * Returns true if the passed value is null, undefined or an empty string (optional).
617 * @param {Mixed} value The value to test
618 * @param {Boolean} allowBlank (optional) Pass true if an empty string is not considered empty
621 isEmpty : function(v, allowBlank){
622 return v === null || v === undefined || (!allowBlank ? v === '' : false);
630 isFirefox : isFirefox,
642 isBorderBox : isBorderBox,
644 isWindows : isWindows,
652 isAndroid : isAndroid,
657 * By default, Ext intelligently decides whether floating elements should be shimmed. If you are using flash,
658 * you may want to set this to true.
661 useShims : ((isIE && !isIE7) || (isGecko && isMac)),
666 * Selects a single element as a Roo Element
667 * This is about as close as you can get to jQuery's $('do crazy stuff')
668 * @param {String} selector The selector/xpath query
669 * @param {Node} root (optional) The start of the query (defaults to document).
670 * @return {Roo.Element}
672 selectNode : function(selector, root)
674 var node = Roo.DomQuery.selectNode(selector,root);
675 return node ? Roo.get(node) : new Roo.Element(false);
683 Roo.namespace("Roo", "Roo.util", "Roo.grid", "Roo.dd", "Roo.tree", "Roo.data",
684 "Roo.form", "Roo.menu", "Roo.state", "Roo.lib", "Roo.layout",
687 "Roo.bootstrap.dash");