mapcomplete/UI/Base/Link.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

65 lines
1.9 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 { Store, 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 {
private readonly _href: string | Store<string>
2021-06-16 17:09:32 +02:00
private readonly _embeddedShow: BaseUIElement
private readonly _newTab: boolean
2020-11-05 12:28:02 +01:00
constructor(
embeddedShow: BaseUIElement | string,
href: string | Store<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"
}
2022-04-01 12:51:55 +02:00
this.onClick(() => {})
2021-06-16 17:09:32 +02:00
}
2021-06-10 01:36:20 +02:00
2022-01-26 21:40:38 +01:00
public static OsmWiki(key: string, value?: string, hideKey = false) {
if (value !== undefined) {
let k = ""
if (!hideKey) {
k = key + "="
}
return new Link(
k + value,
`https://wiki.openstreetmap.org/wiki/Tag:${key}%3D${value}`,
true
)
2022-01-26 21:40:38 +01:00
}
return new Link(key, "https://wiki.openstreetmap.org/wiki/Key:" + key, true)
2022-01-26 21:40:38 +01: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
}
}