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.

542 lines
19 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">(function(){
  19. var EXTUTIL = Ext.util,
  20. EACH = Ext.each,
  21. TRUE = true,
  22. FALSE = false;
  23. <span id='Ext-util-Observable'>/**
  24. </span> * @class Ext.util.Observable
  25. * Base class that provides a common interface for publishing events. Subclasses are expected to
  26. * to have a property &quot;events&quot; with all the events defined, and, optionally, a property &quot;listeners&quot;
  27. * with configured listeners defined.&lt;br&gt;
  28. * For example:
  29. * &lt;pre&gt;&lt;code&gt;
  30. Employee = Ext.extend(Ext.util.Observable, {
  31. constructor: function(config){
  32. this.name = config.name;
  33. this.addEvents({
  34. &quot;fired&quot; : true,
  35. &quot;quit&quot; : true
  36. });
  37. // Copy configured listeners into *this* object so that the base class&amp;#39;s
  38. // constructor will add them.
  39. this.listeners = config.listeners;
  40. // Call our superclass constructor to complete construction process.
  41. Employee.superclass.constructor.call(this, config)
  42. }
  43. });
  44. &lt;/code&gt;&lt;/pre&gt;
  45. * This could then be used like this:&lt;pre&gt;&lt;code&gt;
  46. var newEmployee = new Employee({
  47. name: employeeName,
  48. listeners: {
  49. quit: function() {
  50. // By default, &quot;this&quot; will be the object that fired the event.
  51. alert(this.name + &quot; has quit!&quot;);
  52. }
  53. }
  54. });
  55. &lt;/code&gt;&lt;/pre&gt;
  56. */
  57. EXTUTIL.Observable = function(){
  58. <span id='Ext-util-Observable-cfg-listeners'> /**
  59. </span> * @cfg {Object} listeners (optional) &lt;p&gt;A config object containing one or more event handlers to be added to this
  60. * object during initialization. This should be a valid listeners config object as specified in the
  61. * {@link #addListener} example for attaching multiple handlers at once.&lt;/p&gt;
  62. * &lt;br&gt;&lt;p&gt;&lt;b&gt;&lt;u&gt;DOM events from ExtJs {@link Ext.Component Components}&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;
  63. * &lt;br&gt;&lt;p&gt;While &lt;i&gt;some&lt;/i&gt; ExtJs Component classes export selected DOM events (e.g. &quot;click&quot;, &quot;mouseover&quot; etc), this
  64. * is usually only done when extra value can be added. For example the {@link Ext.DataView DataView}'s
  65. * &lt;b&gt;&lt;code&gt;{@link Ext.DataView#click click}&lt;/code&gt;&lt;/b&gt; event passing the node clicked on. To access DOM
  66. * events directly from a Component's HTMLElement, listeners must be added to the &lt;i&gt;{@link Ext.Component#getEl Element}&lt;/i&gt; after the Component
  67. * has been rendered. A plugin can simplify this step:&lt;pre&gt;&lt;code&gt;
  68. // Plugin is configured with a listeners config object.
  69. // The Component is appended to the argument list of all handler functions.
  70. Ext.DomObserver = Ext.extend(Object, {
  71. constructor: function(config) {
  72. this.listeners = config.listeners ? config.listeners : config;
  73. },
  74. // Component passes itself into plugin&amp;#39;s init method
  75. init: function(c) {
  76. var p, l = this.listeners;
  77. for (p in l) {
  78. if (Ext.isFunction(l[p])) {
  79. l[p] = this.createHandler(l[p], c);
  80. } else {
  81. l[p].fn = this.createHandler(l[p].fn, c);
  82. }
  83. }
  84. // Add the listeners to the Element immediately following the render call
  85. c.render = c.render.{@link Function#createSequence createSequence}(function() {
  86. var e = c.getEl();
  87. if (e) {
  88. e.on(l);
  89. }
  90. });
  91. },
  92. createHandler: function(fn, c) {
  93. return function(e) {
  94. fn.call(this, e, c);
  95. };
  96. }
  97. });
  98. var combo = new Ext.form.ComboBox({
  99. // Collapse combo when its element is clicked on
  100. plugins: [ new Ext.DomObserver({
  101. click: function(evt, comp) {
  102. comp.collapse();
  103. }
  104. })],
  105. store: myStore,
  106. typeAhead: true,
  107. mode: 'local',
  108. triggerAction: 'all'
  109. });
  110. * &lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
  111. */
  112. var me = this, e = me.events;
  113. if(me.listeners){
  114. me.on(me.listeners);
  115. delete me.listeners;
  116. }
  117. me.events = e || {};
  118. };
  119. EXTUTIL.Observable.prototype = {
  120. // private
  121. filterOptRe : /^(?:scope|delay|buffer|single)$/,
  122. <span id='Ext-util-Observable-method-fireEvent'> /**
  123. </span> * &lt;p&gt;Fires the specified event with the passed parameters (minus the event name).&lt;/p&gt;
  124. * &lt;p&gt;An event may be set to bubble up an Observable parent hierarchy (See {@link Ext.Component#getBubbleTarget})
  125. * by calling {@link #enableBubble}.&lt;/p&gt;
  126. * @param {String} eventName The name of the event to fire.
  127. * @param {Object...} args Variable number of parameters are passed to handlers.
  128. * @return {Boolean} returns false if any of the handlers return false otherwise it returns true.
  129. */
  130. fireEvent : function(){
  131. var a = Array.prototype.slice.call(arguments, 0),
  132. ename = a[0].toLowerCase(),
  133. me = this,
  134. ret = TRUE,
  135. ce = me.events[ename],
  136. cc,
  137. q,
  138. c;
  139. if (me.eventsSuspended === TRUE) {
  140. if (q = me.eventQueue) {
  141. q.push(a);
  142. }
  143. }
  144. else if(typeof ce == 'object') {
  145. if (ce.bubble){
  146. if(ce.fire.apply(ce, a.slice(1)) === FALSE) {
  147. return FALSE;
  148. }
  149. c = me.getBubbleTarget &amp;&amp; me.getBubbleTarget();
  150. if(c &amp;&amp; c.enableBubble) {
  151. cc = c.events[ename];
  152. if(!cc || typeof cc != 'object' || !cc.bubble) {
  153. c.enableBubble(ename);
  154. }
  155. return c.fireEvent.apply(c, a);
  156. }
  157. }
  158. else {
  159. a.shift();
  160. ret = ce.fire.apply(ce, a);
  161. }
  162. }
  163. return ret;
  164. },
  165. <span id='Ext-util-Observable-method-addListener'> /**
  166. </span> * Appends an event handler to this object.
  167. * @param {String} eventName The name of the event to listen for.
  168. * @param {Function} handler The method the event invokes.
  169. * @param {Object} scope (optional) The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the handler function is executed.
  170. * &lt;b&gt;If omitted, defaults to the object which fired the event.&lt;/b&gt;
  171. * @param {Object} options (optional) An object containing handler configuration.
  172. * properties. This may contain any of the following properties:&lt;ul&gt;
  173. * &lt;li&gt;&lt;b&gt;scope&lt;/b&gt; : Object&lt;div class=&quot;sub-desc&quot;&gt;The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the handler function is executed.
  174. * &lt;b&gt;If omitted, defaults to the object which fired the event.&lt;/b&gt;&lt;/div&gt;&lt;/li&gt;
  175. * &lt;li&gt;&lt;b&gt;delay&lt;/b&gt; : Number&lt;div class=&quot;sub-desc&quot;&gt;The number of milliseconds to delay the invocation of the handler after the event fires.&lt;/div&gt;&lt;/li&gt;
  176. * &lt;li&gt;&lt;b&gt;single&lt;/b&gt; : Boolean&lt;div class=&quot;sub-desc&quot;&gt;True to add a handler to handle just the next firing of the event, and then remove itself.&lt;/div&gt;&lt;/li&gt;
  177. * &lt;li&gt;&lt;b&gt;buffer&lt;/b&gt; : Number&lt;div class=&quot;sub-desc&quot;&gt;Causes the handler to be scheduled to run in an {@link Ext.util.DelayedTask} delayed
  178. * by the specified number of milliseconds. If the event fires again within that time, the original
  179. * handler is &lt;em&gt;not&lt;/em&gt; invoked, but the new handler is scheduled in its place.&lt;/div&gt;&lt;/li&gt;
  180. * &lt;li&gt;&lt;b&gt;target&lt;/b&gt; : Observable&lt;div class=&quot;sub-desc&quot;&gt;Only call the handler if the event was fired on the target Observable, &lt;i&gt;not&lt;/i&gt;
  181. * if the event was bubbled up from a child Observable.&lt;/div&gt;&lt;/li&gt;
  182. * &lt;/ul&gt;&lt;br&gt;
  183. * &lt;p&gt;
  184. * &lt;b&gt;Combining Options&lt;/b&gt;&lt;br&gt;
  185. * Using the options argument, it is possible to combine different types of listeners:&lt;br&gt;
  186. * &lt;br&gt;
  187. * A delayed, one-time listener.
  188. * &lt;pre&gt;&lt;code&gt;
  189. myDataView.on('click', this.onClick, this, {
  190. single: true,
  191. delay: 100
  192. });&lt;/code&gt;&lt;/pre&gt;
  193. * &lt;p&gt;
  194. * &lt;b&gt;Attaching multiple handlers in 1 call&lt;/b&gt;&lt;br&gt;
  195. * The method also allows for a single argument to be passed which is a config object containing properties
  196. * which specify multiple handlers.
  197. * &lt;p&gt;
  198. * &lt;pre&gt;&lt;code&gt;
  199. myGridPanel.on({
  200. 'click' : {
  201. fn: this.onClick,
  202. scope: this,
  203. delay: 100
  204. },
  205. 'mouseover' : {
  206. fn: this.onMouseOver,
  207. scope: this
  208. },
  209. 'mouseout' : {
  210. fn: this.onMouseOut,
  211. scope: this
  212. }
  213. });&lt;/code&gt;&lt;/pre&gt;
  214. * &lt;p&gt;
  215. * Or a shorthand syntax:&lt;br&gt;
  216. * &lt;pre&gt;&lt;code&gt;
  217. myGridPanel.on({
  218. 'click' : this.onClick,
  219. 'mouseover' : this.onMouseOver,
  220. 'mouseout' : this.onMouseOut,
  221. scope: this
  222. });&lt;/code&gt;&lt;/pre&gt;
  223. */
  224. addListener : function(eventName, fn, scope, o){
  225. var me = this,
  226. e,
  227. oe,
  228. ce;
  229. if (typeof eventName == 'object') {
  230. o = eventName;
  231. for (e in o) {
  232. oe = o[e];
  233. if (!me.filterOptRe.test(e)) {
  234. me.addListener(e, oe.fn || oe, oe.scope || o.scope, oe.fn ? oe : o);
  235. }
  236. }
  237. } else {
  238. eventName = eventName.toLowerCase();
  239. ce = me.events[eventName] || TRUE;
  240. if (typeof ce == 'boolean') {
  241. me.events[eventName] = ce = new EXTUTIL.Event(me, eventName);
  242. }
  243. ce.addListener(fn, scope, typeof o == 'object' ? o : {});
  244. }
  245. },
  246. <span id='Ext-util-Observable-method-removeListener'> /**
  247. </span> * Removes an event handler.
  248. * @param {String} eventName The type of event the handler was associated with.
  249. * @param {Function} handler The handler to remove. &lt;b&gt;This must be a reference to the function passed into the {@link #addListener} call.&lt;/b&gt;
  250. * @param {Object} scope (optional) The scope originally specified for the handler.
  251. */
  252. removeListener : function(eventName, fn, scope){
  253. var ce = this.events[eventName.toLowerCase()];
  254. if (typeof ce == 'object') {
  255. ce.removeListener(fn, scope);
  256. }
  257. },
  258. <span id='Ext-util-Observable-method-purgeListeners'> /**
  259. </span> * Removes all listeners for this object
  260. */
  261. purgeListeners : function(){
  262. var events = this.events,
  263. evt,
  264. key;
  265. for(key in events){
  266. evt = events[key];
  267. if(typeof evt == 'object'){
  268. evt.clearListeners();
  269. }
  270. }
  271. },
  272. <span id='Ext-util-Observable-method-addEvents'> /**
  273. </span> * Adds the specified events to the list of events which this Observable may fire.
  274. * @param {Object|String} o Either an object with event names as properties with a value of &lt;code&gt;true&lt;/code&gt;
  275. * or the first event name string if multiple event names are being passed as separate parameters.
  276. * @param {string} Optional. Event name if multiple event names are being passed as separate parameters.
  277. * Usage:&lt;pre&gt;&lt;code&gt;
  278. this.addEvents('storeloaded', 'storecleared');
  279. &lt;/code&gt;&lt;/pre&gt;
  280. */
  281. addEvents : function(o){
  282. var me = this;
  283. me.events = me.events || {};
  284. if (typeof o == 'string') {
  285. var a = arguments,
  286. i = a.length;
  287. while(i--) {
  288. me.events[a[i]] = me.events[a[i]] || TRUE;
  289. }
  290. } else {
  291. Ext.applyIf(me.events, o);
  292. }
  293. },
  294. <span id='Ext-util-Observable-method-hasListener'> /**
  295. </span> * Checks to see if this object has any listeners for a specified event
  296. * @param {String} eventName The name of the event to check for
  297. * @return {Boolean} True if the event is being listened for, else false
  298. */
  299. hasListener : function(eventName){
  300. var e = this.events[eventName.toLowerCase()];
  301. return typeof e == 'object' &amp;&amp; e.listeners.length &gt; 0;
  302. },
  303. <span id='Ext-util-Observable-method-suspendEvents'> /**
  304. </span> * Suspend the firing of all events. (see {@link #resumeEvents})
  305. * @param {Boolean} queueSuspended Pass as true to queue up suspended events to be fired
  306. * after the {@link #resumeEvents} call instead of discarding all suspended events;
  307. */
  308. suspendEvents : function(queueSuspended){
  309. this.eventsSuspended = TRUE;
  310. if(queueSuspended &amp;&amp; !this.eventQueue){
  311. this.eventQueue = [];
  312. }
  313. },
  314. <span id='Ext-util-Observable-method-resumeEvents'> /**
  315. </span> * Resume firing events. (see {@link #suspendEvents})
  316. * If events were suspended using the &lt;tt&gt;&lt;b&gt;queueSuspended&lt;/b&gt;&lt;/tt&gt; parameter, then all
  317. * events fired during event suspension will be sent to any listeners now.
  318. */
  319. resumeEvents : function(){
  320. var me = this,
  321. queued = me.eventQueue || [];
  322. me.eventsSuspended = FALSE;
  323. delete me.eventQueue;
  324. EACH(queued, function(e) {
  325. me.fireEvent.apply(me, e);
  326. });
  327. }
  328. };
  329. var OBSERVABLE = EXTUTIL.Observable.prototype;
  330. <span id='Ext-util-Observable-method-on'>/**
  331. </span> * Appends an event handler to this object (shorthand for {@link #addListener}.)
  332. * @param {String} eventName The type of event to listen for
  333. * @param {Function} handler The method the event invokes
  334. * @param {Object} scope (optional) The scope (&lt;code&gt;&lt;b&gt;this&lt;/b&gt;&lt;/code&gt; reference) in which the handler function is executed.
  335. * &lt;b&gt;If omitted, defaults to the object which fired the event.&lt;/b&gt;
  336. * @param {Object} options (optional) An object containing handler configuration.
  337. * @method
  338. */
  339. OBSERVABLE.on = OBSERVABLE.addListener;
  340. <span id='Ext-util-Observable-method-un'>/**
  341. </span> * Removes an event handler (shorthand for {@link #removeListener}.)
  342. * @param {String} eventName The type of event the handler was associated with.
  343. * @param {Function} handler The handler to remove. &lt;b&gt;This must be a reference to the function passed into the {@link #addListener} call.&lt;/b&gt;
  344. * @param {Object} scope (optional) The scope originally specified for the handler.
  345. * @method
  346. */
  347. OBSERVABLE.un = OBSERVABLE.removeListener;
  348. <span id='Ext-util-Observable-static-method-releaseCapture'>/**
  349. </span> * Removes &lt;b&gt;all&lt;/b&gt; added captures from the Observable.
  350. * @param {Observable} o The Observable to release
  351. * @static
  352. */
  353. EXTUTIL.Observable.releaseCapture = function(o){
  354. o.fireEvent = OBSERVABLE.fireEvent;
  355. };
  356. function createTargeted(h, o, scope){
  357. return function(){
  358. if(o.target == arguments[0]){
  359. h.apply(scope, Array.prototype.slice.call(arguments, 0));
  360. }
  361. };
  362. };
  363. function createBuffered(h, o, l, scope){
  364. l.task = new EXTUTIL.DelayedTask();
  365. return function(){
  366. l.task.delay(o.buffer, h, scope, Array.prototype.slice.call(arguments, 0));
  367. };
  368. };
  369. function createSingle(h, e, fn, scope){
  370. return function(){
  371. e.removeListener(fn, scope);
  372. return h.apply(scope, arguments);
  373. };
  374. };
  375. function createDelayed(h, o, l, scope){
  376. return function(){
  377. var task = new EXTUTIL.DelayedTask(),
  378. args = Array.prototype.slice.call(arguments, 0);
  379. if(!l.tasks) {
  380. l.tasks = [];
  381. }
  382. l.tasks.push(task);
  383. task.delay(o.delay || 10, function(){
  384. l.tasks.remove(task);
  385. h.apply(scope, args);
  386. }, scope);
  387. };
  388. };
  389. EXTUTIL.Event = function(obj, name){
  390. this.name = name;
  391. this.obj = obj;
  392. this.listeners = [];
  393. };
  394. EXTUTIL.Event.prototype = {
  395. addListener : function(fn, scope, options){
  396. var me = this,
  397. l;
  398. scope = scope || me.obj;
  399. if(!me.isListening(fn, scope)){
  400. l = me.createListener(fn, scope, options);
  401. if(me.firing){ // if we are currently firing this event, don't disturb the listener loop
  402. me.listeners = me.listeners.slice(0);
  403. }
  404. me.listeners.push(l);
  405. }
  406. },
  407. createListener: function(fn, scope, o){
  408. o = o || {};
  409. scope = scope || this.obj;
  410. var l = {
  411. fn: fn,
  412. scope: scope,
  413. options: o
  414. }, h = fn;
  415. if(o.target){
  416. h = createTargeted(h, o, scope);
  417. }
  418. if(o.delay){
  419. h = createDelayed(h, o, l, scope);
  420. }
  421. if(o.single){
  422. h = createSingle(h, this, fn, scope);
  423. }
  424. if(o.buffer){
  425. h = createBuffered(h, o, l, scope);
  426. }
  427. l.fireFn = h;
  428. return l;
  429. },
  430. findListener : function(fn, scope){
  431. var list = this.listeners,
  432. i = list.length,
  433. l;
  434. scope = scope || this.obj;
  435. while(i--){
  436. l = list[i];
  437. if(l){
  438. if(l.fn == fn &amp;&amp; l.scope == scope){
  439. return i;
  440. }
  441. }
  442. }
  443. return -1;
  444. },
  445. isListening : function(fn, scope){
  446. return this.findListener(fn, scope) != -1;
  447. },
  448. removeListener : function(fn, scope){
  449. var index,
  450. l,
  451. k,
  452. me = this,
  453. ret = FALSE;
  454. if((index = me.findListener(fn, scope)) != -1){
  455. if (me.firing) {
  456. me.listeners = me.listeners.slice(0);
  457. }
  458. l = me.listeners[index];
  459. if(l.task) {
  460. l.task.cancel();
  461. delete l.task;
  462. }
  463. k = l.tasks &amp;&amp; l.tasks.length;
  464. if(k) {
  465. while(k--) {
  466. l.tasks[k].cancel();
  467. }
  468. delete l.tasks;
  469. }
  470. me.listeners.splice(index, 1);
  471. ret = TRUE;
  472. }
  473. return ret;
  474. },
  475. // Iterate to stop any buffered/delayed events
  476. clearListeners : function(){
  477. var me = this,
  478. l = me.listeners,
  479. i = l.length;
  480. while(i--) {
  481. me.removeListener(l[i].fn, l[i].scope);
  482. }
  483. },
  484. fire : function(){
  485. var me = this,
  486. listeners = me.listeners,
  487. len = listeners.length,
  488. i = 0,
  489. l;
  490. if(len &gt; 0){
  491. me.firing = TRUE;
  492. var args = Array.prototype.slice.call(arguments, 0);
  493. for (; i &lt; len; i++) {
  494. l = listeners[i];
  495. if(l &amp;&amp; l.fireFn.apply(l.scope || me.obj || window, args) === FALSE) {
  496. return (me.firing = FALSE);
  497. }
  498. }
  499. }
  500. me.firing = FALSE;
  501. return TRUE;
  502. }
  503. };
  504. })();
  505. </pre>
  506. </body>
  507. </html>