event-target.js revision 2a165bf36ff23d834e73c333ea237fabace7a74d
225N/A
225N/AYUI.add("event-target", function(Y) {
225N/A
225N/A var SILENT = { 'yui:log': true };
225N/A
225N/A /**
225N/A * Event.Target is designed to be used with Y.augment to wrap
225N/A * Event.Custom in an interface that allows events to be subscribed to
225N/A * and fired by name. This makes it possible for implementing code to
225N/A * subscribe to an event that either has not been created yet, or will
225N/A * not be created at all.
225N/A *
225N/A * @Class Event.Target
225N/A */
225N/A Y.EventTarget = function(opts) {
225N/A
225N/A // console.log('Event.Target constructor executed: ' + this._yuid);
225N/A
225N/A var o = (Y.Lang.isObject(opts)) ? opts : {};
225N/A
225N/A this._yuievt = {
225N/A
225N/A events: {},
225N/A
225N/A targets: {},
225N/A
225N/A config: o,
225N/A
225N/A defaults: {
618N/A context: this,
225N/A host: this,
225N/A emitFacade: o.emitFacade || false,
225N/A bubbles: ('bubbles' in o) ? o.bubbles : true
225N/A }
225N/A
225N/A };
225N/A
225N/A };
225N/A
225N/A var ET = Y.EventTarget;
225N/A
225N/A ET.prototype = {
225N/A
225N/A /**
225N/A * Subscribe to a custom event hosted by this object
225N/A * @method subscribe
225N/A * @param type {string} The type of the event
225N/A * @param fn {Function} The callback
225N/A * @param context The execution context
225N/A * @param args* 1..n params to supply to the callback
225N/A */
225N/A subscribe: function(type, fn, context) {
225N/A
225N/A var ce = this._yuievt.events[type] || this.publish(type),
225N/A a = Y.Array(arguments, 1, true);
225N/A
225N/A return ce.subscribe.apply(ce, a);
225N/A
225N/A },
225N/A
225N/A /**
225N/A * Unsubscribes one or more listeners the from the specified event
225N/A * @method unsubscribe
225N/A * @param type {string|Object} Either the handle to the subscriber or the
225N/A * type of event. If the type
225N/A * is not specified, it will attempt to remove
225N/A * the listener from all hosted events.
225N/A * @param fn {Function} The subscribed function to unsubscribe, if not
225N/A * supplied, all subscribers will be removed.
225N/A * @param context {Object} The custom object passed to subscribe. This is
225N/A * optional, but if supplied will be used to
225N/A * disambiguate multiple listeners that are the same
225N/A * (e.g., you subscribe many object using a function
225N/A * that lives on the prototype)
225N/A * @return {boolean} true if the subscriber was found and detached.
225N/A */
225N/A unsubscribe: function(type, fn, context) {
225N/A
313N/A // If this is an event handle, use it to detach
313N/A if (Y.Lang.isObject(type) && type.detach) {
225N/A return type.detach();
225N/A }
225N/A
225N/A var evts = this._yuievt.events;
if (type) {
var ce = evts[type];
if (ce) {
return ce.unsubscribe(fn, context);
}
} else {
var ret = true;
for (var i in evts) {
if (Y.Object.owns(evts, i)) {
ret = ret && evts[i].unsubscribe(fn, context);
}
}
return ret;
}
return false;
},
/**
* Removes all listeners from the specified event. If the event type
* is not specified, all listeners from all hosted custom events will
* be removed.
* @method unsubscribeAll
* @param type {string} The type, or name of the event
*/
unsubscribeAll: function(type) {
return this.unsubscribe(type);
},
/**
* Creates a new custom event of the specified type. If a custom event
* by that name already exists, it will not be re-created. In either
* case the custom event is returned.
*
* @method publish
*
* @param type {string} the type, or name of the event
* @param opts {object} optional config params. Valid properties are:
*
* <ul>
* <li>
* context: defines the default execution context. If not defined
* the default context will be this instance.
* </li>
* <li>
* silent: if true, the custom event will not generate log messages.
* This is false by default.
* </li>
* <li>
* 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.
* </li>
* </ul>
*
* @return {Event.Custom} the custom event
*
*/
publish: function(type, opts) {
var o = opts || {}, events = this._yuievt.events, ce = events[type];
if (ce) {
Y.log("publish() skipped: '"+type+"' exists", 'info', 'Event');
// update config for the event
ce.applyConfig(o, true);
} else {
// apply defaults
Y.mix(o, this._yuievt.defaults);
ce = new Y.CustomEvent(type, o);
events[type] = ce;
if (o.onSubscribeCallback) {
ce.subscribeEvent.subscribe(o.onSubscribeCallback);
}
}
return events[type];
},
/**
* Registers another Event.Target as a bubble target. Bubble order
* is determined by the order registered. Multiple targets can
* be specified.
* @method addTarget
* @param o {Event.Target} the target to add
*/
addTarget: function(o) {
this._yuievt.targets[Y.stamp(o)] = o;
},
/**
* Removes a bubble target
* @method removeTarget
* @param o {Event.Target} the target to remove
*/
removeTarget: function(o) {
delete this._yuievt.targets[Y.stamp(o)];
},
/**
* Fire a custom event by name. The callback functions will be executed
* from the context specified when the event was created, and with the
* following parameters.
*
* If the custom event object hasn't been created, then the event hasn't
* been published and it has no subscribers. For performance sake, we
* immediate exit in this case. This means the event won't bubble, so
* if the intention is that a bubble target be notified, the event must
* be published on this object first.
*
* @method fire
* @param type {String|Object} The type of the event, or an object that contains
* a 'type' property.
* @param arguments {Object*} an arbitrary set of parameters to pass to
* the handler.
* @return {boolean} the return value from Event.Custom.fire
*
*/
fire: function(type) {
var typeIncluded = Y.Lang.isString(type),
t = (typeIncluded) ? type : (type && type.type);
var ce = this.getEvent(t);
if (!ce) {
// if (!(type in SILENT)) {
// Y.log(type + ' fire did nothing (not published, no subscribers)', 'info', 'Event');
// }
return true;
}
// Provide this object's subscribers the object they are listening to.
// ce.currentTarget = this;
// This this the target unless target is current not null
// (set in bubble()).
// ce.target = ce.target || this;
var a = Y.Array(arguments, (typeIncluded) ? 1 : 0, true);
var ret = ce.fire.apply(ce, a);
// clear target for next fire()
ce.target = null;
return ret;
},
/**
* Returns the custom event of the provided type has been created, a
* falsy value otherwise
* @method getEvent
* @param type {string} the type, or name of the event
* @return {Event.Target} the custom event or a falsy value
*/
getEvent: function(type) {
var e = this._yuievt.events;
return (e && e[type]);
},
/**
* Propagate an event
* @method bubble
* @param evt {Event.Custom} the custom event to propagate
* @return {boolean} the aggregated return value from Event.Custom.fire
*/
bubble: function(evt) {
var targs = this._yuievt.targets, ret = true;
if (!evt.stopped && targs) {
for (var i in targs) {
if (targs.hasOwnProperty(i)) {
var t = targs[i], type = evt.type,
ce = t.getEvent(type), targetProp = evt.target || this;
// if this event was not published on the bubble target,
// publish it with sensible default properties
if (!ce) {
// publish the event on the bubble target using this event
// for its configuration
ce = t.publish(type, evt);
// set the host and context appropriately
ce.context = (evt.host === evt.context) ? t : evt.context;
ce.host = t;
// clear handlers if specified on this event
ce.defaultFn = null;
ce.preventedFn = null;
ce.stoppedFn = null;
}
ce.target = targetProp;
ce.currentTarget = t;
// ce.target = evt.target;
ret = ret && ce.fire.apply(ce, evt.details);
// stopPropagation() was called
if (ce.stopped) {
break;
}
}
}
}
return ret;
},
/**
* Subscribe to a custom event hosted by this object. The
* supplied callback will execute after any listeners add
* via the subscribe method, and after the default function,
* if configured for the event, has executed.
* @method after
* @param type {string} The type of the event
* @param fn {Function} The callback
* @param context The execution context
* @param args* 1..n params to supply to the callback
*/
after: function(type, fn) {
var ce = this._yuievt.events[type] || this.publish(type),
a = Y.Array(arguments, 1, true);
return ce.after.apply(ce, a);
}
};
// make Y an event target
Y.mix(Y, ET.prototype, false, false, {
bubbles: false
});
ET.call(Y);
}, "3.0.0");