forked from bazaarvoice/bv-ui-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
244 lines (204 loc) · 6.91 KB
/
index.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
/* eslint no-use-before-define: 0 */
/**
* @fileOverview
* Load JavaScript and CSS at runtime.
*
* This module is derived from yepnope.js 2.0.0:
* Copyright (c) 2014, Alex Sexton
* Copyright (c) 2014, Ralph Holzmann
* All rights reserved.
* New BSD license:
* https://github.com/SlexAxton/yepnope.js/blob/master/LICENSE.md
*
* @module
*/
var global = require('../global');
var doc = global.document;
var DEFAULT_TIMEOUT = 10000;
function getFirstScript () {
return doc.getElementsByTagName('script')[0];
}
function isFileReady (readyState) {
// the file is ready if there is no readyState, or if it has reached a
// terminal state.
return !readyState ||
readyState === 'loaded' ||
readyState === 'complete' ||
readyState === 'uninitialized';
}
function validateArguments (url, options, callback) {
if (!url || typeof url !== 'string') {
throw new Error('`url` must be a string');
}
if (typeof options.timeout !== 'number') {
throw new Error('`options.timeout` must be a number');
}
if (callback && typeof callback !== 'function') {
throw new Error('`callback` must be a function');
}
}
module.exports = {
/**
* Load a script from a URL
*
* The script is loaded on a later turn of the event loop. If a callback is
* provided, it will be invoked on success in all supported browsers. The
* callback will execute after the script.
*
* In modern IE and other browsers, the callback will be invoked with an error
* on failure as well. A timeout in milliseconds may be provided as
* `options.timeout`. If this timeout expires before loading fails or
* succeeds, the callback will be invoked with an error.
*
* @param {string} url - URL of the script
* @param {object} [options]
* @param {object} options.attributes - an object containing key/value pairs
* to be used as attributes on the created `script` element (optional)
* @param {number} options.timeout - timeout in milliseconds (defaults to
* 10000ms)
* @param {function} [callback] - node-style callback
*/
loadScript: function (url, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
options.timeout = options.timeout || DEFAULT_TIMEOUT;
validateArguments(url, options, callback);
var script = doc.createElement('script');
var done = false;
var attr;
if (options.attributes) {
for (attr in options.attributes) {
script.setAttribute(attr, options.attributes[attr]);
}
}
function cleanUp (err) {
done = true;
clearTimeout(timeoutHandle);
script.onload = script.onreadystatechange = script.onerror = null;
script.parentNode.removeChild(script);
if (typeof callback === 'function') {
callback(err);
}
}
script.onreadystatechange = script.onload = function () {
if (done || !isFileReady(script.readyState)) {
return;
}
cleanUp();
};
script.onerror = function () {
if (done) {
return;
}
cleanUp(new Error('Error: could not load ' + url));
};
// We should be able to catch most errors with onerror, but for browsers
// that don't support onerror we provide the below timeout cb
var timeoutHandle = setTimeout(function () {
if (done) {
return;
}
cleanUp(new Error('Error: script timeout ' + url));
}, options.timeout);
// If a script is cached, IE may execute it immediately, which breaks
// JavaScript's run-to-completion semantics. So, don't try to load the
// script until a later turn of the event loop.
//
// See http://www.guypo.com/ies-premature-execution-problem/
setTimeout(function () {
script.src = url;
var firstScript = getFirstScript();
firstScript.parentNode.insertBefore(script, firstScript);
}, 0);
},
/**
* Load a stylesheet from a URL
*
* The stylesheet is loaded on a later turn of the event loop. If a callback
* is provided, it will be invoked on success in all supported browsers.
*
* In modern IE and other browsers, the callback will be invoked with an error
* on failure as well. A timeout in milliseconds may be provided as
* `options.timeout`. If this timeout expires before loading fails or
* succeeds, the callback will be invoked with an error.
*
* @param {string} url - URL of the stylesheet
* @param {object} [options]
* @param {object} options.attributes - an object containing key/value pairs
* to be used as attributes on the created `link` element (optional)
* @param {number} options.timeout - timeout in milliseconds (defaults to
* 1000ms)
* @param {element} options.injectionNode - node to inject link element
* into (defaults to parent node of the first script node)
* @param {function} [callback] - node-style callback
*/
loadStyleSheet: function (url, options, callback) {
if (typeof options === 'function') {
callback = options;
options = null;
}
options = options || {};
options.timeout = options.timeout || DEFAULT_TIMEOUT;
validateArguments(url, options, callback);
// This check doesn't happen in `validateArguments` because it's unique
// to `loadStyleSheet`.
if (
'injectionNode' in options &&
!(options.injectionNode instanceof global.Element)
) {
throw new Error('`options.injectionNode` must be a DOM node');
}
var link = doc.createElement('link');
var done = false;
var attr;
if (options.attributes) {
for (attr in options.attributes) {
link.setAttribute(attr, options.attributes[attr]);
}
}
function cleanUp (err) {
done = true;
clearTimeout(timeoutHandle);
link.onload = link.onreadystatechange = link.onerror = null;
if (typeof callback === 'function') {
callback(err);
}
}
link.onreadystatechange = link.onload = function () {
if (done || !isFileReady(link.readyState)) {
return;
}
cleanUp();
};
link.onerror = function () {
if (done) {
return;
}
cleanUp(new Error('Error: could not load ' + url));
};
var timeoutHandle = setTimeout(link.onerror, options.timeout);
// Inject the stylesheet on a later turn of the event loop
setTimeout(function () {
// technique to force non-blocking loading from:
// https://github.com/filamentgroup/loadCSS/blob/master/loadCSS.js#L20
link.media = 'only x';
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = url;
// on next tick, set `media` back
setTimeout(function () {
link.media = 'all';
}, 0);
var injectionNode = options.injectionNode || getFirstScript().parentNode;
try {
injectionNode.appendChild(link);
}
catch (error) {
cleanUp(new Error('Error: could not append LINK element'));
}
}, 0);
}
};