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">
13 // wrappedn so fnCleanup is not in global scope...
15 function fnCleanUp() {
16 var p = Function.prototype;
17 delete p.createSequence;
19 delete p.createDelegate;
20 delete p.createCallback;
21 delete p.createInterceptor;
23 window.detachEvent("onunload", fnCleanUp);
25 window.attachEvent("onunload", fnCleanUp);
32 * These functions are available on every Function object (any JavaScript function).
34 Roo.apply(Function.prototype, {
36 * Creates a callback that passes arguments[0], arguments[1], arguments[2], ...
37 * Call directly on any function. Example: <code>myFunction.createCallback(myarg, myarg2)</code>
38 * Will create a function that is bound to those 2 args.
39 * @return {Function} The new function
41 createCallback : function(/*args...*/){
42 // make args available, in function below
46 return method.apply(window, args);
51 * Creates a delegate (callback) that sets the scope to obj.
52 * Call directly on any function. Example: <code>this.myFunction.createDelegate(this)</code>
53 * Will create a function that is automatically scoped to this.
54 * @param {Object} obj (optional) The object for which the scope is set
55 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
56 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
57 * if a number the args are inserted at the specified position
58 * @return {Function} The new function
60 createDelegate : function(obj, args, appendArgs){
63 var callArgs = args || arguments;
64 if(appendArgs === true){
65 callArgs = Array.prototype.slice.call(arguments, 0);
66 callArgs = callArgs.concat(args);
67 }else if(typeof appendArgs == "number"){
68 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
69 var applyArgs = [appendArgs, 0].concat(args); // create method call params
70 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
72 return method.apply(obj || window, callArgs);
77 * Calls this function after the number of millseconds specified.
78 * @param {Number} millis The number of milliseconds for the setTimeout call (if 0 the function is executed immediately)
79 * @param {Object} obj (optional) The object for which the scope is set
80 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
81 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
82 * if a number the args are inserted at the specified position
83 * @return {Number} The timeout id that can be used with clearTimeout
85 defer : function(millis, obj, args, appendArgs){
86 var fn = this.createDelegate(obj, args, appendArgs);
88 return setTimeout(fn, millis);
94 * Create a combined function call sequence of the original function + the passed function.
95 * The resulting function returns the results of the original function.
96 * The passed fcn is called with the parameters of the original function
97 * @param {Function} fcn The function to sequence
98 * @param {Object} scope (optional) The scope of the passed fcn (Defaults to scope of original function or window)
99 * @return {Function} The new function
101 createSequence : function(fcn, scope){
102 if(typeof fcn != "function"){
107 var retval = method.apply(this || window, arguments);
108 fcn.apply(scope || this || window, arguments);
114 * Creates an interceptor function. The passed fcn is called before the original one. If it returns false, the original one is not called.
115 * The resulting function returns the results of the original function.
116 * The passed fcn is called with the parameters of the original function.
118 * @param {Function} fcn The function to call before the original
119 * @param {Object} scope (optional) The scope of the passed fcn (Defaults to scope of original function or window)
120 * @return {Function} The new function
122 createInterceptor : function(fcn, scope){
123 if(typeof fcn != "function"){
130 if(fcn.apply(scope || this || window, arguments) === false){
133 return method.apply(this || window, arguments);