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

Adds new config parameter defaultBaseMapId #4995

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Change Log
### MobX Development

#### next release (8.0.0-alpha.62)
* Added config parameter `defaultBaseMapId` which is used as a fallback map if no persisted map is found.


#### 8.0.0-alpha.61
* New `CatalogFunctionMixin` and `CatalogFunctionJobMixin`
Expand Down
30 changes: 25 additions & 5 deletions lib/Models/Terria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export function makeModelsMagdaCompatible(models: InitModels) {
interface ConfigParameters {
[key: string]: ConfigParameters[keyof ConfigParameters];
appName?: string;
defaultBaseMapId?: string;
supportEmail?: string;
defaultMaximumShownFeatureInfos?: number;
regionMappingDefinitionsUrl: string;
Expand Down Expand Up @@ -242,6 +243,7 @@ export default class Terria {
@observable
readonly configParameters: ConfigParameters = {
appName: "TerriaJS App",
defaultBaseMapId: undefined,
supportEmail: "info@terria.io",
defaultMaximumShownFeatureInfos: 100,
regionMappingDefinitionsUrl: "build/TerriaJS/data/regionMapping.json",
Expand Down Expand Up @@ -539,23 +541,41 @@ export default class Terria {
updateBaseMaps(baseMaps: BaseMapViewModel[]): void {
this.baseMaps.push(...baseMaps);
if (!this.mainViewer.baseMap) {
this.loadPersistedBaseMap();
this.loadPersistedBaseMap() || this.loadDefaultBaseMap();
}
}

@action
loadPersistedBaseMap(): void {
const persistedBaseMapId = this.getLocalProperty("basemap");
loadBaseMapById(id: string) {
const baseMapSearch = this.baseMaps.find(
baseMap => baseMap.mappable.uniqueId === persistedBaseMapId
baseMap => baseMap.mappable.uniqueId === id
);
if (baseMapSearch) {
this.mainViewer.baseMap = baseMapSearch.mappable;
} else {
return true;
}
return false;
}

@action
loadPersistedBaseMap(): boolean {
const persistedBaseMapId = this.getLocalProperty("basemap");
const loaded =
typeof persistedBaseMapId === "string" &&
this.loadBaseMapById(persistedBaseMapId);
if (!loaded) {
console.error(
`Couldn't find a basemap for unique id ${persistedBaseMapId}`
);
}
return loaded;
}

@action
loadDefaultBaseMap(): boolean {
return this.configParameters.defaultBaseMapId
? this.loadBaseMapById(this.configParameters.defaultBaseMapId)
: false;
}

get isLoadingInitSources(): boolean {
Expand Down
31 changes: 31 additions & 0 deletions test/Models/TerriaSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import WebMapServiceCatalogGroup from "../../lib/Models/WebMapServiceCatalogGrou
import WebMapServiceCatalogItem from "../../lib/Models/WebMapServiceCatalogItem";
import ViewState from "../../lib/ReactViewModels/ViewState";
import { buildShareLink } from "../../lib/ReactViews/Map/Panels/SharePanel/BuildShareLink";
import { BaseMapViewModel } from "../../lib/ViewModels/BaseMapViewModel";
import SimpleCatalogItem from "../Helpers/SimpleCatalogItem";

const mapConfigBasicJson = require("../../wwwroot/test/Magda/map-config-basic.json");
Expand Down Expand Up @@ -602,4 +603,34 @@ describe("Terria", function() {
expect(terria.selectedFeature?.name).toBe("foo");
});
});

describe("updateBaseMaps", function() {
let baseMaps: BaseMapViewModel[];
beforeEach(function() {
baseMaps = [
new BaseMapViewModel(new WebMapServiceCatalogItem("baseMap0", terria)),
new BaseMapViewModel(new WebMapServiceCatalogItem("baseMap1", terria))
];
});

it("updates the basemaps list", function() {
terria.updateBaseMaps(baseMaps);
expect(terria.baseMaps).toEqual(baseMaps);
});

describe("automatically setting viewer basemap", function() {
it("sets it to the persisted base map if present", function() {
spyOn(terria, "getLocalProperty").and.returnValue("baseMap1");
terria.updateBaseMaps(baseMaps);
expect(terria.mainViewer.baseMap).toBe(baseMaps[1].mappable);
});

it("otherwise, sets it to the default basemap", function() {
spyOn(terria, "getLocalProperty").and.returnValue(undefined);
terria.configParameters.defaultBaseMapId = "baseMap1";
terria.updateBaseMaps(baseMaps);
expect(terria.mainViewer.baseMap).toBe(baseMaps[1].mappable);
});
});
});
});