This repository has been archived by the owner on Jun 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.js
203 lines (171 loc) · 4.69 KB
/
main.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
import vendorLinker from './vendorLinker';
import ns from './namespace';
import ObjectContainer from './ObjectContainer';
import Bootstrap from './Bootstrap';
import initBindIma from './config/bind';
import initServicesIma from './config/services';
function getInitialImaConfigFunctions() {
return { initBindIma, initServicesIma };
}
function getNamespace() {
return ns;
}
function getInitialPluginConfig() {
return { plugins: vendorLinker.getImaPlugins() };
}
function _getRoot() {
return _isClient() ? window : global;
}
function _isClient() {
return typeof window !== 'undefined' && window !== null;
}
function createImaApp() {
let oc = new ObjectContainer(ns);
let bootstrap = new Bootstrap(oc);
return { oc, bootstrap };
}
function getClientBootConfig(initialAppConfigFunctions) {
let root = _getRoot();
if ($Debug && _isClient()) {
if ($IMA.$Protocol !== root.location.protocol) {
throw new Error(
`Your client's protocol is not same as server's protocol. ` +
`For right setting protocol on the server site set ` +
`'X-Forwarded-Proto' header.`
);
}
if ($IMA.$Host !== root.location.host) {
throw new Error(
`Your client's host is not same as server's host. For right ` +
`setting host on the server site set 'X-Forwarded-Host' ` +
`header.`
);
}
}
let bootConfig = {
services: {
respond: null,
request: null,
$IMA: $IMA,
dictionary: {
language: $IMA.$Language,
dictionary: $IMA.i18n
},
router: {
$Protocol: $IMA.$Protocol,
$Host: $IMA.$Host,
$Path: $IMA.$Path,
$Root: $IMA.$Root,
$LanguagePartPath: $IMA.$LanguagePartPath
}
},
settings: {
$Debug: $IMA.$Debug,
$Env: $IMA.$Env,
$Version: $IMA.$Version,
$App: $IMA.$App,
$Protocol: $IMA.$Protocol,
$Language: $IMA.$Language,
$Host: $IMA.$Host,
$Path: $IMA.$Path,
$Root: $IMA.$Root,
$LanguagePartPath: $IMA.$LanguagePartPath
}
};
return Object.assign(
bootConfig,
initialAppConfigFunctions,
getInitialPluginConfig(),
getInitialImaConfigFunctions()
);
}
function bootClientApp(app, bootConfig) {
app.bootstrap.run(bootConfig);
$IMA.$Dispatcher = app.oc.get('$Dispatcher');
let cache = app.oc.get('$Cache');
cache.deserialize($IMA.Cache || {});
return app;
}
function routeClientApp(app) {
let router = app.oc.get('$Router');
return router
.listen()
.route(router.getPath())
.catch(error => {
if (typeof $IMA.fatalErrorHandler === 'function') {
$IMA.fatalErrorHandler(error);
} else {
console.warn(
'Define function config.$IMA.fatalErrorHandler in ' + 'services.js.'
);
}
});
}
function hotReloadClientApp(initialAppConfigFunctions) {
if (!$Debug) {
return;
}
let app = createImaApp();
let bootConfig = getClientBootConfig(initialAppConfigFunctions);
app = bootClientApp(app, bootConfig);
let router = app.oc.get('$Router');
let pageManager = app.oc.get('$PageManager');
let currentRouteInfo = router.getCurrentRouteInfo();
let currentRoute = currentRouteInfo.route;
let currentRouteOptions = Object.assign({}, currentRoute.getOptions(), {
onlyUpdate: false,
autoScroll: false,
allowSPA: false
});
router.listen();
try {
return pageManager
.manage(currentRoute, currentRouteOptions, currentRouteInfo.params)
.catch(error => {
return router.handleError({ error });
})
.catch(error => {
if (typeof $IMA.fatalErrorHandler === 'function') {
$IMA.fatalErrorHandler(error);
} else {
console.warn(
'Define the config.$IMA.fatalErrorHandler function ' +
'in services.js.'
);
}
});
} catch (error) {
return router.handleError({ error });
}
}
function reviveClientApp(initialAppConfigFunctions) {
let root = _getRoot();
//set React for ReactJS extension for browser
root.React = vendorLinker.get('react');
root.$Debug = root.$IMA.$Debug;
let app = createImaApp();
let bootConfig = getClientBootConfig(initialAppConfigFunctions);
app = bootClientApp(app, bootConfig);
return routeClientApp(app).then(pageInfo => {
return Object.assign({}, pageInfo || {}, { app, bootConfig });
});
}
function onLoad() {
vendorLinker.bindToNamespace(ns);
if (!_isClient()) {
return Promise.reject(null);
}
return Promise.resolve();
}
export {
getInitialImaConfigFunctions,
getNamespace,
getInitialPluginConfig,
createImaApp,
getClientBootConfig,
bootClientApp,
routeClientApp,
hotReloadClientApp,
reviveClientApp,
onLoad
};