2022-06-05 02:24:14 +02:00
|
|
|
import { Store, UIEventSource } from "../../Logic/UIEventSource"
|
2021-06-10 01:36:20 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement"
|
|
|
|
import { VariableUiElement } from "../Base/VariableUIElement"
|
2021-10-15 05:20:02 +02:00
|
|
|
import Lazy from "../Base/Lazy"
|
2021-06-10 01:36:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The 'Toggle' is a UIElement showing either one of two elements, depending on the state.
|
|
|
|
* It can be used to implement e.g. checkboxes or collapsible elements
|
|
|
|
*/
|
2021-09-09 00:05:51 +02:00
|
|
|
export default class Toggle extends VariableUiElement {
|
2022-06-05 02:24:14 +02:00
|
|
|
public readonly isEnabled: Store<boolean>
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-06-06 19:37:22 +02:00
|
|
|
constructor(
|
|
|
|
showEnabled: string | BaseUIElement,
|
|
|
|
showDisabled: string | BaseUIElement,
|
|
|
|
isEnabled: Store<boolean>
|
|
|
|
) {
|
2022-04-28 00:32:15 +02:00
|
|
|
super(isEnabled?.map((isEnabled) => (isEnabled ? showEnabled : showDisabled)))
|
2021-06-12 02:58:32 +02:00
|
|
|
this.isEnabled = isEnabled
|
2021-06-14 02:39:23 +02:00
|
|
|
}
|
2021-09-09 00:05:51 +02:00
|
|
|
|
2022-06-05 02:24:14 +02:00
|
|
|
public static If(condition: Store<boolean>, constructor: () => BaseUIElement): BaseUIElement {
|
2021-11-07 16:34:51 +01:00
|
|
|
if (constructor === undefined) {
|
2021-10-15 05:20:02 +02:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return new Toggle(new Lazy(constructor), undefined, condition)
|
2021-11-07 16:34:51 +01:00
|
|
|
}
|
2022-06-05 02:24:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as `Toggle`, but will swap on click
|
|
|
|
*/
|
|
|
|
export class ClickableToggle extends Toggle {
|
2023-01-17 02:50:13 +01:00
|
|
|
public declare readonly isEnabled: UIEventSource<boolean>
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-06-05 02:24:14 +02:00
|
|
|
constructor(
|
|
|
|
showEnabled: string | BaseUIElement,
|
|
|
|
showDisabled: string | BaseUIElement,
|
|
|
|
isEnabled: UIEventSource<boolean> = new UIEventSource<boolean>(false)
|
|
|
|
) {
|
|
|
|
super(showEnabled, showDisabled, isEnabled)
|
|
|
|
this.isEnabled = isEnabled
|
|
|
|
}
|
2022-09-08 21:40:48 +02:00
|
|
|
|
2022-06-05 02:24:14 +02:00
|
|
|
public ToggleOnClick(): ClickableToggle {
|
2021-11-07 16:34:51 +01:00
|
|
|
const self = this
|
|
|
|
this.onClick(() => {
|
|
|
|
self.isEnabled.setData(!self.isEnabled.data)
|
|
|
|
})
|
|
|
|
return this
|
2021-10-15 05:20:02 +02:00
|
|
|
}
|
2021-06-10 01:36:20 +02:00
|
|
|
}
|