mapcomplete/UI/Base/Ornament.ts

47 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-01-07 03:50:12 +00:00
import {UIElement} from "../UIElement";
import {UIEventSource} from "../../Logic/UIEventSource";
import Svg from "../../Svg";
2021-01-08 23:03:21 +00:00
import State from "../../State";
2021-01-07 03:50:12 +00:00
export default class Ornament extends UIElement {
private static readonly ornamentsCount = Ornament.countOrnaments();
2021-01-08 23:03:21 +00:00
private readonly _index = new UIEventSource<string>("0")
2021-01-07 03:50:12 +00:00
2021-01-08 23:03:21 +00:00
constructor(index = undefined) {
super();
index = index ?? State.state.osmConnection.GetPreference("ornament");
2021-01-27 01:26:57 +00:00
this.SetClass("pt-3 pb-3 flex justify-center box-border")
2021-01-08 23:03:21 +00:00
this.ListenTo(index);
2021-01-07 03:50:12 +00:00
this._index = index;
const self = this;
this.onClick(() => {
2021-01-08 23:03:21 +00:00
let c = Number(index.data);
2021-01-17 22:06:05 +00:00
if (isNaN(c)) {
2021-01-08 23:03:21 +00:00
c = 0;
}
2021-01-17 22:06:05 +00:00
self._index.setData("" + ((c + 1) % (Ornament.ornamentsCount + 1)));
2021-01-07 03:50:12 +00:00
})
}
private static countOrnaments() {
let ornamentCount = 0;
for (const key in Svg.All) {
if (key.startsWith("Ornament-Horiz-")) {
ornamentCount++;
}
}
return ornamentCount;
}
InnerRender(): string {
2021-01-17 22:06:05 +00:00
const svg = Svg.All[`Ornament-Horiz-${Number(this._index.data) - 1}.svg`];
if (this._index.data == "0" || svg === undefined) {
2021-01-07 04:10:27 +00:00
return "<svg></svg>"
}
2021-01-17 22:06:05 +00:00
return svg;
2021-01-07 03:50:12 +00:00
}
}