API Docs for: 最后更新日期:2014年8月25日
Google搜索   
Show:

File: events/LEventDispatcher.js

/** @language english
 * <p>The LEventDispatcher class is the base class for all classes that dispatch events.</p>
 * @class LEventDispatcher
 * @extends LObject
 * @constructor
 * @since 1.8.0
 * @public
 */
var LEventDispatcher = (function () {
	function LEventDispatcher () {
		var s = this;
		LExtends(s, LObject, []);
		s._eventList = new Array();
	}
	var p = {
		/** @language english
		 * <p>Registers an event listener object with an LEventDispatcher object so that the listener receives notification of an event. You can register event listeners on all nodes in the display list for a specific type of event, phase, and priority.</p>
		 * <p>After you successfully register an event listener, you cannot change its priority through additional calls to addEventListener(). To change a listener's priority, you must first call removeListener(). Then you can register the listener again with the new priority level.</p>
		 * <p>If you no longer need an event listener, remove it by calling removeEventListener(), or memory problems could result.</p>
		 * @method addEventListener
		 * @param {String} type The type of event.
		 * @param {Function} listener The listener function that processes the event. 
		 * @public
		 * @since 1.8.0
		 */
		addEventListener : function (type, listener) {
			this._eventList.push({listener : listener, type : type});
		},
		/** @language english
		 * <p>Removes a listener from the LEventDispatcher object. If there is no matching listener registered with the LEventDispatcher object, a call to this method has no effect.</p>
		 * @method removeEventListener
		 * @param {String} type The type of event.
		 * @param {Function} listener The listener object to remove.
		 * @public
		 * @since 1.8.0
		 */
		removeEventListener : function (type, listener) {
			var s = this, i, length;
			length = s._eventList.length;
			for (i = 0; i < length; i++) {
				if (!s._eventList[i]) {
					continue;
				}
				if (type == s._eventList[i].type && s._eventList[i].listener == listener) {
					s._eventList.splice(i, 1);
					return;
				}
			}
		},
		/** @language english
		 * <p>Removes all the listeners from the LEventDispatcher object. </p>
		 * @method removeAllEventListener
		 * @public
		 * @since 1.8.0
		 */
		removeAllEventListener : function () {
			this._eventList = [];
		},
		/** @language english
		 * <p>Dispatches an event into the event flow. The event target is the LEventDispatcher object upon which the dispatchEvent() method is called.</p>
		 * @method dispatchEvent
		 * @param {LEvent | String} event  The Event object that is dispatched into the event flow. If the event is being redispatched, a clone of the event is created automatically. After an event is dispatched, its target property cannot be changed, so you must create a new copy of the event for redispatching to work.
		 * @return {Boolean} A value of true if the event was successfully dispatched.
		 * 	function MyEventObject(){
		 * 		var self = this;
		 * 		LExtends(self,LSprite,[]);
		 * 		self.graphics.drawRect(1,"#000000",[0,0,100,100],true,"#000000");
		 * 		self.graphics.drawRect(1,"#FF0000",[100,0,100,100],true,"#FF0000");
		 * 		self.addEventListener(LMouseEvent.MOUSE_UP,self.onclick);
		 * 		self.addEventListener(MyEventObject.CLICK_LEFT,function(event){
		 * 			trace("dispatchEvent");
		 * 		});
		 * 		self.addEventListener(MyEventObject.CLICK_RIGHT,function(event){
		 * 			trace("dispatchEvent event.name = " + event.name);
		 * 		});
		 * 	}
		 * 	MyEventObject.CLICK_LEFT = "click_left";
		 * 	MyEventObject.CLICK_RIGHT = "click_right";
		 * 	MyEventObject.prototype.onclick = function(event){
		 * 		var self = event.clickTarget;
		 * 		if(event.selfX < 100){
		 * 			self.dispatchEvent(MyEventObject.CLICK_LEFT);
		 * 		}else{
		 * 			var event = new LEvent(MyEventObject.CLICK_RIGHT);
		 * 			event.name = "LEvent Test";
		 * 			self.dispatchEvent(event);
		 * 		}
		 * 	}
		 * @examplelink <p><a href="../../../api/LEventDispatcher/dispatchEvent.html" target="_blank">Try it »</a></p>
		 * @public
		 * @since 1.8.0
		 */
		dispatchEvent : function (event) {
			var s = this, i, length = s._eventList.length, ctype = (typeof event == "string") ? event : event.eventType;
			for (i = 0; i < length; i++) {
				if (!s._eventList[i]) {
					continue;
				}
				if (ctype == s._eventList[i].type) {
					if (typeof event == "string") {
						s.currentTarget = s.target = s;
						s.eventType = s.event_type = ctype;
						s._eventList[i].listener(s);
					}else{
						if (!event.target) {
							event.target = s;
						}
						if (!event.currentTarget) {
							event.currentTarget = event.target;
						}
						event._ll_preventDefault = false;
						s._eventList[i].listener(event);
						if (event._ll_preventDefault) {
							return false;
						}
					}
					return true;
				}
			}
			return false;
		},
		/** @language english
		 * <p>Checks whether the LEventDispatcher object has any listeners registered for a specific type of event. This allows you to determine where an LEventDispatcher object has altered handling of an event type in the event flow hierarchy. </p>
		 * @method hasEventListener
		 * @param {String} type The type of event.
		 * @return {Boolean} A value of true if a listener of the specified type is registered; false otherwise.
		 * @public
		 * @since 1.8.0
		 */
		hasEventListener : function (type, listener) {
			var s = this, i, length = s._eventList.length;
			for (i = 0; i < length; i++) {
				if (!s._eventList[i]) {
					continue;
				}
				if (type == s._eventList[i].type) {
					if (typeof listener == UNDEFINED || listener == s._eventList[i].listener) {
						return true;
					}
				}
			}
			return false;
		}
	};
	for (var k in p) {
		LEventDispatcher.prototype[k] = p[k];
	}
	return LEventDispatcher;
})();