From e0177777732907b59eb99992630824a86abb89c1 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Sat, 5 May 2018 12:31:04 +0100 Subject: [PATCH] Updated jsdocs --- src/loader/filetypes/ImageFile.js | 2 +- src/loader/filetypes/JSONFile.js | 118 ++++++++++++++++++++++++++---- src/loader/filetypes/TextFile.js | 2 +- src/loader/filetypes/XMLFile.js | 4 +- 4 files changed, 106 insertions(+), 20 deletions(-) diff --git a/src/loader/filetypes/ImageFile.js b/src/loader/filetypes/ImageFile.js index 645b3b033..35ecb1284 100644 --- a/src/loader/filetypes/ImageFile.js +++ b/src/loader/filetypes/ImageFile.js @@ -224,7 +224,7 @@ var ImageFile = new Class({ * and no URL is given then the Loader will set the URL to be "alien.png". It will always add `.png` as the extension, although * this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL. * - * Note: Th ability to load this type of file will only be available if the Image File type has been built into Phaser. + * Note: The ability to load this type of file will only be available if the Image File type has been built into Phaser. * It is available in the default build but can be excluded from custom builds. * * @method Phaser.Loader.LoaderPlugin#image diff --git a/src/loader/filetypes/JSONFile.js b/src/loader/filetypes/JSONFile.js index 4fb25daa1..6b55331df 100644 --- a/src/loader/filetypes/JSONFile.js +++ b/src/loader/filetypes/JSONFile.js @@ -9,11 +9,26 @@ var CONST = require('../const'); var File = require('../File'); var FileTypesManager = require('../FileTypesManager'); var GetFastValue = require('../../utils/object/GetFastValue'); +var GetValue = require('../../utils/object/GetValue'); var IsPlainObject = require('../../utils/object/IsPlainObject'); +/** + * @typedef {object} Phaser.Loader.FileTypes.JSONFileConfig + * + * @property {string} key - The key of the file. Must be unique within both the Loader and the Text Cache. + * @property {string|any} [url] - The absolute or relative URL to load the file from. Or can be a ready formed JSON object, in which case it will be directly added to the Cache. + * @property {string} [extension='json'] - The default file extension to use if no url is provided. + * @property {string} [dataKey] - If specified instead of the whole JSON file being parsed and added to the Cache, only the section corresponding to this property key will be added. If the property you want to extract is nested, use periods to divide it. + * @property {XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file. + */ + /** * @classdesc - * [description] + * A single JSON File suitable for loading by the Loader. + * + * These are created when you use the Phaser.Loader.LoaderPlugin#json method and are not typically created directly. + * + * For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#json. * * @class JSONFile * @extends Phaser.Loader.File @@ -21,10 +36,11 @@ var IsPlainObject = require('../../utils/object/IsPlainObject'); * @constructor * @since 3.0.0 * - * @param {string} key - [description] - * @param {string} url - [description] - * @param {string} path - [description] - * @param {XHRSettingsObject} [xhrSettings] - [description] + * @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file. + * @param {(string|Phaser.Loader.FileTypes.JSONFileConfig)} key - The key to use for this file, or a file configuration object. + * @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `.json`, i.e. if `key` was "alien" then the URL will be "alien.json". + * @param {XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file. + * @param {string} [dataKey] - When the JSON file loads only this property will be stored in the Cache. */ var JSONFile = new Class({ @@ -67,12 +83,19 @@ var JSONFile = new Class({ { // Object provided instead of a URL, so no need to actually load it (populate data with value) - this.data = (dataKey && dataKey !== '' && url.hasOwnProperty(dataKey)) ? url[dataKey] : url; + this.data = GetValue(url, dataKey, url); this.state = CONST.FILE_POPULATED; } }, + /** + * Called automatically by Loader.nextFile. + * This method controls what extra work this File does with its loaded data. + * + * @method Phaser.Loader.FileTypes.JSONFile#onProcess + * @since 3.7.0 + */ onProcess: function () { if (this.state !== CONST.FILE_POPULATED) @@ -80,9 +103,17 @@ var JSONFile = new Class({ this.state = CONST.FILE_PROCESSING; var json = JSON.parse(this.xhrLoader.responseText); + var key = this.config; - this.data = (key && key !== '' && json.hasOwnProperty(key)) ? json[key] : json; + if (key && key !== '') + { + this.data = GetValue(json, key, json); + } + else + { + this.data = json; + } } this.onProcessComplete(); @@ -91,22 +122,77 @@ var JSONFile = new Class({ }); /** - * Adds a JSON file to the current load queue. + * Adds a JSON file, or array of JSON files, to the current load queue. * - * Note: This method will only be available if the JSON File type has been built into Phaser. + * The file is **not** loaded immediately, it is added to a queue ready to be loaded either when the loader starts, + * or if it's already running, when the next free load slot becomes available. This means you cannot use the file + * immediately after calling this method, but instead must wait for the file to complete. + * + * The key must be a unique String. It is used to add the file to the global JSON Cache upon a successful load. + * The key should be unique both in terms of files being loaded and files already present in the JSON Cache. + * Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file + * then remove it from the JSON Cache first, before loading a new one. * - * The file is **not** loaded immediately after calling this method. - * Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts. + * Instead of passing arguments you can pass a configuration object, such as: + * + * ```javascript + * this.load.json({ + * key: 'wavedata', + * url: 'files/AlienWaveData.json' + * }); + * ``` + * + * See the documentation for `Phaser.Loader.FileTypes.JSONFileConfig` for more details. + * + * Once the file has finished loading you can access it from its Cache using its key: + * + * ```javascript + * this.load.json('wavedata', 'files/AlienWaveData.json'); + * // and later in your game ... + * var data = this.cache.json.get('wavedata'); + * ``` + * + * If you have specified a prefix in the loader, via `Loader.setPrefix` then this value will be prepended to this files + * key. For example, if the prefix was `LEVEL1.` and the key was `Waves` the final key will be `LEVEL1.Waves` and + * this is what you would use to retrieve the text from the JSON Cache. + * + * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. + * + * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "data" + * and no URL is given then the Loader will set the URL to be "data.json". It will always add `.json` as the extension, although + * this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL. + * + * You can also optionally provide a `dataKey` to use. This allows you to extract only a part of the JSON and store it in the Cache, + * rather than the whole file. For example, if your JSON data had a structure like this: + * + * ```json + * { + * "level1": { + * "baddies": { + * "aliens": {}, + * "boss": {} + * } + * }, + * "level2": {}, + * "level3": {} + * } + * ``` + * + * And you only wanted to store the `boss` data in the Cache, then you could pass `level1.baddies.boss`as the `dataKey`. + * + * Note: The ability to load this type of file will only be available if the JSON File type has been built into Phaser. + * It is available in the default build but can be excluded from custom builds. * * @method Phaser.Loader.LoaderPlugin#json + * @fires Phaser.Loader.LoaderPlugin#addFileEvent * @since 3.0.0 * - * @param {string} key - [description] - * @param {string} url - [description] - * @param {string} [dataKey] - [description] - * @param {XHRSettingsObject} [xhrSettings] - [description] + * @param {(string|Phaser.Loader.FileTypes.JSONFileConfig|Phaser.Loader.FileTypes.JSONFileConfig[])} key - The key to use for this file, or a file configuration object, or array of them. + * @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `.json`, i.e. if `key` was "alien" then the URL will be "alien.json". + * @param {string} [dataKey] - When the JSON file loads only this property will be stored in the Cache. + * @param {XHRSettingsObject} [xhrSettings] - An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings. * - * @return {Phaser.Loader.LoaderPlugin} The Loader. + * @return {Phaser.Loader.LoaderPlugin} The Loader instance. */ FileTypesManager.register('json', function (key, url, dataKey, xhrSettings) { diff --git a/src/loader/filetypes/TextFile.js b/src/loader/filetypes/TextFile.js index beb3f5b73..4f96ac769 100644 --- a/src/loader/filetypes/TextFile.js +++ b/src/loader/filetypes/TextFile.js @@ -131,7 +131,7 @@ var TextFile = new Class({ * and no URL is given then the Loader will set the URL to be "story.txt". It will always add `.txt` as the extension, although * this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL. * - * Note: Th ability to load this type of file will only be available if the Text File type has been built into Phaser. + * Note: The ability to load this type of file will only be available if the Text File type has been built into Phaser. * It is available in the default build but can be excluded from custom builds. * * @method Phaser.Loader.LoaderPlugin#text diff --git a/src/loader/filetypes/XMLFile.js b/src/loader/filetypes/XMLFile.js index 86b3cd5be..830354ff7 100644 --- a/src/loader/filetypes/XMLFile.js +++ b/src/loader/filetypes/XMLFile.js @@ -36,7 +36,7 @@ var ParseXML = require('../../dom/ParseXML'); * @since 3.0.0 * * @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file. - * @param {(string|Phaser.Loader.FileTypes.TextFileConfig)} key - The key to use for this file, or a file configuration object. + * @param {(string|Phaser.Loader.FileTypes.XMLFileConfig)} key - The key to use for this file, or a file configuration object. * @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `.xml`, i.e. if `key` was "alien" then the URL will be "alien.xml". * @param {XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file. */ @@ -141,7 +141,7 @@ var XMLFile = new Class({ * and no URL is given then the Loader will set the URL to be "data.xml". It will always add `.xml` as the extension, although * this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL. * - * Note: Th ability to load this type of file will only be available if the XML File type has been built into Phaser. + * Note: The ability to load this type of file will only be available if the XML File type has been built into Phaser. * It is available in the default build but can be excluded from custom builds. * * @method Phaser.Loader.LoaderPlugin#xml