mapcomplete/Models/ThemeConfig/LayoutConfig.ts

250 lines
10 KiB
TypeScript
Raw Normal View History

2020-11-11 16:23:49 +01:00
import {Translation} from "../../UI/i18n/Translation";
import {LayoutConfigJson} from "./Json/LayoutConfigJson";
import LayerConfig from "./LayerConfig";
2021-09-07 00:23:00 +02:00
import {LayerConfigJson} from "./Json/LayerConfigJson";
2021-09-29 16:55:05 +02:00
import Constants from "../Constants";
import TilesourceConfig from "./TilesourceConfig";
2020-11-11 16:23:49 +01:00
export default class LayoutConfig {
public readonly id: string;
public readonly maintainer: string;
2021-04-09 17:56:13 +02:00
public readonly credits?: string;
2020-11-11 16:23:49 +01:00
public readonly version: string;
public readonly language: string[];
public readonly title: Translation;
2021-12-21 18:35:31 +01:00
public readonly shortDescription: Translation;
2020-11-11 16:23:49 +01:00
public readonly description: Translation;
public readonly descriptionTail?: Translation;
public readonly icon: string;
public readonly socialImage?: string;
public readonly startZoom: number;
public readonly startLat: number;
public readonly startLon: number;
public readonly widenFactor: number;
public readonly defaultBackgroundId?: string;
2021-05-17 00:18:21 +02:00
public layers: LayerConfig[];
public tileLayerSources: TilesourceConfig[]
2021-01-06 02:52:38 +01:00
public readonly clustering?: {
maxZoom: number,
minNeededElements: number,
2021-01-04 20:09:07 +01:00
};
2020-11-11 16:23:49 +01:00
public readonly hideFromOverview: boolean;
2021-05-27 18:56:02 +02:00
public lockLocation: boolean | [[number, number], [number, number]];
2020-11-11 16:23:49 +01:00
public readonly enableUserBadge: boolean;
public readonly enableShareScreen: boolean;
public readonly enableMoreQuests: boolean;
public readonly enableAddNewPoints: boolean;
public readonly enableLayers: boolean;
public readonly enableSearch: boolean;
public readonly enableGeolocation: boolean;
2021-01-04 18:55:10 +01:00
public readonly enableBackgroundLayerSelection: boolean;
2021-05-17 00:18:21 +02:00
public readonly enableShowAllQuestions: boolean;
public readonly enableExportButton: boolean;
2021-07-28 02:51:07 +02:00
public readonly enablePdfDownload: boolean;
public readonly enableIframePopout: boolean;
2021-07-28 02:51:07 +02:00
2020-11-11 16:23:49 +01:00
public readonly customCss?: string;
2021-09-29 16:55:05 +02:00
public readonly overpassUrl: string[];
public readonly overpassTimeout: number;
public readonly overpassMaxZoom: number
public readonly osmApiTileSize: number
public readonly official: boolean;
2021-11-07 16:34:51 +01:00
constructor(json: LayoutConfigJson, official = true, context?: string) {
this.official = official;
2020-11-11 16:23:49 +01:00
this.id = json.id;
if(official){
if(json.id.toLowerCase() !== json.id){
throw "The id of a theme should be lowercase: "+json.id
}
if(json.id.match(/[a-z0-9-_]/) == null){
throw "The id of a theme should match [a-z0-9-_]*: "+json.id
}
}
2021-01-04 18:55:10 +01:00
context = (context ?? "") + "." + this.id;
2020-11-11 16:23:49 +01:00
this.maintainer = json.maintainer;
2021-04-09 17:56:13 +02:00
this.credits = json.credits;
2020-11-11 16:23:49 +01:00
this.version = json.version;
this.language = [];
2021-11-07 16:34:51 +01:00
2020-11-11 16:23:49 +01:00
if (typeof json.language === "string") {
this.language = [json.language];
} else {
this.language = json.language;
}
2021-12-21 18:35:31 +01:00
{
if (this.language.length == 0) {
throw `No languages defined. Define at least one language. (${context}.languages)`
}
if (json.title === undefined) {
throw "Title not defined in " + this.id;
}
if (json.description === undefined) {
throw "Description not defined in " + this.id;
}
if (json.widenFactor <= 0) {
throw "Widenfactor too small, shoud be > 0"
}
if (json.widenFactor > 20) {
throw "Widenfactor is very big, use a value between 1 and 5 (current value is " + json.widenFactor + ") at " + context
}
if (json["hideInOverview"]) {
throw "The json for " + this.id + " contains a 'hideInOverview'. Did you mean hideFromOverview instead?"
}
if (json.layers === undefined) {
throw "Got undefined layers for " + json.id + " at " + context
}
2020-11-11 16:23:49 +01:00
}
2021-01-04 18:55:10 +01:00
this.title = new Translation(json.title, context + ".title");
this.description = new Translation(json.description, context + ".description");
this.shortDescription = json.shortDescription === undefined ? this.description.FirstSentence() : new Translation(json.shortDescription, context + ".shortdescription");
this.descriptionTail = json.descriptionTail === undefined ? undefined : new Translation(json.descriptionTail, context + ".descriptionTail");
2020-11-11 16:23:49 +01:00
this.icon = json.icon;
this.socialImage = json.socialImage;
this.startZoom = json.startZoom;
this.startLat = json.startLat;
this.startLon = json.startLon;
this.widenFactor = json.widenFactor ?? 1.5;
2021-11-07 16:34:51 +01:00
2020-11-11 16:23:49 +01:00
this.defaultBackgroundId = json.defaultBackgroundId;
2021-11-07 16:34:51 +01:00
this.tileLayerSources = (json.tileLayerSources ?? []).map((config, i) => new TilesourceConfig(config, `${this.id}.tileLayerSources[${i}]`))
2021-12-21 18:35:31 +01:00
// At this point, layers should be expanded and validated either by the generateScript or the LegacyJsonConvert
this.layers = json.layers.map(lyrJson => new LayerConfig(<LayerConfigJson>lyrJson, json.id + ".layers." + lyrJson["id"], official));
2021-11-07 16:34:51 +01:00
2021-06-15 01:24:04 +02:00
this.clustering = {
maxZoom: 16,
minNeededElements: 25,
};
2021-11-07 16:34:51 +01:00
if (json.clustering === false) {
2021-10-13 01:28:46 +02:00
this.clustering = {
maxZoom: 0,
minNeededElements: 100000,
};
2021-11-07 16:34:51 +01:00
} else if (json.clustering) {
2021-01-04 20:09:07 +01:00
this.clustering = {
2021-01-06 02:52:38 +01:00
maxZoom: json.clustering.maxZoom ?? 18,
minNeededElements: json.clustering.minNeededElements ?? 25,
2021-01-04 18:55:10 +01:00
}
}
2020-11-11 16:23:49 +01:00
this.hideFromOverview = json.hideFromOverview ?? false;
2021-11-07 16:34:51 +01:00
this.lockLocation = <[[number, number], [number, number]]>json.lockLocation ?? undefined;
2020-11-11 16:23:49 +01:00
this.enableUserBadge = json.enableUserBadge ?? true;
this.enableShareScreen = json.enableShareScreen ?? true;
this.enableMoreQuests = json.enableMoreQuests ?? true;
this.enableLayers = json.enableLayers ?? true;
this.enableSearch = json.enableSearch ?? true;
this.enableGeolocation = json.enableGeolocation ?? true;
this.enableAddNewPoints = json.enableAddNewPoints ?? true;
this.enableBackgroundLayerSelection = json.enableBackgroundLayerSelection ?? true;
2021-05-17 00:18:21 +02:00
this.enableShowAllQuestions = json.enableShowAllQuestions ?? false;
2021-07-28 02:51:07 +02:00
this.enableExportButton = json.enableDownload ?? false;
this.enablePdfDownload = json.enablePdfDownload ?? false;
this.enableIframePopout = json.enableIframePopout ?? true
2020-11-11 16:23:49 +01:00
this.customCss = json.customCss;
2021-09-29 16:55:05 +02:00
this.overpassUrl = Constants.defaultOverpassUrls
2021-11-07 16:34:51 +01:00
if (json.overpassUrl !== undefined) {
if (typeof json.overpassUrl === "string") {
2021-09-29 16:55:05 +02:00
this.overpassUrl = [json.overpassUrl]
2021-11-07 16:34:51 +01:00
} else {
2021-09-29 16:55:05 +02:00
this.overpassUrl = json.overpassUrl
}
}
this.overpassTimeout = json.overpassTimeout ?? 30
this.overpassMaxZoom = json.overpassMaxZoom ?? 16
this.osmApiTileSize = json.osmApiTileSize ?? this.overpassMaxZoom + 1
}
public CustomCodeSnippets(): string[] {
if (this.official) {
return [];
}
const msg = "<br/><b>This layout uses <span class='alert'>custom javascript</span>, loaded for the wide internet. The code is printed below, please report suspicious code on the issue tracker of MapComplete:</b><br/>"
const custom = [];
for (const layer of this.layers) {
custom.push(...layer.CustomCodeSnippets().map(code => code + "<br />"))
}
if (custom.length === 0) {
return custom;
}
custom.splice(0, 0, msg);
return custom;
}
public ExtractImages(): Set<string> {
const icons = new Set<string>()
for (const layer of this.layers) {
layer.ExtractImages().forEach(icons.add, icons)
}
icons.add(this.icon)
icons.add(this.socialImage)
return icons
}
2021-05-17 00:18:21 +02:00
/**
* Replaces all the relative image-urls with a fixed image url
* This is to fix loading from external sources
*
* It should be passed the location where the original theme file is hosted.
*
* If no images are rewritten, the same object is returned, otherwise a new copy is returned
*/
public patchImages(originalURL: string, originalJson: string): LayoutConfig {
const allImages = Array.from(this.ExtractImages())
const rewriting = new Map<string, string>()
// Needed for absolute urls: note that it doesn't contain a trailing slash
const origin = new URL(originalURL).origin
let path = new URL(originalURL).href
path = path.substring(0, path.lastIndexOf("/"))
for (const image of allImages) {
2021-05-17 00:18:21 +02:00
if (image == "" || image == undefined) {
continue
}
if (image.startsWith("http://") || image.startsWith("https://")) {
continue
}
if (image.startsWith("/")) {
// This is an absolute path
rewriting.set(image, origin + image)
} else if (image.startsWith("./assets/themes")) {
// Legacy workaround
rewriting.set(image, path + image.substring(image.lastIndexOf("/")))
} else if (image.startsWith("./")) {
// This is a relative url
rewriting.set(image, path + image.substring(1))
} else {
// This is a relative URL with only the path
rewriting.set(image, path + image)
}
}
if (rewriting.size == 0) {
return this;
}
rewriting.forEach((value, key) => {
2021-05-17 00:18:21 +02:00
console.log("Rewriting", key, "==>", value)
originalJson = originalJson.replace(new RegExp(key, "g"), value)
})
return new LayoutConfig(JSON.parse(originalJson), false, "Layout rewriting")
}
2021-11-07 16:34:51 +01:00
public isLeftRightSensitive() {
return this.layers.some(l => l.isLeftRightSensitive())
}
public getMatchingLayer(tags: any): LayerConfig | undefined {
if (tags === undefined) {
2021-11-08 20:49:51 +01:00
return undefined
}
for (const layer of this.layers) {
if (layer.source.osmTags.matchesProperties(tags)) {
return layer
}
}
return undefined
}
2020-11-11 16:23:49 +01:00
}