-
Notifications
You must be signed in to change notification settings - Fork 27
/
image.js
50 lines (43 loc) · 1.48 KB
/
image.js
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
/**
* Image loader plugin
*
* Flag "image"
*
* This plugin provides require.image() function
*/
/**
* @name sandbox
*/
(function (sb) {
/**
* Loads any image
*
* @param {String|Array} moduleName path to file
* @param {Function} [callback] callback(result) undefined on error HTMLImageElement on success
*/
sb.require.image = function (moduleName, callback) {
var replacement = sb.trigger('*:request-off-package', moduleName, callback, 'image'), // [[returnResult, moduleName, module, true], callback, type]
returnResult = replacement[0][0];
if (replacement[0][3]) { // isReturnASAP
return returnResult;
}
var module = replacement[0][2];
callback = replacement[1];
moduleName = replacement[0][1];
/*if ($P.WORKER || $P.NODE) {*///#JSCOVERAGE_IF 0/*}*/
var img = new Image();
/*if ($P.IE) {*/var cleanup = function () {try {delete img.onload; delete img.onerror;} catch (err) {img.onload = img.onerror = sb.noop;}};/*}*/
img.onload = function () {
callback(sb.register(moduleName, img));
/*if ($P.IE) {*/cleanup();/*}*/
};
img.onerror = function () {
sb.trigger('*:request-error', moduleName, module);
callback();
/*if ($P.IE) {*/cleanup();/*}*/
};
img.src = moduleName;
return returnResult;
/*if ($P.WORKER || $P.NODE) {*///#JSCOVERAGE_ENDIF/*}*/
};
}(sandbox));