Return to Snippet

Revision: 21232
at December 7, 2009 12:45 by alejandromb


Initial Code
package kc.core {
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;	

	import kc.events.KCComponentEvent;
	import kc.utils.ClassUtil;
	import kc.utils.UID;
	
	[Event( name="disabled", type="kc.events.KCComponentEvent" )]
	[Event( name="enabled", type="kc.events.KCComponentEvent" )]
	[Event( name="dataChange", type="kc.events.KCComponentEvent" )]
	
	public class KCComponent extends MovieClip {
		
		// @protected
		
		protected var _data:XML;
		protected var _autorelease:Boolean;
		
		// @private
		
		private var _uid:String;
		private var _owner:DisplayObjectContainer;
		
		// @constructor
		
		public function KCComponent( data:XML = null, autorelease:Boolean = true ) {
			super();
			// FlashCore
			this.stop();
			this.focusRect = false;
			this.tabEnabled = false;
			this.tabChildren = false;
			// KCCompoenent
			this.data = data;
			this.autorelease = autorelease;
			this.addEventListener( 
				Event.ADDED_TO_STAGE, 
				$config 
			);
		}
		
		// @override
		
		override public function get enabled():Boolean {
			return super.enabled;
		}
		
		override public function set enabled( value:Boolean ):void {
			super.enabled = value;
			this.dispatchEvent( 
				new KCComponentEvent( 
					( ! value )
						? KCComponentEvent.DISABLED 
						: KCComponentEvent.ENABLED 
				)
			);
		}
		
		// @methods
		
		public function get data():XML {
			return this._data;
		}
		
		public function set data( value:XML ):void {
			if( this._data === value ) return;
			this._data = value;
			this.dispatchEvent( 
				new KCComponentEvent ( 
					KCComponentEvent.DATA_CHANGE 
				) 
			);
		}
		
		public function get autorelease():Boolean {
			return this._autorelease;
		}
		
		public function set autorelease( value:Boolean ):void {
			
			if( this._autorelease == value ) {
				return;
			} 
			
			this._autorelease = value;
			
			if( ! this._autorelease ){
				this.removeEventListener( 
					Event.REMOVED_FROM_STAGE, 
					purge 
				);
			}else{
				this.addEventListener( 
					Event.REMOVED_FROM_STAGE, 
					purge 
				);
			}
			
		}	
		
		public function get owner():DisplayObjectContainer {
			return this._owner || this.parent;
		}
		
		public function set owner( value:DisplayObjectContainer ):void {
			this._owner = value;
		}
		
		public function get uid():String {
			if( this._uid == null ){
				this._uid = UID.create();
			} return this._uid;
		}
		
		public function owns( value:DisplayObject ):Boolean {
			if ( this.contains( value ) ) return true;			
			try{
				while( value && value != this ){
					if( value is IKCComponent ){
						value = IKCComponent( value ).owner;
					}else{
						value = value.parent;
					}
				}		         
			}catch( e:SecurityError ){
		        return false;
			} return value == this;
    	}
		
		public function applyToAllChildren( action:String, ...rest ):void {
			var child:IKCComponent;
			for ( var a:int = 0; a < this.numChildren; a++ ) {
				if ( this.getChildAt(a) is IKCComponent ) {
					child = this.getChildAt(a) as IKCComponent;
					if( ClassUtil.isMethod( child, action ) ) {
						child[action].apply( child, rest );
					}else if ( ClassUtil.isPropertyWritable( child, action ) ) {
						child[action] = rest[0];
					}
				}
			}
		}
		
		public function position( x:Number, y:Number = NaN ):void {
			this.x = x;
			this.y = y || x;
		}
		
		public function scale( x:Number, y:Number = NaN ):void {
			this.scaleX = x;
			this.scaleY = y || x;
		}
		
		public function size( w:Number, h:Number = NaN ):void {
			this.width = w;
			this.height = h || w;
		}
		
		// @purge
		
		public function purge(...rest):void {
			
			this._data = null;
			this._uid = null;
			this._owner = null;
			this._autorelease = undefined;
			
			if ( this.hasEventListener( Event.REMOVED_FROM_STAGE ) ) {
				this.removeEventListener(
					Event.REMOVED_FROM_STAGE, 
					purge
				);
			}
			
			if ( this.hasEventListener( Event.ADDED_TO_STAGE ) ) {
				this.removeEventListener(
					Event.ADDED_TO_STAGE, 
					$config
				);
			}
			
		}
		
		// @handlers
		
		protected function $config(e:Event):void {
			
			this.removeEventListener(
				Event.ADDED_TO_STAGE, 
				$config
			);
			
		}
		
	}
	
}

package kc.events {
	
	import flash.events.Event;

	public class KCComponentEvent extends Event {
		
		// @const
		
		public static const ENABLED:String = "enabled";
		public static const DISABLED:String = "disabled";
		public static const DATA_CHANGE:String = "dataChange";
		
		// @constructor
		
		public function KCComponentEvent( type:String, bubbles:Boolean=false, cancelable:Boolean=false ) {
			super( type, bubbles, cancelable );
		}
		
		// @override
		
		override public function clone():Event {
			return new KCComponentEvent( this.type, this.bubbles, this.cancelable );
		} 
		
		override public function toString():String {
			return this.formatToString( "KCComponentEvent", "type", "bubbles", "cancelable", "eventPhase" ); 
		} 
		
	}
	
}

package kc.utils {
	
	import kc.core.KCStatic;

	public class UID extends KCStatic {
		
		// @const 
		
		private static const CHARS:Array = new Array( 48,49,50,51,52,53,54,55,56,57,65,66,67,68,69,70 );
		private static const SEPARATOR:uint = 45;
		
		// @constructor
		
		public function UID() {
			super();
		}
		
		// @methods
		
		public static function create( value:Array = null ):String {
			
			var uid:Array = new Array();
			var template:Array = value || new Array( 8,4,4,4,12 );
			var last:int = template.length - 1;
			
			for ( var a:uint = 0; a < template.length; a++ ) {
				for ( var b:uint = 0; b < template[a]; b++ ) {
					uid.push( CHARS[ Math.floor( Math.random() * CHARS.length ) ] );
				} if ( a < last ) {
					uid.push( SEPARATOR ); 
				}
			}
			
			var time:String = String( 
				"0000000" + new Date().getTime().toString(16).toUpperCase()
			).substr(
				Math.random() * template[last]
			);
			
			return String( 
				String.fromCharCode.apply( 
					null, 
					uid 
				) 
			).substr( 
				0, 
				-time.length 
			) + time;
			
		}
		
	}
	
}

Initial URL
http://www.kirikacode.com.ar/

Initial Description

                                

Initial Title
AS3 | KCComponent | Basic Component

Initial Tags

                                

Initial Language
ActionScript 3