Advertisement
Igor150195

asdasdasd

Feb 9th, 2024 (edited)
785
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.                 autoplayButtonOutput: false, // Отключаем кнопки start/stop
  195.                 autoplayHoverPause: true, // Пауза при наведении мыши
  196.                 autoplayResetOnVisibility: false, // Не сбрасывать автопрокрутку при изменении видимости
  197.                 autoplay: false,
  198.                 rewind: true,
  199.                 nav: this.sliderNav,
  200.                 navAsThumbnails: true,
  201.                 navContainer: this.imageThumbsItemsEl,
  202.                 swipeAngle: false,
  203.                 responsive: {
  204.                     0: {
  205.                         autoplay: true,
  206.                         loop: false
  207.                     },
  208.                     1024: {
  209.                         autoplay: false,
  210.                         loop: false
  211.                     }
  212.                 }
  213.             });
  214.  
  215.             const self = this;
  216.        
  217.             this.slider.events.on('indexChanged', function(info) {
  218.                 if (window.matchMedia('(max-width: 1023px)').matches) {
  219.                     self.thumbsSlider.goTo(info.index);
  220.  
  221.                     setTimeout(function() {
  222.                         self.slider.play();
  223.                     }, 800);
  224.                 }
  225.             });
  226.         }
  227.        
  228.         imageSliderDestroy() {
  229.             if (this.slider) {
  230.                 this.slider.destroy();
  231.                 this.slider = null;
  232.                
  233.                 if (this.productItemEl) {
  234.                     this.wrapper = this.productItemEl.querySelector('.product-image__items:not(.tns-slider)');
  235.                 }
  236.             }
  237.         }
  238.        
  239.         onImageSliderTouchDragEnd(info) {
  240.             if (this.thumbsSlider) {
  241.                 let index = info.displayIndex - 1;
  242.                        
  243.                 this.thumbsSlider.goTo(index);
  244.             }
  245.            
  246.         }
  247.        
  248.         imageThumbsSliderInit() {
  249.             let productItemEl = this.productItemEl,
  250.                 imageThumbsItemsEl = null,
  251.                 thumbsSliderAxis = 'horizontal',
  252.                 thumbsSliderAutoWidth = true;
  253.            
  254.             if (productItemEl) {
  255.                 imageThumbsItemsEl = this.imageThumbsItemsEl = productItemEl.querySelector('.product-image-thumbs__items:not(.tns-slider)');
  256.             }
  257.            
  258.             if (window.matchMedia('(min-width: 1024px)').matches) {
  259.                 thumbsSliderAxis = 'vertical';
  260.                 thumbsSliderAutoWidth = false;
  261.             }
  262.            
  263.             if (imageThumbsItemsEl) {
  264.                 this.thumbsSlider = tns({
  265.                     container: imageThumbsItemsEl,
  266.                     axis: thumbsSliderAxis,
  267.                     autoWidth: thumbsSliderAutoWidth,
  268.                     items: 5,
  269.                     nav: false,
  270.                     autoplayButtonOutput: false, // Отключаем кнопки start/stop
  271.                     autoplayHoverPause: true, // Пауза при наведении мыши
  272.                     autoplayResetOnVisibility: false, // Не сбрасывать автопрокрутку при изменении видимости
  273.                     autoHeight: false,
  274.                     controls: true,
  275.                     controlsPosition: 'bottom',
  276.                     gutter: this.thumbsSliderGutter,
  277.                     onInit: this.onImageThumbsSliderInit,
  278.                     responsive: {
  279.                         0: {
  280.                             loop: false,
  281.                             autoplay: false
  282.                         },
  283.                         1024: {
  284.                             autoplay: false,
  285.                             loop: false
  286.                         }
  287.                     }
  288.                 });
  289.                
  290.                 imageThumbsItemsEl.addEventListener('mouseover', this.onImageThumbsItemsElMouseover);
  291.                
  292.                 if (this.slider) {
  293.                     this.slider.events.on('touchEnd', this.onImageSliderTouchDragEnd);
  294.                     this.slider.events.on('dragEnd', this.onImageSliderTouchDragEnd);
  295.                 }
  296.             }
  297.         }
  298.  
  299.         imageThumbsSliderDestroy() {
  300.             if (this.thumbsSlider && this.imageThumbsItemsEl) {
  301.                 this.imageThumbsItemsEl.removeEventListener('mouseover', this.onImageThumbsItemsElMouseover);
  302.                 this.thumbsSlider.destroy();
  303.                 this.thumbsSlider = null;
  304.             }
  305.         }
  306.        
  307.         onImageThumbsSliderInit() {
  308.             this.imageThumbsSliderControlsHide();
  309.         }
  310.        
  311.         imageThumbsSliderControlsHide() {
  312.             let info = this.thumbsSlider.getInfo(),
  313.                 nextButton = info.nextButton,
  314.                 nextButtonVisible = false,
  315.                 imageThumbs = this.imageThumbs,
  316.                 productItemOverlay = this.productItemOverlay;
  317.            
  318.             if (nextButton) {
  319.                 nextButtonVisible = nextButton.offsetWidth || nextButton.offsetHeight || nextButton.getClientRects().length;
  320.             }
  321.                
  322.             if (!nextButtonVisible && imageThumbs && productItemOverlay) {
  323.                 if (window.matchMedia('(min-width: 1024px)').matches) {
  324.                     let tnsOuter = imageThumbs.querySelector('.tns-outer');
  325.                    
  326.                     if (tnsOuter) {
  327.                         tnsOuter.classList.add('hidden-controls');
  328.                     }
  329.                 } else {
  330.                     let thumbsItemsWrapper = imageThumbs.querySelector('.product-image-thumbs__items-wrapper');
  331.                    
  332.                     if (thumbsItemsWrapper) {
  333.                         thumbsItemsWrapper.classList.add('hidden-controls');
  334.                     }
  335.                 }
  336.             }
  337.         }
  338.        
  339.         onImageThumbsItemsElMouseover(e) {
  340.             let imageThumbsEl = e.target.closest('.product-image-thumbs__item-wrapper');
  341.            
  342.             if (imageThumbsEl) {
  343.                 imageThumbsEl.click();
  344.             }
  345.         }
  346.     }
  347.  
  348.     window.ProductImageSlider = ProductImageSlider;
  349. })();
  350.  
  351. (() => {
  352.     'use strict';
  353.  
  354.     document.addEventListener('DOMContentLoaded', () => {
  355.         new window.ProductImageSlider().init();
  356.     });
  357. })();
  358. $(document).on("ajaxComplete", function() {
  359.     new window.ProductImageSlider().init();
  360. });
  361.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement