mapcomplete/UI/Base/Link.ts

44 lines
1.3 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();
2021-06-16 17:09:32 +02:00
this._embeddedShow =Translations.W(embeddedShow);
this._href = href;
this._newTab = newTab;
2021-06-10 01:36:20 +02:00
2021-06-16 17:09:32 +02:00
}
2021-06-10 01:36:20 +02:00
2021-06-16 17:09:32 +02:00
protected InnerConstructElement(): HTMLElement {
const embeddedShow = this._embeddedShow?.ConstructElement();
if(embeddedShow === undefined){
return undefined;
}
2021-06-10 01:36:20 +02:00
const el = document.createElement("a")
2021-06-16 17:09:32 +02:00
if(typeof this._href === "string"){
el.href = this._href
2021-06-10 01:36:20 +02:00
}else{
2021-06-16 17:09:32 +02:00
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
}
2021-06-16 17:09:32 +02:00
AsMarkdown(): string {
// @ts-ignore
return `[${this._embeddedShow.AsMarkdown()}](${this._href.data ?? this._href})`;
2020-11-05 12:28:02 +01:00
}
}