// @ts-nocheck // tsFix: REFACTOR this file to use typescript export default class HorizontalCarousel { /* * To be called in React useEffect or componentDidMount * after carousel div has been loaded into the DOM. * componentDidMount(){ * this.carousel = new horizontal_carousel(this.ref.current) * } * OR * */ constructor(carousel) { if (!carousel || !carousel.querySelectorAll) { // eslint-disable-next-line no-console console.warn('HorizontalCarousel: carousel element not found'); return; } this.carousel = carousel; /* * Wait for the images to load, then init. */ if (this.carousel) { /* * There is a bug with this implementation - if the images have loading="lazy", then this will never initiate! */ this.private_init_carousel(); // const images = this.carousel.querySelectorAll('img'); // if (images && images.length) { // /* // * Find last image in last carousel - wait for it to finish loading. // */ // const last_image = images[images.length - 1]; // if (last_image && last_image.complete) { // /* // * Image is already loaded - probably from browser cache // */ // this.private_init_carousel(); // } else { // /* // * Wait for image to finish loading // */ // last_image.onload = this.private_init_carousel; // } // } else { // /* // * No images - fire up that carousel immediately! // */ // this.private_init_carousel(); // } } } /* * Important in an SPA like React. Call .end() when componentWillUnmount() * This stops event listeners which were started: el.click, window.scroll, window.resize. * componentWillUnmount(){ * this.carousel.end() * } * or * */ end = () => { this.el_prev && this.el_prev.removeEventListener('click', this.private_handle_click_prev); this.el_next && this.el_next.removeEventListener('click', this.private_handle_click_next); this.el_images && this.el_images.removeEventListener( 'click', this.private_handle_images_scroll ); window.removeEventListener('click', this.private_handle_window_resize); }; /** * INITIATE THE CAROUSELS * Run script to bring the carousel to life */ private_init_carousel = () => { const { carousel } = this; // fix temporary chromium bug - happens to certain horizontal-scrolled elements - even if this script not included // actually looks pretty cool - looks intentional - on page load, it scrolls a little, to bring attention to divs setTimeout(function () { if ( typeof carousel === 'undefined' || !carousel || !carousel.querySelector ) { return; } const slides = carousel.querySelector('.__slides'); if (slides) { slides.scrollTo(0, 0); } }, 1000); // add arrows if not exist const arrows = carousel.querySelector('.__arrows'); if (!arrows) { this.private_add_arrows(carousel); } // fix arrows (show/hide left/right) this.private_fix_arrows(); // dependents const prev = (this.el_prev = carousel.querySelector('.__prev')); const next = (this.el_next = carousel.querySelector('.__next')); const images = (this.el_images = carousel.querySelector('.__slides')); // prev/next next.addEventListener('click', this.private_handle_click_next); prev.addEventListener('click', this.private_handle_click_prev); /* * IMPORTANT: on manual user scroll, fix arrows */ images.addEventListener('scroll', this.private_handle_images_scroll); // and on window resize, also fix arrows window.addEventListener('resize', this.private_handle_window_resize); }; private_handle_images_scroll = () => { debounce(this.private_fix_arrows, 100)(); }; private_handle_window_resize = () => { debounce(this.private_fix_arrows, 200)(); }; private_handle_click_next = () => { const { el_images } = this; // smooth scroll to next frame el_images.scrollBy({ left: el_images.clientWidth, top: 0, behavior: 'smooth', }); // not sure how long the animation will take: setTimeout(this.private_fix_arrows, 500); setTimeout(this.private_fix_arrows, 750); }; private_handle_click_prev = () => { const { el_images } = this; // smooth scroll to next frame el_images.scrollBy({ left: -el_images.clientWidth, top: 0, behavior: 'smooth', }); // not sure how long the animation will take: setTimeout(this.private_fix_arrows, 500); setTimeout(this.private_fix_arrows, 750); }; /** * ADD ARROW ELEMENTS (prev/next buttons) */ private_add_arrows = () => { const { carousel } = this; const arrows = `