package cn.helper.load
{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
/*****************************************************
* 通用加载类
* 参考this6的实现思路,Andy做编写、调整和修订。
* 使用方法:
*<CODE>
* var load:CommonLoader = new CommonLoader("url.swf",CommonLoader.TYPE_SWF);
* load.addEventListener(....);
* load.startLoad().
* ....
* trace(load.data);
* trace(load.getClass("symbolName",load.loaderInfo));
*</CODE>
* or
* <CODE>
* var load:CommonLoader = new CommonLoader();
* load.url = "";
* load.type = CommonLoader.TYPE_SWF;
* load.name = "loader_1";
* load.addEventListener(....);
* load.startLoad().
* ....
* trace(load.data);
* trace(load.getClass("symbolName",load.loaderInfo));
* </CODE>
* Author : Andy Chen
* Copyright (c) 2009 CITV-VINVO
* All rights reserved.
*****************************************************/
public class CommonLoader extends EventDispatcher
{
/**
* to load image
*/
public static const TYPE_IMAGE:String = "LoadImg";
/**
*to load xml file or text file
*/
public static const TYPE_XML:String = "LoadXML";
/**
* to load swf
*/
public static const TYPE_SWF:String = "LoadSwf";
private var loader:Loader;
private var urlLoader:URLLoader;
private var urlRequest:URLRequest;
private var context:LoaderContext;
private var _url:String;
private var _type:String;
private var _name:String;
private var _data:Object;
private var _loaderInfo:LoaderInfo;
/**
* the name of a commonLoader initialized
* @return
*/
public function get name():String
{
return _name;
}
/**
* the name of a commonLoader initialized
* @param value
*/
public function set name(value:String):void
{
_name = value;
}
/**
* the url of the file which you will need to load
* @return
*/
public function get url():String
{
return _url;
}
/**
*the url of the file which you will need to load
* @param value
*/
public function set url(value:String):void
{
_url = value;
}
/**
* the type of the file you will load
* @return
*/
public function get type():String
{
return _type;
}
/**
* the type of the file you will load
* @param value
*/
public function set type(value:String):void
{
switch (value)
{
case TYPE_IMAGE:
case TYPE_XML:
case TYPE_SWF:
_type = value;
break;
default:
throw new Error("加载类型没有找到");
break;
}
}
/**
* get the data loaded
* if you loaded a xml ,you will get a data as xml
* if loaded a imageformat file,you will get data as bitmap
* and get MC data with swf type
* @return
*/
public function get data():*
{
if(!_data) setData();
return _data;
}
/**
* loaderInfo
* @return
*/
public function get loaderInfo():LoaderInfo
{
if(type!=TYPE_IMAGE && type!=TYPE_SWF)
throw new Error("当前加载类型不存在loaderInfo对象");
return _loaderInfo;
}
/**
* constructor
* @param _url
* @param _type
* @param _context
* @throws Error
*/
public function CommonLoader(_url:String = "", _type:String = TYPE_SWF, _context:LoaderContext = null)
{
if (StringUtil.trim(_url).length == 0)
return;
url = _url;
if (_context && _context is LoaderContext)
context = _context;
type = _type;
//startLoad();
}
/**
* get the symbol class you have difined in swf with
* <code>ApplicationDomain</code>
*
* name:symbol name. e.g. <code>cn.wenziguo.AndyChen</code>
* info: LoadInfo. <code>null </code>or
*<code>commonLoader.loaderInfo</code>
*
*
* return a Class or null
*/
public function getClass(name:String, info:LoaderInfo = null):Class
{
if (type != TYPE_SWF)
{
throw new Error("当前加载的不是SWF,不能调用 getClass(..) 方法");
return null;
}
try
{
if (info == null)
{
return ApplicationDomain.currentDomain.getDefinition(name) as Class;
}
else
{
return info.applicationDomain.getDefinition(name) as Class;
}
}
catch (e:ReferenceError)
{
trace("定义 " + name + " 不存在");
//throw new Error("定义 " + name + " 不存在");
return null;
}
return null;
}
/**
* start to load.
* this method will must be called to load file
* after you defined a loader
*/
public function startLoad():void
{
switch (type)
{
case TYPE_IMAGE:
case TYPE_SWF:
loader = new Loader();
urlRequest = new URLRequest(url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHttpStatusHandler);
loader.contentLoaderInfo.addEventListener(Event.INIT, onInitHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorHandler);
loader.contentLoaderInfo.addEventListener(Event.OPEN, onOpenHandler);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityHandler);
loader.contentLoaderInfo.addEventListener(Event.UNLOAD, onUnloadHandler);
if (context != null)
{
loader.load(urlRequest, context);
}
else
{
loader.load(urlRequest);
}
break;
case TYPE_XML:
urlLoader = new URLLoader();
urlRequest = new URLRequest(url);
urlLoader.addEventListener(Event.COMPLETE, onCompleteHandler);
urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHttpStatusHandler);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOErrorHandler);
urlLoader.addEventListener(Event.OPEN, onOpenHandler);
urlLoader.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityHandler);
urlLoader.load(urlRequest);
break;
}
}
//================================================================
// the area of private functions
//================================================================
private function removeListener():void
{
switch (type)
{
case TYPE_IMAGE:
case TYPE_SWF:
if (loader && loader.contentLoaderInfo)
{
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCompleteHandler);
loader.contentLoaderInfo.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onHttpStatusHandler);
loader.contentLoaderInfo.removeEventListener(Event.INIT, onInitHandler);
loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onIOErrorHandler);
loader.contentLoaderInfo.removeEventListener(Event.OPEN, onOpenHandler);
loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityHandler);
loader.contentLoaderInfo.removeEventListener(Event.UNLOAD, onUnloadHandler);
}
break;
case TYPE_XML:
if (urlLoader)
{
urlLoader.removeEventListener(Event.COMPLETE, onCompleteHandler);
urlLoader.removeEventListener(HTTPStatusEvent.HTTP_STATUS, onHttpStatusHandler);
urlLoader.removeEventListener(IOErrorEvent.IO_ERROR, onIOErrorHandler);
urlLoader.removeEventListener(Event.OPEN, onOpenHandler);
urlLoader.removeEventListener(ProgressEvent.PROGRESS, onProgressHandler);
urlLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityHandler);
}
break;
}
}
private function setData():void
{
switch (type)
{
case TYPE_IMAGE:
case TYPE_SWF:
if (loader)
{
//_loaderInfo = evt.currentTarget as LoaderInfo;
_loaderInfo = loader.contentLoaderInfo;
_data = loader.content;
}
break;
case TYPE_XML:
if (urlLoader)
{
_data = urlLoader.data;
}
break;
}
}
//================================================================
// the erea of events
//================================================================
private function onCompleteHandler(evt:Event):void
{
setData();
removeListener();
dispatchEvent(evt);
}
private function onProgressHandler(evt:ProgressEvent):void
{
trace("evt.bytesLoaded / evt.bytesTotal------:"+evt.bytesLoaded / evt.bytesTotal + "");
dispatchEvent(evt);
}
private function onUnloadHandler(evt:Event):void
{
removeListener();
dispatchEvent(evt);
}
private function onIOErrorHandler(evt:IOErrorEvent):void
{
removeListener();
dispatchEvent(evt);
}
private function onSecurityHandler(evt:SecurityErrorEvent):void
{
removeListener();
dispatchEvent(evt);
}
private function onOpenHandler(evt:Event):void
{
dispatchEvent(evt);
}
private function onInitHandler(evt:Event):void
{
dispatchEvent(evt);
}
private function onHttpStatusHandler(evt:HTTPStatusEvent):void
{
dispatchEvent(evt);
}
}
}
通用Loader
最新推荐文章于 2021-02-19 04:10:12 发布