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">
11 XObject = imports.XObject.XObject;
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 = XObject.define(
29 * @param {Object} config properties to use (incuding events / listeners)
33 Observable = XObject.define(
37 this.addEvents(cfg.events || {});
39 delete cfg.events; // make sure
42 XObject.extend(this, cfg);
45 this.on(this.listeners);
46 delete this.listeners;
52 * @cfg {Object} listeners list of events and functions to call for this object,
56 'click' : function(e) {
66 * Fires the specified event with the passed parameters (minus the event name).
67 * @param {String} eventName
68 * @param {Object...} args Variable number of parameters are passed to handlers
69 * @return {Boolean} returns false if any of the handlers return false otherwise it returns true
71 fireEvent : function(){
72 var ce = this.events[arguments[0].toLowerCase()];
73 if(typeof ce == "object"){
74 return ce.fire.apply(ce, Array.prototype.slice.call(arguments, 1));
80 // private - blcoks these types of signals?
81 filterOptRe : /^(?:scope|delay|buffer|single)$/,
84 * Appends an event handler to this component
85 * @param {String} eventName The type of event to listen for
86 * @param {Function} handler The method the event invokes
87 * @param {Object} scope (optional) The scope in which to execute the handler
88 * function. The handler function's "this" context.
89 * @param {Object} options (optional) An object containing handler configuration
90 * properties. This may contain any of the following properties:<ul>
91 * <li>scope {Object} The scope in which to execute the handler function. The handler function's "this" context.</li>
92 * <li>delay {Number} The number of milliseconds to delay the invocation of the handler after te event fires.</li>
93 * <li>single {Boolean} True to add a handler to handle just the next firing of the event, and then remove itself.</li>
94 * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Roo.DelayedTask} delayed
95 * by the specified number of milliseconds. If the event fires again within that time, the original
96 * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
99 * <b>Combining Options</b><br>
100 * Using the options argument, it is possible to combine different types of listeners:<br>
102 * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)
104 el.on('click', this.onClick, this, {
111 * <b>Attaching multiple handlers in 1 call</b><br>
112 * The method also allows for a single argument to be passed which is a config object containing properties
113 * which specify multiple handlers.
122 fn: this.onMouseOver,
132 * Or a shorthand syntax which passes the same scope object to all handlers:
135 'click': this.onClick,
136 'mouseover': this.onMouseOver,
137 'mouseout': this.onMouseOut,
142 on : function(eventName, fn, scope, o){
143 if(typeof eventName == "object"){
146 if(this.filterOptRe.test(e)){
149 if(typeof o[e] == "function"){
151 this.on(e, o[e], o.scope, o);
153 // individual options
154 this.on(e, o[e].fn, o[e].scope, o[e]);
159 o = (!o || typeof o == "boolean") ? {} : o;
160 eventName = eventName.toLowerCase();
161 var ce = this.events[eventName] || true;
162 if(typeof ce == "boolean"){
163 ce = new Event(this, eventName);
164 this.events[eventName] = ce;
166 ce.addListener(fn, scope, o);
171 * @param {String} eventName The type of event to listen for
172 * @param {Function} handler The handler to remove
173 * @param {Object} scope (optional) The scope (this object) for the handler
175 un : function(eventName, fn, scope){
176 var ce = this.events[eventName.toLowerCase()];
177 if(typeof ce == "object"){
178 ce.removeListener(fn, scope);
183 * Removes all listeners for this object
185 purgeListeners : function(){
186 for(var evt in this.events){
187 if(typeof this.events[evt] == "object"){
188 this.events[evt].clearListeners();
193 relayEvents : function(o, events){
194 var createHandler = function(ename){
196 return this.fireEvent.apply(this, Roo.combine(ename, Array.prototype.slice.call(arguments, 0)));
199 for(var i = 0, len = events.length; i < len; i++){
200 var ename = events[i];
201 if(!this.events[ename]){ this.events[ename] = true; };
202 o.on(ename, createHandler(ename), this);
207 * Used to define events on this Observable
208 * @param {Object} object The object with the events defined
210 addEvents : function(o){
214 XObject.extendIf(this.events, o);
218 * Checks to see if this object has any listeners for a specified event
219 * @param {String} eventName The name of the event to check for
220 * @return {Boolean} True if the event is being listened for, else false
222 hasListener : function(eventName){
223 var e = this.events[eventName];
224 return typeof e == "object" && e.listeners.length > 0;
229 * Starts capture on the specified Observable. All events will be passed
230 * to the supplied function with the event name + standard signature of the event
231 * <b>before</b> the event is fired. If the supplied function returns false,
232 * the event will not fire.
233 * @param {Observable} o The Observable to capture
234 * @param {Function} fn The function to call
235 * @param {Object} scope (optional) The scope (this object) for the fn
238 Observable.capture = function(o, fn, scope){
239 o.fireEvent = o.fireEvent.createInterceptor(fn, scope);
243 * Removes <b>all</b> added captures from the Observable.
244 * @param {Observable} o The Observable to release
247 Observable.releaseCapture = function(o){
248 o.fireEvent = Observable.prototype.fireEvent;
253 var createSingle = function(h, e, fn, scope){
255 e.removeListener(fn, scope);
256 return h.apply(scope, arguments);
260 // NOT SUPPORTED YET>
261 //var createBuffered = function(h, o, scope){
262 // var task = new Roo.DelayedTask();
263 // return function(){
264 // task.delay(o.buffer, h, scope, Array.prototype.slice.call(arguments, 0));
269 //var createDelayed = function(h, o, scope){
270 // return function(){
271 // var args = Array.prototype.slice.call(arguments, 0);
272 // setTimeout(function(){
273 // h.apply(scope, args);
274 // }, o.delay || 10);
283 Event = XObject.define({
291 addListener : function(fn, scope, options){
292 var o = options || {};
293 scope = scope || this.obj;
294 if(!this.isListening(fn, scope)){
295 var l = {fn: fn, scope: scope, options: o};
298 // h = createDelayed(h, o, scope);
301 h = createSingle(h, this, fn, scope);
304 // h = createBuffered(h, o, scope);
307 if(!this.firing){ // if we are currently firing this event, don't disturb the listener loop
308 this.listeners.push(l);
310 this.listeners = this.listeners.slice(0);
311 this.listeners.push(l);
316 findListener : function(fn, scope){
317 scope = scope || this.obj;
318 var ls = this.listeners;
319 for(var i = 0, len = ls.length; i < len; i++){
321 if(l.fn == fn && l.scope == scope){
328 isListening : function(fn, scope){
329 return this.findListener(fn, scope) != -1;
332 removeListener : function(fn, scope){
334 if((index = this.findListener(fn, scope)) != -1){
336 this.listeners.splice(index, 1);
338 this.listeners = this.listeners.slice(0);
339 this.listeners.splice(index, 1);
346 clearListeners : function(){
351 var ls = this.listeners, scope, len = ls.length;
354 var args = Array.prototype.slice.call(arguments, 0);
355 for(var i = 0; i < len; i++){
357 if(l.fireFn.apply(l.scope||this.obj||window, arguments) === false){