@@ -10,17 +10,127 @@ import { preloadJSON } from "./parsers/json.js";
1010import { preloadBinary } from "./parsers/binary.js" ;
1111import { preloadJavascript } from "./parsers/script.js" ;
1212import { preloadVideo } from "./parsers/video.js" ;
13- import { baseURL } from "./settings.js" ;
1413import { warning } from "../lang/console.js" ;
1514
16-
1715/**
1816 * a small class to manage loading of stuff and manage resources
1917 * @namespace loader
2018 */
2119
22- // for backward compatibility
23- export * from "./settings.js" ;
20+
21+ // to enable/disable caching
22+ export let nocache = "" ;
23+
24+ // baseURL
25+ export let baseURL = { } ;
26+
27+ /**
28+ * crossOrigin attribute to configure the CORS requests for Image and Video data element.
29+ * By default (that is, when the attribute is not specified), CORS is not used at all.
30+ * The "anonymous" keyword means that there will be no exchange of user credentials via cookies,
31+ * client-side SSL certificates or HTTP authentication as described in the Terminology section of the CORS specification.<br>
32+ * @type {string }
33+ * @name crossOrigin
34+ * @default undefined
35+ * @see loader.setOptions
36+ * @memberof loader
37+ * @see https://developer.mozilla.org/en-US/docs/Web/HTML/CORS_settings_attributes
38+ * @example
39+ * // allow for cross-origin texture loading
40+ * me.loader.crossOrigin = "anonymous";
41+ *
42+ * // set all ressources to be loaded
43+ * me.loader.preload(game.resources, () => this.loaded());
44+ */
45+ export let crossOrigin ;
46+
47+ /**
48+ * indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies,
49+ * authorization headers or TLS client certificates. Setting withCredentials has no effect on same-site requests.
50+ * @public
51+ * @type {boolean }
52+ * @name withCredentials
53+ * @see loader.setOptions
54+ * @default false
55+ * @memberof loader
56+ * @see https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
57+ * @example
58+ * // enable withCredentials
59+ * me.loader.withCredentials = true;
60+ *
61+ * // set all ressources to be loaded
62+ * me.loader.preload(game.resources, () => this.loaded());
63+ */
64+ export let withCredentials = false ;
65+
66+ /**
67+ * enable the nocache mechanism
68+ * @ignore
69+ */
70+ export function setNocache ( enable = false ) {
71+ nocache = enable ? "?" + ~ ~ ( Math . random ( ) * 10000000 ) : "" ;
72+ }
73+
74+ /**
75+ * Sets the options for the loader.
76+ * @memberof loader
77+ * @param {Object } options - The options to set.
78+ * @param {string } [options.crossOrigin] - The crossOrigin attribute to configure the CORS requests for Image and Video data element.
79+ * @param {boolean } [options.nocache] - Enable or disable the nocache mechanism.
80+ * @param {boolean } [options.withCredentials] - Indicates whether or not cross-site Access-Control requests should be made using credentials.
81+ * @example
82+ * // Set the crossOrigin attribute to "anonymous"
83+ * me.loader.setOptions({ crossOrigin: "anonymous" });
84+ *
85+ * // Enable the nocache mechanism
86+ * me.loader.setOptions({ nocache: true });
87+ *
88+ * // Enable withCredentials
89+ * me.loader.setOptions({ withCredentials: true });
90+ */
91+ export function setOptions ( options ) {
92+ if ( options . crossOrigin !== undefined ) {
93+ crossOrigin = options . crossOrigin ;
94+ }
95+ if ( options . nocache !== undefined ) {
96+ setNocache ( options . nocache ) ;
97+ }
98+ if ( options . withCredentials !== undefined ) {
99+ withCredentials = options . withCredentials ;
100+ }
101+ }
102+
103+ /**
104+ * change the default baseURL for the given asset type.<br>
105+ * (this will prepend the asset URL and must finish with a '/')
106+ * @name setBaseURL
107+ * @memberof loader
108+ * @public
109+ * @param {string } type - "*", "audio", "video", "binary", "image", "json", "js", "tmx", "tsx"
110+ * @param {string } [url="./"] - default base URL
111+ * @example
112+ * // change the base URL relative address for audio assets
113+ * me.loader.setBaseURL("audio", "data/audio/");
114+ * // change the base URL absolute address for all object types
115+ * me.loader.setBaseURL("*", "http://myurl.com/")
116+ */
117+ export function setBaseURL ( type , url ) {
118+ if ( type !== "*" ) {
119+ baseURL [ type ] = url ;
120+ } else {
121+ // "wildcards"
122+ baseURL [ "audio" ] = url ;
123+ baseURL [ "video" ] = url ;
124+ baseURL [ "binary" ] = url ;
125+ baseURL [ "image" ] = url ;
126+ baseURL [ "json" ] = url ;
127+ baseURL [ "js" ] = url ;
128+ baseURL [ "tmx" ] = url ;
129+ baseURL [ "tsx" ] = url ;
130+ // XXX ?
131+ //baseURL["fontface"] = url;
132+ }
133+ }
24134
25135/**
26136 * onload callback
@@ -375,7 +485,11 @@ export function load(asset, onload, onerror) {
375485 }
376486
377487 // parser returns the amount of asset to be loaded (usually 1 unless an asset is splitted into several ones)
378- return parser . call ( this , asset , onload , onerror ) ;
488+ return parser . call ( this , asset , onload , onerror , {
489+ nocache : nocache ,
490+ crossOrigin : crossOrigin ,
491+ withCredentials : withCredentials
492+ } ) ;
379493}
380494
381495/**
0 commit comments