-
Notifications
You must be signed in to change notification settings - Fork 17
/
scriptLoader.js
131 lines (118 loc) · 3.81 KB
/
scriptLoader.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
(function() {
const isProduction = (window.ADAPT_BUILD_TYPE !== 'development');
function loadScript(url, callback) {
if (!url || typeof url !== 'string') return;
const script = document.createElement('script');
script.onload = callback;
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
};
// 0. Keep loadScript code to add into Adapt API later
window.__loadScript = loadScript;
// 2. Setup require for old-style module declarations (some code still uses these), configure paths then load JQuery
function setupRequireJS() {
requirejs.config({
map: {
'*': {
coreJS: 'core/js',
coreViews: 'core/js/views',
coreModels: 'core/js/models',
coreCollections: 'core/js/collections'
}
},
paths: {
'regenerator-runtime': 'libraries/regenerator-runtime.min',
'core-js': 'libraries/core-js.min',
jquery: 'libraries/jquery.min',
underscore: 'libraries/underscore.min',
'underscore.results': 'libraries/underscore.results',
backbone: 'libraries/backbone.min',
'backbone.controller': 'libraries/backbone.controller',
'backbone.controller.results': 'libraries/backbone.controller.results',
'backbone.es6': 'libraries/backbone.es6',
handlebars: 'libraries/handlebars.min',
velocity: 'libraries/velocity.min',
imageReady: 'libraries/imageReady',
inview: 'libraries/inview',
scrollTo: 'libraries/scrollTo.min',
bowser: 'libraries/bowser',
enum: 'libraries/enum',
jqueryMobile: 'libraries/jquery.mobile.custom.min',
react: isProduction ? 'libraries/react.production.min' : 'libraries/react.development',
'react-dom': isProduction ? 'libraries/react-dom.production.min' : 'libraries/react-dom.development',
'html-react-parser': 'libraries/html-react-parser.min',
semver: 'libraries/semver'
},
waitSeconds: 0
});
loadJQuery();
}
// 3. start loading JQuery, wait for it to be loaded
function loadJQuery() {
loadScript('libraries/jquery.min.js', checkJQueryStatus);
}
// 4. Wait until JQuery gets loaded completely then load foundation libraries
function checkJQueryStatus() {
if (window.jQuery === undefined) {
setTimeout(checkJQueryStatus, 100);
} else {
setupModernizr();
}
}
// 5. Backward compatibility for Modernizr
function setupModernizr() {
Modernizr.touch = Modernizr.touchevents;
const touchClass = Modernizr.touch ? 'touch' : 'no-touch';
$('html').addClass(touchClass);
loadFoundationLibraries();
}
// 6. Load foundation libraries and templates then load Adapt itself
function loadFoundationLibraries() {
require([
'handlebars',
'underscore',
'regenerator-runtime',
'core-js',
'underscore.results',
'backbone',
'backbone.controller',
'backbone.controller.results',
'backbone.es6',
'velocity',
'imageReady',
'inview',
'jqueryMobile',
'libraries/jquery.resize',
'scrollTo',
'bowser',
'enum',
'react',
'react-dom',
'html-react-parser',
'semver'
], loadGlobals);
}
// 7. Expose global context libraries
function loadGlobals(Handlebars, _) {
window._ = _;
window.Handlebars = Handlebars;
require([
'events/touch'
], loadTemplates);
}
// 8. Load templates
function loadTemplates() {
require([
'templates'
], loadAdapt);
}
// 9. Allow cross-domain AJAX then load Adapt
function loadAdapt() {
$.ajaxPrefilter(function(options) {
options.crossDomain = true;
});
loadScript('adapt/js/adapt.min.js');
}
// 1. Load requirejs then set it up
loadScript('libraries/require.min.js', setupRequireJS);
})();