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

Share keys #4975

Merged
merged 14 commits into from
Nov 26, 2020
Merged
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
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@

# These are auto-generated by css-modules-typescript-loader
**/*.scss.d.ts

!wwwroot
!wwwroot/test
!wwwroot/test/Magda
!wwwroot/test/Magda/shareKeys/**/
!wwwroot/test/Magda/shareKeys/**/*.json
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"typescript.tsdk": "../../node_modules/typescript/lib"
"typescript.tsdk": "node_modules/typescript/lib"
}
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Change Log
* Add `multiplierDefaultDeltaStep` Trait, which tries to calculate sensible multiplier for `DistrectelyTimeVarying` datasets. By default it is set to 2, which results in a new timestep being displayed every 2 seconds (on average) if timeline is playing.
* Fixed an issue with not loading the base map from init file and an issue with viewerMode from init files overriding the persisted viewerMode.
* Hide info sections with empty content in the explorer preview.
* Port `shareKeys` from version 7
* [The next improvement]

#### 8.0.0-alpha.61
Expand Down
16 changes: 12 additions & 4 deletions lib/Models/MagdaReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,14 @@ export default class MagdaReference extends AccessControlMixin(
overriddenMember,
terria.getModelById(BaseModel, member.id)
);
let shareKeys;
if (
isJsonObject(member.aspects) &&
isJsonObject(member.aspects.terria) &&
Array.isArray(member.aspects.terria.shareKeys)
) {
shareKeys = member.aspects.terria.shareKeys.filter(isJsonString);
}

if (!model) {
// Can't create an item or group yet, so create a reference.
Expand Down Expand Up @@ -427,7 +435,7 @@ export default class MagdaReference extends AccessControlMixin(
}

if (terria.getModelById(BaseModel, member.id) === undefined) {
terria.addModel(ref);
terria.addModel(ref, shareKeys);
}

if (AccessControlMixin.isMixedInto(ref)) {
Expand All @@ -437,7 +445,7 @@ export default class MagdaReference extends AccessControlMixin(
return ref.uniqueId;
} else {
if (terria.getModelById(BaseModel, member.id) === undefined) {
terria.addModel(model);
terria.addModel(model, shareKeys);
}
if (AccessControlMixin.isMixedInto(model)) {
model.setAccessType(getAccessTypeFromMagdaRecord(member));
Expand All @@ -459,7 +467,7 @@ export default class MagdaReference extends AccessControlMixin(
if (isJsonObject(aspects.terria)) {
const terriaStrata = aspects.terria;
Object.keys(terriaStrata).forEach(stratum => {
if (stratum === "id" || stratum === "type") {
if (stratum === "id" || stratum === "type" || stratum === "shareKeys") {
return;
}
updateModelFromJson(group, stratum, terriaStrata[stratum], true);
Expand Down Expand Up @@ -514,7 +522,7 @@ export default class MagdaReference extends AccessControlMixin(
}

Object.keys(terriaAspect).forEach(stratum => {
if (stratum === "type" || stratum === "id") {
if (stratum === "id" || stratum === "type" || stratum === "shareKeys") {
return;
}
try {
Expand Down
69 changes: 59 additions & 10 deletions lib/Models/Terria.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ interface HomeCameraInit {

export default class Terria {
private models = observable.map<string, BaseModel>();
// Map from share key -> id
private shareKeysMap = observable.map<string, string>();

readonly baseUrl: string = "build/TerriaJS/";
readonly notification = new CesiumEvent();
Expand Down Expand Up @@ -415,7 +417,7 @@ export default class Terria {
}

@action
addModel(model: BaseModel) {
addModel(model: BaseModel, shareKeys?: string[]) {
if (model.uniqueId === undefined) {
throw new DeveloperError("A model without a `uniqueId` cannot be added.");
}
Expand All @@ -425,6 +427,7 @@ export default class Terria {
}

this.models.set(model.uniqueId, model);
shareKeys?.forEach(shareKey => this.addShareKey(model.uniqueId!, shareKey));
}

/**
Expand All @@ -447,8 +450,32 @@ export default class Terria {
}
}

getModelIdByShareKey(shareKey: string): string | undefined {
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);
}

setupInitializationUrls(baseUri: uri.URI, config: any) {
const initializationUrls: string[] = config.initializationUrls || [];
const initializationUrls: string[] = config?.initializationUrls || [];
const initSources = initializationUrls.map(url =>
generateInitializationUrl(
baseUri,
Expand All @@ -468,7 +495,7 @@ export default class Terria {
options.applicationUrl?.href || getUriWithoutPath(baseUri);
return loadJson5(options.configUrl, options.configUrlHeaders)
.then((config: any) => {
runInAction(() => {
return runInAction(() => {
// If it's a magda config, we only load magda config and parameters should never be a property on the direct
// config aspect (it would be under the `terria-config` aspect)
if (config.aspects) {
Expand Down Expand Up @@ -710,13 +737,15 @@ export default class Terria {
CatalogMemberFactory,
this,
"/",
undefined,
stratumId,
{
...cleanStratumData,
id: modelId
},
replaceStratum
{
replaceStratum,
matchByShareKey: true
}
);

if (Array.isArray(containerIds)) {
Expand Down Expand Up @@ -891,18 +920,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 @@ -951,8 +1001,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
6 changes: 3 additions & 3 deletions lib/Models/WebProcessingServiceCatalogFunctionJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,14 @@ export default class WebProcessingServiceCatalogFunctionJob extends XmlRequestMi
CatalogMemberFactory,
this.terria,
this.uniqueId || "",
undefined,
CommonStrata.user,
{
...itemJson,
id: createGuid()
},
false,
false
{
addModelToTerria: false
}
);
return catalogItem;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Models/createCatalogItemFromFileOrUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ function createCatalogMember(
CatalogMemberFactory,
terria,
"",
undefined,
CommonStrata.definition,
json
json,
{}
);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/Models/createUrlReferenceFromUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export default function createUrlReferenceFromUrl(
CatalogMemberFactory,
terria,
"",
undefined,
CommonStrata.definition,
{
type: UrlReference.type,
name: url,
url: url,
localId: url,
allowLoad: allowLoad
}
},
{}
);

if (item === undefined || !(item instanceof UrlReference)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/Models/updateModelFromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export default function updateModelFromJson(
if (
propertyName === "id" ||
propertyName === "type" ||
propertyName === "localId"
propertyName === "localId" ||
propertyName === "shareKeys"
) {
return;
}
Expand Down
Loading