fix css for date
[roojs1] / Function.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11
12 (function() {    
13     // wrappedn so fnCleanup is not in global scope...
14     if(Roo.isIE) {
15         function fnCleanUp() {
16             var p = Function.prototype;
17             delete p.createSequence;
18             delete p.defer;
19             delete p.createDelegate;
20             delete p.createCallback;
21             delete p.createInterceptor;
22
23             window.detachEvent("onunload", fnCleanUp);
24         }
25         window.attachEvent("onunload", fnCleanUp);
26     }
27 })();
28
29
30 /**
31  * @class Function
32  * These functions are available on every Function object (any JavaScript function).
33  */
34 Roo.apply(Function.prototype, {
35      /**
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
40     */
41     createCallback : function(/*args...*/){
42         // make args available, in function below
43         var args = arguments;
44         var method = this;
45         return function() {
46             return method.apply(window, args);
47         };
48     },
49
50     /**
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
59      */
60     createDelegate : function(obj, args, appendArgs){
61         var method = this;
62         return function() {
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
71             }
72             return method.apply(obj || window, callArgs);
73         };
74     },
75
76     /**
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
84      */
85     defer : function(millis, obj, args, appendArgs){
86         var fn = this.createDelegate(obj, args, appendArgs);
87         if(millis){
88             return setTimeout(fn, millis);
89         }
90         fn();
91         return 0;
92     },
93     /**
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
100      */
101     createSequence : function(fcn, scope){
102         if(typeof fcn != "function"){
103             return this;
104         }
105         var method = this;
106         return function() {
107             var retval = method.apply(this || window, arguments);
108             fcn.apply(scope || this || window, arguments);
109             return retval;
110         };
111     },
112
113     /**
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.
117      * @addon
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
121      */
122     createInterceptor : function(fcn, scope){
123         if(typeof fcn != "function"){
124             return this;
125         }
126         var method = this;
127         return function() {
128             fcn.target = this;
129             fcn.method = method;
130             if(fcn.apply(scope || this || window, arguments) === false){
131                 return;
132             }
133             return method.apply(this || window, arguments);
134         };
135     }
136 });