-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdeferred-bootstrap.js
184 lines (154 loc) · 5.01 KB
/
deferred-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
173
174
175
176
177
178
179
180
181
182
183
184
'use strict';
var isObject = angular.isObject,
isFunction = angular.isFunction,
isArray = angular.isArray,
isString = angular.isString,
forEach = angular.forEach,
loadingClass = 'deferred-bootstrap-loading',
errorClass = 'deferred-bootstrap-error',
bodyElement,
$q;
function addLoadingClass () {
bodyElement.addClass(loadingClass);
}
function removeBodyClasses () {
bodyElement.removeClass(loadingClass);
bodyElement.removeClass(errorClass);
}
function addErrorClass () {
removeBodyClasses();
bodyElement.addClass(errorClass);
}
function isPromise (value) {
return isObject(value) && isFunction(value.then);
}
function checkConfig (config) {
if (!isObject(config)) {
throw new Error('Bootstrap configuration must be an object.');
}
if (!isString(config.module)) {
throw new Error('\'config.module\' must be a string.');
}
if (config.resolve && config.moduleResolves) {
throw new Error('Bootstrap configuration can contain either \'resolve\' or \'moduleResolves\' but not both');
}
if (config.resolve) {
if (!isObject(config.resolve)) {
throw new Error('\'config.resolve\' must be an object.');
}
}
if (config.bootstrapConfig) {
if (!isObject(config.bootstrapConfig)) {
throw new Error('\'config.bootstrapConfig\' must be an object.');
}
}
if (config.moduleResolves) {
if (!isArray(config.moduleResolves)) {
throw new Error('\'config.moduleResolves\' must be an array.');
}
}
forEach(config.moduleResolves, function (moduleResolve) {
if (!moduleResolve.module) {
throw new Error('A \'moduleResolve\' configuration item must contain a \'module\' name.');
}
if (!isObject(moduleResolve.resolve)) {
throw new Error('\'moduleResolve.resolve\' must be an object.');
}
});
if (angular.isDefined(config.onError) && !isFunction(config.onError)) {
throw new Error('\'config.onError\' must be a function.');
}
}
function provideRootElement (modules, element) {
element = angular.element(element);
modules.unshift(['$provide', function($provide) {
$provide.value('$rootElement', element);
}]);
}
function createInjector (injectorModules, element) {
var modules = ['ng'];
if (isString(injectorModules)) {
modules.push(injectorModules);
} else if (isArray(injectorModules)) {
modules = modules.concat(injectorModules);
}
provideRootElement(modules, element);
return angular.injector(modules, element);
}
function doBootstrap (element, module, bootstrapConfig) {
var deferred = $q.defer();
angular.element(document).ready(function () {
angular.bootstrap(element, [module], bootstrapConfig);
removeBodyClasses();
deferred.resolve(true);
});
return deferred.promise;
}
function bootstrap (configParam) {
var config = configParam || {},
element = config.element,
module = config.module,
injectorModules = config.injectorModules || [],
injector,
promises = [],
constants = [],
bootstrapConfig = config.bootstrapConfig;
bodyElement = angular.element(document.body);
addLoadingClass();
checkConfig(config);
injector = createInjector(injectorModules, element);
$q = injector.get('$q');
function callResolveFn (resolveFunction, constantName, moduleName) {
var result;
constants.push({
name: constantName,
moduleName: moduleName || module
});
if (!isFunction(resolveFunction) && !isArray(resolveFunction)) {
throw new Error('Resolve for \'' + constantName + '\' is not a valid dependency injection format.');
}
result = injector.instantiate(resolveFunction);
if (isPromise(result)) {
promises.push(result);
} else {
throw new Error('Resolve function for \'' + constantName + '\' must return a promise.');
}
}
function handleResults (results) {
forEach(results, function (value, index) {
var result = value && value.data ? value.data : value,
moduleName = constants[index].moduleName,
constantName = constants[index].name;
angular.module(moduleName).constant(constantName, result);
});
return doBootstrap(element, module, bootstrapConfig);
}
function handleError (error) {
addErrorClass();
if (isFunction(config.onError)) {
config.onError(error);
}
}
if (config.moduleResolves) {
forEach(config.moduleResolves, function (moduleResolve, index) {
forEach(moduleResolve.resolve, function (resolveFunction, constantName) {
callResolveFn(resolveFunction, constantName, config.moduleResolves[index].module);
});
});
} else {
forEach(config.resolve, function (resolveFunction, constantName) {
callResolveFn(resolveFunction, constantName);
});
}
return $q.all(promises).then(handleResults, handleError);
}
var deferredBootstrapper = {
bootstrap: bootstrap
};
if(typeof define === 'function' && define.amd) {
define([], deferredBootstrapper);
} else if(typeof module === 'object' && module.exports) {
module.exports = deferredBootstrapper;
} else {
window.deferredBootstrapper = deferredBootstrapper;
}