mapcomplete/UI/SlideShow.ts

92 lines
3 KiB
TypeScript
Raw Normal View History

2020-06-24 00:35:19 +02:00
import {UIElement} from "./UIElement";
2020-07-06 13:47:22 +02:00
import {FixedUiElement} from "./Base/FixedUiElement";
import {UIEventSource} from "../Logic/UIEventSource";
2020-06-24 00:35:19 +02:00
export class SlideShow extends UIElement {
private readonly _embeddedElements: UIEventSource<UIElement[]>
2020-07-07 15:08:52 +02:00
public readonly _currentSlide: UIEventSource<number> = new UIEventSource<number>(0);
2020-06-24 00:35:19 +02:00
private readonly _noimages: UIElement;
2020-07-06 13:47:22 +02:00
private _prev: FixedUiElement;
private _next: FixedUiElement;
2020-06-24 00:35:19 +02:00
constructor(
embeddedElements: UIEventSource<UIElement[]>,
noImages: UIElement) {
super(embeddedElements);
this._embeddedElements = embeddedElements;
this.ListenTo(this._currentSlide);
this._noimages = noImages;
2020-07-06 13:47:22 +02:00
const self = this;
this._prev = new FixedUiElement("<div class='prev-button'>" +
"<div class='vspan'></div>" +
"<img src='assets/arrow-left-smooth.svg' alt='Prev'/>" +
"</div>")
.onClick(() => {
const current = self._currentSlide.data;
self.MoveTo(current - 1);
});
this._next = new FixedUiElement("<div class='next-button'>" +
"<div class='vspan'></div>" +
"<img src='assets/arrow-right-smooth.svg' alt='Next'/>" +
"</div>")
.onClick(() => {
const current = self._currentSlide.data;
self.MoveTo(current + 1);
});
2020-06-24 00:35:19 +02:00
}
InnerRender(): string {
2020-06-24 00:35:19 +02:00
if (this._embeddedElements.data.length == 0) {
return this._noimages.Render();
}
if (this._embeddedElements.data.length == 1) {
return "<div class='image-slideshow'><div class='slides'><div class='slide'>" +
this._embeddedElements.data[0].Render() +
"</div></div></div>";
2020-06-24 00:35:19 +02:00
}
let slides = ""
2020-06-24 00:35:19 +02:00
for (let i = 0; i < this._embeddedElements.data.length; i++) {
let embeddedElement = this._embeddedElements.data[i];
let state = "hidden"
if (this._currentSlide.data === i) {
state = "active-slide";
}
slides += " <div class=\"slide " + state + "\">" + embeddedElement.Render() + "</div>\n";
2020-06-24 00:35:19 +02:00
}
return "<div class='image-slideshow'>"
2020-07-06 13:47:22 +02:00
+ this._prev.Render()
+ "<div class='slides'>" + slides + "</div>"
2020-07-06 13:47:22 +02:00
+ this._next.Render()
+ "</div>";
2020-06-24 00:35:19 +02:00
}
public MoveTo(index: number) {
if (index < 0) {
index = this._embeddedElements.data.length - 1;
}
index = index % this._embeddedElements.data.length;
this._currentSlide.setData(index);
}
2020-06-24 00:35:19 +02:00
InnerUpdate(htmlElement) {
2020-07-06 13:47:22 +02:00
this._next.Update();
this._prev.Update();
2020-06-24 00:35:19 +02:00
}
Activate() {
for (const embeddedElement of this._embeddedElements.data) {
embeddedElement.Activate();
}
2020-07-07 15:08:52 +02:00
this._next.Update();
this._prev.Update();
return this;
2020-06-24 00:35:19 +02:00
}
}