Upgrade to ExtJS 4.0.1 - Released 05/18/2011
[extjs.git] / src / app / EventBus.js
1 /**
2  * @class Ext.app.EventBus
3  * @private
4  *
5  * Class documentation for the MVC classes will be present before 4.0 final, in the mean time please refer to the MVC
6  * guide
7  */
8 Ext.define('Ext.app.EventBus', {
9     requires: [
10         'Ext.util.Event'
11     ],
12     mixins: {
13         observable: 'Ext.util.Observable'
14     },
15     
16     constructor: function() {
17         this.mixins.observable.constructor.call(this);
18         
19         this.bus = {};
20         
21         var me = this;
22         Ext.override(Ext.Component, {
23             fireEvent: function(ev) {
24                 if (Ext.util.Observable.prototype.fireEvent.apply(this, arguments) !== false) {
25                     return me.dispatch.call(me, ev, this, arguments);
26                 }
27                 return false;
28             }
29         });
30     },
31
32     dispatch: function(ev, target, args) {
33         var bus = this.bus,
34             selectors = bus[ev],
35             selector, controllers, id, events, event, i, ln;
36         
37         if (selectors) {
38             // Loop over all the selectors that are bound to this event
39             for (selector in selectors) {
40                 // Check if the target matches the selector
41                 if (target.is(selector)) {
42                     // Loop over all the controllers that are bound to this selector   
43                     controllers = selectors[selector];
44                     for (id in controllers) {
45                         // Loop over all the events that are bound to this selector on this controller
46                         events = controllers[id];
47                         for (i = 0, ln = events.length; i < ln; i++) {
48                             event = events[i];
49                             // Fire the event!
50                             return event.fire.apply(event, Array.prototype.slice.call(args, 1));
51                         }
52                     }
53                 }
54             }
55         }
56     },
57     
58     control: function(selectors, listeners, controller) {
59         var bus = this.bus,
60             selector, fn;
61         
62         if (Ext.isString(selectors)) {
63             selector = selectors;
64             selectors = {};
65             selectors[selector] = listeners;
66             this.control(selectors, null, controller);
67             return;
68         }
69     
70         Ext.Object.each(selectors, function(selector, listeners) {
71             Ext.Object.each(listeners, function(ev, listener) {
72                 var options = {},   
73                     scope = controller,
74                     event = Ext.create('Ext.util.Event', controller, ev);
75                 
76                 // Normalize the listener                
77                 if (Ext.isObject(listener)) {
78                     options = listener;
79                     listener = options.fn;
80                     scope = options.scope || controller;
81                     delete options.fn;
82                     delete options.scope;
83                 }
84                 
85                 event.addListener(listener, scope, options);
86
87                 // Create the bus tree if it is not there yet
88                 bus[ev] = bus[ev] || {};
89                 bus[ev][selector] = bus[ev][selector] || {};
90                 bus[ev][selector][controller.id] = bus[ev][selector][controller.id] || [];            
91                 
92                 // Push our listener in our bus
93                 bus[ev][selector][controller.id].push(event);
94             });
95         });
96     }
97 });