mapcomplete/UI/BigComponents/ExtraLinkButton.ts

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

96 lines
2.8 KiB
TypeScript
Raw Normal View History

import { UIElement } from "../UIElement"
import BaseUIElement from "../BaseUIElement"
import { UIEventSource } from "../../Logic/UIEventSource"
import ExtraLinkConfig from "../../Models/ThemeConfig/ExtraLinkConfig"
import Img from "../Base/Img"
import { SubtleButton } from "../Base/SubtleButton"
import Toggle from "../Input/Toggle"
import Loc from "../../Models/Loc"
import Locale from "../i18n/Locale"
import { Utils } from "../../Utils"
2022-03-10 23:18:40 +01:00
import Svg from "../../Svg"
import Translations from "../i18n/Translations"
import { Translation } from "../i18n/Translation"
2022-03-10 23:18:40 +01:00
export default class ExtraLinkButton extends UIElement {
private readonly _config: ExtraLinkConfig
private readonly state: {
2022-03-10 23:18:40 +01:00
layoutToUse: { id: string; title: Translation }
featureSwitchWelcomeMessage: UIEventSource<boolean>
locationControl: UIEventSource<Loc>
}
2022-03-10 23:59:18 +01:00
constructor(
state: {
featureSwitchWelcomeMessage: UIEventSource<boolean>
locationControl: UIEventSource<Loc>
layoutToUse: { id: string; title: Translation }
},
config: ExtraLinkConfig
) {
super()
this.state = state
this._config = config
}
2022-03-10 23:18:40 +01:00
protected InnerRender(): BaseUIElement {
if (this._config === undefined) {
return undefined
}
2022-03-10 23:18:40 +01:00
const c = this._config
const isIframe = window !== window.top
if (c.requirements?.has("iframe") && !isIframe) {
return undefined
}
if (c.requirements?.has("no-iframe") && isIframe) {
return undefined
}
2022-03-10 23:18:40 +01:00
let link: BaseUIElement
const theme = this.state.layoutToUse?.id ?? ""
2022-07-01 00:16:05 +02:00
const basepath = window.location.host
const href = this.state.locationControl.map((loc) => {
const subs = {
...loc,
theme: theme,
2022-07-01 00:16:05 +02:00
basepath,
language: Locale.language.data,
}
return Utils.SubstituteKeys(c.href, subs)
})
2022-03-10 23:18:40 +01:00
let img: BaseUIElement = Svg.pop_out_ui()
if (c.icon !== undefined) {
img = new Img(c.icon).SetClass("h-6")
}
let text: Translation
if (c.text === undefined) {
2022-04-04 05:12:14 +02:00
text = Translations.t.general.screenToSmall.Subs({
theme: this.state.layoutToUse.title,
})
2022-03-10 23:18:40 +01:00
} else {
text = c.text.Clone()
}
2022-03-10 23:18:40 +01:00
link = new SubtleButton(img, text, {
2022-07-01 00:16:05 +02:00
url: href,
2022-03-10 23:18:40 +01:00
newTab: c.newTab,
})
if (c.requirements?.has("no-welcome-message")) {
link = new Toggle(undefined, link, this.state.featureSwitchWelcomeMessage)
}
if (c.requirements?.has("welcome-message")) {
2022-03-10 23:18:40 +01:00
link = new Toggle(link, undefined, this.state.featureSwitchWelcomeMessage)
}
2022-03-10 23:18:40 +01:00
return link
}
}