frontblocks-advanced-option.js
1 month ago
frontblocks-advanced-option.jsx
1 month ago
frontblocks-carousel-editor.css
4 months ago
frontblocks-carousel.css
1 month ago
frontblocks-carousel.js
1 month ago
glide.min.js
8 months ago
frontblocks-carousel.js
163 lines
| 1 | window.addEventListener('load', function (event) { |
| 2 | const carouselItem = document.querySelectorAll('.frontblocks-carousel'); |
| 3 | |
| 4 | if (carouselItem.length > 0) { |
| 5 | carouselItem.forEach((item) => { |
| 6 | // Check if carousel should be disabled on desktop. |
| 7 | const disableOnDesktop = item.getAttribute('data-disable-on-desktop') === 'true'; |
| 8 | const isDesktop = window.innerWidth >= 768; |
| 9 | |
| 10 | // Skip initialization if disabled on desktop and current viewport is desktop. |
| 11 | if (disableOnDesktop && isDesktop) { |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | // First Parent. |
| 16 | const parent = item.parentNode; |
| 17 | const wrapper = document.createElement('div'); |
| 18 | |
| 19 | parent.replaceChild(wrapper, item); |
| 20 | wrapper.appendChild(item); |
| 21 | |
| 22 | wrapper.classList.add('glide__track'); |
| 23 | wrapper.setAttribute('data-glide-el', 'track'); |
| 24 | |
| 25 | // Second Parent. |
| 26 | const parentwrap = wrapper.parentNode; |
| 27 | const wrapperParent = document.createElement('div'); |
| 28 | |
| 29 | parentwrap.replaceChild(wrapperParent, wrapper); |
| 30 | wrapperParent.appendChild(wrapper); |
| 31 | wrapperParent.classList.add('frontblocks', 'glide'); |
| 32 | |
| 33 | // Options |
| 34 | const carouselType = item.getAttribute('data-type') ? item.getAttribute('data-type') : 'carousel'; |
| 35 | const carouselbuttons = item.getAttribute('data-buttons') ? item.getAttribute('data-buttons') : 'bullets'; |
| 36 | const carouselView = item.getAttribute('data-view') ? parseInt(item.getAttribute('data-view')) : 4; |
| 37 | const carouselLaptopView = item.getAttribute('data-laptop-view') ? parseInt(item.getAttribute('data-laptop-view')) : 3; |
| 38 | const carouselTabletView = item.getAttribute('data-tablet-view') ? parseInt(item.getAttribute('data-tablet-view')) : 2; |
| 39 | const carouselMobileView = item.getAttribute('data-mobile-view') ? parseInt(item.getAttribute('data-mobile-view')) : 1; |
| 40 | const autoplayAttr = item.getAttribute('data-autoplay'); |
| 41 | const carouselAutoplay = (autoplayAttr !== '' && autoplayAttr !== null && autoplayAttr !== undefined) ? parseInt(autoplayAttr, 10) : 0; |
| 42 | const carouselGap = item.getAttribute('data-gap') ? parseInt(item.getAttribute('data-gap'), 10) : 20; |
| 43 | const carouselRewind = item.getAttribute('data-rewind') ? item.getAttribute('data-rewind') : false; |
| 44 | const carouselbuttonsColor = item.getAttribute('data-buttons-color') ? item.getAttribute('data-buttons-color') : 'black'; |
| 45 | const carouselbuttonsBackgroundColor = item.getAttribute('data-buttons-background-color') ? item.getAttribute('data-buttons-background-color') : 'transparent'; |
| 46 | const carouselbuttonsPosition = item.getAttribute('data-buttons-position') ? item.getAttribute('data-buttons-position') : 'side'; |
| 47 | |
| 48 | |
| 49 | // Add classes |
| 50 | item.classList.add('glide__slides', 'glide-' + Math.floor(Math.random() * 1000)); |
| 51 | // Force flex layout via inline style to beat WP grid CSS (columns-N, is-layout-grid). |
| 52 | if (item.classList.contains('wp-block-post-template')) { |
| 53 | item.style.setProperty('display', 'flex', 'important'); |
| 54 | item.style.setProperty('flex-wrap', 'nowrap', 'important'); |
| 55 | item.style.setProperty('max-width', 'none', 'important'); |
| 56 | item.style.setProperty('grid-template-columns', 'none', 'important'); |
| 57 | item.style.setProperty('list-style', 'none', 'important'); |
| 58 | item.style.setProperty('padding-left', '0', 'important'); |
| 59 | item.style.setProperty('margin-left', '0', 'important'); |
| 60 | } |
| 61 | for (const child of item.children) { |
| 62 | child.classList.add('glide__slide'); |
| 63 | } |
| 64 | |
| 65 | // Don't show bullets on responsive and more than 10 items. |
| 66 | let showBullets = false; |
| 67 | if (window.screen.availWidth < 768 && item.children.length <= 10) { |
| 68 | showBullets = true; |
| 69 | } else if (window.screen.availWidth > 768) { |
| 70 | showBullets = true; |
| 71 | } |
| 72 | |
| 73 | if (showBullets && carouselbuttons == 'bullets') { |
| 74 | const bullets = document.createElement('div'); |
| 75 | bullets.classList.add('glide__bullets'); |
| 76 | bullets.setAttribute('data-glide-el', 'controls[nav]'); |
| 77 | bullets.setAttribute('role', 'group'); |
| 78 | bullets.setAttribute('aria-label', 'Slide navigation'); |
| 79 | |
| 80 | for (let i = 0; i < item.children.length; i++) { |
| 81 | const bullet = document.createElement('button'); |
| 82 | bullet.classList.add('glide__bullet'); |
| 83 | bullet.setAttribute('data-glide-dir', '=' + i); |
| 84 | bullet.setAttribute('aria-label', 'Go to slide ' + (i + 1)); |
| 85 | bullets.appendChild(bullet); |
| 86 | } |
| 87 | |
| 88 | wrapperParent.appendChild(bullets); |
| 89 | |
| 90 | // Set bullet colors via CSS custom properties on the wrapper. |
| 91 | // This avoids specificity conflicts with the stylesheet. |
| 92 | if (carouselbuttonsColor) { |
| 93 | wrapperParent.style.setProperty('--frbl-bullet-color', carouselbuttonsColor); |
| 94 | } |
| 95 | if (carouselbuttonsBackgroundColor && carouselbuttonsBackgroundColor !== 'transparent') { |
| 96 | wrapperParent.style.setProperty('--frbl-bullet-bg', carouselbuttonsBackgroundColor); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (carouselbuttons == 'arrows') { |
| 101 | const arrows = document.createElement('div'); |
| 102 | arrows.classList.add('glide__arrows'); |
| 103 | if (carouselbuttonsPosition == 'bottom') { |
| 104 | arrows.classList.add('glide__arrows--bottom'); |
| 105 | } else { |
| 106 | arrows.classList.add('glide__arrows--top'); |
| 107 | } |
| 108 | |
| 109 | arrows.setAttribute('data-glide-el', 'controls'); |
| 110 | arrowsHTML = '<button class="glide__arrow glide__arrow--left glide__arrow glide__arrow--left" data-glide-dir="<"'; |
| 111 | arrowsHTML += ' aria-label="Previous slide"'; |
| 112 | arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"'; |
| 113 | arrowsHTML += '><svg width="7" height="12" viewBox="0 0 7 12" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 1L1 6L6 11" stroke="'; |
| 114 | arrowsHTML += carouselbuttonsColor; |
| 115 | arrowsHTML += '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button><button class="glide__arrow glide__arrow--right glide__arrow glide__arrow--right" data-glide-dir=">"'; |
| 116 | arrowsHTML += ' aria-label="Next slide"'; |
| 117 | arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"'; |
| 118 | arrowsHTML += '><svg width="7" height="12" viewBox="0 0 7 12" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 11L6 6L1 1" stroke="'; |
| 119 | arrowsHTML += carouselbuttonsColor; |
| 120 | arrowsHTML += '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>'; |
| 121 | arrows.innerHTML = arrowsHTML; |
| 122 | wrapperParent.appendChild(arrows); |
| 123 | } |
| 124 | const glideFrontBlocks = new Glide(wrapperParent, { |
| 125 | type: carouselType, |
| 126 | perView: carouselView, |
| 127 | startAt: 0, |
| 128 | autoplay: carouselAutoplay > 0 ? carouselAutoplay : false, |
| 129 | gap: isNaN(carouselGap) ? 20 : carouselGap, |
| 130 | rewind: carouselRewind, |
| 131 | breakpoints: { |
| 132 | 768: { |
| 133 | perView: carouselMobileView |
| 134 | }, |
| 135 | 1024: { |
| 136 | perView: carouselTabletView |
| 137 | }, |
| 138 | 1440: { |
| 139 | perView: carouselLaptopView |
| 140 | } |
| 141 | } |
| 142 | }); |
| 143 | |
| 144 | if ('slider' === carouselType) { |
| 145 | glideFrontBlocks.on('run.after', () => { |
| 146 | const currentIndex = glideFrontBlocks.index; |
| 147 | const lastIndex = glideFrontBlocks.selector.querySelectorAll('.glide__slide').length; |
| 148 | |
| 149 | actualView = parseInt(currentIndex) + parseInt(carouselView); |
| 150 | |
| 151 | if (actualView > lastIndex) { |
| 152 | setTimeout(() => { |
| 153 | glideFrontBlocks.go('=0'); |
| 154 | }, 5); |
| 155 | } |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | glideFrontBlocks.mount(); |
| 160 | }); |
| 161 | } |
| 162 | }); |
| 163 |