-
Notifications
You must be signed in to change notification settings - Fork 0
/
preloadjs.js
37 lines (37 loc) · 13.8 KB
/
preloadjs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* PreloadJS
* Visit http://createjs.com/ for documentation, updates and examples.
*
* Copyright (c) 2011 gskinner.com, inc.
*
* Distributed under the terms of the MIT license.
* http://www.opensource.org/licenses/mit-license.html
*
* This notice shall be included in all copies or substantial portions of the Software.
**/(function(g){var c=function(){this.init()};c.prototype={};var a=c.prototype;a.loaded=false;a.progress=0;a._item=null;a.onProgress=null;a.onLoadStart=null;a.onFileLoad=null;a.onFileProgress=null;a.onComplete=null;a.onError=null;a.getItem=function(){return this._item};a.init=function(){};a.load=function(){};a.cancel=function(){};a._sendLoadStart=function(){if(this.onLoadStart)this.onLoadStart({target:this})};a._sendProgress=function(a){var f;if(a instanceof Number)this.progress=a,f={loaded:this.progress,
total:1};else if(f=a,this.progress=a.loaded/a.total,isNaN(this.progress)||this.progress==Infinity)this.progress=0;f.target=this;if(this.onProgress)this.onProgress(f)};a._sendFileProgress=function(a){if(this.onFileProgress)a.target=this,this.onFileProgress(a)};a._sendComplete=function(){if(this.onComplete)this.onComplete({target:this})};a._sendFileComplete=function(a){if(this.onFileLoad)a.target=this,this.onFileLoad(a)};a._sendError=function(a){if(this.onError)a==null&&(a={}),a.target=this,this.onError(a)};
a.toString=function(){return"[PreloadJS AbstractLoader]"};g.AbstractLoader=c})(window);(function(g){function c(){}var a=function(a){this.initialize(a)},b=a.prototype=new AbstractLoader;a.IMAGE="image";a.SOUND="sound";a.JSON="json";a.JAVASCRIPT="javascript";a.CSS="css";a.XML="xml";a.TEXT="text";a.TIMEOUT_TIME=8E3;b.useXHR=true;b.stopOnError=false;b.maintainScriptOrder=true;b.next=null;b.typeHandlers=null;b.extensionHandlers=null;b._maxConnections=1;b._currentLoads=null;b._loadQueue=null;b._loadedItemsById=null;b._loadedItemsBySrc=null;b._targetProgress=0;b._numItems=0;b._numItemsLoaded=
null;b._scriptOrder=null;b._loadedScripts=null;b.TAG_LOAD_OGGS=true;b.initialize=function(a){this._targetProgress=this._numItemsLoaded=this._numItems=0;this._paused=false;this._currentLoads=[];this._loadQueue=[];this._scriptOrder=[];this._loadedScripts=[];this._loadedItemsById={};this._loadedItemsBySrc={};this.typeHandlers={};this.extensionHandlers={};this.useXHR=a!=false&&g.XMLHttpRequest!=null;this.determineCapabilities()};b.determineCapabilities=function(){var f=a.lib.BrowserDetect;if(f!=null)a.TAG_LOAD_OGGS=
f.isFirefox||f.isOpera};a.isBinary=function(f){switch(f){case a.IMAGE:case a.SOUND:return true;default:return false}};b.installPlugin=function(a){if(!(a==null||a.getPreloadHandlers==null)){a=a.getPreloadHandlers();if(a.types!=null)for(var b=0,d=a.types.length;b<d;b++)this.typeHandlers[a.types[b]]=a.callback;if(a.extensions!=null)for(b=0,d=a.extensions.length;b<d;b++)this.extensionHandlers[a.extensions[b]]=a.callback}};b.setMaxConnections=function(a){this._maxConnections=a;this._paused||this._loadNext()};
b.loadFile=function(a,b){this._addItem(a);b!==false&&this.setPaused(false)};b.loadManifest=function(a,b){var d;a instanceof Array?d=a:a instanceof Object&&(d=[a]);for(var c=0,g=d.length;c<g;c++)this._addItem(d[c],false);b!==false&&this._loadNext()};b.load=function(){this.setPaused(false)};b.getResult=function(a){return this._loadedItemsById[a]||this._loadedItemsBySrc[a]};b.setPaused=function(a){(this._paused=a)||this._loadNext()};b.close=function(){for(;this._currentLoads.length;)this._currentLoads.pop().cancel();
this._currentLoads=[];this._scriptOrder=[];this._loadedScripts=[]};b._addItem=function(f){f=this._createLoadItem(f);f!=null&&(this._loadQueue.push(f),this._numItems++,this._updateProgress(),f.getItem().type==a.JAVASCRIPT&&(this._scriptOrder.push(f.getItem()),this._loadedScripts.push(null)))};b._loadNext=function(){if(!this._paused){this._loadQueue.length==0&&(this._sendComplete(),this.next&&this.next.load&&this.next.load.apply(this.next));for(;this._loadQueue.length&&this._currentLoads.length<this._maxConnections;){var f=
this._loadQueue.shift();f.onProgress=a.proxy(this._handleProgress,this);f.onComplete=a.proxy(this._handleFileComplete,this);f.onError=a.proxy(this._handleFileError,this);this._currentLoads.push(f);f.load()}}};b._handleFileError=function(a){var a=a.target,b=this._createResultData(a.getItem());this._numItemsLoaded++;this._updateProgress();this._sendError(b);this.stopOnError||(this._removeLoadItem(a),this._loadNext())};b._createResultData=function(a){return{id:a.id,result:null,data:a.data,type:a.type,
src:a.src}};b._handleFileComplete=function(b){this._numItemsLoaded++;var b=b.target,e=b.getItem();this._removeLoadItem(b);var d=this._createResultData(e);d.result=b instanceof a.lib.XHRLoader?this._createResult(e,b.getResult()):e.tag;this._loadedItemsById[e.id]=d;this._loadedItemsBySrc[e.src]=d;var c=this;switch(e.type){case a.IMAGE:if(b instanceof a.lib.XHRLoader){d.result.onload=function(){c._handleFileTagComplete(e,d)};return}break;case a.JAVASCRIPT:if(this.maintainScriptOrder){this._loadedScripts[this._scriptOrder.indexOf(e)]=
e;this._checkScriptLoadOrder();return}}this._handleFileTagComplete(e,d)};b._checkScriptLoadOrder=function(){for(var a=this._loadedScripts.length,b=0;b<a;b++){var d=this._loadedScripts[b];if(d===null)break;if(d!==true){var d=this.getResult(d.src),c=this._createResultData(d);c.result=d.result;this._handleFileTagComplete(d,c);this._loadedScripts[b]=true;b--;a--}}};b._handleFileTagComplete=function(a,b){a.completeHandler&&a.completeHandler(b);this._updateProgress();this._sendFileComplete(b);this._loadNext()};
b._removeLoadItem=function(a){for(var b=this._currentLoads.length,d=0;d<b;d++)if(this._currentLoads[d]==a){this._currentLoads.splice(d,1);break}};b._createResult=function(b,e){var d=null,c;switch(b.type){case a.IMAGE:d=this._createImage();break;case a.SOUND:d=b.tag||this._createAudio();break;case a.CSS:d=this._createLink();break;case a.JAVASCRIPT:d=this._createScript();break;case a.XML:if(g.DOMParser){var h=new DOMParser;h.parseFromString(e,"text/xml")}else h=new ActiveXObject("Microsoft.XMLDOM"),
h.async=false,h.loadXML(e),c=h;break;case a.JSON:case a.TEXT:c=e}return d?(b.type==a.CSS?d.href=b.src:d.src=b.src,d):c};b._handleProgress=function(a){var a=a.target,b=this._createResultData(a.getItem());b.progress=a.progress;this._sendFileProgress(b);this._updateProgress()};b._updateProgress=function(){var a=this._numItemsLoaded/this._numItems,b=this._numItems-this._numItemsLoaded;if(b>0){for(var d=0,c=0,g=this._currentLoads.length;c<g;c++)d+=this._currentLoads[c].progress;a+=d/b*(b/this._numItems)}this._sendProgress({loaded:a,
total:1})};b._createLoadItem=function(b){var e={};switch(typeof b){case "string":e.src=b;break;case "object":b instanceof HTMLAudioElement?(e.tag=b,e.src=e.tag.src,e.type=a.SOUND):e=b}e.ext=this._getNameAfter(e.src,".");if(!e.type)e.type=this.getType(e.ext);if(e.id==null||e.id=="")e.id=e.src;if(b=this.typeHandlers[e.type]||this.extensionHandlers[e.ext]){b=b(e.src,e.type,e.id,e.data);if(b===false)return null;else if(b!==true){if(b.src!=null)e.src=b.src;if(b.id!=null)e.id=b.id;if(b.tag!=null&&b.tag.load instanceof
Function)e.tag=b.tag}e.ext=this._getNameAfter(e.src,".")}b=this.useXHR;switch(e.type){case a.JSON:case a.XML:case a.TEXT:b=true;break;case a.SOUND:e.ext=="ogg"&&a.TAG_LOAD_OGGS&&(b=false)}if(b)return new a.lib.XHRLoader(e);else if(e.tag)return new a.lib.TagLoader(e);else{var d,b="src",c=false;switch(e.type){case a.IMAGE:d=this._createImage();break;case a.SOUND:d=this._createAudio();break;case a.CSS:b="href";c=true;d=this._createLink();break;case a.JAVASCRIPT:c=true,d=this._createScript()}e.tag=d;
return new a.lib.TagLoader(e,b,c)}};b.getType=function(b){switch(b){case "jpeg":case "jpg":case "gif":case "png":return a.IMAGE;case "ogg":case "mp3":case "wav":return a.SOUND;case "json":return a.JSON;case "xml":return a.XML;case "css":return a.CSS;case "js":return a.JAVASCRIPT;default:return a.TEXT}};b._getNameAfter=function(a,b){var d=a.lastIndexOf(b),d=a.substr(d+1),c=d.lastIndexOf(/[\b|\?|\#|\s]/);return c==-1?d:d.substr(0,c)};b._createImage=function(){return document.createElement("img")};b._createAudio=
function(){var a=document.createElement("audio");a.autoplay=false;a.type="audio/ogg";return a};b._createScript=function(){var a=document.createElement("script");a.type="text/javascript";return a};b._createLink=function(){var a=document.createElement("link");a.type="text/css";a.rel="stylesheet";return a};b.toString=function(){return"[PreloadJS]"};a.proxy=function(a,b){return function(c){return a.apply(b,arguments)}};a.lib={};g.PreloadJS=a;c.init=function(){var a=navigator.userAgent;c.isFirefox=a.indexOf("Firefox")>
-1;c.isOpera=g.opera!=null;c.isIOS=a.indexOf("iPod")>-1||a.indexOf("iPhone")>-1||a.indexOf("iPad")>-1};c.init();a.lib.BrowserDetect=c})(window);(function(){var g=function(a,b,c){this.init(a,b,c)},c=g.prototype=new AbstractLoader;c._srcAttr=null;c._loadTimeOutTimeout=null;c.tagCompleteProxy=null;c.init=function(a,b,c){this._item=a;this._srcAttr=b||"src";this._useXHR=c==true;this.isAudio=a.tag instanceof HTMLAudioElement;this.tagCompleteProxy=PreloadJS.proxy(this._handleTagLoad,this)};c.cancel=function(){this._clean();var a=this.getItem();if(a!=null)a.src=null};c.load=function(){this._useXHR?this.loadXHR():this.loadTag()};c.loadXHR=function(){var a=
this.getItem(),a=new PreloadJS.lib.XHRLoader(a);a.onProgress=PreloadJS.proxy(this._handleProgress,this);a.onFileLoad=PreloadJS.proxy(this._handleXHRComplete,this);a.onFileError=PreloadJS.proxy(this._handleLoadError,this);a.load()};c._handleXHRComplete=function(a){this._clean();var b=a.getItem();a.getResult();b.type==PreloadJS.IMAGE?(b.tag.onload=PreloadJS.proxy(this._sendComplete,this),b.tag.src=b.src):(b.tag[this._srcAttr]=b.src,this._sendComplete())};c._handleLoadError=function(a){this._clean();
this._sendError(a)};c.loadTag=function(){var a=this.getItem(),b=a.tag;clearTimeout(this._loadTimeOutTimeout);this._loadTimeOutTimeout=setTimeout(PreloadJS.proxy(this._handleLoadTimeOut,this),PreloadJS.TIMEOUT_TIME);if(this.isAudio)b.src=null,b.preload="auto",b.setAttribute("data-temp","true");b.onerror=PreloadJS.proxy(this._handleLoadError,this);b.onprogress=PreloadJS.proxy(this._handleProgress,this);this.isAudio?(b.onstalled=PreloadJS.proxy(this._handleStalled,this),b.addEventListener("canplaythrough",
this.tagCompleteProxy)):b.onload=PreloadJS.proxy(this._handleTagLoad,this);b[this._srcAttr]=a.src;a=a.type==PreloadJS.SOUND&&a.ext=="ogg"&&PreloadJS.lib.BrowserDetect.isFirefox;b.load!=null&&!a&&b.load()};c._handleLoadTimeOut=function(){this._clean();this._sendError()};c._handleStalled=function(){};c._handleLoadError=function(){this._clean();this._sendError()};c._handleTagLoad=function(){var a=this.getItem().tag;clearTimeout(this._loadTimeOutTimeout);if(!(this.isAudio&&a.readyState!==4)&&!this.loaded)this.loaded=
true,this._clean(),this._sendComplete()};c._clean=function(){clearTimeout(this._loadTimeOutTimeout);var a=this.getItem().tag;a.onload=null;a.removeEventListener("canplaythrough",this.tagCompleteProxy);a.onstalled=null;a.onprogress=null;a.onerror=null};c._handleProgress=function(a){clearTimeout(this._loadTimeOutTimeout);if(this.isAudio){a=this.getItem();if(a.buffered==null)return;a={loaded:a.buffered.length>0?a.buffered.end(0):0,total:a.duration}}this._sendProgress(a)};c.toString=function(){return"[PreloadJS TagLoader]"};
PreloadJS.lib.TagLoader=g})(window);(function(g){var c=function(a){this.init(a)},a=c.prototype=new AbstractLoader;a._wasLoaded=false;a._request=null;a._loadTimeOutTimeout=null;a._xhrLevel=null;a.init=function(a){this._item=a;this._createXHR(a)};a.getResult=function(){try{return this._request.responseText}catch(a){}return this._request.response};a.cancel=function(){this._clean();this._request.abort()};a.load=function(){if(this._request==null)this.handleError();else{if(this._xhrLevel==1)this._loadTimeOutTimeout=setTimeout(PreloadJS.proxy(this.handleTimeout,
this),PreloadJS.TIMEOUT_TIME);this._request.onloadstart=PreloadJS.proxy(this.handleLoadStart,this);this._request.onprogress=PreloadJS.proxy(this.handleProgress,this);this._request.onabort=PreloadJS.proxy(this.handleAbort,this);this._request.onerror=PreloadJS.proxy(this.handleError,this);this._request.ontimeout=PreloadJS.proxy(this.handleTimeout,this);this._request.onload=PreloadJS.proxy(this.handleLoad,this);this._request.onreadystatechange=PreloadJS.proxy(this.handleReadyStateChange,this);try{this._request.send()}catch(a){}}};
a.handleProgress=function(a){a.loaded>0&&a.total==0||this._sendProgress({loaded:a.loaded,total:a.total})};a.handleLoadStart=function(){clearTimeout(this._loadTimeOutTimeout);this._sendLoadStart()};a.handleAbort=function(){this._clean();this._sendError()};a.handleError=function(){this._clean();this._sendError()};a.handleReadyStateChange=function(){this._request.readyState==4&&this.handleLoad()};a._checkError=function(){switch(parseInt(this._request.status)){case 404:case 0:return false}if(this._request.response==
null){try{if(this._request.responseXML!=null)return true}catch(a){}return false}return true};a.handleLoad=function(){if(!this.loaded)this.loaded=true,this._checkError()?(this._clean(),this._sendComplete()):this.handleError()};a.handleTimeout=function(){this._clean();this._sendError()};a.handleLoadEnd=function(){this._clean()};a._createXHR=function(a){this._xhrLevel=1;if(g.ArrayBuffer)this._xhrLevel=2;if(g.XMLHttpRequest)this._request=new XMLHttpRequest;else try{this._request=new ActiveXObject("MSXML2.XMLHTTP.3.0")}catch(c){return null}a.type==
PreloadJS.TEXT&&this._request.overrideMimeType("text/plain; charset=x-user-defined");this._request.open("GET",a.src,true);if(PreloadJS.isBinary(a.type))this._request.responseType="arraybuffer";return true};a._clean=function(){clearTimeout(this._loadTimeOutTimeout);var a=this._request;a.onloadstart=null;a.onprogress=null;a.onabort=null;a.onerror=null;a.onload=null;a.ontimeout=null;a.onloadend=null;a.onreadystatechange=null;clearInterval(this._checkLoadInterval)};a.toString=function(){return"[PreloadJS XHRLoader]"};
PreloadJS.lib.XHRLoader=c})(window);