-
Notifications
You must be signed in to change notification settings - Fork 29
/
images.js
73 lines (56 loc) · 1.51 KB
/
images.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
goog.provide('pl.images');
goog.require('goog.asserts');
goog.require('goog.net.EventType');
goog.require('goog.net.ImageLoader');
goog.require('goog.object');
/**
* @constructor
* @param {Object.<string, string>} source
*/
pl.images = function(source) {
this._source = source;
/**
* @private
* @type {Object.<string, !HTMLImageElement>}
*/
this._images = {};
};
pl.images.prototype.load = function(progress, complete) {
var loader = new goog.net.ImageLoader();
goog.object.forEach(this._source, function(e,i,o) {
loader.addImage(i, e);
});
var ratio = 0;
loader.addEventListener(goog.events.EventType.LOAD, function(e) {
var key = e.target.id;
var image = e.target;
goog.asserts.assert(key in this._source);
goog.asserts.assert(!(key in this._images));
this._images[e.target.id] = e.target;
ratio = goog.object.getCount(this._images) / goog.object.getCount(this._source);
progress(ratio);
}, false, this);
loader.addEventListener(goog.net.EventType.COMPLETE, function(e) {
complete();
}, false, this);
loader.start();
};
/**
* @param {!string} key
* @param {!string} path
**/
pl.images.prototype.add = function(key, path) {
goog.asserts.assert(!goog.object.containsKey(this._source, key));
goog.object.set(this._source, key, path);
};
/**
* @param {string} key
* @return {!HTMLImageElement}
*/
pl.images.prototype.get = function(key) {
if (key in this._images) {
return this._images[key];
} else {
throw Error('not found');
}
};