$(function () { const searchParams = new URLSearchParams(window.location.search); var razdelId = opts.razdelId const availableSortParams = ["popular", "price_asc", "price_desc", "cnt_sell_desc"]; var sort = "popular"; if (searchParams.has("sort") && availableSortParams.includes(searchParams.get("sort"))) { sort = searchParams.get("sort"); } var currentPage = 1 var rowsCount = opts.rowsCount var currency = opts.currency var minPrice = null var maxPrice = null if (searchParams.has("currency") && searchParams.get("currency") == currency) { minPrice = searchParams.get("priceFrom"); maxPrice = searchParams.get("priceTo"); } if (searchParams.size > 0) { UpdateUrlParams(); } var minPriceBorder = 0 var maxPriceBorder = 1000000 var totalCount = 0 var scrollLoad = false; function UpdateUrlParams() { if (sort != "popular") { searchParams.set("sort", sort); } else { searchParams.delete("sort"); } if (minPrice != null && minPrice != minPriceBorder || maxPrice != null && maxPrice != maxPriceBorder) { searchParams.set("currency", currency); if (minPrice != null && minPrice != minPriceBorder) { searchParams.set("priceFrom", minPrice); } else { searchParams.delete("priceFrom"); } if (maxPrice != null && maxPrice != maxPriceBorder) { searchParams.set("priceTo", maxPrice); } else { searchParams.delete("priceTo"); } } else { searchParams.delete("currency"); searchParams.delete("priceFrom"); searchParams.delete("priceTo"); } let pathParts = window.location.pathname.split('/').filter(x => x != ""); if (pathParts.length > 2 && pathParts[2] != razdelId) { searchParams.set("sub_r", razdelId);; } else { searchParams.delete("sub_r"); } let url = window.location.pathname + "?"+searchParams.toString(); let newState = {} for (let [key, value] of searchParams.entries()) { newState[key] = value; } window.history.replaceState(newState, '', url); } function LoadProducts(idr, sort, page, rows, curr, minPrice, maxPrice) { let request = { "id_r": idr, "sort": sort, "page": page, "rows": rows, "curr": curr, "lang": opts.lang, "minprice": minPrice, "maxprice": maxPrice, "rnd": Math.random(), } showLoading(page == 1 && $('ul#itemsList li:not([name="loader"])').length > 0); return $.get("/asp/block_goods_r.asp", request) .done(function (response) { if (page == 1) { $('ul#itemsList li:not([name="loader"])').remove(); } hideLoading(); if (response.length > 0) { showResultOnPage(response); UpdateFiltersPosition("#filters") } else { showEmptyResult(); } }) .fail(function () { alert(opts.i18n.something_went_wrong) hideLoading(); }); } function LoadItemsStats(idr, curr, priceFrom = null, priceTo = null) { let request = { "id_r": idr, "curr": curr, "minPrice": priceFrom, "maxPrice": priceTo, "GetStats": 1, "rnd": Math.random(), } return $.get("/asp/block_goods_r.asp", request) .done(function (response) { parsedResult = JSON.parse(response); if (priceFrom == null) { minPriceBorder = Math.floor(parsedResult.MinPrice || 0) } if (priceTo == null) { maxPriceBorder = Math.ceil(parsedResult.MaxPrice || 1000000) } totalCount = parsedResult.TotalCount }) .fail(function () { alert(opts.i18n.something_went_wrong) }); } function showResultOnPage(data) { $('ul#itemsList [name="loader"]').first().before(data); } function showEmptyResult() { $("#items-not-found").removeClass("d-none"); } function showLoading(itSorting = false) { scrollLoad = true; if (itSorting) { $("#itemsList").addClass("disabled"); } else { $("[name='loader']").removeClass("d-none") } $("#products-not-found").addClass('d-none') } function hideLoading() { scrollLoad = false; if (currentPage * rowsCount >= totalCount) { $("[name='loader']").addClass("d-none") } $("#itemsList").removeClass("disabled"); } function createLoader() { let itemsListSelector = 'ul#itemsList'; $(itemsListSelector).find("[name='loader']").remove(); let currentViewType = $.cookie('items_list_view') || "grid"; if(currentViewType == "grid"){ CreateSkeletonLoader(itemsListSelector); } else { CreateSkeletonLoaderListViewType(itemsListSelector); } } $("input[data-razdel-id]").on("change", function (ev) { razdelId = $(ev.currentTarget).attr("data-razdel-id"); $('input[data-razdel-id]').not(ev.currentTarget).removeAttr('checked'); $(ev.currentTarget).attr("checked", "checked"); minPrice = null maxPrice = null applyFilters(true, true) }) $("li[sort-by]").on("click", function (ev) { $("#selected_sorting").text($(ev.currentTarget).find("span").text()) sort = $(ev.currentTarget).attr("sort-by"); applyFilters(false, false); $("li[sort-by]").removeClass("custom-dropdown-item--active"); $(ev.currentTarget).addClass("custom-dropdown-item--active") }) function applyFilters(loadNewStats = true, rebuildPriceFilter = false) { currentPage = 1; UpdateUrlParams() scrollToShow("#selected_sorting") if (loadNewStats) { LoadItemsStats(razdelId, currency, minPrice, maxPrice) .then(function () { $("#total_count span").text(totalCount); if (rebuildPriceFilter) { var priceSlider = document.getElementById('price-slider'); priceSlider.noUiSlider.updateOptions({ start: [minPrice > 0 ? minPrice : minPriceBorder, maxPrice > 0 ? maxPrice : maxPriceBorder], range: { 'min': minPriceBorder, 'max': maxPriceBorder } }, true); } }); } return LoadProducts(razdelId, sort, currentPage, rowsCount, currency, minPrice, maxPrice); } $(document).ready(function () { createLoader(); if (searchParams.has("currency")) { LoadItemsStats(razdelId, currency, null, null) .then(function () { InitPriceFilter() UpdateFiltersPosition("#filters") }); } else { LoadProducts(razdelId, sort, currentPage, rowsCount, currency, minPrice, maxPrice); LoadItemsStats(razdelId, currency, minPrice, maxPrice) .then(function () { $("#total_count span").text(totalCount) InitPriceFilter() UpdateFiltersPosition("#filters") }); } $(window).on("scroll resize", () => UpdateFiltersPosition("#filters")); }) $(window).scroll(function () { if (currentPage * rowsCount < totalCount) { if (!scrollLoad && $(window).scrollTop() >= $(document).height() - $(window).height() - 2050) { currentPage += 1; LoadProducts(razdelId, sort, currentPage, rowsCount, currency, minPrice, maxPrice) } } }); var delayInputPriceTimer; function applyPriceFilter() { clearTimeout(delayInputPriceTimer); delayInputPriceTimer = setTimeout(function () { if (minPrice == null) { minPrice = $('#price-start-value-input').val(); maxPrice = $('#price-end-value-input').val(); } else { minPrice = $('#price-start-value-input').val(); maxPrice = $('#price-end-value-input').val(); applyFilters(); } }, 400); } function InitPriceFilter() { minPriceBorder = Math.floor(minPriceBorder || 0); maxPriceBorder = Math.ceil(maxPriceBorder || 1000000); var priceSlider = document.getElementById('price-slider'); var startValueInput = document.getElementById('price-start-value-input'); var endValueInput = document.getElementById('price-end-value-input'); var inputs = [startValueInput, endValueInput]; noUiSlider.create(priceSlider, { start: [minPrice || minPriceBorder, maxPrice || maxPriceBorder], step: 1, connect: true, range: { 'min': minPriceBorder, 'max': maxPriceBorder }, format: { to: (v) => parseFloat(v).toFixed(0), from: (v) => parseFloat(v).toFixed(0) } }); priceSlider.noUiSlider.on('update', function (values, handle) { inputs[handle].value = values[handle]; applyPriceFilter(); }); inputs.forEach(function (input, handle) { input.addEventListener('change', function () { priceSlider.noUiSlider.setHandle(handle, this.value); applyPriceFilter(); }); input.addEventListener('keydown', function (e) { var values = priceSlider.noUiSlider.get(); var value = Number(values[handle]); var steps = priceSlider.noUiSlider.steps(); // [down, up] var step = steps[handle]; var position; // 13 is enter, // 38 is key up, // 40 is key down. switch (e.which) { case 13: priceSlider.noUiSlider.setHandle(handle, this.value); applyPriceFilter(); break; case 38: // Get step to go increase slider value (up) position = step[1]; // false = no step is set if (position === false) { position = 1; } // null = edge of slider if (position !== null) { priceSlider.noUiSlider.setHandle(handle, value + position); } break; case 40: position = step[0]; if (position === false) { position = 1; } if (position !== null) { priceSlider.noUiSlider.setHandle(handle, value - position); } break; } }); }); } /* $("#itemsList").on("click", gameCardClick); function gameCardClick(ev) { let action = $(ev.target).closest('[action]'); if (action.length == 0) return; switch (action.attr("action")) { case 'toggleInFavorites': ev.preventDefault() toggleInFavorites(ev); break; } } */ $("#items-list-view [aria-controls]").on("click", (ev)=>{ SwitchItemsListView(ev) applyFilters(false, false); createLoader(); $('ul#itemsList').toggleClass("vertical-list", $.cookie('items_list_view') == "table") }) $(window).on("resize", ItemsListViewResize) ItemsListViewResize(); }); $(function() { new Swiper("#best-offer-slider", { slidesPerView: 1.1, spaceBetween: 8, breakpoints: { 420: { slidesPerView: "auto", spaceBetween: 16, }, 500: { slidesPerView: "auto", }, 992: { allowTouchMove: false, disableOnInteraction: false } }, on: { init: function () { $('#best-offer-slider').removeClass('yet'); } } }); });