2020-08-08 17:50:43 +02:00
|
|
|
import {TagRenderingOptions} from "../TagRenderingOptions";
|
|
|
|
import {LayerDefinition, Preset} from "../LayerDefinition";
|
|
|
|
import {Layout} from "../Layout";
|
|
|
|
import Translation from "../../UI/i18n/Translation";
|
|
|
|
import Combine from "../../UI/Base/Combine";
|
2020-08-22 02:12:46 +02:00
|
|
|
import {And, Tag} from "../../Logic/TagsFilter";
|
2020-08-08 17:50:43 +02:00
|
|
|
import FixedText from "../Questions/FixedText";
|
|
|
|
import {ImageCarouselWithUploadConstructor} from "../../UI/Image/ImageCarouselWithUpload";
|
2020-08-22 02:12:46 +02:00
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
|
|
|
import {TagDependantUIElementConstructor} from "../UIElementConstructor";
|
2020-08-23 02:26:17 +02:00
|
|
|
import {Map} from "../Layers/Map";
|
2020-08-23 16:59:06 +02:00
|
|
|
import {UIElement} from "../../UI/UIElement";
|
2020-08-26 15:36:04 +02:00
|
|
|
import Translations from "../../UI/i18n/Translations";
|
2020-08-08 17:50:43 +02:00
|
|
|
|
|
|
|
|
2020-08-22 02:12:46 +02:00
|
|
|
export interface TagRenderingConfigJson {
|
2020-08-08 21:17:17 +02:00
|
|
|
// If this key is present, then...
|
|
|
|
key?: string,
|
|
|
|
// Use this string to render
|
2020-08-23 16:59:06 +02:00
|
|
|
render?: string | any,
|
2020-08-08 21:17:17 +02:00
|
|
|
// One of string, int, nat, float, pfloat, email, phone. Default: string
|
|
|
|
type?: string,
|
|
|
|
// If it is not known (and no mapping below matches), this question is asked; a textfield is inserted in the rendering above
|
2020-08-23 16:59:06 +02:00
|
|
|
question?: string | any,
|
2020-08-08 21:17:17 +02:00
|
|
|
// If a value is added with the textfield, this extra tag is addded. Optional field
|
2020-08-23 02:26:17 +02:00
|
|
|
addExtraTags?: string | { k: string, v: string }[];
|
|
|
|
// Extra tags: rendering is only shown/asked if these tags are present
|
|
|
|
condition?: string;
|
2020-08-08 21:17:17 +02:00
|
|
|
// Alternatively, these tags are shown if they match - even if the key above is not there
|
|
|
|
// If unknown, these become a radio button
|
|
|
|
mappings?:
|
|
|
|
{
|
|
|
|
if: string,
|
2020-08-23 16:59:06 +02:00
|
|
|
then: string | any
|
2020-08-08 21:17:17 +02:00
|
|
|
}[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LayerConfigJson {
|
2020-08-26 20:11:43 +02:00
|
|
|
name: string;
|
2020-08-23 02:26:17 +02:00
|
|
|
title: string | any | TagRenderingConfigJson;
|
|
|
|
description: string | any;
|
|
|
|
minzoom: number | string,
|
|
|
|
icon?: TagRenderingConfigJson;
|
|
|
|
color?: TagRenderingConfigJson;
|
|
|
|
width?: TagRenderingConfigJson;
|
|
|
|
overpassTags: string | { k: string, v: string }[];
|
2020-08-23 16:59:06 +02:00
|
|
|
wayHandling?: number,
|
|
|
|
presets: {
|
|
|
|
tags: string,
|
|
|
|
title: string | any,
|
|
|
|
description?: string | any,
|
|
|
|
icon?: string
|
|
|
|
}[],
|
2020-08-08 21:17:17 +02:00
|
|
|
tagRenderings: TagRenderingConfigJson []
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface LayoutConfigJson {
|
2020-08-23 16:59:06 +02:00
|
|
|
widenFactor?: number;
|
2020-08-08 21:17:17 +02:00
|
|
|
name: string;
|
2020-08-23 16:59:06 +02:00
|
|
|
title: string | any;
|
|
|
|
description: string | any;
|
2020-08-22 02:12:46 +02:00
|
|
|
maintainer: string;
|
2020-08-23 16:59:06 +02:00
|
|
|
language: string | string[];
|
2020-08-08 21:17:17 +02:00
|
|
|
layers: LayerConfigJson[],
|
2020-08-23 16:59:06 +02:00
|
|
|
startZoom: string | number;
|
|
|
|
startLat: string | number;
|
|
|
|
startLon: string | number;
|
2020-08-08 21:17:17 +02:00
|
|
|
/**
|
|
|
|
* Either a URL or a base64 encoded value (which should include 'data:image/svg+xml;base64,'
|
|
|
|
*/
|
|
|
|
icon: string;
|
|
|
|
}
|
|
|
|
|
2020-08-08 17:50:43 +02:00
|
|
|
export class CustomLayoutFromJSON {
|
|
|
|
|
|
|
|
|
|
|
|
public static FromQueryParam(layoutFromBase64: string): Layout {
|
2020-08-22 02:12:46 +02:00
|
|
|
return CustomLayoutFromJSON.LayoutFromJSON(JSON.parse(atob(layoutFromBase64)));
|
2020-08-08 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 02:26:17 +02:00
|
|
|
public static TagRenderingFromJson(json: TagRenderingConfigJson): TagDependantUIElementConstructor {
|
2020-08-08 17:50:43 +02:00
|
|
|
|
2020-08-22 17:33:08 +02:00
|
|
|
if(json === undefined){
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-08-08 17:50:43 +02:00
|
|
|
if (typeof (json) === "string") {
|
|
|
|
return new FixedText(json);
|
|
|
|
}
|
|
|
|
|
|
|
|
let freeform = undefined;
|
2020-08-22 02:12:46 +02:00
|
|
|
if (json.render !== undefined) {
|
2020-08-08 17:50:43 +02:00
|
|
|
const type = json.type ?? "text";
|
2020-08-22 02:12:46 +02:00
|
|
|
let renderTemplate = CustomLayoutFromJSON.MaybeTranslation(json.render);;
|
|
|
|
const template = renderTemplate.replace("{" + json.key + "}", "$" + type + "$");
|
|
|
|
if(type === "url"){
|
|
|
|
renderTemplate = json.render.replace("{" + json.key + "}",
|
|
|
|
`<a href='{${json.key}}' target='_blank'>{${json.key}}</a>`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-08 17:50:43 +02:00
|
|
|
freeform = {
|
|
|
|
key: json.key,
|
2020-08-22 02:12:46 +02:00
|
|
|
template: template,
|
|
|
|
renderTemplate: renderTemplate,
|
2020-08-08 17:50:43 +02:00
|
|
|
extraTags: CustomLayoutFromJSON.TagsFromJson(json.addExtraTags),
|
|
|
|
}
|
2020-08-22 02:12:46 +02:00
|
|
|
if (freeform.key === "*") {
|
|
|
|
freeform.key = "id"; // Id is always there -> always take the rendering. Used for 'icon' and 'stroke'
|
|
|
|
}
|
2020-08-08 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mappings = undefined;
|
|
|
|
if (json.mappings !== undefined) {
|
|
|
|
mappings = [];
|
|
|
|
for (const mapping of json.mappings) {
|
|
|
|
mappings.push({
|
2020-08-22 02:12:46 +02:00
|
|
|
k: new And(CustomLayoutFromJSON.TagsFromJson(mapping.if)),
|
|
|
|
txt: CustomLayoutFromJSON.MaybeTranslation(mapping.then)
|
2020-08-08 17:50:43 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-22 02:12:46 +02:00
|
|
|
const rendering = new TagRenderingOptions({
|
|
|
|
question: CustomLayoutFromJSON.MaybeTranslation(json.question),
|
2020-08-08 17:50:43 +02:00
|
|
|
freeform: freeform,
|
|
|
|
mappings: mappings
|
2020-08-22 02:12:46 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (json.condition) {
|
|
|
|
const conditionTags: Tag[] = CustomLayoutFromJSON.TagsFromJson(json.condition);
|
|
|
|
return rendering.OnlyShowIf(new And(conditionTags));
|
|
|
|
}
|
|
|
|
return rendering;
|
2020-08-08 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private static PresetFromJson(layout: any, preset: any): Preset {
|
|
|
|
const t = CustomLayoutFromJSON.MaybeTranslation;
|
|
|
|
const tags = CustomLayoutFromJSON.TagsFromJson;
|
|
|
|
return {
|
2020-08-22 02:12:46 +02:00
|
|
|
icon: preset.icon ?? CustomLayoutFromJSON.TagRenderingFromJson(layout.icon),
|
2020-08-08 17:50:43 +02:00
|
|
|
tags: tags(preset.tags) ?? tags(layout.overpassTags),
|
|
|
|
title: t(preset.title) ?? t(layout.title),
|
|
|
|
description: t(preset.description) ?? t(layout.description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 02:26:17 +02:00
|
|
|
private static StyleFromJson(layout: LayerConfigJson): ((tags: any) => {
|
2020-08-08 17:50:43 +02:00
|
|
|
color: string,
|
|
|
|
weight?: number,
|
|
|
|
icon: {
|
|
|
|
iconUrl: string,
|
|
|
|
iconSize: number[],
|
|
|
|
},
|
|
|
|
}) {
|
2020-08-22 02:12:46 +02:00
|
|
|
const iconRendering: TagDependantUIElementConstructor = CustomLayoutFromJSON.TagRenderingFromJson(layout.icon);
|
|
|
|
const colourRendering = CustomLayoutFromJSON.TagRenderingFromJson(layout.color);
|
2020-08-22 17:33:08 +02:00
|
|
|
let thickness = CustomLayoutFromJSON.TagRenderingFromJson(layout.width);
|
|
|
|
|
2020-08-22 02:12:46 +02:00
|
|
|
|
2020-08-08 17:50:43 +02:00
|
|
|
return (tags) => {
|
2020-08-22 02:12:46 +02:00
|
|
|
const iconUrl = iconRendering.GetContent(tags);
|
2020-08-23 01:49:19 +02:00
|
|
|
const stroke = colourRendering.GetContent(tags) ?? "#00f";
|
2020-08-22 17:33:08 +02:00
|
|
|
let weight = parseInt(thickness?.GetContent(tags)) ?? 10;
|
|
|
|
if(isNaN(weight)){
|
|
|
|
weight = 10;
|
|
|
|
}
|
2020-08-08 17:50:43 +02:00
|
|
|
return {
|
2020-08-22 02:12:46 +02:00
|
|
|
color: stroke,
|
2020-08-22 17:33:08 +02:00
|
|
|
weight: weight,
|
2020-08-08 17:50:43 +02:00
|
|
|
icon: {
|
2020-08-22 02:12:46 +02:00
|
|
|
iconUrl: iconUrl,
|
2020-08-08 17:50:43 +02:00
|
|
|
iconSize: [40, 40],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-08 21:17:17 +02:00
|
|
|
private static TagFromJson(json: string | { k: string, v: string }): Tag {
|
2020-08-08 17:50:43 +02:00
|
|
|
if (json === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-08-22 12:44:11 +02:00
|
|
|
if (typeof (json) !== "string") {
|
|
|
|
return new Tag(json.k.trim(), json.v.trim())
|
|
|
|
}
|
2020-08-22 02:12:46 +02:00
|
|
|
|
2020-08-22 12:44:11 +02:00
|
|
|
let kv: string[] = undefined;
|
|
|
|
let invert = false;
|
2020-08-23 01:49:19 +02:00
|
|
|
let regex = false;
|
2020-08-22 12:44:11 +02:00
|
|
|
if (json.indexOf("!=") >= 0) {
|
|
|
|
kv = json.split("!=");
|
|
|
|
invert = true;
|
2020-08-23 01:49:19 +02:00
|
|
|
} else if (json.indexOf("~=") >= 0) {
|
|
|
|
kv = json.split("~=");
|
|
|
|
regex = true;
|
2020-08-22 12:44:11 +02:00
|
|
|
} else {
|
|
|
|
kv = json.split("=");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kv.length !== 2) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (kv[0].trim() === "") {
|
|
|
|
return undefined;
|
2020-08-08 17:50:43 +02:00
|
|
|
}
|
2020-08-23 01:49:19 +02:00
|
|
|
let v = kv[1].trim();
|
|
|
|
if(v.startsWith("/") && v.endsWith("/")){
|
|
|
|
v = v.substr(1, v.length - 2);
|
|
|
|
regex = true;
|
|
|
|
}
|
|
|
|
return new Tag(kv[0].trim(), regex ? new RegExp(v): v, invert);
|
2020-08-08 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
2020-08-22 02:12:46 +02:00
|
|
|
public static TagsFromJson(json: string | { k: string, v: string }[]): Tag[] {
|
|
|
|
if (json === undefined) {
|
2020-08-08 17:50:43 +02:00
|
|
|
return undefined;
|
|
|
|
}
|
2020-08-22 02:12:46 +02:00
|
|
|
if (json === "") {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
let tags = [];
|
2020-08-08 17:50:43 +02:00
|
|
|
if (typeof (json) === "string") {
|
2020-08-22 02:12:46 +02:00
|
|
|
tags = json.split("&").map(CustomLayoutFromJSON.TagFromJson);
|
|
|
|
} else {
|
2020-08-23 02:26:17 +02:00
|
|
|
tags = json.map(x => {CustomLayoutFromJSON.TagFromJson(x)});
|
2020-08-22 02:12:46 +02:00
|
|
|
}
|
|
|
|
for (const tag of tags) {
|
|
|
|
if (tag === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-08-08 17:50:43 +02:00
|
|
|
}
|
2020-08-22 02:12:46 +02:00
|
|
|
return tags;
|
2020-08-08 17:50:43 +02:00
|
|
|
}
|
|
|
|
|
2020-08-23 02:26:17 +02:00
|
|
|
private static LayerFromJson(json: LayerConfigJson): LayerDefinition {
|
2020-08-08 17:50:43 +02:00
|
|
|
const t = CustomLayoutFromJSON.MaybeTranslation;
|
|
|
|
const tr = CustomLayoutFromJSON.TagRenderingFromJson;
|
2020-08-22 02:12:46 +02:00
|
|
|
const tags = CustomLayoutFromJSON.TagsFromJson(json.overpassTags);
|
|
|
|
// We run the icon rendering with the bare minimum of tags (the overpass tags) to get the actual icon
|
2020-08-26 20:11:43 +02:00
|
|
|
const icon = CustomLayoutFromJSON.TagRenderingFromJson(json.icon).GetContent({id:"node/-1"});
|
2020-08-22 02:12:46 +02:00
|
|
|
|
2020-08-26 20:11:43 +02:00
|
|
|
// @ts-ignore
|
|
|
|
const id = json.name?.replace(/[^a-zA-Z0-9_-]/g,'') ?? json.id;
|
2020-08-08 17:50:43 +02:00
|
|
|
return new LayerDefinition(
|
2020-08-26 20:11:43 +02:00
|
|
|
id,
|
2020-08-08 17:50:43 +02:00
|
|
|
{
|
|
|
|
description: t(json.description),
|
2020-08-26 20:11:43 +02:00
|
|
|
name: Translations.WT(t(json.name)),
|
2020-08-22 02:12:46 +02:00
|
|
|
icon: icon,
|
2020-08-23 02:26:17 +02:00
|
|
|
minzoom: parseInt(""+json.minzoom),
|
2020-08-22 02:12:46 +02:00
|
|
|
title: tr(json.title),
|
2020-08-08 17:50:43 +02:00
|
|
|
presets: json.presets.map((preset) => {
|
|
|
|
return CustomLayoutFromJSON.PresetFromJson(json, preset)
|
|
|
|
}),
|
|
|
|
elementsToShow:
|
|
|
|
[new ImageCarouselWithUploadConstructor()].concat(json.tagRenderings.map(tr)),
|
2020-08-22 02:12:46 +02:00
|
|
|
overpassFilter: new And(tags),
|
2020-08-23 02:26:17 +02:00
|
|
|
wayHandling: parseInt(""+json.wayHandling) ?? LayerDefinition.WAYHANDLING_CENTER_AND_WAY,
|
2020-08-08 17:50:43 +02:00
|
|
|
maxAllowedOverlapPercentage: 0,
|
2020-08-23 02:26:17 +02:00
|
|
|
style: CustomLayoutFromJSON.StyleFromJson(json)
|
2020-08-08 17:50:43 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static MaybeTranslation(json: any): Translation | string {
|
|
|
|
if (json === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
if (typeof (json) === "string") {
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
return new Translation(json);
|
|
|
|
}
|
|
|
|
|
2020-08-23 02:26:17 +02:00
|
|
|
public static LayoutFromJSON(json: LayoutConfigJson) {
|
2020-08-08 17:50:43 +02:00
|
|
|
const t = CustomLayoutFromJSON.MaybeTranslation;
|
2020-08-23 16:59:06 +02:00
|
|
|
let languages : string[] ;
|
2020-08-22 02:12:46 +02:00
|
|
|
if(typeof (json.language) === "string"){
|
|
|
|
languages = [json.language];
|
2020-08-23 16:59:06 +02:00
|
|
|
}else{
|
|
|
|
languages = json.language
|
2020-08-22 02:12:46 +02:00
|
|
|
}
|
2020-08-08 17:50:43 +02:00
|
|
|
const layout = new Layout(json.name,
|
2020-08-22 02:12:46 +02:00
|
|
|
languages,
|
2020-08-08 17:50:43 +02:00
|
|
|
t(json.title),
|
|
|
|
json.layers.map(CustomLayoutFromJSON.LayerFromJson),
|
2020-08-23 16:59:06 +02:00
|
|
|
parseInt(""+json.startZoom),
|
|
|
|
parseFloat(""+json.startLat),
|
|
|
|
parseFloat(""+json.startLon),
|
2020-08-08 17:50:43 +02:00
|
|
|
new Combine(['<h3>', t(json.title), '</h3><br/>', t(json.description)])
|
|
|
|
);
|
|
|
|
layout.icon = json.icon;
|
2020-08-22 02:12:46 +02:00
|
|
|
layout.maintainer = json.maintainer;
|
2020-08-23 02:26:17 +02:00
|
|
|
layout.widenFactor = parseFloat(""+json.widenFactor) ?? 0.03;
|
|
|
|
if(isNaN(layout.widenFactor)){
|
|
|
|
layout.widenFactor = 0.03;
|
|
|
|
}
|
2020-08-23 01:49:19 +02:00
|
|
|
if (layout.widenFactor > 0.1) {
|
|
|
|
layout.widenFactor = 0.1;
|
|
|
|
}
|
2020-08-08 17:50:43 +02:00
|
|
|
return layout;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|