📄 ext.js
字号:
/** * @class Function * These functions are available on every Function object (any JavaScript function). */Ext.apply(Function.prototype, { /** * Creates a callback that passes arguments[0], arguments[1], arguments[2], ... * Call directly on any function. Example: <code>myFunction.createCallback(arg1, arg2)</code> * Will create a function that is bound to those 2 args. <b>If a specific scope is required in the * callback, use {@link #createDelegate} instead.</b> The function returned by createCallback always * executes in the window scope. * <p>This method is required when you want to pass arguments to a callback function. If no arguments * are needed, you can simply pass a reference to the function as a callback (e.g., callback: myFn). * However, if you tried to pass a function with arguments (e.g., callback: myFn(arg1, arg2)) the function * would simply execute immediately when the code is parsed. Example usage: * <pre><code>var sayHi = function(name){ alert('Hi, ' + name);}// clicking the button alerts "Hi, Fred"new Ext.Button({ text: 'Say Hi', renderTo: Ext.getBody(), handler: sayHi.createCallback('Fred')});</code></pre> * @return {Function} The new function */ createCallback : function(/*args...*/){ // make args available, in function below var args = arguments; var method = this; return function() { return method.apply(window, args); }; }, /** * Creates a delegate (callback) that sets the scope to obj. * Call directly on any function. Example: <code>this.myFunction.createDelegate(this, [arg1, arg2])</code> * Will create a function that is automatically scoped to obj so that the <tt>this</tt> variable inside the * callback points to obj. Example usage: * <pre><code>var sayHi = function(name){ // Note this use of "this.text" here. This function expects to // execute within a scope that contains a text property. In this // example, the "this" variable is pointing to the btn object that // was passed in createDelegate below. alert('Hi, ' + name + '. You clicked the "' + this.text + '" button.');}var btn = new Ext.Button({ text: 'Say Hi', renderTo: Ext.getBody()});// This callback will execute in the scope of the// button instance. Clicking the button alerts// "Hi, Fred. You clicked the "Say Hi" button."btn.on('click', sayHi.createDelegate(btn, ['Fred']));</code></pre> * @param {Object} obj (optional) The object for which the scope is set * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller) * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding, * if a number the args are inserted at the specified position * @return {Function} The new function */ createDelegate : function(obj, args, appendArgs){ var method = this; return function() { var callArgs = args || arguments; if(appendArgs === true){ callArgs = Array.prototype.slice.call(arguments, 0); callArgs = callArgs.concat(args); }else if(typeof appendArgs == "number"){ callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first var applyArgs = [appendArgs, 0].concat(args); // create method call params Array.prototype.splice.apply(callArgs, applyArgs); // splice them in } return method.apply(obj || window, callArgs); }; }, /** * Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage: * <pre><code>var sayHi = function(name){ alert('Hi, ' + name);}// executes immediately:sayHi('Fred');// executes after 2 seconds:sayHi.defer(2000, this, ['Fred']);// this syntax is sometimes useful for deferring// execution of an anonymous function:(function(){ alert('Anonymous');}).defer(100);</code></pre> * @param {Number} millis The number of milliseconds for the setTimeout call (if 0 the function is executed immediately) * @param {Object} obj (optional) The object for which the scope is set * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller) * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding, * if a number the args are inserted at the specified position * @return {Number} The timeout id that can be used with clearTimeout */ defer : function(millis, obj, args, appendArgs){ var fn = this.createDelegate(obj, args, appendArgs); if(millis){ return setTimeout(fn, millis); } fn(); return 0; }, /** * Create a combined function call sequence of the original function + the passed function. * The resulting function returns the results of the original function. * The passed fcn is called with the parameters of the original function. Example usage: * <pre><code>var sayHi = function(name){ alert('Hi, ' + name);}sayHi('Fred'); // alerts "Hi, Fred"var sayGoodbye = sayHi.createSequence(function(name){ alert('Bye, ' + name);});sayGoodbye('Fred'); // both alerts show</code></pre> * @param {Function} fcn The function to sequence * @param {Object} scope (optional) The scope of the passed fcn (Defaults to scope of original function or window) * @return {Function} The new function */ createSequence : function(fcn, scope){ if(typeof fcn != "function"){ return this; } var method = this; return function() { var retval = method.apply(this || window, arguments); fcn.apply(scope || this || window, arguments); return retval; }; }, /** * Creates an interceptor function. The passed fcn is called before the original one. If it returns false, * the original one is not called. The resulting function returns the results of the original function. * The passed fcn is called with the parameters of the original function. Example usage: * <pre><code>var sayHi = function(name){ alert('Hi, ' + name);}sayHi('Fred'); // alerts "Hi, Fred"// create a new function that validates input without// directly modifying the original function:var sayHiToFriend = sayHi.createInterceptor(function(name){ return name == 'Brian';});sayHiToFriend('Fred'); // no alertsayHiToFriend('Brian'); // alerts "Hi, Brian"</code></pre> * @param {Function} fcn The function to call before the original * @param {Object} scope (optional) The scope of the passed fcn (Defaults to scope of original function or window) * @return {Function} The new function */ createInterceptor : function(fcn, scope){ if(typeof fcn != "function"){ return this; } var method = this; return function() { fcn.target = this; fcn.method = method; if(fcn.apply(scope || this || window, arguments) === false){ return; } return method.apply(this || window, arguments); }; }});/** * @class String * These functions are available as static methods on the JavaScript String object. */Ext.applyIf(String, { /** * Escapes the passed string for ' and \ * @param {String} string The string to escape * @return {String} The escaped string * @static */ escape : function(string) { return string.replace(/('|\\)/g, "\\$1"); }, /** * Pads the left side of a string with a specified character. This is especially useful * for normalizing number and date strings. Example usage: * <pre><code>var s = String.leftPad('123', 5, '0');// s now contains the string: '00123'</code></pre> * @param {String} string The original string * @param {Number} size The total length of the output string * @param {String} char (optional) The character with which to pad the original string (defaults to empty string " ") * @return {String} The padded string * @static */ leftPad : function (val, size, ch) { var result = new String(val); if(!ch) { ch = " "; } while (result.length < size) { result = ch + result; } return result.toString(); }, /** * Allows you to define a tokenized string and pass an arbitrary number of arguments to replace the tokens. Each * token must be unique, and must increment in the format {0}, {1}, etc. Example usage: * <pre><code>var cls = 'my-class', text = 'Some text';var s = String.format('<div class="{0}">{1}</div>', cls, text);// s now contains the string: '<div class="my-class">Some text</div>'</code></pre> * @param {String} string The tokenized string to be formatted * @param {String} value1 The value to replace token {0} * @param {String} value2 Etc... * @return {String} The formatted string * @static */ format : function(format){ var args = Array.prototype.slice.call(arguments, 1); return format.replace(/\{(\d+)\}/g, function(m, i){ return args[i]; }); }});/** * Utility function that allows you to easily switch a string between two alternating values. The passed value * is compared to the current string, and if they are equal, the other value that was passed in is returned. If * they are already different, the first value passed in is returned. Note that this method returns the new value * but does not change the current string. * <pre><code>// alternate sort directionssort = sort.toggle('ASC', 'DESC');// instead of conditional logic:sort = (sort == 'ASC' ? 'DESC' : 'ASC');</code></pre> * @param {String} value The value to compare to the current string * @param {String} other The new value to use if the string already equals the first value passed in * @return {String} The new value */String.prototype.toggle = function(value, other){ return this == value ? other : value;};/** * Trims whitespace from either end of a string, leaving spaces within the string intact. Example: * <pre><code>var s = ' foo bar ';alert('-' + s + '-'); //alerts "- foo bar -"alert('-' + s.trim() + '-'); //alerts "-foo bar-"</code></pre> * @return {String} The trimmed string */String.prototype.trim = function(){ var re = /^\s+|\s+$/g; return function(){ return this.replace(re, ""); };}();/** * @class Number */Ext.applyIf(Number.prototype, { /** * Checks whether or not the current number is within a desired range. If the number is already within the * range it is returned, otherwise the min or max value is returned depending on which side of the range is * exceeded. Note that this method returns the constrained value but does not change the current number. * @param {Number} min The minimum number in the range * @param {Number} max The maximum number in the range * @return {Number} The constrained value if outside the range, otherwise the current value */ constrain : function(min, max){ return Math.min(Math.max(this, min), max); }});/** * @class Array */Ext.applyIf(Array.prototype, { /** * Checks whether or not the specified object exists in the array. * @param {Object} o The object to check for * @return {Number} The index of o in the array (or -1 if it is not found) */ indexOf : function(o){ for (var i = 0, len = this.length; i < len; i++){ if(this[i] == o) return i; } return -1; }, /** * Removes the specified object from the array. If the object is not found nothing happens. * @param {Object} o The object to remove * @return {Array} this array */ remove : function(o){ var index = this.indexOf(o); if(index != -1){ this.splice(index, 1); } return this; }});/** Returns the number of milliseconds between this date and date @param {Date} date (optional) Defaults to now @return {Number} The diff in milliseconds @member Date getElapsed */Date.prototype.getElapsed = function(date) { return Math.abs((date || new Date()).getTime()-this.getTime());};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -