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

feature(geocorekeysdiv) Geocore keys by div attribute with data-geocore-keys. #1836

Merged
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
43 changes: 38 additions & 5 deletions packages/geoview-core/public/templates/default-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ <h1><strong>Default Configuration</strong></h1>
<a href="#HLCONF2">2. Load with wrong JSON object</a><br />
<a href="#HLCONF3">3. Load with bad config values</a><br />
<a href="#HLCONF4">4. Load layers with bad config values</a><br />
<a href="#HLCONF5">5. Load config from URL params</a><br />
<a href="#HLCONF5A">5.A Load config from URL params</a><br />
<a href="#HLCONF5B">5.B Load config from div params</a><br />
<a href="#HLCONF6">6. Load config from file</a><br />
<a href="#HLCONF7">7. Load config from function call</a><br />
</td>
Expand Down Expand Up @@ -184,13 +185,45 @@ <h4 id="HLCONF4">4. Load layers with bad config values</h4>
<hr />

<div class="map-title-holder">
<h4 id="HLCONF5">5. Load config from URL params</h4>
<h4 id="HLCONF5A">5.A. Load config from URL params</h4>
<a class="ref-link" href="#top">Top</a>
</div>
<div id="CONF5" class="geoview-map" data-lang="en" data-shared="true"></div>
<div id="CONF5A" class="geoview-map" data-lang="en" data-shared="true"></div>
<p>
This map loads it's configurations from the URL parameters by providing <strong>data-shared</strong>. If
<strong>data-config</strong> is provided then it will override all loaded config from parameters.
This map loads it's configurations from the URL parameters by providing a <strong>data-shared</strong> attribute. If a
<strong>data-config</strong> attribute was also provided then the later will have its configuration overriden with config from url parameters.
</p>
<hr />

<div class="map-title-holder">
<h4 id="HLCONF5B">5.B. Load config from div params</h4>
<a class="ref-link" href="#top">Top</a>
</div>
<div id="CONF5B" class="geoview-map" data-config="{
'map': {
'interaction': 'dynamic',
'viewSettings': {
'zoom': 4,
'center': [-100, 40],
'projection': 3857
},
'basemapOptions': {
'basemapId': 'transport',
'shaded': false,
'labeled': true
},
'listOfGeoviewLayerConfig': []
},
'components': ['overview-map'],
'corePackages': [],
'suportedLanguages': ['en', 'fr']
}"
data-lang="en"
data-geocore-keys="12acd145-626a-49eb-b850-0a59c9bc7506,ccc75c12-5acc-4a6a-959f-ef6f621147b9"
data-geocore-endpoint="https://geocore-stage.api.geo.ca">
</div>
<p>
This map loads it's configurations from the div parameters by providing a <strong>data-geocore-keys</strong> and optionally a <strong>data-geocore-endpoint</strong>.
</p>
<hr />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ export class GetLegendsPayload extends PayloadBaseClass {
* Static method used to create a get legends payload that will run a get legend on the specified layer path.
*
* @param {string | null} handlerName the handler name
* @param {string} layerPath layer path to query
* the set.
* @param {string} layerPath layer path to query the set.
*
* @returns {TypeQueryLegendPayload} the queryLegendPayload object created
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,8 @@ export class ConfigValidation {
if (typeof config === 'object') {
Object.keys(config).forEach((key) => {
if (!key.startsWith('_') && typeof config[key] === 'object') {
logger.logDebug(`Key=${key}`, config[key]);
// Leaving the commented line here in case a developer needs to quickly uncomment it again to troubleshoot
// logger.logDebug(`Key=${key}`, config[key]);
if (config?.[key]?.en || config?.[key]?.fr)
this.SynchronizeLocalizedString(Cast<TypeLocalizedString>(config[key]), sourceKey, destinationKey);
// Avoid the 'geoviewLayerConfig' and 'parentLayerConfig' properties because they loop on themself and cause a
Expand Down
7 changes: 2 additions & 5 deletions packages/geoview-core/src/core/utils/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export class Config {
let mapFeaturesConfig: TypeMapFeaturesConfig | undefined;

// check if inline div config has been passed
const inlineDivConfig = InlineDivConfigReader.getMapFeaturesConfig(this.mapId, this.mapElement);
const inlineDivConfig = await InlineDivConfigReader.getMapFeaturesConfig(this.mapId, this.mapElement);

// use inline config if provided
if (inlineDivConfig) mapFeaturesConfig = { ...inlineDivConfig };
Expand All @@ -166,10 +166,7 @@ export class Config {
const shared = this.mapElement.getAttribute('data-shared');
if (shared === 'true') {
// check if config params have been passed
const urlParamsConfig = await URLmapConfigReader.getMapFeaturesConfig(
this.configValidation.defaultMapFeaturesConfig.serviceUrls.geocoreUrl,
this.mapId
);
const urlParamsConfig = await URLmapConfigReader.getMapFeaturesConfig(this.mapId);

// use the url params config if provided
if (urlParamsConfig) mapFeaturesConfig = { ...urlParamsConfig };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable no-console */
import { TypeMapFeaturesConfig } from '@/core/types/global-types';
import { isJsonString, removeCommentsFromJSON } from '../../utilities';
import { TypeJsonValue, TypeMapFeaturesConfig } from '@/core/types/global-types';
import { getLocalizedMessage, isJsonString, removeCommentsFromJSON, replaceParams, showError } from '../../utilities';
import { logger } from '@/core/utils/logger';
import { api } from '@/app';
import { ConfigValidation } from '../config-validation';
import { UUIDmapConfigReader } from './uuid-config-reader';

/**
* A class to read the configuration of the GeoView map features from an online div. The configuration is provided in an HTML div
Expand All @@ -24,9 +26,9 @@ export class InlineDivConfigReader {
*
* @returns {TypeMapFeaturesConfig | undefined} The generated map features config object from inline map element.
*/
static getMapFeaturesConfig(mapId: string, mapElement: Element): TypeMapFeaturesConfig | undefined {
// create a new config object
let mapConfig: TypeMapFeaturesConfig | undefined;
static async getMapFeaturesConfig(mapId: string, mapElement: Element): Promise<TypeMapFeaturesConfig | undefined> {
// instanciate the configValidation object used to validate map config attributes and define default values.
const configValidation = new ConfigValidation();

let configObjStr = mapElement.getAttribute('data-config');

Expand All @@ -41,14 +43,53 @@ export class InlineDivConfigReader {
// Then, replace apostrophes preceded by a backslash with a single apostrophe
configObjStr = configObjStr.replace(/\\'/gm, "'");

if (!isJsonString(configObjStr)) {
logger.logWarning(`- Map: ${mapId} - Invalid JSON configuration object in div, a fallback strategy will be used -`);
api.utilities.showError(mapId, api.utilities.getLocalizedMessage(mapId, 'validation.invalidConfig'), true);
} else {
mapConfig = { ...JSON.parse(configObjStr) };
if (isJsonString(configObjStr)) {
// Create the config
const gvConfig = JSON.parse(configObjStr);

// Read the geocore keys
const geocoreKeys = mapElement.getAttribute('data-geocore-keys');

// If any
if (geocoreKeys) {
try {
// Make sure we have a mapConfig.serviceUrls.geocoreUrl set by default
if (!gvConfig.serviceUrls)
gvConfig.serviceUrls = { geocoreUrl: configValidation.defaultMapFeaturesConfig.serviceUrls.geocoreUrl };

// If there's a data-geocore-endpoint attribute, use it as the geoCoreUrl
const geocoreEndpoint = mapElement.getAttribute('data-geocore-endpoint');
if (geocoreEndpoint) gvConfig.serviceUrls.geocoreUrl = geocoreEndpoint;

// Get the layers config
const promise = UUIDmapConfigReader.getGVConfigFromUUIDs(
gvConfig.serviceUrls.geocoreUrl,
gvConfig.displayLanguage || 'en',
geocoreKeys.split(',')
);
const listOfGeoviewLayerConfig = (await promise).layers;

// Append the layers to the config (possibly with others)
if (!gvConfig.map.listOfGeoviewLayerConfig) gvConfig.map.listOfGeoviewLayerConfig = [];
gvConfig.map.listOfGeoviewLayerConfig.push(...listOfGeoviewLayerConfig);
} catch (error) {
// Log
logger.logError('Failed to get the GeoView layers from url keys', mapElement.getAttribute('data-geocore-keys'), error);
const message = replaceParams([error as TypeJsonValue, mapId], getLocalizedMessage(mapId, 'validation.layer.loadfailed'));
showError(mapId, message);
}
}

// Return the config
return gvConfig;
}

// Log
logger.logWarning(`- Map: ${mapId} - Invalid JSON configuration object in div, a fallback strategy will be used -`);
api.utilities.showError(mapId, api.utilities.getLocalizedMessage(mapId, 'validation.invalidConfig'), true);
}

return mapConfig;
// None
return undefined;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class URLmapConfigReader {
*
* @returns {Promise<TypeMapFeaturesConfig | undefined>} A map features configuration object generated from url parameters
*/
static async getMapFeaturesConfig(baseUrl: string, mapId: string): Promise<TypeMapFeaturesConfig | undefined> {
static async getMapFeaturesConfig(mapId: string): Promise<TypeMapFeaturesConfig | undefined> {
// instanciate the configValidation object used to validate map config attributes and define default values.
const configValidation = new ConfigValidation();

Expand Down Expand Up @@ -132,7 +132,7 @@ export class URLmapConfigReader {
try {
// Get the layers config
const promise = UUIDmapConfigReader.getGVConfigFromUUIDs(
baseUrl,
configValidation.defaultMapFeaturesConfig.serviceUrls.geocoreUrl,
displayLanguage.split('-')[0],
urlParams.keys.toString().split(',')
);
Expand Down Expand Up @@ -172,7 +172,7 @@ export class URLmapConfigReader {
extraOptions: {},
},
serviceUrls: {
geocoreUrl: baseUrl,
geocoreUrl: configValidation.defaultMapFeaturesConfig.serviceUrls.geocoreUrl,
},
components,
corePackages,
Expand All @@ -182,7 +182,7 @@ export class URLmapConfigReader {
}

// Trace the detail config read from url
logger.logTraceDetailed('URL Config - ', mapConfig);
logger.logTraceDetailed('URL Config - ', mapId, mapConfig);

return mapConfig;
}
Expand Down
Loading