PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.3.4
FrontBlocks for Gutenberg/GeneratePress v1.3.4
trunk 0.2.0 0.2.1 0.2.2 0.2.3 0.2.4 0.2.5 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1.0 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 ci-artifacts
frontblocks / assets / carousel / frontblocks-carousel.js
frontblocks / assets / carousel Last commit date
frontblocks-advanced-option.js 4 months ago frontblocks-carousel-editor.css 4 months ago frontblocks-carousel.css 4 months ago frontblocks-carousel.js 4 months ago glide.min.js 8 months ago
frontblocks-carousel.js
153 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 for (const child of item.children) {
52 child.classList.add('glide__slide');
53 }
54
55 // Don't show bullets on responsive and more than 10 items.
56 let showBullets = false;
57 if (window.screen.availWidth < 768 && item.children.length <= 10) {
58 showBullets = true;
59 } else if (window.screen.availWidth > 768) {
60 showBullets = true;
61 }
62
63 if (showBullets && carouselbuttons == 'bullets') {
64 const bullets = document.createElement('div');
65 bullets.classList.add('glide__bullets');
66 bullets.setAttribute('data-glide-el', 'controls[nav]');
67 bullets.setAttribute('role', 'group');
68 bullets.setAttribute('aria-label', 'Slide navigation');
69
70 for (let i = 0; i < item.children.length; i++) {
71 const bullet = document.createElement('button');
72 bullet.classList.add('glide__bullet');
73 bullet.setAttribute('data-glide-dir', '=' + i);
74 bullet.setAttribute('aria-label', 'Go to slide ' + (i + 1));
75 bullets.appendChild(bullet);
76 }
77
78 wrapperParent.appendChild(bullets);
79
80 // Set bullet colors via CSS custom properties on the wrapper.
81 // This avoids specificity conflicts with the stylesheet.
82 if (carouselbuttonsColor) {
83 wrapperParent.style.setProperty('--frbl-bullet-color', carouselbuttonsColor);
84 }
85 if (carouselbuttonsBackgroundColor && carouselbuttonsBackgroundColor !== 'transparent') {
86 wrapperParent.style.setProperty('--frbl-bullet-bg', carouselbuttonsBackgroundColor);
87 }
88 }
89
90 if (carouselbuttons == 'arrows') {
91 const arrows = document.createElement('div');
92 arrows.classList.add('glide__arrows');
93 if (carouselbuttonsPosition == 'bottom') {
94 arrows.classList.add('glide__arrows--bottom');
95 } else {
96 arrows.classList.add('glide__arrows--top');
97 }
98
99 arrows.setAttribute('data-glide-el', 'controls');
100 arrowsHTML = '<button class="glide__arrow glide__arrow--left glide__arrow glide__arrow--left" data-glide-dir="<"';
101 arrowsHTML += ' aria-label="Previous slide"';
102 arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"';
103 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="';
104 arrowsHTML += carouselbuttonsColor;
105 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=">"';
106 arrowsHTML += ' aria-label="Next slide"';
107 arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"';
108 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="';
109 arrowsHTML += carouselbuttonsColor;
110 arrowsHTML += '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>';
111 arrows.innerHTML = arrowsHTML;
112 wrapperParent.appendChild(arrows);
113 }
114 const glideFrontBlocks = new Glide(wrapperParent, {
115 type: carouselType,
116 perView: carouselView,
117 startAt: 0,
118 autoplay: carouselAutoplay > 0 ? carouselAutoplay : false,
119 gap: isNaN(carouselGap) ? 20 : carouselGap,
120 rewind: carouselRewind,
121 breakpoints: {
122 768: {
123 perView: carouselMobileView
124 },
125 1024: {
126 perView: carouselTabletView
127 },
128 1440: {
129 perView: carouselLaptopView
130 }
131 }
132 });
133
134 if ('slider' === carouselType) {
135 glideFrontBlocks.on('run.after', () => {
136 const currentIndex = glideFrontBlocks.index;
137 const lastIndex = glideFrontBlocks.selector.querySelectorAll('.glide__slide').length;
138
139 actualView = parseInt(currentIndex) + parseInt(carouselView);
140
141 if (actualView > lastIndex) {
142 setTimeout(() => {
143 glideFrontBlocks.go('=0');
144 }, 5);
145 }
146 });
147 }
148
149 glideFrontBlocks.mount();
150 });
151 }
152 });
153