2017-07-03 15:05:22 +00:00
|
|
|
var Class = require('../utils/Class');
|
2017-07-03 16:06:40 +00:00
|
|
|
var GetFastValue = require('../utils/object/GetFastValue');
|
2016-12-06 15:15:42 +00:00
|
|
|
var CONST = require('./const');
|
2017-07-03 15:05:22 +00:00
|
|
|
var GetURL = require('./GetURL');
|
|
|
|
var MergeXHRSettings = require('./MergeXHRSettings');
|
2016-12-01 12:50:58 +00:00
|
|
|
var XHRLoader = require('./XHRLoader');
|
|
|
|
var XHRSettings = require('./XHRSettings');
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
// Phaser.Loader.File
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
var File = new Class({
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
initialize:
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 16:06:40 +00:00
|
|
|
// old signature: type, key, url, responseType, xhrSettings, config
|
|
|
|
function File (fileConfig)
|
2017-07-03 15:05:22 +00:00
|
|
|
{
|
|
|
|
// file type (image, json, etc) for sorting within the Loader
|
2017-07-03 16:06:40 +00:00
|
|
|
this.type = GetFastValue(fileConfig, 'type', false);
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
// unique cache key (unique within its file type)
|
2017-07-03 16:06:40 +00:00
|
|
|
this.key = GetFastValue(fileConfig, 'key', false);
|
|
|
|
|
|
|
|
if (!this.type || !this.key)
|
|
|
|
{
|
|
|
|
throw new Error('Error calling \'Loader.' + this.type + '\' invalid key provided.');
|
|
|
|
}
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
// The URL of the file, not including baseURL
|
2017-07-05 12:36:45 +00:00
|
|
|
this.url = GetFastValue(fileConfig, 'url');
|
2017-07-03 16:06:40 +00:00
|
|
|
|
2017-07-05 12:36:45 +00:00
|
|
|
if (this.url === undefined)
|
2017-07-03 16:06:40 +00:00
|
|
|
{
|
2017-07-05 12:36:45 +00:00
|
|
|
this.url = GetFastValue(fileConfig, 'path', '') + this.key + '.' + GetFastValue(fileConfig, 'extension', '');
|
2017-07-03 16:06:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.url = GetFastValue(fileConfig, 'path', '').concat(this.url);
|
|
|
|
}
|
2017-07-03 15:05:22 +00:00
|
|
|
|
|
|
|
// Set when the Loader calls 'load' on this file
|
|
|
|
this.src = '';
|
2016-12-12 22:27:53 +00:00
|
|
|
|
2017-07-03 16:06:40 +00:00
|
|
|
this.xhrSettings = XHRSettings(GetFastValue(fileConfig, 'responseType', undefined));
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 16:06:40 +00:00
|
|
|
if (GetFastValue(fileConfig, 'xhrSettings', false))
|
2017-07-03 15:05:22 +00:00
|
|
|
{
|
2017-07-03 16:06:40 +00:00
|
|
|
this.xhrSettings = MergeXHRSettings(this.xhrSettings, GetFastValue(fileConfig, 'xhrSettings', {}));
|
2017-07-03 15:05:22 +00:00
|
|
|
}
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
this.xhrLoader = null;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
this.state = CONST.FILE_PENDING;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
// Set by onProgress (only if loading via XHR)
|
|
|
|
this.bytesTotal = 0;
|
|
|
|
this.bytesLoaded = -1;
|
|
|
|
this.percentComplete = -1;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
// For CORs based loading.
|
|
|
|
// If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set)
|
|
|
|
this.crossOrigin = undefined;
|
2017-02-04 05:36:06 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
// The actual processed file data
|
|
|
|
this.data = undefined;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
// A config object that can be used by file types to store transitional data
|
2017-07-03 16:06:40 +00:00
|
|
|
this.config = GetFastValue(fileConfig, 'config', {});
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
// Multipart file? (i.e. an atlas and its json together)
|
|
|
|
this.linkFile = undefined;
|
|
|
|
this.linkType = '';
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
this.callback = null;
|
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
resetXHR: function ()
|
|
|
|
{
|
|
|
|
this.xhrLoader.onload = undefined;
|
|
|
|
this.xhrLoader.onerror = undefined;
|
|
|
|
this.xhrLoader.onprogress = undefined;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Called when the Image loads
|
|
|
|
// ProgressEvent
|
|
|
|
onLoad: function (event)
|
|
|
|
{
|
|
|
|
this.resetXHR();
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.callback(this, true);
|
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
onError: function (event)
|
|
|
|
{
|
|
|
|
this.resetXHR();
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.callback(this, false);
|
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
onProgress: function (event)
|
|
|
|
{
|
2016-12-07 01:13:17 +00:00
|
|
|
if (event.lengthComputable)
|
|
|
|
{
|
|
|
|
this.bytesLoaded = event.loaded;
|
|
|
|
this.bytesTotal = event.total;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-07 01:13:17 +00:00
|
|
|
this.percentComplete = Math.min((this.bytesLoaded / this.bytesTotal), 1);
|
|
|
|
}
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-07 01:13:17 +00:00
|
|
|
// console.log(this.percentComplete + '% (' + this.bytesLoaded + ' bytes)');
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-07 00:27:56 +00:00
|
|
|
onProcess: function (callback)
|
2016-12-01 12:50:58 +00:00
|
|
|
{
|
2016-12-07 00:27:56 +00:00
|
|
|
this.state = CONST.FILE_PROCESSING;
|
|
|
|
|
|
|
|
this.onComplete();
|
2016-12-08 16:21:16 +00:00
|
|
|
|
|
|
|
callback(this);
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
onComplete: function ()
|
|
|
|
{
|
2016-12-07 04:43:02 +00:00
|
|
|
if (this.linkFile)
|
|
|
|
{
|
|
|
|
if (this.linkFile.state === CONST.FILE_WAITING_LINKFILE)
|
|
|
|
{
|
|
|
|
// The linkfile has finished processing, and is waiting for this file, so let's do them both
|
|
|
|
this.state = CONST.FILE_COMPLETE;
|
|
|
|
this.linkFile.state = CONST.FILE_COMPLETE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The linkfile still hasn't finished loading and/or processing yet
|
|
|
|
this.state = CONST.FILE_WAITING_LINKFILE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.state = CONST.FILE_COMPLETE;
|
|
|
|
}
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
// Called by the Loader, starts the actual file downloading
|
|
|
|
load: function (callback, baseURL, globalXHR)
|
|
|
|
{
|
|
|
|
if (baseURL === undefined) { baseURL = ''; }
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.callback = callback;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-01 12:50:58 +00:00
|
|
|
this.src = GetURL(this, baseURL);
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2016-12-08 16:21:16 +00:00
|
|
|
if (this.src.indexOf('data:') === 0)
|
|
|
|
{
|
|
|
|
console.log('Local data URI');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.xhrLoader = XHRLoader(this, globalXHR);
|
|
|
|
}
|
2016-12-01 12:50:58 +00:00
|
|
|
}
|
2017-07-03 15:05:22 +00:00
|
|
|
|
|
|
|
});
|
2016-11-30 01:24:33 +00:00
|
|
|
|
|
|
|
module.exports = File;
|