-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoader.js
132 lines (126 loc) · 2.99 KB
/
Loader.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
function Loader(res) {
this.resources = {};
this.resourcesLen = res.length;
this.counter = 0;
this.timer = {};
this.init(res);
}
Loader.prototype = {
init: function(res) {
var self = this;
self.setRes(res);
self.fetch(self.resources);
Loader.prototype = utils.mixin(Loader.prototype, Events.prototype);
},
setRes: function(res) {
var self = this;
res.forEach(function(v, i) {
var name = self.getFileName(v);
var ext = self.getFileExt(v);
var type = self.getNodeType(ext);
var r = {
ext: ext,
type: type,
path: v
}
self.resources[name] = r;
});
},
getFileName: function(url) {
return url.slice(url.lastIndexOf('/') + 1).replace(/\?.*$/, '').toLowerCase();
},
getFileExt: function(url) {
return url.slice(url.lastIndexOf('.') + 1, url.length).toLowerCase();
},
getNodeType: function(ext) {
var types = {
img: ['jpg', 'jpeg', 'gif', 'png', 'bmp'],
audio: ['ogg', 'wav', 'mp3', 'aac']
}
for (var type in types) {
if (types[type].indexOf(ext) > -1) {
return type;
}
}
},
fetchImg: function(path) {
var self = this;
var img = new Image();
img.style.display = 'none';
img.onload = function() {
self.count();
console.log('img done', self.counter);
};
img.src = path;
},
fetchAudio: function(path) {
var self = this;
var audio = new Audio();
audio.preload = 'auto';
var audioLoaded = (function() {
return function() {
if (audio.readyState > 0) {
clearInterval(self.timer[path]);
self.timer[path] = null;
self.count();
console.log('audio done', self.counter);
}
}
})();
self.timer[path] = setInterval(audioLoaded, 100);
audio.src = path;
},
fetch: function(res) {
for (var k in res) {
switch (res[k].type) {
case 'img': this.fetchImg(res[k].path); break;
case 'audio': this.fetchAudio(res[k].path); break;
}
}
},
count: function() {
this.counter++;
if (this.counter === this.resourcesLen) {
this.trigger('loaded');
} else {
var percentage = (this.counter/this.resourcesLen * 100).toFixed(2) + '%';
this.trigger('progress', percentage);
}
}
}
var Events = function() {
// this.map = {};
};
Events.prototype = {
constructor: Event,
eventMap: {},
trigger: function(eventname, args) {
if (this.eventMap[eventname]) {
this.eventMap[eventname].forEach(function(fn) {
fn.call(this, args);
});
}
},
on: function(eventname, callback) {
if (this.eventMap[eventname]) {
this.eventMap[eventname].push(callback);
} else {
this.eventMap[eventname] = [callback];
}
}
};
var utils = {
mixin: function(sup, obj) {
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
sup[i] = obj[i];
}
}
return sup;
}
};
var loader = (function() {
return function(res) {
return new Loader(res);
}
})();