-
Notifications
You must be signed in to change notification settings - Fork 86
feat: add slider on gallery images #4446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
girishpanchal30
wants to merge
3
commits into
development
Choose a base branch
from
feat/pro/2508
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+237
−83
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,95 +1,157 @@ | ||
| /* jshint esversion: 6 */ | ||
| /* global CustomEvent */ | ||
| /* global jQuery, CustomEvent */ | ||
| import { tns } from 'tiny-slider/src/tiny-slider'; | ||
|
|
||
| /** | ||
| * Init shop. | ||
| */ | ||
| function initShop() { | ||
| if (document.body.classList.contains('woocommerce')) { | ||
| handleShopSidebar(); | ||
| (function ($) { | ||
| /** | ||
| * Init shop. | ||
| */ | ||
| function initShop() { | ||
| const $body = $('body'); | ||
| if ($body.hasClass('woocommerce')) { | ||
| handleShopSidebar(); | ||
| } | ||
|
|
||
| const countExclusive = $('.exclusive-products li.product').length; | ||
|
|
||
| if ($body.hasClass('nv-exclusive') && countExclusive > 4) { | ||
| handleExclusiveSlider(); | ||
| } | ||
|
|
||
| if ( | ||
| $body.hasClass('single-product') && | ||
| !$body.hasClass('sp-ct-enabled') | ||
| ) { | ||
| handleGallerySlider(); | ||
| } | ||
| } | ||
|
|
||
| const countExclusive = document.querySelectorAll( | ||
| '.exclusive-products li.product' | ||
| ).length; | ||
| /** | ||
| * Add prev and next | ||
| * | ||
| * @param {Node} targetNode | ||
| * @param {Node} slider | ||
| * @param {string} vertical | ||
| */ | ||
| function addNextPrev(targetNode, slider, vertical = false) { | ||
| const $next = $('<span/>') | ||
| .addClass('next dashicons') | ||
| .addClass( | ||
| 'dashicons-arrow-' + (vertical ? 'down' : 'right') + '-alt2' | ||
| ); | ||
| const $prev = $('<span/>') | ||
| .addClass('prev dashicons') | ||
| .addClass( | ||
| 'dashicons-arrow-' + (vertical ? 'up' : 'left') + '-alt2' | ||
| ); | ||
|
|
||
| $prev.on('click', () => slider.goTo('prev')); | ||
|
|
||
| if ( | ||
| document.body.classList.contains('nv-exclusive') && | ||
| countExclusive > 4 | ||
| ) { | ||
| handleExclusiveSlider(); | ||
| $next.on('click', () => slider.goTo('next')); | ||
|
|
||
| const $target = $(targetNode); | ||
| $prev.insertBefore($target); | ||
| $next.insertAfter($target); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handle the shop sidebar. | ||
| */ | ||
| function handleShopSidebar() { | ||
| const sidebar = document.querySelector('.shop-sidebar'); | ||
| if (sidebar === null) { | ||
| return; | ||
|
|
||
| /** | ||
| * Handle the shop sidebar. | ||
| */ | ||
| function handleShopSidebar() { | ||
| const $sidebar = $('.shop-sidebar'); | ||
| if (0 === $sidebar.length) { | ||
| return; | ||
| } | ||
| const $html = $('html'); | ||
| const $toggles = $('.nv-sidebar-toggle'); | ||
| $toggles.each(function () { | ||
| $(this).on('click', function (e) { | ||
| e.preventDefault(); | ||
| $sidebar.toggleClass('sidebar-open'); | ||
| $html.toggleClass('menu-openend'); | ||
| }); | ||
| }); | ||
| } | ||
| const html = document.querySelector('html'); | ||
| const toggles = document.querySelectorAll('.nv-sidebar-toggle') || []; | ||
| toggles.forEach((toggle) => { | ||
| toggle.addEventListener('click', function (e) { | ||
| e.preventDefault(); | ||
| sidebar.classList.toggle('sidebar-open'); | ||
| html.classList.toggle('menu-openend'); | ||
|
|
||
| /** | ||
| * Handle Exclusive Products Slider | ||
| */ | ||
| function handleExclusiveSlider() { | ||
| const $items = $('ul.exclusive-products'); | ||
|
|
||
| if (0 === $items.length) return; | ||
|
|
||
| const responsive = { | ||
| 0: { items: 2, gutter: 21 }, | ||
| 768: { items: 4, gutter: 27 }, | ||
| 1200: { items: 4, gutter: 30 }, | ||
| }; | ||
|
|
||
| const slider = tns({ | ||
| container: 'ul.exclusive-products', | ||
| slideBy: 1, | ||
| arrowKeys: true, | ||
| loop: true, | ||
| autoplay: true, | ||
| items: 4, | ||
| edgePadding: 0, | ||
| autoplayButtonOutput: false, | ||
| autoplayHoverPause: true, | ||
| speed: 1000, | ||
| autoplayTimeout: 3000, | ||
| autoplayButton: false, | ||
| controls: false, | ||
| navPosition: 'bottom', | ||
| navContainer: '.dots-nav', | ||
| navAsThumbnails: true, | ||
| responsive, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Handle Exclusive Products Slider | ||
| */ | ||
| function handleExclusiveSlider() { | ||
| const items = document.querySelector('ul.exclusive-products'); | ||
|
|
||
| if (items === null) return false; | ||
|
|
||
| const responsive = { | ||
| 0: { items: 2, gutter: 21 }, | ||
| 768: { items: 4, gutter: 27 }, | ||
| 1200: { items: 4, gutter: 30 }, | ||
| }; | ||
|
|
||
| const slider = tns({ | ||
| container: 'ul.exclusive-products', | ||
| slideBy: 1, | ||
| arrowKeys: true, | ||
| loop: true, | ||
| autoplay: true, | ||
| items: 4, | ||
| edgePadding: 0, | ||
| autoplayButtonOutput: false, | ||
| autoplayHoverPause: true, | ||
| speed: 1000, | ||
| autoplayTimeout: 3000, | ||
| autoplayButton: false, | ||
| controls: false, | ||
| navPosition: 'bottom', | ||
| navContainer: '.dots-nav', | ||
| navAsThumbnails: true, | ||
| responsive, | ||
| }); | ||
|
|
||
| // [If Sparks Variation Swatches is enabled and ] Initialize Sparks Variation Swatches for cloned products. | ||
| if (document.body.classList.contains('sparks-vs-shop-attribute')) { | ||
| slider.events.on('transitionEnd', () => { | ||
| document.dispatchEvent( | ||
| new CustomEvent('sparksVSNeedsInit', { | ||
| detail: { | ||
| container: '.products.exclusive', | ||
| }, | ||
| }) | ||
| ); | ||
| // [If Sparks Variation Swatches is enabled and ] Initialize Sparks Variation Swatches for cloned products. | ||
| if ($('body').hasClass('sparks-vs-shop-attribute')) { | ||
| slider.events.on('transitionEnd', () => { | ||
| document.dispatchEvent( | ||
| new CustomEvent('sparksVSNeedsInit', { | ||
| detail: { container: '.products.exclusive' }, | ||
| }) | ||
| ); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Handle Gallery Image Slider | ||
| */ | ||
| function handleGallerySlider() { | ||
| const $galleryNav = $('ol.flex-control-nav'); | ||
|
|
||
| if (0 === $galleryNav.length) return; | ||
|
|
||
| const isDesktop = window.innerWidth >= 992; | ||
|
|
||
| const slider = tns({ | ||
| container: 'ol.flex-control-nav', | ||
| items: 4, | ||
| axis: isDesktop ? 'vertical' : 'horizontal', | ||
| slideBy: 'page', | ||
| rewind: true, | ||
| loop: false, | ||
| nav: false, | ||
| controls: false, | ||
| mouseDrag: true, | ||
| }); | ||
|
|
||
| addNextPrev( | ||
| $('.woocommerce-product-gallery .tns-inner')[0], | ||
| slider, | ||
| isDesktop | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Run JS on load. | ||
| */ | ||
| window.addEventListener('load', initShop); | ||
| /** | ||
| * Run JS on load. | ||
| */ | ||
| $(function () { | ||
| initShop(); | ||
| }); | ||
| })(jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is an interesting discussion.
Using
jQueryallowed us to reduce the size of the script, but we introduced a new dependancie that needs to be loaded in the page, so the total size will be this script + jQuery script size.But jQuery is also loaded by WooCommerce, thus we can ignore its script size since it will be always present.
@HardeepAsrani, what do you think?