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.

240 lines
9.3 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-Observable'>/**
  19. </span> * @class Ext.util.Observable
  20. */
  21. Ext.apply(Ext.util.Observable.prototype, function(){
  22. // this is considered experimental (along with beforeMethod, afterMethod, removeMethodListener?)
  23. // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
  24. // private
  25. function getMethodEvent(method){
  26. var e = (this.methodEvents = this.methodEvents ||
  27. {})[method], returnValue, v, cancel, obj = this;
  28. if (!e) {
  29. this.methodEvents[method] = e = {};
  30. e.originalFn = this[method];
  31. e.methodName = method;
  32. e.before = [];
  33. e.after = [];
  34. var makeCall = function(fn, scope, args){
  35. if((v = fn.apply(scope || obj, args)) !== undefined){
  36. if (typeof v == 'object') {
  37. if(v.returnValue !== undefined){
  38. returnValue = v.returnValue;
  39. }else{
  40. returnValue = v;
  41. }
  42. cancel = !!v.cancel;
  43. }
  44. else
  45. if (v === false) {
  46. cancel = true;
  47. }
  48. else {
  49. returnValue = v;
  50. }
  51. }
  52. };
  53. this[method] = function(){
  54. var args = Array.prototype.slice.call(arguments, 0),
  55. b;
  56. returnValue = v = undefined;
  57. cancel = false;
  58. for(var i = 0, len = e.before.length; i &lt; len; i++){
  59. b = e.before[i];
  60. makeCall(b.fn, b.scope, args);
  61. if (cancel) {
  62. return returnValue;
  63. }
  64. }
  65. if((v = e.originalFn.apply(obj, args)) !== undefined){
  66. returnValue = v;
  67. }
  68. for(var i = 0, len = e.after.length; i &lt; len; i++){
  69. b = e.after[i];
  70. makeCall(b.fn, b.scope, args);
  71. if (cancel) {
  72. return returnValue;
  73. }
  74. }
  75. return returnValue;
  76. };
  77. }
  78. return e;
  79. }
  80. return {
  81. // these are considered experimental
  82. // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
  83. // adds an 'interceptor' called before the original method
  84. beforeMethod : function(method, fn, scope){
  85. getMethodEvent.call(this, method).before.push({
  86. fn: fn,
  87. scope: scope
  88. });
  89. },
  90. // adds a 'sequence' called after the original method
  91. afterMethod : function(method, fn, scope){
  92. getMethodEvent.call(this, method).after.push({
  93. fn: fn,
  94. scope: scope
  95. });
  96. },
  97. removeMethodListener: function(method, fn, scope){
  98. var e = this.getMethodEvent(method);
  99. for(var i = 0, len = e.before.length; i &lt; len; i++){
  100. if(e.before[i].fn == fn &amp;&amp; e.before[i].scope == scope){
  101. e.before.splice(i, 1);
  102. return;
  103. }
  104. }
  105. for(var i = 0, len = e.after.length; i &lt; len; i++){
  106. if(e.after[i].fn == fn &amp;&amp; e.after[i].scope == scope){
  107. e.after.splice(i, 1);
  108. return;
  109. }
  110. }
  111. },
  112. <span id='Ext-util-Observable-method-relayEvents'> /**
  113. </span> * Relays selected events from the specified Observable as if the events were fired by &lt;tt&gt;&lt;b&gt;this&lt;/b&gt;&lt;/tt&gt;.
  114. * @param {Object} o The Observable whose events this object is to relay.
  115. * @param {Array} events Array of event names to relay.
  116. */
  117. relayEvents : function(o, events){
  118. var me = this;
  119. function createHandler(ename){
  120. return function(){
  121. return me.fireEvent.apply(me, [ename].concat(Array.prototype.slice.call(arguments, 0)));
  122. };
  123. }
  124. for(var i = 0, len = events.length; i &lt; len; i++){
  125. var ename = events[i];
  126. me.events[ename] = me.events[ename] || true;
  127. o.on(ename, createHandler(ename), me);
  128. }
  129. },
  130. <span id='Ext-util-Observable-method-enableBubble'> /**
  131. </span> * &lt;p&gt;Enables events fired by this Observable to bubble up an owner hierarchy by calling
  132. * &lt;code&gt;this.getBubbleTarget()&lt;/code&gt; if present. There is no implementation in the Observable base class.&lt;/p&gt;
  133. * &lt;p&gt;This is commonly used by Ext.Components to bubble events to owner Containers. See {@link Ext.Component.getBubbleTarget}. The default
  134. * implementation in Ext.Component returns the Component's immediate owner. But if a known target is required, this can be overridden to
  135. * access the required target more quickly.&lt;/p&gt;
  136. * &lt;p&gt;Example:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  137. Ext.override(Ext.form.Field, {
  138. // Add functionality to Field&amp;#39;s initComponent to enable the change event to bubble
  139. initComponent : Ext.form.Field.prototype.initComponent.createSequence(function() {
  140. this.enableBubble('change');
  141. }),
  142. // We know that we want Field&amp;#39;s events to bubble directly to the FormPanel.
  143. getBubbleTarget : function() {
  144. if (!this.formPanel) {
  145. this.formPanel = this.findParentByType('form');
  146. }
  147. return this.formPanel;
  148. }
  149. });
  150. var myForm = new Ext.formPanel({
  151. title: 'User Details',
  152. items: [{
  153. ...
  154. }],
  155. listeners: {
  156. change: function() {
  157. // Title goes red if form has been modified.
  158. myForm.header.setStyle('color', 'red');
  159. }
  160. }
  161. });
  162. &lt;/code&gt;&lt;/pre&gt;
  163. * @param {String/Array} events The event name to bubble, or an Array of event names.
  164. */
  165. enableBubble : function(events){
  166. var me = this;
  167. if(!Ext.isEmpty(events)){
  168. events = Ext.isArray(events) ? events : Array.prototype.slice.call(arguments, 0);
  169. for(var i = 0, len = events.length; i &lt; len; i++){
  170. var ename = events[i];
  171. ename = ename.toLowerCase();
  172. var ce = me.events[ename] || true;
  173. if (typeof ce == 'boolean') {
  174. ce = new Ext.util.Event(me, ename);
  175. me.events[ename] = ce;
  176. }
  177. ce.bubble = true;
  178. }
  179. }
  180. }
  181. };
  182. }());
  183. <span id='Ext-util-Observable-static-method-capture'>/**
  184. </span> * Starts capture on the specified Observable. All events will be passed
  185. * to the supplied function with the event name + standard signature of the event
  186. * &lt;b&gt;before&lt;/b&gt; the event is fired. If the supplied function returns false,
  187. * the event will not fire.
  188. * @param {Observable} o The Observable to capture events from.
  189. * @param {Function} fn The function to call when an event is fired.
  190. * @param {Object} scope (optional) The scope (&lt;code&gt;this&lt;/code&gt; reference) in which the function is executed. Defaults to the Observable firing the event.
  191. * @static
  192. */
  193. Ext.util.Observable.capture = function(o, fn, scope){
  194. o.fireEvent = o.fireEvent.createInterceptor(fn, scope);
  195. };
  196. <span id='Ext-util-Observable-static-method-observeClass'>/**
  197. </span> * Sets observability on the passed class constructor.&lt;p&gt;
  198. * &lt;p&gt;This makes any event fired on any instance of the passed class also fire a single event through
  199. * the &lt;i&gt;class&lt;/i&gt; allowing for central handling of events on many instances at once.&lt;/p&gt;
  200. * &lt;p&gt;Usage:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;
  201. Ext.util.Observable.observeClass(Ext.data.Connection);
  202. Ext.data.Connection.on('beforerequest', function(con, options) {
  203. console.log('Ajax request made to ' + options.url);
  204. });&lt;/code&gt;&lt;/pre&gt;
  205. * @param {Function} c The class constructor to make observable.
  206. * @param {Object} listeners An object containing a series of listeners to add. See {@link #addListener}.
  207. * @static
  208. */
  209. Ext.util.Observable.observeClass = function(c, listeners){
  210. if(c){
  211. if(!c.fireEvent){
  212. Ext.apply(c, new Ext.util.Observable());
  213. Ext.util.Observable.capture(c.prototype, c.fireEvent, c);
  214. }
  215. if(typeof listeners == 'object'){
  216. c.on(listeners);
  217. }
  218. return c;
  219. }
  220. };
  221. </pre>
  222. </body>
  223. </html>