/* * EventDispatcher * Visit http://createjs.com/ for documentation, updates and examples. * * Copyright (c) 2010 gskinner.com, inc. * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ // namespace: this.createjs = this.createjs||{}; (function() { /** * The EventDispatcher provides methods for managing prioritized queues of event listeners and dispatching events. All * {{#crossLink "DisplayObject"}}{{/crossLink}} classes dispatch events, as well as some of the utilities like {{#crossLink "Ticker"}}{{/crossLink}}. * * You can either extend this class or mix its methods into an existing prototype or instance by using the * EventDispatcher {{#crossLink "EventDispatcher/initialize"}}{{/crossLink}} method. * *

Example

* Add EventDispatcher capabilities to the "MyClass" class. * * EventDispatcher.initialize(MyClass.prototype); * * Add an event (see {{#crossLink "EventDispatcher/addEventListener"}}{{/crossLink}}). * * instance.addEventListener("eventName", handlerMethod); * function handlerMethod(event) { * console.log(event.target + " Was Clicked"); * } * * Maintaining proper scope
* When using EventDispatcher in a class, you may need to use Function.bind or another approach to * maintain you method scope. Note that Function.bind is not supported in some older browsers. * * instance.addEventListener("click", handleClick.bind(this)); * function handleClick(event) { * console.log("Method called in scope: " + this); * } * * Please note that currently, EventDispatcher does not support event priority or bubbling. Future versions may add * support for one or both of these features. * * @class EventDispatcher * @constructor **/ var EventDispatcher = function() { this.initialize(); }; var p = EventDispatcher.prototype; /** * Static initializer to mix in EventDispatcher methods. * @method initialize * @static * @param {Object} target The target object to inject EventDispatcher methods into. This can be an instance or a * 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; }; // private properties: /** * @protected * @property _listeners * @type Object **/ p._listeners = null; // constructor: /** * Initialization method. * @method initialize * @protected **/ p.initialize = function() {}; // public methods: /** * Adds the specified event listener. * @method addEventListener * @param {String} type The string type of the event. * @param {Function | Object} listener An object with a handleEvent method, or a function that will be called when * the event is dispatched. * @return {Function | Object} Returns the listener for chaining or assignment. **/ p.addEventListener = function(type, listener) { var listeners = this._listeners; if (!listeners) { listeners = this._listeners = {}; } else { this.removeEventListener(type, listener); } var arr = listeners[type]; if (!arr) { arr = listeners[type] = []; } arr.push(listener); return listener; }; /** * Removes the specified event listener. * @method removeEventListener * @param {String} type The string type of the event. * @param {Function | Object} listener The listener function or object. **/ p.removeEventListener = function(type, listener) { var listeners = this._listeners; if (!listeners) { return; } var arr = listeners[type]; if (!arr) { return; } for (var i=0,l=arr.length; i