3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js">(function(){
11 var EXTUTIL = Ext.util,
12 TOARRAY = Ext.toArray,
14 ISOBJECT = Ext.isObject,
18 * @class Ext.util.Observable
19 * Base class that provides a common interface for publishing events. Subclasses are expected to
20 * to have a property "events" with all the events defined, and, optionally, a property "listeners"
21 * with configured listeners defined.<br>
24 Employee = Ext.extend(Ext.util.Observable, {
25 constructor: function(config){
26 this.name = config.name;
32 // Copy configured listeners into *this* object so that the base class's
33 // constructor will add them.
34 this.listeners = config.listeners;
36 // Call our superclass constructor to complete construction process.
37 Employee.superclass.constructor.call(config)
41 * This could then be used like this:<pre><code>
42 var newEmployee = new Employee({
46 // By default, "this" will be the object that fired the event.
47 alert(this.name + " has quit!");
53 EXTUTIL.Observable = function(){
54 <div id="cfg-Ext.util.Observable-listeners"></div>/**
55 * @cfg {Object} listeners (optional) <p>A config object containing one or more event handlers to be added to this
56 * object during initialization. This should be a valid listeners config object as specified in the
57 * {@link #addListener} example for attaching multiple handlers at once.</p>
58 * <br><p><b><u>DOM events from ExtJs {@link Ext.Component Components}</u></b></p>
59 * <br><p>While <i>some</i> ExtJs Component classes export selected DOM events (e.g. "click", "mouseover" etc), this
60 * is usually only done when extra value can be added. For example the {@link Ext.DataView DataView}'s
61 * <b><code>{@link Ext.DataView#click click}</code></b> event passing the node clicked on. To access DOM
62 * events directly from a Component's HTMLElement, listeners must be added to the <i>{@link Ext.Component#getEl Element}</i> after the Component
63 * has been rendered. A plugin can simplify this step:<pre><code>
64 // Plugin is configured with a listeners config object.
65 // The Component is appended to the argument list of all handler functions.
66 Ext.DomObserver = Ext.extend(Object, {
67 constructor: function(config) {
68 this.listeners = config.listeners ? config.listeners : config;
71 // Component passes itself into plugin's init method
73 var p, l = this.listeners;
75 if (Ext.isFunction(l[p])) {
76 l[p] = this.createHandler(l[p], c);
78 l[p].fn = this.createHandler(l[p].fn, c);
82 // Add the listeners to the Element immediately following the render call
83 c.render = c.render.{@link Function#createSequence createSequence}(function() {
91 createHandler: function(fn, c) {
98 var combo = new Ext.form.ComboBox({
100 // Collapse combo when its element is clicked on
101 plugins: [ new Ext.DomObserver({
102 click: function(evt, comp) {
113 var me = this, e = me.events;
121 EXTUTIL.Observable.prototype = {
123 filterOptRe : /^(?:scope|delay|buffer|single)$/,
125 <div id="method-Ext.util.Observable-fireEvent"></div>/**
126 * <p>Fires the specified event with the passed parameters (minus the event name).</p>
127 * <p>An event may be set to bubble up an Observable parent hierarchy (See {@link Ext.Component#getBubbleTarget})
128 * by calling {@link #enableBubble}.</p>
129 * @param {String} eventName The name of the event to fire.
130 * @param {Object...} args Variable number of parameters are passed to handlers.
131 * @return {Boolean} returns false if any of the handlers return false otherwise it returns true.
133 fireEvent : function(){
134 var a = TOARRAY(arguments),
135 ename = a[0].toLowerCase(),
138 ce = me.events[ename],
141 if (me.eventsSuspended === TRUE) {
142 if (q = me.eventQueue) {
146 else if(ISOBJECT(ce) && ce.bubble){
147 if(ce.fire.apply(ce, a.slice(1)) === FALSE) {
150 c = me.getBubbleTarget && me.getBubbleTarget();
151 if(c && c.enableBubble) {
152 if(!c.events[ename] || !Ext.isObject(c.events[ename]) || !c.events[ename].bubble) {
153 c.enableBubble(ename);
155 return c.fireEvent.apply(c, a);
161 ret = ce.fire.apply(ce, a);
167 <div id="method-Ext.util.Observable-addListener"></div>/**
168 * Appends an event handler to this object.
169 * @param {String} eventName The name of the event to listen for.
170 * @param {Function} handler The method the event invokes.
171 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the handler function is executed.
172 * <b>If omitted, defaults to the object which fired the event.</b>
173 * @param {Object} options (optional) An object containing handler configuration.
174 * properties. This may contain any of the following properties:<ul>
175 * <li><b>scope</b> : Object<div class="sub-desc">The scope (<code><b>this</b></code> reference) in which the handler function is executed.
176 * <b>If omitted, defaults to the object which fired the event.</b></div></li>
177 * <li><b>delay</b> : Number<div class="sub-desc">The number of milliseconds to delay the invocation of the handler after the event fires.</div></li>
178 * <li><b>single</b> : Boolean<div class="sub-desc">True to add a handler to handle just the next firing of the event, and then remove itself.</div></li>
179 * <li><b>buffer</b> : Number<div class="sub-desc">Causes the handler to be scheduled to run in an {@link Ext.util.DelayedTask} delayed
180 * by the specified number of milliseconds. If the event fires again within that time, the original
181 * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</div></li>
182 * <li><b>target</b> : Observable<div class="sub-desc">Only call the handler if the event was fired on the target Observable, <i>not</i>
183 * if the event was bubbled up from a child Observable.</div></li>
186 * <b>Combining Options</b><br>
187 * Using the options argument, it is possible to combine different types of listeners:<br>
189 * A delayed, one-time listener.
191 myDataView.on('click', this.onClick, this, {
196 * <b>Attaching multiple handlers in 1 call</b><br>
197 * The method also allows for a single argument to be passed which is a config object containing properties
198 * which specify multiple handlers.
208 fn: this.onMouseOver,
217 * Or a shorthand syntax:<br>
220 'click' : this.onClick,
221 'mouseover' : this.onMouseOver,
222 'mouseout' : this.onMouseOut,
226 addListener : function(eventName, fn, scope, o){
232 if (ISOBJECT(eventName)) {
236 if (!me.filterOptRe.test(e)) {
237 me.addListener(e, oe.fn || oe, oe.scope || o.scope, oe.fn ? oe : o);
241 eventName = eventName.toLowerCase();
242 ce = me.events[eventName] || TRUE;
243 if (Ext.isBoolean(ce)) {
244 me.events[eventName] = ce = new EXTUTIL.Event(me, eventName);
246 ce.addListener(fn, scope, ISOBJECT(o) ? o : {});
250 <div id="method-Ext.util.Observable-removeListener"></div>/**
251 * Removes an event handler.
252 * @param {String} eventName The type of event the handler was associated with.
253 * @param {Function} handler The handler to remove. <b>This must be a reference to the function passed into the {@link #addListener} call.</b>
254 * @param {Object} scope (optional) The scope originally specified for the handler.
256 removeListener : function(eventName, fn, scope){
257 var ce = this.events[eventName.toLowerCase()];
259 ce.removeListener(fn, scope);
263 <div id="method-Ext.util.Observable-purgeListeners"></div>/**
264 * Removes all listeners for this object
266 purgeListeners : function(){
267 var events = this.events,
273 evt.clearListeners();
278 <div id="method-Ext.util.Observable-addEvents"></div>/**
279 * Adds the specified events to the list of events which this Observable may fire.
280 * @param {Object|String} o Either an object with event names as properties with a value of <code>true</code>
281 * or the first event name string if multiple event names are being passed as separate parameters.
282 * @param {string} Optional. Event name if multiple event names are being passed as separate parameters.
284 this.addEvents('storeloaded', 'storecleared');
287 addEvents : function(o){
289 me.events = me.events || {};
290 if (Ext.isString(o)) {
294 me.events[a[i]] = me.events[a[i]] || TRUE;
297 Ext.applyIf(me.events, o);
301 <div id="method-Ext.util.Observable-hasListener"></div>/**
302 * Checks to see if this object has any listeners for a specified event
303 * @param {String} eventName The name of the event to check for
304 * @return {Boolean} True if the event is being listened for, else false
306 hasListener : function(eventName){
307 var e = this.events[eventName];
308 return ISOBJECT(e) && e.listeners.length > 0;
311 <div id="method-Ext.util.Observable-suspendEvents"></div>/**
312 * Suspend the firing of all events. (see {@link #resumeEvents})
313 * @param {Boolean} queueSuspended Pass as true to queue up suspended events to be fired
314 * after the {@link #resumeEvents} call instead of discarding all suspended events;
316 suspendEvents : function(queueSuspended){
317 this.eventsSuspended = TRUE;
318 if(queueSuspended && !this.eventQueue){
319 this.eventQueue = [];
323 <div id="method-Ext.util.Observable-resumeEvents"></div>/**
324 * Resume firing events. (see {@link #suspendEvents})
325 * If events were suspended using the <tt><b>queueSuspended</b></tt> parameter, then all
326 * events fired during event suspension will be sent to any listeners now.
328 resumeEvents : function(){
330 queued = me.eventQueue || [];
331 me.eventsSuspended = FALSE;
332 delete me.eventQueue;
333 EACH(queued, function(e) {
334 me.fireEvent.apply(me, e);
339 var OBSERVABLE = EXTUTIL.Observable.prototype;
340 <div id="method-Ext.util.Observable-on"></div>/**
341 * Appends an event handler to this object (shorthand for {@link #addListener}.)
342 * @param {String} eventName The type of event to listen for
343 * @param {Function} handler The method the event invokes
344 * @param {Object} scope (optional) The scope (<code><b>this</b></code> reference) in which the handler function is executed.
345 * <b>If omitted, defaults to the object which fired the event.</b>
346 * @param {Object} options (optional) An object containing handler configuration.
349 OBSERVABLE.on = OBSERVABLE.addListener;
350 <div id="method-Ext.util.Observable-un"></div>/**
351 * Removes an event handler (shorthand for {@link #removeListener}.)
352 * @param {String} eventName The type of event the handler was associated with.
353 * @param {Function} handler The handler to remove. <b>This must be a reference to the function passed into the {@link #addListener} call.</b>
354 * @param {Object} scope (optional) The scope originally specified for the handler.
357 OBSERVABLE.un = OBSERVABLE.removeListener;
359 <div id="method-Ext.util.Observable-Observable.releaseCapture"></div>/**
360 * Removes <b>all</b> added captures from the Observable.
361 * @param {Observable} o The Observable to release
364 EXTUTIL.Observable.releaseCapture = function(o){
365 o.fireEvent = OBSERVABLE.fireEvent;
368 function createTargeted(h, o, scope){
370 if(o.target == arguments[0]){
371 h.apply(scope, TOARRAY(arguments));
376 function createBuffered(h, o, l, scope){
377 l.task = new EXTUTIL.DelayedTask();
379 l.task.delay(o.buffer, h, scope, TOARRAY(arguments));
383 function createSingle(h, e, fn, scope){
385 e.removeListener(fn, scope);
386 return h.apply(scope, arguments);
390 function createDelayed(h, o, l, scope){
392 var task = new EXTUTIL.DelayedTask();
397 task.delay(o.delay || 10, h, scope, TOARRAY(arguments));
401 EXTUTIL.Event = function(obj, name){
407 EXTUTIL.Event.prototype = {
408 addListener : function(fn, scope, options){
411 scope = scope || me.obj;
412 if(!me.isListening(fn, scope)){
413 l = me.createListener(fn, scope, options);
414 if(me.firing){ // if we are currently firing this event, don't disturb the listener loop
415 me.listeners = me.listeners.slice(0);
417 me.listeners.push(l);
421 createListener: function(fn, scope, o){
422 o = o || {}, scope = scope || this.obj;
429 h = createTargeted(h, o, scope);
432 h = createDelayed(h, o, l, scope);
435 h = createSingle(h, this, fn, scope);
438 h = createBuffered(h, o, l, scope);
444 findListener : function(fn, scope){
445 var list = this.listeners,
453 if(l.fn == fn && (s == scope || s == this.obj)){
461 isListening : function(fn, scope){
462 return this.findListener(fn, scope) != -1;
465 removeListener : function(fn, scope){
471 if((index = me.findListener(fn, scope)) != -1){
473 me.listeners = me.listeners.slice(0);
475 l = me.listeners[index];
480 k = l.tasks && l.tasks.length;
487 me.listeners.splice(index, 1);
493 // Iterate to stop any buffered/delayed events
494 clearListeners : function(){
499 me.removeListener(l[i].fn, l[i].scope);
505 args = TOARRAY(arguments),
506 listeners = me.listeners,
507 len = listeners.length,
513 for (; i < len; i++) {
515 if(l && l.fireFn.apply(l.scope || me.obj || window, args) === FALSE) {
516 return (me.firing = FALSE);