Advertisement
Igor150195

asdads

Feb 9th, 2024
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (() => {
  2.     'use strict';
  3.    
  4.     class ProductImageSlider {
  5.         constructor(selector = '.product-image__items:not(.tns-slider)') {
  6.             this.selector = selector;
  7.             this.items = [];
  8.             this.currentMedia = 0;
  9.             this.windowResizeTimeoutId = null;
  10.             this.onWindowResize = this.onWindowResize.bind(this);
  11.         }
  12.  
  13.         init() {
  14.             let wrappers = document.querySelectorAll(this.selector);
  15.  
  16.             for (let wrapper of wrappers) {
  17.                 let el = new ProductImageSliderEl(wrapper).init();
  18.                 this.items.push(el);
  19.             }
  20.            
  21.             if (window.matchMedia('(min-width: 1024px)').matches) {
  22.                 this.currentMedia = 1;
  23.             }
  24.            
  25.             window.addEventListener('resize', this.onWindowResize);
  26.         }
  27.         onWindowResize() {
  28.             if (this.windowResizeTimeoutId) {
  29.                 clearTimeout(this.windowResizeTimeoutId);
  30.             }
  31.            
  32.             this.windowResizeTimeoutId = setTimeout(() => {
  33.                 let beforeResizeMedia = this.currentMedia;
  34.                
  35.                 this.currentMedia = 0;
  36.                
  37.                 if (window.matchMedia('(min-width: 1024px)').matches) {
  38.                     this.currentMedia = 1;
  39.                 }
  40.                 if ( (this.currentMedia === 1 && beforeResizeMedia === 0) || (this.currentMedia === 0 && beforeResizeMedia === 1) ) {
  41.                     for (let item of this.items) {
  42.                         item.destroy();
  43.                         item.init();
  44.                     }
  45.                 } else {
  46.                     for (let item of this.items) {
  47.                         item.imageThumbsSliderControlsHide();
  48.                     }
  49.                 }
  50.             }, 250);
  51.         }
  52.     }
  53.  
  54.     class ProductImageSliderEl {
  55.         constructor(el) {
  56.             this.wrapper = el;
  57.             this.slider = null;
  58.             this.thumbsSlider = null;
  59.             this.productItemEl = null;
  60.             this.imageThumbs = null;
  61.             this.productItemOverlay = null;
  62.             this.imageThumbsItemsEl = null;
  63.             this.sliderNav = false;
  64.             this.thumbsSliderGutter = 10;
  65.             this.init = this.init.bind(this);
  66.             this.destroy = this.destroy.bind(this);
  67.             this.onProductItemElMouseenter = this.onProductItemElMouseenter.bind(this);
  68.             this.onProductItemElMouseleave = this.onProductItemElMouseleave.bind(this);
  69.             this.imageSliderInit = this.imageSliderInit.bind(this);
  70.             this.imageSliderDestroy = this.imageSliderDestroy.bind(this);
  71.             this.onImageSliderTouchDragEnd = this.onImageSliderTouchDragEnd.bind(this);
  72.             this.imageThumbsSliderInit = this.imageThumbsSliderInit.bind(this);
  73.             this.onImageThumbsSliderInit = this.onImageThumbsSliderInit.bind(this);
  74.             this.imageThumbsSliderControlsHide = this.imageThumbsSliderControlsHide.bind(this);
  75.             this.imageThumbsSliderDestroy = this.imageThumbsSliderDestroy.bind(this);
  76.             this.onImageThumbsItemsElMouseover = this.onImageThumbsItemsElMouseover.bind(this);
  77.         }
  78.  
  79.         init() {
  80.             let wrapper = this.wrapper,
  81.                 productItemEl = this.productItemEl = wrapper.closest('.product-item-thumb2');
  82.            
  83.             if (productItemEl) {
  84.                 this.imageThumbsItemsEl = productItemEl.querySelector('.product-image-thumbs__items:not(.tns-slider)');
  85.             }
  86.            
  87.             if (this.imageThumbsItemsEl) {
  88.                 this.sliderNav = true;
  89.                 this.imageThumbs = productItemEl.querySelector('.product-image-thumbs');
  90.                 this.productItemOverlay = productItemEl.querySelector('.product-item-overlay');
  91.             }
  92.            
  93.             if (this.imageThumbs && this.productItemOverlay) {
  94.                 productItemEl.addEventListener('mouseenter', this.onProductItemElMouseenter);
  95.                 productItemEl.addEventListener('mouseleave', this.onProductItemElMouseleave);
  96.             }
  97.            
  98.             this.imageSliderInit();
  99.             this.imageThumbsSliderInit();
  100.  
  101.             return this;
  102.         }
  103.        
  104.         destroy() {
  105.             if (this.productItemEl && this.imageThumbs && this.productItemOverlay) {
  106.                 this.productItemEl.classList.remove('image-thumbs-active');
  107.                 this.productItemEl.removeEventListener('mouseenter', this.onProductItemElMouseenter);
  108.                 this.productItemEl.removeEventListener('mouseleave', this.onProductItemElMouseleave);
  109.             }
  110.            
  111.             this.imageSliderDestroy();
  112.             this.imageThumbsSliderDestroy();
  113.            
  114.             this.imageThumbs = null;
  115.             this.productItemOverlay = null;
  116.             this.imageThumbsItemsEl = null;
  117.             this.sliderNav = false;
  118.         }
  119.        
  120.         onProductItemElMouseenter() {
  121.             if (!window.matchMedia('(min-width: 1024px)').matches) {
  122.                 return;
  123.             }
  124.            
  125.             let imageThumbs = this.imageThumbs,
  126.                 productItemOverlay = this.productItemOverlay;
  127.                
  128.             if (imageThumbs && productItemOverlay) {
  129.                 let productItemElRect = this.productItemEl.getBoundingClientRect();
  130.                
  131.                 this.productItemEl.classList.add('image-thumbs-active');
  132.                
  133.                 if (productItemElRect.left >= 115) {
  134.                     imageThumbs.classList.add('product-image-thumbs--left');
  135.                     productItemOverlay.classList.add('product-item-overlay--left');
  136.                 } else {
  137.                     imageThumbs.classList.add('product-image-thumbs--right');
  138.                     productItemOverlay.classList.add('product-item-overlay--right');
  139.                 }
  140.                
  141.                 let tnsOuter = imageThumbs.querySelector('.tns-outer'),
  142.                     tnsOuterClientHeight;
  143.                
  144.                 if (tnsOuter) {
  145.                     tnsOuterClientHeight = tnsOuter.clientHeight;
  146.                    
  147.                     let productItemOverlayStyle = window.getComputedStyle(productItemOverlay),
  148.                         productItemOverlayTop = parseInt(productItemOverlayStyle.top),
  149.                         productItemOverlayBottom = parseInt(productItemOverlayStyle.bottom);
  150.                    
  151.                     productItemOverlay.style.minHeight = (tnsOuterClientHeight - productItemOverlayTop - productItemOverlayBottom) + 'px';
  152.                 }
  153.                
  154.                 let tnsNextButton = this.thumbsSlider ? this.thumbsSlider.getInfo().nextButton : null;
  155.                
  156.                 if (tnsNextButton && tnsOuter) {
  157.                     tnsNextButton.style.top = (tnsOuterClientHeight - tnsNextButton.clientHeight - this.thumbsSliderGutter) + 'px';
  158.                 }
  159.             }
  160.         }
  161.        
  162.         onProductItemElMouseleave() {
  163.             if (!window.matchMedia('(min-width: 1024px)').matches) {
  164.                 return;
  165.             }
  166.            
  167.             let imageThumbs = this.imageThumbs,
  168.                 productItemOverlay = this.productItemOverlay;
  169.                
  170.             if (imageThumbs && productItemOverlay) {
  171.                 this.productItemEl.classList.remove('image-thumbs-active');
  172.                 imageThumbs.classList.remove('product-image-thumbs--left');
  173.                 productItemOverlay.classList.remove('product-item-overlay--left');
  174.                 imageThumbs.classList.remove('product-image-thumbs--right');
  175.                 productItemOverlay.classList.remove('product-item-overlay--right');
  176.                 productItemOverlay.style.minHeight = '';
  177.                
  178.                 let tnsNextButton = this.thumbsSlider ? this.thumbsSlider.getInfo().nextButton : null;
  179.                
  180.                 if (tnsNextButton) {
  181.                     tnsNextButton.style.top = '';
  182.                 }
  183.             }
  184.         }
  185.        
  186.         imageSliderInit() {
  187.             this.slider = tns({
  188.                 container: this.wrapper,
  189.                 items: 1,
  190.                 navPosition: 'bottom',
  191.                 loop: false,
  192.                 autoHeight: false,
  193.                 controls: false,
  194.                 autoplay: false,
  195.                 rewind: true,
  196.                 nav: this.sliderNav,
  197.                 navAsThumbnails: true,
  198.                 navContainer: this.imageThumbsItemsEl,
  199.                 swipeAngle: false,
  200.                 responsive: {
  201.                     0: {
  202.                         autoplay: true,
  203.                         loop: false
  204.                     },
  205.                     1024: {
  206.                         autoplay: false,
  207.                         loop: false
  208.                     }
  209.                 }
  210.             });
  211.  
  212.             const self = this;
  213.        
  214.             this.slider.events.on('indexChanged', function(info) {
  215.                 if (window.matchMedia('(max-width: 1023px)').matches) {
  216.                     self.thumbsSlider.goTo(info.index);
  217.                 }
  218.             });
  219.         }
  220.        
  221.         imageSliderDestroy() {
  222.             if (this.slider) {
  223.                 this.slider.destroy();
  224.                 this.slider = null;
  225.                
  226.                 if (this.productItemEl) {
  227.                     this.wrapper = this.productItemEl.querySelector('.product-image__items:not(.tns-slider)');
  228.                 }
  229.             }
  230.         }
  231.        
  232.         onImageSliderTouchDragEnd(info) {
  233.             if (this.thumbsSlider) {
  234.                 let index = info.displayIndex - 1;
  235.                        
  236.                 this.thumbsSlider.goTo(index);
  237.             }
  238.            
  239.         }
  240.        
  241.         imageThumbsSliderInit() {
  242.             let productItemEl = this.productItemEl,
  243.                 imageThumbsItemsEl = null,
  244.                 thumbsSliderAxis = 'horizontal',
  245.                 thumbsSliderAutoWidth = true;
  246.            
  247.             if (productItemEl) {
  248.                 imageThumbsItemsEl = this.imageThumbsItemsEl = productItemEl.querySelector('.product-image-thumbs__items:not(.tns-slider)');
  249.             }
  250.            
  251.             if (window.matchMedia('(min-width: 1024px)').matches) {
  252.                 thumbsSliderAxis = 'vertical';
  253.                 thumbsSliderAutoWidth = false;
  254.             }
  255.            
  256.             if (imageThumbsItemsEl) {
  257.                 this.thumbsSlider = tns({
  258.                     container: imageThumbsItemsEl,
  259.                     axis: thumbsSliderAxis,
  260.                     autoWidth: thumbsSliderAutoWidth,
  261.                     items: 5,
  262.                     nav: false,
  263.                     autoHeight: false,
  264.                     controls: true,
  265.                     controlsPosition: 'bottom',
  266.                     gutter: this.thumbsSliderGutter,
  267.                     onInit: this.onImageThumbsSliderInit,
  268.                     responsive: {
  269.                         0: {
  270.                             loop: false,
  271.                             autoplay: false
  272.                         },
  273.                         1024: {
  274.                             autoplay: false,
  275.                             loop: false
  276.                         }
  277.                     }
  278.                 });
  279.                
  280.                 imageThumbsItemsEl.addEventListener('mouseover', this.onImageThumbsItemsElMouseover);
  281.                
  282.                 if (this.slider) {
  283.                     this.slider.events.on('touchEnd', this.onImageSliderTouchDragEnd);
  284.                     this.slider.events.on('dragEnd', this.onImageSliderTouchDragEnd);
  285.                 }
  286.             }
  287.         }
  288.  
  289.         imageThumbsSliderDestroy() {
  290.             if (this.thumbsSlider && this.imageThumbsItemsEl) {
  291.                 this.imageThumbsItemsEl.removeEventListener('mouseover', this.onImageThumbsItemsElMouseover);
  292.                 this.thumbsSlider.destroy();
  293.                 this.thumbsSlider = null;
  294.             }
  295.         }
  296.        
  297.         onImageThumbsSliderInit() {
  298.             this.imageThumbsSliderControlsHide();
  299.         }
  300.        
  301.         imageThumbsSliderControlsHide() {
  302.             let info = this.thumbsSlider.getInfo(),
  303.                 nextButton = info.nextButton,
  304.                 nextButtonVisible = false,
  305.                 imageThumbs = this.imageThumbs,
  306.                 productItemOverlay = this.productItemOverlay;
  307.            
  308.             if (nextButton) {
  309.                 nextButtonVisible = nextButton.offsetWidth || nextButton.offsetHeight || nextButton.getClientRects().length;
  310.             }
  311.                
  312.             if (!nextButtonVisible && imageThumbs && productItemOverlay) {
  313.                 if (window.matchMedia('(min-width: 1024px)').matches) {
  314.                     let tnsOuter = imageThumbs.querySelector('.tns-outer');
  315.                    
  316.                     if (tnsOuter) {
  317.                         tnsOuter.classList.add('hidden-controls');
  318.                     }
  319.                 } else {
  320.                     let thumbsItemsWrapper = imageThumbs.querySelector('.product-image-thumbs__items-wrapper');
  321.                    
  322.                     if (thumbsItemsWrapper) {
  323.                         thumbsItemsWrapper.classList.add('hidden-controls');
  324.                     }
  325.                 }
  326.             }
  327.         }
  328.        
  329.         onImageThumbsItemsElMouseover(e) {
  330.             let imageThumbsEl = e.target.closest('.product-image-thumbs__item-wrapper');
  331.            
  332.             if (imageThumbsEl) {
  333.                 imageThumbsEl.click();
  334.             }
  335.         }
  336.     }
  337.  
  338.     window.ProductImageSlider = ProductImageSlider;
  339. })();
  340.  
  341. (() => {
  342.     'use strict';
  343.  
  344.     document.addEventListener('DOMContentLoaded', () => {
  345.         new window.ProductImageSlider().init();
  346.     });
  347. })();
  348. $(document).on("ajaxComplete", function() {
  349.     new window.ProductImageSlider().init();
  350. });
  351.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement