mapcomplete/UI/Base/Toggleable.ts

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

94 lines
3 KiB
TypeScript
Raw Normal View History

2021-11-25 21:31:33 +01:00
import BaseUIElement from "../BaseUIElement"
import { UIEventSource } from "../../Logic/UIEventSource"
import Combine from "./Combine"
import Title from "./Title"
import Hash from "../../Logic/Web/Hash"
2021-11-25 21:31:33 +01:00
export class Accordeon extends Combine {
constructor(toggles: Toggleable[]) {
for (const el of toggles) {
el.isVisible.addCallbackAndRun((isVisible) =>
toggles.forEach((toggle) => {
if (toggle !== el && isVisible) {
toggle.isVisible.setData(false)
}
})
2022-09-08 21:40:48 +02:00
)
2021-11-25 21:31:33 +01:00
}
2022-01-18 18:52:42 +01:00
2021-11-25 21:31:33 +01:00
super(toggles)
}
}
2021-11-25 22:16:00 +01:00
export default class Toggleable extends Combine {
2021-11-25 21:31:33 +01:00
public readonly isVisible = new UIEventSource(false)
2022-01-25 21:55:51 +01:00
constructor(
title: Title | Combine | BaseUIElement,
content: BaseUIElement,
options?: {
2022-07-13 17:58:01 +02:00
closeOnClick?: true | boolean
height?: "100vh" | string
2022-01-25 21:55:51 +01:00
}
) {
2021-11-25 22:16:00 +01:00
super([title, content])
2022-01-24 01:41:57 +01:00
content.SetClass("animate-height border-l-4 pl-2 block")
2021-11-25 22:16:00 +01:00
title.SetClass("background-subtle rounded-lg")
2021-11-25 21:31:33 +01:00
const self = this
2022-01-25 21:55:51 +01:00
this.onClick(() => {
2022-01-26 21:40:38 +01:00
if (self.isVisible.data) {
if (options?.closeOnClick ?? true) {
2022-01-25 21:55:51 +01:00
self.isVisible.setData(false)
}
2022-01-26 21:40:38 +01:00
} else {
2022-01-25 21:55:51 +01:00
self.isVisible.setData(true)
}
})
2021-11-25 22:16:00 +01:00
const contentElement = content.ConstructElement()
2022-01-26 21:40:38 +01:00
if (title instanceof Combine) {
for (const el of title.getElements()) {
if (el instanceof Title) {
2022-01-25 21:55:51 +01:00
title = el
break
}
}
}
2021-11-25 21:31:33 +01:00
2022-01-18 18:52:42 +01:00
if (title instanceof Title) {
Hash.hash.addCallbackAndRun((h) => {
2022-01-26 21:40:38 +01:00
if (h === (<Title>title).id) {
self.isVisible.setData(true)
content.RemoveClass("border-gray-300")
content.SetClass("border-red-300")
2022-01-18 18:52:42 +01:00
} else {
content.SetClass("border-gray-300")
content.RemoveClass("border-red-300")
}
})
this.isVisible.addCallbackAndRun((isVis) => {
2022-01-18 18:52:42 +01:00
if (isVis) {
2022-01-25 21:55:51 +01:00
Hash.hash.setData((<Title>title).id)
}
})
}
2022-01-18 18:52:42 +01:00
2021-11-25 21:31:33 +01:00
this.isVisible.addCallbackAndRun((isVisible) => {
if (isVisible) {
2022-07-13 17:58:01 +02:00
contentElement.style.maxHeight = options?.height ?? "100vh"
2022-01-24 01:41:57 +01:00
contentElement.style.overflowY = "auto"
2021-11-25 22:16:00 +01:00
contentElement.style["-webkit-mask-image"] = "unset"
2021-11-25 21:31:33 +01:00
} else {
2021-11-25 22:16:00 +01:00
contentElement.style["-webkit-mask-image"] =
"-webkit-gradient(linear, left top, left bottom, from(rgba(0,0,0,1)), to(rgba(0,0,0,0)))"
contentElement.style.maxHeight = "2rem"
2021-11-25 21:31:33 +01:00
}
})
}
2022-01-26 21:40:38 +01:00
public Collapse(): Toggleable {
this.isVisible.setData(false)
return this
}
2021-11-25 21:31:33 +01:00
}