Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / docs / source / Event2.html
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="../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; }
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">Ext.require('Ext.util.DelayedTask', function() {
19
20     Ext.util.Event = Ext.extend(Object, (function() {
21         function createBuffered(handler, listener, o, scope) {
22             listener.task = new Ext.util.DelayedTask();
23             return function() {
24                 listener.task.delay(o.buffer, handler, scope, Ext.Array.toArray(arguments));
25             };
26         }
27
28         function createDelayed(handler, listener, o, scope) {
29             return function() {
30                 var task = new Ext.util.DelayedTask();
31                 if (!listener.tasks) {
32                     listener.tasks = [];
33                 }
34                 listener.tasks.push(task);
35                 task.delay(o.delay || 10, handler, scope, Ext.Array.toArray(arguments));
36             };
37         }
38
39         function createSingle(handler, listener, o, scope) {
40             return function() {
41                 listener.ev.removeListener(listener.fn, scope);
42                 return handler.apply(scope, arguments);
43             };
44         }
45
46         return {
47             isEvent: true,
48
49             constructor: function(observable, name) {
50                 this.name = name;
51                 this.observable = observable;
52                 this.listeners = [];
53             },
54
55             addListener: function(fn, scope, options) {
56                 var me = this,
57                     listener;
58                     scope = scope || me.observable;
59
60                 //&lt;debug error&gt;
61                 if (!fn) {
62                     Ext.Error.raise({
63                         sourceClass: Ext.getClassName(this.observable),
64                         sourceMethod: &quot;addListener&quot;,
65                         msg: &quot;The specified callback function is undefined&quot;
66                     });
67                 }
68                 //&lt;/debug&gt;
69
70                 if (!me.isListening(fn, scope)) {
71                     listener = me.createListener(fn, scope, options);
72                     if (me.firing) {
73                         // if we are currently firing this event, don't disturb the listener loop
74                         me.listeners = me.listeners.slice(0);
75                     }
76                     me.listeners.push(listener);
77                 }
78             },
79
80             createListener: function(fn, scope, o) {
81                 o = o || {};
82                 scope = scope || this.observable;
83
84                 var listener = {
85                         fn: fn,
86                         scope: scope,
87                         o: o,
88                         ev: this
89                     },
90                     handler = fn;
91
92                 // The order is important. The 'single' wrapper must be wrapped by the 'buffer' and 'delayed' wrapper
93                 // because the event removal that the single listener does destroys the listener's DelayedTask(s)
94                 if (o.single) {
95                     handler = createSingle(handler, listener, o, scope);
96                 }
97                 if (o.delay) {
98                     handler = createDelayed(handler, listener, o, scope);
99                 }
100                 if (o.buffer) {
101                     handler = createBuffered(handler, listener, o, scope);
102                 }
103
104                 listener.fireFn = handler;
105                 return listener;
106             },
107
108             findListener: function(fn, scope) {
109                 var listeners = this.listeners,
110                 i = listeners.length,
111                 listener,
112                 s;
113
114                 while (i--) {
115                     listener = listeners[i];
116                     if (listener) {
117                         s = listener.scope;
118                         if (listener.fn == fn &amp;&amp; (s == scope || s == this.observable)) {
119                             return i;
120                         }
121                     }
122                 }
123
124                 return - 1;
125             },
126
127             isListening: function(fn, scope) {
128                 return this.findListener(fn, scope) !== -1;
129             },
130
131             removeListener: function(fn, scope) {
132                 var me = this,
133                     index,
134                     listener,
135                     k;
136                 index = me.findListener(fn, scope);
137                 if (index != -1) {
138                     listener = me.listeners[index];
139
140                     if (me.firing) {
141                         me.listeners = me.listeners.slice(0);
142                     }
143
144                     // cancel and remove a buffered handler that hasn't fired yet
145                     if (listener.task) {
146                         listener.task.cancel();
147                         delete listener.task;
148                     }
149
150                     // cancel and remove all delayed handlers that haven't fired yet
151                     k = listener.tasks &amp;&amp; listener.tasks.length;
152                     if (k) {
153                         while (k--) {
154                             listener.tasks[k].cancel();
155                         }
156                         delete listener.tasks;
157                     }
158
159                     // remove this listener from the listeners array
160                     me.listeners.splice(index, 1);
161                     return true;
162                 }
163
164                 return false;
165             },
166
167             // Iterate to stop any buffered/delayed events
168             clearListeners: function() {
169                 var listeners = this.listeners,
170                     i = listeners.length;
171
172                 while (i--) {
173                     this.removeListener(listeners[i].fn, listeners[i].scope);
174                 }
175             },
176
177             fire: function() {
178                 var me = this,
179                     listeners = me.listeners,
180                     count = listeners.length,
181                     i,
182                     args,
183                     listener;
184
185                 if (count &gt; 0) {
186                     me.firing = true;
187                     for (i = 0; i &lt; count; i++) {
188                         listener = listeners[i];
189                         args = arguments.length ? Array.prototype.slice.call(arguments, 0) : [];
190                         if (listener.o) {
191                             args.push(listener.o);
192                         }
193                         if (listener &amp;&amp; listener.fireFn.apply(listener.scope || me.observable, args) === false) {
194                             return (me.firing = false);
195                         }
196                     }
197                 }
198                 me.firing = false;
199                 return true;
200             }
201         };
202     })());
203 });
204 </pre>
205 </body>
206 </html>