mapcomplete/UI/CustomGenerator/SettingsTable.ts

58 lines
2 KiB
TypeScript
Raw Permalink Normal View History

import SingleSetting from "./SingleSetting";
import {UIElement} from "../UIElement";
import {FixedUiElement} from "../Base/FixedUiElement";
import {UIEventSource} from "../../Logic/UIEventSource";
2020-09-02 11:37:34 +02:00
import PageSplit from "../Base/PageSplit";
import Combine from "../Base/Combine";
export default class SettingsTable extends UIElement {
private _col1: UIElement[] = [];
2020-09-02 11:37:34 +02:00
private _col2: UIElement[] = [];
public selectedSetting: UIEventSource<SingleSetting<any>>;
2020-09-02 11:37:34 +02:00
constructor(elements: (SingleSetting<any> | string)[],
2020-12-08 23:44:34 +01:00
currentSelectedSetting?: UIEventSource<SingleSetting<any>>) {
super(undefined);
const self = this;
this.selectedSetting = currentSelectedSetting ?? new UIEventSource<SingleSetting<any>>(undefined);
for (const element of elements) {
2020-09-02 11:37:34 +02:00
if(typeof element === "string"){
this._col1.push(new FixedUiElement(element));
this._col2.push(null);
continue;
}
let title: UIElement = element._name === undefined ? null : new FixedUiElement(element._name);
this._col1.push(title);
this._col2.push(element._value);
2020-09-02 11:37:34 +02:00
element._value.SetStyle("display:block");
element._value.IsSelected.addCallback(isSelected => {
if (isSelected) {
self.selectedSetting.setData(element);
} else if (self.selectedSetting.data === element) {
self.selectedSetting.setData(undefined);
}
})
}
}
InnerRender(): string {
2020-09-02 11:37:34 +02:00
let elements = [];
for (let i = 0; i < this._col1.length; i++) {
2020-09-02 11:37:34 +02:00
if(this._col1[i] !== null && this._col2[i] !== null){
elements.push(new PageSplit(this._col1[i], this._col2[i], 25));
}else if(this._col1[i] !== null){
elements.push(this._col1[i])
}else{
elements.push(this._col2[i])
}
}
2020-09-02 11:37:34 +02:00
return new Combine(elements).Render();
}
2020-09-02 11:37:34 +02:00
}