/** * QCObjects Framework SDK * ________________ * * Author: Jean Machuca * * Cross Browser Javascript Framework for MVC Patterns * QCObjects is licensed under the MIT License * [LICENSE] (https://github.com/QCObjects/qcobjects-sdk/blob/main/LICENSE.txt) * * Copyright (C) 2015 Jean Machuca, * * Everyone is permitted to copy and distribute verbatim copies of this * license document, but changing it is not allowed. */ "use strict"; import { ClassFactory, Controller, New, Package, logger, Component } from "qcobjects"; type SliderParams = { dependencies: any[]; component: Component; duration: number; slideIndex: number; interval: number | null; sliderHandlerName: string | null; }; export class SliderController extends Controller { slideIndex = 0; duration = 7100; interval: any = null; sliderHandlerName = ""; _componentRoot: HTMLElement | ShadowRoot | undefined; component: Component; constructor({ component, dependencies = [], duration = 7100, slideIndex = 0, interval = null, sliderHandlerName = null }: SliderParams) { super({ component, dependencies, duration, slideIndex, interval, sliderHandlerName }); this.component = component; this._componentRoot = (component.shadowed) ? (component.shadowRoot) : (component.body); this.sliderHandlerName = "slider_" + this.component.__instanceID.toString(); } stop() { if (this.interval != null) { clearInterval(this.interval); } } plusSlidesAndStop(n: number) { this.stop(); this.plusSlides(n); } plusSlides(n: number) { this.showSlides(this.slideIndex += n); } currentSlide(n: number) { this.stop(); this.showSlides(this.slideIndex = n); } automate() { this.interval = setInterval(() => { this.plusSlides(1); }, this.duration); } showSlides(n: number) { const slides = (this._componentRoot as any)?.subelements(".qcoSlides"); const dots = (this._componentRoot as any)?.subelements(".qcoSlider__dots--dot"); if (n > (slides.length - 1)) { this.slideIndex = 0; } if (n < 0) { this.slideIndex = 0; } slides.filter((slide: any, index: number) => { return index !== this.slideIndex; }).map((slide: any) => { return (New(ClassFactory("Fade"), {})).apply(slide, 1, 0); }); // eslint-disable-next-line no-unused-vars dots.filter((dot: any, index: number) => { return index !== this.slideIndex; }).map((dot: { classList: { remove: (arg0: string) => any; }; }) => { return dot.classList.remove("active"); }); try { dots[this.slideIndex].classList.add("active"); } catch (e: any) { logger.debug(`Something went wrong when trying to activate a slide: ${this.slideIndex} - ${e.message}`); } setTimeout(() => { slides.filter((slide: any, index: number) => { return index !== this.slideIndex; }).map((slide: { style: { display: string; }; }) => { slide.style.display = "none"; return slide.style.display; }); try { slides[this.slideIndex].style.display = "block"; (New(ClassFactory("Fade"), {})).apply(slides[this.slideIndex], 0, 1); } catch (e: any) { logger.debug(`Something went wrong when trying to show a slide: ${this.slideIndex} - ${e.message}`); } }, 700); } fillDots() { const slides = (this._componentRoot as any)?.subelements(".qcoSlides"); slides.map((slide: HTMLElement, index: number) => { const dot = document.createElement("span"); dot.className = "qcoSlider__dots--dot"; dot.addEventListener("click", () => this.currentSlide(index)); return (this._componentRoot as any)?.subelements(".qcoSlider__dots")[0].append(dot); }); } bindControls() { const prev = (this._componentRoot as any)?.subelements(".prev"); const next = (this._componentRoot as any)?.subelements(".next"); prev?.map((el: HTMLElement) => el.addEventListener("click", () => this.plusSlidesAndStop(-1))); next?.map((el: HTMLElement) => el.addEventListener("click", () => this.plusSlidesAndStop(1))); } done() { const slides = (this._componentRoot as any)?.subelements(".qcoSlides"); slides.filter((slide: HTMLElement, index: number) => { return index !== this.slideIndex; }).map((slide: { style: { display: string; }; }) => { slide.style.display = "none"; return slide.style.display; }); setTimeout(() => { this.fillDots(); this.bindControls(); this.slideIndex = 0; this.showSlides(this.slideIndex); this.automate(); }, 3000); } } Package("org.qcobjects.controllers.slider", [ SliderController ]);