mapcomplete/UI/Base/Link.ts

58 lines
1.8 KiB
TypeScript
Raw Normal View History

2020-11-06 01:58:26 +01:00
import Translations from "../i18n/Translations";
2021-06-10 01:36:20 +02:00
import BaseUIElement from "../BaseUIElement";
import {UIEventSource} from "../../Logic/UIEventSource";
2020-11-05 12:28:02 +01:00
2021-06-10 01:36:20 +02:00
export default class Link extends BaseUIElement {
2021-06-16 17:09:32 +02:00
private readonly _href: string | UIEventSource<string>;
private readonly _embeddedShow: BaseUIElement;
private readonly _newTab: boolean;
2020-11-05 12:28:02 +01:00
2021-06-12 02:58:32 +02:00
constructor(embeddedShow: BaseUIElement | string, href: string | UIEventSource<string>, newTab: boolean = false) {
2020-11-05 12:28:02 +01:00
super();
this._embeddedShow = Translations.W(embeddedShow);
2021-06-16 17:09:32 +02:00
this._href = href;
this._newTab = newTab;
2022-01-18 18:52:42 +01:00
if (this._embeddedShow === undefined) {
throw "Error: got a link where embeddedShow is undefined"
}
2021-06-10 01:36:20 +02:00
2021-06-16 17:09:32 +02:00
}
2021-06-10 01:36:20 +02:00
AsMarkdown(): string {
// @ts-ignore
return `[${this._embeddedShow.AsMarkdown()}](${this._href.data ?? this._href})`;
}
2021-06-16 17:09:32 +02:00
protected InnerConstructElement(): HTMLElement {
const embeddedShow = this._embeddedShow?.ConstructElement();
if (embeddedShow === undefined) {
2021-06-16 17:09:32 +02:00
return undefined;
}
2021-06-10 01:36:20 +02:00
const el = document.createElement("a")
if (typeof this._href === "string") {
2021-06-16 17:09:32 +02:00
el.href = this._href
} else {
this._href.addCallbackAndRun(href => {
2021-06-12 02:58:32 +02:00
el.href = href;
2021-06-10 01:36:20 +02:00
})
}
2021-06-16 17:09:32 +02:00
if (this._newTab) {
2021-06-10 01:36:20 +02:00
el.target = "_blank"
2020-11-05 12:28:02 +01:00
}
2021-06-16 17:09:32 +02:00
el.appendChild(embeddedShow)
return el;
2020-11-05 12:28:02 +01:00
}
2022-01-18 18:52:42 +01:00
public static OsmWiki(key: string, value?: string, hideKey = false) {
if (value !== undefined) {
let k = "";
2022-01-18 18:52:42 +01:00
if (!hideKey) {
k = key + "="
}
2022-01-18 18:52:42 +01:00
return new Link(k + value, `https://wiki.openstreetmap.org/wiki/Tag:${key}%3D${value}`)
}
2022-01-18 18:52:42 +01:00
return new Link(key, "https://wiki.openstreetmap.org/wiki/Key:" + key)
}
2020-11-05 12:28:02 +01:00
}