* Alias a method while keeping the context correct, to allow for overwriting of target method.
*
* @param {String} name The name of the target method.
* @return {Function} The aliased method
* @api private
*/
functionalias(name){
returnfunctionaliasClosure(){
returnthis[name].apply(this,arguments);
functionnoop(){}
varlogError=typeofconsole==='undefined'?noop:
function(message){
console.error(message);
};
}
/**
* Returns the listener array for the specified event.
* Will initialise the event object and listener arrays if required.
* Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them.
* Each property in the object response is an array of listener functions.
*
* @param {String|RegExp} evt Name of the event to return the listeners from.
* @return {Function[]|Object} All listener functions for the event.
* do it on initial getSize(), rather than on script load
* For Firefox bug https://bugzilla.mozilla.org/show_bug.cgi?id=548397
*/
functionsetup(){
// setup once
if(isSetup){
return;
}
isSetup=true;
returnflatListeners;
vargetComputedStyle=window.getComputedStyle;
getStyle=(function(){
vargetStyleFn=getComputedStyle?
function(elem){
returngetComputedStyle(elem,null);
}:
function(elem){
returnelem.currentStyle;
};
/**
* Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
returnfunctiongetStyle(elem){
varstyle=getStyleFn(elem);
if(!style){
logError('Style returned '+style+
'. Are you running this code in a hidden iframe on Firefox? '+
* Can be extended to provide event functionality in other classes.
*
* @class EventEmitter Manages event registering and emitting.
*/
functionEventEmitter(){}
// Shortcuts to improve speed and size
varproto=EventEmitter.prototype;
varexports=this;
varoriginalGlobalValue=exports.EventEmitter;
/**
* Finds the index of the listener for the event in its storage array.
*
* @param {Function[]} listeners Array of listeners to search through.
* @param {Function} listener Method to look for.
* @return {Number} Index of the specified listener, -1 if not found
* @api private
*/
functionindexOfListener(listeners,listener){
vari=listeners.length;
while(i--){
if(listeners[i].listener===listener){
returni;
}
}
return-1;
}
/**
* Alias a method while keeping the context correct, to allow for overwriting of target method.
*
* @param {String} name The name of the target method.
* @return {Function} The aliased method
* @api private
*/
functionalias(name){
returnfunctionaliasClosure(){
returnthis[name].apply(this,arguments);
};
}
/**
* Returns the listener array for the specified event.
* Will initialise the event object and listener arrays if required.
* Will return an object if you use a regex search. The object contains keys for each matched event. So /ba[rz]/ might return an object containing bar and baz. But only if you have either defined them with defineEvent or added some listeners to them.
* Each property in the object response is an array of listener functions.
*
* @param {String|RegExp} evt Name of the event to return the listeners from.
* @return {Function[]|Object} All listener functions for the event.
*/
proto.getListeners=functiongetListeners(evt){
varevents=this._getEvents();
varresponse;
varkey;
// Return a concatenated array of all matching events if
// the selector is a regular expression.
if(evtinstanceofRegExp){
response={};
for(keyinevents){
if(events.hasOwnProperty(key)&&evt.test(key)){
response[key]=events[key];
}
}
}
else{
response=events[evt]||(events[evt]=[]);
}
returnresponse;
};
/**
* Takes a list of listener objects and flattens it into a list of listener functions.
*
* @param {Object[]} listeners Raw listener objects.
* @return {Function[]} Just the listener functions.
* Fetches the requested listeners via getListeners but will always return the results inside an object. This is mainly for internal use but others may find it useful.
*
* @param {String|RegExp} evt Name of the event to return the listeners from.
* @return {Object} All listener functions for an event in an object.
...
...
@@ -315,7 +776,7 @@
/**
* Semi-alias of addListener. It will add a listener that will be
* automatically removed after it's first execution.
* automatically removed after its first execution.
*
* @param {String|RegExp} evt Name of the event to attach the listener to.
* @param {Function} listener Method to be called when the event is emitted. If the function returns true then it will be removed after calling.