-
Notifications
You must be signed in to change notification settings - Fork 0
/
malt.js
277 lines (232 loc) · 7.47 KB
/
malt.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
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
var Malt = (function() {
// Constants
var LOADING = 1;
var FINISHED = 2;
// Script bodies that have been fetched via XHR, but haven't yet
// been evaled.
var _deferredScripts = [];
// Resource map. Tracks both user-named modules, and resources that
// are loading/loaded.
var _resourceMap = {};
// A log of resources downloaded; used for testing
var _log = [];
//-----------------------------------------------------
// Private Utility Methods
//=====================================================
// A clone of jQuery's $.each utility method
var each = function(arr, callback) {
for (var i = 0; i < arr.length; i++) {
callback(i, arr[i]);
}
};
// A clone of jQuery's $.makeArray utility method
var makeArray = function(array) {
var ret = [];
if (array != null) {
var i = array.length;
while (i) {
ret[--i] = array[i];
}
}
return ret;
};
var loadScript = function(url, onload) {
var xhrObj = getXHRObject();
xhrObj.onreadystatechange = function() {
if (xhrObj.readyState == 4) {
_deferredScripts[url] = xhrObj.responseText;
onload();
}
};
xhrObj.open('GET', url, true);
xhrObj.send('');
};
var getXHRObject = function() {
var xhrObj = false;
try {
xhrObj = new XMLHttpRequest();
}
catch (e) {
var aTypes = ["Msxml2.XMLHTTP.6.0",
"Msxml2.XMLHTTP.3.0",
"Msxml2.XMLHTTP",
"Microsoft.XMLHTTP"];
var len = aTypes.length;
for (var i = 0; i < len; i++) {
try {
xhrObj = new ActiveXObject(aTypes[i]);
}
catch (e) {
continue;
}
break;
}
}
finally {
return xhrObj;
}
};
var loadCSS = function(file, callback) {
var link = document.createElement('link');
link.setAttribute('rel', 'stylesheet');
link.setAttribute('type', 'text/css');
link.setAttribute('href', file);
var head = document.getElementsByTagName('head')[0];
head.appendChild(link);
callback();
};
var loadImage = function(file, callback) {
var image = new Image();
image.src = file;
callback();
};
// Generic resource getter - hands off to one of the methods above
var getGeneric = function(file, callback) {
var loader = null;
var extension = file.match(/\.([^\.]+)$/)[1];
if (extension == 'js') {
loader = loadScript;
} else if (extension == 'css') {
loader = loadCSS;
} else if (/png|jpg|jpeg|gif/.test(extension)) {
loader = loadImage;
}
loader(file, callback);
};
// Class to encapsulate a resource. A resource can be a single file (leaf node),
// or it can be a collection of many resources.
var Resource = function(name, dependencies, callback) {
var self = this;
self.name = name; // Symbolic name (can be null)
self.status = null; // Loading status: null, LOADING, or FINISHED
self.parents = []; // Resources that are "watching" this resource (parents)
self.callback = callback; // Callback to execute once resource is done loading
self.children = null; // Child resources (if they exist)
// If this is a named resource (declared module or leaf node / file), register it
// for later retrieval
if (self.name !== null) {
_resourceMap[self.name] = self;
}
if (typeof dependencies === 'object')
{
// If we're passed an array for names, that means this resource is composed
// of many resources (can also be an array of just ONE resource)
self.children = [];
// For each child resource ...
each(dependencies, function(k, dep) {
// Check our resourceMap to see if we're already tracking this
// resource name, otherwise create a new object
self.children.push(_resourceMap[dep] ? _resourceMap[dep] : new Resource(dep));
});
}
};
Resource.prototype.load = function() {
var self = this;
if (self.status !== null) {
// If this resource is LOADING or FINISHED -- do nothing. The loading
// process will handle all the work once it finishes.
return;
}
// Otherwise this is a brand new resource we haven't seen yet.
self.status = LOADING;
// If this is a leaf node, then we're dealing with an individual file,
// so just fetch the file.
if (!self.children) {
getGeneric(self.name, function() {
self.status = FINISHED;
_log.push(self.name);
// Walk up the tree and find out if any parent resources have
// been satisfied.
self.updateParents();
});
} else {
// This resource has further children. Execute #load for each of them.
each(self.children, function(k, child) {
child.parents.push(self);
child.load();
});
}
};
// Walk up the tree, and for each parent resource, see if it has become
// satisfied as a result of whatever loaded resource triggered this function.
Resource.prototype.updateParents = function() {
each(this.parents, function(k, parent) {
if (parent.isLoaded()) {
// Mark as finished so we don't have to traverse down the tree again.
parent.status = FINISHED;
parent.insertDeferredScripts();
parent.callback && parent.callback();
parent.updateParents();
}
});
};
// Returns true if a resource is loaded.
Resource.prototype.isLoaded = function() {
if (this.status === FINISHED) {
return true;
}
if (!this.children) {
return this.status === FINISHED;
} else {
var allLoaded = true;
each(this.children, function(k, child) {
if (!child.isLoaded()) {
allLoaded = false;
}
});
return allLoaded;
}
};
// Take all of the deferred scripts that belong to this
// dependency and inject them into the page.
Resource.prototype.insertDeferredScripts = function() {
each(this.children, function(k, resource) {
if (!_deferredScripts[resource.name]) {
return;
}
var response = _deferredScripts[resource.name];
var se = document.createElement('script');
document.getElementsByTagName('head')[0].appendChild(se);
se.text = response;
// No longer queued
_deferredScripts[resource.name] = null;
});
};
//-----------------------------------------------------
// Public Methods
//=====================================================
var Malt = {};
Malt.module = function() {
var name = arguments[0];
var urls = [];
var callback = arguments[arguments.length - 1];
if (typeof callback !== 'function') {
callback = null;
urls = makeArray(arguments).slice(1);
} else {
urls = makeArray(arguments).slice(1, -1);
}
// Just need to create a resource with the given name. It will
// register itself for later retrieval.
new Resource(name, urls, callback);
};
Malt.require = function() {
var urls = makeArray(arguments).slice(0, -1);
var callback = arguments[arguments.length - 1];
// If a sole parameter was passed
if (typeof callback === 'string') {
urls = [callback];
callback = null;
}
(new Resource(null, urls, callback)).load();
};
Malt.reset = function() {
_deferredScripts = {};
_resourceMap = {};
_log = [];
};
Malt.getLog = function() {
return _log;
};
return Malt;
})();