Browse Source

Loaders: Convert to ES6. (#21622)

Michael Herzog 4 years ago
parent
commit
2a2a8a55a9

+ 6 - 8
src/loaders/CompressedTextureLoader.js

@@ -9,17 +9,15 @@ import { Loader } from './Loader.js';
  * Sub classes have to implement the parse() method which will be used in load().
  */
 
-function CompressedTextureLoader( manager ) {
+class CompressedTextureLoader extends Loader {
 
-	Loader.call( this, manager );
+	constructor( manager ) {
 
-}
-
-CompressedTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		super( manager );
 
-	constructor: CompressedTextureLoader,
+	}
 
-	load: function ( url, onLoad, onProgress, onError ) {
+	load( url, onLoad, onProgress, onError ) {
 
 		const scope = this;
 
@@ -130,7 +128,7 @@ CompressedTextureLoader.prototype = Object.assign( Object.create( Loader.prototy
 
 	}
 
-} );
+}
 
 
 export { CompressedTextureLoader };

+ 6 - 8
src/loaders/DataTextureLoader.js

@@ -9,17 +9,15 @@ import { Loader } from './Loader.js';
  * Sub classes have to implement the parse() method which will be used in load().
  */
 
-function DataTextureLoader( manager ) {
+class DataTextureLoader extends Loader {
 
-	Loader.call( this, manager );
+	constructor( manager ) {
 
-}
-
-DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		super( manager );
 
-	constructor: DataTextureLoader,
+	}
 
-	load: function ( url, onLoad, onProgress, onError ) {
+	load( url, onLoad, onProgress, onError ) {
 
 		const scope = this;
 
@@ -110,7 +108,7 @@ DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ),
 
 	}
 
-} );
+}
 
 
 export { DataTextureLoader };

+ 10 - 12
src/loaders/FileLoader.js

@@ -3,17 +3,15 @@ import { Loader } from './Loader.js';
 
 const loading = {};
 
-function FileLoader( manager ) {
+class FileLoader extends Loader {
 
-	Loader.call( this, manager );
+	constructor( manager ) {
 
-}
-
-FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		super( manager );
 
-	constructor: FileLoader,
+	}
 
-	load: function ( url, onLoad, onProgress, onError ) {
+	load( url, onLoad, onProgress, onError ) {
 
 		if ( url === undefined ) url = '';
 
@@ -277,23 +275,23 @@ FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
 		return request;
 
-	},
+	}
 
-	setResponseType: function ( value ) {
+	setResponseType( value ) {
 
 		this.responseType = value;
 		return this;
 
-	},
+	}
 
-	setMimeType: function ( value ) {
+	setMimeType( value ) {
 
 		this.mimeType = value;
 		return this;
 
 	}
 
-} );
+}
 
 
 export { FileLoader };

+ 17 - 19
src/loaders/ImageBitmapLoader.js

@@ -1,41 +1,37 @@
 import { Cache } from './Cache.js';
 import { Loader } from './Loader.js';
 
-function ImageBitmapLoader( manager ) {
+class ImageBitmapLoader extends Loader {
 
-	if ( typeof createImageBitmap === 'undefined' ) {
+	constructor( manager ) {
 
-		console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
+		super( manager );
 
-	}
-
-	if ( typeof fetch === 'undefined' ) {
-
-		console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
+		if ( typeof createImageBitmap === 'undefined' ) {
 
-	}
+			console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
 
-	Loader.call( this, manager );
+		}
 
-	this.options = { premultiplyAlpha: 'none' };
+		if ( typeof fetch === 'undefined' ) {
 
-}
+			console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
 
-ImageBitmapLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		}
 
-	constructor: ImageBitmapLoader,
+		this.options = { premultiplyAlpha: 'none' };
 
-	isImageBitmapLoader: true,
+	}
 
-	setOptions: function setOptions( options ) {
+	setOptions( options ) {
 
 		this.options = options;
 
 		return this;
 
-	},
+	}
 
-	load: function ( url, onLoad, onProgress, onError ) {
+	load( url, onLoad, onProgress, onError ) {
 
 		if ( url === undefined ) url = '';
 
@@ -96,6 +92,8 @@ ImageBitmapLoader.prototype = Object.assign( Object.create( Loader.prototype ),
 
 	}
 
-} );
+}
+
+ImageBitmapLoader.prototype.isImageBitmapLoader = true;
 
 export { ImageBitmapLoader };

+ 23 - 23
src/loaders/Loader.js

@@ -1,22 +1,22 @@
 import { DefaultLoadingManager } from './LoadingManager.js';
 
-function Loader( manager ) {
+class Loader {
 
-	this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
+	constructor( manager ) {
 
-	this.crossOrigin = 'anonymous';
-	this.withCredentials = false;
-	this.path = '';
-	this.resourcePath = '';
-	this.requestHeader = {};
+		this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
 
-}
+		this.crossOrigin = 'anonymous';
+		this.withCredentials = false;
+		this.path = '';
+		this.resourcePath = '';
+		this.requestHeader = {};
 
-Object.assign( Loader.prototype, {
+	}
 
-	load: function ( /* url, onLoad, onProgress, onError */ ) {},
+	load( /* url, onLoad, onProgress, onError */ ) {}
 
-	loadAsync: function ( url, onProgress ) {
+	loadAsync( url, onProgress ) {
 
 		const scope = this;
 
@@ -26,45 +26,45 @@ Object.assign( Loader.prototype, {
 
 		} );
 
-	},
+	}
 
-	parse: function ( /* data */ ) {},
+	parse( /* data */ ) {}
 
-	setCrossOrigin: function ( crossOrigin ) {
+	setCrossOrigin( crossOrigin ) {
 
 		this.crossOrigin = crossOrigin;
 		return this;
 
-	},
+	}
 
-	setWithCredentials: function ( value ) {
+	setWithCredentials( value ) {
 
 		this.withCredentials = value;
 		return this;
 
-	},
+	}
 
-	setPath: function ( path ) {
+	setPath( path ) {
 
 		this.path = path;
 		return this;
 
-	},
+	}
 
-	setResourcePath: function ( resourcePath ) {
+	setResourcePath( resourcePath ) {
 
 		this.resourcePath = resourcePath;
 		return this;
 
-	},
+	}
 
-	setRequestHeader: function ( requestHeader ) {
+	setRequestHeader( requestHeader ) {
 
 		this.requestHeader = requestHeader;
 		return this;
 
 	}
 
-} );
+}
 
 export { Loader };

+ 5 - 5
src/loaders/LoaderUtils.js

@@ -1,6 +1,6 @@
-const LoaderUtils = {
+class LoaderUtils {
 
-	decodeText: function ( array ) {
+	static decodeText( array ) {
 
 		if ( typeof TextDecoder !== 'undefined' ) {
 
@@ -32,9 +32,9 @@ const LoaderUtils = {
 
 		}
 
-	},
+	}
 
-	extractUrlBase: function ( url ) {
+	static extractUrlBase( url ) {
 
 		const index = url.lastIndexOf( '/' );
 
@@ -44,6 +44,6 @@ const LoaderUtils = {
 
 	}
 
-};
+}
 
 export { LoaderUtils };

+ 72 - 69
src/loaders/LoadingManager.js

@@ -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 };

+ 6 - 8
src/loaders/TextureLoader.js

@@ -3,17 +3,15 @@ import { ImageLoader } from './ImageLoader.js';
 import { Texture } from '../textures/Texture.js';
 import { Loader } from './Loader.js';
 
-function TextureLoader( manager ) {
+class TextureLoader extends Loader {
 
-	Loader.call( this, manager );
+	constructor( manager ) {
 
-}
-
-TextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
+		super( manager );
 
-	constructor: TextureLoader,
+	}
 
-	load: function ( url, onLoad, onProgress, onError ) {
+	load( url, onLoad, onProgress, onError ) {
 
 		const texture = new Texture();
 
@@ -43,7 +41,7 @@ TextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
 
 	}
 
-} );
+}
 
 
 export { TextureLoader };