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 console.log('-------ua---------');
56 var isStrict = document.compatMode == "CSS1Compat",
57 isOpera = ua.indexOf("opera") > -1,
58 isSafari = (/webkit|khtml/).test(ua),
59 isFirefox = ua.indexOf("firefox") > -1,
60 isIE = ua.indexOf("msie") > -1,
61 isIE7 = ua.indexOf("msie 7") > -1,
62 isIE11 = /trident.*rv\:11\./.test(ua),
63 isGecko = !isSafari && ua.indexOf("gecko") > -1,
64 isBorderBox = isIE && !isStrict,
65 isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1),
66 isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1),
67 isLinux = (ua.indexOf("linux") != -1),
68 isSecure = window.location.href.toLowerCase().indexOf("https") === 0,
69 isIOS = /iphone|ipad/.test(ua),
70 isAndroid = /android/.test(ua),
71 isTouch = (function() {
73 if (ua.indexOf('chrome') != -1 && ua.indexOf('android') == -1) {
74 window.addEventListener('touchstart', function __set_has_touch__ () {
76 window.removeEventListener('touchstart', __set_has_touch__);
78 return false; // no touch on chrome!?
80 document.createEvent("TouchEvent");
87 // remove css image flicker
90 document.execCommand("BackgroundImageCache", false, true);
96 * True if the browser is in strict mode
101 * True if the page is running over SSL
106 * True when the document is fully initialized and ready for action
111 * Turn on debugging output (currently only the factory uses this)
118 * True to automatically uncache orphaned Roo.Elements periodically (defaults to true)
121 enableGarbageCollector : true,
124 * True to automatically purge event listeners after uncaching an element (defaults to false).
125 * Note: this only happens if enableGarbageCollector is true.
128 enableListenerCollection:false,
131 * URL to a blank file used by Roo when in secure mode for iframe src and onReady src to prevent
132 * the IE insecure content warning (defaults to javascript:false).
135 SSL_SECURE_URL : "javascript:false",
138 * URL to a 1x1 transparent gif image used by Roo to create inline icons with CSS background images. (Defaults to
139 * "http://Roojs.com/s.gif" and you should change this to a URL on your server).
142 BLANK_IMAGE_URL : "http:/"+"/localhost/s.gif",
144 emptyFn : function(){},
147 * Copies all the properties of config to obj if they don't already exist.
148 * @param {Object} obj The receiver of the properties
149 * @param {Object} config The source of the properties
150 * @return {Object} returns obj
152 applyIf : function(o, c){
155 if(typeof o[p] == "undefined"){ o[p] = c[p]; }
162 * Applies event listeners to elements by selectors when the document is ready.
163 * The event name is specified with an @ suffix.
166 // add a listener for click on all anchors in element with id foo
167 '#foo a@click' : function(e, t){
171 // add the same listener to multiple selectors (separated by comma BEFORE the @)
172 '#foo a, #bar span.some-class@mouseover' : function(){
177 * @param {Object} obj The list of behaviors to apply
179 addBehaviors : function(o){
181 Roo.onReady(function(){
186 var cache = {}; // simple cache for applying multiple behaviors to same selector does query multiple times
188 var parts = b.split('@');
189 if(parts[1]){ // for Object prototype breakers
192 cache[s] = Roo.select(s);
194 cache[s].on(parts[1], o[b]);
201 * Generates unique ids. If the element already has an id, it is unchanged
202 * @param {String/HTMLElement/Element} el (optional) The element to generate an id for
203 * @param {String} prefix (optional) Id prefix (defaults "Roo-gen")
204 * @return {String} The generated Id.
206 id : function(el, prefix){
207 prefix = prefix || "roo-gen";
209 var id = prefix + (++idSeed);
210 return el ? (el.id ? el.id : (el.id = id)) : id;
215 * Extends one class with another class and optionally overrides members with the passed literal. This class
216 * also adds the function "override()" to the class that can be used to override
217 * members on an instance.
218 * @param {Object} subclass The class inheriting the functionality
219 * @param {Object} superclass The class being extended
220 * @param {Object} overrides (optional) A literal with members
225 var io = function(o){
230 return function(sb, sp, overrides){
231 if(typeof sp == 'object'){ // eg. prototype, rather than function constructor..
234 sb = function(){sp.apply(this, arguments);};
236 var F = function(){}, sbp, spp = sp.prototype;
238 sbp = sb.prototype = new F();
242 if(spp.constructor == Object.prototype.constructor){
247 sb.override = function(o){
251 Roo.override(sb, overrides);
257 * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
259 Roo.override(MyClass, {
260 newMethod1: function(){
263 newMethod2: function(foo){
268 * @param {Object} origclass The class to override
269 * @param {Object} overrides The list of functions to add to origClass. This should be specified as an object literal
270 * containing one or more methods.
273 override : function(origclass, overrides){
275 var p = origclass.prototype;
276 for(var method in overrides){
277 p[method] = overrides[method];
282 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
284 Roo.namespace('Company', 'Company.data');
285 Company.Widget = function() { ... }
286 Company.data.CustomStore = function(config) { ... }
288 * @param {String} namespace1
289 * @param {String} namespace2
290 * @param {String} etc
293 namespace : function(){
294 var a=arguments, o=null, i, j, d, rt;
295 for (i=0; i<a.length; ++i) {
299 eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
300 for (j=1; j<d.length; ++j) {
301 o[d[j]]=o[d[j]] || {};
307 * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
309 Roo.factory({ xns: Roo.data, xtype : 'Store', .....});
310 Roo.factory(conf, Roo.data);
312 * @param {String} classname
313 * @param {String} namespace (optional)
317 factory : function(c, ns)
319 // no xtype, no ns or c.xns - or forced off by c.xns
320 if (!c.xtype || (!ns && !c.xns) || (c.xns === false)) { // not enough info...
323 ns = c.xns ? c.xns : ns; // if c.xns is set, then use that..
324 if (c.constructor == ns[c.xtype]) {// already created...
328 if (Roo.debug) { Roo.log("Roo.Factory(" + c.xtype + ")"); }
329 var ret = new ns[c.xtype](c);
333 c.xns = false; // prevent recursion..
337 * Logs to console if it can.
339 * @param {String|Object} string
344 if ((typeof(console) == 'undefined') || (typeof(console.log) == 'undefined')) {
351 * 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.
355 urlEncode : function(o){
361 var ov = o[key], k = Roo.encodeURIComponent(key);
362 var type = typeof ov;
363 if(type == 'undefined'){
365 }else if(type != "function" && type != "object"){
366 buf.push(k, "=", Roo.encodeURIComponent(ov), "&");
367 }else if(ov instanceof Array){
369 for(var i = 0, len = ov.length; i < len; i++) {
370 buf.push(k, "=", Roo.encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
381 * Safe version of encodeURIComponent
382 * @param {String} data
386 encodeURIComponent : function (data)
389 return encodeURIComponent(data);
390 } catch(e) {} // should be an uri encode error.
392 if (data == '' || data == null){
395 // http://stackoverflow.com/questions/2596483/unicode-and-uri-encoding-decoding-and-escaping-in-javascript
396 function nibble_to_hex(nibble){
397 var chars = '0123456789ABCDEF';
398 return chars.charAt(nibble);
400 data = data.toString();
402 for(var i=0; i<data.length; i++){
403 var c = data.charCodeAt(i);
404 var bs = new Array();
407 bs[0] = 0xF0 | ((c & 0x1C0000) >>> 18);
408 bs[1] = 0x80 | ((c & 0x3F000) >>> 12);
409 bs[2] = 0x80 | ((c & 0xFC0) >>> 6);
410 bs[3] = 0x80 | (c & 0x3F);
411 }else if (c > 0x800){
413 bs[0] = 0xE0 | ((c & 0xF000) >>> 12);
414 bs[1] = 0x80 | ((c & 0xFC0) >>> 6);
415 bs[2] = 0x80 | (c & 0x3F);
418 bs[0] = 0xC0 | ((c & 0x7C0) >>> 6);
419 bs[1] = 0x80 | (c & 0x3F);
424 for(var j=0; j<bs.length; j++){
426 var hex = nibble_to_hex((b & 0xF0) >>> 4)
427 + nibble_to_hex(b &0x0F);
436 * 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]}.
437 * @param {String} string
438 * @param {Boolean} overwrite (optional) Items of the same name will overwrite previous values instead of creating an an array (Defaults to false).
439 * @return {Object} A literal with members
441 urlDecode : function(string, overwrite){
442 if(!string || !string.length){
446 var pairs = string.split('&');
447 var pair, name, value;
448 for(var i = 0, len = pairs.length; i < len; i++){
449 pair = pairs[i].split('=');
450 name = decodeURIComponent(pair[0]);
451 value = decodeURIComponent(pair[1]);
452 if(overwrite !== true){
453 if(typeof obj[name] == "undefined"){
455 }else if(typeof obj[name] == "string"){
456 obj[name] = [obj[name]];
457 obj[name].push(value);
459 obj[name].push(value);
469 * Iterates an array calling the passed function with each item, stopping if your function returns false. If the
470 * passed array is not really an array, your function is called once with it.
471 * The supplied function is called with (Object item, Number index, Array allItems).
472 * @param {Array/NodeList/Mixed} array
473 * @param {Function} fn
474 * @param {Object} scope
476 each : function(array, fn, scope){
477 if(typeof array.length == "undefined" || typeof array == "string"){
480 for(var i = 0, len = array.length; i < len; i++){
481 if(fn.call(scope || array[i], array[i], i, array) === false){ return i; };
486 combine : function(){
487 var as = arguments, l = as.length, r = [];
488 for(var i = 0; i < l; i++){
490 if(a instanceof Array){
492 }else if(a.length !== undefined && !a.substr){
493 r = r.concat(Array.prototype.slice.call(a, 0));
502 * Escapes the passed string for use in a regular expression
503 * @param {String} str
506 escapeRe : function(s) {
507 return s.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1");
511 callback : function(cb, scope, args, delay){
512 if(typeof cb == "function"){
514 cb.defer(delay, scope, args || []);
516 cb.apply(scope, args || []);
522 * Return the dom node for the passed string (id), dom node, or Roo.Element
523 * @param {String/HTMLElement/Roo.Element} el
524 * @return HTMLElement
526 getDom : function(el){
530 return el.dom ? el.dom : (typeof el == 'string' ? document.getElementById(el) : el);
534 * Shorthand for {@link Roo.ComponentMgr#get}
536 * @return Roo.Component
538 getCmp : function(id){
539 return Roo.ComponentMgr.get(id);
542 num : function(v, defaultValue){
543 if(typeof v != 'number'){
549 destroy : function(){
550 for(var i = 0, a = arguments, len = a.length; i < len; i++) {
554 as.removeAllListeners();
558 if(typeof as.purgeListeners == 'function'){
561 if(typeof as.destroy == 'function'){
568 // inpired by a similar function in mootools library
570 * Returns the type of object that is passed in. If the object passed in is null or undefined it
571 * return false otherwise it returns one of the following values:<ul>
572 * <li><b>string</b>: If the object passed is a string</li>
573 * <li><b>number</b>: If the object passed is a number</li>
574 * <li><b>boolean</b>: If the object passed is a boolean value</li>
575 * <li><b>function</b>: If the object passed is a function reference</li>
576 * <li><b>object</b>: If the object passed is an object</li>
577 * <li><b>array</b>: If the object passed is an array</li>
578 * <li><b>regexp</b>: If the object passed is a regular expression</li>
579 * <li><b>element</b>: If the object passed is a DOM Element</li>
580 * <li><b>nodelist</b>: If the object passed is a DOM NodeList</li>
581 * <li><b>textnode</b>: If the object passed is a DOM text node and contains something other than whitespace</li>
582 * <li><b>whitespace</b>: If the object passed is a DOM text node and contains only whitespace</li>
583 * @param {Mixed} object
587 if(o === undefined || o === null){
594 if(t == 'object' && o.nodeName) {
596 case 1: return 'element';
597 case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
600 if(t == 'object' || t == 'function') {
601 switch(o.constructor) {
602 case Array: return 'array';
603 case RegExp: return 'regexp';
605 if(typeof o.length == 'number' && typeof o.item == 'function') {
613 * Returns true if the passed value is null, undefined or an empty string (optional).
614 * @param {Mixed} value The value to test
615 * @param {Boolean} allowBlank (optional) Pass true if an empty string is not considered empty
618 isEmpty : function(v, allowBlank){
619 return v === null || v === undefined || (!allowBlank ? v === '' : false);
627 isFirefox : isFirefox,
637 isBorderBox : isBorderBox,
639 isWindows : isWindows,
647 isAndroid : isAndroid,
652 * By default, Ext intelligently decides whether floating elements should be shimmed. If you are using flash,
653 * you may want to set this to true.
656 useShims : ((isIE && !isIE7) || (isGecko && isMac)),
661 * Selects a single element as a Roo Element
662 * This is about as close as you can get to jQuery's $('do crazy stuff')
663 * @param {String} selector The selector/xpath query
664 * @param {Node} root (optional) The start of the query (defaults to document).
665 * @return {Roo.Element}
667 selectNode : function(selector, root)
669 var node = Roo.DomQuery.selectNode(selector,root);
670 return node ? Roo.get(node) : new Roo.Element(false);
678 Roo.namespace("Roo", "Roo.util", "Roo.grid", "Roo.dd", "Roo.tree", "Roo.data",
679 "Roo.form", "Roo.menu", "Roo.state", "Roo.lib", "Roo.layout",
682 "Roo.bootstrap.dash");