1 | /*! |
---|
2 | * Ext JS Library 3.0.0 |
---|
3 | * Copyright(c) 2006-2009 Ext JS, LLC |
---|
4 | * licensing@extjs.com |
---|
5 | * http://www.extjs.com/license |
---|
6 | */ |
---|
7 | /** |
---|
8 | * @class Ext.util.Observable |
---|
9 | */ |
---|
10 | Ext.apply(Ext.util.Observable.prototype, function(){ |
---|
11 | // this is considered experimental (along with beforeMethod, afterMethod, removeMethodListener?) |
---|
12 | // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call |
---|
13 | // private |
---|
14 | function getMethodEvent(method){ |
---|
15 | var e = (this.methodEvents = this.methodEvents || |
---|
16 | {})[method], returnValue, v, cancel, obj = this; |
---|
17 | |
---|
18 | if (!e) { |
---|
19 | this.methodEvents[method] = e = {}; |
---|
20 | e.originalFn = this[method]; |
---|
21 | e.methodName = method; |
---|
22 | e.before = []; |
---|
23 | e.after = []; |
---|
24 | |
---|
25 | var makeCall = function(fn, scope, args){ |
---|
26 | if (!Ext.isEmpty(v = fn.apply(scope || obj, args))) { |
---|
27 | if (Ext.isObject(v)) { |
---|
28 | returnValue = !Ext.isEmpty(v.returnValue) ? v.returnValue : v; |
---|
29 | cancel = !!v.cancel; |
---|
30 | } |
---|
31 | else |
---|
32 | if (v === false) { |
---|
33 | cancel = true; |
---|
34 | } |
---|
35 | else { |
---|
36 | returnValue = v; |
---|
37 | } |
---|
38 | } |
---|
39 | }; |
---|
40 | |
---|
41 | this[method] = function(){ |
---|
42 | var args = Ext.toArray(arguments); |
---|
43 | returnValue = v = undefined; |
---|
44 | cancel = false; |
---|
45 | |
---|
46 | Ext.each(e.before, function(b){ |
---|
47 | makeCall(b.fn, b.scope, args); |
---|
48 | if (cancel) { |
---|
49 | return returnValue; |
---|
50 | } |
---|
51 | }); |
---|
52 | |
---|
53 | if (!Ext.isEmpty(v = e.originalFn.apply(obj, args))) { |
---|
54 | returnValue = v; |
---|
55 | } |
---|
56 | Ext.each(e.after, function(a){ |
---|
57 | makeCall(a.fn, a.scope, args); |
---|
58 | if (cancel) { |
---|
59 | return returnValue; |
---|
60 | } |
---|
61 | }); |
---|
62 | return returnValue; |
---|
63 | }; |
---|
64 | } |
---|
65 | return e; |
---|
66 | } |
---|
67 | |
---|
68 | return { |
---|
69 | // these are considered experimental |
---|
70 | // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call |
---|
71 | // adds an "interceptor" called before the original method |
---|
72 | beforeMethod: function(method, fn, scope){ |
---|
73 | getMethodEvent.call(this, method).before.push({ |
---|
74 | fn: fn, |
---|
75 | scope: scope |
---|
76 | }); |
---|
77 | }, |
---|
78 | |
---|
79 | // adds a "sequence" called after the original method |
---|
80 | afterMethod: function(method, fn, scope){ |
---|
81 | getMethodEvent.call(this, method).after.push({ |
---|
82 | fn: fn, |
---|
83 | scope: scope |
---|
84 | }); |
---|
85 | }, |
---|
86 | |
---|
87 | removeMethodListener: function(method, fn, scope){ |
---|
88 | var e = getMethodEvent.call(this, method), found = false; |
---|
89 | Ext.each(e.before, function(b, i, arr){ |
---|
90 | if (b.fn == fn && b.scope == scope) { |
---|
91 | arr.splice(i, 1); |
---|
92 | found = true; |
---|
93 | return false; |
---|
94 | } |
---|
95 | }); |
---|
96 | if (!found) { |
---|
97 | Ext.each(e.after, function(a, i, arr){ |
---|
98 | if (a.fn == fn && a.scope == scope) { |
---|
99 | arr.splice(i, 1); |
---|
100 | return false; |
---|
101 | } |
---|
102 | }); |
---|
103 | } |
---|
104 | }, |
---|
105 | |
---|
106 | /** |
---|
107 | * Relays selected events from the specified Observable as if the events were fired by <tt><b>this</b></tt>. |
---|
108 | * @param {Object} o The Observable whose events this object is to relay. |
---|
109 | * @param {Array} events Array of event names to relay. |
---|
110 | */ |
---|
111 | relayEvents: function(o, events){ |
---|
112 | var me = this; |
---|
113 | function createHandler(ename){ |
---|
114 | return function(){ |
---|
115 | return me.fireEvent.apply(me, [ename].concat(Ext.toArray(arguments))); |
---|
116 | }; |
---|
117 | } |
---|
118 | Ext.each(events, function(ename){ |
---|
119 | me.events[ename] = me.events[ename] || true; |
---|
120 | o.on(ename, createHandler(ename), me); |
---|
121 | }); |
---|
122 | }, |
---|
123 | |
---|
124 | /** |
---|
125 | * Used to enable bubbling of events |
---|
126 | * @param {Object} events |
---|
127 | */ |
---|
128 | enableBubble: function(events){ |
---|
129 | var me = this; |
---|
130 | events = Ext.isArray(events) ? events : Ext.toArray(arguments); |
---|
131 | Ext.each(events, function(ename){ |
---|
132 | ename = ename.toLowerCase(); |
---|
133 | var ce = me.events[ename] || true; |
---|
134 | if (typeof ce == "boolean") { |
---|
135 | ce = new Ext.util.Event(me, ename); |
---|
136 | me.events[ename] = ce; |
---|
137 | } |
---|
138 | ce.bubble = true; |
---|
139 | }); |
---|
140 | } |
---|
141 | }; |
---|
142 | }()); |
---|
143 | |
---|
144 | |
---|
145 | /** |
---|
146 | * Starts capture on the specified Observable. All events will be passed |
---|
147 | * to the supplied function with the event name + standard signature of the event |
---|
148 | * <b>before</b> the event is fired. If the supplied function returns false, |
---|
149 | * the event will not fire. |
---|
150 | * @param {Observable} o The Observable to capture |
---|
151 | * @param {Function} fn The function to call |
---|
152 | * @param {Object} scope (optional) The scope (this object) for the fn |
---|
153 | * @static |
---|
154 | */ |
---|
155 | Ext.util.Observable.capture = function(o, fn, scope){ |
---|
156 | o.fireEvent = o.fireEvent.createInterceptor(fn, scope); |
---|
157 | }; |
---|
158 | |
---|
159 | |
---|
160 | /** |
---|
161 | * Sets observability on the passed class constructor.<p> |
---|
162 | * <p>This makes any event fired on any instance of the passed class also fire a single event through |
---|
163 | * the <i>class</i> allowing for central handling of events on many instances at once.</p> |
---|
164 | * <p>Usage:</p><pre><code> |
---|
165 | Ext.util.Observable.observeClass(Ext.data.Connection); |
---|
166 | Ext.data.Connection.on('beforerequest', function(con, options) { |
---|
167 | console.log("Ajax request made to " + options.url); |
---|
168 | });</code></pre> |
---|
169 | * @param {Function} c The class constructor to make observable. |
---|
170 | * @static |
---|
171 | */ |
---|
172 | Ext.util.Observable.observeClass = function(c){ |
---|
173 | Ext.apply(c, new Ext.util.Observable()); |
---|
174 | c.prototype.fireEvent = function(){ |
---|
175 | return (c.fireEvent.apply(c, arguments) !== false) && |
---|
176 | (Ext.util.Observable.prototype.fireEvent.apply(this, arguments) !== false); |
---|
177 | }; |
---|
178 | }; |
---|