diff --git a/js/media-slider-lazy-load.js b/js/media-slider-lazy-load.js deleted file mode 100644 index f97b726..0000000 --- a/js/media-slider-lazy-load.js +++ /dev/null @@ -1,283 +0,0 @@ -/** - * @name MediaSlider - * @version 1.1.0 - * @author @RealTime - * @description A lazy-loading media slider component supporting multiple media types with URL lazy loading - */ - -class MediaSlider { - constructor(containerId, mediaUrls) { - this.containerId = containerId; - this.mediaUrls = Array.isArray(mediaUrls) ? mediaUrls : []; // Ensure mediaUrls is an array - this.loadedMedia = new Set(); // Track which media URLs have been loaded - this.init(); - } - - init() { - this.injectStyles(); - this.createSlider(); - this.initializeSwiper(); - } - - injectStyles() { - const styles = ` - - - `; - document.head.insertAdjacentHTML('beforeend', styles); - } - - detectMediaType(url) { - const extension = url.split('.').pop().toLowerCase(); - const mediaTypes = { - 'jpg': 'image', - 'jpeg': 'image', - 'png': 'image', - 'gif': 'image', - 'webp': 'image', - 'avif': 'image', - 'mp3': 'audio', - 'm4a': 'audio', - 'wav': 'audio', - 'ogg': 'audio', - 'mp4': 'video', - 'webm': 'video', - 'ogv': 'video', - 'pdf': 'pdf', - 'docx': 'office', - 'xlsx': 'office', - 'pptx': 'office' - }; - return mediaTypes[extension] || 'unknown'; - } - - createPlaceholder(url) { - return ` -
- Loading ${this.detectMediaType(url)} media... -
- `; - } - - createMediaElement(url) { - const mediaType = this.detectMediaType(url); - switch(mediaType) { - case 'image': - return `Slide`; - case 'audio': - return ``; - case 'video': - return ``; - case 'pdf': - return ``; - case 'office': - return ``; - default: - return `
Unsupported media type
`; - } - } - - createSlider() { - if (this.mediaUrls.length === 0) { - document.getElementById(this.containerId).innerHTML = '
No media available
'; - return; // Exit if there are no media URLs - } - - const sliderHtml = ` -
-
- ${this.mediaUrls.map(url => ` -
- ${this.createPlaceholder(url)} -
- `).join('')} -
-
-
-
- `; - document.getElementById(this.containerId).innerHTML = sliderHtml; - } - - initializeSwiper() { - const script = document.createElement('script'); - script.src = 'https://cdn.jsdelivr.net/npm/swiper@10/swiper-bundle.min.js'; - script.onload = () => { - const swiper = new Swiper('.swiper', { - loop: true, - autoplay: false, - navigation: { - nextEl: '.swiper-button-next', - prevEl: '.swiper-button-prev', - }, - effect: 'fade', - fadeEffect: { - crossFade: true - }, - speed: 900, - preloadImages: true, - watchSlidesProgress: true, - on: { - slideChangeTransitionStart: (swiper) => { - this.lazyLoadMedia(swiper.slides[swiper.activeIndex]); - } - } - }); - - // Load first slide's media initially - this.lazyLoadMedia(swiper.slides[swiper.activeIndex]); - }; - document.body.appendChild(script); - } - - lazyLoadMedia(slideElement) { - const lazyElements = slideElement.querySelectorAll('.lazy-load'); - - lazyElements.forEach(el => { - const url = el.getAttribute('data-src'); - - // Skip if already loaded - if (this.loadedMedia.has(url)) { - this.replaceWithLoadedMedia(el, url); - return; - } - - // Create the appropriate media element - const mediaType = this.detectMediaType(url); - let newElement; - - switch(mediaType) { - case 'image': - newElement = document.createElement('img'); - newElement.src = url; - newElement.alt = 'Slide'; - break; - case 'audio': - newElement = document.createElement('audio'); - newElement.innerHTML = ``; - newElement.controls = true; - break; - case 'video': - newElement = document.createElement('video'); - newElement.innerHTML = ``; - newElement.controls = true; - newElement.width = '100%'; - break; - case 'pdf': - case 'office': - newElement = document.createElement('iframe'); - newElement.src = `https://docs.google.com/viewer?url=${encodeURIComponent(url)}&embedded=true`; - newElement.style.width = '100%'; - newElement.style.height = '100%'; - newElement.style.border = 'none'; - break; - } - - // Replace placeholder or lazy element - el.parentNode.replaceChild(newElement, el); - - // Mark as loaded - this.loadedMedia.add(url); - }); - } - - replaceWithLoadedMedia(lazyElement, url) { - // If media is already loaded, simply replace the lazy element - const mediaType = this.detectMediaType(url); - let newElement; - - switch(mediaType) { - case 'image': - newElement = document.createElement('img'); - newElement.src = url; - newElement.alt = 'Slide'; - break; - case 'audio': - newElement = document.createElement('audio'); - newElement.innerHTML = ``; - newElement.controls = true; - break; - case 'video': - newElement = document.createElement('video'); - newElement.innerHTML = ``; - newElement.controls = true; - newElement.width = '100%'; - break; - case 'pdf': - case 'office': - newElement = document.createElement('iframe'); - newElement.src = `https://docs.google.com/viewer?url=${encodeURIComponent(url)}&embedded=true`; - newElement.style.width = '100%'; - newElement.style.height = '100%'; - newElement.style.border = 'none'; - break; - } - - lazyElement.parentNode.replaceChild(newElement, lazyElement); - } -}