This repository has been archived by the owner on Dec 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnautilus.js
126 lines (113 loc) · 2.43 KB
/
nautilus.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
(function(root, factory) {
root.nautilus = factory;
}(this, function nautilus() {
var self = this,
hasOwn = Object.prototype.hasOwnProperty;
var uPaths = {};
var _ = {
extends: function(a, b, undefOnly) {
for (var prop in b) {
if (hasOwn.call(b, prop)) {
if (prop !== "constructor" || a !== global) {
if (b[prop] === undefined) {
delete a[prop];
} else if (!(undefOnly && typeof a[prop] !== "undefined")) {
a[prop] = b[prop];
}
}
}
}
return a;
},
merge: function(obj1, obj2) {
var obj3 = {};
for (var attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (var attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
}
}
var queue = {
queues: [],
push: function(lenPaths, fn) {
this.queues.push({
paths: lenPaths,
loaded: 0,
exec: fn
});
return this.queues.length - 1;
},
incr: function(queueIndex) {
var curr = this.queues[queueIndex];
curr.loaded += 1;
if (curr.paths <= curr.loaded) {
if (typeof curr.exec === 'function') {
curr.exec();
}
}
},
reset: function() {
this.queues = [];
}
}
function loadScript(path, currentQueue) {
var scr = document.createElement('script');
scr.type = 'text/javascript';
scr.onload = handleLoad;
scr.async = true;
scr.onreadystatechange = handleReadyStateChange;
scr.onerror = handleError;
scr.src = path;
document.head.appendChild(scr);
function handleLoad() {
queue.incr(currentQueue);
}
function handleReadyStateChange() {
if (scr.readyState === 'complete') {
handleLoad();
}
}
function handleError() {
console.warn(
'[nautilus] occurred an error while fetching',
path
);
}
}
function fetchBuiltIn(arr) {
fetch.apply(this, arr)
}
function fetch() {
var args = Array.prototype.slice.call(arguments);
var paths = args[0];
if (typeof(paths) === 'string') {
paths = [paths];
}
if (Object.prototype.toString.call(args[1]) === '[object Array]') {
args[1] = fetchBuiltIn.bind(this, args.slice(1, args.length));
}
var q = queue.push(paths.length, args[1]);
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
loadScript(uPaths[path] || path, q);
}
}
this.config = function(settings) {
if (typeof(settings.paths) === 'object') {
uPaths = _.merge(uPaths, settings.paths);
}
}
this.getConfig = function() {
return {
paths: uPaths
}
}
this.resetConfig = function() {
uPaths = {};
queue.reset();
}
return _.extends(fetch.bind(this), this);
}()));