Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 144 additions & 82 deletions assets/js/src/shop/app.js
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);
92 changes: 92 additions & 0 deletions assets/scss/components/compat/woocommerce/_sidebar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,95 @@
}
}
}

body:not(.sp-ct-enabled) {

.tns-ovh {
display: flex;
align-items: center;
cursor: pointer;
}

.tns-inner {
overflow: hidden;
}

.tns-visually-hidden {
display: none;
}

/* Make WooCommerce gallery vertical on desktop */
@media (min-width: 992px) {

div.product {

.onsale {
left: 110px;
}

div.images {
display: flex;
flex-direction: row-reverse;
gap: 10px;

.tns-ovh {
width: 100px;
flex-direction: column;
position: relative;

.dashicons {
position: absolute;
z-index: 1;
color: var(--nv-text-color);
width: 100px;
text-align: center;

&.prev {
top: 0;
}

&.next {
bottom: 0;
}

&:hover {
background-color: var(--nv-site-bg);
}
}
}

.flex-viewport {
width: calc(100% - 100px);
}

.flex-control-nav {
display: flex;
flex-direction: column;
width: 100px;
margin-top: -5px;

li {
width: 100px;
}
}
}
}
}

/* On mobile, keep horizontal layout */
@media (max-width: 991px) {

div.product {

div.images {

.flex-control-nav {
flex-direction: row;
width: auto;
max-height: none;
display: flex;
}
}
}
}
}
2 changes: 1 addition & 1 deletion inc/core/front_end.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ private function add_scripts() {
}

if ( class_exists( 'WooCommerce', false ) && is_woocommerce() ) {
wp_register_script( 'neve-shop-script', NEVE_ASSETS_URL . 'js/build/modern/shop.js', array(), NEVE_VERSION, true );
wp_register_script( 'neve-shop-script', NEVE_ASSETS_URL . 'js/build/modern/shop.js', array( 'jquery' ), NEVE_VERSION, true );
Copy link
Contributor

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 jQuery allowed 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?

wp_enqueue_script( 'neve-shop-script' );
wp_script_add_data( 'neve-shop-script', 'async', true );
}
Expand Down
Loading