1
0
Fork 0

Loosen coupling of FileLoader and window.FileReader

master
Mike Gerwitz 2015-12-21 00:47:39 -05:00
parent 887d450955
commit 2240a61ac2
2 changed files with 12 additions and 7 deletions

View File

@ -24,8 +24,11 @@
/* jshint browser:true */ /* jshint browser:true */
/* global lasertank */ /* global lasertank */
const load_ltg = lasertank.FileLoader( document.getElementById( 'ltg' ) ), const ltg_input = document.getElementById( 'ltg' ),
load_lvl = lasertank.FileLoader( document.getElementById( 'lvl' ) ), 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' ), ele_game = document.getElementById( 'game' ),
ctx = document.getElementById( 'render' ).getContext( '2d' ); ctx = document.getElementById( 'render' ).getContext( '2d' );

View File

@ -49,8 +49,9 @@ module.exports = Class( 'FileLoader',
* Initialize file loader, monitoring the given file element * Initialize file loader, monitoring the given file element
* *
* @param {HtmlInputElement} element file element to monitor * @param {HtmlInputElement} element file element to monitor
* @param {FileReader} reader file reader
*/ */
__construct: function( element ) __construct: function( element, reader )
{ {
if ( element.type !== 'file' ) if ( element.type !== 'file' )
{ {
@ -58,6 +59,7 @@ module.exports = Class( 'FileLoader',
} }
this._element = element; this._element = element;
this._reader = reader;
}, },
@ -105,18 +107,18 @@ module.exports = Class( 'FileLoader',
if ( files.length === 0 ) return; if ( files.length === 0 ) return;
const reader = new FileReader(); this._reader.onload = function( revent )
reader.onload = function( revent )
{ {
_self._callback.call( this.__inst, null, revent.target.result ); _self._callback.call( this.__inst, null, revent.target.result );
}; };
reader.onerror = function( e )
this._reader.onerror = function( e )
{ {
_self._callback.call( this.__inst, e ); _self._callback.call( this.__inst, e );
}; };
// load file // load file
reader.readAsBinaryString( files[ 0 ] ); this._reader.readAsBinaryString( files[ 0 ] );
} }
} ); } );