2020-08-30 01:13:18 +02:00
|
|
|
import {Layout} from "../Layout";
|
|
|
|
import {LayoutConfigJson} from "./LayoutConfigJson";
|
|
|
|
import {AndOrTagConfigJson} from "./TagConfigJson";
|
2020-09-02 11:37:34 +02:00
|
|
|
import {And, Or, RegexTag, Tag, TagsFilter} from "../../Logic/Tags";
|
2020-08-30 01:13:18 +02:00
|
|
|
import {TagRenderingConfigJson} from "./TagRenderingConfigJson";
|
|
|
|
import {TagRenderingOptions} from "../TagRenderingOptions";
|
|
|
|
import Translation from "../../UI/i18n/Translation";
|
|
|
|
import {LayerConfigJson} from "./LayerConfigJson";
|
|
|
|
import {LayerDefinition, Preset} from "../LayerDefinition";
|
|
|
|
import {TagDependantUIElementConstructor} from "../UIElementConstructor";
|
|
|
|
import Combine from "../../UI/Base/Combine";
|
|
|
|
import {ImageCarouselWithUploadConstructor} from "../../UI/Image/ImageCarouselWithUpload";
|
|
|
|
import {ImageCarouselConstructor} from "../../UI/Image/ImageCarousel";
|
2020-08-31 02:59:47 +02:00
|
|
|
import * as drinkingWater from "../../assets/layers/drinking_water/drinking_water.json";
|
|
|
|
import * as ghostbikes from "../../assets/layers/ghost_bike/ghost_bike.json"
|
|
|
|
import * as viewpoint from "../../assets/layers/viewpoint/viewpoint.json"
|
2020-09-03 02:05:09 +02:00
|
|
|
import * as bike_parking from "../../assets/layers/bike_parking/bike_parking.json"
|
2020-09-03 19:05:18 +02:00
|
|
|
import * as birdhides from "../../assets/layers/bird_hide/birdhides.json"
|
|
|
|
|
2020-08-31 02:59:47 +02:00
|
|
|
import {Utils} from "../../Utils";
|
2020-08-30 01:13:18 +02:00
|
|
|
|
|
|
|
export class FromJSON {
|
|
|
|
|
2020-08-31 02:59:47 +02:00
|
|
|
public static sharedLayers: Map<string, LayerDefinition> = FromJSON.getSharedLayers();
|
|
|
|
|
|
|
|
private static getSharedLayers() {
|
|
|
|
const sharedLayers = new Map<string, LayerDefinition>();
|
|
|
|
|
|
|
|
const sharedLayersList = [
|
|
|
|
FromJSON.Layer(drinkingWater),
|
|
|
|
FromJSON.Layer(ghostbikes),
|
|
|
|
FromJSON.Layer(viewpoint),
|
2020-09-03 02:05:09 +02:00
|
|
|
FromJSON.Layer(bike_parking),
|
2020-09-03 19:05:18 +02:00
|
|
|
FromJSON.Layer(birdhides),
|
2020-08-31 02:59:47 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const layer of sharedLayersList) {
|
|
|
|
sharedLayers.set(layer.id, layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sharedLayers;
|
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
|
|
|
|
public static FromBase64(layoutFromBase64: string): Layout {
|
|
|
|
return FromJSON.LayoutFromJSON(JSON.parse(atob(layoutFromBase64)));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static LayoutFromJSON(json: LayoutConfigJson): Layout {
|
|
|
|
const tr = FromJSON.Translation;
|
|
|
|
|
|
|
|
const layers = json.layers.map(FromJSON.Layer);
|
|
|
|
const roaming: TagDependantUIElementConstructor[] = json.roamingRenderings?.map(FromJSON.TagRendering) ?? [];
|
|
|
|
for (const layer of layers) {
|
|
|
|
layer.elementsToShow.push(...roaming);
|
|
|
|
}
|
|
|
|
|
|
|
|
const layout = new Layout(
|
|
|
|
json.id,
|
|
|
|
typeof (json.language) === "string" ? [json.language] : json.language,
|
|
|
|
tr(json.title),
|
|
|
|
layers,
|
|
|
|
json.startZoom,
|
|
|
|
json.startLat,
|
|
|
|
json.startLon,
|
|
|
|
new Combine(["<h3>", tr(json.title), "</h3>", tr(json.description)]),
|
|
|
|
);
|
|
|
|
|
|
|
|
layout.widenFactor = json.widenFactor ?? 0.07;
|
|
|
|
layout.icon = json.icon;
|
|
|
|
layout.maintainer = json.maintainer;
|
|
|
|
layout.version = json.version;
|
|
|
|
layout.socialImage = json.socialImage;
|
|
|
|
layout.changesetMessage = json.changesetmessage;
|
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Translation(json: string | any): string | Translation {
|
|
|
|
if (json === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (typeof (json) === "string") {
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
const tr = {};
|
2020-09-02 11:37:34 +02:00
|
|
|
let keyCount = 0;
|
2020-08-30 01:13:18 +02:00
|
|
|
for (let key in json) {
|
2020-09-02 11:37:34 +02:00
|
|
|
keyCount ++;
|
2020-08-30 01:13:18 +02:00
|
|
|
tr[key] = json[key]; // I'm doing this wrong, I know
|
|
|
|
}
|
2020-09-02 11:37:34 +02:00
|
|
|
if(keyCount == 0){
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
return new Translation(tr);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TagRendering(json: TagRenderingConfigJson | string): TagDependantUIElementConstructor {
|
|
|
|
return FromJSON.TagRenderingWithDefault(json, "", undefined);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static TagRenderingWithDefault(json: TagRenderingConfigJson | string, propertyName, defaultValue: string): TagDependantUIElementConstructor {
|
2020-09-02 11:37:34 +02:00
|
|
|
if (json === undefined) {
|
2020-08-30 01:13:18 +02:00
|
|
|
if(defaultValue !== undefined){
|
2020-09-03 00:00:37 +02:00
|
|
|
console.log(`Using default value ${defaultValue} for ${propertyName}`)
|
2020-08-30 01:13:18 +02:00
|
|
|
return FromJSON.TagRendering(defaultValue);
|
|
|
|
}
|
|
|
|
throw `Tagrendering ${propertyName} is undefined...`
|
|
|
|
}
|
2020-09-02 11:37:34 +02:00
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
if (typeof json === "string") {
|
|
|
|
|
|
|
|
switch (json) {
|
|
|
|
case "picture": {
|
|
|
|
return new ImageCarouselWithUploadConstructor()
|
|
|
|
}
|
|
|
|
case "pictures": {
|
|
|
|
return new ImageCarouselWithUploadConstructor()
|
|
|
|
}
|
|
|
|
case "image": {
|
|
|
|
return new ImageCarouselWithUploadConstructor()
|
|
|
|
}
|
|
|
|
case "images": {
|
|
|
|
return new ImageCarouselWithUploadConstructor()
|
|
|
|
}
|
|
|
|
case "picturesNoUpload": {
|
|
|
|
return new ImageCarouselConstructor()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new TagRenderingOptions({
|
|
|
|
freeform: {
|
|
|
|
key: "id",
|
|
|
|
renderTemplate: json,
|
|
|
|
template: "$$$"
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let template = FromJSON.Translation(json.render);
|
|
|
|
|
|
|
|
let freeform = undefined;
|
2020-09-03 00:00:37 +02:00
|
|
|
if (json.freeform?.key && json.freeform.key !== "") {
|
2020-09-02 11:37:34 +02:00
|
|
|
// Setup the freeform
|
|
|
|
if (template === undefined) {
|
|
|
|
console.error("Freeform.key is defined, but render is not. This is not allowed.", json)
|
2020-08-30 01:13:18 +02:00
|
|
|
throw "Freeform is defined, but render is not. This is not allowed."
|
|
|
|
}
|
2020-09-02 11:37:34 +02:00
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
freeform = {
|
|
|
|
template: `$${json.freeform.type ?? "string"}$`,
|
|
|
|
renderTemplate: template,
|
|
|
|
key: json.freeform.key
|
|
|
|
};
|
|
|
|
if (json.freeform.addExtraTags) {
|
2020-09-02 11:37:34 +02:00
|
|
|
freeform.extraTags = new And(json.freeform.addExtraTags.map(FromJSON.SimpleTag))
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
|
|
|
} else if (json.render) {
|
2020-09-02 11:37:34 +02:00
|
|
|
// Template (aka rendering) is defined, but freeform.key is not. We allow an input as string
|
2020-08-30 01:13:18 +02:00
|
|
|
freeform = {
|
2020-09-02 11:37:34 +02:00
|
|
|
template: undefined, // Template to ask is undefined -> we block asking for this key
|
2020-08-30 01:13:18 +02:00
|
|
|
renderTemplate: template,
|
2020-09-02 11:37:34 +02:00
|
|
|
key: "id" // every object always has an id
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mappings = json.mappings?.map(mapping => (
|
|
|
|
{
|
|
|
|
k: FromJSON.Tag(mapping.if),
|
|
|
|
txt: FromJSON.Translation(mapping.then),
|
2020-08-31 02:59:47 +02:00
|
|
|
hideInAnswer: mapping.hideInAnswer
|
2020-08-30 01:13:18 +02:00
|
|
|
})
|
|
|
|
);
|
2020-09-02 11:37:34 +02:00
|
|
|
|
|
|
|
if(template === undefined && (mappings === undefined || mappings.length === 0)){
|
2020-09-03 00:00:37 +02:00
|
|
|
console.error("Empty tagrendering detected: no mappings nor template given", json)
|
2020-09-02 11:37:34 +02:00
|
|
|
throw "Empty tagrendering detected: no mappings nor template given"
|
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
|
2020-08-31 02:59:47 +02:00
|
|
|
|
|
|
|
let rendering = new TagRenderingOptions({
|
2020-08-30 01:13:18 +02:00
|
|
|
question: FromJSON.Translation(json.question),
|
|
|
|
freeform: freeform,
|
|
|
|
mappings: mappings
|
|
|
|
});
|
2020-08-31 02:59:47 +02:00
|
|
|
|
|
|
|
if (json.condition) {
|
|
|
|
return rendering.OnlyShowIf(FromJSON.Tag(json.condition));
|
|
|
|
}
|
|
|
|
|
|
|
|
return rendering;
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static SimpleTag(json: string): Tag {
|
2020-08-31 02:59:47 +02:00
|
|
|
const tag = Utils.SplitFirst(json, "=");
|
2020-08-30 01:13:18 +02:00
|
|
|
return new Tag(tag[0], tag[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Tag(json: AndOrTagConfigJson | string): TagsFilter {
|
2020-09-02 11:37:34 +02:00
|
|
|
if(json === undefined){
|
|
|
|
throw "Error while parsing a tag: nothing defined. Make sure all the tags are defined and at least one tag is present in a complex expression"
|
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
if (typeof (json) == "string") {
|
|
|
|
const tag = json as string;
|
|
|
|
if (tag.indexOf("!~") >= 0) {
|
2020-08-31 02:59:47 +02:00
|
|
|
const split = Utils.SplitFirst(tag, "!~");
|
|
|
|
if (split[1] === "*") {
|
2020-09-03 02:05:09 +02:00
|
|
|
split[1] = "..*"
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
|
|
|
return new RegexTag(
|
2020-08-31 02:59:47 +02:00
|
|
|
split[0],
|
|
|
|
new RegExp("^" + split[1] + "$"),
|
2020-08-30 01:13:18 +02:00
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (tag.indexOf("!=") >= 0) {
|
2020-08-31 02:59:47 +02:00
|
|
|
const split = Utils.SplitFirst(tag, "!=");
|
|
|
|
if (split[1] === "*") {
|
2020-09-03 02:05:09 +02:00
|
|
|
split[1] = "..*"
|
2020-08-31 02:59:47 +02:00
|
|
|
}
|
2020-08-30 01:13:18 +02:00
|
|
|
return new RegexTag(
|
2020-08-31 02:59:47 +02:00
|
|
|
split[0],
|
|
|
|
new RegExp("^" + split[1] + "$"),
|
2020-08-30 01:13:18 +02:00
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (tag.indexOf("~") >= 0) {
|
2020-08-31 02:59:47 +02:00
|
|
|
const split = Utils.SplitFirst(tag, "~");
|
|
|
|
if (split[1] === "*") {
|
2020-09-03 02:05:09 +02:00
|
|
|
split[1] = "..*"
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
|
|
|
return new RegexTag(
|
2020-08-31 02:59:47 +02:00
|
|
|
split[0],
|
|
|
|
new RegExp("^" + split[1] + "$")
|
2020-08-30 01:13:18 +02:00
|
|
|
);
|
|
|
|
}
|
2020-08-31 02:59:47 +02:00
|
|
|
const split = Utils.SplitFirst(tag, "=");
|
2020-08-30 01:13:18 +02:00
|
|
|
return new Tag(split[0], split[1])
|
|
|
|
}
|
|
|
|
if (json.and !== undefined) {
|
|
|
|
return new And(json.and.map(FromJSON.Tag));
|
|
|
|
}
|
|
|
|
if (json.or !== undefined) {
|
2020-09-02 11:37:34 +02:00
|
|
|
return new Or(json.or.map(FromJSON.Tag));
|
2020-08-30 01:13:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 02:59:47 +02:00
|
|
|
public static Layer(json: LayerConfigJson | string): LayerDefinition {
|
|
|
|
|
|
|
|
if (typeof (json) === "string") {
|
|
|
|
const cached = FromJSON.sharedLayers.get(json);
|
|
|
|
if (cached) {
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw "Layer not yet loaded..."
|
|
|
|
}
|
|
|
|
|
2020-09-03 00:00:37 +02:00
|
|
|
console.log("Parsing layer", json)
|
2020-08-30 01:13:18 +02:00
|
|
|
const tr = FromJSON.Translation;
|
|
|
|
const overpassTags = FromJSON.Tag(json.overpassTags);
|
|
|
|
const icon = FromJSON.TagRenderingWithDefault(json.icon, "layericon", "./assets/bug.svg");
|
2020-08-31 02:59:47 +02:00
|
|
|
const iconSize = FromJSON.TagRenderingWithDefault(json.iconSize, "iconSize", "40,40,center");
|
2020-08-30 01:13:18 +02:00
|
|
|
const color = FromJSON.TagRenderingWithDefault(json.color, "layercolor", "#0000ff");
|
|
|
|
const width = FromJSON.TagRenderingWithDefault(json.width, "layerwidth", "10");
|
2020-09-03 16:44:48 +02:00
|
|
|
|
|
|
|
let tagRenderingDefs = json.tagRenderings ?? [];
|
|
|
|
if (tagRenderingDefs.indexOf("images") < 0) {
|
|
|
|
tagRenderingDefs = ["images", ...tagRenderingDefs];
|
|
|
|
}
|
|
|
|
let tagRenderings = tagRenderingDefs.map(FromJSON.TagRendering);
|
|
|
|
|
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
const renderTags = {"id": "node/-1"}
|
|
|
|
const presets: Preset[] = json?.presets?.map(preset => {
|
|
|
|
return ({
|
|
|
|
title: tr(preset.title),
|
|
|
|
description: tr(preset.description),
|
|
|
|
tags: preset.tags.map(FromJSON.SimpleTag)
|
|
|
|
});
|
|
|
|
}) ?? [];
|
|
|
|
|
|
|
|
function style(tags) {
|
2020-09-02 11:37:34 +02:00
|
|
|
const iconSizeStr =
|
|
|
|
iconSize.GetContent(tags).txt.split(",");
|
2020-08-31 02:59:47 +02:00
|
|
|
const iconwidth = Number(iconSizeStr[0]);
|
|
|
|
const iconheight = Number(iconSizeStr[1]);
|
|
|
|
const iconmode = iconSizeStr[2];
|
|
|
|
const iconAnchor = [iconwidth / 2, iconheight / 2] // x, y
|
|
|
|
// If iconAnchor is set to [0,0], then the top-left of the icon will be placed at the geographical location
|
|
|
|
if (iconmode.indexOf("left") >= 0) {
|
|
|
|
iconAnchor[0] = 0;
|
|
|
|
}
|
|
|
|
if (iconmode.indexOf("right") >= 0) {
|
|
|
|
iconAnchor[0] = iconwidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iconmode.indexOf("top") >= 0) {
|
|
|
|
iconAnchor[1] = 0;
|
|
|
|
}
|
|
|
|
if (iconmode.indexOf("bottom") >= 0) {
|
|
|
|
iconAnchor[1] = iconheight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the anchor is always set from the center of the point
|
|
|
|
// x, y with x going right and y going down if the values are bigger
|
2020-09-03 16:44:48 +02:00
|
|
|
const popupAnchor = [0, 3 - iconAnchor[1]];
|
2020-08-31 02:59:47 +02:00
|
|
|
|
2020-08-30 01:13:18 +02:00
|
|
|
return {
|
|
|
|
color: color.GetContent(tags).txt,
|
|
|
|
weight: width.GetContent(tags).txt,
|
|
|
|
icon: {
|
2020-08-31 02:59:47 +02:00
|
|
|
iconUrl: icon.GetContent(tags).txt,
|
|
|
|
iconSize: [iconwidth, iconheight],
|
|
|
|
popupAnchor: popupAnchor,
|
|
|
|
iconAnchor: iconAnchor
|
2020-08-30 01:13:18 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const layer = new LayerDefinition(
|
|
|
|
json.id,
|
|
|
|
{
|
|
|
|
name: tr(json.name),
|
|
|
|
description: tr(json.description),
|
|
|
|
icon: icon.GetContent(renderTags).txt,
|
|
|
|
overpassFilter: overpassTags,
|
|
|
|
|
2020-09-03 00:00:37 +02:00
|
|
|
title: FromJSON.TagRendering(json.title),
|
2020-08-30 01:13:18 +02:00
|
|
|
minzoom: json.minzoom,
|
|
|
|
presets: presets,
|
2020-09-03 02:05:09 +02:00
|
|
|
elementsToShow: tagRenderings,
|
2020-08-30 01:13:18 +02:00
|
|
|
style: style,
|
|
|
|
wayHandling: json.wayHandling
|
|
|
|
|
|
|
|
}
|
|
|
|
);
|
2020-09-03 02:05:09 +02:00
|
|
|
console.log("Parsed layer is ", layer);
|
2020-08-30 01:13:18 +02:00
|
|
|
return layer;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|