Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
image-carousel.js 2.41 KiB
Newer Older
  • Learn to ignore specific revisions
  • peter_rabbit's avatar
    peter_rabbit committed
    "use strict";
    
    const translator = require("ks-cheap-translator")
    
    peter_rabbit's avatar
    peter_rabbit committed
    class ImageCarousel {
        constructor(props) {
            this.props = props;
    
    Pierre Jarriges's avatar
    Pierre Jarriges committed
            this.id = this.props.images.join("").replace(/[\s\./]/g, "");
    
    peter_rabbit's avatar
    peter_rabbit committed
            this.state = {
                showImageIndex: 0,
            };
            this.RUN_INTERVAL = 5000;
    
    peter_rabbit's avatar
    peter_rabbit committed
            this.props.images.length > 1 && this.run();
    
    peter_rabbit's avatar
    peter_rabbit committed
        }
    
        run() {
    
    Pierre Jarriges's avatar
    Pierre Jarriges committed
            const runningInterval = setInterval(() => {
    
    peter_rabbit's avatar
    peter_rabbit committed
                let { showImageIndex } = this.state;
                const { images } = this.props;
                this.state.showImageIndex = showImageIndex < images.length - 1 ? ++showImageIndex : 0;
    
                this.refresh();
    
    peter_rabbit's avatar
    peter_rabbit committed
            }, this.RUN_INTERVAL);
    
    Pierre Jarriges's avatar
    Pierre Jarriges committed
    
            obj2htm.registerAsyncSubscription(this.id, function () {
    
                clearInterval(runningInterval);
    
    Pierre Jarriges's avatar
    Pierre Jarriges committed
            });
    
            this.runningInterval = runningInterval;
    
    peter_rabbit's avatar
    peter_rabbit committed
        }
    
    
        set_image_index(i) {
    
    peter_rabbit's avatar
    peter_rabbit committed
            clearInterval(this.runningInterval);
            this.state.showImageIndex = i;
    
            this.refresh();
    
    peter_rabbit's avatar
    peter_rabbit committed
        }
    
    
        refresh() {
            obj2htm.subRender(this.render_image(), document.getElementById(this.id + "-img-element"), {
                mode: "replace",
            });
            obj2htm.subRender(this.render_bullets(), document.getElementById(this.id + "-bullets"), {
    
    peter_rabbit's avatar
    peter_rabbit committed
                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" };
        }
    
    
    peter_rabbit's avatar
    peter_rabbit committed
        render() {
            return {
                tag: "div",
                id: this.id,
                class: "image-carousel",
                contents: [
    
                    this.render_image(),
                    this.render_bullets(),
    
    peter_rabbit's avatar
    peter_rabbit committed
                ],
            };
        }
    }
    
    module.exports = ImageCarousel;