Skip to content

Commit

Permalink
Use shareKeys when deserialising workbench and timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
tephenavies committed Nov 16, 2020
1 parent caf17a3 commit 6c97206
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 9 deletions.
45 changes: 40 additions & 5 deletions lib/Models/Terria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,21 @@ export default class Terria {
return this.shareKeysMap.get(shareKey);
}

getModelByIdOrShareKey<T extends BaseModel>(
type: Class<T>,
id: string
): T | undefined {
let model = this.getModelById(type, id);
if (model) {
return model;
} else {
const idFromShareKey = this.getModelIdByShareKey(id);
return idFromShareKey !== undefined
? this.getModelById(type, idFromShareKey)
: undefined;
}
}

@action
addShareKey(id: string, shareKey: string) {
this.shareKeysMap.set(shareKey, id);
Expand Down Expand Up @@ -854,18 +869,39 @@ export default class Terria {
message: "A model ID in the workbench list is not a string."
});
}

return this.getModelById(BaseModel, modelId);
return this.getModelByIdOrShareKey(BaseModel, modelId);
})
);

this.workbench.items = newItems;

// For ids that don't correspond to models resolve an id by share keys
const timelineWithShareKeysResolved = new Set(
filterOutUndefined(
timeline.map(modelId => {
if (typeof modelId !== "string") {
throw new TerriaError({
sender: this,
title: "Invalid model ID in timeline",
message: "A model ID in the timneline list is not a string."
});
}
if (this.getModelById(BaseModel, modelId) !== undefined) {
return modelId;
} else {
return this.getModelIdByShareKey(modelId);
}
})
)
);

// TODO: the timelineStack should be populated from the `timeline` property,
// not from the workbench.
this.timelineStack.items = this.workbench.items
.filter(item => {
return item.uniqueId && timeline.indexOf(item.uniqueId) >= 0;
return (
item.uniqueId && timelineWithShareKeysResolved.has(item.uniqueId)
);
// && TODO: what is a good way to test if an item is of type TimeVarying.
})
.map(item => <TimeVarying>item);
Expand Down Expand Up @@ -914,8 +950,7 @@ export default class Terria {
.toString();

const aspects = config.aspects;
const configParams =
aspects["terria-config"] && aspects["terria-config"].parameters;
const configParams = aspects["terria-config"]?.parameters;

if (configParams) {
this.updateParameters(configParams);
Expand Down
107 changes: 103 additions & 4 deletions test/Models/TerriaSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import PickedFeatures from "../../lib/Map/PickedFeatures";
import CameraView from "../../lib/Models/CameraView";
import Cesium from "../../lib/Models/Cesium";
import CommonStrata from "../../lib/Models/CommonStrata";
import CsvCatalogItem from "../../lib/Models/CsvCatalogItem";
import Feature from "../../lib/Models/Feature";
import { isInitData, isInitUrl } from "../../lib/Models/InitSource";
import MagdaReference from "../../lib/Models/MagdaReference";
Expand Down Expand Up @@ -70,11 +71,7 @@ describe("Terria", function() {
jasmine.Ajax.stubRequest(/.*map-config-dereferenced.*/).andReturn({
responseText: mapConfigDereferencedString
});

terria = new Terria({
baseUrl: "./"
});
});

afterEach(function() {
jasmine.Ajax.uninstall();
Expand Down Expand Up @@ -344,6 +341,108 @@ describe("Terria", function() {
});
});

// Test share keys by serialising from one catalog and deserialising with a reorganised catalog
describe("shareKeys", function() {
let newTerria: Terria;
let viewState: ViewState;
beforeEach(async function() {
// Create a config.json in a URL to pass to Terria.start
const configUrl = `data:application/json;base64,${btoa(
JSON.stringify({
initializationUrls: [],
parameters: {
regionMappingDefinitionsUrl: "data/regionMapping.json"
}
})
)}`;
newTerria = new Terria({ baseUrl: "./" });
viewState = new ViewState({
terria: terria,
catalogSearchProvider: null,
locationSearchProviders: []
});

await Promise.all([terria, newTerria].map(t => t.start({ configUrl })));

terria.catalog.group.addMembersFromJson(CommonStrata.definition, [
{
name: "Old group",
type: "group",
members: [
{
name: "Random CSV",
type: "csv",
url:
"data:text/csv,lon%2Clat%2Cval%2Cdate%0A151%2C-31%2C15%2C2010%0A151%2C-31%2C15%2C2011"
}
]
}
]);

newTerria.catalog.group.addMembersFromJson(CommonStrata.definition, [
{
name: "New group",
type: "group",
members: [
{
name: "Extra group",
type: "group",
members: [
{
name: "My random CSV",
type: "csv",
url:
"data:text/csv,lon%2Clat%2Cval%2Cdate%0A151%2C-31%2C15%2C2010%0A151%2C-31%2C15%2C2011",
shareKeys: ["//Old group/Random CSV"]
}
]
}
]
}
]);
});

it("correctly applies user stratum changes to moved item", async function() {
const csv = terria.getModelById(CsvCatalogItem, "//Old group/Random CSV");
expect(csv).toBeDefined("Can't find csv item in source terria");
csv?.setTrait(CommonStrata.user, "opacity", 0.5);
const shareLink = buildShareLink(terria, viewState);
await newTerria.updateApplicationUrl(shareLink);

const newCsv = newTerria.getModelById(
CsvCatalogItem,
"//New group/Extra group/My random CSV"
);
expect(newCsv).toBeDefined(
"Can't find newCsv item in destination newTerria"
);
expect(newCsv?.opacity).toBe(0.5);
});

it("correctly adds moved item to workbench and timeline", async function() {
const csv = terria.getModelById(CsvCatalogItem, "//Old group/Random CSV");
expect(csv).toBeDefined("csv not found in source terria");
if (csv === undefined) return;
terria.workbench.add(csv);
terria.timelineStack.addToTop(csv);
const shareLink = buildShareLink(terria, viewState);
await newTerria.updateApplicationUrl(shareLink);

const newCsv = newTerria.getModelById(
CsvCatalogItem,
"//New group/Extra group/My random CSV"
);
expect(newCsv).toBeDefined("newCsv not found in destination newTerria");
if (newCsv === undefined) return;
expect(newTerria.workbench.contains(newCsv)).toBeTruthy(
"newCsv not found in destination newTerria workbench"
);
expect(newTerria.timelineStack.contains(newCsv)).toBeTruthy(
"newCsv not found in destination newTerria timeline"
);
});
});

describe("proxyConfiguration", function() {
beforeEach(function() {
jasmine.Ajax.install();
Expand Down

0 comments on commit 6c97206

Please sign in to comment.