Newer
Older
class ImageCarousel {
constructor(props) {
this.props = props;
this.id = this.props.images.join("").replace(/[\s\./]/g, "");
this.toggle_fullscreen = this.toggle_fullscreen.bind(this);
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;
toggle_fullscreen() {
this.state.fullscreen = !this.state.fullscreen;
document.getElementById(this.id).classList.toggle("fullscreen");
document.body.style.overflow = this.state.fullscreen ? "hidden" : "initial";
if (this.state.fullscreen) {
window.addEventListener("keydown", this.toggle_fullscreen);
} else {
window.removeEventListener("keydown", this.toggle_fullscreen);
}
}
refresh() {
obj2htm.subRender(this.render_image(), document.getElementById(this.id + "-img-element"), {
mode: "replace",
});
obj2htm.subRender(this.render_bullets(), document.getElementById(this.id + "-bullets"), {
obj2htm.subRender(this.render_fullscreen_button(), document.getElementById(this.id + "-fullscreen-btn"), {
mode: "replace",
});
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_fullscreen_button() {
return {
tag: "button",
id: this.id + "-fullscreen-btn",
class: `toggle-fullscreen-button ${this.state.fullscreen ? "to-small" : "to-full"}`,
contents: this.state.fullscreen ? "▢" : "⛶",
onclick: e => {
e.stopImmediatePropagation();
render() {
return {
tag: "div",
id: this.id,
class: "image-carousel",
contents: [
this.render_image(),
this.render_bullets(),
],
};
}
}
module.exports = ImageCarousel;