2018-02-12 16:01:20 +00:00
/ * *
* @ author Richard Davey < rich @ photonstorm . com >
2019-01-15 16:20:22 +00:00
* @ copyright 2019 Photon Storm Ltd .
2018-02-12 16:01:20 +00:00
* @ license { @ link https : //github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
* /
2016-11-30 00:18:34 +00:00
var MergeXHRSettings = require ( './MergeXHRSettings' ) ;
2018-02-08 17:00:14 +00:00
/ * *
* Creates a new XMLHttpRequest ( xhr ) object based on the given File and XHRSettings
* and starts the download of it . It uses the Files own XHRSettings and merges them
* with the global XHRSettings object to set the xhr values before download .
*
* @ function Phaser . Loader . XHRLoader
* @ since 3.0 . 0
*
* @ param { Phaser . Loader . File } file - The File to download .
2018-03-21 12:03:14 +00:00
* @ param { XHRSettingsObject } globalXHRSettings - The global XHRSettings object .
2018-02-08 17:00:14 +00:00
*
* @ return { XMLHttpRequest } The XHR object .
* /
2016-11-30 00:18:34 +00:00
var XHRLoader = function ( file , globalXHRSettings )
{
2016-12-01 12:50:58 +00:00
var config = MergeXHRSettings ( globalXHRSettings , file . xhrSettings ) ;
2016-11-30 00:18:34 +00:00
var xhr = new XMLHttpRequest ( ) ;
xhr . open ( 'GET' , file . src , config . async , config . user , config . password ) ;
2016-12-01 12:50:58 +00:00
xhr . responseType = file . xhrSettings . responseType ;
2016-11-30 00:18:34 +00:00
xhr . timeout = config . timeout ;
if ( config . header && config . headerValue )
{
xhr . setRequestHeader ( config . header , config . headerValue ) ;
}
2018-05-04 16:33:48 +00:00
if ( config . requestedWith )
{
xhr . setRequestHeader ( 'X-Requested-With' , config . requestedWith ) ;
}
2016-11-30 00:18:34 +00:00
if ( config . overrideMimeType )
{
xhr . overrideMimeType ( config . overrideMimeType ) ;
}
// After a successful request, the xhr.response property will contain the requested data as a DOMString, ArrayBuffer, Blob, or Document (depending on what was set for responseType.)
2018-05-04 16:33:48 +00:00
xhr . onload = file . onLoad . bind ( file , xhr ) ;
2016-12-01 12:50:58 +00:00
xhr . onerror = file . onError . bind ( file ) ;
xhr . onprogress = file . onProgress . bind ( file ) ;
2016-11-30 17:16:45 +00:00
// This is the only standard method, the ones above are browser additions (maybe not universal?)
// xhr.onreadystatechange
2016-11-30 00:18:34 +00:00
xhr . send ( ) ;
return xhr ;
} ;
module . exports = XHRLoader ;