mapcomplete/UI/Base/Ornament.ts

45 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-01-07 04:50:12 +01:00
import {UIElement} from "../UIElement";
import {UIEventSource} from "../../Logic/UIEventSource";
import Svg from "../../Svg";
2021-01-09 00:03:21 +01:00
import State from "../../State";
2021-01-07 04:50:12 +01:00
export default class Ornament extends UIElement {
private static readonly ornamentsCount = Ornament.countOrnaments();
2021-01-09 00:03:21 +01:00
private readonly _index = new UIEventSource<string>("0")
2021-01-07 04:50:12 +01:00
2021-01-09 00:03:21 +01:00
constructor(index = undefined) {
super();
index = index ?? State.state.osmConnection.GetPreference("ornament");
this.ListenTo(index);
2021-01-07 04:50:12 +01:00
this._index = index;
this.SetClass("ornament")
const self = this;
this.onClick(() => {
2021-01-09 00:03:21 +01:00
let c = Number(index.data);
2021-01-17 23:06:05 +01:00
if (isNaN(c)) {
2021-01-09 00:03:21 +01:00
c = 0;
}
2021-01-17 23:06:05 +01:00
self._index.setData("" + ((c + 1) % (Ornament.ornamentsCount + 1)));
2021-01-07 04:50:12 +01: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 23:06:05 +01:00
const svg = Svg.All[`Ornament-Horiz-${Number(this._index.data) - 1}.svg`];
if (this._index.data == "0" || svg === undefined) {
2021-01-07 05:10:27 +01:00
return "<svg></svg>"
}
2021-01-17 23:06:05 +01:00
return svg;
2021-01-07 04:50:12 +01:00
}
}