Repository navigation
Expand file tree
/
Copy pathloader.js
More file actions
423 lines (388 loc) · 14 KB
/
Copy pathloader.js
File metadata and controls
423 lines (388 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
var Crafty = require('./core.js'),
document = window.document;
Crafty.extend({
/**@
* #Crafty.assets
* @category Assets
* An object containing every asset used in the current Crafty game.
* The key is the URL and the value is the `Audio` or `Image` object.
*
* If loading an asset, check that it is in this object first to avoid loading twice.
*
* @example
* ~~~
* var isLoaded = !!Crafty.assets["images/sprite.png"];
* ~~~
* @see Crafty.loader
*/
assets: {},
/**@
* #Crafty.asset
* @category Assets
*
* @trigger NewAsset - After setting new asset - Object - key and value of new added asset.
* @sign public void Crafty.asset(String key, Object asset)
* @param key - asset url.
* @param asset - Audio` or `Image` object.
* Add new asset to assets object.
*
* @sign public void Crafty.asset(String key)
* @param key - asset url.
* Get asset from assets object.
*
* @example
* ~~~
* Crafty.asset(key, value);
* var asset = Crafty.asset(key); //object with key and value fields
* ~~~
*
* @see Crafty.assets
*/
asset: function (key, value) {
if (arguments.length === 1) {
return Crafty.assets[key];
}
if (!Crafty.assets[key]) {
Crafty.assets[key] = value;
this.trigger("NewAsset", {
key: key,
value: value
});
return value;
}
},
/**@
* #Crafty.image_whitelist
* @category Assets
*
*
* A list of file extensions that can be loaded as images by Crafty.load
*
* @example
* ~~~
* Crafty.image_whitelist.push("tif")
* Crafty.load(["images/sprite.tif", "sounds/jump.mp3"],
* function() {
* //when loaded
* Crafty.scene("main"); //go to main scene
* Crafty.audio.play("jump.mp3"); //Play the audio file
* },
*
* function(e) {
* //progress
* },
*
* function(e) {
* //uh oh, error loading
* }
* );
* ~~~
*
* @see Crafty.asset
* @see Crafty.load
*/
image_whitelist: ["jpg", "jpeg", "gif", "png", "svg"],
/**@
* #Crafty.loader
* @category Assets
* @sign public void Crafty.load(Array assets, Function onLoad[, Function onProgress, Function onError])
* @param assets - Array of assets to load (accepts sounds and images)
* @param onLoad - Callback when the assets are loaded
* @param onProgress - Callback when an asset is loaded. Contains information about assets loaded
* @param onError - Callback when an asset fails to load
*
* Preloader for all assets. Takes an array of URLs and
* adds them to the `Crafty.assets` object.
*
* Files with suffixes in `image_whitelist` (case insensitive) will be loaded.
*
* If `Crafty.support.audio` is `true`, files with the following suffixes `mp3`, `wav`, `ogg` and `mp4` (case insensitive) can be loaded.
*
* The `onProgress` function will be passed on object with information about
* the progress including how many assets loaded, total of all the assets to
* load and a percentage of the progress.
* ~~~
* { loaded: j, total: total, percent: (j / total * 100) ,src:src})
* ~~~
*
* `onError` will be passed with the asset that couldn't load.
*
* When `onError` is not provided, the onLoad is loaded even some assets are not successfully loaded. Otherwise, onLoad will be called no matter whether there are errors or not.
*
* @example
* ~~~
* Crafty.load(["images/sprite.png", "sounds/jump.mp3"],
* function() {
* //when loaded
* Crafty.scene("main"); //go to main scene
* Crafty.audio.play("jump.mp3"); //Play the audio file
* },
*
* function(e) {
* //progress
* },
*
* function(e) {
* //uh oh, error loading
* }
* );
* ~~~
*
* @see Crafty.assets
* @see Crafty.image_whitelist
*/
load: function (data, oncomplete, onprogress, onerror) {
var i = 0,
l = data.length,
current, obj, total = l,
j = 0,
ext = "";
//Progress function
function pro() {
var src = this.src;
//Remove events cause audio trigger this event more than once(depends on browser)
if (this.removeEventListener) {
this.removeEventListener('canplaythrough', pro, false);
}
++j;
//if progress callback, give information of assets loaded, total and percent
if (onprogress)
onprogress({
loaded: j,
total: total,
percent: (j / total * 100),
src: src
});
if (j === total && oncomplete) oncomplete();
}
//Error function
function err() {
var src = this.src;
if (onerror)
onerror({
loaded: j,
total: total,
percent: (j / total * 100),
src: src
});
j++;
if (j === total && oncomplete) oncomplete();
}
for (; i < l; ++i) {
current = data[i];
ext = current.substr(current.lastIndexOf('.') + 1, 3).toLowerCase();
obj = Crafty.asset(current) || null;
if (Crafty.audio.supports(ext)) {
//Create a new asset if necessary, using the file name as an id
if (!obj) {
var name = current.substr(current.lastIndexOf('/') + 1).toLowerCase();
obj = Crafty.audio.create(name, current).obj;
}
//addEventListener is supported on IE9 , Audio as well
if (obj.addEventListener) {
obj.addEventListener('canplaythrough', pro, false);
}
} else if (Crafty.image_whitelist.indexOf(ext) >= 0) {
if (!obj) {
obj = new Image();
Crafty.asset(current, obj);
}
obj.onload = pro;
if (Crafty.support.prefix === 'webkit') {
obj.src = ""; // workaround for webkit bug
}
obj.src = current; //setup src after onload function Opera/IE Bug
} else {
total--;
continue; //skip if not applicable
}
obj.onerror = err;
}
// If we aren't trying to handle *any* of the files, that's as complete as it gets!
if (total === 0)
oncomplete();
},
/**@
* #Crafty.modules
* @category Assets
* @sign public void Crafty.modules([String repoLocation,] Object moduleMap[, Function onLoad])
* @param modules - Map of name:version pairs for modules to load
* @param onLoad - Callback when the modules are loaded
*
* Browse the selection of community modules on http://craftycomponents.com
*
* It is possible to create your own repository.
*
*
* @example
* ~~~
* // Loading from default repository
* Crafty.modules({ moveto: 'DEV' }, function () {
* //module is ready
* Crafty.e("MoveTo, 2D, DOM");
* });
*
* // Loading from your own server
* Crafty.modules({ 'http://mydomain.com/js/mystuff.js': 'DEV' }, function () {
* //module is ready
* Crafty.e("MoveTo, 2D, DOM");
* });
*
* // Loading from alternative repository
* Crafty.modules('http://cdn.crafty-modules.com', { moveto: 'DEV' }, function () {
* //module is ready
* Crafty.e("MoveTo, 2D, DOM");
* });
*
* // Loading from the latest component website
* Crafty.modules(
* 'http://cdn.craftycomponents.com'
* , { MoveTo: 'release' }
* , function () {
* Crafty.e("2D, DOM, Color, MoveTo")
* .attr({x: 0, y: 0, w: 50, h: 50})
* .color("green");
* });
* });
* ~~~
*
*/
modules: function (modulesRepository, moduleMap, oncomplete) {
if (arguments.length === 2 && typeof modulesRepository === "object") {
oncomplete = moduleMap;
moduleMap = modulesRepository;
modulesRepository = 'http://cdn.craftycomponents.com';
}
/*!
* $script.js Async loader & dependency manager
* https://github.com/ded/script.js
* (c) Dustin Diaz, Jacob Thornton 2011
* License: MIT
*/
var $script = (function () {
var win = this,
doc = document,
head = doc.getElementsByTagName('head')[0],
validBase = /^https?:\/\//,
old = win.$script,
list = {}, ids = {}, delay = {}, scriptpath, scripts = {}, s = 'string',
f = false,
push = 'push',
domContentLoaded = 'DOMContentLoaded',
readyState = 'readyState',
addEventListener = 'addEventListener',
onreadystatechange = 'onreadystatechange';
function every(ar, fn, i) {
for (i = 0, j = ar.length; i < j; ++i)
if (!fn(ar[i])) return f;
return 1;
}
function each(ar, fn) {
every(ar, function (el) {
return !fn(el);
});
}
if (!doc[readyState] && doc[addEventListener]) {
doc[addEventListener](domContentLoaded, function fn() {
doc.removeEventListener(domContentLoaded, fn, f);
doc[readyState] = 'complete';
}, f);
doc[readyState] = 'loading';
}
function $script(paths, idOrDone, optDone) {
paths = paths[push] ? paths : [paths];
var idOrDoneIsDone = idOrDone && idOrDone.call,
done = idOrDoneIsDone ? idOrDone : optDone,
id = idOrDoneIsDone ? paths.join('') : idOrDone,
queue = paths.length;
function loopFn(item) {
return item.call ? item() : list[item];
}
function callback() {
if (!--queue) {
list[id] = 1;
if (done)
done();
for (var dset in delay) {
if (every(dset.split('|'), loopFn) && !each(delay[dset], loopFn))
delay[dset] = [];
}
}
}
setTimeout(function () {
each(paths, function (path) {
if (scripts[path]) {
if (id)
ids[id] = 1;
return scripts[path] == 2 && callback();
}
scripts[path] = 1;
if (id)
ids[id] = 1;
create(!validBase.test(path) && scriptpath ? scriptpath + path + '.js' : path, callback);
});
}, 0);
return $script;
}
function create(path, fn) {
var el = doc.createElement('script'),
loaded = f;
el.onload = el.onerror = el[onreadystatechange] = function () {
if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return;
el.onload = el[onreadystatechange] = null;
loaded = 1;
scripts[path] = 2;
fn();
};
el.async = 1;
el.src = path;
head.insertBefore(el, head.firstChild);
}
$script.get = create;
$script.order = function (scripts, id, done) {
(function callback(s) {
s = scripts.shift();
if (!scripts.length) $script(s, id, done);
else $script(s, callback);
}());
};
$script.path = function (p) {
scriptpath = p;
};
// This function is a tangled mess of conciseness, so suppress warnings here
/* jshint -W030 */
$script.ready = function (deps, ready, req) {
deps = deps[push] ? deps : [deps];
var missing = [];
!each(deps, function (dep) {
list[dep] || missing[push](dep);
}) && every(deps, function (dep) {
return list[dep];
}) ?
ready() : ! function (key) {
delay[key] = delay[key] || [];
delay[key][push](ready);
req && req(missing);
}(deps.join('|'));
return $script;
};
/* jshint +W030 */
$script.noConflict = function () {
win.$script = old;
return this;
};
return $script;
})();
var modules = [];
var validBase = /^(https?|file):\/\//;
for (var i in moduleMap) {
if (validBase.test(i))
modules.push(i);
else
modules.push(modulesRepository + '/' + i.toLowerCase() + '-' + moduleMap[i].toLowerCase() + '.js');
}
$script(modules, function () {
if (oncomplete) oncomplete();
});
}
});