2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @copyright 2018 Photon Storm Ltd.
|
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-07-03 15:05:22 +00:00
|
|
|
var Class = require('../utils/Class');
|
2016-12-06 15:15:42 +00:00
|
|
|
var CONST = require('./const');
|
2018-01-26 14:23:00 +00:00
|
|
|
var GetFastValue = require('../utils/object/GetFastValue');
|
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
|
|
|
|
2018-03-19 21:57:46 +00:00
|
|
|
/**
|
|
|
|
* @callback FileProcessCallback
|
|
|
|
*
|
|
|
|
* @param {Phaser.Loader.File} file - [description]
|
|
|
|
*/
|
|
|
|
|
2018-03-21 12:03:14 +00:00
|
|
|
/**
|
|
|
|
* @typedef {object} FileConfig
|
|
|
|
*
|
|
|
|
* @property {(string|false)} [type=false] - The file type string (image, json, etc) for sorting within the Loader.
|
|
|
|
* @property {(string|false)} [key=false] - Unique cache key (unique within its file type)
|
|
|
|
* @property {string} [url] - The URL of the file, not including baseURL.
|
|
|
|
* @property {string} [path=''] - [description]
|
|
|
|
* @property {string} [extension=''] - [description]
|
|
|
|
* @property {XMLHttpRequestResponseType} [responseType] - [description]
|
|
|
|
* @property {(XHRSettingsObject|false)} [xhrSettings=false] - [description]
|
|
|
|
* @property {object} [config] - A config object that can be used by file types to store transitional data.
|
|
|
|
*/
|
|
|
|
|
2018-02-07 15:27:21 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class File
|
|
|
|
* @memberOf Phaser.Loader
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-04-25 16:13:06 +00:00
|
|
|
* @param {Phaser.Loader.LoaderPlugin} loader - The Loader that is going to load this File.
|
2018-03-21 12:03:14 +00:00
|
|
|
* @param {FileConfig} fileConfig - [description]
|
2018-02-07 15:27:21 +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
|
|
|
|
2018-04-25 16:13:06 +00:00
|
|
|
function File (loader, fileConfig)
|
2017-07-03 15:05:22 +00:00
|
|
|
{
|
2018-04-25 16:13:06 +00:00
|
|
|
/**
|
|
|
|
* A reference to the Loader that is going to load this file.
|
|
|
|
*
|
|
|
|
* @name Phaser.Loader.File#loader
|
|
|
|
* @type {Phaser.Loader.LoaderPlugin}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
this.loader = loader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A reference to the Cache, or Texture Manager, that is going to store this file if it loads.
|
|
|
|
*
|
|
|
|
* @name Phaser.Loader.File#cache
|
|
|
|
* @type {(Phaser.Cache.BaseCache|Phaser.Textures.TextureManager)}
|
|
|
|
* @since 3.7.0
|
|
|
|
*/
|
2018-04-25 22:16:17 +00:00
|
|
|
this.cache = GetFastValue(fileConfig, 'cache', false);
|
2018-04-25 16:13:06 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* The file type string (image, json, etc) for sorting within the Loader.
|
2018-03-19 21:57:46 +00:00
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#type
|
|
|
|
* @type {string}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 16:06:40 +00:00
|
|
|
this.type = GetFastValue(fileConfig, 'type', false);
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* Unique cache key (unique within its file type)
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#key
|
|
|
|
* @type {string}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-05-02 12:11:17 +00:00
|
|
|
this.key = loader.prefix + GetFastValue(fileConfig, 'key', false);
|
2017-07-03 16:06:40 +00:00
|
|
|
|
|
|
|
if (!this.type || !this.key)
|
|
|
|
{
|
|
|
|
throw new Error('Error calling \'Loader.' + this.type + '\' invalid key provided.');
|
|
|
|
}
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* The URL of the file, not including baseURL.
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#url
|
|
|
|
* @type {string}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
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
|
|
|
{
|
2018-05-02 12:11:17 +00:00
|
|
|
this.url = loader.path + this.key + '.' + GetFastValue(fileConfig, 'extension', '');
|
2017-07-03 16:06:40 +00:00
|
|
|
}
|
2018-02-15 17:11:55 +00:00
|
|
|
else if (typeof(this.url) !== 'function')
|
2017-07-03 16:06:40 +00:00
|
|
|
{
|
2018-05-02 12:11:17 +00:00
|
|
|
this.url = loader.path + this.url;
|
2017-07-03 16:06:40 +00:00
|
|
|
}
|
2017-07-03 15:05:22 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* Set when the Loader calls 'load' on this file.
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#src
|
|
|
|
* @type {string}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 15:05:22 +00:00
|
|
|
this.src = '';
|
2016-12-12 22:27:53 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* The merged XHRSettings for this file.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#xhrSettings
|
2018-03-21 12:03:14 +00:00
|
|
|
* @type {XHRSettingsObject}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
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
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-03-28 14:04:09 +00:00
|
|
|
* The XMLHttpRequest instance (as created by XHR Loader) that is loading this File.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#xhrLoader
|
2018-03-28 14:04:09 +00:00
|
|
|
* @type {?XMLHttpRequest}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 15:05:22 +00:00
|
|
|
this.xhrLoader = null;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* The current state of the file. One of the FILE_CONST values.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#state
|
|
|
|
* @type {integer}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-03-05 01:40:11 +00:00
|
|
|
this.state = typeof(this.url) === 'function' ? CONST.FILE_POPULATED : CONST.FILE_PENDING;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* The total size of this file.
|
|
|
|
* Set by onProgress and only if loading via XHR.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#bytesTotal
|
|
|
|
* @type {number}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @default 0
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 15:05:22 +00:00
|
|
|
this.bytesTotal = 0;
|
2018-01-26 14:23:00 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* Updated as the file loads.
|
|
|
|
* Only set if loading via XHR.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#bytesLoaded
|
|
|
|
* @type {number}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @default -1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 15:05:22 +00:00
|
|
|
this.bytesLoaded = -1;
|
2018-01-26 14:23:00 +00:00
|
|
|
|
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* A percentage value between 0 and 1 indicating how much of this file has loaded.
|
|
|
|
* Only set if loading via XHR.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#percentComplete
|
|
|
|
* @type {float}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @default -1
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 15:05:22 +00:00
|
|
|
this.percentComplete = -1;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* For CORs based loading.
|
|
|
|
* If this is undefined then the File will check BaseLoader.crossOrigin and use that (if set)
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#crossOrigin
|
2018-03-20 15:12:42 +00:00
|
|
|
* @type {(string|undefined)}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 15:05:22 +00:00
|
|
|
this.crossOrigin = undefined;
|
2017-02-04 05:36:06 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* The processed file data, stored in here after the file has loaded.
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#data
|
2018-03-20 16:15:49 +00:00
|
|
|
* @type {*}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 15:05:22 +00:00
|
|
|
this.data = undefined;
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* A config object that can be used by file types to store transitional data.
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#config
|
|
|
|
* @type {object}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-07-03 16:06:40 +00:00
|
|
|
this.config = GetFastValue(fileConfig, 'config', {});
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* If this is a multipart file, i.e. an atlas and its json together, then this is a reference
|
|
|
|
* to the linked file. Set and used internally by the Loader.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @name Phaser.Loader.File#linkFile
|
2018-04-27 17:44:12 +00:00
|
|
|
* @type {?Phaser.Loader.LinkFile}
|
2018-01-26 14:23:00 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-27 17:44:12 +00:00
|
|
|
this.linkFile;
|
2017-07-03 15:05:22 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* Resets the XHRLoader instance.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#resetXHR
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-01 12:50:58 +00:00
|
|
|
resetXHR: function ()
|
|
|
|
{
|
2018-03-16 17:00:45 +00:00
|
|
|
if (this.xhrLoader)
|
|
|
|
{
|
|
|
|
this.xhrLoader.onload = undefined;
|
|
|
|
this.xhrLoader.onerror = undefined;
|
|
|
|
this.xhrLoader.onprogress = undefined;
|
|
|
|
}
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
|
|
|
* Called by the Loader, starts the actual file downloading.
|
|
|
|
* During the load the methods onLoad, onProgress, etc are called based on the XHR events.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#load
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-04-25 16:13:06 +00:00
|
|
|
load: function ()
|
2018-01-19 19:13:02 +00:00
|
|
|
{
|
|
|
|
if (this.state === CONST.FILE_POPULATED)
|
|
|
|
{
|
|
|
|
this.onComplete();
|
|
|
|
|
2018-04-25 16:13:06 +00:00
|
|
|
this.loader.nextFile(this);
|
2018-01-19 19:13:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-25 16:13:06 +00:00
|
|
|
this.src = GetURL(this, this.loader.baseURL);
|
2018-01-19 19:13:02 +00:00
|
|
|
|
|
|
|
if (this.src.indexOf('data:') === 0)
|
|
|
|
{
|
2018-02-22 17:28:02 +00:00
|
|
|
console.warn('Local data URIs are not supported: ' + this.key);
|
2018-01-19 19:13:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-25 16:13:06 +00:00
|
|
|
this.xhrLoader = XHRLoader(this, this.loader.xhr);
|
2018-01-19 19:13:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* Called when the file finishes loading, is sent a DOM ProgressEvent.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#onLoad
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @param {ProgressEvent} event - The DOM ProgressEvent that resulted from this load.
|
2018-01-26 14:23:00 +00:00
|
|
|
*/
|
2016-12-01 12:50:58 +00:00
|
|
|
onLoad: function (event)
|
|
|
|
{
|
|
|
|
this.resetXHR();
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-19 17:29:14 +00:00
|
|
|
if (event.target && event.target.status !== 200)
|
|
|
|
{
|
2018-01-19 19:13:02 +00:00
|
|
|
this.loader.nextFile(this, false);
|
2018-01-19 17:29:14 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-01-19 19:13:02 +00:00
|
|
|
this.loader.nextFile(this, true);
|
2018-01-19 17:29:14 +00:00
|
|
|
}
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* Called if the file errors while loading, is sent a DOM ProgressEvent.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#onError
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @param {ProgressEvent} event - The DOM ProgressEvent that resulted from this error.
|
2018-01-26 14:23:00 +00:00
|
|
|
*/
|
2018-02-16 19:08:50 +00:00
|
|
|
onError: function ()
|
2016-12-01 12:50:58 +00:00
|
|
|
{
|
|
|
|
this.resetXHR();
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-19 19:13:02 +00:00
|
|
|
this.loader.nextFile(this, false);
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* Called during the file load progress. Is sent a DOM ProgressEvent.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#onProgress
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @param {ProgressEvent} event - The DOM ProgressEvent.
|
2018-01-26 14:23:00 +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
|
|
|
|
2018-01-19 19:13:02 +00:00
|
|
|
// console.log(this.percentComplete + '% (' + this.bytesLoaded + ' bytes)');
|
|
|
|
this.loader.emit('fileprogress', this, this.percentComplete);
|
|
|
|
}
|
2016-12-01 12:50:58 +00:00
|
|
|
},
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-02-08 17:00:14 +00:00
|
|
|
* Usually overridden by the FileTypes and is called by Loader.finishedLoading.
|
2018-01-26 14:23:00 +00:00
|
|
|
* The callback is Loader.processUpdate
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#onProcess
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 21:57:46 +00:00
|
|
|
* @param {FileProcessCallback} callback - The callback to invoke to process this File.
|
2018-01-26 14:23:00 +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
|
|
|
|
2018-01-26 14:23:00 +00:00
|
|
|
/**
|
2018-04-27 17:44:12 +00:00
|
|
|
* Called when the File has completed loading.
|
2018-02-08 17:00:14 +00:00
|
|
|
* Checks on the state of its linkfile, if set.
|
2018-01-26 14:23:00 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#onComplete
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2016-12-01 12:50:58 +00:00
|
|
|
onComplete: function ()
|
|
|
|
{
|
2018-04-27 17:44:12 +00:00
|
|
|
this.state = CONST.FILE_COMPLETE;
|
|
|
|
|
2016-12-07 04:43:02 +00:00
|
|
|
if (this.linkFile)
|
|
|
|
{
|
2018-04-27 17:44:12 +00:00
|
|
|
this.linkFile.onFileComplete(this);
|
2016-12-07 04:43:02 +00:00
|
|
|
}
|
2018-04-25 16:13:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a key matching the one used by this file exists in the target Cache or not.
|
|
|
|
* This is called automatically by the LoaderPlugin to decide if the file can be safely
|
|
|
|
* loaded or will conflict.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#hasCacheConflict
|
|
|
|
* @since 3.7.0
|
|
|
|
*
|
|
|
|
* @return {boolean} `true` if adding this file will cause a conflict, otherwise `false`.
|
|
|
|
*/
|
|
|
|
hasCacheConflict: function ()
|
|
|
|
{
|
2018-04-25 22:16:17 +00:00
|
|
|
return (this.cache && this.cache.exists(this.key));
|
2018-04-25 16:13:06 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds this file to its target cache upon successful loading and processing.
|
|
|
|
* It will emit a `filecomplete` event from the LoaderPlugin.
|
|
|
|
* This method is often overridden by specific file types.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#addToCache
|
|
|
|
* @since 3.7.0
|
|
|
|
*/
|
|
|
|
addToCache: function ()
|
|
|
|
{
|
2018-04-25 22:16:17 +00:00
|
|
|
if (this.cache)
|
|
|
|
{
|
|
|
|
this.cache.add(this.key, this.data);
|
|
|
|
}
|
2018-04-25 16:13:06 +00:00
|
|
|
|
|
|
|
this.loader.emit('filecomplete', this.key, this);
|
2018-04-27 17:44:12 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy this File and any references it holds.
|
|
|
|
* Called automatically by the Loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader.File#destroy
|
|
|
|
* @since 3.7.0
|
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.loader = null;
|
|
|
|
this.cache = null;
|
|
|
|
this.xhrSettings = null;
|
|
|
|
this.linkFile = null;
|
|
|
|
this.data = null;
|
2016-12-01 12:50:58 +00:00
|
|
|
}
|
2017-07-03 15:05:22 +00:00
|
|
|
|
|
|
|
});
|
2016-11-30 01:24:33 +00:00
|
|
|
|
2017-10-12 16:13:40 +00:00
|
|
|
/**
|
|
|
|
* Static method for creating object URL using URL API and setting it as image 'src' attribute.
|
|
|
|
* If URL API is not supported (usually on old browsers) it falls back to creating Base64 encoded url using FileReader.
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @method Phaser.Loader.File.createObjectURL
|
2017-10-12 16:13:40 +00:00
|
|
|
* @static
|
2018-04-18 12:29:22 +00:00
|
|
|
* @param {HTMLImageElement} image - Image object which 'src' attribute should be set to object URL.
|
2018-02-08 17:00:14 +00:00
|
|
|
* @param {Blob} blob - A Blob object to create an object URL for.
|
|
|
|
* @param {string} defaultType - Default mime type used if blob type is not available.
|
2017-10-12 16:13:40 +00:00
|
|
|
*/
|
|
|
|
File.createObjectURL = function (image, blob, defaultType)
|
2017-10-12 14:20:42 +00:00
|
|
|
{
|
2017-11-14 12:21:25 +00:00
|
|
|
if (typeof URL === 'function')
|
2017-10-12 14:20:42 +00:00
|
|
|
{
|
2017-10-12 16:13:40 +00:00
|
|
|
image.src = URL.createObjectURL(blob);
|
2017-10-12 14:20:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var reader = new FileReader();
|
|
|
|
|
2017-11-14 12:21:25 +00:00
|
|
|
reader.onload = function ()
|
2017-10-12 14:20:42 +00:00
|
|
|
{
|
2017-11-14 12:21:25 +00:00
|
|
|
image.removeAttribute('crossOrigin');
|
2017-10-12 16:13:40 +00:00
|
|
|
image.src = 'data:' + (blob.type || defaultType) + ';base64,' + reader.result.split(',')[1];
|
2017-10-12 14:20:42 +00:00
|
|
|
};
|
|
|
|
|
2017-10-12 16:13:40 +00:00
|
|
|
reader.onerror = image.onerror;
|
2017-10-12 14:20:42 +00:00
|
|
|
|
2017-10-12 16:13:40 +00:00
|
|
|
reader.readAsDataURL(blob);
|
2017-10-12 14:20:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-10-12 16:13:40 +00:00
|
|
|
/**
|
|
|
|
* Static method for releasing an existing object URL which was previously created
|
|
|
|
* by calling {@link File#createObjectURL} method.
|
|
|
|
*
|
2018-02-08 17:00:14 +00:00
|
|
|
* @method Phaser.Loader.File.revokeObjectURL
|
2017-10-12 16:13:40 +00:00
|
|
|
* @static
|
2018-04-18 12:29:22 +00:00
|
|
|
* @param {HTMLImageElement} image - Image object which 'src' attribute should be revoked.
|
2017-10-12 16:13:40 +00:00
|
|
|
*/
|
|
|
|
File.revokeObjectURL = function (image)
|
2017-10-12 14:20:42 +00:00
|
|
|
{
|
2018-01-18 16:48:25 +00:00
|
|
|
if (typeof URL === 'function')
|
2017-10-12 14:20:42 +00:00
|
|
|
{
|
2017-10-12 16:13:40 +00:00
|
|
|
URL.revokeObjectURL(image.src);
|
2017-10-12 14:20:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-11-30 01:24:33 +00:00
|
|
|
module.exports = File;
|