Loosen coupling of FileLoader and window.FileReader
parent
887d450955
commit
2240a61ac2
|
@ -24,8 +24,11 @@
|
|||
/* jshint browser:true */
|
||||
/* global lasertank */
|
||||
|
||||
const load_ltg = lasertank.FileLoader( document.getElementById( 'ltg' ) ),
|
||||
load_lvl = lasertank.FileLoader( document.getElementById( 'lvl' ) ),
|
||||
const ltg_input = document.getElementById( 'ltg' ),
|
||||
lvl_input = document.getElementById( 'lvl' ),
|
||||
|
||||
load_ltg = lasertank.FileLoader( ltg_input, new window.FileReader() ),
|
||||
load_lvl = lasertank.FileLoader( lvl_input, new window.FileReader() ),
|
||||
|
||||
ele_game = document.getElementById( 'game' ),
|
||||
ctx = document.getElementById( 'render' ).getContext( '2d' );
|
||||
|
|
|
@ -49,8 +49,9 @@ module.exports = Class( 'FileLoader',
|
|||
* Initialize file loader, monitoring the given file element
|
||||
*
|
||||
* @param {HtmlInputElement} element file element to monitor
|
||||
* @param {FileReader} reader file reader
|
||||
*/
|
||||
__construct: function( element )
|
||||
__construct: function( element, reader )
|
||||
{
|
||||
if ( element.type !== 'file' )
|
||||
{
|
||||
|
@ -58,6 +59,7 @@ module.exports = Class( 'FileLoader',
|
|||
}
|
||||
|
||||
this._element = element;
|
||||
this._reader = reader;
|
||||
},
|
||||
|
||||
|
||||
|
@ -105,18 +107,18 @@ module.exports = Class( 'FileLoader',
|
|||
|
||||
if ( files.length === 0 ) return;
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function( revent )
|
||||
this._reader.onload = function( revent )
|
||||
{
|
||||
_self._callback.call( this.__inst, null, revent.target.result );
|
||||
};
|
||||
reader.onerror = function( e )
|
||||
|
||||
this._reader.onerror = function( e )
|
||||
{
|
||||
_self._callback.call( this.__inst, e );
|
||||
};
|
||||
|
||||
// load file
|
||||
reader.readAsBinaryString( files[ 0 ] );
|
||||
this._reader.readAsBinaryString( files[ 0 ] );
|
||||
}
|
||||
} );
|
||||
|
||||
|
|
Loading…
Reference in New Issue