Created
June 11, 2013 06:31
-
-
Save ticking-clock/5754846 to your computer and use it in GitHub Desktop.
Change to Easeljs/events/EventDispatcher.js to support multiple space-separated event types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EventDispatcher = function() { | |
this.initialize(); | |
}; | |
var p = EventDispatcher.prototype; | |
EventDispatcher.initialize = function(target) { | |
target.addEventListener = p.addEventListener; | |
target.removeEventListener = p.removeEventListener; | |
target.removeAllEventListeners = p.removeAllEventListeners; | |
target.hasEventListener = p.hasEventListener; | |
target.dispatchEvent = p.dispatchEvent; | |
}; | |
p._listeners = null; | |
p.initialize = function() {}; | |
p.addEventListener = function(types, listener) { | |
var type, i = 0, typesArr = types.split(" "), listeners = this._listeners; | |
if (!listeners) { listeners = this._listeners = {}; } | |
else { this.removeEventListener(types, listener); } | |
while (type = typesArr[i++]) { | |
var arr = listeners[type]; | |
if (!arr) { arr = listeners[type] = []; } | |
arr.push(listener); | |
} | |
return listener; | |
}; | |
p.removeEventListener = function(types, listener) { | |
types = types.split(" "); | |
var type, i = 0, listeners = this._listeners; | |
if (!listeners) { return; } | |
while (type = types[i++]) { | |
var arr = listeners[type]; | |
if (!arr) { return; } | |
for (var i=0,l=arr.length; i<l; i++) { | |
if (arr[i] == listener) { | |
if (l==1) { delete(listeners[type]); } // allows for faster checks. | |
else { arr.splice(i,1); } | |
break; | |
} | |
} | |
} | |
}; | |
p.removeAllEventListeners = function(types) { | |
var type, i = 0; | |
if (!types) { this._listeners = null; } | |
else if (this._listeners) { | |
types = types.split(" "); | |
while (type = types[i++]) { delete(this._listeners[type]); } | |
} | |
}; | |
p.dispatchEvent = function(eventObj, target) { | |
var ret=false, listeners = this._listeners; | |
if (eventObj && listeners) { | |
if (typeof eventObj == "string") { eventObj = {type:eventObj}; } | |
var arr = listeners[eventObj.type]; | |
if (!arr) { return ret; } | |
eventObj.target = target||this; | |
arr = arr.slice(); // to avoid issues with items being removed or added during the dispatch | |
for (var i=0,l=arr.length; i<l; i++) { | |
var o = arr[i]; | |
if (o.handleEvent) { ret = ret||o.handleEvent(eventObj); } | |
else { ret = ret||o(eventObj); } | |
} | |
} | |
return !!ret; | |
}; | |
p.hasEventListener = function(type) { | |
var listeners = this._listeners; | |
return !!(listeners && listeners[type]); | |
}; | |
p.toString = function() { | |
return "[EventDispatcher]"; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment