PluginProbe ʕ •ᴥ•ʔ
FrontBlocks for Gutenberg/GeneratePress / 1.2.0
FrontBlocks for Gutenberg/GeneratePress v1.2.0
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 8 months ago frontblocks-carousel.css 9 months ago frontblocks-carousel.js 8 months ago glide.min.js 8 months ago
frontblocks-carousel.js
147 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('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 carouselAutoplay = item.getAttribute('data-autoplay') ? item.getAttribute('data-autoplay') : 0;
41 const carouselRewind = item.getAttribute('data-rewind') ? item.getAttribute('data-rewind') : false;
42 const carouselbuttonsColor = item.getAttribute('data-buttons-color') ? item.getAttribute('data-buttons-color') : 'black';
43 const carouselbuttonsBackgroundColor = item.getAttribute('data-buttons-background-color') ? item.getAttribute('data-buttons-background-color') : 'transparent';
44 const carouselbuttonsPosition = item.getAttribute('data-buttons-position') ? item.getAttribute('data-buttons-position') : 'side';
45
46
47 // Add classes
48 item.classList.add('glide__slides', 'glide-' + Math.floor(Math.random() * 1000));
49 for (const child of item.children) {
50 child.classList.add('glide__slide');
51 }
52
53 // Don't show bullets on responsive and more than 10 items.
54 let showBullets = false;
55 if (window.screen.availWidth < 768 && item.children.length <= 10) {
56 showBullets = true;
57 } else if (window.screen.availWidth > 768) {
58 showBullets = true;
59 }
60
61 if (showBullets && carouselbuttons == 'bullets') {
62 const bullets = document.createElement('div');
63 bullets.classList.add('glide__bullets');
64 bullets.setAttribute('data-glide-el', 'controls[nav]');
65
66 for (let i = 0; i < item.children.length; i++) {
67 const bullet = document.createElement('button');
68 bullet.classList.add('glide__bullet');
69 bullet.setAttribute('data-glide-dir', '=' + i);
70 bullet.style.backgroundColor = carouselbuttonsBackgroundColor;
71 bullets.appendChild(bullet);
72 }
73
74 wrapperParent.appendChild(bullets);
75
76 // Add custom CSS for active bullet color
77 const style = document.createElement('style');
78 style.textContent = `
79 .glide__bullet.glide__bullet--active {
80 background-color: ${carouselbuttonsColor} !important;
81 }
82 `;
83 document.head.appendChild(style);
84 }
85
86 if (carouselbuttons == 'arrows') {
87 const arrows = document.createElement('div');
88 arrows.classList.add('glide__arrows');
89 if (carouselbuttonsPosition == 'bottom') {
90 arrows.classList.add('glide__arrows--bottom');
91 } else {
92 arrows.classList.add('glide__arrows--top');
93 }
94
95 arrows.setAttribute('data-glide-el', 'controls');
96 arrowsHTML = '<button class="glide__arrow glide__arrow--left glide__arrow glide__arrow--left" data-glide-dir="<"';
97 arrowsHTML += ' style="background-color: ' + carouselbuttonsBackgroundColor + '"';
98 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="';
99 arrowsHTML += carouselbuttonsColor;
100 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=">"';
101
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="M1 11L6 6L1 1" stroke="';
104 arrowsHTML += carouselbuttonsColor;
105 arrowsHTML += '" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg></button>';
106 arrows.innerHTML = arrowsHTML;
107 wrapperParent.appendChild(arrows);
108 }
109 const glideFrontBlocks = new Glide(wrapperParent, {
110 type: carouselType,
111 perView: carouselView,
112 startAt: 0,
113 autoplay: carouselAutoplay === 0 ? 2500 : carouselAutoplay,
114 gap: 20,
115 rewind: carouselRewind,
116 breakpoints: {
117 768: {
118 perView: carouselMobileView
119 },
120 1024: {
121 perView: carouselTabletView
122 },
123 1440: {
124 perView: carouselLaptopView
125 }
126 }
127 });
128
129 if ('slider' === carouselType) {
130 glideFrontBlocks.on('run.after', () => {
131 const currentIndex = glideFrontBlocks.index;
132 const lastIndex = glideFrontBlocks.selector.querySelectorAll('.glide__slide').length;
133
134 actualView = parseInt(currentIndex) + parseInt(carouselView);
135
136 if (actualView > lastIndex) {
137 setTimeout(() => {
138 glideFrontBlocks.go('=0');
139 }, 5);
140 }
141 });
142 }
143
144 glideFrontBlocks.mount();
145 });
146 }
147 });