-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
131 lines (111 loc) · 3.91 KB
/
main.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
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
define([
/*====="dojo/_base/declare",=====*/
"dojo/_base/lang",
"dojo/Deferred",
"dojo/promise/all",
"dojo/_base/array",
"dojo/dom-prop"
], function(/*=====declare, =====*/lang, Deferred, all, array, domProp){
/*=====
var ImageLoaderOptions = declare(null, {
// srcRoot: String
// A root URL to prepend to relative URLs.
srcRoot: null,
// defaultAttributes: Object
// A hash of default attribute values for the loaded image(s).
defaultAttributes: null
});
=====*/
var trailingSlashPattern = /\/$/;
function prepareSrcRoot(srcRoot){
return trailingSlashPattern.test(srcRoot) ? srcRoot : (srcRoot + "/");
}
var leadingProtocolPattern = /^\w+:\/\//;
function resolveUrl(srcRoot, url){
return leadingProtocolPattern.test(url) ? url : (srcRoot + url);
}
function compileImageAttributes(srcRoot, defaultAttributes, urlOrAttributes){
var imageAttributes = lang.mixin(
{},
defaultAttributes,
typeof urlOrAttributes === 'string' ? { src: urlOrAttributes } : urlOrAttributes
);
if(!imageAttributes.src){
throw new Error("No 'src' provided for image.");
}
if(srcRoot){
imageAttributes.src = resolveUrl(srcRoot, imageAttributes.src);
}
return imageAttributes;
}
function load(srcRoot, defaultAttributes, urlOrAttributes){
var dfd = new Deferred(),
attributes = compileImageAttributes(srcRoot, defaultAttributes, urlOrAttributes),
image = new Image();
// Set up event handlers before setting image attributes because the load event
// can fire immediately for cached images in some browsers.
image.onload = function(){
image.onload = image.onerror = null;
dfd.resolve(image);
};
image.onerror = function(){
image.onload = image.onerror = null;
dfd.reject(new Error("Image failed to load: " + attributes.src));
};
domProp.set(image, attributes);
return dfd.promise;
}
function imageLoad(options, arrayOrObject){
// summary:
// Loads a collection of images using the specified options.
// description:
// Takes a collection of image URLs and/or attribute hashes and
// returns a promise that is fulfilled when all images have been loaded.
// If one of the images fails to load, the returned promise is rejected.
// options: ImageLoaderOptions?
// The load options.
// arrayOrObject: Array|Object
// An array or object hash of URLs and/or image attribute hashes to load.
// returns: dojo/promise/Promise
// The promise will be fulfilled with an array of results if invoked with
// an array, or an object of results when passed an object
// (using the same keys).
arrayOrObject || ((arrayOrObject = options) && (options = {}));
var srcRoot = options.srcRoot && prepareSrcRoot(options.srcRoot),
defaultAttributes = options.defaultAttributes || {},
promises;
if(arrayOrObject instanceof Array){
promises = array.map(arrayOrObject, function(imageSpec){
return load(srcRoot, defaultAttributes, imageSpec);
});
}else if(arrayOrObject instanceof Object){
promises = {};
for(var key in arrayOrObject){
promises[key] = load(srcRoot, defaultAttributes, arrayOrObject[key]);
}
}else{
throw new Error("Argument must be an Array or Object.");
}
return all(promises);
}
imageLoad.one = function one(options, stringOrObject){
// summary:
// Loads a single image.
// options: ImageLoaderOptions?
// The load options.
// stringOrObject: String|Object
// A URL or an image attributes hash containing at least a 'src' attribute.
// returns: dojo/promise/Promise
// Returns a promise for an image.
stringOrObject || ((stringOrObject = options) && (options = {}));
var imageSpec = typeof stringOrObject === "string" ? { src: stringOrObject } : stringOrObject;
return imageLoad(options, [ imageSpec ]).then(function(images){
return images[0];
});
};
// AMD loader plugin
imageLoad.load = function(id, require, load){
imageLoad.one(require.toUrl(id)).then(load);
};
return imageLoad;
});