// ===================================================== // carousel.js — Swiper.js Carousel Testimoni // Touch-friendly, auto-play, responsive breakpoints // ===================================================== let testimoniSwiper = null; /** Render slide testimoni ke dalam swiper-wrapper */ function renderTestimoniSlides(data) { const wrapper = document.getElementById('testimonial-wrapper'); if (!wrapper) return; wrapper.innerHTML = data.map(t => `
`).join(''); } /** Inisialisasi Swiper setelah slide di-render */ function initSwiper(count) { if (testimoniSwiper) { testimoniSwiper.destroy(true, true); testimoniSwiper = null; } testimoniSwiper = new Swiper('.testimonial-swiper', { slidesPerView: 1, spaceBetween: 24, loop: count > 3, centerInsufficientSlides: true, autoplay: { delay: 4500, disableOnInteraction: false, pauseOnMouseEnter: true, }, pagination: { el: '.testimonial-pagination', clickable: true, }, grabCursor: true, breakpoints: { 640: { slidesPerView: 2 }, 1024: { slidesPerView: 3 }, }, a11y: { prevSlideMessage: 'Slide sebelumnya', nextSlideMessage: 'Slide berikutnya', }, }); } /** Fetch data testimoni dan render */ async function initCarousel() { const section = document.getElementById('testimonial'); try { const res = await fetch('data/testimoni.json'); if (!res.ok) throw new Error(`HTTP ${res.status}`); const data = await res.json(); if (!data || data.length === 0) { // Sembunyikan seksi testimoni jika data kosong (sesuai UC-07 Alt Flow) section?.classList.add('hidden'); return; } renderTestimoniSlides(data); // Init Swiper setelah DOM di-render requestAnimationFrame(() => initSwiper(data.length)); } catch { // Jika gagal fetch, sembunyikan seksi (graceful degradation) section?.classList.add('hidden'); } } document.addEventListener('DOMContentLoaded', initCarousel);