/*
Copyright (c) 2006, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 0.12.2
*/
/**
* The CustomEvent class lets you define events for your application
* that can be subscribed to by one or more independent component.
*
* @param {String} type The type of event, which is passed to the callback
* when the event fires
* @param {Object} oScope The context the event will fire from. "this" will
* refer to this object in the callback. Default value:
* the window object. The listener can override this.
* @param {boolean} silent pass true to prevent the event from writing to
* the debugsystem
* @param {int} signature the signature that the custom event subscriber
* will receive. YAHOO.util.CustomEvent.LIST or
* YAHOO.util.CustomEvent.FLAT. The default is
* YAHOO.util.CustomEvent.LIST.
* @namespace YAHOO.util
* @class CustomEvent
* @constructor
*/
YAHOO.util.CustomEvent = function(type, oScope, silent, signature) {
/**
* The type of event, returned to subscribers when the event fires
* @property type
* @type string
*/
this.type = type;
/**
* The scope the the event will fire from by default. Defaults to the window
* obj
* @property scope
* @type object
*/
this.scope = oScope || window;
/**
* By default all custom events are logged in the debug build, set silent
* to true to disable debug outpu for this event.
* @property silent
* @type boolean
*/
this.silent = silent;
/**
* Custom events support two styles of arguments provided to the event
* subscribers.
*
* - YAHOO.util.CustomEvent.LIST:
*
* - param1: event name
* - param2: array of arguments sent to fire
* - param3: a custom object supplied by the subscriber
*
*
* - YAHOO.util.CustomEvent.FLAT
*
* - param1: the first argument passed to fire. If you need to
* pass multiple parameters, use and array or object literal
* - param2: a custom object supplied by the subscriber
*
*
*
* @property signature
* @type int
*/
this.signature = signature || YAHOO.util.CustomEvent.LIST;
/**
* The subscribers to this event
* @property subscribers
* @type Subscriber[]
*/
this.subscribers = [];
if (!this.silent) {
}
var onsubscribeType = "_YUICEOnSubscribe";
// Only add subscribe events for events that are not generated by
// CustomEvent
if (type !== onsubscribeType) {
/**
* Custom events provide a custom event that fires whenever there is
* a new subscriber to the event. This provides an opportunity to
* handle the case where there is a non-repeating event that has
* already fired has a new subscriber.
*
* @event subscribeEvent
* @type YAHOO.util.CustomEvent
* @param {Function} fn The function to execute
* @param {Object} obj An object to be passed along when the event
* fires
* @param {boolean|Object} override If true, the obj passed in becomes
* the execution scope of the listener.
* if an object, that object becomes the
* the execution scope.
*/
this.subscribeEvent =
new YAHOO.util.CustomEvent(onsubscribeType, this, true);
}
};
/**
* Subscriber listener sigature constant. The LIST type returns three
* parameters: the event type, the array of args passed to fire, and
* the optional custom object
* @property YAHOO.util.CustomEvent.LIST
* @static
* @type int
*/
YAHOO.util.CustomEvent.LIST = 0;
/**
* Subscriber listener sigature constant. The FLAT type returns two
* parameters: the first argument passed to fire and the optional
* custom object
* @property YAHOO.util.CustomEvent.FLAT
* @static
* @type int
*/
YAHOO.util.CustomEvent.FLAT = 1;
YAHOO.util.CustomEvent.prototype = {
/**
* Subscribes the caller to this event
* @method subscribe
* @param {Function} fn The function to execute
* @param {Object} obj An object to be passed along when the event
* fires
* @param {boolean|Object} override If true, the obj passed in becomes
* the execution scope of the listener.
* if an object, that object becomes the
* the execution scope.
*/
subscribe: function(fn, obj, override) {
if (this.subscribeEvent) {
this.subscribeEvent.fire(fn, obj, override);
}
this.subscribers.push( new YAHOO.util.Subscriber(fn, obj, override) );
},
/**
* Unsubscribes the caller from this event
* @method unsubscribe
* @param {Function} fn The function to execute
* @param {Object} obj The custom object passed to subscribe (optional)
* @return {boolean} True if the subscriber was found and detached.
*/
unsubscribe: function(fn, obj) {
var found = false;
for (var i=0, len=this.subscribers.length; i
* The type of event
* All of the arguments fire() was executed with as an array
* The custom object (if any) that was passed into the subscribe()
* method
*
* @method fire
* @param {Object*} arguments an arbitrary set of parameters to pass to
* the handler.
* @return {boolean} false if one of the subscribers returned false,
* true otherwise
*/
fire: function() {
var len=this.subscribers.length;
if (!len && this.silent) {
return true;
}
var args=[], ret=true, i;
for (i=0; i 0) {
param = args[0];
}
ret = s.fn.call(scope, param, s.obj);
} else {
ret = s.fn.call(scope, this.type, args, s.obj);
}
if (false === ret) {
if (!this.silent) {
}
//break;
return false;
}
}
}
return true;
},
/**
* Removes all listeners
* @method unsubscribeAll
*/
unsubscribeAll: function() {
for (var i=0, len=this.subscribers.length; i= 0) {
cacheItem = listeners[index];
}
if (!el || !cacheItem) {
return false;
}
if (this.useLegacyEvent(el, sType)) {
var legacyIndex = this.getLegacyIndex(el, sType);
var llist = legacyHandlers[legacyIndex];
if (llist) {
for (i=0, len=llist.length; i 0);
}
// onAvailable
var notAvail = [];
for (var i=0,len=onAvailStack.length; i 0) {
for (var i=0,len=listeners.length; i 0) {
j = listeners.length;
while (j) {
index = j-1;
l = listeners[index];
if (l) {
EU.removeListener(l[EU.EL], l[EU.TYPE],
l[EU.FN], index);
}
j = j - 1;
}
l=null;
EU.clearCache();
}
for (i=0,len=legacyEvents.length; i
*
* scope: defines the default execution scope. If not defined
* the default scope will be this instance.
*
*
* silent: if true, the custom event will not generate log messages.
* This is false by default.
*
*
* onSubscribeCallback: specifies a callback to execute when the
* event has a new subscriber. This will fire immediately for
* each queued subscriber if any exist prior to the creation of
* the event.
*
*
*
* @return {CustomEvent} the custom event
*
*/
createEvent: function(p_type, p_config) {
this.__yui_events = this.__yui_events || {};
var opts = p_config || {};
var events = this.__yui_events;
if (events[p_type]) {
} else {
var scope = opts.scope || this;
var silent = opts.silent || null;
var ce = new YAHOO.util.CustomEvent(p_type, scope, silent,
YAHOO.util.CustomEvent.FLAT);
events[p_type] = ce;
if (opts.onSubscribeCallback) {
ce.subscribeEvent.subscribe(opts.onSubscribeCallback);
}
this.__yui_subscribers = this.__yui_subscribers || {};
var qs = this.__yui_subscribers[p_type];
if (qs) {
for (var i=0; i
* The first argument fire() was executed with
* The custom object (if any) that was passed into the subscribe()
* method
*
* @method fireEvent
* @param p_type {string} the type, or name of the event
* @param arguments {Object*} an arbitrary set of parameters to pass to
* the handler.
* @return {boolean} the return value from CustomEvent.fire, or null if
* the custom event does not exist.
*/
fireEvent: function(p_type, arg1, arg2, etc) {
this.__yui_events = this.__yui_events || {};
var ce = this.__yui_events[p_type];
if (ce) {
var args = [];
for (var i=1; i