|
| 1 | +/** |
| 2 | + * For the full copyright and license information, please view the LICENSE |
| 3 | + * file that was distributed with this source code. |
| 4 | + */ |
| 5 | + |
| 6 | +import {Carousel} from 'bootstrap'; |
| 7 | +import SelectorsMap from '../constants/selectors-map'; |
| 8 | + |
| 9 | +export default () => { |
| 10 | + const {prestashop, Theme: {events}} = window; |
| 11 | + |
| 12 | + // Product images sync on open/close modal |
| 13 | + document.addEventListener('show.bs.modal', (event) => { |
| 14 | + const modal = event.target as HTMLElement; |
| 15 | + |
| 16 | + if (!modal.matches(SelectorsMap.product.productImagesModal)) return; |
| 17 | + |
| 18 | + const modalCarouselElement = modal.querySelector(SelectorsMap.product.productImagesModalCarousel); |
| 19 | + const mainCarouselElement = document.querySelector(SelectorsMap.product.carousel); |
| 20 | + |
| 21 | + if (!modalCarouselElement || !mainCarouselElement) return; |
| 22 | + |
| 23 | + const modalCarousel = Carousel.getOrCreateInstance(modalCarouselElement as HTMLElement); |
| 24 | + const activeIndex = getActiveSlideIndex(mainCarouselElement as HTMLElement); |
| 25 | + |
| 26 | + if (activeIndex !== -1) { |
| 27 | + modalCarousel.to(activeIndex); |
| 28 | + } |
| 29 | + }); |
| 30 | + |
| 31 | + document.addEventListener('hide.bs.modal', (event) => { |
| 32 | + const modal = event.target as HTMLElement; |
| 33 | + |
| 34 | + if (!modal.matches(SelectorsMap.product.productImagesModal)) return; |
| 35 | + |
| 36 | + const modalCarouselElement = modal.querySelector(SelectorsMap.product.productImagesModalCarousel); |
| 37 | + const mainCarouselElement = document.querySelector(SelectorsMap.product.carousel); |
| 38 | + |
| 39 | + if (!modalCarouselElement || !mainCarouselElement) return; |
| 40 | + |
| 41 | + const mainCarousel = Carousel.getOrCreateInstance(mainCarouselElement as HTMLElement); |
| 42 | + const activeIndex = getActiveSlideIndex(modalCarouselElement as HTMLElement); |
| 43 | + |
| 44 | + if (activeIndex !== -1) { |
| 45 | + mainCarousel.to(activeIndex); |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + const getActiveSlideIndex = (carouselEl: HTMLElement): number => { |
| 50 | + const items = carouselEl.querySelectorAll('.carousel-item'); |
| 51 | + |
| 52 | + return Array.from(items).findIndex((item) => item.classList.contains('active')); |
| 53 | + }; |
| 54 | + |
| 55 | + // Stores the element to refocus, per context ("main" or "quickview") |
| 56 | + const focusMap = new Map<'quickview' | 'main', { id: string }>(); |
| 57 | + |
| 58 | + // Save the element before the update |
| 59 | + prestashop.on(events.updateProduct, ({event}: { event: Event }) => { |
| 60 | + const target = event?.target as HTMLElement; |
| 61 | + |
| 62 | + if (!target || !target.id) return; |
| 63 | + |
| 64 | + const isInQuickView = !!target.closest(SelectorsMap.quickviewModal); |
| 65 | + const context: 'quickview' | 'main' = isInQuickView ? 'quickview' : 'main'; |
| 66 | + |
| 67 | + focusMap.set(context, {id: target.id}); |
| 68 | + }); |
| 69 | + |
| 70 | + // Restore focus after update |
| 71 | + prestashop.on(events.updatedProduct, () => { |
| 72 | + focusMap.forEach((focusData, context) => { |
| 73 | + let container: HTMLElement | null = null; |
| 74 | + |
| 75 | + if (context === 'quickview') { |
| 76 | + container = document.querySelector(SelectorsMap.quickviewModal); |
| 77 | + } else { |
| 78 | + container = document.querySelector(SelectorsMap.product.container); |
| 79 | + } |
| 80 | + |
| 81 | + if (!container) return; |
| 82 | + |
| 83 | + const elementToFocus = container.querySelector<HTMLElement>(`#${focusData.id}`); |
| 84 | + |
| 85 | + if (elementToFocus) { |
| 86 | + elementToFocus.focus(); |
| 87 | + |
| 88 | + // Emit event to when the focus is restored |
| 89 | + prestashop.emit(events.combinationFocusRestored, { |
| 90 | + context, |
| 91 | + elementId: focusData.id, |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + focusMap.delete(context); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + // Product availability messages announcement |
| 100 | + prestashop.on(events.combinationFocusRestored, ({context}: { context: 'quickview' | 'main' }) => { |
| 101 | + let container: HTMLElement | null = null; |
| 102 | + |
| 103 | + // Get the container of the context |
| 104 | + if (context === 'quickview') { |
| 105 | + container = document.querySelector(SelectorsMap.quickviewModal); |
| 106 | + } else { |
| 107 | + container = document.querySelector(SelectorsMap.product.container); |
| 108 | + } |
| 109 | + |
| 110 | + if (!container) return; |
| 111 | + |
| 112 | + const productAvailabilityElement = container.querySelector(SelectorsMap.product.productAvailability); |
| 113 | + |
| 114 | + if (!productAvailabilityElement) return; |
| 115 | + |
| 116 | + // Delay to ensure focus announcement finishes first |
| 117 | + setTimeout(() => { |
| 118 | + productAvailabilityElement.setAttribute('aria-live', 'polite'); |
| 119 | + }, 250); |
| 120 | + }); |
| 121 | +}; |
0 commit comments