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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | 2x 2x 3x 6x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | 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()) { Iif ($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.` ); } Iif ($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 getTestClientBootConfig(initialAppConfigFunctions) { let root = _getRoot(); $IMA.$Debug = true; root.$Debug = $IMA.$Debug; let bootConfig = { services: { respond: null, request: null, $IMA: $IMA, dictionary: { $Language: $IMA.$Language, dictionary: $IMA.i18n }, router: { $Host: $IMA.$Host, $Root: $IMA.$Root, $Path: $IMA.$Path, $LanguagePartPath: $IMA.$LanguagePartPath } }, settings: { $Env: 'dev', $Language: 'en', $Protocol: 'http:', $Debug: $IMA.$Debug, $App: {} }, plugins: [] }; return Object.assign( bootConfig, initialAppConfigFunctions, getInitialPluginConfig(), getInitialImaConfigFunctions() ); } function bootClientApp(app, bootConfig) { app.bootstrap.run(bootConfig); 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.getController(), currentRoute.getView(), 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(); Eif (_isClient()) { // hack for the Chrome browser, which sometimes has problems with // rendering the page document.body.style.display = 'none'; document.body.offsetHeight; //eslint-disable-line document.body.style.display = ''; } //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); } function reviveTestClientApp(initialAppConfigFunctions) { vendorLinker.bindToNamespace(ns); let root = _getRoot(); let app = createImaApp(); let bootConfig = getTestClientBootConfig(initialAppConfigFunctions); app = bootClientApp(app, bootConfig); root.ns = ns; root.oc = app.oc; } function onLoad(callback) { //TODO remove @0.15.0 if ($IMA.$Debug && typeof callback === 'function') { throw new Error( `The onLoad method use promise pattern instead of callback ` + `pattern. Update your app/main.js file.` ); } vendorLinker.bindToNamespace(ns); if (!_isClient()) { return Promise.reject(null); } return new Promise((resolve, reject) => { if ( document.readyState === 'complete' || document.readyState === 'interactive' ) { $IMA.Loader .initAllModules() .then(resolve) .catch(error => { reject(error); }); } else { window.addEventListener('DOMContentLoaded', () => { $IMA.Loader .initAllModules() .then(resolve) .catch(error => { reject(error); }); }); } }); } export { getInitialImaConfigFunctions, getNamespace, getInitialPluginConfig, createImaApp, getClientBootConfig, getTestClientBootConfig, bootClientApp, routeClientApp, hotReloadClientApp, reviveClientApp, reviveTestClientApp, onLoad }; |