2021-10-19 02:31:32 +02:00
|
|
|
import PointRenderingConfigJson from "./Json/PointRenderingConfigJson";
|
|
|
|
import TagRenderingConfig from "./TagRenderingConfig";
|
|
|
|
import {TagsFilter} from "../../Logic/Tags/TagsFilter";
|
|
|
|
import SharedTagRenderings from "../../Customizations/SharedTagRenderings";
|
|
|
|
import {TagUtils} from "../../Logic/Tags/TagUtils";
|
|
|
|
import {Utils} from "../../Utils";
|
|
|
|
import Svg from "../../Svg";
|
|
|
|
import WithContextLoader from "./WithContextLoader";
|
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource";
|
|
|
|
import BaseUIElement from "../../UI/BaseUIElement";
|
|
|
|
import {FixedUiElement} from "../../UI/Base/FixedUiElement";
|
|
|
|
import Img from "../../UI/Base/Img";
|
|
|
|
import Combine from "../../UI/Base/Combine";
|
|
|
|
import {VariableUiElement} from "../../UI/Base/VariableUIElement";
|
|
|
|
|
|
|
|
export default class PointRenderingConfig extends WithContextLoader {
|
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
private static readonly allowed_location_codes = new Set<string>(["point", "centroid", "start", "end"])
|
2021-12-22 01:08:11 +01:00
|
|
|
public readonly location: Set<"point" | "centroid" | "start" | "end" | string>
|
2021-10-22 01:07:32 +02:00
|
|
|
|
2021-10-19 02:31:32 +02:00
|
|
|
public readonly icon: TagRenderingConfig;
|
2021-10-21 21:41:45 +02:00
|
|
|
public readonly iconBadges: { if: TagsFilter; then: TagRenderingConfig }[];
|
2021-10-19 02:31:32 +02:00
|
|
|
public readonly iconSize: TagRenderingConfig;
|
|
|
|
public readonly label: TagRenderingConfig;
|
|
|
|
public readonly rotation: TagRenderingConfig;
|
|
|
|
|
|
|
|
constructor(json: PointRenderingConfigJson, context: string) {
|
|
|
|
super(json, context)
|
2021-11-07 16:34:51 +01:00
|
|
|
|
|
|
|
if (typeof json.location === "string") {
|
2021-10-28 03:15:36 +02:00
|
|
|
json.location = [json.location]
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-10-22 01:07:32 +02:00
|
|
|
this.location = new Set(json.location)
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-10-28 03:15:36 +02:00
|
|
|
this.location.forEach(l => {
|
|
|
|
const allowed = PointRenderingConfig.allowed_location_codes
|
2021-11-07 16:34:51 +01:00
|
|
|
if (!allowed.has(l)) {
|
2021-10-28 03:15:36 +02:00
|
|
|
throw `A point rendering has an invalid location: '${l}' is not one of ${Array.from(allowed).join(", ")} (at ${context}.location)`
|
|
|
|
}
|
|
|
|
})
|
2021-11-07 16:34:51 +01:00
|
|
|
|
|
|
|
if (json.icon === undefined && json.label === undefined) {
|
2021-11-05 01:19:27 +01:00
|
|
|
throw `A point rendering should define at least an icon or a label`
|
|
|
|
}
|
2021-10-22 01:07:32 +02:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
if (this.location.size == 0) {
|
|
|
|
throw "A pointRendering should have at least one 'location' to defined where it should be rendered. (At " + context + ".location)"
|
2021-10-22 01:07:32 +02:00
|
|
|
}
|
2021-11-22 00:50:29 +01:00
|
|
|
this.icon = this.tr("icon", undefined);
|
2021-10-21 21:41:45 +02:00
|
|
|
this.iconBadges = (json.iconBadges ?? []).map((overlay, i) => {
|
2021-11-07 16:34:51 +01:00
|
|
|
let tr: TagRenderingConfig;
|
2021-10-21 21:41:45 +02:00
|
|
|
if (typeof overlay.then === "string" &&
|
|
|
|
SharedTagRenderings.SharedIcons.get(overlay.then) !== undefined) {
|
2021-10-19 02:31:32 +02:00
|
|
|
tr = SharedTagRenderings.SharedIcons.get(overlay.then);
|
2021-11-07 16:34:51 +01:00
|
|
|
} else {
|
2021-10-21 21:41:45 +02:00
|
|
|
tr = new TagRenderingConfig(
|
|
|
|
overlay.then,
|
|
|
|
`iconBadges.${i}`
|
|
|
|
);
|
2021-10-19 02:31:32 +02:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
if: TagUtils.Tag(overlay.if),
|
2021-10-21 21:41:45 +02:00
|
|
|
then: tr
|
2021-10-19 02:31:32 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2021-11-22 00:50:29 +01:00
|
|
|
const iconPath = this.icon?.GetRenderValue({id: "node/-1"})?.txt;
|
|
|
|
if (iconPath !== undefined && iconPath.startsWith(Utils.assets_path)) {
|
2021-10-19 02:31:32 +02:00
|
|
|
const iconKey = iconPath.substr(Utils.assets_path.length);
|
|
|
|
if (Svg.All[iconKey] === undefined) {
|
2022-01-26 21:40:38 +01:00
|
|
|
throw context + ": builtin SVG asset not found: " + iconPath;
|
2021-10-19 02:31:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.iconSize = this.tr("iconSize", "40,40,center");
|
2021-10-21 21:41:45 +02:00
|
|
|
this.label = this.tr("label", undefined);
|
2021-10-19 02:31:32 +02:00
|
|
|
this.rotation = this.tr("rotation", "0");
|
|
|
|
}
|
|
|
|
|
2021-10-21 21:41:45 +02:00
|
|
|
/**
|
|
|
|
* Given a single HTML spec (either a single image path OR "image_path_to_known_svg:fill-colour", returns a fixedUIElement containing that
|
|
|
|
* The element will fill 100% and be positioned absolutely with top:0 and left: 0
|
|
|
|
*/
|
|
|
|
private static FromHtmlSpec(htmlSpec: string, style: string, isBadge = false): BaseUIElement {
|
|
|
|
if (htmlSpec === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const match = htmlSpec.match(/([a-zA-Z0-9_]*):([^;]*)/);
|
|
|
|
if (match !== null && Svg.All[match[1] + ".svg"] !== undefined) {
|
|
|
|
const svg = (Svg.All[match[1] + ".svg"] as string)
|
|
|
|
const targetColor = match[2]
|
2022-01-06 21:35:52 +01:00
|
|
|
const img = new Img(svg.replace(/rgb\(0%,0%,0%\)/g, targetColor), true)
|
2021-10-21 21:41:45 +02:00
|
|
|
.SetStyle(style)
|
2021-11-07 16:34:51 +01:00
|
|
|
if (isBadge) {
|
2021-10-21 21:41:45 +02:00
|
|
|
img.SetClass("badge")
|
|
|
|
}
|
|
|
|
return img
|
|
|
|
} else {
|
|
|
|
return new FixedUiElement(`<img src="${htmlSpec}" style="${style}" />`);
|
|
|
|
}
|
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
|
|
|
|
private static FromHtmlMulti(multiSpec: string, rotation: string, isBadge: boolean, defaultElement: BaseUIElement = undefined) {
|
|
|
|
if (multiSpec === undefined) {
|
2021-10-21 21:41:45 +02:00
|
|
|
return defaultElement
|
|
|
|
}
|
|
|
|
const style = `width:100%;height:100%;transform: rotate( ${rotation} );display:block;position: absolute; top: 0; left: 0`;
|
|
|
|
|
|
|
|
const htmlDefs = multiSpec.trim()?.split(";") ?? []
|
|
|
|
const elements = Utils.NoEmpty(htmlDefs).map(def => PointRenderingConfig.FromHtmlSpec(def, style, isBadge))
|
|
|
|
if (elements.length === 0) {
|
|
|
|
return defaultElement
|
|
|
|
} else {
|
|
|
|
return new Combine(elements).SetClass("relative block w-full h-full")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
public ExtractImages(): Set<string> {
|
|
|
|
const parts: Set<string>[] = [];
|
|
|
|
parts.push(this.icon?.ExtractImages(true));
|
|
|
|
parts.push(
|
|
|
|
...this.iconBadges?.map((overlay) => overlay.then.ExtractImages(true))
|
|
|
|
);
|
|
|
|
|
|
|
|
const allIcons = new Set<string>();
|
|
|
|
for (const part of parts) {
|
|
|
|
part?.forEach(allIcons.add, allIcons);
|
|
|
|
}
|
|
|
|
return allIcons;
|
|
|
|
}
|
|
|
|
|
2021-10-21 21:41:45 +02:00
|
|
|
public GetSimpleIcon(tags: UIEventSource<any>): BaseUIElement {
|
|
|
|
const self = this;
|
|
|
|
if (this.icon === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return new VariableUiElement(tags.map(tags => {
|
2021-11-04 22:34:22 +01:00
|
|
|
const rotation = Utils.SubstituteKeys(self.rotation?.GetRenderValue(tags)?.txt ?? "0deg", tags)
|
2021-11-07 16:34:51 +01:00
|
|
|
|
2021-10-22 01:07:32 +02:00
|
|
|
const htmlDefs = Utils.SubstituteKeys(self.icon.GetRenderValue(tags)?.txt, tags)
|
2021-11-07 16:34:51 +01:00
|
|
|
let defaultPin: BaseUIElement = undefined
|
|
|
|
if (self.label === undefined) {
|
|
|
|
defaultPin = Svg.teardrop_with_hole_green_svg()
|
2021-10-21 21:41:45 +02:00
|
|
|
}
|
2021-11-07 16:34:51 +01:00
|
|
|
return PointRenderingConfig.FromHtmlMulti(htmlDefs, rotation, false, defaultPin)
|
2021-10-21 21:41:45 +02:00
|
|
|
})).SetClass("w-full h-full block")
|
|
|
|
}
|
|
|
|
|
2021-10-19 02:31:32 +02:00
|
|
|
public GenerateLeafletStyle(
|
|
|
|
tags: UIEventSource<any>,
|
2021-10-30 02:34:16 +02:00
|
|
|
clickable: boolean,
|
|
|
|
options?: {
|
2022-01-27 01:23:04 +01:00
|
|
|
noSize: false | boolean,
|
|
|
|
includeBadges: true | boolean
|
2021-10-30 02:34:16 +02:00
|
|
|
}
|
2021-10-21 21:41:45 +02:00
|
|
|
):
|
|
|
|
{
|
2021-10-19 02:31:32 +02:00
|
|
|
html: BaseUIElement;
|
|
|
|
iconSize: [number, number];
|
|
|
|
iconAnchor: [number, number];
|
|
|
|
popupAnchor: [number, number];
|
|
|
|
iconUrl: string;
|
|
|
|
className: string;
|
2021-10-21 21:41:45 +02:00
|
|
|
} {
|
2021-10-19 02:31:32 +02:00
|
|
|
function num(str, deflt = 40) {
|
|
|
|
const n = Number(str);
|
|
|
|
if (isNaN(n)) {
|
|
|
|
return deflt;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
function render(tr: TagRenderingConfig, deflt?: string) {
|
|
|
|
if (tags === undefined) {
|
|
|
|
return deflt
|
|
|
|
}
|
|
|
|
const str = tr?.GetRenderValue(tags.data)?.txt ?? deflt;
|
|
|
|
return Utils.SubstituteKeys(str, tags.data).replace(/{.*}/g, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
const iconSize = render(this.iconSize, "40,40,center").split(",");
|
|
|
|
|
|
|
|
const iconW = num(iconSize[0]);
|
|
|
|
let iconH = num(iconSize[1]);
|
|
|
|
const mode = iconSize[2]?.trim()?.toLowerCase() ?? "center";
|
|
|
|
|
|
|
|
let anchorW = iconW / 2;
|
|
|
|
let anchorH = iconH / 2;
|
|
|
|
if (mode === "left") {
|
|
|
|
anchorW = 0;
|
|
|
|
}
|
|
|
|
if (mode === "right") {
|
|
|
|
anchorW = iconW;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode === "top") {
|
|
|
|
anchorH = 0;
|
|
|
|
}
|
|
|
|
if (mode === "bottom") {
|
|
|
|
anchorH = iconH;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-22 00:50:29 +01:00
|
|
|
const icon = this.GetSimpleIcon(tags)
|
2022-01-27 01:23:04 +01:00
|
|
|
let badges = undefined;
|
|
|
|
if( options?.includeBadges ?? true){
|
|
|
|
badges = this.GetBadges(tags)
|
|
|
|
}
|
|
|
|
const iconAndBadges = new Combine([icon,badges ])
|
2021-10-21 21:41:45 +02:00
|
|
|
.SetClass("block relative")
|
2021-10-19 02:31:32 +02:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
if (!options?.noSize) {
|
2021-10-30 02:34:16 +02:00
|
|
|
iconAndBadges.SetStyle(`width: ${iconW}px; height: ${iconH}px`)
|
2021-11-07 16:34:51 +01:00
|
|
|
} else {
|
2021-10-30 02:34:16 +02:00
|
|
|
iconAndBadges.SetClass("w-full h-full")
|
|
|
|
}
|
2022-01-26 21:40:38 +01:00
|
|
|
|
2021-11-22 00:50:29 +01:00
|
|
|
let label = this.GetLabel(tags)
|
2022-01-26 21:40:38 +01:00
|
|
|
let htmlEl: BaseUIElement;
|
|
|
|
if (icon === undefined && label === undefined) {
|
2021-11-22 00:50:29 +01:00
|
|
|
htmlEl = undefined
|
2022-01-26 21:40:38 +01:00
|
|
|
} else if (icon === undefined) {
|
2021-12-05 05:17:29 +01:00
|
|
|
htmlEl = new Combine([label])
|
2022-01-26 21:40:38 +01:00
|
|
|
} else if (label === undefined) {
|
|
|
|
htmlEl = new Combine([iconAndBadges])
|
|
|
|
} else {
|
2021-11-22 00:50:29 +01:00
|
|
|
htmlEl = new Combine([iconAndBadges, label]).SetStyle("flex flex-col")
|
|
|
|
}
|
2021-10-19 02:31:32 +02:00
|
|
|
|
|
|
|
return {
|
2021-11-22 00:50:29 +01:00
|
|
|
html: htmlEl,
|
2021-10-21 21:41:45 +02:00
|
|
|
iconSize: [iconW, iconH],
|
|
|
|
iconAnchor: [anchorW, anchorH],
|
|
|
|
popupAnchor: [0, 3 - anchorH],
|
|
|
|
iconUrl: undefined,
|
|
|
|
className: clickable
|
|
|
|
? "leaflet-div-icon"
|
|
|
|
: "leaflet-div-icon unclickable",
|
2021-10-19 02:31:32 +02:00
|
|
|
};
|
|
|
|
}
|
2021-10-21 21:41:45 +02:00
|
|
|
|
2021-11-07 16:34:51 +01:00
|
|
|
private GetBadges(tags: UIEventSource<any>): BaseUIElement {
|
|
|
|
if (this.iconBadges.length === 0) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return new VariableUiElement(
|
|
|
|
tags.map(tags => {
|
|
|
|
|
|
|
|
const badgeElements = this.iconBadges.map(badge => {
|
|
|
|
|
|
|
|
if (!badge.if.matchesProperties(tags)) {
|
|
|
|
// Doesn't match...
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
const htmlDefs = Utils.SubstituteKeys(badge.then.GetRenderValue(tags)?.txt, tags)
|
|
|
|
const badgeElement = PointRenderingConfig.FromHtmlMulti(htmlDefs, "0", true)?.SetClass("block relative")
|
|
|
|
if (badgeElement === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return new Combine([badgeElement]).SetStyle("width: 1.5rem").SetClass("block")
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
return new Combine(badgeElements).SetClass("inline-flex h-full")
|
|
|
|
})).SetClass("absolute bottom-0 right-1/3 h-1/2 w-0")
|
|
|
|
}
|
|
|
|
|
|
|
|
private GetLabel(tags: UIEventSource<any>): BaseUIElement {
|
|
|
|
if (this.label === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
const self = this;
|
|
|
|
return new VariableUiElement(tags.map(tags => {
|
|
|
|
const label = self.label
|
|
|
|
?.GetRenderValue(tags)
|
|
|
|
?.Subs(tags)
|
|
|
|
?.SetClass("block text-center")
|
|
|
|
return new Combine([label]).SetClass("flex flex-col items-center mt-1")
|
|
|
|
}))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-10-19 02:31:32 +02:00
|
|
|
}
|