event-target.js revision e69255aa5a65f8406ba2fabaf69fe4e1d05daf69
(function() {
/**
* 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
* not be created at all.
* @class EventTarget
* @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
* chaining, otherwise they return an EventHandle (default false)
*/
var L = Y.Lang,
PREFIX_DELIMITER = ':',
CATEGORY_DELIMITER = '|',
AFTER_PREFIX = '~AFTER~',
/**
* If the instance has a prefix attribute and the
* event type is not prefixed, the instance prefix is
* applied to the supplied type.
* @method _getType
* @private
*/
return type;
}
}),
/**
* Returns an array with the detach key (if provided),
* and the prefixed event name from _getType
* Y.on('detachcategory, menu:click', fn)
* @method _parseType
* @private
*/
if (!L.isString(t)) {
return t;
}
i = t.indexOf(AFTER_PREFIX);
if (i > -1) {
after = true;
// Y.log(t);
}
// parts = t.split(DETACH_PREFIX_SPLITTER);
// if (parts.length > 1) {
// detachcategory = parts[0];
// t = parts[1];
// if (t == '*') {
// t = null;
// }
// }
i = t.indexOf(CATEGORY_DELIMITER);
if (i > -1) {
t = t.substr(i+1);
if (t == '*') {
t = null;
}
}
}),
// Y.log('EventTarget constructor executed: ' + this._yuid);
this._yuievt = {
events: {},
targets: {},
config: o,
defaults: {
host: this,
emitFacade: o.emitFacade,
}
};
};
/**
* Subscribe to a custom event hosted by this object
* @method on
* @param type {string} The type of the event
* @param fn {Function} The callback
* @return the event target or a detach handle per 'chain' config
*/
f = fn;
c = context;
ret = {};
if (v) {
c = v.context || c;
}
args[1] = f;
args[2] = c;
}, this);
} else if (L.isFunction(type)) {
}
// extra redirection so we catch adaptor events too. take a look at this.
// Y.log("Node detected, redirecting with these args: " + args);
}
if (this instanceof YUI) {
if (Node) {
n = args[2];
if (n instanceof Y.NodeList) {
} else if (n instanceof Node) {
}
}
// check for the existance of an event adaptor
if (adapt) {
}
}
if (!handle) {
// Y.log('parts: ' + parts);
// args = Y.Array(arguments, 1, true);
// f = (after) ? ce.after : ce.on;
// handle = f.apply(ce, args);
handle = ce._on(fn, context, (arguments.length > 3) ? Y.Array(arguments, 3, true) : null, (after) ? 'after' : true);
}
if (detachcategory) {
// Y.log('storing: ' + key);
}
},
/**
* subscribe to an event
* @method subscribe
* @deprecated use on
*/
subscribe: function() {
},
/**
* Detach one or more listeners the from the specified event
* @method detach
* @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 (handles) {
}
}
};
if (detachcategory) {
if (cat) {
if (type) {
} else {
for (i in cat) {
if (cat.hasOwnProperty(i)) {
keyDetacher(cat, i);
}
}
}
}
// If this is an event handle, use it to detach
// extra redirection so we catch adaptor events too. take a look at this.
} else if (Y.Node && (this instanceof Y.Node) && ((!shorttype) || (shorttype in Y.Node.DOM_EVENTS))) {
}
// The YUI instance handles DOM events and adaptors
if (this instanceof YUI) {
// use the adaptor specific detach code if
// DOM event fork
}
}
if (type) {
if (ce) {
}
} else {
for (i in evts) {
if (evts.hasOwnProperty(i)) {
}
}
return ret;
}
},
/**
* detach a listener
* @method unsubscribe
* @deprecated use detach
*/
unsubscribe: function() {
},
/**
* 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 detachAll
* @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
* be removed.
* @method unsubscribeAll
* @param type {string} The type, or name of the event
* @deprecated use detachAll
*/
unsubscribeAll: function() {
},
/**
* 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>
* 'broadcast': whether or not the YUI instance and YUI global are notified when the event is fired (false)
* </li>
* <li>
* 'bubbles': whether or not this event bubbles (true)
* </li>
* <li>
* 'context': the default execution context for the listeners (this)
* </li>
* <li>
* 'defaultFn': the default function to execute when this event fires if preventDefault was not called
* </li>
* <li>
* 'emitFacade': whether or not this event emits a facade (false)
* </li>
* <li>
* 'prefix': the prefix for this targets events, e.g., 'menu' in 'menu:click'
* </li>
* <li>
* 'fireOnce': if an event is configured to fire once, new subscribers after
* the fire will be notified immediately.
* </li>
* <li>
* 'preventable': whether or not preventDefault() has an effect (true)
* </li>
* <li>
* 'preventedFn': a function that is executed when preventDefault is called
* </li>
* <li>
* 'queuable': whether or not this event can be queued during bubbling (false)
* </li>
* <li>
* 'silent': if silent is true, debug messages are not provided for this event.
* </li>
* <li>
* 'stoppedFn': a function that is executed when stopPropagation is called
* </li>
* <li>
* 'type': the event type (valid option if not provided as the first parameter to publish)
* </li>
* </ul>
*
* @return {Event.Custom} the custom event
*
*/
// this._yuievt.config.prefix
ret = {};
}, this);
return ret;
}
//if (ce && !ce.configured) {
if (ce) {
// ce.log("publish applying new config to published event: '"+type+"' exists", 'info', 'event');
if (opts) {
}
} else {
// apply defaults
}
// make sure we turn the broadcast flag off if this
// event was published as a result of bubbling
if (opts instanceof Y.CustomEvent) {
}
},
/**
* Registers another EventTarget as a bubble target. Bubble order
* is determined by the order registered. Multiple targets can
* be specified.
* @method addTarget
* @param o {EventTarget} the target to add
*/
addTarget: function(o) {
this._yuievt.hasTargets = true;
},
/**
* Removes a bubble target
* @method removeTarget
* @param o {EventTarget} the target to remove
*/
removeTarget: function(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 {Event.Target} the event host
*
*/
// this event has not been published or subscribed to
if (!ce) {
// if this object has bubble targets, we need to publish the
// event in order for it to bubble.
if (this._yuievt.hasTargets) {
// ce = this.publish(t);
// ce.details = Y.Array(arguments, (typeIncluded) ? 1 : 0, true);
return this.bubble(null, a, this);
}
// otherwise there is nothing to be done
ret = true;
} else {
// clear target for next fire()
}
},
/**
* 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.Custom} the custom event or null
*/
},
/**
* Propagate an event
* @method bubble
* @param evt {Event.Custom} the custom event to propagate
* @return {boolean} the aggregated return value from Event.Custom.fire
*/
// Y.log('Bubbling ' + evt.type);
for (i in targs) {
if (targs.hasOwnProperty(i)) {
t = targs[i];
// 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;
if (t._yuievt.hasTargets) {
}
} else {
ce.currentTarget = t;
// stopPropagation() was called
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
* @return the event target or a detach handle per 'chain' config
*/
var a = Y.Array(arguments, 0, true);
case 'function':
case 'object':
a[0]._after = true;
break;
default:
}
},
/**
* 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
*
* For methods:
* callback, object (method host), methodName, context, 1-n arguments
*
* @method before
* @return detach handle
* @deprecated use the on method
*/
before: function() {
}
};
Y.EventTarget = ET;
// make Y an event target
bubbles: false
});
/**
* 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.
* @property Global
* @type EventTarget
* @for YUI
*/
// @TODO implement a global namespace function on Y.Global?
})();