You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
11 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-util-Functions'>/**
  19. </span> * @class Ext.util.Functions
  20. * @singleton
  21. */
  22. Ext.util.Functions = {
  23. <span id='Ext-util-Functions-method-createInterceptor'> /**
  24. </span> * Creates an interceptor function. The passed function is called before the original one. If it returns false,
  25. * the original one is not called. The resulting function returns the results of the original function.
  26. * The passed function is called with the parameters of the original function. Example usage:
  27. * &lt;pre&gt;&lt;code&gt;
  28. var sayHi = function(name){
  29. alert('Hi, ' + name);
  30. }
  31. sayHi('Fred'); // alerts &quot;Hi, Fred&quot;
  32. // create a new function that validates input without
  33. // directly modifying the original function:
  34. var sayHiToFriend = Ext.createInterceptor(sayHi, function(name){
  35. return name == 'Brian';
  36. });
  37. sayHiToFriend('Fred'); // no alert
  38. sayHiToFriend('Brian'); // alerts &quot;Hi, Brian&quot;
  39. &lt;/code&gt;&lt;/pre&gt;
  40. * @param {Function} origFn The original function.
  41. * @param {Function} newFn The function to call before the original
  42. * @param {Object} scope (optional) The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the passed function is executed.
  43. * &lt;b&gt;If omitted, defaults to the scope in which the original function is called or the browser window.&lt;/b&gt;
  44. * @return {Function} The new function
  45. */
  46. createInterceptor: function(origFn, newFn, scope) {
  47. var method = origFn;
  48. if (!Ext.isFunction(newFn)) {
  49. return origFn;
  50. }
  51. else {
  52. return function() {
  53. var me = this,
  54. args = arguments;
  55. newFn.target = me;
  56. newFn.method = origFn;
  57. return (newFn.apply(scope || me || window, args) !== false) ?
  58. origFn.apply(me || window, args) :
  59. null;
  60. };
  61. }
  62. },
  63. <span id='Ext-util-Functions-method-createDelegate'> /**
  64. </span> * Creates a delegate (callback) that sets the scope to obj.
  65. * Call directly on any function. Example: &lt;code&gt;Ext.createDelegate(this.myFunction, this, [arg1, arg2])&lt;/code&gt;
  66. * Will create a function that is automatically scoped to obj so that the &lt;tt&gt;this&lt;/tt&gt; variable inside the
  67. * callback points to obj. Example usage:
  68. * &lt;pre&gt;&lt;code&gt;
  69. var sayHi = function(name){
  70. // Note this use of &quot;this.text&quot; here. This function expects to
  71. // execute within a scope that contains a text property. In this
  72. // example, the &quot;this&quot; variable is pointing to the btn object that
  73. // was passed in createDelegate below.
  74. alert('Hi, ' + name + '. You clicked the &quot;' + this.text + '&quot; button.');
  75. }
  76. var btn = new Ext.Button({
  77. text: 'Say Hi',
  78. renderTo: Ext.getBody()
  79. });
  80. // This callback will execute in the scope of the
  81. // button instance. Clicking the button alerts
  82. // &quot;Hi, Fred. You clicked the &quot;Say Hi&quot; button.&quot;
  83. btn.on('click', Ext.createDelegate(sayHi, btn, ['Fred']));
  84. &lt;/code&gt;&lt;/pre&gt;
  85. * @param {Function} fn The function to delegate.
  86. * @param {Object} scope (optional) The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the function is executed.
  87. * &lt;b&gt;If omitted, defaults to the browser window.&lt;/b&gt;
  88. * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
  89. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  90. * if a number the args are inserted at the specified position
  91. * @return {Function} The new function
  92. */
  93. createDelegate: function(fn, obj, args, appendArgs) {
  94. if (!Ext.isFunction(fn)) {
  95. return fn;
  96. }
  97. return function() {
  98. var callArgs = args || arguments;
  99. if (appendArgs === true) {
  100. callArgs = Array.prototype.slice.call(arguments, 0);
  101. callArgs = callArgs.concat(args);
  102. }
  103. else if (Ext.isNumber(appendArgs)) {
  104. callArgs = Array.prototype.slice.call(arguments, 0);
  105. // copy arguments first
  106. var applyArgs = [appendArgs, 0].concat(args);
  107. // create method call params
  108. Array.prototype.splice.apply(callArgs, applyArgs);
  109. // splice them in
  110. }
  111. return fn.apply(obj || window, callArgs);
  112. };
  113. },
  114. <span id='Ext-util-Functions-method-defer'> /**
  115. </span> * Calls this function after the number of millseconds specified, optionally in a specific scope. Example usage:
  116. * &lt;pre&gt;&lt;code&gt;
  117. var sayHi = function(name){
  118. alert('Hi, ' + name);
  119. }
  120. // executes immediately:
  121. sayHi('Fred');
  122. // executes after 2 seconds:
  123. Ext.defer(sayHi, 2000, this, ['Fred']);
  124. // this syntax is sometimes useful for deferring
  125. // execution of an anonymous function:
  126. Ext.defer(function(){
  127. alert('Anonymous');
  128. }, 100);
  129. &lt;/code&gt;&lt;/pre&gt;
  130. * @param {Function} fn The function to defer.
  131. * @param {Number} millis The number of milliseconds for the setTimeout call (if less than or equal to 0 the function is executed immediately)
  132. * @param {Object} scope (optional) The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the function is executed.
  133. * &lt;b&gt;If omitted, defaults to the browser window.&lt;/b&gt;
  134. * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
  135. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  136. * if a number the args are inserted at the specified position
  137. * @return {Number} The timeout id that can be used with clearTimeout
  138. */
  139. defer: function(fn, millis, obj, args, appendArgs) {
  140. fn = Ext.util.Functions.createDelegate(fn, obj, args, appendArgs);
  141. if (millis &gt; 0) {
  142. return setTimeout(fn, millis);
  143. }
  144. fn();
  145. return 0;
  146. },
  147. <span id='Ext-util-Functions-method-createSequence'> /**
  148. </span> * Create a combined function call sequence of the original function + the passed function.
  149. * The resulting function returns the results of the original function.
  150. * The passed fcn is called with the parameters of the original function. Example usage:
  151. *
  152. var sayHi = function(name){
  153. alert('Hi, ' + name);
  154. }
  155. sayHi('Fred'); // alerts &quot;Hi, Fred&quot;
  156. var sayGoodbye = Ext.createSequence(sayHi, function(name){
  157. alert('Bye, ' + name);
  158. });
  159. sayGoodbye('Fred'); // both alerts show
  160. * @param {Function} origFn The original function.
  161. * @param {Function} newFn The function to sequence
  162. * @param {Object} scope (optional) The scope (this reference) in which the passed function is executed.
  163. * If omitted, defaults to the scope in which the original function is called or the browser window.
  164. * @return {Function} The new function
  165. */
  166. createSequence: function(origFn, newFn, scope) {
  167. if (!Ext.isFunction(newFn)) {
  168. return origFn;
  169. }
  170. else {
  171. return function() {
  172. var retval = origFn.apply(this || window, arguments);
  173. newFn.apply(scope || this || window, arguments);
  174. return retval;
  175. };
  176. }
  177. }
  178. };
  179. <span id='Ext-method-defer'>/**
  180. </span> * Shorthand for {@link Ext.util.Functions#defer}
  181. * @param {Function} fn The function to defer.
  182. * @param {Number} millis The number of milliseconds for the setTimeout call (if less than or equal to 0 the function is executed immediately)
  183. * @param {Object} scope (optional) The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the function is executed.
  184. * &lt;b&gt;If omitted, defaults to the browser window.&lt;/b&gt;
  185. * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
  186. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  187. * if a number the args are inserted at the specified position
  188. * @return {Number} The timeout id that can be used with clearTimeout
  189. * @member Ext
  190. * @method defer
  191. */
  192. Ext.defer = Ext.util.Functions.defer;
  193. <span id='Ext-method-createInterceptor'>/**
  194. </span> * Shorthand for {@link Ext.util.Functions#createInterceptor}
  195. * @param {Function} origFn The original function.
  196. * @param {Function} newFn The function to call before the original
  197. * @param {Object} scope (optional) The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the passed function is executed.
  198. * &lt;b&gt;If omitted, defaults to the scope in which the original function is called or the browser window.&lt;/b&gt;
  199. * @return {Function} The new function
  200. * @member Ext
  201. * @method createInterceptor
  202. */
  203. Ext.createInterceptor = Ext.util.Functions.createInterceptor;
  204. <span id='Ext-method-createSequence'>/**
  205. </span> * Shorthand for {@link Ext.util.Functions#createSequence}
  206. * @param {Function} origFn The original function.
  207. * @param {Function} newFn The function to sequence
  208. * @param {Object} scope (optional) The scope (this reference) in which the passed function is executed.
  209. * If omitted, defaults to the scope in which the original function is called or the browser window.
  210. * @return {Function} The new function
  211. * @member Ext
  212. * @method createSequence
  213. */
  214. Ext.createSequence = Ext.util.Functions.createSequence;
  215. <span id='Ext-method-createDelegate'>/**
  216. </span> * Shorthand for {@link Ext.util.Functions#createDelegate}
  217. * @param {Function} fn The function to delegate.
  218. * @param {Object} scope (optional) The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the function is executed.
  219. * &lt;b&gt;If omitted, defaults to the browser window.&lt;/b&gt;
  220. * @param {Array} args (optional) Overrides arguments for the call. (Defaults to the arguments passed by the caller)
  221. * @param {Boolean/Number} appendArgs (optional) if True args are appended to call args instead of overriding,
  222. * if a number the args are inserted at the specified position
  223. * @return {Function} The new function
  224. * @member Ext
  225. * @method createDelegate
  226. */
  227. Ext.createDelegate = Ext.util.Functions.createDelegate;
  228. </pre>
  229. </body>
  230. </html>