|
@@ -1,139 +1,142 @@
|
|
|
-function LoadingManager( onLoad, onProgress, onError ) {
|
|
|
+class LoadingManager {
|
|
|
|
|
|
- const scope = this;
|
|
|
+ constructor( onLoad, onProgress, onError ) {
|
|
|
|
|
|
- let isLoading = false;
|
|
|
- let itemsLoaded = 0;
|
|
|
- let itemsTotal = 0;
|
|
|
- let urlModifier = undefined;
|
|
|
- const handlers = [];
|
|
|
+ const scope = this;
|
|
|
|
|
|
- // Refer to #5689 for the reason why we don't set .onStart
|
|
|
- // in the constructor
|
|
|
+ let isLoading = false;
|
|
|
+ let itemsLoaded = 0;
|
|
|
+ let itemsTotal = 0;
|
|
|
+ let urlModifier = undefined;
|
|
|
+ const handlers = [];
|
|
|
|
|
|
- this.onStart = undefined;
|
|
|
- this.onLoad = onLoad;
|
|
|
- this.onProgress = onProgress;
|
|
|
- this.onError = onError;
|
|
|
+ // Refer to #5689 for the reason why we don't set .onStart
|
|
|
+ // in the constructor
|
|
|
|
|
|
- this.itemStart = function ( url ) {
|
|
|
+ this.onStart = undefined;
|
|
|
+ this.onLoad = onLoad;
|
|
|
+ this.onProgress = onProgress;
|
|
|
+ this.onError = onError;
|
|
|
|
|
|
- itemsTotal ++;
|
|
|
+ this.itemStart = function ( url ) {
|
|
|
|
|
|
- if ( isLoading === false ) {
|
|
|
+ itemsTotal ++;
|
|
|
|
|
|
- if ( scope.onStart !== undefined ) {
|
|
|
+ if ( isLoading === false ) {
|
|
|
|
|
|
- scope.onStart( url, itemsLoaded, itemsTotal );
|
|
|
+ if ( scope.onStart !== undefined ) {
|
|
|
+
|
|
|
+ scope.onStart( url, itemsLoaded, itemsTotal );
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
+ isLoading = true;
|
|
|
|
|
|
- isLoading = true;
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
+ this.itemEnd = function ( url ) {
|
|
|
|
|
|
- this.itemEnd = function ( url ) {
|
|
|
+ itemsLoaded ++;
|
|
|
|
|
|
- itemsLoaded ++;
|
|
|
+ if ( scope.onProgress !== undefined ) {
|
|
|
|
|
|
- if ( scope.onProgress !== undefined ) {
|
|
|
+ scope.onProgress( url, itemsLoaded, itemsTotal );
|
|
|
|
|
|
- scope.onProgress( url, itemsLoaded, itemsTotal );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ if ( itemsLoaded === itemsTotal ) {
|
|
|
|
|
|
- if ( itemsLoaded === itemsTotal ) {
|
|
|
+ isLoading = false;
|
|
|
|
|
|
- isLoading = false;
|
|
|
+ if ( scope.onLoad !== undefined ) {
|
|
|
|
|
|
- if ( scope.onLoad !== undefined ) {
|
|
|
+ scope.onLoad();
|
|
|
|
|
|
- scope.onLoad();
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
+ this.itemError = function ( url ) {
|
|
|
|
|
|
- this.itemError = function ( url ) {
|
|
|
+ if ( scope.onError !== undefined ) {
|
|
|
|
|
|
- if ( scope.onError !== undefined ) {
|
|
|
+ scope.onError( url );
|
|
|
|
|
|
- scope.onError( url );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
+ this.resolveURL = function ( url ) {
|
|
|
|
|
|
- this.resolveURL = function ( url ) {
|
|
|
+ if ( urlModifier ) {
|
|
|
|
|
|
- if ( urlModifier ) {
|
|
|
+ return urlModifier( url );
|
|
|
|
|
|
- return urlModifier( url );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ return url;
|
|
|
|
|
|
- return url;
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
+ this.setURLModifier = function ( transform ) {
|
|
|
|
|
|
- this.setURLModifier = function ( transform ) {
|
|
|
+ urlModifier = transform;
|
|
|
|
|
|
- urlModifier = transform;
|
|
|
+ return this;
|
|
|
|
|
|
- return this;
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
+ this.addHandler = function ( regex, loader ) {
|
|
|
|
|
|
- this.addHandler = function ( regex, loader ) {
|
|
|
+ handlers.push( regex, loader );
|
|
|
|
|
|
- handlers.push( regex, loader );
|
|
|
+ return this;
|
|
|
|
|
|
- return this;
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
+ this.removeHandler = function ( regex ) {
|
|
|
|
|
|
- this.removeHandler = function ( regex ) {
|
|
|
+ const index = handlers.indexOf( regex );
|
|
|
|
|
|
- const index = handlers.indexOf( regex );
|
|
|
+ if ( index !== - 1 ) {
|
|
|
|
|
|
- if ( index !== - 1 ) {
|
|
|
+ handlers.splice( index, 2 );
|
|
|
|
|
|
- handlers.splice( index, 2 );
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ return this;
|
|
|
|
|
|
- return this;
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
+ this.getHandler = function ( file ) {
|
|
|
|
|
|
- this.getHandler = function ( file ) {
|
|
|
+ for ( let i = 0, l = handlers.length; i < l; i += 2 ) {
|
|
|
|
|
|
- for ( let i = 0, l = handlers.length; i < l; i += 2 ) {
|
|
|
+ const regex = handlers[ i ];
|
|
|
+ const loader = handlers[ i + 1 ];
|
|
|
|
|
|
- const regex = handlers[ i ];
|
|
|
- const loader = handlers[ i + 1 ];
|
|
|
+ if ( regex.global ) regex.lastIndex = 0; // see #17920
|
|
|
|
|
|
- if ( regex.global ) regex.lastIndex = 0; // see #17920
|
|
|
+ if ( regex.test( file ) ) {
|
|
|
|
|
|
- if ( regex.test( file ) ) {
|
|
|
+ return loader;
|
|
|
|
|
|
- return loader;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
+ return null;
|
|
|
|
|
|
- return null;
|
|
|
+ };
|
|
|
|
|
|
- };
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
|
|
|
const DefaultLoadingManager = new LoadingManager();
|
|
|
|
|
|
-
|
|
|
export { DefaultLoadingManager, LoadingManager };
|