@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
529 lines (528 loc) • 19.9 kB
JavaScript
class GirafeConfig {
/**
* Creates the configuration of the app validating the json passed or giving default values.
*
* Every property of config that is not complying with GirafeConfig type is ignored.
* @param config the configuration
*/
constructor(config) {
Object.defineProperty(this, "general", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "languages", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "interface", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "themes", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "basemaps", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "treeview", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "search", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "print", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "selection", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "drawing", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "share", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "projections", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "map", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "map3d", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "lidar", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "contextmenu", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "crs", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "csv", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "metadata", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "infoWindow", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "offline", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "query", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "gmfauth", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "oauth", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "userdata", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "contact", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
// The extended configuration can be used by third-party components or extensions
// to add custom attributes to the GirafeConfig.
Object.defineProperty(this, "extendedConfig", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
// Default values are documented here : https://doc.geomapfish.dev/docs/configuration
// NOTE: Please adapt the documentation if necessary when doing changes here.
this.general = this.initConfigGeneral(config);
this.languages = this.initConfigLanguages(config);
this.interface = this.initConfigInterface(config);
this.themes = this.initConfigThemes(config);
this.basemaps = this.initConfigBasemaps(config);
this.treeview = this.initConfigTreeview(config);
this.selection = this.initConfigSelection(config);
this.drawing = this.initConfigDrawing(config);
this.projections = this.initConfigProjections(config);
this.map = this.initConfigMap(config);
this.lidar = this.initConfigLidar(config);
this.csv = this.initConfigCsv(config);
this.metadata = this.initConfigMetadata(config);
this.infoWindow = this.initConfigInfoWindow(config);
this.offline = this.initConfigOffline(config);
this.query = this.initConfigQuery(config);
this.oauth = this.initConfigOauth(config);
this.gmfauth = this.initGmfOauth(config);
this.userdata = this.initUserData(config);
this.contextmenu = this.initContextMenu(config);
this.crs = this.initCRS(config);
this.contact = this.initConfigContact(config);
this.extendedConfig = this.initExtendedConfig(config);
try {
this.search = this.initConfigSearch(config);
}
catch (e) {
// The application can be started even if the search is not correctly configured
// We just display a warning in the console
console.warn(e);
this.search = {
url: ''
};
}
try {
this.share = this.initConfigShare(config);
}
catch (e) {
// The application can be started even if the search is not correctly configured
// We just display a warning in the console
console.warn(e);
}
try {
this.print = this.initConfigPrint(config);
}
catch (e) {
// The application can be started even if the print is not correctly configured
// We just display a warning in the console
console.warn(e);
}
try {
this.map3d = this.initConfigMap3D(config);
}
catch (e) {
// The application can be started even if the 3D Part is not correctly configured
// We just display a warning in the console
console.warn(e);
}
}
initConfigProjections(config) {
if (!config.projections) {
throw new Error(`Configuration for projections is required. See https://doc.geomapfish.dev/docs/configuration`);
}
return config.projections;
}
initConfigMap3D(config) {
return config.map3d;
}
initConfigMap(config) {
// Map
if (!config.map?.srid) {
throw new Error(`Configuration for projections is required. See https://doc.geomapfish.dev/docs/configuration`);
}
if (!config.map?.scales) {
throw new Error(`Configuration for projections is required. See https://doc.geomapfish.dev/docs/configuration`);
}
if (!config.map?.startPosition) {
throw new Error(`Configuration for projections is required. See https://doc.geomapfish.dev/docs/configuration`);
}
if (!config.map?.startZoom) {
throw new Error(`Configuration for projections is required. See https://doc.geomapfish.dev/docs/configuration`);
}
return {
srid: config.map.srid,
startZoom: config.map.startZoom,
startPosition: config.map.startPosition,
maxExtent: config.map.maxExtent,
scales: config.map.scales,
constrainScales: config.map?.constrainScales ?? true,
constrainRotation: config.map?.constrainRotation ?? false,
showScaleLine: config.map?.showScaleLine ?? true
};
}
initConfigQuery(config) {
return {
legacy: config.query?.legacy ?? false
};
}
initConfigDrawing(config) {
return {
defaultFillColor: config.drawing?.defaultFillColor ?? '#6666ff7f',
defaultStrokeColor: config.drawing?.defaultStrokeColor ?? '#0000ff',
defaultStrokeWidth: config.drawing?.defaultStrokeWidth ?? 2,
defaultTextSize: config.drawing?.defaultTextSize ?? 12,
defaultFont: config.drawing?.defaultFont ?? 'Arial',
defaultVertexRadius: config.drawing?.defaultVertexRadius ?? 8,
defaultVertexFillColor: config.drawing?.defaultVertexFillColor ?? '#ffffffbf',
defaultVertexStrokeWidth: config.drawing?.defaultVertexStrokeWidth ?? 2
};
}
initConfigShare(config) {
if (!config.share?.createUrl) {
throw new Error(`Configuration for share.createUrl is required. See https://doc.geomapfish.dev/docs/configuration`);
}
return {
service: config.share?.service ?? 'gmf',
createUrl: config.share?.createUrl
};
}
initConfigSelection(config) {
return {
maxFeature: config.selection?.maxFeature ?? 300,
defaultFillColor: config.selection?.defaultFillColor ?? '#ff66667f',
defaultStrokeColor: config.selection?.defaultStrokeColor ?? '#ff3333',
defaultStrokeWidth: config.selection?.defaultStrokeWidth ?? 4,
highlightFillColor: config.selection?.highlightFillColor ?? '#00ff227f',
highlightStrokeColor: config.selection?.highlightStrokeColor ?? '#00ff22'
};
}
initConfigPrint(config) {
if (!config.print?.url) {
throw new Error(`Configuration for print.url is required. See https://doc.geomapfish.dev/docs/configuration`);
}
if (!config.print?.attributeNames) {
config.print.attributeNames = ['title', 'comments', 'legend'];
}
if (!config.print?.formats) {
config.print.formats = ['pdf', 'png'];
}
return config.print;
}
initConfigSearch(config) {
if (!config.search?.url) {
throw new Error(`Configuration for search.url is required. See https://doc.geomapfish.dev/docs/configuration`);
}
if (!config.search.url.includes('###SEARCHTERM###')) {
throw new Error(`search.url is missing the expected pattern. See https://doc.geomapfish.dev/docs/configuration`);
}
return {
url: config.search.url,
objectPreview: config.search.objectPreview ?? false,
layerPreview: config.search.layerPreview ?? false,
minResolution: config.search.minResolution ?? 0.5,
defaultFillColor: config.search?.defaultFillColor ?? '#3388ff7f',
defaultStrokeColor: config.search?.defaultStrokeColor ?? '#3388ff',
defaultStrokeWidth: config.search?.defaultStrokeWidth ?? 2,
paintSearchResults: config.search?.paintSearchResults ?? true
};
}
initConfigTreeview(config) {
return {
hideLegendWhenLayerIsDeactivated: config.treeview?.hideLegendWhenLayerIsDeactivated ?? true,
defaultIconSize: {
height: config.treeview?.defaultIconSize?.height ?? 20,
width: config.treeview?.defaultIconSize?.width ?? 20
}
};
}
initConfigBasemaps(config) {
return {
show: config.basemaps?.show ?? true,
defaultBasemap: config.basemaps?.defaultBasemap ?? 'Empty',
OSM: config.basemaps?.OSM ?? false,
SwissTopoVectorTiles: config.basemaps?.SwissTopoVectorTiles ?? false,
emptyBasemap: config.basemaps?.emptyBasemap ?? true
};
}
initConfigLidar(config) {
return config.lidar;
}
initConfigCsv(config) {
const defaultConfig = {
encoding: 'utf-8',
extension: '.csv',
includeHeader: true,
quote: "'",
separator: ','
};
return {
...defaultConfig,
...config.csv
};
}
initConfigMetadata(config) {
const defaultConfig = {
metadataUrlPrefix: ''
};
return {
...defaultConfig,
...config.metadata
};
}
initConfigInfoWindow(config) {
const defaultConfig = {
defaultWindowWidth: '960px',
defaultWindowHeight: '460px',
defaultWindowPositionTop: '1rem',
defaultWindowPositionLeft: '370px'
};
return {
...defaultConfig,
...config.infoWindow
};
}
initConfigContact(config) {
return config.contact;
}
initConfigOffline(config) {
// This can be null, that's not a problem. No default value either.
return config.offline;
}
initConfigThemes(config) {
if (!config.themes?.url) {
throw new Error(`Configuration for themes.url is required. See https://doc.geomapfish.dev/docs/configuration.`);
}
return {
url: config.themes.url,
defaultTheme: config.themes.defaultTheme ?? '',
imagesUrlPrefix: config.themes.imagesUrlPrefix ?? '',
showErrorsOnStart: config.themes.showErrorsOnStart ?? false,
selectionMode: config.themes.selectionMode ?? 'replace'
};
}
initConfigLanguages(config) {
if (!config.languages) {
throw new Error(`Configuration for languages is required. See https://doc.geomapfish.dev/docs/configuration.`);
}
return config.languages;
}
initConfigInterface(config) {
const defaultConfig = {
defaultSelectionComponent: 'window',
darkFrontendMode: undefined,
darkMapMode: false
};
return {
...defaultConfig,
...config.interface
};
}
initConfigGeneral(config) {
return {
locale: config.general.locale ?? GirafeConfig.DEFAULT_LOCALE,
// NOTE REG: Small hack specific to Vite: When running in debug mode, we force the logLevel to debug.
// Otherwise we will always have to manually activate it.
logLevel: import.meta.env.DEV ? 'debug' : (config.general.logLevel ?? 'warn')
};
}
initGmfOauth(config) {
if (!config.gmfauth) {
// No GMF-Auth configuration
return undefined;
}
if (!config.gmfauth.url) {
throw new Error(`Configuration for gmfauth.url is required. See https://doc.geomapfish.dev/docs/configuration.`);
}
if (!config.gmfauth.audience) {
throw new Error(`Configuration for gmfauth.audience is required. See https://doc.geomapfish.dev/docs/configuration.`);
}
return {
url: config.gmfauth.url,
audience: config.gmfauth.audience,
loginRequired: config.gmfauth.loginRequired ?? false,
checkSessionOnLoad: config.gmfauth.checkSessionOnLoad ?? true,
authMode: 'cookie',
refererPolicy: config.gmfauth.refererPolicy ?? 'strict-origin-when-cross-origin',
audienceExcludedPaths: config.gmfauth.audienceExcludedPaths ?? []
};
}
initConfigOauth(config) {
if (!config.oauth) {
// No oAuth configuration
return undefined;
}
if (!config.oauth.issuer.url) {
throw new Error(`Configuration for oauth.issuer.url is required. See https://doc.geomapfish.dev/docs/configuration.`);
}
if (!config.oauth.issuer.clientId) {
throw new Error(`Configuration for oauth.issuer.clientId is required. See https://doc.geomapfish.dev/docs/configuration.`);
}
if (!config.oauth.issuer.audience) {
throw new Error(`Configuration for oauth.issuer.audience is required. See https://doc.geomapfish.dev/docs/configuration.`);
}
if (!config.oauth.geomapfish.userInfoUrl) {
throw new Error(`Configuration for oauth.geomapfish.userInfoUrl is required. See https://doc.geomapfish.dev/docs/configuration.`);
}
const issuerConfig = {
url: config.oauth.issuer.url,
algorithm: config.oauth.issuer.algorithm ?? 'oidc',
codeChallengeMethod: config.oauth.issuer.codeChallengeMethod ?? 'S256',
clientId: config.oauth.issuer.clientId,
scope: config.oauth.issuer.scope ?? 'openid',
loginRequired: config.oauth.issuer.loginRequired ?? false,
checkSessionOnLoad: config.oauth.issuer.checkSessionOnLoad ?? false,
audience: config.oauth.issuer.audience,
audienceExcludedPaths: config.oauth.issuer.audienceExcludedPaths ?? []
};
const geomapfishConfig = {
userInfoUrl: config.oauth.geomapfish.userInfoUrl,
loginUrl: config.oauth.geomapfish.loginUrl,
logoutUrl: config.oauth.geomapfish.logoutUrl,
anonymousUsername: config.oauth.geomapfish.anonymousUsername,
authMode: config.oauth.geomapfish.authMode ?? 'cookie',
refererPolicy: config.oauth.geomapfish.refererPolicy ?? 'strict-origin-when-cross-origin'
};
return {
issuer: issuerConfig,
geomapfish: geomapfishConfig
};
}
initUserData(config) {
const defaultConfig = {
source: 'localStorage'
};
return {
...defaultConfig,
...config.userdata
};
}
initContextMenu(config) {
const contextMenuConfig = {
crs: config.contextmenu?.crs ?? [
{ code: 'EPSG:4326', translation: 'EPSG:4326', format: 'decimal', precision: 7 },
{ code: 'EPSG:4326', translation: 'EPSG:4326-DMS', format: 'dms', precision: 2 }
],
sources: config.contextmenu?.sources ?? [],
links: config.contextmenu?.links ?? []
};
return contextMenuConfig;
}
initCRS(config) {
return config.crs;
}
initExtendedConfig(config) {
return config.extendedConfig ?? undefined;
}
}
Object.defineProperty(GirafeConfig, "DEFAULT_LOCALE", {
enumerable: true,
configurable: true,
writable: true,
value: 'en-US'
});
export default GirafeConfig;