|
1 | 1 | /* jshint esversion: 6 */ |
2 | | -/* global jQuery, CustomEvent */ |
3 | | -/* global neveShopSlider */ |
| 2 | +/* global CustomEvent */ |
4 | 3 | import { tns } from 'tiny-slider/src/tiny-slider'; |
5 | 4 |
|
6 | | -(function ($) { |
7 | | - /** |
8 | | - * Init shop. |
9 | | - */ |
10 | | - function initShop() { |
11 | | - const $body = $('body'); |
12 | | - if ($body.hasClass('woocommerce')) { |
13 | | - handleShopSidebar(); |
14 | | - } |
15 | | - |
16 | | - const countExclusive = $('.exclusive-products li.product').length; |
17 | | - |
18 | | - if ($body.hasClass('nv-exclusive') && countExclusive > 4) { |
19 | | - handleExclusiveSlider(); |
20 | | - } |
21 | | - |
22 | | - if ( |
23 | | - '1' !== neveShopSlider.isSparkActive && |
24 | | - $body.hasClass('single-product') |
25 | | - ) { |
26 | | - handleGallerySlider(); |
27 | | - } |
| 5 | +/** |
| 6 | + * Init shop. |
| 7 | + */ |
| 8 | +function initShop() { |
| 9 | + if (document.body.classList.contains('woocommerce')) { |
| 10 | + handleShopSidebar(); |
28 | 11 | } |
29 | 12 |
|
30 | | - /** |
31 | | - * Add prev and next |
32 | | - * |
33 | | - * @param {Node} targetNode |
34 | | - * @param {Node} slider |
35 | | - * @param {string} vertical |
36 | | - */ |
37 | | - function addNextPrev(targetNode, slider, vertical = false) { |
38 | | - const $next = $('<span/>') |
39 | | - .addClass('next dashicons') |
40 | | - .addClass( |
41 | | - 'dashicons-arrow-' + (vertical ? 'down' : 'right') + '-alt2' |
42 | | - ); |
43 | | - const $prev = $('<span/>') |
44 | | - .addClass('prev dashicons') |
45 | | - .addClass( |
46 | | - 'dashicons-arrow-' + (vertical ? 'up' : 'left') + '-alt2' |
47 | | - ); |
48 | | - |
49 | | - $prev.on('click', () => slider.goTo('prev')); |
| 13 | + const countExclusive = document.querySelectorAll( |
| 14 | + '.exclusive-products li.product' |
| 15 | + ).length; |
50 | 16 |
|
51 | | - $next.on('click', () => slider.goTo('next')); |
52 | | - |
53 | | - const $target = $(targetNode); |
54 | | - $prev.insertBefore($target); |
55 | | - $next.insertAfter($target); |
| 17 | + if ( |
| 18 | + document.body.classList.contains('nv-exclusive') && |
| 19 | + countExclusive > 4 |
| 20 | + ) { |
| 21 | + handleExclusiveSlider(); |
56 | 22 | } |
57 | | - |
58 | | - /** |
59 | | - * Handle the shop sidebar. |
60 | | - */ |
61 | | - function handleShopSidebar() { |
62 | | - const $sidebar = $('.shop-sidebar'); |
63 | | - if (0 === $sidebar.length) { |
64 | | - return; |
65 | | - } |
66 | | - const $html = $('html'); |
67 | | - const $toggles = $('.nv-sidebar-toggle'); |
68 | | - $toggles.each(function () { |
69 | | - $(this).on('click', function (e) { |
70 | | - e.preventDefault(); |
71 | | - $sidebar.toggleClass('sidebar-open'); |
72 | | - $html.toggleClass('menu-openend'); |
73 | | - }); |
74 | | - }); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Handle the shop sidebar. |
| 27 | + */ |
| 28 | +function handleShopSidebar() { |
| 29 | + const sidebar = document.querySelector('.shop-sidebar'); |
| 30 | + if (sidebar === null) { |
| 31 | + return; |
75 | 32 | } |
76 | | - |
77 | | - /** |
78 | | - * Handle Exclusive Products Slider |
79 | | - */ |
80 | | - function handleExclusiveSlider() { |
81 | | - const $items = $('ul.exclusive-products'); |
82 | | - |
83 | | - if (0 === $items.length) return; |
84 | | - |
85 | | - const responsive = { |
86 | | - 0: { items: 2, gutter: 21 }, |
87 | | - 768: { items: 4, gutter: 27 }, |
88 | | - 1200: { items: 4, gutter: 30 }, |
89 | | - }; |
90 | | - |
91 | | - const slider = tns({ |
92 | | - container: 'ul.exclusive-products', |
93 | | - slideBy: 1, |
94 | | - arrowKeys: true, |
95 | | - loop: true, |
96 | | - autoplay: true, |
97 | | - items: 4, |
98 | | - edgePadding: 0, |
99 | | - autoplayButtonOutput: false, |
100 | | - autoplayHoverPause: true, |
101 | | - speed: 1000, |
102 | | - autoplayTimeout: 3000, |
103 | | - autoplayButton: false, |
104 | | - controls: false, |
105 | | - navPosition: 'bottom', |
106 | | - navContainer: '.dots-nav', |
107 | | - navAsThumbnails: true, |
108 | | - responsive, |
| 33 | + const html = document.querySelector('html'); |
| 34 | + const toggles = document.querySelectorAll('.nv-sidebar-toggle') || []; |
| 35 | + toggles.forEach((toggle) => { |
| 36 | + toggle.addEventListener('click', function (e) { |
| 37 | + e.preventDefault(); |
| 38 | + sidebar.classList.toggle('sidebar-open'); |
| 39 | + html.classList.toggle('menu-openend'); |
109 | 40 | }); |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Handle Exclusive Products Slider |
| 46 | + */ |
| 47 | +function handleExclusiveSlider() { |
| 48 | + const items = document.querySelector('ul.exclusive-products'); |
| 49 | + |
| 50 | + if (items === null) return false; |
| 51 | + |
| 52 | + const responsive = { |
| 53 | + 0: { items: 2, gutter: 21 }, |
| 54 | + 768: { items: 4, gutter: 27 }, |
| 55 | + 1200: { items: 4, gutter: 30 }, |
| 56 | + }; |
| 57 | + |
| 58 | + const slider = tns({ |
| 59 | + container: 'ul.exclusive-products', |
| 60 | + slideBy: 1, |
| 61 | + arrowKeys: true, |
| 62 | + loop: true, |
| 63 | + autoplay: true, |
| 64 | + items: 4, |
| 65 | + edgePadding: 0, |
| 66 | + autoplayButtonOutput: false, |
| 67 | + autoplayHoverPause: true, |
| 68 | + speed: 1000, |
| 69 | + autoplayTimeout: 3000, |
| 70 | + autoplayButton: false, |
| 71 | + controls: false, |
| 72 | + navPosition: 'bottom', |
| 73 | + navContainer: '.dots-nav', |
| 74 | + navAsThumbnails: true, |
| 75 | + responsive, |
| 76 | + }); |
110 | 77 |
|
111 | | - // [If Sparks Variation Swatches is enabled and ] Initialize Sparks Variation Swatches for cloned products. |
112 | | - if ($('body').hasClass('sparks-vs-shop-attribute')) { |
113 | | - slider.events.on('transitionEnd', () => { |
114 | | - document.dispatchEvent( |
115 | | - new CustomEvent('sparksVSNeedsInit', { |
116 | | - detail: { container: '.products.exclusive' }, |
117 | | - }) |
118 | | - ); |
119 | | - }); |
120 | | - } |
121 | | - } |
122 | | - |
123 | | - /** |
124 | | - * Handle Gallery Image Slider |
125 | | - */ |
126 | | - function handleGallerySlider() { |
127 | | - const $galleryNav = $('ol.flex-control-nav'); |
128 | | - |
129 | | - if (0 === $galleryNav.length) return; |
130 | | - |
131 | | - const isDesktop = window.innerWidth >= 992; |
132 | | - |
133 | | - const slider = tns({ |
134 | | - container: 'ol.flex-control-nav', |
135 | | - items: 4, |
136 | | - axis: isDesktop ? 'vertical' : 'horizontal', |
137 | | - slideBy: 'page', |
138 | | - rewind: true, |
139 | | - loop: false, |
140 | | - nav: false, |
141 | | - controls: false, |
142 | | - mouseDrag: true, |
| 78 | + // [If Sparks Variation Swatches is enabled and ] Initialize Sparks Variation Swatches for cloned products. |
| 79 | + if (document.body.classList.contains('sparks-vs-shop-attribute')) { |
| 80 | + slider.events.on('transitionEnd', () => { |
| 81 | + document.dispatchEvent( |
| 82 | + new CustomEvent('sparksVSNeedsInit', { |
| 83 | + detail: { |
| 84 | + container: '.products.exclusive', |
| 85 | + }, |
| 86 | + }) |
| 87 | + ); |
143 | 88 | }); |
144 | | - |
145 | | - addNextPrev( |
146 | | - $('.woocommerce-product-gallery .tns-inner')[0], |
147 | | - slider, |
148 | | - isDesktop |
149 | | - ); |
150 | 89 | } |
| 90 | +} |
151 | 91 |
|
152 | | - /** |
153 | | - * Run JS on load. |
154 | | - */ |
155 | | - $(function () { |
156 | | - initShop(); |
157 | | - }); |
158 | | -})(jQuery); |
| 92 | +/** |
| 93 | + * Run JS on load. |
| 94 | + */ |
| 95 | +window.addEventListener('load', initShop); |
0 commit comments