4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
13 * @class Roo.util.Observable
14 * Base class that provides a common interface for publishing events. Subclasses are expected to
15 * to have a property "events" with all the events defined.<br>
18 Employee = function(name){
25 Roo.extend(Employee, Roo.util.Observable);
27 * @param {Object} config properties to use (incuding events / listeners)
30 Roo.util.Observable = function(cfg){
33 this.addEvents(cfg.events || {});
35 delete cfg.events; // make sure
41 this.on(this.listeners);
42 delete this.listeners;
45 Roo.util.Observable.prototype = {
47 * @cfg {Object} listeners list of events and functions to call for this object,
51 'click' : function(e) {
61 * Fires the specified event with the passed parameters (minus the event name).
62 * @param {String} eventName
63 * @param {Object...} args Variable number of parameters are passed to handlers
64 * @return {Boolean} returns false if any of the handlers return false otherwise it returns true
66 fireEvent : function(){
67 var ce = this.events[arguments[0].toLowerCase()];
68 if(typeof ce == "object"){
69 return ce.fire.apply(ce, Array.prototype.slice.call(arguments, 1));
76 filterOptRe : /^(?:scope|delay|buffer|single)$/,
79 * Appends an event handler to this component
80 * @param {String} eventName The type of event to listen for
81 * @param {Function} handler The method the event invokes
82 * @param {Object} scope (optional) The scope in which to execute the handler
83 * function. The handler function's "this" context.
84 * @param {Object} options (optional) An object containing handler configuration
85 * properties. This may contain any of the following properties:<ul>
86 * <li>scope {Object} The scope in which to execute the handler function. The handler function's "this" context.</li>
87 * <li>delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.</li>
88 * <li>single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.</li>
89 * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Roo.util.DelayedTask} delayed
90 * by the specified number of milliseconds. If the event fires again within that time, the original
91 * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
94 * <b>Combining Options</b><br>
95 * Using the options argument, it is possible to combine different types of listeners:<br>
97 * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)
99 el.on('click', this.onClick, this, {
106 * <b>Attaching multiple handlers in 1 call</b><br>
107 * The method also allows for a single argument to be passed which is a config object containing properties
108 * which specify multiple handlers.
117 fn: this.onMouseOver,
127 * Or a shorthand syntax which passes the same scope object to all handlers:
130 'click': this.onClick,
131 'mouseover': this.onMouseOver,
132 'mouseout': this.onMouseOut,
137 addListener : function(eventName, fn, scope, o){
138 if(typeof eventName == "object"){
141 if(this.filterOptRe.test(e)){
144 if(typeof o[e] == "function"){
146 this.addListener(e, o[e], o.scope, o);
148 // individual options
149 this.addListener(e, o[e].fn, o[e].scope, o[e]);
154 o = (!o || typeof o == "boolean") ? {} : o;
155 eventName = eventName.toLowerCase();
156 var ce = this.events[eventName] || true;
157 if(typeof ce == "boolean"){
158 ce = new Roo.util.Event(this, eventName);
159 this.events[eventName] = ce;
161 ce.addListener(fn, scope, o);
166 * @param {String} eventName The type of event to listen for
167 * @param {Function} handler The handler to remove
168 * @param {Object} scope (optional) The scope (this object) for the handler
170 removeListener : function(eventName, fn, scope){
171 var ce = this.events[eventName.toLowerCase()];
172 if(typeof ce == "object"){
173 ce.removeListener(fn, scope);
178 * Removes all listeners for this object
180 purgeListeners : function(){
181 for(var evt in this.events){
182 if(typeof this.events[evt] == "object"){
183 this.events[evt].clearListeners();
188 relayEvents : function(o, events){
189 var createHandler = function(ename){
191 return this.fireEvent.apply(this, Roo.combine(ename, Array.prototype.slice.call(arguments, 0)));
194 for(var i = 0, len = events.length; i < len; i++){
195 var ename = events[i];
196 if(!this.events[ename]){ this.events[ename] = true; };
197 o.on(ename, createHandler(ename), this);
202 * Used to define events on this Observable
203 * @param {Object} object The object with the events defined
205 addEvents : function(o){
209 Roo.applyIf(this.events, o);
213 * Checks to see if this object has any listeners for a specified event
214 * @param {String} eventName The name of the event to check for
215 * @return {Boolean} True if the event is being listened for, else false
217 hasListener : function(eventName){
218 var e = this.events[eventName];
219 return typeof e == "object" && e.listeners.length > 0;
223 * Appends an event handler to this element (shorthand for addListener)
224 * @param {String} eventName The type of event to listen for
225 * @param {Function} handler The method the event invokes
226 * @param {Object} scope (optional) The scope in which to execute the handler
227 * function. The handler function's "this" context.
228 * @param {Object} options (optional)
231 Roo.util.Observable.prototype.on = Roo.util.Observable.prototype.addListener;
233 * Removes a listener (shorthand for removeListener)
234 * @param {String} eventName The type of event to listen for
235 * @param {Function} handler The handler to remove
236 * @param {Object} scope (optional) The scope (this object) for the handler
239 Roo.util.Observable.prototype.un = Roo.util.Observable.prototype.removeListener;
242 * Starts capture on the specified Observable. All events will be passed
243 * to the supplied function with the event name + standard signature of the event
244 * <b>before</b> the event is fired. If the supplied function returns false,
245 * the event will not fire.
246 * @param {Observable} o The Observable to capture
247 * @param {Function} fn The function to call
248 * @param {Object} scope (optional) The scope (this object) for the fn
251 Roo.util.Observable.capture = function(o, fn, scope){
252 o.fireEvent = o.fireEvent.createInterceptor(fn, scope);
256 * Removes <b>all</b> added captures from the Observable.
257 * @param {Observable} o The Observable to release
260 Roo.util.Observable.releaseCapture = function(o){
261 o.fireEvent = Roo.util.Observable.prototype.fireEvent;
266 var createBuffered = function(h, o, scope){
267 var task = new Roo.util.DelayedTask();
269 task.delay(o.buffer, h, scope, Array.prototype.slice.call(arguments, 0));
273 var createSingle = function(h, e, fn, scope){
275 e.removeListener(fn, scope);
276 return h.apply(scope, arguments);
280 var createDelayed = function(h, o, scope){
282 var args = Array.prototype.slice.call(arguments, 0);
283 setTimeout(function(){
284 h.apply(scope, args);
289 Roo.util.Event = function(obj, name){
295 Roo.util.Event.prototype = {
296 addListener : function(fn, scope, options){
297 var o = options || {};
298 scope = scope || this.obj;
299 if(!this.isListening(fn, scope)){
300 var l = {fn: fn, scope: scope, options: o};
303 h = createDelayed(h, o, scope);
306 h = createSingle(h, this, fn, scope);
309 h = createBuffered(h, o, scope);
312 if(!this.firing){ // if we are currently firing this event, don't disturb the listener loop
313 this.listeners.push(l);
315 this.listeners = this.listeners.slice(0);
316 this.listeners.push(l);
321 findListener : function(fn, scope){
322 scope = scope || this.obj;
323 var ls = this.listeners;
324 for(var i = 0, len = ls.length; i < len; i++){
326 if(l.fn == fn && l.scope == scope){
333 isListening : function(fn, scope){
334 return this.findListener(fn, scope) != -1;
337 removeListener : function(fn, scope){
339 if((index = this.findListener(fn, scope)) != -1){
341 this.listeners.splice(index, 1);
343 this.listeners = this.listeners.slice(0);
344 this.listeners.splice(index, 1);
351 clearListeners : function(){
356 var ls = this.listeners, scope, len = ls.length;
359 var args = Array.prototype.slice.call(arguments, 0);
360 for(var i = 0; i < len; i++){
362 if(l.fireFn.apply(l.scope||this.obj||window, arguments) === false){