4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-Function'>/**
19 </span> * @class Ext.Function
21 * A collection of useful static methods to deal with function callbacks
27 <span id='Ext-Function-method-flexSetter'> /**
28 </span> * A very commonly used method throughout the framework. It acts as a wrapper around another method
29 * which originally accepts 2 arguments for <code>name</code> and <code>value</code>.
30 * The wrapped function then allows "flexible" value setting of either:
33 * <li><code>name</code> and <code>value</code> as 2 arguments</li>
34 * <li>one single object argument with multiple key - value pairs</li>
38 * <pre><code>
39 var setValue = Ext.Function.flexSetter(function(name, value) {
44 // Setting a single name - value
45 setValue('name1', 'value1');
47 // Settings multiple name - value pairs
53 * </code></pre>
54 * @param {Function} setter
55 * @returns {Function} flexSetter
57 flexSetter: function(fn) {
58 return function(a, b) {
65 if (typeof a !== 'string') {
67 if (a.hasOwnProperty(k)) {
68 fn.call(this, k, a[k]);
72 if (Ext.enumerables) {
73 for (i = Ext.enumerables.length; i--;) {
74 k = Ext.enumerables[i];
75 if (a.hasOwnProperty(k)) {
76 fn.call(this, k, a[k]);
88 <span id='Ext-Function-method-bind'> /**
89 </span> * Create a new function from the provided <code>fn</code>, change <code>this</code> to the provided scope, optionally
90 * overrides arguments for the call. (Defaults to the arguments passed by the caller)
92 * @param {Function} fn The function to delegate.
93 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
94 * <b>If omitted, defaults to the browser window.</b>
95 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
96 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
97 * if a number the args are inserted at the specified position
98 * @return {Function} The new function
100 bind: function(fn, scope, args, appendArgs) {
105 var callArgs = args || arguments;
107 if (appendArgs === true) {
108 callArgs = Array.prototype.slice.call(arguments, 0);
109 callArgs = callArgs.concat(args);
111 else if (Ext.isNumber(appendArgs)) {
112 callArgs = Array.prototype.slice.call(arguments, 0); // copy arguments first
113 applyArgs = [appendArgs, 0].concat(args); // create method call params
114 Array.prototype.splice.apply(callArgs, applyArgs); // splice them in
117 return method.apply(scope || window, callArgs);
121 <span id='Ext-Function-method-pass'> /**
122 </span> * Create a new function from the provided <code>fn</code>, the arguments of which are pre-set to `args`.
123 * New arguments passed to the newly created callback when it's invoked are appended after the pre-set ones.
124 * This is especially useful when creating callbacks.
127 var originalFunction = function(){
128 alert(Ext.Array.from(arguments).join(' '));
131 var callback = Ext.Function.pass(originalFunction, ['Hello', 'World']);
133 callback(); // alerts 'Hello World'
134 callback('by Me'); // alerts 'Hello World by Me'
136 * @param {Function} fn The original function
137 * @param {Array} args The arguments to pass to new callback
138 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
139 * @return {Function} The new callback function
141 pass: function(fn, args, scope) {
143 args = Ext.Array.from(args);
147 return fn.apply(scope, args.concat(Ext.Array.toArray(arguments)));
151 <span id='Ext-Function-method-alias'> /**
152 </span> * Create an alias to the provided method property with name <code>methodName</code> of <code>object</code>.
153 * Note that the execution scope will still be bound to the provided <code>object</code> itself.
155 * @param {Object/Function} object
156 * @param {String} methodName
157 * @return {Function} aliasFn
159 alias: function(object, methodName) {
161 return object[methodName].apply(object, arguments);
165 <span id='Ext-Function-method-createInterceptor'> /**
166 </span> * Creates an interceptor function. The passed function is called before the original one. If it returns false,
167 * the original one is not called. The resulting function returns the results of the original function.
168 * The passed function is called with the parameters of the original function. Example usage:
169 * <pre><code>
170 var sayHi = function(name){
171 alert('Hi, ' + name);
174 sayHi('Fred'); // alerts "Hi, Fred"
176 // create a new function that validates input without
177 // directly modifying the original function:
178 var sayHiToFriend = Ext.Function.createInterceptor(sayHi, function(name){
179 return name == 'Brian';
182 sayHiToFriend('Fred'); // no alert
183 sayHiToFriend('Brian'); // alerts "Hi, Brian"
184 </code></pre>
185 * @param {Function} origFn The original function.
186 * @param {Function} newFn The function to call before the original
187 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the passed function is executed.
188 * <b>If omitted, defaults to the scope in which the original function is called or the browser window.</b>
189 * @param {Mixed} returnValue (optional) The value to return if the passed function return false (defaults to null).
190 * @return {Function} The new function
192 createInterceptor: function(origFn, newFn, scope, returnValue) {
194 if (!Ext.isFunction(newFn)) {
202 newFn.method = origFn;
203 return (newFn.apply(scope || me || window, args) !== false) ? origFn.apply(me || window, args) : returnValue || null;
208 <span id='Ext-Function-method-createDelayed'> /**
209 </span> * Creates a delegate (callback) which, when called, executes after a specific delay.
210 * @param {Function} fn The function which will be called on a delay when the returned function is called.
211 * Optionally, a replacement (or additional) argument list may be specified.
212 * @param {Number} delay The number of milliseconds to defer execution by whenever called.
213 * @param {Object} scope (optional) The scope (<code>this</code> reference) used by the function at execution time.
214 * @param {Array} args (optional) Override arguments for the call. (Defaults to the arguments passed by the caller)
215 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
216 * if a number the args are inserted at the specified position.
217 * @return {Function} A function which, when called, executes the original function after the specified delay.
219 createDelayed: function(fn, delay, scope, args, appendArgs) {
221 fn = Ext.Function.bind(fn, scope, args, appendArgs);
225 setTimeout(function() {
226 fn.apply(me, arguments);
231 <span id='Ext-Function-method-defer'> /**
232 </span> * Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
233 * <pre><code>
234 var sayHi = function(name){
235 alert('Hi, ' + name);
238 // executes immediately:
241 // executes after 2 seconds:
242 Ext.Function.defer(sayHi, 2000, this, ['Fred']);
244 // this syntax is sometimes useful for deferring
245 // execution of an anonymous function:
246 Ext.Function.defer(function(){
249 </code></pre>
250 * @param {Function} fn The function to defer.
251 * @param {Number} millis The number of milliseconds for the setTimeout call (if less than or equal to 0 the function is executed immediately)
252 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the function is executed.
253 * <b>If omitted, defaults to the browser window.</b>
254 * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
255 * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
256 * if a number the args are inserted at the specified position
257 * @return {Number} The timeout id that can be used with clearTimeout
259 defer: function(fn, millis, obj, args, appendArgs) {
260 fn = Ext.Function.bind(fn, obj, args, appendArgs);
262 return setTimeout(fn, millis);
268 <span id='Ext-Function-method-createSequence'> /**
269 </span> * Create a combined function call sequence of the original function + the passed function.
270 * The resulting function returns the results of the original function.
271 * The passed function is called with the parameters of the original function. Example usage:
273 * <pre><code>
274 var sayHi = function(name){
275 alert('Hi, ' + name);
278 sayHi('Fred'); // alerts "Hi, Fred"
280 var sayGoodbye = Ext.Function.createSequence(sayHi, function(name){
281 alert('Bye, ' + name);
284 sayGoodbye('Fred'); // both alerts show
285 * </code></pre>
287 * @param {Function} origFn The original function.
288 * @param {Function} newFn The function to sequence
289 * @param {Object} scope (optional) The scope (this reference) in which the passed function is executed.
290 * If omitted, defaults to the scope in which the original function is called or the browser window.
291 * @return {Function} The new function
293 createSequence: function(origFn, newFn, scope) {
294 if (!Ext.isFunction(newFn)) {
299 var retval = origFn.apply(this || window, arguments);
300 newFn.apply(scope || this || window, arguments);
306 <span id='Ext-Function-method-createBuffered'> /**
307 </span> * <p>Creates a delegate function, optionally with a bound scope which, when called, buffers
308 * the execution of the passed function for the configured number of milliseconds.
309 * If called again within that period, the impending invocation will be canceled, and the
310 * timeout period will begin again.</p>
312 * @param {Function} fn The function to invoke on a buffered timer.
313 * @param {Number} buffer The number of milliseconds by which to buffer the invocation of the
315 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which
316 * the passed function is executed. If omitted, defaults to the scope specified by the caller.
317 * @param {Array} args (optional) Override arguments for the call. Defaults to the arguments
318 * passed by the caller.
319 * @return {Function} A function which invokes the passed function after buffering for the specified time.
321 createBuffered: function(fn, buffer, scope, args) {
327 clearInterval(timerId);
330 timerId = setTimeout(function(){
331 fn.apply(scope || me, args || arguments);
337 <span id='Ext-Function-method-createThrottled'> /**
338 </span> * <p>Creates a throttled version of the passed function which, when called repeatedly and
339 * rapidly, invokes the passed function only after a certain interval has elapsed since the
340 * previous invocation.</p>
342 * <p>This is useful for wrapping functions which may be called repeatedly, such as
343 * a handler of a mouse move event when the processing is expensive.</p>
345 * @param fn {Function} The function to execute at a regular time interval.
346 * @param interval {Number} The interval <b>in milliseconds</b> on which the passed function is executed.
347 * @param scope (optional) The scope (<code><b>this</b></code> reference) in which
348 * the passed function is executed. If omitted, defaults to the scope specified by the caller.
349 * @returns {Function} A function which invokes the passed function at the specified interval.
351 createThrottled: function(fn, interval, scope) {
352 var lastCallTime, elapsed, lastArgs, timer, execute = function() {
353 fn.apply(scope || this, lastArgs);
354 lastCallTime = new Date().getTime();
358 elapsed = new Date().getTime() - lastCallTime;
359 lastArgs = arguments;
362 if (!lastCallTime || (elapsed >= interval)) {
365 timer = setTimeout(execute, interval - elapsed);
371 <span id='Ext-method-defer'>/**
372 </span> * Shorthand for {@link Ext.Function#defer}
376 Ext.defer = Ext.Function.alias(Ext.Function, 'defer');
378 <span id='Ext-method-pass'>/**
379 </span> * Shorthand for {@link Ext.Function#pass}
383 Ext.pass = Ext.Function.alias(Ext.Function, 'pass');
385 <span id='Ext-method-bind'>/**
386 </span> * Shorthand for {@link Ext.Function#bind}
390 Ext.bind = Ext.Function.alias(Ext.Function, 'bind');