import { Controller } from '@hotwired/stimulus'; import Swiper from 'swiper/bundle'; export default class extends Controller { static values = { options: Object, paginationDelay: Number, menu: Array, }; static targets = ['pagination', 'next', 'prev']; connect() { this.swiper = new Swiper(this.element, { ...this.swiperOptions(), on: { slideChangeTransitionEnd: () => { this.dispatch('slideChangeTransitionEnd', { detail: { activeSlideIndex: this.swiper.activeIndex } }); }, }, }); } disconnect() { if (this.swiper) { this.swiper.destroy(); } } swiperOptions() { let options = {}; if (this.optionsValue) { options = this.optionsValue; } if (this.hasPaginationTarget) { options.pagination ||= {}; options.pagination.el = this.paginationTarget; if (this.hasMenuValue) { options.pagination.clickable = true; options.pagination.type = 'bullets'; options.pagination.renderBullet = (index, className) => { return `${this.menuValue[index]}`; }; } } if (this.hasNextTarget) { options.navigation ||= {}; options.navigation.nextEl = this.nextTarget; } if (this.hasPrevTarget) { options.navigation ||= {}; options.navigation.prevEl = this.prevTarget; } options.speed ||= 1200; return options; } }