Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ($filter) {
- //price filter_slider;
- var select_model = $filter.find('select[name="model"]'),
- select_body = $filter.find('select[name="kuzov"]'),
- select_engine = $filter.find('select[name="dvigun"]');
- $('#filterReset').click(function () {
- select_body.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- select_engine.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- select_model.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- $('.cars-filter__form').trigger('reset');
- filter_slider.noUiSlider.set(['2000000']);
- filterCars();
- $(this).removeClass('active');
- });
- select_model.change(function () {
- select_body.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- select_engine.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- // select_model.find('option').each(function () {
- // $(this).removeAttr('disabled');
- // });
- filterCars();
- var val = $(this).val(),
- selected = $(this).find('option:selected'),
- bodies = selected.data('body'),
- engines = selected.data('engines');
- if (val != 0) {
- !$('#filterReset').hasClass('active') ? $('#filterReset').addClass('active') : console.log('model already showed');
- bodies = bodies.split(',');
- console.log(bodies);
- engines = engines.split(',');
- console.log(engines);
- // select_model.find('option').attr('disabled', 'true');
- select_body.find('option').each(function () {
- var val_arr = $(this).val().split(','),
- elem = $(this);
- if (val_arr.length >= 1) {
- var valid = 0;
- for (var i = 0; i <= val_arr.length; i++) {
- if (bodies.includes(val_arr[i]) && val_arr[i] != 0)
- valid++;
- }
- if (valid == 0)
- elem.attr('disabled', true);
- } else if (!bodies.includes($(this).val()) && $(this).val() != 0) {
- $(this).attr('disabled', true);
- }
- });
- select_engine.find('option').each(function () {
- // $(this).removeAttr('disabled');
- if (!engines.includes($(this).val()) && $(this).val() != 0) {
- $(this).attr('disabled', true);
- }
- });
- }
- });
- select_body.change(function () {
- select_engine.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- if (select_model.val() != 0) {
- select_model.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- }
- filterCars();
- var val = $(this).val(),
- selected = $(this).find('option:selected'),
- models = selected.data('models'),
- engines = selected.data('engines');
- if (val != 0) {
- !$('#filterReset').hasClass('active') ? $('#filterReset').addClass('active') : console.log('body already showed');
- models = models.split(',');
- console.log(models);
- engines = engines.split(',');
- console.log(engines);
- // select_model.find('option').attr('disabled', 'true');
- select_model.find('option').each(function () {
- // $(this).removeAttr('disabled');
- console.log(models.includes($(this).val()));
- if (!models.includes($(this).val()) && $(this).val() != 0) {
- $(this).attr('disabled', true);
- }
- });
- select_engine.find('option').each(function () {
- // $(this).removeAttr('disabled');
- console.log(engines.includes($(this).val()));
- if (!engines.includes($(this).val()) && $(this).val() != 0) {
- $(this).attr('disabled', true);
- }
- })
- }
- });
- select_engine.change(function () {
- select_body.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- // select_engine.find('option').each(function () {
- // $(this).removeAttr('disabled');
- // });
- select_model.find('option').each(function () {
- $(this).removeAttr('disabled');
- });
- filterCars();
- var val = $(this).val(),
- selected = $(this).find('option:selected'),
- models = selected.data('models'),
- bodies = selected.data('bodies');
- console.log('av models:' + models);
- console.log('av bodies:' + bodies);
- if (val != 0) {
- !$('#filterReset').hasClass('active') ? $('#filterReset').addClass('active') : console.log('energy already showed');
- bodies = bodies.split(',');
- console.log(bodies);
- models = models.split(',');
- console.log(models);
- select_model.find('option').each(function () {
- // $(this).removeAttr('disabled');
- if (!models.includes($(this).val()) && $(this).val() != 0) {
- $(this).attr('disabled', true);
- }
- });
- // select_model.find('option').attr('disabled', 'true');
- select_body.find('option').each(function () {
- // $(this).removeAttr('disabled');
- // if($(this).val().split())
- if (!bodies.includes($(this).val()) && $(this).val() != 0) {
- $(this).attr('disabled', true);
- }
- });
- }
- });
- if (filter_slider) {
- filter_slider.noUiSlider.on('change', function () {
- filterCars();
- });
- }
- }
- function filterCars() {
- var $all_items = $('.car-filter-item'),
- select_model = $filter.find('select[name="model"]'),
- select_body = $filter.find('select[name="kuzov"]'),
- select_engine = $filter.find('select[name="dvigun"]'),
- model_val = select_model.val(),
- body_val = select_body.val(),
- engine_val = select_engine.val(),
- selected_arr = [],
- query = '',
- price_val = filter_slider.noUiSlider.get();
- //
- if (body_val != 0)
- body_val = body_val.toLowerCase();
- if (engine_val != 0)
- engine_val = engine_val.toLowerCase();
- console.log('body ' + body_val);
- console.log('engine ' + engine_val);
- console.log('model ' + model_val);
- if (model_val != 0) {
- query += '[data-id="' + model_val + '"]';
- }
- if (body_val != 0 && body_val.split(',').length <= 1) {
- query += '[data-body-' + body_val + '="1"]';
- }
- if (engine_val != 0 && engine_val.split(',').length <= 1) {
- query += '[data-engine-' + engine_val + '="1"]';
- }
- // console.log(query);
- var selected_items = $('.car-filter-item' + query);
- selected_items.each(function (index) {
- var price = $(this).attr('data-price'),
- id = $(this).attr('data-id'),
- valid = 0;
- if (body_val != 0 && body_val.split(',').length > 0) {
- for (var j = 0; j < body_val.split(',').length; j++) {
- if ($(this).attr('data-body-' + body_val.split(',')[j]) == "1") {
- // available_bodies.push(body_val.split(',')[j]);
- valid++;
- }
- }
- } else
- valid++;
- if (engine_val != 0 && engine_val.split(',').length > 0) {
- for (var x = 0; x < engine_val.split(',').length; x++) {
- if ($(this).attr('data-engine-' + engine_val.split(',')[x]) == '1') {
- // available_engines.push(engine_val.split(',')[x]);
- valid++;
- }
- }
- } else
- valid++;
- if (parseFloat(price) < price_val)
- valid++;
- if (valid >= 3)
- selected_arr.push(id);
- });
- $all_items.hide();
- console.log(selected_arr);
- //hide elements and show filtered
- $('.car-filter-active').removeClass('car-filter-active');
- for (var h = 0; h < selected_arr.length; h++) {
- $('.car-filter-item[data-id="' + selected_arr[h] + '"]').show();
- $('.car-filter-item[data-id="' + selected_arr[h] + '"]').addClass('car-filter-active');
- }
- //updating the carousel
- if ($('.car-main') && $(window).width() <= 768) {
- console.log('reinit');
- $('.car-main').trigger('destroy.owl.carousel');
- $('.car-main').html($('.car-main__duplicate').html());
- $('.car-main').find('.car-filter-item').not('.car-filter-active').remove();
- // initMobileCarsCarousel();
- $('.car-main').owlCarousel({
- stagePadding: 80,
- margin: 5,
- items: 1,
- dots: false
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement