Advertisement
Igor150195

asd

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