mapcomplete/UI/Base/ScrollableFullScreen.ts

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

165 lines
5.5 KiB
TypeScript
Raw Normal View History

2021-01-08 02:13:44 +01:00
import Svg from "../../Svg"
import Combine from "./Combine"
2022-12-08 02:56:49 +01:00
import {FixedUiElement} from "./FixedUiElement"
import {UIEventSource} from "../../Logic/UIEventSource"
import Hash from "../../Logic/Web/Hash"
2021-06-10 14:05:26 +02:00
import BaseUIElement from "../BaseUIElement"
import Title from "./Title"
2021-01-08 02:13:44 +01:00
/**
*
2021-06-10 01:36:20 +02:00
* The scrollableFullScreen is a bit of a peculiar component:
* - It shows a title and some contents, constructed from the respective functions passed into the constructor
* - When the element is 'activated', one clone of title+contents is attached to the fullscreen
* - The element itself will - upon rendering - also show the title and contents (allthough it'll be a different clone)
*
*
2021-01-08 02:13:44 +01:00
*/
2022-12-08 02:56:49 +01:00
export default class ScrollableFullScreen {
private static readonly empty = ScrollableFullScreen.initEmpty()
private static _currentlyOpen: ScrollableFullScreen
public isShown: UIEventSource<boolean>
2021-11-07 16:34:51 +01:00
private hashToShow: string
2021-06-10 14:05:26 +02:00
private _fullscreencomponent: BaseUIElement
private _resetScrollSignal: UIEventSource<void> = new UIEventSource<void>(undefined)
2022-09-08 21:40:48 +02:00
constructor(
title: (options: { mode: string }) => BaseUIElement,
content: (options: {
mode: string
resetScrollSignal: UIEventSource<void>
}) => BaseUIElement,
2021-10-25 20:50:59 +02:00
hashToShow: string,
2022-07-08 03:14:55 +02:00
isShown: UIEventSource<boolean> = new UIEventSource<boolean>(false),
options?: {
setHash?: true | boolean
}
) {
this.hashToShow = hashToShow
this.isShown = isShown
2021-10-25 20:50:59 +02:00
if (hashToShow === undefined) {
throw "HashToShow should be defined as it is vital for the 'back' key functionality"
}
2022-09-08 21:40:48 +02:00
const mobileOptions = {
mode: "mobile",
resetScrollSignal: this._resetScrollSignal,
}
2021-10-25 20:50:59 +02:00
this._fullscreencomponent = this.BuildComponent(
title(mobileOptions),
content(mobileOptions).SetClass("pb-20")
2022-09-08 21:40:48 +02:00
)
const self = this
2022-07-08 03:14:55 +02:00
const setHash = options?.setHash ?? true
if (setHash) {
Hash.hash.addCallback((h) => {
if (h === undefined) {
isShown.setData(false)
}
})
}
isShown.addCallback((isShown) => {
if (isShown) {
2021-12-02 22:57:27 +01:00
// We first must set the hash, then activate the panel
// If the order is wrong, this will cause the panel to disactivate again
2022-07-08 03:14:55 +02:00
if (setHash) {
Hash.hash.setData(hashToShow)
}
ScrollableFullScreen._currentlyOpen = self
2021-12-02 22:57:27 +01:00
self.Activate()
} else {
if(self.hashToShow !== undefined){
Hash.hash.setData(undefined)
}
2022-01-06 18:51:52 +01:00
// Some cleanup...
ScrollableFullScreen.collapse()
2022-01-26 21:40:38 +01:00
}
})
}
private static initEmpty(): FixedUiElement{
document.addEventListener("keyup", function (event) {
if (event.code === "Escape") {
ScrollableFullScreen.collapse()
event.preventDefault()
}
})
return new FixedUiElement("")
}
public static collapse(){
const fs = document.getElementById("fullscreen")
if (fs !== null) {
ScrollableFullScreen.empty.AttachTo("fullscreen")
fs.classList.add("hidden")
}
const opened = ScrollableFullScreen._currentlyOpen
if( opened !== undefined){
opened?.isShown?.setData(false)
}
}
2022-01-06 18:51:52 +01:00
Destroy() {
this._fullscreencomponent.Destroy()
}
2022-12-08 02:56:49 +01:00
/**
* Actually show this in the 'fullscreen'-div
* @constructor
*/
public Activate(): void {
this.isShown.setData(true)
this._fullscreencomponent.AttachTo("fullscreen")
const fs = document.getElementById("fullscreen")
ScrollableFullScreen._currentlyOpen = this
fs.classList.remove("hidden")
}
2022-01-18 18:52:42 +01:00
private BuildComponent(title: BaseUIElement, content: BaseUIElement): BaseUIElement {
2021-01-25 03:12:09 +01:00
const returnToTheMap = new Combine([
2022-02-01 19:02:46 +01:00
Svg.back_svg().SetClass("block md:hidden w-12 h-12 p-2 svg-foreground"),
Svg.close_svg().SetClass("hidden md:block w-12 h-12 p-3 svg-foreground"),
]).SetClass("rounded-full p-0 flex-shrink-0 self-center")
returnToTheMap.onClick(() => {
this.isShown.setData(false)
2022-01-06 18:51:52 +01:00
Hash.hash.setData(undefined)
})
title = new Title(title, 2)
title.SetClass(
"text-l sm:text-xl md:text-2xl w-full p-0 max-h-20vh overflow-y-auto self-center"
)
2022-09-08 21:40:48 +02:00
const contentWrapper = new Combine([content]).SetClass(
"block p-2 md:pt-4 w-full h-full overflow-y-auto desktop:max-h-65vh"
)
2022-09-08 21:40:48 +02:00
this._resetScrollSignal.addCallback((_) => {
contentWrapper.ScrollToTop()
})
2022-09-08 21:40:48 +02:00
return new Combine([
2021-01-24 18:33:16 +01:00
new Combine([
new Combine([returnToTheMap, title]).SetClass(
"border-b-1 border-black shadow bg-white flex flex-shrink-0 pt-1 pb-1 md:pt-0 md:pb-0"
),
contentWrapper,
// We add an ornament which takes around 5em. This is in order to make sure the Web UI doesn't hide
]).SetClass("flex flex-col h-full relative bg-white"),
]).SetClass(
"fixed top-0 left-0 right-0 h-screen w-screen desktop:max-h-65vh md:w-auto md:relative z-above-controls md:rounded-xl overflow-hidden"
2022-09-08 21:40:48 +02:00
)
2021-01-25 03:12:09 +01:00
}
2021-01-08 02:13:44 +01:00
}