Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / app / EventBus.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.app.EventBus
17  * @private
18  */
19 Ext.define('Ext.app.EventBus', {
20     requires: [
21         'Ext.util.Event'
22     ],
23     mixins: {
24         observable: 'Ext.util.Observable'
25     },
26
27     constructor: function() {
28         this.mixins.observable.constructor.call(this);
29
30         this.bus = {};
31
32         var me = this;
33         Ext.override(Ext.Component, {
34             fireEvent: function(ev) {
35                 if (Ext.util.Observable.prototype.fireEvent.apply(this, arguments) !== false) {
36                     return me.dispatch.call(me, ev, this, arguments);
37                 }
38                 return false;
39             }
40         });
41     },
42
43     dispatch: function(ev, target, args) {
44         var bus = this.bus,
45             selectors = bus[ev],
46             selector, controllers, id, events, event, i, ln;
47
48         if (selectors) {
49             // Loop over all the selectors that are bound to this event
50             for (selector in selectors) {
51                 // Check if the target matches the selector
52                 if (target.is(selector)) {
53                     // Loop over all the controllers that are bound to this selector
54                     controllers = selectors[selector];
55                     for (id in controllers) {
56                         // Loop over all the events that are bound to this selector on this controller
57                         events = controllers[id];
58                         for (i = 0, ln = events.length; i < ln; i++) {
59                             event = events[i];
60                             // Fire the event!
61                             if (event.fire.apply(event, Array.prototype.slice.call(args, 1)) === false) {
62                                 return false;
63                             };
64                         }
65                     }
66                 }
67             }
68         }
69     },
70
71     control: function(selectors, listeners, controller) {
72         var bus = this.bus,
73             selector, fn;
74
75         if (Ext.isString(selectors)) {
76             selector = selectors;
77             selectors = {};
78             selectors[selector] = listeners;
79             this.control(selectors, null, controller);
80             return;
81         }
82
83         Ext.Object.each(selectors, function(selector, listeners) {
84             Ext.Object.each(listeners, function(ev, listener) {
85                 var options = {},
86                     scope = controller,
87                     event = Ext.create('Ext.util.Event', controller, ev);
88
89                 // Normalize the listener
90                 if (Ext.isObject(listener)) {
91                     options = listener;
92                     listener = options.fn;
93                     scope = options.scope || controller;
94                     delete options.fn;
95                     delete options.scope;
96                 }
97
98                 event.addListener(listener, scope, options);
99
100                 // Create the bus tree if it is not there yet
101                 bus[ev] = bus[ev] || {};
102                 bus[ev][selector] = bus[ev][selector] || {};
103                 bus[ev][selector][controller.id] = bus[ev][selector][controller.id] || [];
104
105                 // Push our listener in our bus
106                 bus[ev][selector][controller.id].push(event);
107             });
108         });
109     }
110 });