-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbootstrap.js
172 lines (154 loc) · 4.66 KB
/
bootstrap.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
/**
* Copyright 2010 Scott Koon
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
var bootstrap = (function() {
var itemsLoaded = [];
// maintain a list of items we have already loaded to prevent duplicates
var bootstrap = function() {
var args = arguments;
if (args.length == 0)
return;
var scriptSrc = "";
// figure out if we were passed a script URL, a config object, or an
// array of script URLs
var src = args[0];
if (typeof src === "string") {
scriptSrc = src;
} else if (typeof src == "object" || typeof src == "array") {
processConfigObject(src);
}
var head = document.head || document.getElementsByTagName("head")[0];
var scriptId = args[1] || "script" + Math.floor(Math.random() * 1234);
var loaded = false;
var scriptTag = null;
var callback = callback = args[2] || null;
function notLoaded(src, type) {
var nodeList;
var propToCheck;
var i = 0, l = itemsLoaded.length;
while (i < l) {
if (itemsLoaded[i] == scriptSrc)
return false;
i++
}
// last ditch effort to see if the script we are trying to load
// was already loaded using a static script tag.
if (type === "js") {
var nodeList = document.getElementsByTagName("script");
propToCheck = "src";
} else if (type === "css") {
var nodeList = document.getElementsByTagName("link");
propToCheck = "href";
}
var nodeArray = (function() {
var a = [];
for (var c = nodeList.length; c-- > 0;) {
if (c in nodeList) {
a.push(nodeList.c)
}
}
return a
});
for (var c = 0, len = nodeArray.length; c < len; c++) {
if ((propToCheck in nodeArray[c])
&& nodeArray[c][propToCheck] == src)
return false;
}
return true;
}
if (scriptSrc.indexOf(".js") > -1 && notLoaded(scriptSrc, "js")) {
itemsLoaded.push(scriptSrc);
scriptTag = createScriptElement(scriptSrc, scriptId);
}
if (scriptSrc.indexOf(".css") > -1 && notLoaded(scriptSrc, "css")) {
itemsLoaded.push(scriptSrc);
scriptTag = createCssElement(scriptSrc, scriptId);
}
if (scriptTag) {
scriptTag.onload = function() {
loaded = true;
}
scriptTag.onreadystatechange = function() {
if (!loaded) {
if (this.readyState == "loaded"
|| this.readyState == "complete") {
loaded = true;
}
}
}
head.appendChild(scriptTag);
}
function makeCallback() {
window.clearInterval(id);
if (callback != null) {
callback();
}
}
var id = window.setInterval(function() {
if (loaded) {
makeCallback();
}
}, 0)
}
function createScriptElement(scriptSrc, scriptId) {
var scriptTag = document.createElementNS(
'http://www.w3.org/1999/xhtml', 'html:script')
// document.createElement('script');
scriptTag.setAttribute("type", "text/javascript");
scriptTag.src = scriptSrc;
scriptTag.id = scriptId;
scriptTag.async = false;
return scriptTag;
}
function createCssElement(scriptSrc, scriptId) {
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = scriptSrc;
link.id = scriptId;
return link;
}
function processConfigObject(config) {
for ( var script in config) {
bootstrap(config[script], script);
}
;
}
return (window.bootstrap = window.$b = bootstrap);
}());
/**
* Bootstrap is a small library for dynamically loading JavaScript files. It's
* primary use case is for using in your JS code to load JS files only when they
* are used. syntax:
*
* bootstrap("script file name.js","id you want the script element to have");
*
* or
*
* $b("script file name.js","id you want the script element to have");
*
* You can also not pass in an id and Bootstrap will assign a random one to your
* script tag
*
* alternatively, you can pass in a call back function. $b("script file
* name.js", "id of your element", callBackFunction);
*
* The ID element is also optional if you are not passing in a call back.
*
* If you'd like to specify multiple files, you can either pass in an array
* containing the names/urls of the scripts you would like to load or you can
* pass in a config object using the following format: { "{id for the script
* tag}" : "{name or url for the script}" }
*/