-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptr.js
More file actions
329 lines (289 loc) · 10.7 KB
/
scriptr.js
File metadata and controls
329 lines (289 loc) · 10.7 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
(function () {
"use strict";
var doc = window.document,
head = doc.getElementsByTagName('head')[0],
a = doc.createElement("a"); //for resolving urls
var debug = function (e) {
if (require.debug && console) { console.log(e); }
};
var SELF_URL = (function () {
var script_tags = document.getElementsByTagName('script'),
path = script_tags[script_tags.length - 1].src;
path = path.match(/(.*\/).*\.js/);
return path[1];
})(), Global_Path = SELF_URL;
function isArray (obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
}
function isURL (url) {
if (/^(f|ht)tps?:\/\//i.test(url)) { return true; }
return false;
}
function resolveUri (uri, parent) {
var fullURI;
if (uri.indexOf('/') === 0){
fullURI = uri;
} else {
fullURI = parent + uri;
}
var resolved;
a.href = fullURI;
resolved = a.href;
return resolved;
}
function Module (id, cb, parent) {
this.id = id;
this.exports = {};
this.cb = [cb];
this.parent = parent;
if (parent && parent.children) {
parent.children.push(this);
}
this.filename = null;
this.loaded = false;
this.fired = false;
this.children = [];
}
var __require = Module.prototype.require = function (path,cb) {
return Module._load(path, cb, this);
};
Module._cache = {};
Module._Native = {};
Module._resolveFilename = function (request, parent) {
var path = '';
var _imports = [];
if (typeof request === 'object'){
_imports = request.imports || [];
request = request.file;
}
if (Module._Native[request]) {
request = Module._Native[request];
if (typeof request === 'object'){
_imports = request.imports;
request = request.file;
}
if (!isURL(request)) { path = Global_Path; }
} else if (isURL(request)) {
//nothing to do
} else if (parent && parent.id) {
path = resolveUri('./../', parent.id);
} else {
path = Global_Path;
}
return {
file : resolveUri(request, path),
imports : _imports
};
};
var definedObjects = [];
window.define = function () {
var args = [];
Array.prototype.push.apply( args, arguments );
var cb = args.pop();
var requireList = [];
if (typeof cb !== 'function') {
throw new Error("define must has a cb function as last argument");
}
if (isArray(args[0])) {
requireList = args[0];
} else if (args[0] && typeof args[0] === 'string'){
requireList = [args[0]];
}
var hasExportsRequire = [];
var list = [];
for (var i = 0; i < requireList.length; i++){
if (requireList[i] === 'require' || requireList[i] === 'exports'){
hasExportsRequire[i] = requireList[i];
} else {
list.push(requireList[i]);
}
}
if (hasExportsRequire.length){
requireList = list;
var _cb = cb;
cb = function(){
var self = this;
var args = [];
Array.prototype.push.apply( args, arguments );
for (var i =0; i < hasExportsRequire.length; i++){
var name = hasExportsRequire[i];
if (name){
if ( args[i]){ args[i+1] = args[i]; }
args.splice(i, 1, self[name]);
}
}
_cb.apply(this, args);
};
}
definedObjects.push({
cb : cb,
list : requireList
});
};
Module.prototype.load = function (filename) {
var self = this;
debug('load ' + JSON.stringify(filename) +
' for module ' + JSON.stringify(this.id));
if(this.loaded) { throw('should not be loaded yet!'); }
this.filename = filename;
var $require = function () {
return __require.apply(self, arguments);
};
var el = doc.createElement('script');
el.onload = el.onerror = el.onreadystatechange = function () {
if ((el.readyState && el.readyState !== "complete" &&
el.readyState !== "loaded") || self.loaded ){
return false;
}
self.loaded = true;
el.onload = el.onreadystatechange = null;
var _run = function () {
self.require = $require;
var _fireNestedCb = function () {
var imports = self.imports;
var importLen = imports.length;
if (importLen > 0){
for (var i = 0; i < importLen; i++){
var toImport = imports[i];
if (window[toImport]){
if (importLen == 1){
self.exports = window[toImport];
} else {
self.exports[toImport] = window[toImport];
}
delete [window[toImport]];
}
}
}
if (self.children.length) {
var childs = self.children;
var allcalled = true;
for (var y = 0; y < childs.length; y++){
if (!childs[y].fired) {
allcalled = false;
break;
}
}
if (!allcalled && !self.callNow) {
setTimeout(function () {
_fireNestedCb();
},25);
return;
}
}
if (!self.fired) {
for (var x = 0; x < self.cb.length; x++){
var _cb = self.cb[x];
if (typeof _cb === 'function') {
_cb(self.exports);
}
}
self.fired = true;
}
};
var definedObject = definedObjects.shift();
if (definedObject) {
var callback = definedObject.cb;
var list = definedObject.list;
if (list.length) {
var e = [], nested = function () {
Array.prototype.push.apply( e, arguments );
if (list.length) {
var mid = list.shift();
var cb = list.length ? nested : function () {
Array.prototype.push.apply( e, arguments );
var args = [].concat(e);
callback.apply(self, args);
_fireNestedCb();
};
Module._load(mid, cb, self);
}
};
nested();
return;
}
callback.apply(self);
}
_fireNestedCb();
};
_run();
return true;
};
el.async = true;
if (require.noCache){
el.src = filename + "?" + Date.now();
} else {
el.src = filename;
}
head.insertBefore(el, head.lastChild);
};
Module._load = function (request, cb, parent) {
if (!cb){ cb = function(){}; }
if (isArray(request)) {
var e = [], nested = function () {
Array.prototype.push.apply( e, arguments );
if (request.length) {
var id = request.shift();
var callback = request.length ? nested : function () {
Array.prototype.push.apply( e, arguments );
cb.apply({}, e);
};
Module._load(id, callback, parent);
}
};
nested();
return e;
}
if (parent) {
debug('Module._load REQUEST ' + (request) +
' parent: ' + parent.id);
}
var obj = Module._resolveFilename(request, parent);
var filename = obj.file;
var imports = obj.imports || [];
var cachedModule = Module._cache[filename];
if (cachedModule) {
debug("Loading " + filename + " from cache");
if (typeof cb === 'function') {
if (cachedModule.fired === true) {
cb(cachedModule.exports);
} else {
if (parent && parent.parent &&
parent.parent.id === cachedModule.id) {
debug("WARNING: Circular Dependency Detected at " +
cachedModule.id + ' from ' + parent.id);
cb(cachedModule.exports);
} else {
cachedModule.cb.push(cb);
}
}
}
return cachedModule.exports;
}
var module = new Module(filename, cb, parent);
module.imports = imports;
Module._cache[filename] = module;
module.load(filename);
return module.exports;
};
var require = function (path, cb) {
return Module._load(path,cb);
};
require.Path = function (path) {
var old = Global_Path;
var self_path = isURL(path) ? '' : SELF_URL;
Global_Path = resolveUri(path, self_path);
debug("PATH: Change Global Path From " + old + " to " + Global_Path);
};
require.scriptr = true;
require.noCache = false;
require.debug = false;
require.Register = function (obj) {
for (var property in obj){
if (obj.hasOwnProperty(property)) {
Module._Native[property] = obj[property];
}
}
};
window.require = require;
}());