/* * Class written by Xavier MARTIN (xxlm or zeflasher) * http://dev.webbymx.net * http://www.webbymx.net * If you are using this class, I will be glad to receive a postcard of your place :) * To do so please visit http://dev.webbymx.net and go in the about page do get my details... */ class net.webbymx.events.XClassEventListener { /* ***************************************************************************** * PRIVATE STATIC VARIABLES ***************************************************************************** */ private static var $instance : XClassEventListener = undefined; private static var _oListeners : Object; /* ***************************************************************************** * CONSTRUCTOR ***************************************************************************** */ private function XClassEventListener() {} /* ***************************************************************************** * PUBLIC STATIC FUNCTIONS ***************************************************************************** */ /** * Initialize the singleton and delegate function * @param obj */ public static function initialize( obj : Object ) : Void { if ( $instance == undefined ) $instance = new XClassEventListener; obj.addEventListener = $instance.addEventListener; obj.removeEventListener = $instance.removeEventListener; } /* ***************************************************************************** * PRIVATE STATIC FUNCTIONS ***************************************************************************** */ /** * dispatch the event to a c * @param eventObj */ public static function dispatchEvent( eventObj : Object ) : Void { // check if the object has the good properties if ( eventObj.target == undefined || eventObj.clazz == undefined || typeof( eventObj.clazz ) != "string" || eventObj.type == undefined || typeof( eventObj.type ) != "string" ) return; // check is the class end event is registered if ( _oListeners[ eventObj.clazz ] == undefined || _oListeners[ eventObj.clazz ][ eventObj.type ] == undefined ) return; // list object and call the function for ( var i : String in _oListeners[ eventObj.clazz ][ eventObj.type ] ) { var o : Object = _oListeners[ eventObj.clazz ][ eventObj.type ][ i ].o; var oType : String = typeof( o ); var f : String = _oListeners[ eventObj.clazz ][ eventObj.type ][ i ].f; if ( oType == "object" || oType == "movieclip" ) { if ( o.handleEvent != undefined && f == undefined ) { o.handleEvent( eventObj ); } else { if ( f == undefined ) f = eventObj.type; o[ f ]( eventObj ); } } } } // internal function to locate listeners: private static function $indexOfListener( listeners : Array, obj : Object, func : String) : Number { var l:Number = listeners.length; var i:Number = -1; while ( ++i < l ) { var tmpObj:Object = listeners[ i ]; if ( tmpObj.o == obj && func == undefined ) return i; else if ( tmpObj.o == obj && tmpObj.f == func ) return i; } return -1; } /* ***************************************************************************** * PRIVATE FUNCTIONS ***************************************************************************** */ /** * Add a listener for an event, for a class * @param p_event: String * @param p_obj: Function (Class.prototype) * @param p_function: String */ // OK private function addEventListener( event : String, clazz : String, func : String) : Void { if ( _oListeners == undefined ) { _oListeners = {}; } // creating a new object for each class holding the events if ( _oListeners[ clazz ] == undefined ) _oListeners[ clazz ] = {}; // creating a array (same name as the function) holding the obj (ref, callbackfunction) if ( _oListeners[ clazz ][ event ] == undefined ) _oListeners[ clazz ][ event ] = []; // check if the object is already in the array, if not pushing it if ( $indexOfListener( _oListeners[ clazz ][ event ], this, func ) == -1 ) _oListeners[ clazz ][ event ].push( { o : this, f : func } ); } /** * Remove the listener for an event, for a class * @param p_event: String * @param p_obj: Function (Class.prototype) * @param p_function: String (optional) */ private function removeEventListener( event : String, clazz : String ) : Void { var func:String = arguments[ 2 ]; // go out of the function if either the class is not registered nor the event for the class if ( _oListeners == undefined || _oListeners[ clazz ] == undefined || _oListeners[ clazz ][ event ] == undefined ) return; /* find the index for the object. If the function is pass as the parameter we will delete just the instance holding this callback function for this class, else we will delete all the instance */ var index:Number; if ( arguments[ 2 ] != undefined ) { index = $indexOfListener( _oListeners[ clazz ][ event ], this, func ); if ( index != -1 ) _oListeners[ clazz ][ event ].splice( index, 1 ); } else { while ( ( index = $indexOfListener( _oListeners[ clazz ][ event ], this ) ) != -1 ) { _oListeners[ clazz ][ event ].splice( index, 1 ); } } if ( index != -1 ) { if ( _oListeners[ clazz ][ event ].length == 0 ) delete _oListeners[ clazz ][ event ]; if ( _oListeners[ clazz ].length == 0 ) delete _oListeners[ clazz ]; } } /** * Remove all the listener listening to an event of a class * @param event * @param clazz */ private function removeAllEventListener( event : String, clazz : String ) : Void { // go out of the function if either the class is not registered nor the event for the class if ( _oListeners == undefined || _oListeners[ clazz ] == undefined || _oListeners[ clazz ][ event ] == undefined ) return; delete _oListeners[ clazz ][ event ]; if ( _oListeners[ clazz ].length == 0 ) delete _oListeners[ clazz ]; if ( _oListeners.length == 0 ) delete _oListeners; } /** * Remove all the listener listening to a class * @param clazz */ private function removeClassListener( clazz : String ) : Void { // go out of the function if either the class is not registered nor the event for the class if ( _oListeners == undefined || _oListeners[ clazz ] == undefined ) return; delete _oListeners[ clazz ]; } }