/** * VERSION: 1.921 * DATE: 2012-08-09 * AS3 * UPDATES AND DOCS AT: http://www.greensock.com/loadermax/ **/ package com.greensock.loading { import com.greensock.events.LoaderEvent; import com.greensock.loading.core.LoaderCore; import com.greensock.loading.core.LoaderItem; import flash.display.DisplayObject; import flash.events.Event; import flash.net.URLRequest; import flash.system.LoaderContext; import flash.utils.Dictionary; /** Dispatched when any child of the LoaderMax instance starts loading. So if a LoaderMax contains 5 loaders, the CHILD_OPEN event will be dispatched 5 times during the course of the LoaderMax's load. This can occur even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="childOpen", type="com.greensock.events.LoaderEvent")] /** Dispatched when any child of the LoaderMax instance dispatches a PROGRESS event. This can occur even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="childProgress", type="com.greensock.events.LoaderEvent")] /** Dispatched when any child of the LoaderMax instance completes. So if a LoaderMax contains 5 loaders, the CHILD_COMPLETE event will be dispatched 5 times during the course of the LoaderMax's load. This can occur even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="childComplete", type="com.greensock.events.LoaderEvent")] /** Dispatched when any child of the LoaderMax instance fails to load. This occurs even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="childFail", type="com.greensock.events.LoaderEvent")] /** Dispatched when any child of the LoaderMax instance dispatches a CANCEL event which could occur when another child is prioritized in the queue or when the LoaderMax is canceled while loading the child. CHILD_CANCEL can be dispatched even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="childCancel", type="com.greensock.events.LoaderEvent")] /** Dispatched when any child of the LoaderMax instance dispatches a SCRIPT_ACCESS_DENIED event. This can occur even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="scriptAccessDenied", type="com.greensock.events.LoaderEvent")] /** Dispatched when any child of the LoaderMax instance dispatches an HTTP_STATUS event. This can occur even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="httpStatus", type="com.greensock.events.LoaderEvent")] /** Dispatched when any child of the LoaderMax instance dispatches an IO_ERROR event. This can occur even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="ioError", type="com.greensock.events.LoaderEvent")] /** Dispatched when any child of the LoaderMax instance dispatches a SECURITY_ERROR event. This can occur even if the LoaderMax itself isn't in the process of loading (because load() or prioritize() could have been called directly on a child loader) **/ [Event(name="securityError", type="com.greensock.events.LoaderEvent")] /** * In its simplest form, a LoaderMax provides a way to group a sequence of loaders together and * report their progress as a whole. It is essentially a queue of loaders. But there are many other * conveniences that the LoaderMax system delivers: *
* * @example Example AS3 code:import com.greensock.~~; import com.greensock.loading.~~; import com.greensock.events.LoaderEvent; import com.greensock.loading.display.~~; //create a LoaderMax named "mainQueue" and set up onProgress, onComplete and onError listeners var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler}); //append several loaders queue.append( new XMLLoader("xml/data.xml", {name:"xmlDoc", alternateURL:"http://otherserver.com/data.xml"}) ); queue.append( new ImageLoader("img/photo1.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0, width:250, height:150, scaleMode:"proportionalInside"}) ); queue.append( new SWFLoader("swf/main.swf", {name:"mainClip", estimatedBytes:3000, container:this, x:250, autoPlay:false}) ); //add a loader to the top of the queue using prepend() queue.prepend( new MP3Loader("mp3/audio.mp3", {name:"audio", repeat:100, autoPlay:true}) ); //prioritize the loader named "photo1" LoaderMax.prioritize("photo1"); //same as LoaderMax.getLoader("photo1").prioritize(); //start loading queue.load(); function progressHandler(event:LoaderEvent):void { trace("progress: " + event.target.progress); } function completeHandler(event:LoaderEvent):void { var image:ContentDisplay = LoaderMax.getContent("photo1"); TweenLite.to(image, 1, {alpha:1, y:100}); trace(event.target + " is complete!"); } function errorHandler(event:LoaderEvent):void { trace("error occured with " + event.target + ": " + event.text); } * * LoaderMax will automatically skip over any child loaders in the queue that are already complete. By default * it will also skip any that have failed or are paused (you can change this behavior with the skipFailed * and skipPaused special properties). To flush the content and force a full reload, simply unload() * first or use the flushContent parameter in load() like load(true).

* * OPTIONAL VARS PROPERTIES
* The following special properties can be passed into the LoaderMax constructor via the vars * parameter which can be either a generic object or a LoaderMaxVars object:
*

* * Note: Using a LoaderMaxVars instance * instead of a generic object to define your vars is a bit more verbose but provides * code hinting and improved debugging because it enforces strict data typing. Use whichever one you prefer.

* * Copyright 2010-2012, GreenSock. All rights reserved. This work is subject to the terms in http://www.greensock.com/terms_of_use.html or for corporate Club GreenSock members, the software agreement that was issued with the corporate membership. * * @see com.greensock.loading.data.LoaderMaxVars * * @author Jack Doyle, jack@greensock.com */ public class LoaderMax extends LoaderCore { /** @private **/ public static const version:Number = 1.921; /** The default value that will be used for the estimatedBytes on loaders that don't declare one in the vars parameter of the constructor. **/ public static var defaultEstimatedBytes:uint = 20000; /** Controls the default value of auditSize in LoaderMax instances (normally true). For most situations, the auditSize feature is very convenient for ensuring that the overall progress of LoaderMax instances is reported accurately, but when working with very large quantities of files that have no estimatedBytes defined, some developers prefer to turn auditSize off by default. Of course you can always override the default for individual LoaderMax instances by defining an auditSize value in the vars parameter of the constructor. **/ public static var defaultAuditSize:Boolean = true; /** Optionally define a default LoaderContext to use with SWFLoaders and ImageLoaders. This can be useful if you're loading a lot of swfs, for example, and don't want to pass a custom "context" in to each one. LoaderContexts are typically used to tell Flash which ApplicationDomain to load the code into and which SecurityDomain to use. See Adobe's docs for details. **/ public static var defaultContext:LoaderContext; /** The class used by ImageLoaders, SWFLoaders, and VideoLoaders to create the containers into which they'll dump their rawContent - by default it is the com.greensock.loading.display.ContentDisplay class but if you're using Flex, it is typically best to change this to com.greensock.loading.display.FlexContentDisplay. You only need to do this once, like
import com.greensock.loading.LoaderMax;
import com.greensock.loading.display.FlexContentDisplay;
LoaderMax.contentDisplayClass = FlexContentDisplay;
**/ public static var contentDisplayClass:Class; /** @private **/ protected var _loaders:Array; /** @private **/ protected var _activeLoaders:Dictionary; /** If skipFailed is true (the default), any failed loaders in the queue will be skipped. Otherwise, the LoaderMax will stop when it hits a failed loader and the LoaderMax's status will become LoaderStatus.FAILED. Skipped loaders are also ignored when the LoaderMax determines its bytesLoaded, bytesTotal, and progress values. **/ public var skipFailed:Boolean; /** If skipPaused is true (the default), any paused loaders in the queue will be skipped. Otherwise, the LoaderMax will stop when it hits a paused loader and the LoaderMax's status will become LoaderStatus.FAILED. Skipped loaders are also ignored when the LoaderMax determines its bytesLoaded, bytesTotal, and progress values. **/ public var skipPaused:Boolean; /** Maximum number of simultaneous connections that should be used while loading the LoaderMax queue. A higher number will generally result in faster overall load times for the group. The default is 2. This value is instance-based, not system-wide, so if you have two LoaderMax instances that both have a maxConnections value of 3 and they are both loading, there could be up to 6 connections at a time total. **/ public var maxConnections:uint; /** If true, the LoaderMax instance will automatically call load() whenever you insert()/append()/prepend() a new loader whose status is LoaderStatus.READY. This basically makes it easy to create a LoaderMax queue and dump stuff into it whenever you want something to load without having to check the LoaderMax's status and call load() manually if it's not already loading. **/ public var autoLoad:Boolean; /** * Constructor * * @param vars An object containing optional configuration details. For example: new LoaderMax({name:"queue", onComplete:completeHandler, onProgress:progressHandler, maxConnections:3}).

* * The following special properties can be passed into the LoaderMax constructor via the vars parameter * which can be either a generic object or a LoaderMaxVars object:
* * @see com.greensock.loading.data.LoaderMaxVars */ public function LoaderMax(vars:Object=null) { super(vars); _type = "LoaderMax"; _loaders = []; _activeLoaders = new Dictionary(); this.skipFailed = Boolean(this.vars.skipFailed != false); this.skipPaused = Boolean(this.vars.skipPaused != false); this.autoLoad = Boolean(this.vars.autoLoad == true); this.maxConnections = ("maxConnections" in this.vars) ? uint(this.vars.maxConnections) : 2; if (this.vars.loaders is Array) { for (var i:int = 0; i < this.vars.loaders.length; i++) { insert(this.vars.loaders[i], i); } } } /** * Analyzes a url or array of urls and attempts to automatically create the appropriate loader(s) based * on file extension(s) in the url(s), returning either an individual loader like an ImageLoader, * SWFLoader, XMLLoader, etc or if an array is passed in, a LoaderMax will be returned containing * a child for each parsed url (or URLRequest) in the array. Arrays may also contain LoaderCore instances * (not just url Strings). For example:
* @example Single loader example:import com.greensock.loading.~~; import com.greensock.loading.core.~~; import com.greensock.events.LoaderEvent; //activate the necessary loaders so that their file extensions can be recognized (do this once) LoaderMax.activate([ImageLoader, SWFLoader, XMLLoader]); //now parse a url and create the correct type of loader (an ImageLoader in this case because the file extension is ".jpg") var loader:LoaderCore = LoaderMax.parse("../img/photo1.jpg", {name:"parsedLoader", onComplete:completeHandler}); //begin loading loader.load(); function completeHandler(event:LoaderEvent):void { trace("finished loading " + event.target); } * If an array is passed to the LoaderMax.parse() method, it will create a LoaderMax instance * and add the necessary children based on the contents of the array:
* @example Array example:import com.greensock.loading.~~; import com.greensock.events.LoaderEvent; //activate the necessary loaders so that their file extensions can be recognized (do this once) LoaderMax.activate([ImageLoader, SWFLoader, XMLLoader, MP3Loader]); var urls:Array = ["img/photo1.jpg","../../xml/data.xml","swf/main.swf","http://www.greensock.com/audio/music.mp3"]; //now parse all of the urls, creating a LoaderMax that contains the correct type of loaders (an ImageLoader, XMLLoader, SWFLoader, and MP3Loader respectively) var loader:LoaderMax = LoaderMax.parse(urls, {name:"mainQueue", onComplete:completeHandler}) as LoaderMax; //begin loading loader.load(); function completeHandler(event:LoaderEvent):void { trace("finished loading " + loader.numChildren + " loaders."); } * * @param data A String or an array of Strings (and/or LoaderCore instances and/or URLRequest instances) to parse. * @param vars The vars object to pass the loader's constructor. If data is an array, this vars will be passed to the LoaderMax instance that gets created, and no vars object will be passed to the child loaders that get created. * @param childrenVars The vars object that will be passed to each child loader's constructor (only applicable when the data parameter is an array which means parse() will return a LoaderMax). For example, if you parse() and array of video urls and want autoPlay set to false for all of them, you'd do LoaderMax.parse(["1.flv","2.f4v","3.mp4"], null, {autoPlay:false});. * @return If data is an array, parse() will return a LoaderMax. Otherwise, it will return the appropriate loader based on the file extension found in the URL. In any case, the object returned will be a LoaderCore object (all LoaderMax loaders extend LoaderCore, so if you need to datatype your object use com.greensock.loading.core.LoaderCore). The return value is typed as "*" in order to avoid compiler errors when developers forget to cast ther objects like var image:ImageLoader = LoaderMax.parse("photo.jpg") as ImageLoader */ public static function parse(data:*, vars:Object=null, childrenVars:Object=null):* { if (data is Array) { var queue:LoaderMax = new LoaderMax(vars); var l:int = data.length; for (var i:int = 0; i < l; i++) { queue.append(LoaderMax.parse(data[i], childrenVars)); } return queue; } else if (data is String || data is URLRequest) { var s:String = (data is String) ? data : URLRequest(data).url; s = s.toLowerCase().split("?")[0]; s = s.substr(s.lastIndexOf(".") + 1); if (s in _extensions) { return new _extensions[s](data, vars); } } else if (data is LoaderCore) { return data as LoaderCore; } throw new Error("LoaderMax could not parse " + data + ". Don't forget to use LoaderMax.activate() to activate the necessary types of loaders."); return null; } /** @private **/ override protected function _load():void { _loadNext(null); } /** * Appends a loader to the end of the queue. * * @param loader The loader to append to the queue. It can be any loader (ImageLoader, XMLLoader, SWFLoader, MP3Loader, another LoaderMax, etc.). * @return The loader that was appended. * @see #prepend() * @see #insert() * @see #remove() */ public function append(loader:LoaderCore):LoaderCore { return insert(loader, _loaders.length); } /** * Prepends a loader at the beginning of the queue (append() adds the loader to the end whereas prepend() adds it to the beginning). * * @param loader The loader to prepend to the queue. It can be any loader (ImageLoader, XMLLoader, SWFLoader, MP3Loader, another LoaderMax, etc.). * @return The loader that was prepended. * @see #append() * @see #insert() * @see #remove() */ public function prepend(loader:LoaderCore):LoaderCore { return insert(loader, 0); } /** * Inserts a loader at a particular position in the queue. Index values are zero-based just like arrays. * For example, if the LoaderMax has 10 loaders in it already and you want to insert a loader at the 3rd * position (index: 2) while moving the others back in the queue (like the way splice() works * in arrays), you'd do:

* * queue.insert( new ImageLoader("img/photo.jpg"), 2);

* * When a new loader is added to the LoaderMax, the LoaderMax's status changes to LoaderStatus.READY * unless it is paused or disposed. If the loader is already in the queue, it will be removed first. * * @param loader The loader to insert into the queue. It can be any loader (ImageLoader, XMLLoader, SWFLoader, MP3Loader, DataLoader, CSSLoader, another LoaderMax, etc.). * @param index The index position at which the loader should be inserted, exactly like the way splice() works for arrays. Index values are 0-based, so the first position is 0, the second is 1, the third is 2, etc. * @return The loader that was inserted * @see #append() * @see #prepend() * @see #remove() */ public function insert(loader:LoaderCore, index:uint=999999999):LoaderCore { if (loader == null || loader == this || _status == LoaderStatus.DISPOSED) { return null; } if (this != loader.rootLoader) { _removeLoader(loader, false); //in case it was already added. } if (loader.rootLoader == _globalRootLoader) { //don't remove from rootLoaders other than _globalRootLoader, otherwise subloading swfs with loaders that contain LoaderMax instances with nested loaders that have requiredWithRoot set to the associated rootLoader won't be able to be found inside that rootLoader. We could of course leave loaders in _globalRootLoader, but that we get a performance benefit from removing them (fewer event listeners getting called). loader.rootLoader.remove(loader); } if (index > _loaders.length) { index = _loaders.length; } _loaders.splice(index, 0, loader); if (this != _globalRootLoader) { for (var p:String in _listenerTypes) { if (p != "onProgress" && p != "onInit") { loader.addEventListener(_listenerTypes[p], _passThroughEvent, false, -100, true); } } loader.addEventListener(LoaderEvent.PROGRESS, _progressHandler, false, -100, true); //use -1 so that if the user adds an event listener, it gets called before LoaderMax is notified. Otherwise bubbling behavior doesn't go in the proper order. loader.addEventListener("prioritize", _prioritizeHandler, false, -100, true); } loader.addEventListener("dispose", _disposeHandler, false, -100, true); _cacheIsDirty = true; if (_status == LoaderStatus.LOADING) { //do nothing } else if (_status != LoaderStatus.PAUSED) { _status = LoaderStatus.READY; } else if (_prePauseStatus == LoaderStatus.COMPLETED) { _prePauseStatus = LoaderStatus.READY; } if (this.autoLoad && loader.status == LoaderStatus.READY) { if (_status != LoaderStatus.LOADING) { this.load(false); } else { _loadNext(null); //to ensure the maxConnections pipeline is full } } return loader; } /** * Removes a loader from the LoaderMax. * * @param loader The loader to remove from the LoaderMax * @see #append() * @see #insert() * @see #prepend() */ public function remove(loader:LoaderCore):void { _removeLoader(loader, true); } /** @private **/ protected function _removeLoader(loader:LoaderCore, rootLoaderAppend:Boolean):void { if (loader == null) { return; } if (rootLoaderAppend && this != loader.rootLoader) { loader.rootLoader.append(loader); } _removeLoaderListeners(loader, true); _loaders.splice(getChildIndex(loader), 1); if (loader in _activeLoaders) { delete _activeLoaders[loader]; loader.cancel(); if (_status == LoaderStatus.LOADING) { _loadNext(null); } } _cacheIsDirty = true; _progressHandler(null); //has conditional logic that will only dispatch a PROGRESS event if bytesLoaded or bytesTotal has changed. } /** * Empties the LoaderMax of all its loaders and optionally disposes/unloads them. * * @param disposeChildren If true (the default), dispose() will be called on all loaders in the LoaderMax. * @param unloadAllContent If true, the content of all child loaders will be unloaded. * @see #dispose() * @see #unload() */ public function empty(disposeChildren:Boolean=true, unloadAllContent:Boolean=false):void { var i:int = _loaders.length; while (--i > -1) { if (disposeChildren) { LoaderCore(_loaders[i]).dispose(unloadAllContent); } else if (unloadAllContent) { LoaderCore(_loaders[i]).unload(); } else { _removeLoader(_loaders[i], true); } } } /** @private scrubLevel: 0 = cancel, 1 = unload, 2 = dispose, 3 = flush **/ override protected function _dump(scrubLevel:int=0, newStatus:int=0, suppressEvents:Boolean=false):void { if (newStatus == LoaderStatus.DISPOSED) { _status = LoaderStatus.DISPOSED; //must set it first so that when events from children are dispatched, it doesn't trigger other unnecessary actions. empty(true, Boolean(scrubLevel == 3)); if (this.vars.requireWithRoot is DisplayObject) { delete _rootLookup[this.vars.requireWithRoot]; } _activeLoaders = null; } if (scrubLevel <= 1) { _cancelActiveLoaders(); } if (scrubLevel == 1) { var i:int = _loaders.length; while (--i > -1) { LoaderCore(_loaders[i]).unload(); } } super._dump(scrubLevel, newStatus, suppressEvents); _cacheIsDirty = true; } /** @private **/ override protected function _calculateProgress():void { _cachedBytesLoaded = 0; _cachedBytesTotal = 0; var i:int = _loaders.length; var loader:LoaderCore, s:int; while (--i > -1) { loader = _loaders[i]; s = loader.status; if (s <= LoaderStatus.COMPLETED || (!this.skipPaused && s == LoaderStatus.PAUSED) || (!this.skipFailed && s == LoaderStatus.FAILED)) { _cachedBytesLoaded += loader.bytesLoaded; _cachedBytesTotal += loader.bytesTotal; } } _cacheIsDirty = false; } /** @private **/ protected function _cancelActiveLoaders():void { var i:int = _loaders.length; var loader:LoaderCore; while (--i > -1) { loader = _loaders[i]; if (loader.status == LoaderStatus.LOADING) { delete _activeLoaders[loader]; _removeLoaderListeners(loader, false); loader.cancel(); } } } /** @private **/ protected function _removeLoaderListeners(loader:LoaderCore, all:Boolean):void { loader.removeEventListener(LoaderEvent.COMPLETE, _loadNext); loader.removeEventListener(LoaderEvent.CANCEL, _loadNext); if (all) { loader.removeEventListener(LoaderEvent.PROGRESS, _progressHandler); loader.removeEventListener("prioritize", _prioritizeHandler); loader.removeEventListener("dispose", _disposeHandler); for (var p:String in _listenerTypes) { if (p != "onProgress" && p != "onInit") { loader.removeEventListener(_listenerTypes[p], _passThroughEvent); } } } } /** * Returns and array of child loaders that currently have a particular status. For example, * to find all loaders inside the LoaderMax instance that are actively in the process of loading:

* * loader.getChildrenByStatus(LoaderStatus.LOADING, false); * * @param status Status code like LoaderStatus.READY, LoaderStatus.LOADING, LoaderStatus.COMPLETED, LoaderStatus.PAUSED, or LoaderStatus.FAILED. * @param includeNested If true, loaders that are nested inside other loaders (like LoaderMax instances or XMLLoaders or SWFLoaders) will be returned in the array. * @return An array of loaders that match the defined status. * @see #getChildren() * @see #getLoader() * @see #numChildren */ public function getChildrenByStatus(status:int, includeNested:Boolean=false):Array { var a:Array = []; var loaders:Array = getChildren(includeNested, false); var l:int = loaders.length; for (var i:int = 0; i < l; i++) { if (LoaderCore(loaders[i]).status == status) { a.push(loaders[i]); } } return a; } /** * Returns the child that is at a particular position (zero-based index) in the queue. For example, * myLoaderMax.getChildAt(0) would get the first loader in the queue. * myLoaderMax.getChildAt(2) would get the 3rd loader in the queue. * * @param index The index of the child in the queue that should be returned. For example, myLoaderMax.getChildAt(0) would get the first loader in the queue. myLoaderMax.getChildAt(2) would get the 3rd loader. * @return The child loader that is located at the corresponding index * @see #getChildren() * @see #getLoader() * @see #getChildrenByStatus() */ public function getChildAt(index:int):* { return _loaders[index]; } /** * Returns and array of all child loaders inside the LoaderMax, optionally exposing more deeply nested * instances as well (like loaders inside a child LoaderMax instance). * * @param includeNested If true, loaders that are nested inside child LoaderMax, XMLLoader, or SWFLoader instances will be included in the returned array as well. The default is false. * @param omitLoaderMaxes If true, no LoaderMax instances will be returned in the array; only LoaderItems like ImageLoaders, XMLLoaders, SWFLoaders, MP3Loaders, etc. The default is false. * @return An array of loaders. * @see #getChildrenByStatus() * @see #getLoader() * @see #numChildren */ public function getChildren(includeNested:Boolean=false, omitLoaderMaxes:Boolean=false):Array { var a:Array = []; var l:int = _loaders.length; for (var i:int = 0; i < l; i++) { if (!omitLoaderMaxes || !(_loaders[i] is LoaderMax)) { a.push(_loaders[i]); } if (includeNested && _loaders[i].hasOwnProperty("getChildren")) { a = a.concat(_loaders[i].getChildren(true, omitLoaderMaxes)); } } return a; } /** * Immediately prepends a value to the beginning of each child loader's url. For example, * if the "myLoaderMax" instance contains 3 ImageLoaders with the urls "image1.jpg", "image2.jpg", and "image3.jpg" * and you'd like to add "http://www.greensock.com/images/" to the beginning of them all, you'd do:

* * myLoaderMax.prependURLs("http://www.greensock.com/images/", false);

* * Now the ImageLoader urls would be "http://www.greensock.com/images/image1.jpg", "http://www.greensock.com/images/image2.jpg", * and "http://www.greensock.com/images/image3.jpg" respectively.

* * prependURLs() permanently affects each child loader's url meaning that * LoaderMax.getContent("image1.jpg") would not find the loader whose url * is now "http://www.greensock.com/images/image1.jpg" (although you could simply use its name * instead of its url to find it). It also means that if a single loader has been * inserted into multiple LoaderMax instances, its url change affects them all.

* * prependURLs() only affects loaders that are children of the LoaderMax when * the method is called - it does not affect loaders that are inserted later.

* * prependURLs() does NOT affect any alternateURL values that are defined * for each child loader. * * @param value The String that should be prepended to each child loader * @param includeNested If true, loaders nested inside child LoaderMax instances will also be affected. It is false by default. * @see #replaceURLText() */ public function prependURLs(prependText:String, includeNested:Boolean=false):void { var loaders:Array = getChildren(includeNested, true); var i:int = loaders.length; while (--i > -1) { LoaderItem(loaders[i]).url = prependText + LoaderItem(loaders[i]).url; } } /** * Immediately replaces a certain substring in each child loader's url with another string, * making it simple to do something like change "{imageDirectory}image1.jpg" to * "http://www.greensock.com/images/image1.jpg". For example, * if the "myLoaderMax" instance contains 3 ImageLoaders with the urls "{imageDirectory}image1.jpg", * "{imageDirectory}image2.jpg", and "{imageDirectory}image3.jpg" * and you'd like to replace {imageDirectory} with http://www.greensock.com/images/ * you'd do:

* * myLoaderMax.replaceURLText("{imageDirectory}", "http://www.greensock.com/images/", false);

* * Now the ImageLoader urls would be "http://www.greensock.com/images/image1.jpg", "http://www.greensock.com/images/image2.jpg", * and "http://www.greensock.com/images/image3.jpg" respectively.

* * replaceURLText() permanently affects each child loader's url meaning that * LoaderMax.getContent("image1.jpg") would not find the loader whose url * is now "http://www.greensock.com/images/image1.jpg" (although you could simply use its name * instead of its url to find it). It also means that if a single loader has been * inserted into multiple LoaderMax instances, its url change affects them all.

* * replaceURLText() only affects loaders that are children of the LoaderMax when * the method is called - it does not affect loaders that are inserted later.

* * replaceURLText() does affect alternateURL values for child loaders. * * @param fromText The old String that should be replaced in each child loader. * @param toText The new String that should replace the fromText. * @param includeNested If true, loaders nested inside child LoaderMax instances will also be affected. It is false by default. * @see #prependURLs() */ public function replaceURLText(fromText:String, toText:String, includeNested:Boolean=false):void { var loaders:Array = getChildren(includeNested, true); var loader:LoaderItem; var i:int = loaders.length; while (--i > -1) { loader = loaders[i]; loader.url = loader.url.split(fromText).join(toText); if ("alternateURL" in loader.vars) { loader.vars.alternateURL = loader.vars.alternateURL.split(fromText).join(toText); } } } /** * Finds a loader based on its name or url. For example:

* * var loader:ImageLoader = queue.getLoader("myPhoto1");

* * Feel free to use the static LoaderMax.getLoader() method instead of the instance-based getLoader() * method because the static one will search ALL loaders (the only exception being loaders in a different security * sandbox, like in subloaded swfs from a different domain that didn't have a crossdomain.xml file in place granting permission). * * @param nameOrURL The name or url associated with the loader that should be found. * @return The loader associated with the name or url. * @see #getContent() * @see #getChildren() * @see #getChildrenByStatus() */ public function getLoader(nameOrURL:String):* { var i:int = _loaders.length; var loader:LoaderCore; while (--i > -1) { loader = _loaders[i]; if (loader.name == nameOrURL || (loader is LoaderItem && (loader as LoaderItem).url == nameOrURL)) { return loader; } else if (loader.hasOwnProperty("getLoader")) { loader = (loader as Object).getLoader(nameOrURL) as LoaderCore; if (loader != null) { return loader; } } } return null; } /** * Finds the content of a loader based on its name or url. For example:

* * var image:Bitmap = queue.getContent("myPhoto1");

* * Feel free to use the static LoaderMax.getContent() method instead of the instance-based getContent() * method because the static one will search ALL loaders (the only exception being loaders in a different security * sandbox, like in subloaded swfs from a different domain that didn't have a crossdomain.xml file in place granting permission). * * @param nameOrURL The name or url associated with the loader whose content should be found. * @return The content that was loaded by the loader which varies by the type of loader: * * @see #getLoader() * @see #content */ public function getContent(nameOrURL:String):* { var loader:LoaderCore = this.getLoader(nameOrURL); return (loader != null) ? loader.content : null; } /** * Finds the index position of a particular loader in the LoaderMax. Index values are always zero-based, * meaning the first position is 0, the second is 1, the third is 2, etc. * * @param loader The loader whose index position should be returned * @return The index position of the loader * @see #getChildren() * @see #getChildrenByStatus() */ public function getChildIndex(loader:LoaderCore):uint { var i:int = _loaders.length; while (--i > -1) { if (_loaders[i] == loader) { return i; } } return 999999999; } /** @inheritDoc **/ override public function auditSize():void { if (!this.auditedSize) { _auditSize(null); } } /** @private **/ protected function _auditSize(event:Event=null):void { if (event != null) { event.target.removeEventListener("auditedSize", _auditSize); event.target.removeEventListener(LoaderEvent.FAIL, _auditSize); } var l:uint = _loaders.length; var maxStatus:int = (this.skipPaused) ? LoaderStatus.COMPLETED : LoaderStatus.PAUSED; var loader:LoaderCore, found:Boolean; for (var i:int = 0; i < l; i++) { loader = _loaders[i]; if (!loader.auditedSize && loader.status <= maxStatus) { if (!found) { loader.addEventListener("auditedSize", _auditSize, false, -100, true); loader.addEventListener(LoaderEvent.FAIL, _auditSize, false, -100, true); } found = true; loader.auditSize(); } } if (!found) { if (_status == LoaderStatus.LOADING) { _loadNext(null); } dispatchEvent(new Event("auditedSize")); } } //---- EVENT HANDLERS ------------------------------------------------------------------------------------ /** @private **/ protected function _loadNext(event:Event=null):void { if (event != null && _activeLoaders != null) { delete _activeLoaders[event.target]; _removeLoaderListeners(LoaderCore(event.target), false); } if (_status == LoaderStatus.LOADING) { var audit:Boolean = ("auditSize" in this.vars) ? Boolean(this.vars.auditSize) : LoaderMax.defaultAuditSize; if (audit && !this.auditedSize) { _auditSize(null); return; } var loader:LoaderCore, loaders:Array = _loaders.concat(), l:int = loaders.length, activeCount:uint = 0; //use _loaders.concat() because in some rare situations, a loader's COMPLETE event might occur immediately and if autoDispose is true, the length of the array could change mid-loop causing a skip. _calculateProgress(); for (var i:int = 0; i < l; i++) { loader = loaders[i]; if (!this.skipPaused && loader.status == LoaderStatus.PAUSED) { super._failHandler(new LoaderEvent(LoaderEvent.FAIL, this, "Did not complete LoaderMax because skipPaused was false and " + loader.toString() + " was paused."), false); return; } else if (!this.skipFailed && loader.status == LoaderStatus.FAILED) { super._failHandler(new LoaderEvent(LoaderEvent.FAIL, this, "Did not complete LoaderMax because skipFailed was false and " + loader.toString() + " failed."), false); return; } else if (loader.status <= LoaderStatus.LOADING) { activeCount++; if (!(loader in _activeLoaders)) { _activeLoaders[loader] = true; loader.addEventListener(LoaderEvent.COMPLETE, _loadNext, false, -100, true); loader.addEventListener(LoaderEvent.CANCEL, _loadNext, false, -100, true); loader.load(false); } if (activeCount == this.maxConnections) { break; } } } if (activeCount == 0 && _cachedBytesLoaded == _cachedBytesTotal) { _completeHandler(null); } } } /** @private **/ override protected function _progressHandler(event:Event):void { if (_dispatchChildProgress && event != null) { dispatchEvent(new LoaderEvent(LoaderEvent.CHILD_PROGRESS, event.target)); } if (_dispatchProgress && _status != LoaderStatus.DISPOSED) { var bl:uint = _cachedBytesLoaded; var bt:uint = _cachedBytesTotal; _calculateProgress(); if (bl == 0 && _cachedBytesLoaded == 0) { //do nothing } else if ((_cachedBytesLoaded != _cachedBytesTotal || _status != LoaderStatus.LOADING) && (bl != _cachedBytesLoaded || bt != _cachedBytesTotal)) { //note: added _status != LoaderStatus.LOADING because it's possible for all the children to load independently (without the LoaderMax actively loading), so in those cases, the progress would never reach 1 since LoaderMax's _completeHandler() won't be called to dispatch the final PROGRESS event. dispatchEvent(new LoaderEvent(LoaderEvent.PROGRESS, this)); } } else { _cacheIsDirty = true; } } /** @private **/ protected function _disposeHandler(event:Event):void { _removeLoader(LoaderCore(event.target), false); } /** @private **/ protected function _prioritizeHandler(event:Event):void { var loader:LoaderCore = event.target as LoaderCore; _loaders.splice(getChildIndex(loader), 1); _loaders.unshift(loader); if (_status == LoaderStatus.LOADING && loader.status <= LoaderStatus.LOADING && !(loader in _activeLoaders)) { _cancelActiveLoaders(); var prevMaxConnections:uint = this.maxConnections; this.maxConnections = 1; _loadNext(null); this.maxConnections = prevMaxConnections; } } //---- STATIC METHODS ---------------------------------------------------------------------------- /** * Activates particular loader classes (like ImageLoader, SWFLoader, etc.) so that they can be * recognized inside the parse() method and XMLLoader. For example, if LoaderMax.parse("image.jpg") * is called without first activating ImageLoader (like LoaderMax.activate([ImageLoader])), * it wouldn't properly recognize the ".jpg" extension and return the necessary ImageLoader instance. Likewise, * without activating ImageLoader first, XMLLoader wouldn't be able to recognize <ImageLoader> * nodes nested inside an XML file. You only need to activate() the loader classes once in your swf. * For example:

* * LoaderMax.activate([ImageLoader, SWFLoader, MP3Loader, DataLoader, CSSLoader]);

* * The reason all loaders aren't activated by default is to conserve file size.

* * @param loaderClasses An array of loader classes, like [ImageLoader, SWFLoader, MP3Loader]. */ public static function activate(loaderClasses:Array):void { //no need to do anything - we just want to force the classes to get compiled in the swf. Each one calls the _activateClass() method in LoaderCore on its own. } /** * By default, LoaderMax associates certain file extensions with certain types of loaders, like "jpg", "png", and "gif" * are associated with ImageLoader and "swf" is associated with SWFLoader so that the LoaderMax.parse() method * can recognize and create the appropriate loaders for each URL passed in. If you'd like to associate additional file * extensions with certain loader types, you may do so with registerFileType(). For example, to associate * "pdf" with BinaryDataLoader, you would do this:

* * LoaderMax.registerFileType("pdf", BinaryDataLoader);

* * Then, if you call LoaderMax.parse("file/myFile.pdf"), it would recognize the "pdf" file extension * as being associated with BinaryDataLoader and would return a BinaryDataLoader instance accordingly.

* * There is no reason to use registerFileType() unless you plan on utilizing the parse() * method and need it to recognize a extensions that LoaderMax doesn't already recognize by default. * * NOTE: Make sure you activate() the various loader types you want LoaderMax to recognize before calling parse() - see the documentation for LoaderMax.activate()) * * @param extensions The extension (or comma-delimited list of extensions) that should be associated with the loader class, like "zip" or "zip,pdf". Do not include the dot in the extension. * @param loaderClass The loader class that should be associated with the extension(s), like BinaryDataLoader. * @see #activate() */ public static function registerFileType(extensions:String, loaderClass:Class):void { _activateClass("", loaderClass, extensions); } /** * Searches ALL loaders to find one based on its name or url. For example:

* * var loader:ImageLoader = LoaderMax.getLoader("myPhoto1") as ImageLoader;

* * @param nameOrURL The name or url associated with the loader that should be found. * @return The loader associated with the name or url. */ public static function getLoader(nameOrURL:String):* { return (_globalRootLoader != null) ? _globalRootLoader.getLoader(nameOrURL) : null; } /** * Searches ALL loaders to find content based on its name or url. For example:

* * var image:Bitmap = LoaderMax.getContent("myPhoto1");

* * @param nameOrURL The name or url associated with the loader whose content should be found. * @return The content that was loaded by the loader which varies by the type of loader: * */ public static function getContent(nameOrURL:String):* { return (_globalRootLoader != null) ? _globalRootLoader.getContent(nameOrURL) : null; } /** * Immediately prioritizes a loader inside any LoaderMax instances that contain it, * forcing it to the top position in their queue and optionally calls load() * immediately as well. If one of its parent LoaderMax instances is currently loading a * different loader, that one will be temporarily cancelled.

* * By contrast, when load() is called, it doesn't change the loader's position/index * in any LoaderMax queues. For example, if a LoaderMax is working on loading the first object in * its queue, you can call load() on the 20th item and it will honor your request without * changing its index in the queue. prioritize(), however, affects the position * in the queue and optionally loads it immediately as well.

* * So even if your LoaderMax hasn't begun loading yet, you could prioritize(false) * a loader and it will rise to the top of all LoaderMax instances to which it belongs, but not * start loading yet. If the goal is to load something immediately, you can just use the * load() method.

* * For example, to immediately prioritize the loader named "myPhoto1":

* * LoaderMax.prioritize("myPhoto1"); * * @param nameOrURL The name or url associated with the loader that should be prioritized * @param loadNow If true (the default), the loader will start loading immediately (otherwise it is simply placed at the top the queue in any LoaderMax instances to which it belongs). * @return The loader that was prioritized. If no loader was found, null is returned. */ public static function prioritize(nameOrURL:String, loadNow:Boolean=true):LoaderCore { var loader:LoaderCore = getLoader(nameOrURL); if (loader != null) { loader.prioritize(loadNow); } return loader; } //---- GETTERS / SETTERS ------------------------------------------------------------------------- /** Number of child loaders currently contained in the LoaderMax instance (does not include deeply nested loaders - only children). To get the quantity of all children including nested ones, use getChildren(true, true).length @see #getChildren() **/ public function get numChildren():uint { return _loaders.length; } /** An array containing the content of each loader inside the LoaderMax **/ override public function get content():* { var a:Array = []; var i:int = _loaders.length; while (--i > -1) { a[i] = LoaderCore(_loaders[i]).content; } return a; } /** @inheritDoc **/ override public function get status():int { //if the status of children changed after the LoaderMax completed, we need to make adjustments to the LoaderMax's status. if (_status == LoaderStatus.COMPLETED) { var statusCounts:Array = [0, 0, 0, 0, 0, 0]; //store the counts of each type of status (index 0 is for READY, 1 is LOADING, 2 is COMPLETE, etc. var i:int = _loaders.length; while (--i > -1) { statusCounts[LoaderCore(_loaders[i]).status]++; } if ((!this.skipFailed && statusCounts[4] != 0) || (!this.skipPaused && statusCounts[3] != 0)) { _status = LoaderStatus.FAILED; } else if (statusCounts[0] + statusCounts[1] != 0) { _status = LoaderStatus.READY; _cacheIsDirty = true; } } return _status; } /** @inheritDoc **/ override public function get auditedSize():Boolean { var maxStatus:int = (this.skipPaused) ? LoaderStatus.COMPLETED : LoaderStatus.PAUSED; var i:int = _loaders.length; while (--i > -1) { if (!LoaderCore(_loaders[i]).auditedSize && LoaderCore(_loaders[i]).status <= maxStatus) { return false; } } return true; } /** * An unweighted value between 0 and 1 indicating the overall loading progress of the LoaderMax - this calculation does not concern * itself whatsoever with bytesLoaded and bytesTotal but rather the ratio of the children that are loaded * (all having equal weight). Therefore, rawProgress is a more crude way of measuring the overall loading progress and * isn't weighted in terms of file size the way that progress is. The only benefit of using rawProgress instead * of progress is that there is never a risk of the value moving backwards the way it can with progress * when child loaders have inaccurately low estimatedByte values (before LoaderMax audits the file size values). The rate at which * rawProgress increases may slow down or speed up depending on the file size of the asset currently loading. For example, * if a LoaderMax contains two loaders, the first for a file that's 100k and the second for a file that's 10,000k, rawProgress * will move quickly (while loading the 100k file) until it reaches 0.5 and then slow down significantly (while loading the 10,000k file) * until it reaches 1.

* * Or let's say you have a LoaderMax that contains 3 ImageLoaders: the first two must load images that are 25k each and the * 3rd one must load an image that's 450k. After the first two ImageLoaders finish, the LoaderMax's progress property would * report 0.1 (50k loaded out of 500k total) whereas the rawProgress would report 0.66 (2 loaders out of 3 total have completed). * However, if you set the estimatedBytes of all of the ImageLoaders in this example to 25600 (25k) and set the LoaderMax's * auditSize to false, the progress would read about 0.66 after the first two ImageLoaders complete * (it still thinks they're all 25k) and then when the 3rd one starts loading and LoaderMax finds out that it's 450k, the bytesTotal * would automatically adjust and the progress would jump backwards to 0.1 (which correctly reflects the weighted progress). * Of course a solution would be to more accurately set the estimatedBytes and/or leave auditSize true in the * LoaderMax, but rawProgress can be useful if those solutions are undesirable in your scenario and you need to avoid any * backwards adjustment of a preloader progress bar or some other interface element.

* * @see #progress **/ public function get rawProgress():Number { var loaded:Number = 0; var total:uint = 0; var status:int; var i:int = _loaders.length; while (--i > -1) { status = LoaderCore(_loaders[i]).status; if (status != LoaderStatus.DISPOSED && !(status == LoaderStatus.PAUSED && this.skipPaused) && !(status == LoaderStatus.FAILED && this.skipFailed)) { total++; loaded += (_loaders[i] is LoaderMax) ? LoaderMax(_loaders[i]).rawProgress : LoaderCore(_loaders[i]).progress; } } return (total == 0) ? 0 : loaded / total; } } }