const baseUrl = 'https://www.gamadex.com.pl/', uploadPath = 'https://www.gamadex.com.pl/upload/thumbnails/', resourcesPath = 'https://www.gamadex.com.pl/resources/site/'; let main = null, products = null, options = null; function getProducts(){ $.ajax({ method: 'post', url: '', data: {getProductsList: true} }).fail(function(){ $('.product-list').html('

Wystąpił błąd podczas pobierania listy produktów.

'); }).done(function(data){ main = JSON.parse(data); products = main.products; options = { page: 0, filters: {}, autocomplete: null }; if(localStorage.getItem('OptionsPage')) { options.page = parseInt(localStorage.getItem('OptionsPage')); localStorage.removeItem('OptionsPage'); } createProductsList(products, options); }); } function createProductsList(list, options){ let html = '', nav = '', title = '', count = 0, limit = 9, ac = []; for(const x in list){ let display = true; ac.push(list[x]['title']); if(typeof options.filters == 'object'){ for(const y in options.filters){ if(typeof options.filters[y] == 'object'){ switch(options.filters[y].type){ case 'equal': if(options.filters[y].value.indexOf(list[x][y]) == -1) display = false; break; case 'contain': if(list[x][y].toLowerCase().indexOf(options.filters[y].value[0].toLowerCase()) == -1) display = false; break; case 'between': if(Number(options.filters[y].value[0]) !== NaN && list[x][y] < Number(options.filters[y].value[0])) display = false; if(Number(options.filters[y].value[1]) !== NaN && list[x][y] > Number(options.filters[y].value[1])) display = false; break; } } } } //console.log('Options.page = ' + options.page); //console.log('Count = ' + count); //console.log('Limit: ' + limit); //console.log(count >= (options.page * limit) && count < ((options.page + 1) * limit)); //console.log(options.page * limit); //console.log(options.page + 1); //console.log((options.page + 1) * limit); if(display){ if(count >= (options.page * limit) && count < ((options.page + 1) * limit)){ html += `
${list[x]['title']} ${ (list[x]['sold'] == 1) ? '' : '' } ${list[x]['icon']['title']}
${ (list[x]['parent'] != '4') ? `
${(list[x]['drive'] != null) ? list[x]['drive'] : ''}
${(list[x]['tire'] != null) ? list[x]['tire'] : ''}
Rok prod.: ${(list[x]['year'] > 0) ? list[x]['year'] : 'Nie podano'}
Rodzaj masztu: ${(list[x]['mast'] != '') ? list[x]['mast'] : 'Nie podano'}
${list[x]['capacity']} kg
${list[x]['arm']} mm
` : `
Numer własny: ${(list[x]['individual_number'] != '') ? list[x]['individual_number'] : 'Nie podano'}
` }
${ (list[x]['sellable'] == 1 && list[x]['sold'] != 1) ? `
${list[x]['price']} zł
DODAJ DO KOSZYKA
` : `` }
`; } count += 1; } } if(count > 0){ nav += `<`; for(let i = 0; i < (Math.ceil(count/limit)); i++) nav += `${i + 1}`; nav += `>`; } title += `liczba produktów: ${count} ${(count >= 5 || count == 0) ? 'urządzeń' : (count >= 2 && count <=4 ? 'urządzenia' : 'urządzenie')}`; $('.product-list-title').html(title); $('.product-list').html(html); $('.product-list-nav').html(nav); if(options.autocomplete == null){ options.autocomplete = ac; $('#products-search').autocomplete({ source: options.autocomplete }); } $('.product-item-title, .product-item-picture, .product-ask').click(function(e){ //e.preventDefault(); //alert('click'); //$.cookie('OptionsPageTemp', options.page); localStorage.setItem('OptionsPage', options.page); }); } $(document).ready(function(){ getProducts(); $('.products-menu li a').each(function(){ if(location.href.indexOf(this.href) != -1) $(this).parent().addClass('active'); }); $('.product-list-nav').on('click', '.product-nav-item, .product-nav-prev, .product-nav-next', function(e){ e.preventDefault(); const pageTo = $(this).attr('page'); options.page = Number(pageTo); createProductsList(products, options); $(window).scrollTop($('.product-list-box').offset().top); }); $('.filter-option-checkbox').on('click', function(){ let filter = $(this).attr('filter'), value = $(this).attr('value'); if(this.checked){ if(typeof options.filters[filter] == 'object'){ if(options.filters[filter].value.indexOf(value) == -1) options.filters[filter].value.push(value); }else{ options.filters[filter] = { type: 'equal', value: [value] }; } }else{ var index = options.filters[filter].value.indexOf(value); if(index != -1){ options.filters[filter].value.splice(index, 1); if(options.filters[filter].value.length == 0) delete options.filters[filter]; } } options.page = 0; createProductsList(products, options); }); $('.filter-option-input').on('change', function(){ let filter = $(this).attr('filter'), type = $(this).attr('filter-type'), value = $(this).val(); if((type == 'from' || type == 'to') && value == '') value = NaN; if(typeof options.filters[filter] == 'object'){ if(type == 'from') options.filters[filter].value[0] = Number(value); else if(type == 'to') options.filters[filter].value[1] = Number(value); else if(type == 'contain') options.filters[filter].value = [value.toLowerCase()]; if((type == 'from' || type == 'to') && (isNaN(options.filters[filter].value[0]) && isNaN(options.filters[filter].value[1]))) delete options.filters[filter]; else if(type == 'contain' && options.filters[filter].value[0] == '') delete options.filters[filter]; }else{ if((type == 'from' || type == 'to') && !isNaN(value)){ options.filters[filter] = { type: 'between', value: [NaN, NaN] }; if(type == 'from') options.filters[filter].value[0] = Number(value); else if(type == 'to') options.filters[filter].value[1] = Number(value); }else if(type == 'contain'){ options.filters[filter] = { type: 'contain', value: [value] }; } } options.page = 0; createProductsList(products, options); }); });