Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update terriajs load procedure #603

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,5 @@ wwwroot/help/
wwwroot/public/
wwwroot/*.html


ckanext-cesiumpreview
deploy/helm/terria/charts/terriamap/templates/
deploy/helm/terria/charts/terriamap/templates/
5 changes: 3 additions & 2 deletions entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function loadMainScript() {
require.ensure(
["./index"],
function (require) {
resolve(require("./index"));
resolve(require("./index")());
},
reject,
"index"
Expand Down Expand Up @@ -46,7 +46,8 @@ function createLoader() {

polyfill(function () {
loadMainScript()
.catch(() => {
.catch((e) => {
console.error(e);
// Ignore errors and try to show the map anyway
})
.then(() => {
Expand Down
212 changes: 102 additions & 110 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,23 @@
"use strict";

var terriaOptions = {
baseUrl: "build/TerriaJS"
};

import { runInAction } from "mobx";

// checkBrowserCompatibility('ui');
import ConsoleAnalytics from "terriajs/lib/Core/ConsoleAnalytics";
import GoogleAnalytics from "terriajs/lib/Core/GoogleAnalytics";
import isDefined from "terriajs/lib/Core/isDefined";
import ShareDataService from "terriajs/lib/Models/ShareDataService";
// import registerAnalytics from 'terriajs/lib/Models/registerAnalytics';
// import registerCatalogMembers from 'terriajs/lib/Models/registerCatalogMembers';
import registerCustomComponentTypes from "terriajs/lib/ReactViews/Custom/registerCustomComponentTypes";
import Terria from "terriajs/lib/Models/Terria";
import updateApplicationOnHashChange from "terriajs/lib/ViewModels/updateApplicationOnHashChange";
import updateApplicationOnMessageFromParentWindow from "terriajs/lib/ViewModels/updateApplicationOnMessageFromParentWindow";
import ViewState from "terriajs/lib/ReactViewModels/ViewState";
import BingMapsSearchProviderViewModel from "terriajs/lib/Models/SearchProviders/BingMapsSearchProvider";
// import GazetteerSearchProviderViewModel from 'terriajs/lib/ViewModels/GazetteerSearchProviderViewModel.js';
// import GnafSearchProviderViewModel from 'terriajs/lib/ViewModels/GnafSearchProviderViewModel.js';
// import defined from 'terriajs-cesium/Source/Core/defined';
import render from "./lib/Views/render";
import registerCatalogMembers from "terriajs/lib/Models/Catalog/registerCatalogMembers";
import defined from "terriajs-cesium/Source/Core/defined";
import loadPlugins from "./lib/Core/loadPlugins";
import plugins from "./plugins";

// Register all types of catalog members in the core TerriaJS. If you only want to register a subset of them
// (i.e. to reduce the size of your application if you don't actually use them all), feel free to copy a subset of
// the code in the registerCatalogMembers function here instead.
// registerCatalogMembers();
// registerAnalytics();
const terriaOptions = {
baseUrl: "build/TerriaJS"
};

// we check exact match for development to reduce chances that production flag isn't set on builds(?)
if (process.env.NODE_ENV === "development") {
Expand All @@ -41,7 +27,11 @@ if (process.env.NODE_ENV === "development") {
}

// Construct the TerriaJS application, arrange to show errors to the user, and start it up.
var terria = new Terria(terriaOptions);
const terria = new Terria(terriaOptions);

// Register all types of catalog members in the core TerriaJS. If you only want to register a subset of them
// (i.e. to reduce the size of your application if you don't actually use them all), feel free to copy a subset of
registerCatalogMembers();

// Register custom components in the core TerriaJS. If you only want to register a subset of them, or to add your own,
// insert your custom version of the code in the registerCustomComponentTypes function here instead.
Expand All @@ -52,8 +42,7 @@ const viewState = new ViewState({
terria: terria
});

registerCatalogMembers();

// Add viewState to window global in dev environment for easy debugging
if (process.env.NODE_ENV === "development") {
window.viewState = viewState;
}
Expand All @@ -64,99 +53,102 @@ if (process.env.NODE_ENV !== "production" && module.hot) {
document.styleSheets[0].disabled = true;
}

module.exports = terria
.start({
applicationUrl: window.location,
configUrl: "config.json",
shareDataService: new ShareDataService({
terria: terria
}),
beforeRestoreAppState: () => {
// Load plugins before restoring app state because app state may
// reference plugin components and catalog items.
return loadPlugins(viewState, plugins).catch((error) => {
console.error(`Error loading plugins`);
console.error(error);
});
}
})
.catch(function (e) {
/** Terria load procedure:
* - Terria.start
* - Load plugins
* - load init sources
* - render
*/
module.exports = async function () {
try {
await terria.start({
configUrl: "config.json",
shareDataService: new ShareDataService({
terria: terria
}),
applicationUrl: window.location
});
} catch (e) {
terria.raiseErrorToUser(e);
})
.finally(function () {
terria.loadInitSources().then((result) => result.raiseError(terria));

try {
viewState.searchState.locationSearchProviders = [
new BingMapsSearchProviderViewModel({
terria: terria,
key: terria.configParameters.bingMapsKey
})
// new GazetteerSearchProviderViewModel({terria}),
// new GnafSearchProviderViewModel({terria})
];

// Automatically update Terria (load new catalogs, etc.) when the hash part of the URL changes.
updateApplicationOnHashChange(terria, window);
updateApplicationOnMessageFromParentWindow(terria, window);

// Show a modal disclaimer before user can do anything else.
if (defined(terria.configParameters.globalDisclaimer)) {
var globalDisclaimer = terria.configParameters.globalDisclaimer;
var hostname = window.location.hostname;
}

// Load plugins before loading init sources
// as plugins can register new catalog member types.
try {
await loadPlugins(viewState, plugins);
} catch (e) {
console.error(`Error loading plugins`);
console.error(e);
}

// Asynchronously load InitSources
terria.loadInitSources().then((result) => result.raiseError(terria));

try {
viewState.searchState.locationSearchProviders = [
new BingMapsSearchProviderViewModel({
terria: terria,
key: terria.configParameters.bingMapsKey
})
];

// Show a modal disclaimer before user can do anything else.
if (isDefined(terria.configParameters.globalDisclaimer)) {
const globalDisclaimer = terria.configParameters.globalDisclaimer;
const hostname = window.location.hostname;
if (
globalDisclaimer.enableOnLocalhost ||
hostname.indexOf("localhost") === -1
) {
let message = "";
// Sometimes we want to show a preamble if the user is viewing a site other than the official production instance.
// This can be expressed as a devHostRegex ("any site starting with staging.") or a negative prodHostRegex ("any site not ending in .gov.au")
if (
globalDisclaimer.enableOnLocalhost ||
hostname.indexOf("localhost") === -1
(isDefined(globalDisclaimer.devHostRegex) &&
hostname.match(globalDisclaimer.devHostRegex)) ||
(isDefined(globalDisclaimer.prodHostRegex) &&
!hostname.match(globalDisclaimer.prodHostRegex))
) {
var message = "";
// Sometimes we want to show a preamble if the user is viewing a site other than the official production instance.
// This can be expressed as a devHostRegex ("any site starting with staging.") or a negative prodHostRegex ("any site not ending in .gov.au")
if (
(defined(globalDisclaimer.devHostRegex) &&
hostname.match(globalDisclaimer.devHostRegex)) ||
(defined(globalDisclaimer.prodHostRegex) &&
!hostname.match(globalDisclaimer.prodHostRegex))
) {
message += require("./lib/Views/DevelopmentDisclaimerPreamble.html");
}
message += require("./lib/Views/GlobalDisclaimer.html");

var options = {
title:
globalDisclaimer.title !== undefined
? globalDisclaimer.title
: "Warning",
confirmText: globalDisclaimer.buttonTitle || "Ok",
denyText: globalDisclaimer.denyText || "Cancel",
denyAction: globalDisclaimer.afterDenyLocation
? function () {
window.location = globalDisclaimer.afterDenyLocation;
}
: undefined,
width: 600,
height: 550,
message: message,
horizontalPadding: 100
};
runInAction(() => {
viewState.disclaimerSettings = options;
viewState.disclaimerVisible = true;
});
message += require("./lib/Views/DevelopmentDisclaimerPreamble.html");
}
message += require("./lib/Views/GlobalDisclaimer.html");

const options = {
title:
globalDisclaimer.title !== undefined
? globalDisclaimer.title
: "Warning",
confirmText: globalDisclaimer.buttonTitle || "Ok",
denyText: globalDisclaimer.denyText || "Cancel",
denyAction: globalDisclaimer.afterDenyLocation
? function () {
window.location = globalDisclaimer.afterDenyLocation;
}
: undefined,
width: 600,
height: 550,
message: message,
horizontalPadding: 100
};
runInAction(() => {
viewState.disclaimerSettings = options;
viewState.disclaimerVisible = true;
});
}
}

// Add font-imports
const fontImports = terria.configParameters.theme?.fontImports;
if (fontImports) {
const styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = fontImports;
document.head.appendChild(styleSheet);
}

render(terria, [], viewState);
} catch (e) {
console.error(e);
console.error(e.stack);
// Add font-imports
const fontImports = terria.configParameters.theme?.fontImports;
if (fontImports) {
const styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = fontImports;
document.head.appendChild(styleSheet);
}
});

render(terria, [], viewState);
} catch (e) {
console.error(e);
console.error(e.stack);
}
};