UNPKG

8.01 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.onLoad = exports.reviveTestClientApp = exports.reviveClientApp = exports.hotReloadClientApp = exports.routeClientApp = exports.bootClientApp = exports.getTestClientBootConfig = exports.getClientBootConfig = exports.createImaApp = exports.getInitialPluginConfig = exports.getNamespace = exports.getInitialImaConfigFunctions = undefined;
7
8var _vendorLinker = require('./vendorLinker');
9
10var _vendorLinker2 = _interopRequireDefault(_vendorLinker);
11
12var _namespace = require('./namespace');
13
14var _namespace2 = _interopRequireDefault(_namespace);
15
16var _ObjectContainer = require('./ObjectContainer');
17
18var _ObjectContainer2 = _interopRequireDefault(_ObjectContainer);
19
20var _Bootstrap = require('./Bootstrap');
21
22var _Bootstrap2 = _interopRequireDefault(_Bootstrap);
23
24var _bind = require('./config/bind');
25
26var _bind2 = _interopRequireDefault(_bind);
27
28var _services = require('./config/services');
29
30var _services2 = _interopRequireDefault(_services);
31
32function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
34function getInitialImaConfigFunctions() {
35 return { initBindIma: _bind2.default, initServicesIma: _services2.default };
36}
37
38function getNamespace() {
39 return _namespace2.default;
40}
41
42function getInitialPluginConfig() {
43 return { plugins: _vendorLinker2.default.getImaPlugins() };
44}
45
46function _getRoot() {
47 return _isClient() ? window : global;
48}
49
50function _isClient() {
51 return typeof window !== 'undefined' && window !== null;
52}
53
54function createImaApp() {
55 let oc = new _ObjectContainer2.default(_namespace2.default);
56 let bootstrap = new _Bootstrap2.default(oc);
57
58 return { oc, bootstrap };
59}
60
61function getClientBootConfig(initialAppConfigFunctions) {
62 let root = _getRoot();
63
64 if ($Debug && _isClient()) {
65 if ($IMA.$Protocol !== root.location.protocol) {
66 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.`);
67 }
68
69 if ($IMA.$Host !== root.location.host) {
70 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.`);
71 }
72 }
73
74 let bootConfig = {
75 services: {
76 respond: null,
77 request: null,
78 $IMA: $IMA,
79 dictionary: {
80 $Language: $IMA.$Language,
81 dictionary: $IMA.i18n
82 },
83 router: {
84 $Protocol: $IMA.$Protocol,
85 $Host: $IMA.$Host,
86 $Path: $IMA.$Path,
87 $Root: $IMA.$Root,
88 $LanguagePartPath: $IMA.$LanguagePartPath
89 }
90 },
91 settings: {
92 $Debug: $IMA.$Debug,
93 $Env: $IMA.$Env,
94 $Version: $IMA.$Version,
95 $App: $IMA.$App,
96 $Protocol: $IMA.$Protocol,
97 $Language: $IMA.$Language,
98 $Host: $IMA.$Host,
99 $Path: $IMA.$Path,
100 $Root: $IMA.$Root,
101 $LanguagePartPath: $IMA.$LanguagePartPath
102 }
103 };
104
105 return Object.assign(bootConfig, initialAppConfigFunctions, getInitialPluginConfig(), getInitialImaConfigFunctions());
106}
107
108function getTestClientBootConfig(initialAppConfigFunctions) {
109 let root = _getRoot();
110 $IMA.$Debug = true;
111 root.$Debug = $IMA.$Debug;
112
113 let bootConfig = {
114 services: {
115 respond: null,
116 request: null,
117 $IMA: $IMA,
118 dictionary: {
119 $Language: $IMA.$Language,
120 dictionary: $IMA.i18n
121 },
122 router: {
123 $Host: $IMA.$Host,
124 $Root: $IMA.$Root,
125 $Path: $IMA.$Path,
126 $LanguagePartPath: $IMA.$LanguagePartPath
127 }
128 },
129 settings: {
130 $Env: 'dev',
131 $Language: 'en',
132 $Protocol: 'http:',
133 $Debug: $IMA.$Debug,
134 $App: {}
135 },
136 plugins: []
137 };
138
139 return Object.assign(bootConfig, initialAppConfigFunctions, getInitialPluginConfig(), getInitialImaConfigFunctions());
140}
141
142function bootClientApp(app, bootConfig) {
143 app.bootstrap.run(bootConfig);
144
145 $IMA.$Dispatcher = app.oc.get('$Dispatcher');
146
147 let cache = app.oc.get('$Cache');
148 cache.deserialize($IMA.Cache || {});
149
150 return app;
151}
152
153function routeClientApp(app) {
154 let router = app.oc.get('$Router');
155
156 return router.listen().route(router.getPath()).catch(error => {
157 if (typeof $IMA.fatalErrorHandler === 'function') {
158 $IMA.fatalErrorHandler(error);
159 } else {
160 console.warn('Define function config.$IMA.fatalErrorHandler in ' + 'services.js.');
161 }
162 });
163}
164
165function hotReloadClientApp(initialAppConfigFunctions) {
166 if (!$Debug) {
167 return;
168 }
169
170 let app = createImaApp();
171 let bootConfig = getClientBootConfig(initialAppConfigFunctions);
172 app = bootClientApp(app, bootConfig);
173
174 let router = app.oc.get('$Router');
175 let pageManager = app.oc.get('$PageManager');
176 let currentRouteInfo = router.getCurrentRouteInfo();
177 let currentRoute = currentRouteInfo.route;
178 let currentRouteOptions = Object.assign({}, currentRoute.getOptions(), {
179 onlyUpdate: false,
180 autoScroll: false,
181 allowSPA: false
182 });
183
184 router.listen();
185
186 try {
187 return pageManager.manage(currentRoute.getController(), currentRoute.getView(), currentRouteOptions, currentRouteInfo.params).catch(error => {
188 return router.handleError({ error });
189 }).catch(error => {
190 if (typeof $IMA.fatalErrorHandler === 'function') {
191 $IMA.fatalErrorHandler(error);
192 } else {
193 console.warn('Define the config.$IMA.fatalErrorHandler function ' + 'in services.js.');
194 }
195 });
196 } catch (error) {
197 return router.handleError({ error });
198 }
199}
200
201function reviveClientApp(initialAppConfigFunctions) {
202 let root = _getRoot();
203
204 //set React for ReactJS extension for browser
205 root.React = _vendorLinker2.default.get('react');
206 root.$Debug = root.$IMA.$Debug;
207
208 let app = createImaApp();
209 let bootConfig = getClientBootConfig(initialAppConfigFunctions);
210 app = bootClientApp(app, bootConfig);
211
212 return routeClientApp(app);
213}
214
215function reviveTestClientApp(initialAppConfigFunctions) {
216 _vendorLinker2.default.bindToNamespace(_namespace2.default);
217
218 let root = _getRoot();
219 let app = createImaApp();
220 let bootConfig = getTestClientBootConfig(initialAppConfigFunctions);
221
222 app = bootClientApp(app, bootConfig);
223
224 root.ns = _namespace2.default;
225 root.oc = app.oc;
226}
227
228function onLoad() {
229 _vendorLinker2.default.bindToNamespace(_namespace2.default);
230
231 if (!_isClient()) {
232 return Promise.reject(null);
233 }
234
235 return Promise.resolve();
236}
237
238exports.getInitialImaConfigFunctions = getInitialImaConfigFunctions;
239exports.getNamespace = getNamespace;
240exports.getInitialPluginConfig = getInitialPluginConfig;
241exports.createImaApp = createImaApp;
242exports.getClientBootConfig = getClientBootConfig;
243exports.getTestClientBootConfig = getTestClientBootConfig;
244exports.bootClientApp = bootClientApp;
245exports.routeClientApp = routeClientApp;
246exports.hotReloadClientApp = hotReloadClientApp;
247exports.reviveClientApp = reviveClientApp;
248exports.reviveTestClientApp = reviveTestClientApp;
249exports.onLoad = onLoad;
250
251typeof $IMA !== 'undefined' && $IMA !== null && $IMA.Loader && $IMA.Loader.register('ima/main', [], function (_export, _context) {
252 'use strict';
253 return {
254 setters: [],
255 execute: function () {
256 _export('onLoad', exports.onLoad);
257 _export('getInitialImaConfigFunctions', exports.getInitialImaConfigFunctions);
258 _export('getNamespace', exports.getNamespace);
259 _export('getInitialPluginConfig', exports.getInitialPluginConfig);
260 _export('createImaApp', exports.createImaApp);
261 _export('getClientBootConfig', exports.getClientBootConfig);
262 _export('getTestClientBootConfig', exports.getTestClientBootConfig);
263 _export('bootClientApp', exports.bootClientApp);
264 _export('routeClientApp', exports.routeClientApp);
265 _export('hotReloadClientApp', exports.hotReloadClientApp);
266 _export('reviveClientApp', exports.reviveClientApp);
267 _export('reviveTestClientApp', exports.reviveTestClientApp);
268 _export('onLoad', exports.onLoad);
269 }
270 };
271});