Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. if ($filter) {
  2.     //price filter_slider;
  3.     var select_model = $filter.find('select[name="model"]'),
  4.         select_body = $filter.find('select[name="kuzov"]'),
  5.         select_engine = $filter.find('select[name="dvigun"]');
  6.  
  7.     $('#filterReset').click(function () {
  8.         select_body.find('option').each(function () {
  9.             $(this).removeAttr('disabled');
  10.         });
  11.         select_engine.find('option').each(function () {
  12.             $(this).removeAttr('disabled');
  13.         });
  14.         select_model.find('option').each(function () {
  15.             $(this).removeAttr('disabled');
  16.         });
  17.         $('.cars-filter__form').trigger('reset');
  18.         filter_slider.noUiSlider.set(['2000000']);
  19.         filterCars();
  20.         $(this).removeClass('active');
  21.     });
  22.  
  23.     select_model.change(function () {
  24.  
  25.         select_body.find('option').each(function () {
  26.             $(this).removeAttr('disabled');
  27.         });
  28.         select_engine.find('option').each(function () {
  29.             $(this).removeAttr('disabled');
  30.         });
  31.         // select_model.find('option').each(function () {
  32.         //     $(this).removeAttr('disabled');
  33.         // });
  34.         filterCars();
  35.         var val = $(this).val(),
  36.             selected = $(this).find('option:selected'),
  37.             bodies = selected.data('body'),
  38.             engines = selected.data('engines');
  39.  
  40.         if (val != 0) {
  41.             !$('#filterReset').hasClass('active') ? $('#filterReset').addClass('active') : console.log('model already showed');
  42.             bodies = bodies.split(',');
  43.             console.log(bodies);
  44.  
  45.             engines = engines.split(',');
  46.             console.log(engines);
  47.  
  48.             // select_model.find('option').attr('disabled', 'true');
  49.             select_body.find('option').each(function () {
  50.                 var val_arr = $(this).val().split(','),
  51.                     elem = $(this);
  52.                 if (val_arr.length >= 1) {
  53.                     var valid = 0;
  54.                     for (var i = 0; i <= val_arr.length; i++) {
  55.                         if (bodies.includes(val_arr[i]) && val_arr[i] != 0)
  56.                             valid++;
  57.                     }
  58.                     if (valid == 0)
  59.                         elem.attr('disabled', true);
  60.  
  61.                 } else if (!bodies.includes($(this).val()) && $(this).val() != 0) {
  62.                     $(this).attr('disabled', true);
  63.                 }
  64.             });
  65.  
  66.             select_engine.find('option').each(function () {
  67.                 // $(this).removeAttr('disabled');
  68.                 if (!engines.includes($(this).val()) && $(this).val() != 0) {
  69.                     $(this).attr('disabled', true);
  70.                 }
  71.             });
  72.  
  73.         }
  74.     });
  75.  
  76.     select_body.change(function () {
  77.         select_engine.find('option').each(function () {
  78.             $(this).removeAttr('disabled');
  79.         });
  80.         if (select_model.val() != 0) {
  81.             select_model.find('option').each(function () {
  82.                 $(this).removeAttr('disabled');
  83.             });
  84.         }
  85.         filterCars();
  86.         var val = $(this).val(),
  87.             selected = $(this).find('option:selected'),
  88.             models = selected.data('models'),
  89.             engines = selected.data('engines');
  90.         if (val != 0) {
  91.             !$('#filterReset').hasClass('active') ? $('#filterReset').addClass('active') : console.log('body already showed');
  92.  
  93.             models = models.split(',');
  94.             console.log(models);
  95.             engines = engines.split(',');
  96.             console.log(engines);
  97.             // select_model.find('option').attr('disabled', 'true');
  98.             select_model.find('option').each(function () {
  99.                 // $(this).removeAttr('disabled');
  100.                 console.log(models.includes($(this).val()));
  101.                 if (!models.includes($(this).val()) && $(this).val() != 0) {
  102.                     $(this).attr('disabled', true);
  103.                 }
  104.             });
  105.             select_engine.find('option').each(function () {
  106.                 // $(this).removeAttr('disabled');
  107.                 console.log(engines.includes($(this).val()));
  108.                 if (!engines.includes($(this).val()) && $(this).val() != 0) {
  109.                     $(this).attr('disabled', true);
  110.                 }
  111.             })
  112.  
  113.         }
  114.     });
  115.  
  116.     select_engine.change(function () {
  117.         select_body.find('option').each(function () {
  118.             $(this).removeAttr('disabled');
  119.         });
  120.         // select_engine.find('option').each(function () {
  121.         //     $(this).removeAttr('disabled');
  122.         // });
  123.         select_model.find('option').each(function () {
  124.             $(this).removeAttr('disabled');
  125.         });
  126.         filterCars();
  127.         var val = $(this).val(),
  128.             selected = $(this).find('option:selected'),
  129.             models = selected.data('models'),
  130.             bodies = selected.data('bodies');
  131.         console.log('av models:' + models);
  132.         console.log('av bodies:' + bodies);
  133.  
  134.         if (val != 0) {
  135.             !$('#filterReset').hasClass('active') ? $('#filterReset').addClass('active') : console.log('energy already showed');
  136.  
  137.             bodies = bodies.split(',');
  138.             console.log(bodies);
  139.  
  140.             models = models.split(',');
  141.             console.log(models);
  142.  
  143.             select_model.find('option').each(function () {
  144.                 // $(this).removeAttr('disabled');
  145.                 if (!models.includes($(this).val()) && $(this).val() != 0) {
  146.                     $(this).attr('disabled', true);
  147.                 }
  148.             });
  149.             // select_model.find('option').attr('disabled', 'true');
  150.             select_body.find('option').each(function () {
  151.                 // $(this).removeAttr('disabled');
  152.                 // if($(this).val().split())
  153.                 if (!bodies.includes($(this).val()) && $(this).val() != 0) {
  154.                     $(this).attr('disabled', true);
  155.                 }
  156.             });
  157.  
  158.         }
  159.     });
  160.  
  161.  
  162.     if (filter_slider) {
  163.         filter_slider.noUiSlider.on('change', function () {
  164.             filterCars();
  165.         });
  166.     }
  167. }
  168.  
  169. function filterCars() {
  170.     var $all_items = $('.car-filter-item'),
  171.         select_model = $filter.find('select[name="model"]'),
  172.         select_body = $filter.find('select[name="kuzov"]'),
  173.         select_engine = $filter.find('select[name="dvigun"]'),
  174.         model_val = select_model.val(),
  175.         body_val = select_body.val(),
  176.         engine_val = select_engine.val(),
  177.         selected_arr = [],
  178.         query = '',
  179.         price_val = filter_slider.noUiSlider.get();
  180.  
  181.     //
  182.     if (body_val != 0)
  183.         body_val = body_val.toLowerCase();
  184.     if (engine_val != 0)
  185.         engine_val = engine_val.toLowerCase();
  186.  
  187.  
  188.     console.log('body ' + body_val);
  189.     console.log('engine ' + engine_val);
  190.     console.log('model ' + model_val);
  191.     if (model_val != 0) {
  192.         query += '[data-id="' + model_val + '"]';
  193.     }
  194.     if (body_val != 0 && body_val.split(',').length <= 1) {
  195.         query += '[data-body-' + body_val + '="1"]';
  196.     }
  197.     if (engine_val != 0 && engine_val.split(',').length <= 1) {
  198.         query += '[data-engine-' + engine_val + '="1"]';
  199.     }
  200.     // console.log(query);
  201.     var selected_items = $('.car-filter-item' + query);
  202.  
  203.  
  204.     selected_items.each(function (index) {
  205.         var price = $(this).attr('data-price'),
  206.             id = $(this).attr('data-id'),
  207.             valid = 0;
  208.  
  209.         if (body_val != 0 && body_val.split(',').length > 0) {
  210.             for (var j = 0; j < body_val.split(',').length; j++) {
  211.                 if ($(this).attr('data-body-' + body_val.split(',')[j]) == "1") {
  212.                     // available_bodies.push(body_val.split(',')[j]);
  213.                     valid++;
  214.                 }
  215.             }
  216.         } else
  217.             valid++;
  218.  
  219.         if (engine_val != 0 && engine_val.split(',').length > 0) {
  220.             for (var x = 0; x < engine_val.split(',').length; x++) {
  221.                 if ($(this).attr('data-engine-' + engine_val.split(',')[x]) == '1') {
  222.                     // available_engines.push(engine_val.split(',')[x]);
  223.                     valid++;
  224.                 }
  225.             }
  226.         } else
  227.             valid++;
  228.  
  229.         if (parseFloat(price) < price_val)
  230.             valid++;
  231.  
  232.         if (valid >= 3)
  233.             selected_arr.push(id);
  234.     });
  235.     $all_items.hide();
  236.     console.log(selected_arr);
  237.     //hide elements and show filtered
  238.     $('.car-filter-active').removeClass('car-filter-active');
  239.     for (var h = 0; h < selected_arr.length; h++) {
  240.         $('.car-filter-item[data-id="' + selected_arr[h] + '"]').show();
  241.         $('.car-filter-item[data-id="' + selected_arr[h] + '"]').addClass('car-filter-active');
  242.     }
  243.     //updating the carousel
  244.     if ($('.car-main') && $(window).width() <= 768) {
  245.         console.log('reinit');
  246.         $('.car-main').trigger('destroy.owl.carousel');
  247.         $('.car-main').html($('.car-main__duplicate').html());
  248.         $('.car-main').find('.car-filter-item').not('.car-filter-active').remove();
  249.         // initMobileCarsCarousel();
  250.         $('.car-main').owlCarousel({
  251.             stagePadding: 80,
  252.             margin: 5,
  253.             items: 1,
  254.             dots: false
  255.         });
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement