PageRenderTime 73ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

/public/javascripts/extjs/source/core/Ext.js

https://github.com/xlsuite/xlsuite
JavaScript | 1066 lines | 879 code | 23 blank | 164 comment | 62 complexity | 53a017a7a1c355cab5250e52700ed1d6 MD5 | raw file
Possible License(s): JSON, MPL-2.0-no-copyleft-exception, GPL-2.0
  1. /*
  2. * Ext JS Library 2.2.1
  3. * Copyright(c) 2006-2009, Ext JS, LLC.
  4. * licensing@extjs.com
  5. *
  6. * http://extjs.com/license
  7. */
  8. Ext = {version: '2.2.1'};
  9. // for old browsers
  10. window["undefined"] = window["undefined"];
  11. /**
  12. * @class Ext
  13. * Ext core utilities and functions.
  14. * @singleton
  15. */
  16. /**
  17. * Copies all the properties of config to obj.
  18. * @param {Object} obj The receiver of the properties
  19. * @param {Object} config The source of the properties
  20. * @param {Object} defaults A different object that will also be applied for default values
  21. * @return {Object} returns obj
  22. * @member Ext apply
  23. */
  24. Ext.apply = function(o, c, defaults){
  25. if(defaults){
  26. // no "this" reference for friendly out of scope calls
  27. Ext.apply(o, defaults);
  28. }
  29. if(o && c && typeof c == 'object'){
  30. for(var p in c){
  31. o[p] = c[p];
  32. }
  33. }
  34. return o;
  35. };
  36. (function(){
  37. var idSeed = 0;
  38. var ua = navigator.userAgent.toLowerCase();
  39. var isStrict = document.compatMode == "CSS1Compat",
  40. isOpera = ua.indexOf("opera") > -1,
  41. isChrome = ua.indexOf("chrome") > -1,
  42. isWebKit = ua.indexOf("webkit") > -1,
  43. isSafari = !isChrome && (/webkit|khtml/).test(ua),
  44. isSafari3 = isSafari && ua.indexOf('webkit/5') != -1,
  45. isIE = !isOpera && ua.indexOf("msie") > -1,
  46. isIE7 = !isOpera && ua.indexOf("msie 7") > -1,
  47. isIE8 = !isOpera && ua.indexOf("msie 8") > -1,
  48. isGecko = !isSafari && !isChrome && ua.indexOf("gecko") > -1,
  49. isGecko3 = isGecko && ua.indexOf("rv:1.9") > -1,
  50. isBorderBox = isIE && !isStrict,
  51. isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1),
  52. isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1),
  53. isAir = (ua.indexOf("adobeair") != -1),
  54. isLinux = (ua.indexOf("linux") != -1),
  55. isSecure = /^https/i.test(window.location.protocol);
  56. // remove css image flicker
  57. if(isIE && !(isIE7 || isIE8)){
  58. try{
  59. document.execCommand("BackgroundImageCache", false, true);
  60. }catch(e){}
  61. }
  62. Ext.apply(Ext, {
  63. /**
  64. * True if the browser is in strict (standards-compliant) mode, as opposed to quirks mode
  65. * @type Boolean
  66. */
  67. isStrict : isStrict,
  68. /**
  69. * True if the page is running over SSL
  70. * @type Boolean
  71. */
  72. isSecure : isSecure,
  73. /**
  74. * True when the document is fully initialized and ready for action
  75. * @type Boolean
  76. */
  77. isReady : false,
  78. /**
  79. * True to automatically uncache orphaned Ext.Elements periodically (defaults to true)
  80. * @type Boolean
  81. */
  82. enableGarbageCollector : true,
  83. /**
  84. * True to automatically purge event listeners after uncaching an element (defaults to false).
  85. * Note: this only happens if enableGarbageCollector is true.
  86. * @type Boolean
  87. */
  88. enableListenerCollection:false,
  89. /**
  90. * URL to a blank file used by Ext when in secure mode for iframe src and onReady src to prevent
  91. * the IE insecure content warning (defaults to javascript:false).
  92. * @type String
  93. */
  94. SSL_SECURE_URL : "javascript:false",
  95. /**
  96. * URL to a 1x1 transparent gif image used by Ext to create inline icons with CSS background images. (Defaults to
  97. * "http://extjs.com/s.gif" and you should change this to a URL on your server).
  98. * @type String
  99. */
  100. BLANK_IMAGE_URL : "http:/"+"/extjs.com/s.gif",
  101. /**
  102. * A reusable empty function
  103. * @property
  104. * @type Function
  105. */
  106. emptyFn : function(){},
  107. /**
  108. * Copies all the properties of config to obj if they don't already exist.
  109. * @param {Object} obj The receiver of the properties
  110. * @param {Object} config The source of the properties
  111. * @return {Object} returns obj
  112. */
  113. applyIf : function(o, c){
  114. if(o && c){
  115. for(var p in c){
  116. if(typeof o[p] == "undefined"){ o[p] = c[p]; }
  117. }
  118. }
  119. return o;
  120. },
  121. /**
  122. * Applies event listeners to elements by selectors when the document is ready.
  123. * The event name is specified with an @ suffix.
  124. <pre><code>
  125. Ext.addBehaviors({
  126. // add a listener for click on all anchors in element with id foo
  127. '#foo a@click' : function(e, t){
  128. // do something
  129. },
  130. // add the same listener to multiple selectors (separated by comma BEFORE the @)
  131. '#foo a, #bar span.some-class@mouseover' : function(){
  132. // do something
  133. }
  134. });
  135. </code></pre>
  136. * @param {Object} obj The list of behaviors to apply
  137. */
  138. addBehaviors : function(o){
  139. if(!Ext.isReady){
  140. Ext.onReady(function(){
  141. Ext.addBehaviors(o);
  142. });
  143. return;
  144. }
  145. var cache = {}; // simple cache for applying multiple behaviors to same selector does query multiple times
  146. for(var b in o){
  147. var parts = b.split('@');
  148. if(parts[1]){ // for Object prototype breakers
  149. var s = parts[0];
  150. if(!cache[s]){
  151. cache[s] = Ext.select(s);
  152. }
  153. cache[s].on(parts[1], o[b]);
  154. }
  155. }
  156. cache = null;
  157. },
  158. /**
  159. * Generates unique ids. If the element already has an id, it is unchanged
  160. * @param {Mixed} el (optional) The element to generate an id for
  161. * @param {String} prefix (optional) Id prefix (defaults "ext-gen")
  162. * @return {String} The generated Id.
  163. */
  164. id : function(el, prefix){
  165. prefix = prefix || "ext-gen";
  166. el = Ext.getDom(el);
  167. var id = prefix + (++idSeed);
  168. return el ? (el.id ? el.id : (el.id = id)) : id;
  169. },
  170. /**
  171. * Extends one class with another class and optionally overrides members with the passed literal. This class
  172. * also adds the function "override()" to the class that can be used to override
  173. * members on an instance.
  174. * * <p>
  175. * This function also supports a 2-argument call in which the subclass's constructor is
  176. * not passed as an argument. In this form, the parameters are as follows:</p><p>
  177. * <div class="mdetail-params"><ul>
  178. * <li><code>superclass</code>
  179. * <div class="sub-desc">The class being extended</div></li>
  180. * <li><code>overrides</code>
  181. * <div class="sub-desc">A literal with members which are copied into the subclass's
  182. * prototype, and are therefore shared among all instances of the new class.<p>
  183. * This may contain a special member named <tt><b>constructor</b></tt>. This is used
  184. * to define the constructor of the new class, and is returned. If this property is
  185. * <i>not</i> specified, a constructor is generated and returned which just calls the
  186. * superclass's constructor passing on its parameters.</p></div></li>
  187. * </ul></div></p><p>
  188. * For example, to create a subclass of the Ext GridPanel:
  189. * <pre><code>
  190. MyGridPanel = Ext.extend(Ext.grid.GridPanel, {
  191. constructor: function(config) {
  192. // Your preprocessing here
  193. MyGridPanel.superclass.constructor.apply(this, arguments);
  194. // Your postprocessing here
  195. },
  196. yourMethod: function() {
  197. // etc.
  198. }
  199. });
  200. </code></pre>
  201. * </p>
  202. * @param {Function} subclass The class inheriting the functionality
  203. * @param {Function} superclass The class being extended
  204. * @param {Object} overrides (optional) A literal with members which are copied into the subclass's
  205. * prototype, and are therefore shared between all instances of the new class.
  206. * @return {Function} The subclass constructor.
  207. * @method extend
  208. */
  209. extend : function(){
  210. // inline overrides
  211. var io = function(o){
  212. for(var m in o){
  213. this[m] = o[m];
  214. }
  215. };
  216. var oc = Object.prototype.constructor;
  217. return function(sb, sp, overrides){
  218. if(typeof sp == 'object'){
  219. overrides = sp;
  220. sp = sb;
  221. sb = overrides.constructor != oc ? overrides.constructor : function(){sp.apply(this, arguments);};
  222. }
  223. var F = function(){}, sbp, spp = sp.prototype;
  224. F.prototype = spp;
  225. sbp = sb.prototype = new F();
  226. sbp.constructor=sb;
  227. sb.superclass=spp;
  228. if(spp.constructor == oc){
  229. spp.constructor=sp;
  230. }
  231. sb.override = function(o){
  232. Ext.override(sb, o);
  233. };
  234. sbp.override = io;
  235. Ext.override(sb, overrides);
  236. sb.extend = function(o){Ext.extend(sb, o);};
  237. return sb;
  238. };
  239. }(),
  240. /**
  241. * Adds a list of functions to the prototype of an existing class, overwriting any existing methods with the same name.
  242. * Usage:<pre><code>
  243. Ext.override(MyClass, {
  244. newMethod1: function(){
  245. // etc.
  246. },
  247. newMethod2: function(foo){
  248. // etc.
  249. }
  250. });
  251. </code></pre>
  252. * @param {Object} origclass The class to override
  253. * @param {Object} overrides The list of functions to add to origClass. This should be specified as an object literal
  254. * containing one or more methods.
  255. * @method override
  256. */
  257. override : function(origclass, overrides){
  258. if(overrides){
  259. var p = origclass.prototype;
  260. for(var method in overrides){
  261. p[method] = overrides[method];
  262. }
  263. if(Ext.isIE && overrides.toString != origclass.toString){
  264. p.toString = overrides.toString;
  265. }
  266. }
  267. },
  268. /**
  269. * Creates namespaces to be used for scoping variables and classes so that they are not global. Usage:
  270. * <pre><code>
  271. Ext.namespace('Company', 'Company.data');
  272. Company.Widget = function() { ... }
  273. Company.data.CustomStore = function(config) { ... }
  274. </code></pre>
  275. * @param {String} namespace1
  276. * @param {String} namespace2
  277. * @param {String} etc
  278. * @method namespace
  279. */
  280. namespace : function(){
  281. var a=arguments, o=null, i, j, d, rt;
  282. for (i=0; i<a.length; ++i) {
  283. d=a[i].split(".");
  284. rt = d[0];
  285. eval('if (typeof ' + rt + ' == "undefined"){' + rt + ' = {};} o = ' + rt + ';');
  286. for (j=1; j<d.length; ++j) {
  287. o[d[j]]=o[d[j]] || {};
  288. o=o[d[j]];
  289. }
  290. }
  291. },
  292. /**
  293. * Takes an object and converts it to an encoded URL. e.g. Ext.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.
  294. * @param {Object} o
  295. * @return {String}
  296. */
  297. urlEncode : function(o){
  298. if(!o){
  299. return "";
  300. }
  301. var buf = [];
  302. for(var key in o){
  303. var ov = o[key], k = encodeURIComponent(key);
  304. var type = typeof ov;
  305. if(type == 'undefined'){
  306. buf.push(k, "=&");
  307. }else if(type != "function" && type != "object"){
  308. buf.push(k, "=", encodeURIComponent(ov), "&");
  309. }else if(Ext.isDate(ov)){
  310. var s = Ext.encode(ov).replace(/"/g, '');
  311. buf.push(k, "=", s, "&");
  312. }else if(Ext.isArray(ov)){
  313. if (ov.length) {
  314. for(var i = 0, len = ov.length; i < len; i++) {
  315. buf.push(k, "=", encodeURIComponent(ov[i] === undefined ? '' : ov[i]), "&");
  316. }
  317. } else {
  318. buf.push(k, "=&");
  319. }
  320. }
  321. }
  322. buf.pop();
  323. return buf.join("");
  324. },
  325. /**
  326. * Takes an encoded URL and and converts it to an object. Example: <pre><code>
  327. Ext.urlDecode("foo=1&bar=2"); // returns {foo: "1", bar: "2"}
  328. Ext.urlDecode("foo=1&bar=2&bar=3&bar=4", false); // returns {foo: "1", bar: ["2", "3", "4"]}
  329. * </code></pre>
  330. * @param {String} string
  331. * @param {Boolean} overwrite (optional) Items of the same name will overwrite previous values instead of creating an an array (Defaults to false).
  332. * @return {Object} A literal with members
  333. */
  334. urlDecode : function(string, overwrite){
  335. if(!string || !string.length){
  336. return {};
  337. }
  338. var obj = {};
  339. var pairs = string.split('&');
  340. var pair, name, value;
  341. for(var i = 0, len = pairs.length; i < len; i++){
  342. pair = pairs[i].split('=');
  343. name = decodeURIComponent(pair[0]);
  344. value = decodeURIComponent(pair[1]);
  345. if(overwrite !== true){
  346. if(typeof obj[name] == "undefined"){
  347. obj[name] = value;
  348. }else if(typeof obj[name] == "string"){
  349. obj[name] = [obj[name]];
  350. obj[name].push(value);
  351. }else{
  352. obj[name].push(value);
  353. }
  354. }else{
  355. obj[name] = value;
  356. }
  357. }
  358. return obj;
  359. },
  360. /**
  361. * Iterates an array calling the passed function with each item, stopping if your function returns false. If the
  362. * passed array is not really an array, your function is called once with it.
  363. * The supplied function is called with (Object item, Number index, Array allItems).
  364. * @param {Array/NodeList/Mixed} array
  365. * @param {Function} fn
  366. * @param {Object} scope
  367. */
  368. each : function(array, fn, scope){
  369. if(typeof array.length == "undefined" || typeof array == "string"){
  370. array = [array];
  371. }
  372. for(var i = 0, len = array.length; i < len; i++){
  373. if(fn.call(scope || array[i], array[i], i, array) === false){ return i; };
  374. }
  375. },
  376. // deprecated
  377. combine : function(){
  378. var as = arguments, l = as.length, r = [];
  379. for(var i = 0; i < l; i++){
  380. var a = as[i];
  381. if(Ext.isArray(a)){
  382. r = r.concat(a);
  383. }else if(a.length !== undefined && !a.substr){
  384. r = r.concat(Array.prototype.slice.call(a, 0));
  385. }else{
  386. r.push(a);
  387. }
  388. }
  389. return r;
  390. },
  391. /**
  392. * Escapes the passed string for use in a regular expression
  393. * @param {String} str
  394. * @return {String}
  395. */
  396. escapeRe : function(s) {
  397. return s.replace(/([.*+?^${}()|[\]\/\\])/g, "\\$1");
  398. },
  399. // internal
  400. callback : function(cb, scope, args, delay){
  401. if(typeof cb == "function"){
  402. if(delay){
  403. cb.defer(delay, scope, args || []);
  404. }else{
  405. cb.apply(scope, args || []);
  406. }
  407. }
  408. },
  409. /**
  410. * Return the dom node for the passed string (id), dom node, or Ext.Element
  411. * @param {Mixed} el
  412. * @return HTMLElement
  413. */
  414. getDom : function(el){
  415. if(!el || !document){
  416. return null;
  417. }
  418. return el.dom ? el.dom : (typeof el == 'string' ? document.getElementById(el) : el);
  419. },
  420. /**
  421. * Returns the current HTML document object as an {@link Ext.Element}.
  422. * @return Ext.Element The document
  423. */
  424. getDoc : function(){
  425. return Ext.get(document);
  426. },
  427. /**
  428. * Returns the current document body as an {@link Ext.Element}.
  429. * @return Ext.Element The document body
  430. */
  431. getBody : function(){
  432. return Ext.get(document.body || document.documentElement);
  433. },
  434. /**
  435. * Shorthand for {@link Ext.ComponentMgr#get}
  436. * @param {String} id
  437. * @return Ext.Component
  438. */
  439. getCmp : function(id){
  440. return Ext.ComponentMgr.get(id);
  441. },
  442. /**
  443. * Utility method for validating that a value is numeric, returning the specified default value if it is not.
  444. * @param {Mixed} value Should be a number, but any type will be handled appropriately
  445. * @param {Number} defaultValue The value to return if the original value is non-numeric
  446. * @return {Number} Value, if numeric, else defaultValue
  447. */
  448. num : function(v, defaultValue){
  449. v = Number(v == null? NaN : v);
  450. return isNaN(v)? defaultValue : v;
  451. },
  452. /**
  453. * Attempts to destroy any objects passed to it by removing all event listeners, removing them from the
  454. * DOM (if applicable) and calling their destroy functions (if available). This method is primarily
  455. * intended for arguments of type {@link Ext.Element} and {@link Ext.Component}, but any subclass of
  456. * {@link Ext.util.Observable} can be passed in. Any number of elements and/or components can be
  457. * passed into this function in a single call as separate arguments.
  458. * @param {Mixed} arg1 An {@link Ext.Element} or {@link Ext.Component} to destroy
  459. * @param {Mixed} arg2 (optional)
  460. * @param {Mixed} etc... (optional)
  461. */
  462. destroy : function(){
  463. for(var i = 0, a = arguments, len = a.length; i < len; i++) {
  464. var as = a[i];
  465. if(as){
  466. if(typeof as.destroy == 'function'){
  467. as.destroy();
  468. }
  469. else if(as.dom){
  470. as.removeAllListeners();
  471. as.remove();
  472. }
  473. }
  474. }
  475. },
  476. /**
  477. * Removes a DOM node from the document. The body node will be ignored if passed in.
  478. * @param {HTMLElement} node The node to remove
  479. */
  480. removeNode : isIE ? function(){
  481. var d;
  482. return function(n){
  483. if(n && n.tagName != 'BODY'){
  484. d = d || document.createElement('div');
  485. d.appendChild(n);
  486. d.innerHTML = '';
  487. }
  488. }
  489. }() : function(n){
  490. if(n && n.parentNode && n.tagName != 'BODY'){
  491. n.parentNode.removeChild(n);
  492. }
  493. },
  494. // inpired by a similar function in mootools library
  495. /**
  496. * Returns the type of object that is passed in. If the object passed in is null or undefined it
  497. * return false otherwise it returns one of the following values:<ul>
  498. * <li><b>string</b>: If the object passed is a string</li>
  499. * <li><b>number</b>: If the object passed is a number</li>
  500. * <li><b>boolean</b>: If the object passed is a boolean value</li>
  501. * <li><b>date</b>: If the object passed is a Date object</li>
  502. * <li><b>function</b>: If the object passed is a function reference</li>
  503. * <li><b>object</b>: If the object passed is an object</li>
  504. * <li><b>array</b>: If the object passed is an array</li>
  505. * <li><b>regexp</b>: If the object passed is a regular expression</li>
  506. * <li><b>element</b>: If the object passed is a DOM Element</li>
  507. * <li><b>nodelist</b>: If the object passed is a DOM NodeList</li>
  508. * <li><b>textnode</b>: If the object passed is a DOM text node and contains something other than whitespace</li>
  509. * <li><b>whitespace</b>: If the object passed is a DOM text node and contains only whitespace</li>
  510. * @param {Mixed} object
  511. * @return {String}
  512. */
  513. type : function(o){
  514. if(o === undefined || o === null){
  515. return false;
  516. }
  517. if(o.htmlElement){
  518. return 'element';
  519. }
  520. var t = typeof o;
  521. if(t == 'object' && o.nodeName) {
  522. switch(o.nodeType) {
  523. case 1: return 'element';
  524. case 3: return (/\S/).test(o.nodeValue) ? 'textnode' : 'whitespace';
  525. }
  526. }
  527. if(t == 'object' || t == 'function') {
  528. switch(o.constructor) {
  529. case Array: return 'array';
  530. case RegExp: return 'regexp';
  531. case Date: return 'date';
  532. }
  533. if(typeof o.length == 'number' && typeof o.item == 'function') {
  534. return 'nodelist';
  535. }
  536. }
  537. return t;
  538. },
  539. /**
  540. * Returns true if the passed value is null, undefined or an empty string.
  541. * @param {Mixed} value The value to test
  542. * @param {Boolean} allowBlank (optional) true to allow empty strings (defaults to false)
  543. * @return {Boolean}
  544. */
  545. isEmpty : function(v, allowBlank){
  546. return v === null || v === undefined || (!allowBlank ? v === '' : false);
  547. },
  548. /**
  549. * Utility method for validating that a value is non-empty (i.e. i) not null, ii) not undefined, and iii) not an empty string),
  550. * returning the specified default value if it is.
  551. * @param {Mixed} value The value to test
  552. * @param {Mixed} defaultValue The value to return if the original value is empty
  553. * @param {Boolean} allowBlank (optional) true to allow empty strings (defaults to false)
  554. * @return {Mixed} value, if non-empty, else defaultValue
  555. */
  556. value : function(v, defaultValue, allowBlank){
  557. return Ext.isEmpty(v, allowBlank) ? defaultValue : v;
  558. },
  559. /**
  560. * Returns true if the passed object is a JavaScript array, otherwise false.
  561. * @param {Object} object The object to test
  562. * @return {Boolean}
  563. */
  564. isArray : function(v){
  565. return v && typeof v.length == 'number' && typeof v.splice == 'function';
  566. },
  567. /**
  568. * Returns true if the passed object is a JavaScript date object, otherwise false.
  569. * @param {Object} object The object to test
  570. * @return {Boolean}
  571. */
  572. isDate : function(v){
  573. return v && typeof v.getFullYear == 'function';
  574. },
  575. /**
  576. * True if the detected browser is Opera.
  577. * @type Boolean
  578. */
  579. isOpera : isOpera,
  580. /**
  581. * True if the detected browser uses WebKit.
  582. * @type Boolean
  583. */
  584. isWebKit: isWebKit,
  585. /**
  586. * True if the detected browser is Chrome.
  587. * @type Boolean
  588. */
  589. isChrome : isChrome,
  590. /**
  591. * True if the detected browser is Safari.
  592. * @type Boolean
  593. */
  594. isSafari : isSafari,
  595. /**
  596. * True if the detected browser is Safari 3.x.
  597. * @type Boolean
  598. */
  599. isSafari3 : isSafari3,
  600. /**
  601. * True if the detected browser is Safari 2.x.
  602. * @type Boolean
  603. */
  604. isSafari2 : isSafari && !isSafari3,
  605. /**
  606. * True if the detected browser is Internet Explorer.
  607. * @type Boolean
  608. */
  609. isIE : isIE,
  610. /**
  611. * True if the detected browser is Internet Explorer 6.x.
  612. * @type Boolean
  613. */
  614. isIE6 : isIE && !isIE7 && !isIE8,
  615. /**
  616. * True if the detected browser is Internet Explorer 7.x.
  617. * @type Boolean
  618. */
  619. isIE7 : isIE7,
  620. /**
  621. * True if the detected browser is Internet Explorer 8.x.
  622. * @type Boolean
  623. */
  624. isIE8 : isIE8,
  625. /**
  626. * True if the detected browser uses the Gecko layout engine (e.g. Mozilla, Firefox).
  627. * @type Boolean
  628. */
  629. isGecko : isGecko,
  630. /**
  631. * True if the detected browser uses a pre-Gecko 1.9 layout engine (e.g. Firefox 2.x).
  632. * @type Boolean
  633. */
  634. isGecko2 : isGecko && !isGecko3,
  635. /**
  636. * True if the detected browser uses a Gecko 1.9+ layout engine (e.g. Firefox 3.x).
  637. * @type Boolean
  638. */
  639. isGecko3 : isGecko3,
  640. /**
  641. * True if the detected browser is Internet Explorer running in non-strict mode.
  642. * @type Boolean
  643. */
  644. isBorderBox : isBorderBox,
  645. /**
  646. * True if the detected platform is Linux.
  647. * @type Boolean
  648. */
  649. isLinux : isLinux,
  650. /**
  651. * True if the detected platform is Windows.
  652. * @type Boolean
  653. */
  654. isWindows : isWindows,
  655. /**
  656. * True if the detected platform is Mac OS.
  657. * @type Boolean
  658. */
  659. isMac : isMac,
  660. /**
  661. * True if the detected platform is Adobe Air.
  662. * @type Boolean
  663. */
  664. isAir : isAir,
  665. /**
  666. * By default, Ext intelligently decides whether floating elements should be shimmed. If you are using flash,
  667. * you may want to set this to true.
  668. * @type Boolean
  669. */
  670. useShims : ((isIE && !(isIE7 || isIE8)) || (isMac && isGecko && !isGecko3))
  671. });
  672. // in intellij using keyword "namespace" causes parsing errors
  673. Ext.ns = Ext.namespace;
  674. })();
  675. Ext.ns("Ext", "Ext.util", "Ext.grid", "Ext.dd", "Ext.tree", "Ext.data",
  676. "Ext.form", "Ext.menu", "Ext.state", "Ext.lib", "Ext.layout", "Ext.app", "Ext.ux");
  677. /**
  678. * @class Function
  679. * These functions are available on every Function object (any JavaScript function).
  680. */
  681. Ext.apply(Function.prototype, {
  682. /**
  683. * Creates a callback that passes arguments[0], arguments[1], arguments[2], ...
  684. * Call directly on any function. Example: <code>myFunction.createCallback(arg1, arg2)</code>
  685. * Will create a function that is bound to those 2 args. <b>If a specific scope is required in the
  686. * callback, use {@link #createDelegate} instead.</b> The function returned by createCallback always
  687. * executes in the window scope.
  688. * <p>This method is required when you want to pass arguments to a callback function. If no arguments
  689. * are needed, you can simply pass a reference to the function as a callback (e.g., callback: myFn).
  690. * However, if you tried to pass a function with arguments (e.g., callback: myFn(arg1, arg2)) the function
  691. * would simply execute immediately when the code is parsed. Example usage:
  692. * <pre><code>
  693. var sayHi = function(name){
  694. alert('Hi, ' + name);
  695. }
  696. // clicking the button alerts "Hi, Fred"
  697. new Ext.Button({
  698. text: 'Say Hi',
  699. renderTo: Ext.getBody(),
  700. handler: sayHi.createCallback('Fred')
  701. });
  702. </code></pre>
  703. * @return {Function} The new function
  704. */
  705. createCallback : function(/*args...*/){
  706. // make args available, in function below
  707. var args = arguments;
  708. var method = this;
  709. return function() {
  710. return method.apply(window, args);
  711. };
  712. },
  713. /**
  714. * Creates a delegate (callback) that sets the scope to obj.
  715. * Call directly on any function. Example: <code>this.myFunction.createDelegate(this, [arg1, arg2])</code>
  716. * Will create a function that is automatically scoped to obj so that the <tt>this</tt> variable inside the
  717. * callback points to obj. Example usage:
  718. * <pre><code>
  719. var sayHi = function(name){
  720. // Note this use of "this.text" here. This function expects to
  721. // execute within a scope that contains a text property. In this
  722. // example, the "this" variable is pointing to the btn object that
  723. // was passed in createDelegate below.
  724. alert('Hi, ' + name + '. You clicked the "' + this.text + '" button.');
  725. }
  726. var btn = new Ext.Button({
  727. text: 'Say Hi',
  728. renderTo: Ext.getBody()
  729. });
  730. // This callback will execute in the scope of the
  731. // button instance. Clicking the button alerts
  732. // "Hi, Fred. You clicked the "Say Hi" button."
  733. btn.on('click', sayHi.createDelegate(btn, ['Fred']));
  734. </code></pre>
  735. * @param {Object} obj (optional) The object for which the scope is set
  736. * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
  737. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  738. * if a number the args are inserted at the specified position
  739. * @return {Function} The new function
  740. */
  741. createDelegate : function(obj, args, appendArgs){
  742. var method = this;
  743. return function() {
  744. var callArgs = args || arguments;
  745. if(appendArgs === true){
  746. callArgs = Array.prototype.slice.call(arguments, 0);
  747. callArgs = callArgs.concat(args);
  748. }else if(typeof appendArgs == "number"){
  749. callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
  750. var applyArgs = [appendArgs, 0].concat(args); // create method call params
  751. Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
  752. }
  753. return method.apply(obj || window, callArgs);
  754. };
  755. },
  756. /**
  757. * Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
  758. * <pre><code>
  759. var sayHi = function(name){
  760. alert('Hi, ' + name);
  761. }
  762. // executes immediately:
  763. sayHi('Fred');
  764. // executes after 2 seconds:
  765. sayHi.defer(2000, this, ['Fred']);
  766. // this syntax is sometimes useful for deferring
  767. // execution of an anonymous function:
  768. (function(){
  769. alert('Anonymous');
  770. }).defer(100);
  771. </code></pre>
  772. * @param {Number} millis The number of milliseconds for the setTimeout call (if 0 the function is executed immediately)
  773. * @param {Object} obj (optional) The object for which the scope is set
  774. * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
  775. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  776. * if a number the args are inserted at the specified position
  777. * @return {Number} The timeout id that can be used with clearTimeout
  778. */
  779. defer : function(millis, obj, args, appendArgs){
  780. var fn = this.createDelegate(obj, args, appendArgs);
  781. if(millis){
  782. return setTimeout(fn, millis);
  783. }
  784. fn();
  785. return 0;
  786. },
  787. /**
  788. * Create a combined function call sequence of the original function + the passed function.
  789. * The resulting function returns the results of the original function.
  790. * The passed fcn is called with the parameters of the original function. Example usage:
  791. * <pre><code>
  792. var sayHi = function(name){
  793. alert('Hi, ' + name);
  794. }
  795. sayHi('Fred'); // alerts "Hi, Fred"
  796. var sayGoodbye = sayHi.createSequence(function(name){
  797. alert('Bye, ' + name);
  798. });
  799. sayGoodbye('Fred'); // both alerts show
  800. </code></pre>
  801. * @param {Function} fcn The function to sequence
  802. * @param {Object} scope (optional) The scope of the passed fcn (Defaults to scope of original function or window)
  803. * @return {Function} The new function
  804. */
  805. createSequence : function(fcn, scope){
  806. if(typeof fcn != "function"){
  807. return this;
  808. }
  809. var method = this;
  810. return function() {
  811. var retval = method.apply(this || window, arguments);
  812. fcn.apply(scope || this || window, arguments);
  813. return retval;
  814. };
  815. },
  816. /**
  817. * Creates an interceptor function. The passed fcn is called before the original one. If it returns false,
  818. * the original one is not called. The resulting function returns the results of the original function.
  819. * The passed fcn is called with the parameters of the original function. Example usage:
  820. * <pre><code>
  821. var sayHi = function(name){
  822. alert('Hi, ' + name);
  823. }
  824. sayHi('Fred'); // alerts "Hi, Fred"
  825. // create a new function that validates input without
  826. // directly modifying the original function:
  827. var sayHiToFriend = sayHi.createInterceptor(function(name){
  828. return name == 'Brian';
  829. });
  830. sayHiToFriend('Fred'); // no alert
  831. sayHiToFriend('Brian'); // alerts "Hi, Brian"
  832. </code></pre>
  833. * @param {Function} fcn The function to call before the original
  834. * @param {Object} scope (optional) The scope of the passed fcn (Defaults to scope of original function or window)
  835. * @return {Function} The new function
  836. */
  837. createInterceptor : function(fcn, scope){
  838. if(typeof fcn != "function"){
  839. return this;
  840. }
  841. var method = this;
  842. return function() {
  843. fcn.target = this;
  844. fcn.method = method;
  845. if(fcn.apply(scope || this || window, arguments) === false){
  846. return;
  847. }
  848. return method.apply(this || window, arguments);
  849. };
  850. }
  851. });
  852. /**
  853. * @class String
  854. * These functions are available as static methods on the JavaScript String object.
  855. */
  856. Ext.applyIf(String, {
  857. /**
  858. * Escapes the passed string for ' and \
  859. * @param {String} string The string to escape
  860. * @return {String} The escaped string
  861. * @static
  862. */
  863. escape : function(string) {
  864. return string.replace(/('|\\)/g, "\\$1");
  865. },
  866. /**
  867. * Pads the left side of a string with a specified character. This is especially useful
  868. * for normalizing number and date strings. Example usage:
  869. * <pre><code>
  870. var s = String.leftPad('123', 5, '0');
  871. // s now contains the string: '00123'
  872. </code></pre>
  873. * @param {String} string The original string
  874. * @param {Number} size The total length of the output string
  875. * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ")
  876. * @return {String} The padded string
  877. * @static
  878. */
  879. leftPad : function (val, size, ch) {
  880. var result = new String(val);
  881. if(!ch) {
  882. ch = " ";
  883. }
  884. while (result.length < size) {
  885. result = ch + result;
  886. }
  887. return result.toString();
  888. },
  889. /**
  890. * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each
  891. * token must be unique, and must increment in the format {0}, {1}, etc. Example usage:
  892. * <pre><code>
  893. var cls = 'my-class', text = 'Some text';
  894. var s = String.format('&lt;div class="{0}">{1}&lt;/div>', cls, text);
  895. // s now contains the string: '&lt;div class="my-class">Some text&lt;/div>'
  896. </code></pre>
  897. * @param {String} string The tokenized string to be formatted
  898. * @param {String} value1 The value to replace token {0}
  899. * @param {String} value2 Etc...
  900. * @return {String} The formatted string
  901. * @static
  902. */
  903. format : function(format){
  904. var args = Array.prototype.slice.call(arguments, 1);
  905. return format.replace(/\{(\d+)\}/g, function(m, i){
  906. return args[i];
  907. });
  908. }
  909. });
  910. /**
  911. * Utility function that allows you to easily switch a string between two alternating values. The passed value
  912. * is compared to the current string, and if they are equal, the other value that was passed in is returned. If
  913. * they are already different, the first value passed in is returned. Note that this method returns the new value
  914. * but does not change the current string.
  915. * <pre><code>
  916. // alternate sort directions
  917. sort = sort.toggle('ASC', 'DESC');
  918. // instead of conditional logic:
  919. sort = (sort == 'ASC' ? 'DESC' : 'ASC');
  920. </code></pre>
  921. * @param {String} value The value to compare to the current string
  922. * @param {String} other The new value to use if the string already equals the first value passed in
  923. * @return {String} The new value
  924. */
  925. String.prototype.toggle = function(value, other){
  926. return this == value ? other : value;
  927. };
  928. /**
  929. * Trims whitespace from either end of a string, leaving spaces within the string intact. Example:
  930. * <pre><code>
  931. var s = ' foo bar ';
  932. alert('-' + s + '-'); //alerts "- foo bar -"
  933. alert('-' + s.trim() + '-'); //alerts "-foo bar-"
  934. </code></pre>
  935. * @return {String} The trimmed string
  936. */
  937. String.prototype.trim = function(){
  938. var re = /^\s+|\s+$/g;
  939. return function(){ return this.replace(re, ""); };
  940. }();
  941. /**
  942. * @class Number
  943. */
  944. Ext.applyIf(Number.prototype, {
  945. /**
  946. * Checks whether or not the current number is within a desired range. If the number is already within the
  947. * range it is returned, otherwise the min or max value is returned depending on which side of the range is
  948. * exceeded. Note that this method returns the constrained value but does not change the current number.
  949. * @param {Number} min The minimum number in the range
  950. * @param {Number} max The maximum number in the range
  951. * @return {Number} The constrained value if outside the range, otherwise the current value
  952. */
  953. constrain : function(min, max){
  954. return Math.min(Math.max(this, min), max);
  955. }
  956. });
  957. /**
  958. * @class Array
  959. */
  960. Ext.applyIf(Array.prototype, {
  961. /**
  962. * Checks whether or not the specified object exists in the array.
  963. * @param {Object} o The object to check for
  964. * @return {Number} The index of o in the array (or -1 if it is not found)
  965. */
  966. indexOf : function(o){
  967. for (var i = 0, len = this.length; i < len; i++){
  968. if(this[i] == o) return i;
  969. }
  970. return -1;
  971. },
  972. /**
  973. * Removes the specified object from the array. If the object is not found nothing happens.
  974. * @param {Object} o The object to remove
  975. * @return {Array} this array
  976. */
  977. remove : function(o){
  978. var index = this.indexOf(o);
  979. if(index != -1){
  980. this.splice(index, 1);
  981. }
  982. return this;
  983. }
  984. });
  985. /**
  986. Returns the number of milliseconds between this date and date
  987. @param {Date} date (optional) Defaults to now
  988. @return {Number} The diff in milliseconds
  989. @member Date getElapsed
  990. */
  991. Date.prototype.getElapsed = function(date) {
  992. return Math.abs((date || new Date()).getTime()-this.getTime());
  993. };