|
1 | | -import ConsoleAnalytics from "terriajs/lib/Core/ConsoleAnalytics"; |
2 | | -import GoogleAnalytics from "terriajs/lib/Core/GoogleAnalytics"; |
3 | | -import registerCatalogMembers from "terriajs/lib/Models/Catalog/registerCatalogMembers"; |
4 | | -import registerSearchProviders from "terriajs/lib/Models/SearchProviders/registerSearchProviders"; |
5 | | -import ShareDataService from "terriajs/lib/Models/ShareDataService"; |
6 | | -import Terria from "terriajs/lib/Models/Terria"; |
7 | | -import ViewState from "terriajs/lib/ReactViewModels/ViewState"; |
8 | | -import registerCustomComponentTypes from "terriajs/lib/ReactViews/Custom/registerCustomComponentTypes"; |
9 | | -import updateApplicationOnHashChange from "terriajs/lib/ViewModels/updateApplicationOnHashChange"; |
10 | | -import updateApplicationOnMessageFromParentWindow from "terriajs/lib/ViewModels/updateApplicationOnMessageFromParentWindow"; |
11 | | - |
12 | | -import loadPlugins from "../Core/loadPlugins"; |
13 | | -import showGlobalDisclaimer from "./showGlobalDisclaimer"; |
14 | | -import plugins from "../../plugins"; |
15 | | - |
16 | | -const initTerria = async () => { |
17 | | - console.log("Initializing TerriaJS...", Date.now()); |
18 | | - await import("terriajs/lib/Core/prerequisites"); |
19 | | - |
20 | | - const terriaOptions: ConstructorParameters<typeof Terria>[0] = { |
21 | | - baseUrl: "build/TerriaJS" |
22 | | - }; |
23 | | - |
24 | | - // we check exact match for development to reduce chances that production flag isn't set on builds(?) |
25 | | - if (process.env.NODE_ENV === "development") { |
26 | | - terriaOptions.analytics = new ConsoleAnalytics(); |
27 | | - } else { |
28 | | - terriaOptions.analytics = new GoogleAnalytics(); |
29 | | - } |
30 | | - |
31 | | - // Construct the TerriaJS application, arrange to show errors to the user, and start it up. |
32 | | - const terria = new Terria(terriaOptions); |
33 | | - |
34 | | - // Create the ViewState before terria.start so that errors have somewhere to go. |
35 | | - // @ts-expect-error: test |
36 | | - const viewState = new ViewState({ |
37 | | - terria: terria |
38 | | - }); |
39 | | - |
40 | | - // Register all types of catalog members in the core TerriaJS. If you only want to register a subset of them |
41 | | - // (i.e. to reduce the size of your application if you don't actually use them all), feel free to copy a subset of |
42 | | - // the code in the registerCatalogMembers function here instead. |
43 | | - registerCatalogMembers(); |
44 | | - |
45 | | - // Register custom search providers in the core TerriaJS. If you only want to register a subset of them, or to add your own, |
46 | | - // insert your custom version of the code in the registerSearchProviders function here instead. |
47 | | - registerSearchProviders(); |
48 | | - |
49 | | - // Register custom components in the core TerriaJS. If you only want to register a subset of them, or to add your own, |
50 | | - // insert your custom version of the code in the registerCustomComponentTypes function here instead. |
51 | | - registerCustomComponentTypes(terria); |
52 | | - |
53 | | - if (process.env.NODE_ENV === "development") { |
54 | | - // @ts-expect-error: test |
55 | | - window.viewState = viewState; |
56 | | - } |
57 | | - |
58 | | - await terria |
59 | | - .start({ |
60 | | - applicationUrl: window.location, |
61 | | - configUrl: "config.json", |
62 | | - shareDataService: new ShareDataService({ |
63 | | - terria: terria |
64 | | - }), |
65 | | - beforeRestoreAppState: () => { |
66 | | - // Load plugins before restoring app state because app state may |
67 | | - // reference plugin components and catalog items. |
68 | | - return loadPlugins(viewState, plugins).catch((error) => { |
69 | | - console.error(`Error loading plugins`); |
70 | | - console.error(error); |
71 | | - }); |
72 | | - } |
73 | | - }) |
74 | | - .catch(function (e) { |
75 | | - terria.raiseErrorToUser(e); |
76 | | - }) |
77 | | - .finally(function () { |
78 | | - // Override the default document title with appName. Check first for default |
79 | | - // title, because user might have already customized the title in |
80 | | - // index.ejs |
81 | | - if (document.title === "Terria Map") { |
82 | | - document.title = terria.appName; |
83 | | - } |
84 | | - |
85 | | - // Load init sources like init files and share links |
86 | | - terria.loadInitSources().then((result) => result.raiseError(terria)); |
87 | | - |
88 | | - try { |
89 | | - // Automatically update Terria (load new catalogs, etc.) when the hash part of the URL changes. |
90 | | - updateApplicationOnHashChange(terria, window); |
91 | | - updateApplicationOnMessageFromParentWindow(terria, window); |
92 | | - |
93 | | - // Show a modal disclaimer before user can do anything else. |
94 | | - if (terria.configParameters.globalDisclaimer) { |
95 | | - showGlobalDisclaimer(viewState); |
96 | | - } |
97 | | - |
98 | | - // Add font-imports |
99 | | - const fontImports = terria.configParameters.theme?.fontImports; |
100 | | - if (fontImports) { |
101 | | - const styleSheet = document.createElement("style"); |
102 | | - styleSheet.type = "text/css"; |
103 | | - styleSheet.innerText = fontImports; |
104 | | - document.head.appendChild(styleSheet); |
105 | | - } |
106 | | - } catch (e) { |
107 | | - console.error(e); |
108 | | - console.error((e as Error).stack); |
109 | | - } |
110 | | - }); |
111 | | - |
112 | | - console.log("TerriaJS initialized successfully"); |
113 | | - return { terria, viewState }; |
114 | | -}; |
| 1 | +import type Terria from "terriajs/lib/Models/Terria"; |
| 2 | +import type ViewState from "terriajs/lib/ReactViewModels/ViewState"; |
115 | 3 |
|
116 | 4 | type State = |
117 | 5 | | { |
@@ -141,7 +29,12 @@ const emitChange = () => { |
141 | 29 |
|
142 | 30 | export const terriaStore = { |
143 | 31 | async init() { |
144 | | - const { terria, viewState } = await initTerria(); |
| 32 | + await import("terriajs/lib/Core/prerequisites"); |
| 33 | + |
| 34 | + const { terria, viewState } = await import("../../index.js").then( |
| 35 | + (module) => module.default |
| 36 | + ); |
| 37 | + |
145 | 38 | state = { |
146 | 39 | terria, |
147 | 40 | viewState, |
|
0 commit comments