event-custom.js revision 4e0176adfd6a21804a9f09a09cdbdaa014c32283
* Allows for the insertion of methods that are executed before or after * Cache of objects touched by the utility * Execute the supplied method before the specified function * @param fn {Function} the function to execute * @param obj the object hosting the method to displace * @param sFn {string} the name of the method to displace * @param c The execution context for fn * @return {string} handle for the subscription * Execute the supplied method after the specified function * @param fn {Function} the function to execute * @param obj the object hosting the method to displace * @param sFn {string} the name of the method to displace * @param c The execution context for fn * @return {string} handle for the subscription * Execute the supplied method after the specified function * @param when {string} before or after * @param fn {Function} the function to execute * @param obj the object hosting the method to displace * @param sFn {string} the name of the method to displace * @param c The execution context for fn * @return {string} handle for the subscription // create a map entry for the obj if it doesn't exist // create a map entry for the method if it doesn't exist // re-route the method to our wrapper * Detach a before or after subscription * @param handle {string} the subscription handle ////////////////////////////////////////////////////////////////////////// * Wrapper for a displaced method with aop enabled * @param obj The object to operate on * @param sFn The name of the method to displace * Register a aop subscriber * @param sid {string} the subscriber id * @param fn {Function} the function to execute * @param when {string} when to execute the function * Unregister a aop subscriber * @param sid {string} the subscriber id * @param fn {Function} the function to execute * @param when {string} when to execute the function * Execute the wrapped method // execute after methods. // Stop processing if a Halt object is returned // Check for a new return value ////////////////////////////////////////////////////////////////////////// * Return an AlterArgs object when you want to change the arguments that * were passed into the function. An example would be a service that scrubs * out illegal characters prior to executing the core business logic. * Return an AlterReturn object when you want to change the result returned * from the core method to the caller * Return a Halt object when you want to terminate the execution * of all subsequent subscribers as well as the wrapped method * if it has not exectued yet. * Return a Prevent object when you want to prevent the wrapped function * from executing, but want the remaining listeners to execute * Return an Error object when you want to terminate the execution * of all subsequent method calls. * @deprecated use Y.Do.Halt or Y.Do.Prevent ////////////////////////////////////////////////////////////////////////// // Y["Event"] && Y.Event.addListener(window, "unload", Y.Do._unload, Y.Do); * Wraps and protects a custom event for use when emitFacade is set to true. * @param e {Event} the custom event * @param currentTarget {HTMLElement} the element the listener was attached to stopImmediatePropagation: 2, Y.EventFacade = function(e, currentTarget) { Y.Object.each(PROPS, function(v, k) { //this[k] = (v == 2) ? e[k].apply(e, arguments) : e[k]; this[k] = (v == 2) ? function() { console.log('missing ' + k); * The arguments passed to fire ////////////////////////////////////////////////////// * Node reference for the targeted eventtarget * Node reference for the element that the listener was attached to. * Node reference to the relatedTarget * Stops the propagation to the next bubble target * @method stopPropagation * Stops the propagation to the next bubble target and * prevents any additional listeners from being exectued * @method stopImmediatePropagation * Prevents the event's default behavior * Stops the event propagation and prevents the default * @param immediate {boolean} if true additional listeners * on the current target will not be executed * Custom event engine, DOM event listener abstraction layer, synthetic DOM * Return value from all subscribe operations * @param evt {CustomEvent} the custom event * @param sub {Subscriber} the subscriber // var onsubscribeType = "_event:onsub", * Detaches this subscriber * 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 * @param o configuration object // if (arguments.length > 2) { // this.log('CustomEvent context and silent are now in the config', 'warn', 'Event'); * The type of event, returned to subscribers when the event fires * The context the the event will fire from by default. Defaults to the YUI * If 0, this event does not broadcast. If 1, the YUI instance is notified * every time this event fires. If 2, the YUI instance and the YUI global * (if event is enabled on the global) are notified every time this event * By default all custom events are logged in the debug build, set silent * to true to disable debug outpu for this event. // this.queuable = false; * The subscribers to this event * The publisher has configured this event * This event has fired if true * This event should only fire one time if true, and if * it has fired, any new subscribers should be notified // this.fireOnce = false; * Flag for stopPropagation that is modified during fire() * 1 means to stop propagation to bubble targets. 2 means * to also stop additional subscribers on this target. * Flag for preventDefault that is modified during fire(). * if it is not 0, the default behavior for this event * Specifies the host for this custom event. This is used * to enable event bubbling * The default function to execute after event listeners * have fire, but only if the default action was not // this.defaultFn = null; * The function to execute if a subscriber calls * stopPropagation or stopImmediatePropagation // this.stoppedFn = null; * The function to execute if a subscriber calls // this.preventedFn = null; * Specifies whether or not this event's default function * can be cancelled by a subscriber by executing preventDefault() * Specifies whether or not a subscriber can stop the event propagation * via stopPropagation(), stopImmediatePropagation(), or halt() * Supports multiple options for listener signatures in order to // this.hasSubscribers = false; // this.hasAfters = false; * If set to true, the custom event will deliver an EventFacade object * that is similar to a DOM event object. // this.emitFacade = false; // this.log("Creating " + this.type); * Apply configuration properties. Only applies the CONFIG whitelist * @param o hash of properties to apply * @param force {boolean} if true, properties that exist on the event Y.
error(
"Invalid callback for CE: " +
this.
type);
* @param {Function} fn The function to execute * @return {EventHandle|EventTarget} unsubscribe handle or a * chainable event target depending on the 'chain' config. * @param {Function} fn The function to execute * @return {EventHandle|EventTarget} unsubscribe handle or a * chainable event target depending on the 'chain' config. * Listen for this event after the normal subscribers have been notified and * the default behavior has been applied. If a normal subscriber prevents the * default behavior, it also prevents after listeners from firing. * @param {Function} fn The function to execute * @return {EventHandle|EventTarget} unsubscribe handle or a * chainable event target depending on the 'chain' config. * @param {Function} fn The subscribed function to remove, if not supplied * @param {Object} context The context object passed to subscribe. * @return {boolean|EventTarget} returns a chainable event target * or a boolean for legacy detach support. // if arg[0] typeof unsubscribe handle * @param {Function} fn The subscribed function to remove, if not supplied * @param {Object} context The context object passed to subscribe. * @return {boolean|EventTarget} returns a chainable event target * or a boolean for legacy detach support. // if the first argument is an object literal, apply the // properties to the event facade // protect the event facade properties // update the details field with the arguments * Notify a single subscriber * @param s {Subscriber} the subscriber * @param args {Array} the arguments array to apply to the listener this.
log(
this.
type +
"->" +
": " + s);
// emit an EventFacade if this is that sort of event // @TODO object literal support to fire makes it possible for // config info to be passed if we wish. this.
log(
this.
type +
" cancelled by subscriber");
* Logger abstraction to centralize the application of the silent flag * @param msg {string} message to log * @param cat {string} log category * Notifies the subscribers. The callback functions will be executed * from the context specified when the event was created, and with the * <li>The type of event</li> * <li>All of the arguments fire() was executed with as an array</li> * <li>The custom object (if any) that was passed into the subscribe() * @param {Object*} arguments an arbitrary set of parameters to pass to * @return {boolean} false if one of the subscribers returned false, // @TODO find a better way to short circuit this. // if (!this.broadcast && !this.defaultFn && !this.hasSubscribers && !this.hasAfters) { // queue this event if the current item in the queue bubbles // if (b && this.queuable && this.type != es.next.type) { // id of the first event in the stack this.
log(
'fireOnce event: ' +
this.
type +
' already fired');
// this.log("Firing " + this + ", " + "args: " + args); // } // foo is still 1 unless we create a new facade // stopImmediatePropagation // bubble if this is hosted in an event target and propagation has not been stopped // execute the default behavior if not prevented // broadcast listeners are fired as discreet events on the // YUI instance and potentially the YUI global. // process after listeners. If the default behavior was // prevented, the after events don't fire. // stopImmediatePropagation // console.log('clearing stack: ' + es.id + ', ' + this); // reset propragation properties while processing the rest of the queue // q[0] = the event, q[1] = arguments to fire // set up stack to allow the next item to be processed // return (ret !== false); return this.
stopped ?
false :
true;
* @return {int} The number of listeners unsubscribed * @deprecated use detachAll * @return {int} The number of listeners unsubscribed * @param subscriber object * Stop propagation to bubble targets * @method stopPropagation * Stops propagation to bubble targets, and prevents any remaining * subscribers on the current target from executing. * @method stopImmediatePropagation * Prevents the execution of this event's defaultFn * Stops the event propagation and prevents the default * @param immediate {boolean} if true additional listeners * on the current target will not be executed ///////////////////////////////////////////////////////////////////// * Stores the subscriber information to be used when the event fires. * @param {Function} fn The wrapped function to execute * @param {Object} context The value of the keyword 'this' in the listener * @param {Array} args* 0..n additional arguments to supply the listener * The callback that will be execute when the event fires * This is wrapped by Y.rbind if obj was supplied. * Optional 'this' keyword for the listener * fn bound to obj with additional arguments applied via Y.rbind * Additional arguments to propagate to the subscriber * Custom events for a given fire transaction. // this.wrappedFn = Y.rbind.apply(Y, args); * Executes the subscriber. * @param args {Array} Arguments array for the subscriber * @param ce {CustomEvent} The custom event that sent the notification // Ease debugging by only catching errors if we will not re-throw * Returns true if the fn and obj match this objects properties. * Used by the unsubscribe method to match the right subscriber. * @param {Function} fn the function to execute * @param {Object} context optional 'this' keyword for the listener * @return {boolean} true if the supplied arguments match this * subscriber's signature. return "Subscriber " +
this.
id;
// FACADE = new Y.EventFacade(new Y.CustomEvent('x')); * EventTarget provides the implementation for any object to * publish, subscribe and fire to custom events, and also * alows other EventTargets to target the object with events * sourced from the other object. * EventTarget is designed to be used with Y.augment to wrap * EventCustom in an interface that allows events to be listened to * and fired by name. This makes it possible for implementing code to * subscribe to an event that either has not been created yet, or will * @param opts a configuration object * @config emitFacade {boolean} if true, all events will emit event * facade payloads by default (default false) * @config prefix {string} the prefix to apply to non-prefixed event names * @config chain {boolean} if true, on/after/detach return the host to allow * chaining, otherwise they return an EventHandle (default false) * If the instance has a prefix attribute and the * event type is not prefixed, the instance prefix is * applied to the supplied type. * Returns an array with the detach key (if provided), * and the prefixed event name from _getType * Y.on('detachcategory, menu:click', fn) // parts = t.split(DETACH_PREFIX_SPLITTER); // if (parts.length > 1) { // detachcategory = parts[0]; * Subscribe to a custom event hosted by this object * @param type {string} The type of the event * @param fn {Function} The callback * @return the event target or a detach handle per 'chain' config // extra redirection so we catch adaptor events too. take a look at this. if (
this instanceof YUI) {
}
else if (n
instanceof Node) {
// Captures both DOM events and event plugins. // check for the existance of an event adaptor * Detach one or more listeners the from the specified event * @param type {string|Object} Either the handle to the subscriber or the * type of event. If the type * is not specified, it will attempt to remove * the listener from all hosted events. * @param fn {Function} The subscribed function to unsubscribe, if not * supplied, all subscribers will be removed. * @param context {Object} The custom object passed to subscribe. This is * optional, but if supplied will be used to * disambiguate multiple listeners that are the same * (e.g., you subscribe many object using a function * that lives on the prototype) * @return {EventTarget} the host // If this is an event handle, use it to detach // extra redirection so we catch adaptor events too. take a look at this. // The YUI instance handles DOM events and adaptors if (
this instanceof YUI) {
// use the adaptor specific detach code if * Removes all listeners from the specified event. If the event type * is not specified, all listeners from all hosted custom events will * @param type {string} The type, or name of the event * Removes all listeners from the specified event. If the event type * is not specified, all listeners from all hosted custom events will * @param type {string} The type, or name of the event * @deprecated use detachAll * 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. * @param type {string} the type, or name of the event * @param opts {object} optional config params. Valid properties are: * 'broadcast': whether or not the YUI instance and YUI global are notified when the event is fired (false) * 'bubbles': whether or not this event bubbles (true) * 'context': the default execution context for the listeners (this) * 'defaultFn': the default function to execute when this event fires if preventDefault was not called * 'emitFacade': whether or not this event emits a facade (false) * 'prefix': the prefix for this targets events, e.g., 'menu' in 'menu:click' * 'fireOnce': if an event is configured to fire once, new subscribers after * the fire will be notified immediately. * 'preventable': whether or not preventDefault() has an effect (true) * 'preventedFn': a function that is executed when preventDefault is called * 'queuable': whether or not this event can be queued during bubbling (false) * 'silent': if silent is true, debug messages are not provided for this event. * 'stoppedFn': a function that is executed when stopPropagation is called * 'type': the event type (valid option if not provided as the first parameter to publish) * @return {Event.Custom} the custom event // ce.log("publish applying new config to published event: '"+type+"' exists", 'info', 'event'); // make sure we turn the broadcast flag off if this // event was published as a result of bubbling * Registers another EventTarget as a bubble target. Bubble order * is determined by the order registered. Multiple targets can * @param o {EventTarget} the target to add * Removes a bubble target * @param o {EventTarget} the target to remove * Fire a custom event by name. The callback functions will be executed * from the context specified when the event was created, and with the * 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. * The first argument is the event type, and any additional arguments are * passed to the listeners as parameters. If the first of these is an * object literal, and the event is configured to emit an event facade, * that object is mixed into the event facade and the facade is provided * in place of the original object. * @param type {String|Object} The type of the event, or an object that contains * @param arguments {Object*} an arbitrary set of parameters to pass to * the handler. If the first of these is an object literal and the event is * configured to emit an event facade, the event facade will replace that * parameter after the properties the object literal contains are copied to * @return {Event.Target} the event host // this event has not been published or subscribed to // if this object has bubble targets, we need to publish the // event in order for it to bubble. // ce.details = Y.Array(arguments, (typeIncluded) ? 1 : 0, true); return this.
bubble(
null, a,
this);
// otherwise there is nothing to be done // clear target for next fire() * Returns the custom event of the provided type has been created, a * @param type {string} the type, or name of the event * @return {Event.Custom} the custom event or null return (e &&
type in e) ? e[
type] :
null;
* @param evt {Event.Custom} the custom event to propagate * @return {boolean} the aggregated return value from Event.Custom.fire // if this event was not published on the bubble target, // publish it with sensible default properties // publish the event on the bubble target using this event // ce = t.publish(type, evt); // set the host and context appropriately // ce.context = (evt.host === evt.context) ? t : evt.context; // clear handlers if specified on this event // ce.preventedFn = null; // stopPropagation() was called * 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. * @param type {string} The type of the event * @param fn {Function} The callback * @return the event target or a detach handle per 'chain' config * Executes the callback before a DOM event, custom event * or method. If the first argument is a function, it * is assumed the target is a method. For DOM and custom * events, this is an alias for Y.on. * For DOM and custom events: * type, callback, context, 1-n arguments * callback, object (method host), methodName, context, 1-n arguments * @deprecated use the on method // make Y an event target * Hosts YUI page level events. This is where events bubble to * when the broadcast config is set to 2. This property is * only available if the custom event module is loaded. // @TODO implement a global namespace function on Y.Global?