Newer
Older
const translator = require("ks-cheap-translator")
class ImageCarousel {
constructor(props) {
this.props = props;
this.id = this.props.images.join("").replace(/[\s\./]/g, "");
this.state = {
showImageIndex: 0,
};
this.RUN_INTERVAL = 5000;
let { showImageIndex } = this.state;
const { images } = this.props;
this.state.showImageIndex = showImageIndex < images.length - 1 ? ++showImageIndex : 0;
obj2htm.registerAsyncSubscription(this.id, function () {
clearInterval(this.runningInterval);
this.state.showImageIndex = i;
refresh() {
obj2htm.subRender(this.render_image(), document.getElementById(this.id + "-img-element"), {
mode: "replace",
});
obj2htm.subRender(this.render_bullets(), document.getElementById(this.id + "-bullets"), {
render_image() {
return {
tag: "img",
id: this.id + "-img-element",
property: "image",
alt: `image carousel ${this.props.images[this.state.showImageIndex].replace(/\.[A-Za-z]+/, "")}`,
src: this.props.images[this.state.showImageIndex],
};
}
render_bullets() {
return this.props.images.length > 1 ? {
tag: "div",
id: this.id + "-bullets",
class: "carousel-bullets",
contents: this.props.images.map((_, i) => {
const active = this.state.showImageIndex === i;
return {
tag: "span",
class: `bullet ${active ? "active" : ""}`,
onclick: this.set_image_index.bind(this, i),
};
}),
} : { tag: "span" };
}
render() {
return {
tag: "div",
id: this.id,
class: "image-carousel",
contents: [