-
Notifications
You must be signed in to change notification settings - Fork 336
/
Copy pathscript.js
131 lines (118 loc) · 3.85 KB
/
script.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
/*!
* $script.js JS loader & dependency manager
* https://github.com/ded/script.js
* (c) Dustin Diaz 2014 | License MIT
*/
(function (name, definition) {
if (typeof module != 'undefined' && module.exports) module.exports = definition()
else if (typeof define == 'function' && define.amd) define(definition)
else this[name] = definition()
})('$script', function () {
var doc = document
, head = doc.getElementsByTagName('head')[0]
, s = 'string'
, f = false
, push = 'push'
, readyState = 'readyState'
, onreadystatechange = 'onreadystatechange'
, list = {}
, ids = {}
, delay = {}
, scripts = {}
, scriptpath
, urlArgs
function every(ar, fn) {
for (var i = 0, j = ar.length; i < j; ++i) if (!fn(ar[i])) return f
return 1
}
function each(ar, fn) {
every(ar, function (el) {
fn(el)
return 1
})
}
function $script(paths, maybeId, maybeOptions, maybeDone) {
paths = paths[push] ? paths : [paths]
var maybeIdIsDone = maybeId && maybeId.call
, maybeIdIsOptions = !maybeIdIsDone && typeof maybeId === 'object'
, maybeOptionsIsDone = !maybeIdIsDone && maybeOptions && maybeOptions.call
, done = maybeIdIsDone ? maybeId : (maybeOptionsIsDone ? maybeOptions : maybeDone)
, id = maybeIdIsDone || maybeIdIsOptions ? paths.join('') : maybeId
, options = maybeIdIsOptions ? maybeId : (maybeOptionsIsDone ? null : maybeOptions)
, queue = paths.length
function loopFn(item) {
return item.call ? item() : list[item]
}
function callback() {
if (!--queue) {
list[id] = 1
done && done()
for (var dset in delay) {
every(dset.split('|'), loopFn) && !each(delay[dset], loopFn) && (delay[dset] = [])
}
}
}
setTimeout(function () {
each(paths, function loading(path, force) {
if (path === null) return callback()
if (!force && !/^https?:\/\//.test(path) && scriptpath) {
path = (path.indexOf('.js') === -1) ? scriptpath + path + '.js' : scriptpath + path;
}
if (scripts[path]) {
if (id) ids[id] = 1
return (scripts[path] == 2) ? callback() : setTimeout(function () { loading(path, true) }, 0)
}
scripts[path] = 1
if (id) ids[id] = 1
create(path, callback, options)
})
}, 0)
return $script
}
function create(path, fn, options) {
var el = doc.createElement('script'), loaded
el.onload = el.onerror = el[onreadystatechange] = function () {
if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) return;
el.onload = el[onreadystatechange] = null
loaded = 1
scripts[path] = 2
fn()
}
el.async = !options || !options.preserveExecutionOrder
el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path;
head.insertBefore(el, head.lastChild)
}
$script.get = create
$script.order = $script.serial = function (scripts, id, done) {
(function callback(s) {
s = scripts.shift()
!scripts.length ? $script(s, id, done) : $script(s, callback)
}())
}
$script.parallel = function (scripts, id, done) {
return $script(scripts, id, { preserveExecutionOrder: true }, done)
}
$script.path = function (p) {
scriptpath = p
}
$script.urlArgs = function (str) {
urlArgs = str;
}
$script.ready = function (deps, ready, req) {
deps = deps[push] ? deps : [deps]
var missing = [];
!each(deps, function (dep) {
list[dep] || missing[push](dep);
}) && every(deps, function (dep) {return list[dep]}) ?
ready() : !function (key) {
delay[key] = delay[key] || []
delay[key][push](ready)
req && req(missing)
}(deps.join('|'))
return $script
}
$script.done = function (idOrDone) {
$script([null], idOrDone)
}
return $script
});