mapcomplete/UI/BigComponents/ExtraLinkButton.ts

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

94 lines
2.8 KiB
TypeScript
Raw Normal View History

import {UIElement} from "../UIElement"
import BaseUIElement from "../BaseUIElement"
import {Store} 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 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"
interface ExtraLinkButtonState {
layout: { id: string; title: Translation }
featureSwitches: { featureSwitchWelcomeMessage: Store<boolean> },
mapProperties: {
location: Store<{ lon: number, lat: number }>;
zoom: Store<number>
}
}
2022-03-10 23:18:40 +01:00
export default class ExtraLinkButton extends UIElement {
private readonly _config: ExtraLinkConfig
private readonly state: ExtraLinkButtonState
2022-03-10 23:59:18 +01:00
constructor(
state: ExtraLinkButtonState,
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.layout?.id ?? ""
2022-07-01 00:16:05 +02:00
const basepath = window.location.host
const href = this.state.mapProperties.location.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)
}, [this.state.mapProperties.zoom])
2022-03-10 23:18:40 +01:00
let img: BaseUIElement = Svg.pop_out_svg()
2022-03-10 23:18:40 +01:00
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.layout.title,
2022-04-04 05:12:14 +02:00
})
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.featureSwitches.featureSwitchWelcomeMessage)
}
if (c.requirements?.has("welcome-message")) {
link = new Toggle(link, undefined, this.state.featureSwitches.featureSwitchWelcomeMessage)
}
2022-03-10 23:18:40 +01:00
return link
}
}