2023-05-14 03:24:13 +02:00
|
|
|
import {InputElement} from "./InputElement"
|
|
|
|
import {UIEventSource} from "../../Logic/UIEventSource"
|
|
|
|
import {Utils} from "../../Utils"
|
2021-07-26 18:00:21 +02:00
|
|
|
import BaseUIElement from "../BaseUIElement"
|
2022-03-31 03:30:20 +02:00
|
|
|
import InputElementMap from "./InputElementMap"
|
2022-10-27 01:50:41 +02:00
|
|
|
import Translations from "../i18n/Translations"
|
2022-03-31 03:30:20 +02:00
|
|
|
|
2023-05-08 23:53:52 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2022-07-08 03:14:55 +02:00
|
|
|
export class CheckBox extends InputElementMap<number[], boolean> {
|
2022-10-27 01:50:41 +02:00
|
|
|
constructor(el: BaseUIElement | string, defaultValue?: boolean) {
|
2022-03-31 03:30:20 +02:00
|
|
|
super(
|
2022-09-27 18:48:02 +02:00
|
|
|
new CheckBoxes([Translations.W(el)]),
|
2022-03-31 03:30:20 +02:00
|
|
|
(x0, x1) => x0 === x1,
|
|
|
|
(t) => t.length > 0,
|
2022-07-08 03:14:55 +02:00
|
|
|
(x) => (x ? [0] : [])
|
2022-03-31 03:30:20 +02:00
|
|
|
)
|
|
|
|
if (defaultValue !== undefined) {
|
|
|
|
this.GetValue().setData(defaultValue)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-09 22:17:46 +02:00
|
|
|
|
|
|
|
/**
|
2022-06-03 01:33:41 +02:00
|
|
|
* A list of individual checkboxes
|
|
|
|
* The value will contain the indexes of the selected checkboxes
|
2020-09-09 22:17:46 +02:00
|
|
|
*/
|
2020-10-02 19:00:24 +02:00
|
|
|
export default class CheckBoxes extends InputElement<number[]> {
|
2021-07-26 18:00:21 +02:00
|
|
|
private static _nextId = 0
|
|
|
|
private readonly value: UIEventSource<number[]>
|
|
|
|
private readonly _elements: BaseUIElement[]
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-27 17:00:05 +02:00
|
|
|
constructor(elements: BaseUIElement[], value = new UIEventSource<number[]>([])) {
|
2021-07-26 18:00:21 +02:00
|
|
|
super()
|
|
|
|
this.value = value
|
|
|
|
this._elements = Utils.NoNull(elements)
|
|
|
|
this.SetClass("flex flex-col")
|
2021-07-26 17:59:15 +02:00
|
|
|
}
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 17:59:15 +02:00
|
|
|
IsValid(ts: number[]): boolean {
|
2021-07-26 18:00:21 +02:00
|
|
|
return ts !== undefined
|
2021-07-26 17:59:15 +02:00
|
|
|
}
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 17:59:15 +02:00
|
|
|
GetValue(): UIEventSource<number[]> {
|
2021-07-26 18:00:21 +02:00
|
|
|
return this.value
|
2021-07-26 17:59:15 +02:00
|
|
|
}
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 17:59:15 +02:00
|
|
|
protected InnerConstructElement(): HTMLElement {
|
2021-09-30 21:41:21 +02:00
|
|
|
const formTag = document.createElement("form")
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 18:00:21 +02:00
|
|
|
const value = this.value
|
|
|
|
const elements = this._elements
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 17:59:15 +02:00
|
|
|
for (let i = 0; i < elements.length; i++) {
|
2021-07-26 18:00:21 +02:00
|
|
|
let inputI = elements[i]
|
|
|
|
const input = document.createElement("input")
|
|
|
|
const id = CheckBoxes._nextId
|
|
|
|
CheckBoxes._nextId++
|
|
|
|
input.id = "checkbox" + id
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 18:00:21 +02:00
|
|
|
input.type = "checkbox"
|
|
|
|
input.classList.add("p-1", "cursor-pointer", "m-3", "pl-3", "mr-0")
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 18:00:21 +02:00
|
|
|
const label = document.createElement("label")
|
|
|
|
label.htmlFor = input.id
|
2023-05-11 02:17:41 +02:00
|
|
|
label.appendChild(input)
|
2021-07-26 18:00:21 +02:00
|
|
|
label.appendChild(inputI.ConstructElement())
|
2021-07-26 17:59:15 +02:00
|
|
|
label.classList.add("block", "w-full", "p-2", "cursor-pointer", "bg-red")
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2023-05-14 03:24:13 +02:00
|
|
|
formTag.appendChild(label)
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 17:59:15 +02:00
|
|
|
value.addCallbackAndRunD((selectedValues) => {
|
2022-06-03 01:33:41 +02:00
|
|
|
input.checked = selectedValues.indexOf(i) >= 0
|
2021-06-12 02:58:32 +02:00
|
|
|
|
2021-07-26 17:59:15 +02:00
|
|
|
if (input.checked) {
|
2023-05-14 03:24:13 +02:00
|
|
|
label.classList.add("checked")
|
2021-07-26 17:59:15 +02:00
|
|
|
} else {
|
2023-05-14 03:24:13 +02:00
|
|
|
label.classList.remove("checked")
|
2021-07-26 17:59:15 +02:00
|
|
|
}
|
2021-07-26 18:00:21 +02:00
|
|
|
})
|
2021-07-26 17:45:54 +02:00
|
|
|
|
2021-07-26 17:59:15 +02:00
|
|
|
input.onchange = () => {
|
|
|
|
// Index = index in the list of already checked items
|
2021-07-26 18:00:21 +02:00
|
|
|
const index = value.data.indexOf(i)
|
2021-07-26 17:59:15 +02:00
|
|
|
if (input.checked && index < 0) {
|
2021-07-26 18:00:21 +02:00
|
|
|
value.data.push(i)
|
|
|
|
value.ping()
|
2021-07-26 17:59:15 +02:00
|
|
|
} else if (index >= 0) {
|
2021-07-26 18:00:21 +02:00
|
|
|
value.data.splice(index, 1)
|
|
|
|
value.ping()
|
2021-07-26 17:59:15 +02:00
|
|
|
}
|
2021-07-26 18:00:21 +02:00
|
|
|
}
|
2021-07-26 17:45:54 +02:00
|
|
|
}
|
2021-06-11 22:51:45 +02:00
|
|
|
|
2021-09-30 21:41:21 +02:00
|
|
|
return formTag
|
2021-07-26 17:59:15 +02:00
|
|
|
}
|
2021-07-26 17:45:54 +02:00
|
|
|
}
|