Advertisement
GoldFXTrader

Untitled

Nov 10th, 2020
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.     'use strict';
  3.  
  4.     if (!!window.JCCatalogSectionComponent)
  5.         return;
  6.  
  7.     window.JCCatalogSectionComponent = function(params) {
  8.         this.countItems = params.countItems;
  9.         this.formPosting = false;
  10.         this.siteId = params.siteId || '';
  11.         this.ajaxId = params.ajaxId || '';
  12.         this.template = params.template || '';
  13.         this.componentPath = params.componentPath || '';
  14.         this.parameters = params.parameters || '';
  15.  
  16.         this.sectionCountBlock = $('.catalog-control #section-count');
  17.         this.animationCountData = {
  18.             start: {},
  19.             finish: {},
  20.         };
  21.  
  22.         this.animateCount();
  23.  
  24.         if (params.navParams)
  25.         {
  26.             this.navParams = {
  27.                 NavNum: params.navParams.NavNum || 1,
  28.                 NavPageNomer: parseInt(params.navParams.NavPageNomer) || 1,
  29.                 NavPageCount: parseInt(params.navParams.NavPageCount) || 1
  30.             };
  31.         }
  32.  
  33.  
  34.         this.bigData = params.bigData || {enabled: false};
  35.         this.container = document.querySelector('[data-entity="' + params.container + '"]');
  36.         this.lazyLoadContainer = document.querySelector('[data-entity="lazy-' + params.container + '"]');
  37.         this.showMoreButton = null;
  38.         this.showMoreButtonMessage = null;
  39.  
  40.         if (this.bigData.enabled && BX.util.object_keys(this.bigData.rows).length > 0)
  41.         {
  42.             BX.cookie_prefix = this.bigData.js.cookiePrefix || '';
  43.             BX.cookie_domain = this.bigData.js.cookieDomain || '';
  44.             BX.current_server_time = this.bigData.js.serverTime;
  45.  
  46.             BX.ready(BX.delegate(this.bigDataLoad, this));
  47.         }
  48.  
  49.         if (params.initiallyShowHeader)
  50.         {
  51.             BX.ready(BX.delegate(this.showHeader, this));
  52.         }
  53.  
  54.         if (params.deferredLoad)
  55.         {
  56.             BX.ready(BX.delegate(this.deferredLoad, this));
  57.         }
  58.  
  59.         if (params.lazyLoad)
  60.         {
  61.             this.showMoreButton = document.querySelector('[data-use="show-more-' + this.navParams.NavNum + '"]');
  62.             this.showMoreButtonMessage = this.showMoreButton.innerHTML;
  63.             BX.bind(this.showMoreButton, 'click', BX.proxy(this.showMore, this));
  64.         }
  65.  
  66.         if (params.loadOnScroll)
  67.         {
  68.             BX.bind(window, 'scroll', BX.proxy(this.loadOnScroll, this));
  69.         }
  70.         /*минимальная высота каталога по высоте фильтра*/
  71.         var filter = $("#filter");
  72.         var filterH = filter.height();
  73.         var productBlockH = $('.content .product-block').parents('.container').height();
  74.         if(productBlockH >= filterH){
  75.             filter.css('height', productBlockH);
  76.         }
  77.         // productBlock.parents('.container').css('min-height', filter.height());
  78.         // $('.filter-stick').css('height', filter.height());
  79.     };
  80.  
  81.     window.JCCatalogSectionComponent.prototype =
  82.     {
  83.         animateCount: function(){
  84.             var CountVal = this.sectionCountBlock.text();
  85.             this.animationCountData.finish['section-count'] = parseFloat(this.countItems);
  86.             if(CountVal) {
  87.                 this.animationCountData.start['section-count'] = parseFloat(CountVal.replace(' товаров', ''));
  88.             }else{
  89.                 this.animationCountData.start['section-count'] = 0;
  90.             }
  91.             new BX.easing({
  92.                 duration: 300,
  93.                 start: this.animationCountData.start,
  94.                 finish: this.animationCountData.finish,
  95.                 transition: BX.easing.makeEaseOut(BX.easing.transitions.quad),
  96.                 step: BX.delegate(function(state){
  97.                     for (var nodeId in this.animationCountData.start){
  98.                         if (state.hasOwnProperty(nodeId)){
  99.                             this.sectionCountBlock.text(MCT.get_correct_str(state['section-count'], 'товар', 'товара', 'товаров'));
  100.                         }
  101.                     }
  102.                 }, this),
  103.             }).animate();
  104.         },
  105.         checkButton: function()
  106.         {
  107.             if (this.showMoreButton)
  108.             {
  109.                 if (this.navParams.NavPageNomer == this.navParams.NavPageCount)
  110.                 {
  111.                     BX.remove(this.showMoreButton);
  112.                 }
  113.                 else
  114.                 {
  115.                     this.lazyLoadContainer.appendChild(this.showMoreButton);
  116.                 }
  117.             }
  118.         },
  119.  
  120.         enableButton: function()
  121.         {
  122.             if (this.showMoreButton)
  123.             {
  124.                 BX.removeClass(this.showMoreButton, 'disabled');
  125.                 this.showMoreButton.innerHTML = this.showMoreButtonMessage;
  126.             }
  127.         },
  128.  
  129.         disableButton: function()
  130.         {
  131.             if (this.showMoreButton)
  132.             {
  133.                 BX.addClass(this.showMoreButton, 'disabled');
  134.                 this.showMoreButton.innerHTML = BX.message('BTN_MESSAGE_LAZY_LOAD_WAITER');
  135.             }
  136.         },
  137.  
  138.         loadOnScroll: function()
  139.         {
  140.             var scrollTop = BX.GetWindowScrollPos().scrollTop,
  141.                 containerBottom = BX.pos(this.container).bottom;
  142.  
  143.             if (scrollTop + window.innerHeight > containerBottom)
  144.             {
  145.                 this.showMore();
  146.             }
  147.         },
  148.  
  149.         showMore: function()
  150.         {
  151.             if (this.navParams.NavPageNomer < this.navParams.NavPageCount)
  152.             {
  153.                 var data = {};
  154.                 data['action'] = 'showMore';
  155.                 data['PAGEN_' + this.navParams.NavNum] = this.navParams.NavPageNomer + 1;
  156.  
  157.                 if (!this.formPosting)
  158.                 {
  159.                     this.formPosting = true;
  160.                     this.disableButton();
  161.                     this.sendRequest(data);
  162.                 }
  163.             }
  164.         },
  165.  
  166.         bigDataLoad: function()
  167.         {
  168.             var url = 'https://analytics.bitrix.info/crecoms/v1_0/recoms.php',
  169.                 data = BX.ajax.prepareData(this.bigData.params);
  170.  
  171.             if (data)
  172.             {
  173.                 url += (url.indexOf('?') !== -1 ? '&' : '?') + data;
  174.             }
  175.  
  176.             var onReady = BX.delegate(function(result){
  177.                 this.sendRequest({
  178.                     action: 'deferredLoad',
  179.                     bigData: 'Y',
  180.                     items: result && result.items || [],
  181.                     rid: result && result.id,
  182.                     count: this.bigData.count,
  183.                     rowsRange: this.bigData.rowsRange,
  184.                     shownIds: this.bigData.shownIds
  185.                 });
  186.             }, this);
  187.  
  188.             BX.ajax({
  189.                 method: 'GET',
  190.                 dataType: 'json',
  191.                 url: url,
  192.                 timeout: 3,
  193.                 onsuccess: onReady,
  194.                 onfailure: onReady
  195.             });
  196.         },
  197.  
  198.         deferredLoad: function()
  199.         {
  200.             this.sendRequest({action: 'deferredLoad'});
  201.         },
  202.  
  203.         sendRequest: function(data)
  204.         {
  205.             $('.spinnerLoad').removeClass('d-none');
  206.             var defaultData = {
  207.                 siteId: this.siteId,
  208.                 template: this.template,
  209.                 parameters: this.parameters
  210.             };
  211.  
  212.             if (this.ajaxId)
  213.             {
  214.                 defaultData.AJAX_ID = this.ajaxId;
  215.             }
  216.  
  217.             BX.ajax({
  218.                 url: this.componentPath + '/ajax.php' + (document.location.href.indexOf('clear_cache=Y') !== -1 ? '?clear_cache=Y' : ''),
  219.                 method: 'POST',
  220.                 dataType: 'json',
  221.                 timeout: 60,
  222.                 data: BX.merge(defaultData, data),
  223.                 onsuccess: BX.delegate(function(result){
  224.                     if (!result || !result.JS)
  225.                         return;
  226.  
  227.                     BX.ajax.processScripts(
  228.                         BX.processHTML(result.JS).SCRIPT,
  229.                         false,
  230.                         BX.delegate(function(){this.showAction(result, data);}, this)
  231.                     );
  232.  
  233.                     $('.spinnerLoad').addClass('d-none');
  234.                 }, this)
  235.             });
  236.         },
  237.  
  238.         showAction: function(result, data)
  239.         {
  240.             if (!data)
  241.                 return;
  242.  
  243.             switch (data.action)
  244.             {
  245.                 case 'showMore':
  246.                     this.processShowMoreAction(result);
  247.                     break;
  248.                 case 'deferredLoad':
  249.                     this.processDeferredLoadAction(result, data.bigData === 'Y');
  250.                     break;
  251.             }
  252.         },
  253.  
  254.         processShowMoreAction: function(result)
  255.         {
  256.             this.formPosting = false;
  257.             this.enableButton();
  258.  
  259.             if (result)
  260.             {
  261.                 this.navParams.NavPageNomer++;
  262.                 this.processItems(result.items);
  263.                 this.processPagination(result.pagination);
  264.                 this.checkButton();
  265.             }
  266.         },
  267.  
  268.         processDeferredLoadAction: function(result, bigData)
  269.         {
  270.             if (!result)
  271.                 return;
  272.  
  273.             var position = bigData ? this.bigData.rows : {};
  274.  
  275.             this.processItems(result.items, BX.util.array_keys(position));
  276.         },
  277.  
  278.         processItems: function(itemsHtml, position)
  279.         {
  280.             if (!itemsHtml)
  281.                 return;
  282.  
  283.             var processed = BX.processHTML(itemsHtml, false),
  284.                 temporaryNode = BX.create('DIV');
  285.  
  286.             var items, k, origRows;
  287.  
  288.             temporaryNode.innerHTML = processed.HTML;
  289.             items = temporaryNode.querySelectorAll('[data-entity="items-row"]');
  290.  
  291.             if (items.length)
  292.             {
  293.                 this.showHeader(true);
  294.  
  295.                 for (k in items)
  296.                 {
  297.                     if (items.hasOwnProperty(k))
  298.                     {
  299.                         origRows = position ? this.container.querySelectorAll('[data-entity="items-row"]') : false;
  300.                         items[k].style.opacity = 0;
  301.  
  302.                         if (origRows && BX.type.isDomNode(origRows[position[k]]))
  303.                         {
  304.                             origRows[position[k]].parentNode.insertBefore(items[k], origRows[position[k]]);
  305.                         }
  306.                         else
  307.                         {
  308.                             this.container.appendChild(items[k]);
  309.                         }
  310.                     }
  311.                 }
  312.  
  313.                 new BX.easing({
  314.                     duration: 2000,
  315.                     start: {opacity: 0},
  316.                     finish: {opacity: 100},
  317.                     transition: BX.easing.makeEaseOut(BX.easing.transitions.quad),
  318.                     step: function(state){
  319.                         for (var k in items)
  320.                         {
  321.                             if (items.hasOwnProperty(k))
  322.                             {
  323.                                 items[k].style.opacity = state.opacity / 100;
  324.                             }
  325.                         }
  326.                     },
  327.                     complete: function(){
  328.                         for (var k in items)
  329.                         {
  330.                             if (items.hasOwnProperty(k))
  331.                             {
  332.                                 items[k].removeAttribute('style');
  333.                             }
  334.                         }
  335.                     }
  336.                 }).animate();
  337.             }
  338.  
  339.             BX.ajax.processScripts(processed.SCRIPT);
  340.         },
  341.  
  342.         processPagination: function(paginationHtml)
  343.         {
  344.             if (!paginationHtml)
  345.                 return;
  346.  
  347.             var pagination = document.querySelectorAll('[data-pagination-num="' + this.navParams.NavNum + '"]');
  348.             for (var k in pagination)
  349.             {
  350.                 if (pagination.hasOwnProperty(k))
  351.                 {
  352.                     pagination[k].innerHTML = paginationHtml;
  353.                 }
  354.             }
  355.         },
  356.  
  357.         showHeader: function(animate)
  358.         {
  359.             var parentNode = BX.findParent(this.container, {attr: {'data-entity': 'parent-container'}}),
  360.                 header;
  361.  
  362.             if (parentNode && BX.type.isDomNode(parentNode))
  363.             {
  364.                 header = parentNode.querySelector('[data-entity="header"]');
  365.  
  366.                 if (header && header.getAttribute('data-showed') != 'true')
  367.                 {
  368.                     header.style.display = '';
  369.  
  370.                     if (animate)
  371.                     {
  372.                         new BX.easing({
  373.                             duration: 2000,
  374.                             start: {opacity: 0},
  375.                             finish: {opacity: 100},
  376.                             transition: BX.easing.makeEaseOut(BX.easing.transitions.quad),
  377.                             step: function(state){
  378.                                 header.style.opacity = state.opacity / 100;
  379.                             },
  380.                             complete: function(){
  381.                                 header.removeAttribute('style');
  382.                                 header.setAttribute('data-showed', 'true');
  383.                             }
  384.                         }).animate();
  385.                     }
  386.                     else
  387.                     {
  388.                         header.style.opacity = 100;
  389.                     }
  390.                 }
  391.             }
  392.         }
  393.     };
  394.  
  395.     $('.link-block-flex .show-all-link').click(function(event) {
  396.   event.preventDefault();
  397.   $('.link-block-flex a').addClass('is-show');
  398.   $(this).hide();
  399. });
  400. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement