mapcomplete/UI/Input/Checkboxes.ts

96 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-09-09 22:17:46 +02:00
import {InputElement} from "./InputElement";
import {UIEventSource} from "../../Logic/UIEventSource";
import {Utils} from "../../Utils";
2021-06-11 22:51:45 +02:00
import BaseUIElement from "../BaseUIElement";
2020-09-09 22:17:46 +02:00
/**
* Supports multi-input
*/
export default class CheckBoxes extends InputElement<number[]> {
2021-06-16 21:23:03 +02:00
private static _nextId = 0;
2020-09-09 22:17:46 +02:00
IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false);
2021-06-16 21:23:03 +02:00
private readonly value: UIEventSource<number[]>
private readonly _elements: BaseUIElement[];
2020-09-09 22:17:46 +02:00
2021-06-16 21:23:03 +02:00
constructor(elements: BaseUIElement[], value = new UIEventSource<number[]>([])) {
2021-06-11 22:51:45 +02:00
super();
2021-06-12 02:58:32 +02:00
this.value = value;
2021-06-16 21:23:03 +02:00
this._elements = Utils.NoNull(elements);
this.SetClass("flex flex-col")
}
IsValid(ts: number[]): boolean {
return ts !== undefined;
}
GetValue(): UIEventSource<number[]> {
return this.value;
}
protected InnerConstructElement(): HTMLElement {
2021-06-12 02:58:32 +02:00
const el = document.createElement("form")
2021-06-16 21:23:03 +02:00
const value = this.value;
const elements = this._elements;
2021-06-12 02:58:32 +02:00
for (let i = 0; i < elements.length; i++) {
2021-06-16 21:23:03 +02:00
2021-06-12 02:58:32 +02:00
let inputI = elements[i];
const input = document.createElement("input")
const id = CheckBoxes._nextId
2021-06-16 21:23:03 +02:00
CheckBoxes._nextId++;
input.id = "checkbox" + id
2021-06-12 02:58:32 +02:00
input.type = "checkbox"
input.classList.add("p-1","cursor-pointer","m-3","pl-3","mr-0")
2021-06-16 21:23:03 +02:00
2021-06-12 02:58:32 +02:00
const label = document.createElement("label")
label.htmlFor = input.id
label.appendChild(inputI.ConstructElement())
2021-06-16 21:23:03 +02:00
label.classList.add("block","w-full","p-2","cursor-pointer","bg-red")
const wrapper = document.createElement("span")
wrapper.classList.add("flex","w-full","border", "border-gray-400","m-1")
2021-06-16 21:23:03 +02:00
wrapper.appendChild(input)
wrapper.appendChild(label)
el.appendChild(wrapper)
value.addCallbackAndRunD(selectedValues => {
2021-06-16 21:23:03 +02:00
if (selectedValues.indexOf(i) >= 0) {
2021-06-12 02:58:32 +02:00
input.checked = true;
}
if(input.checked){
wrapper.classList.remove("border-gray-400")
wrapper.classList.add("border-black")
}else{
wrapper.classList.add("border-gray-400")
wrapper.classList.remove("border-black")
}
2021-06-12 02:58:32 +02:00
})
input.onchange = () => {
2021-06-16 21:23:03 +02:00
// Index = index in the list of already checked items
2021-06-12 02:58:32 +02:00
const index = value.data.indexOf(i);
2021-06-16 21:23:03 +02:00
if (input.checked && index < 0) {
2021-06-12 02:58:32 +02:00
value.data.push(i);
value.ping();
2021-06-16 21:23:03 +02:00
} else if (index >= 0) {
value.data.splice(index, 1);
2021-06-12 02:58:32 +02:00
value.ping();
}
}
}
2021-06-16 21:23:03 +02:00
return el;
2021-06-11 22:51:45 +02:00
}
2020-09-09 22:17:46 +02:00
}