/** * OrangePix custom js */ // variabile per impedire trigger indesiderati di funzioni al caricamento della pagina let avvia = false; $(document).ready(function () { // quando si preme l'ordina carrello tramite sweet alert si richiede conferma dell'ordine all'utente if ( $('.js-form-conferma-ordine').length ) { $('.js-form-conferma-ordine').on('submit', function (e) { e.preventDefault(); let title= "Confermi l'ordine?"; let cancelButtonText = "Annulla"; let confirmButtonText = "Conferma ordine"; if ($('input[name=hdnLingua]').val() == 'EN') { title = "Confirm the order?"; cancelButtonText = "Cancel"; confirmButtonText = "Confirm order"; } swal.fire({ title: title, type: "warning", showCancelButton: true, cancelButtonText: cancelButtonText, confirmButtonText: confirmButtonText, }).then((result) => { // intercetto il valore della risposta, se true scateno la creazione ordine ed invio email if (result.value) { eseguiOrdineCarrello(); } }); }); } window.init_ajax_select2(); $('.js-send-contact').on('click',function (e) { e.preventDefault(); let id = $(this).data('id'); $.ajax({ type: "post", url: "/assets/ajax/invia_trattativa.php", data: {'id':id}, dataType: "json", success: function (data) { swal.fire({ title: data['titolo'], text: data['messaggio'], type: data['stato'], confirmButttom: 'OK' }).then(() => { // window.location = '/'; // rimando in home l'utente dopo l'ok }); }, error: function () { swal.fire({ title: 'Errore', text: 'Si è presentato un errore durante l\'elaborazione della richiesta', type: 'error', confirmButttom: 'OK' }); } }); }); if($('.login-form').length >0){ $('.login-form').parsley(); } if($('.js-utente').length >0) { $('.js-utente').parsley(); } $(document).on('click','.close',function(){ $(this).closest('.alert-danger').remove(); }); $(document).on('select2:select','.js-data-ajax, .js-select2',function(){ $(this).closest('div').find('ul.parsley-errors-list').remove(); }); if($('.js-contact').length >0) { window.Parsley.on('form:error', function() { alert('COMPLETA TUTTI I CAMPI'); }); } $(document).on('click','.to_self',selfautocompilator); $(".sidebar-link").click(function () { $(".sidebar-link").removeClass("active"); $(this).addClass("active"); }); //impedisce la navigazione in caso un link abbia questa classe $('a').click(function (e) { if ($(this).hasClass('disabled')) { window.stop(); e.preventDefault(); } }); // controlla che il pulsante dei filtri esista nel caso gli fa il bind if ($('.js-reset-filtri').length > 0) { $('.js-reset-filtri').click(function () { resetFiltri(); }); } $('.js-animation').hide(); //nascondo loader nella ricerca per default // binding per la ricerca globale $('.js-ricerca-globale').click(function () { $('#js-risultati-ricerca-globale').html(''); $('#js-ricerca-globale').val(''); $('#js-modale-ricerca-globale').modal('toggle'); setTimeout(()=>{ $('#js-ricerca-globale').focus()}, 500); $('#js-ricerca-globale').keyup(function() { eseguiRicercaGlobale($(this).val()); }); }); // controllo di essere sulla pagina ricerca auto con box dei filtri // if ($('.js-container-filtri').length > 0) { eseguiBindPerRicercaDinamica(); // } //controllo popolamento post ricerca if (parseInt($('.js-select-marche').val()) > 0 && parseInt($('.js-select-modello').val()) == 0) { popolaSelectModelliDaSelectMarche(); popolaSelectGruppiStoriciDaSelectMarche(); } if (parseInt($('.js-select-marche').val()) > 0 && parseInt($('.js-select-modello').val()) > 0) { popolaSelectAllestimentiDaSelectModelli(); } //invio email richiesta servizio $('.js-richiesta-servizio').click(() => { debugger $.ajax({ type: "post", url: "https://www.consorziounica.com/ws/consorziati/ajax/invia_email_richiesta_servizio", data: { servizio: $('[name=js-tipo-servizio]').val() }, dataType: "json", success: function (data) { swal.fire({ title: data['titolo'], text: data['messaggio'], type: data['stato'], confirmButttom: 'OK' }).then(() => { window.location = '/'; // rimando in home l'utente dopo l'ok }); }, error: function () { swal.fire({ title: 'Errore', text: 'Si è presentato un errore durante l\'elaborazione della richiesta', type: 'error', confirmButttom: 'OK' }); } }); }); $("#pulsante-password").click(function () { $("#pulsante-password").addClass("hide"); $("#password-ricambi").removeClass("hide"); }); eseguiRedirectSuLoginUtenteSeNecessario(); // inizializza il plugin select2 initSelect2(); // binding per il popolamento della select allestimenti $('.js-cerca-automobile').click( function () { componiUrlRicercaAuto(); }); if($('.js-export-annunci').length > 0){ $('.js-export-annunci').click(function () { window.location = '/export-lista-auto'; }); } $('select.js-select-ordinamento').change(function () { eseguiRicercaDinamica(); }); bindPulsantiAnnuncioIndice(); // binding per abilitare il filtro wishlist $('.js-whishlist-filter').on('click', function () { controllaStatoFiltroWishlist($(this)); }); // binding sulla scritta testuale che scatena il click del filtro wishlist $('.js-wishlist-filter-trigger').on('click', function () { $('.js-whishlist-filter').click(); }); // mostra password per le aste $(".sidebar-link").click(function () { $(".sidebar-link").removeClass("active"); $(this).addClass("active"); }); $("#pulsante-password").click(function () { $("#pulsante-password").addClass("hide"); $("#password-ricambi").removeClass("hide"); }); // scrolling smooth per i servizi if ($('a.js-accompagna').length > 0) { $('a.js-accompagna').on('click', function () { if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); if (target.length) { $('html, body').animate({ scrollTop: target.offset().top - 150 }, 1000); return false; } } }); } // binding sul tasto nelle ricerche che redireziona al login if ($('.js-goto-login').length > 0) { $('.js-goto-login').on('click', function () { window.location = "/login" }); } // bindig pulsante submit per salvare la ricerca degli annunci if ($('#js-salva-ricerca').length > 0) { $('#js-salva-ricerca').on('click', function () { let nome_ricerca = $('#nome-ricerca').val(); let url_research = location.href; //pesco la url attuale if ($('[name=js-url-salvataggio]').length > 0) { url_research = $('[name=js-url-salvataggio]').val(); } $.ajax({ url: '/ws/annunci/ajax/salva_cancella_ricerca_annunci', type: 'POST', dataType: 'json', data: { nome: nome_ricerca, //qui va il nome della ricerca, il resto ci pensa la func ajax operazione: 'salva', url_da_salvare: url_research, filtri: getDatiFiltri() }, success: function (data) { if (data[0]['status'] == 'success') { Swal.fire({ text: data[0]['message'], type: data[0]['status'], confirmButtonText: 'OK' }).then(() => { location.reload(); }); } }, error: function () { Swal.fire({ title: 'Errore', text: 'Si è presentato un errore durante l\'elaborazione della richiesta', type: 'error', confirmButtonText: 'OK' }); } }); $('#salva-ricerca-modale').modal('toggle'); //dismiss del modale }); } // binding submit recupero password // $('.js-recupera').click(function (e) { // e.preventDefault(); // if ($('[name=email]').val() !== null && $('[name=email]').val() !== '') { // $.ajax({ // url: "/ws/consorziati/ajax/reset_password", // type: 'POST', // dataType: 'json', // data: { // email: $('[name=email]').val(), // recaptcha: $('[name=g-recaptcha-response]').val() // }, // success: function (data) { // if (data['risultato']) { // swal.fire({ // title: data['titolo'], // text: data['messaggio'], // type: data['stato'], // confirmButton: 'OK' // }).then(() => { // window.location = '/'; // }); // } else { // swal.fire({ // title: data['titolo'], // text: data['messaggio'], // type: data['stato'], // confirmButton: 'OK' // }).then(() => { // // }) // } // }, // error: function () { // swal.fire({ // title: 'Errore', // text: 'qualcosa è andato storto', // type: 'error', // confirmButttom: 'OK' // }) // } // }); // } // }); if($('#js-spedisci-mail-privato').length > 0){ $selector = $('.js-contact-privato'); $selector.parsley(); $selector.on('submit',function(e){ e.preventDefault(); inviaContattoPrivato(); }); } if(window.location.pathname == '/annunci' && window.location.search.length > 0) { sessionStorage.setItem('search', window.location.href); // aggiorno la eventuale ricerca o la setto } avvia = true; // NON MODIFICARE QUESTA VARIABILE, vedere a cima file descrizione }); $('a.js-accompagna').on('click', function () { if (location.pathname.replace(/^\//, '') === this.pathname.replace(/^\//, '') && location.hostname === this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); if (target.length) { $('html, body').animate({ scrollTop: target.offset().top - 150 }, 1000); return false; } } }); function eseguiOrdineCarrello() { if ($('.js-conferma-ordine-carrello').length > 0) { $.ajax({ url: "/ws/prodotti/ajax/esegui-ordine-carrello", type: "POST", async: false, dataType: 'json', data: { tipo_prodotto: $('input[name=hdnTipoProdotto]').val(), datas:$(".js-form-conferma-ordine").serialize() }, success: function (data) { if (data.stato !== undefined && data.stato === true) { ottieniCarrello(); } } }); } } function sanitizeURI(uri) { let res = ''; res = uri.replace(/\s/g, "-"); res = res.toLowerCase(); return res; } /** * Init jquery select2 plugin for select having js-select2 class */ function initSelect2() { let elements = $('.js-select2'); let element = false; let i = 0; for (i = 0; i <= elements.length; i++) { element = $(elements[i]); $(element).select2(); } } /** * Popola la select dei gruppi storici filtrati a seconda del valore della select marca * (nella pagina devono essere presenti sia la select di marche che del gruppo storico) */ function popolaSelectGruppiStoriciDaSelectMarche() { let select_marche_element = $('.js-select-marche'); let select_gruppo_storico_element = $('.js-select-gruppo-storico'); let id_marca = 0; let codice_gruppo_storico = parseInt($('[name=gruppo-storico]').val()); if (select_marche_element.length > 0 && select_gruppo_storico_element.length > 0) { id_marca = $(select_marche_element).val(); // reset della select gruppo storico $(select_gruppo_storico_element).html(''); $(select_gruppo_storico_element).select2().trigger('change'); $.ajax({ url: "/ws/automobili/ajax/ottieni_gruppi_storici", type: "POST", async: false, dataType: 'json', data: { id_marca: id_marca, }, success: function (data) { let i = 0; let options = ''; for (i = 0; i < data.length; i++) { options += ''; } // popolamento della select modelli $(select_gruppo_storico_element).html(options); $(select_gruppo_storico_element).select2().trigger('change'); controllaAbilitazioneInputRicercaTestualeModello(); } }); } } /** * Popola la select dei modelli con i modelli filtrati a seconda del valore della select marca * (nella pagina devono essere presenti sia la select di marche che modelli) */ function popolaSelectModelliDaSelectMarche() { let select_marche_element = $('.js-select-marche'); let select_modelli_element = $('.js-select-modello'); let id_marca = 0; let id_modello = parseInt($('[name=modello]').val()); if (select_marche_element.length > 0 && select_modelli_element.length > 0) { id_marca = $(select_marche_element).val(); // reset della select modelli $(select_modelli_element).html(''); $(select_modelli_element).select2().trigger('change'); $.ajax({ url: "/ws/automobili/ajax/ottieni_modelli", type: "POST", async: false, dataType: 'json', data: { id_marca: id_marca, }, success: function (data) { let i = 0; let options = ''; for (i = 0; i < data.length; i++) { options += ''; } // popolamento della select modelli $(select_modelli_element).html(options); $(select_modelli_element).select2().trigger('change'); controllaAbilitazioneInputRicercaTestualeModello(); } }); } } /** * Popola la select degli allestimenti con gli allestimenti filtrati a seconda del valore della select modello * (nella pagina devono essere presenti sia la select di marche, modelli e allestimenti) */ function popolaSelectAllestimentiDaSelectModelli() { let select_marche_element = $('.js-select-marche'); let select_modelli_element = $('.js-select-modello'); let select_allestimenti_element = $('.js-select-allestimento'); let id_marca = 0; let id_allestimento = parseInt($('[name=allestimento]').val()); if (select_marche_element.length > 0 && select_modelli_element.length > 0 && select_allestimenti_element.length > 0) { id_marca = $(select_marche_element).val(); id_modello = $(select_modelli_element).val(); // reset della select allestimenti $(select_allestimenti_element).html(''); $(select_allestimenti_element).select2().trigger('change'); if (id_modello > 0) { $.ajax({ url: "/ws/automobili/ajax/ottieni_allestimenti", type: "POST", async: false, dataType: 'json', data: { id_marca: id_marca, id_modello: id_modello }, success: function (data) { let i = 0; let options = ''; for (i = 0; i < data.length; i++) { options += ''; } // popolamento della select allestimenti $(select_allestimenti_element).html(options); $(select_allestimenti_element).select2().trigger('change'); } }); } } } /** * Si occupa di controllare se la casella input di ricerca testuale * va disabilitato od abilitata a seconda che il modello della select sia compilata * * questa casella serve per fare una ricerca testuale dei modelli e quindi deve essere * abilitata quando non e' stato scelto un modello dalla select, * ma deve essere disabilitata quando questo ha un valore */ function controllaAbilitazioneInputRicercaTestualeModello() { // questo controllo impedisce che il form delle spese di trasporto si rompa sulla scelta di marca e modello if ($('.js-ricerca-testuale-marca-modello').length > 0) { let dati_filtri = getDatiFiltri(); if (dati_filtri.marca == "0" || dati_filtri.modello == "0") { $('.js-ricerca-testuale-marca-modello').prop('disabled', false); } if (parseInt(dati_filtri.modello) != "0") { $('.js-ricerca-testuale-marca-modello').val(''); $('.js-ricerca-testuale-marca-modello').prop('disabled', true); } } } /** * ritorna un oggetto contenente i valori dei filtri */ function getDatiFiltri() { let div_contenitore_filtri = $('.js-container-filtri'); if (div_contenitore_filtri.length > 0) { let filtri = {}; filtri.url_sito = $(div_contenitore_filtri).find('.js-url_sito').val(); filtri.vista = $(div_contenitore_filtri).find('.js-tipo_vista').val(); filtri.ordinamento = "nuove"; // lo prevalorizzo per la provenienza da homepage if (typeof $('select.js-select-ordinamento').val() !== "undefined") { filtri.ordinamento = $('select.js-select-ordinamento').val(); } filtri.marca = 0; if ($(div_contenitore_filtri).find('.js-select-marche').length > 0) { filtri.marca = $(div_contenitore_filtri).find('.js-select-marche').val(); } filtri.nome_marca = ''; if ($(div_contenitore_filtri).find('.js-select-marche').length > 0) { filtri.nome_marca = $(div_contenitore_filtri).find('.js-select-marche option:selected').text(); } filtri.modello = 0; if ($(div_contenitore_filtri).find('.js-select-gruppo-storico').length > 0) { filtri.modello = $(div_contenitore_filtri).find('.js-select-gruppo-storico').val(); } /* vecchio modello ora rimpiazzato da gruppo storico filtri.modello = 0; if ($(div_contenitore_filtri).find('.js-select-modello').length > 0) { filtri.modello = $(div_contenitore_filtri).find('.js-select-modello').val(); } */ filtri.nome_modello = ''; if ($(div_contenitore_filtri).find('.js-select-modello').length > 0) { filtri.nome_modello = $(div_contenitore_filtri).find('.js-select-modello option:selected').text(); } filtri.ricerca_testuale_marca_modello = ''; if ($(div_contenitore_filtri).find('.js-ricerca-testuale-marca-modello').length > 0) { filtri.ricerca_testuale_marca_modello = $(div_contenitore_filtri).find('.js-ricerca-testuale-marca-modello').val(); filtri.ricerca_testuale_marca_modello = filtri.ricerca_testuale_marca_modello.split('-').join('_'); // Visto che il carattere "-" separa i filtri negli url, lo sostituisco con underscore e // nel recupero dei dati prima di far la query di ricerca lo riassegno } filtri.allestimento = 0; if ($(div_contenitore_filtri).find('.js-select-allestimento').length > 0) { filtri.allestimento = $(div_contenitore_filtri).find('.js-select-allestimento').val(); } filtri.nome_allestimento = ''; if ($(div_contenitore_filtri).find('.js-ricerca-testuale-allestimento').length > 0) { filtri.nome_allestimento = $(div_contenitore_filtri).find('.js-ricerca-testuale-allestimento').val(); } filtri.nazione = 0; if ($(div_contenitore_filtri).find('.js-select-nazione').length > 0) { filtri.nazione = $(div_contenitore_filtri).find('.js-select-nazione').val(); } filtri.alimentazione = 0; if ($(div_contenitore_filtri).find('.js-select-alimentazione').length > 0) { filtri.alimentazione = $(div_contenitore_filtri).find('.js-select-alimentazione').val(); } filtri.nome_alimentazione = ''; if ($(div_contenitore_filtri).find('.js-select-alimentazione').length > 0) { filtri.nome_alimentazione = $(div_contenitore_filtri).find('.js-select-alimentazione option:selected').text(); } filtri.trazione = 0; if ($(div_contenitore_filtri).find('.js-select-trazioni').length > 0) { filtri.trazione = $(div_contenitore_filtri).find('.js-select-trazioni').val(); } filtri.cambio = 0; if ($(div_contenitore_filtri).find('.js-select-cambio').length > 0) { filtri.cambio = $(div_contenitore_filtri).find('.js-select-cambio').val(); } filtri.carrozzeria = 0; if ($(div_contenitore_filtri).find('.js-select-carrozzeria').length > 0) { filtri.carrozzeria = $(div_contenitore_filtri).find('.js-select-carrozzeria').val(); } filtri.porte = 0; if ($(div_contenitore_filtri).find('.js-select-porte').length > 0) { filtri.porte = $(div_contenitore_filtri).find('.js-select-porte').val(); } filtri.colore_esterno = 0; if ($(div_contenitore_filtri).find('.js-select-colore-esterno').length > 0) { filtri.colore_esterno = $(div_contenitore_filtri).find('.js-select-colore-esterno').val(); } filtri.nome_colore_esterno = ''; if ($(div_contenitore_filtri).find('.js-select-colore-esterno').length > 0) { filtri.nome_colore_esterno = $(div_contenitore_filtri).find('.js-select-colore-esternoalimentazione option:selected').text(); } filtri.nuovo = 0; if ($('input#js-nuovo').is(':checked')) { filtri.nuovo = 1; } filtri.usato = 0; if ($('input#js-usato').is(':checked')) { filtri.usato = 1; } filtri.km0 = 0; if ($('input#js-km0').is(':checked')) { filtri.km0 = 1; } filtri.incidentato = 0; if ($('input#js-incidentato').is(':checked')) { filtri.incidentato = 1; } filtri.emissione = 0; if ($(div_contenitore_filtri).find('.js-select-emissione').length > 0) { filtri.emissione = $(div_contenitore_filtri).find('.js-select-emissione').val(); } filtri.anno_minimo = 1980; if ($(div_contenitore_filtri).find('.js-range-anno .current-min').length > 0) { filtri.anno_minimo = $(div_contenitore_filtri).find('.js-range-anno .current-min').val(); } filtri.anno_massimo = 2020; if ($(div_contenitore_filtri).find('.js-range-anno .current-max').length > 0) { filtri.anno_massimo = $(div_contenitore_filtri).find('.js-range-anno .current-max').val(); } filtri.km_minimo = 0; if ($(div_contenitore_filtri).find('.js-range-km .current-min').length > 0) { filtri.km_minimo = $(div_contenitore_filtri).find('.js-range-km .current-min').val(); filtri.km_minimo = filtri.km_minimo.toString().replace(/[^\d]/g, ''); filtri.km_minimo = parseFloat(filtri.km_minimo); } filtri.km_massimo = 400000; if ($(div_contenitore_filtri).find('.js-range-km .current-max').length > 0) { filtri.km_massimo = $(div_contenitore_filtri).find('.js-range-km .current-max').val(); filtri.km_massimo = filtri.km_massimo.toString().replace(/[^\d]/g, ''); filtri.km_massimo = parseFloat(filtri.km_massimo); } filtri.prezzo_minimo = 0; if ($(div_contenitore_filtri).find('.js-range-prezzo .current-min').length > 0) { filtri.prezzo_minimo = $(div_contenitore_filtri).find('.js-range-prezzo .current-min').val(); filtri.prezzo_minimo = filtri.prezzo_minimo.toString().replace(/[^\d]/g, ''); filtri.prezzo_minimo = parseFloat(filtri.prezzo_minimo); } filtri.prezzo_massimo = 200000; if ($(div_contenitore_filtri).find('.js-range-prezzo .current-max').length > 0) { filtri.prezzo_massimo = $(div_contenitore_filtri).find('.js-range-prezzo .current-max').val(); filtri.prezzo_massimo = filtri.prezzo_massimo.toString().replace(/[^\d]/g, ''); filtri.prezzo_massimo = parseFloat(filtri.prezzo_massimo); } filtri.flag_wishlist = 0; if ($('.js-whishlist-filter').length > 0) { filtri.flag_wishlist = parseInt($('.js-whishlist-filter input').val()); } filtri.tag = 0; if ($('.js-tag').length > 0) { filtri.tag = $(div_contenitore_filtri).find('.js-tag').val(); } return filtri; } } /** * compone l'url con tutti i valori dei filtri ed esegue il redirect * al routing che si occupa di servire la pagina di ricerca * * @param {object} filtri contiene tutti i valori dei filtri e della vista * @param {bool} redirect true[default]: esegue il redirect, false: esegue il return dell'url composta * * @returns {string} url stringa composita di stringa di ricerca codificata in base64 e opzioni vista */ function componiUrlRicercaAuto(filtri = null, redirect = true) { if (filtri == null) { filtri = getDatiFiltri(); } let url = ''; let hash = ''; url += typeof filtri.url_sito == 'undefined' ? '' : filtri.url_sito; hash += filtri.marca; hash += '-' + filtri.modello; hash += '-' + filtri.nome_allestimento; hash += '-' + filtri.nazione; hash += '-' + filtri.alimentazione; hash += '-' + filtri.trazione; hash += '-' + filtri.cambio; hash += '-' + filtri.carrozzeria; if (filtri.porte !== 0) { hash += '-' + filtri.porte.replace('/', ''); } else { hash += '-' + 0; } hash += '-' + filtri.colore_esterno; //adeguamento nuovi filtri hash += '-' + filtri.nuovo; hash += '-' + filtri.usato; hash += '-' + filtri.km0; hash += '-' + filtri.incidentato; if (filtri.emissione !== 0) { hash += '-' + filtri.emissione.replace('-', ''); } else { hash += '-' + 0; } hash += '-' + filtri.anno_minimo; hash += '-' + filtri.anno_massimo; hash += '-' + filtri.km_minimo; hash += '-' + filtri.km_massimo; hash += '-' + filtri.prezzo_minimo; hash += '-' + filtri.prezzo_massimo; hash += '-' + filtri.flag_wishlist; hash += '-' + filtri.tag; hash += '-' + filtri.ricerca_testuale_marca_modello; url = sanitizeURI(url); url += '?tipo=' + 'annunci_indice'; url += '&vista=' + filtri.vista; url += '&sort=' + filtri.ordinamento; var new_hash = window.btoa(hash); url += '&filters=' + new_hash; if (redirect) { window.location.href = url; } else { return url; } } /** * Aggiunge un annuncio alla lista di comparazione tramite chiamata ajax * @param element l'elemento bottone di confronto premuto */ function comparaAnnuncio(element) { let id = $(element).data('id'); if (id > 0) { $.ajax({ url: "/ws/annunci/ajax/aggiungi_confronto_annuncio", type: "POST", async: false, dataType: 'json', data: { id_annuncio: id }, success: function (data) { if (data.stato === 'OK') { // controllo di non essere in prima pagina di una ricerca dinamica, nel caso controllo di avere l'url salvata nell'html $('[name=js-url-salvataggio]').length > 0 ? window.location.href = $('[name=js-url-salvataggio]').val() : location.reload(); } else { Swal.fire({ title: "Errore", text: data.messaggio, type: 'error', confirmButtonText: 'OK' }); } } }); } else { alert('Errore ID univoco automobile non valorizzata!'); } } /** * Rimuove un annuncio dalla lista di comparazione tramite chiamata ajax * @param element l'elemento bottone di rimozione confronto premuto */ function rimuoviComparaAnnuncio(element) { let id = $(element).data('id'); if (id > 0) { $.ajax({ url: "/ws/annunci/ajax/rimuovi_confronto_annuncio", type: "POST", async: false, dataType: 'json', data: { id_annuncio: id }, success: function (data) { if (data.stato === 'OK') { $(element).closest('.js-elemento-annuncio-confronto').remove(); location.reload(); } else { Swal.fire({ title: "Errore", text: data.messaggio, type: 'error', confirmButtonText: 'OK' }); } } }); } } /** * Funzione per manipolare la wishlist * * @param element elemento che andra' nella wishlist */ function aggiungiRimuoviAnnuncioWishlist(element) { let wishlist_status = $(element).data('wishlist'); let id_annuncio = $(element).data('id_annuncio'); $.ajax({ url: "/ws/annunci/ajax/aggiungi_rimuovi_annuncio_wishlist", type: "POST", async: false, dataType: 'json', data: { wishlist_status: wishlist_status, id_annuncio: id_annuncio }, success: function (data) { if (data.stato === false) { swal.fire({ type: 'error', text: data.messaggio, buttons: { confirm: true } }).then(() => { location.reload(); }); } else { wishlist_status = parseInt(data.dati.stato_wishlist); $(element).find('i').removeClass('fa-star-o'); $(element).find('i').removeClass('fa-star'); if (wishlist_status == 1) { $(element).find('i').addClass('fa-star-o'); $(element).data('wishlist', '0'); } else { $(element).find('i').addClass('fa-star'); $(element).data('wishlist', '1'); } } }, error: function () { swal.fire({ type: 'error', text: 'Si è verificato un problema, contattare l\'amministrazione', buttons: { confirm: true } }).then(() => { location.reload(); }); } }); } /** * Alterna il valore del filtro di visualizzazione preferiti * * @param element */ function controllaStatoFiltroWishlist(element) { let current_value = parseInt($(element).find('input').val()); $(element).removeClass('fa-star'); $(element).removeClass('fa-star-o'); if (current_value === 1) { $(element).find('input').val(0); $(element).addClass('fa-star-o'); } else { $(element).find('input').val(1); $(element).addClass('fa-star'); } componiUrlRicercaAuto(); } /** * * Mostra il popup di conferma opzionamento e se confermato * esegue la chiamata ajax per eseguire l'opzionamento * * @param dom_element element */ function opzionaAnnuncio(element) { // setto in una variabile il valore dell'id dell'annuncio da opzionare id_annuncio = $(element).data("id"); swal.fire({ title: "Confermi l'acquisto?", type: "warning", showCancelButton: true, cancelButtonText: "Annulla", confirmButtonText: "Conferma acquisto", }).then((result) => { //intercetto il valore della risposta, se true scateno l'opzionamento if (result.value) { $.ajax({ url: "/ws/annunci/ajax/opziona_annuncio", type: "POST", async: false, dataType: "json", data: { 'id': id_annuncio }, success: function (dati) { swal.fire({ type: dati.risultato ? 'success' : 'error', text: dati.messaggio, buttons: { confirm: true } }).then(() => { location.reload(); }); }, error: function (jqxhr, status, exception) { swal.fire({ type: "error", text: "Si è presentato un errore durante l'elaborazione della richiesta!" }) } }); } }); } /** * Questa funzione serve per pagine dove bisogna controllare se il login dell'utente e obbligatorio per consultarne il contenuto * controlla se presente un utente loggato tramite un campo hidden che ha il valore 1 quando l'utente e' loggato * controlla se presente un campo hidden nella pagina che determina se bisogna effettuare il controllo del login * di conseguenza se la pagina necessita del controllo, e l'utente non e' loggato riporta alla pagina di redirect */ function eseguiRedirectSuLoginUtenteSeNecessario() { let utente_loggato = parseInt($('.js-utente-loggato-sito').val()); if ($('.js-esegui-controllo-redirect-login_utente').length > 0) { if (utente_loggato === 0) { window.location.href = "login"; } } } $(window).on("load", function () { if ($(window).width() > 991) { var e = 0; $(".eq_height").each(function () { $(this).height() > e && (e = $(this).height()) }), $(".eq_height").height(e) } }); /** * Abilita/disabilita tutti i link di navigazione della pagina * * @param {bool} abilita */ function Navigazione(abilita = false) { if (abilita) { $('a').each(function () { $(this).removeClass('disabled'); }); } else { $('a').each(function () { $(this).addClass('disabled'); }); } } // esegue un reset dei filtri laterali per le auto function resetFiltri() { let div_contenitore_filtri = $('.js-container-filtri'); // checkbox if (div_contenitore_filtri.find('#js-nuovo').is(':checked')) { div_contenitore_filtri.find('#js-nuovo').prop('checked', false); } if (div_contenitore_filtri.find('#js-usato').is(':checked')) { div_contenitore_filtri.find('#js-usato').prop('checked', false); } if (div_contenitore_filtri.find('#js-km0').is(':checked')) { div_contenitore_filtri.find('#js-km0').prop('checked', false); } if (div_contenitore_filtri.find('#js-incidentato').is(':checked')) { div_contenitore_filtri.find('#js-incidentato').prop('checked', false); } //js-select if (parseInt(div_contenitore_filtri.find('.js-select-marche').val()) !== 0) { div_contenitore_filtri.find('.js-select-marche').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-modello').val()) !== 0) { div_contenitore_filtri.find('.js-select-modello').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-allestimento').val()) !== 0) { div_contenitore_filtri.find('.js-select-allestimento').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-nazione').val()) !== 0) { div_contenitore_filtri.find('.js-select-nazione').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-trazioni').val()) !== 0) { div_contenitore_filtri.find('.js-select-trazioni').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-cambio').val()) !== 0) { div_contenitore_filtri.find('.js-select-cambio').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-carrozzeria').val()) !== 0) { div_contenitore_filtri.find('.js-select-carrozzeria').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-porte').val()) !== 0) { div_contenitore_filtri.find('.js-select-porte').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-colore-esterno').val()) !== 0) { div_contenitore_filtri.find('.js-select-colore-esterno').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-select-emissione').val()) !== 0) { div_contenitore_filtri.find('.js-select-emissione').val("0").change(); } if (parseInt(div_contenitore_filtri.find('.js-tag').val()) !== 0) { div_contenitore_filtri.find('.js-tag').val("0").change(); } //cancellazione ricerca testuale if (div_contenitore_filtri.find('.js-ricerca-testuale-marca-modello').length > 0) { div_contenitore_filtri.find('.js-ricerca-testuale-marca-modello').val(''); } //range slider // distruggo e reistanzio lo slider $(".range-slider-ui").each(function () { $(this).destroy; }); $(".range-slider-ui").each(function () { var minRangeValue = parseFloat($(this).attr('data-min')); var maxRangeValue = parseFloat($(this).attr('data-max')); var currentMinValue = parseFloat($(this).attr('data-min')); var currentMaxValue = parseFloat($(this).attr('data-max')); var annoMassimoDB = parseFloat($('.js-anno-massimo').val()); var unit = $(this).attr('data-unit'); $(this).slider({ range: true, min: minRangeValue, max: maxRangeValue, values: [currentMinValue, currentMaxValue], slide: function (event, ui) { event = event; if (ui.values[0] >= 1980 && ui.values[1] <= annoMassimoDB) { var currentMin = ui.values[0]; var currentMax = ui.values[1]; } else { var currentMin = ui.values[0].toLocaleString('it-IT'); var currentMax = ui.values[1].toLocaleString('it-IT'); } $(this).children(".min-value").text(currentMin + " " + unit); $(this).children(".max-value").text(currentMax + " " + unit); $(this).children(".current-min").val(currentMin); $(this).children(".current-max").val(currentMax); } }); if ($(this).slider("values", 0) >= 1980 && $(this).slider("values", 1) <= annoMassimoDB) { var currentMin = $(this).slider("values", 0); var currentMax = $(this).slider("values", 1); } else { var currentMin = $(this).slider("values", 0).toLocaleString('it-IT'); var currentMax = $(this).slider("values", 1).toLocaleString('it-IT'); } $(this).children(".min-value").text(currentMin + " " + unit); $(this).children(".max-value").text(currentMax + " " + unit); $(this).children(".current-min").val(currentMin); $(this).children(".current-max").val(currentMax); }); eseguiRicercaDinamica(); // eseguo una ricerca al volo per pulire i risultati //disabilito il pulsante di salvataggio della ricerca $('.js-salva-ricerca').attr('disabled', true); $('.js-salva-ricerca').addClass('disabled-save'); history.pushState(null, "", location.href.split("?")[0]); // pulisco barra indirizzi } // permette di fare la ricerca al cambio di uno degli elementi del box filtri e stampare i risultati // nel box laterale function eseguiBindPerRicercaDinamica() { // binding di tutti i campi // binding per il popolamento della select marche $('.js-select-marche').on('change', function () { // popolaSelectModelliDaSelectMarche(); popolaSelectGruppiStoriciDaSelectMarche(); controllaAbilitazioneInputRicercaTestualeModello(); // debugger; eseguiRicercaDinamica(); }); // binding per il popolamento della select modelli (gruppi storici) $('.js-select-gruppo-storico').on('select2:select', function () { controllaAbilitazioneInputRicercaTestualeModello(); eseguiRicercaDinamica(); }); // bind di tutte le check in un colpo solo [eccetto modello e marca] let checks = ['#js-nuovo', '#js-usato', '#js-km0', '#js-incidentato']; $.each(checks, function (k, v) { $(v).click(eseguiRicercaDinamica); }); // bind delle dropdowns let dropdowns = ['.js-select-allestimento', '.js-select-nazione', '.js-select-alimentazione', '.js-select-trazioni', '.js-select-cambio', '.js-select-carrozzeria', '.js-select-porte', '.js-select-colore-esterno', '.js-select-emissione', '.js-tag']; $.each(dropdowns, function (k, v) { $(v).change(eseguiRicercaDinamica); }); // bind del campo di ricerca testuale $('.js-ricerca-testuale-marca-modello').change(eseguiRicercaDinamica); // bind del campo di ricerca testuale $('.js-ricerca-testuale-allestimento').change(eseguiRicercaDinamica); // bind delle range sliders $(".range-slider-ui").each(function () { $(this).on("slidechange", eseguiRicercaDinamica); }) } // esegue la chiamata per la ricerca delle auto in maniera dinamica function eseguiRicercaDinamica() { // controllo l'esistenza della classe "js-esegui-ricerca-dinamica" // se esiste vuol dire che sono su una pagina che ha bisogno di mostrare gli annunci // quindi posso eseguire la ricerca dinamica via ajax // NB: questo campo é stato aggiunto perchè questa funzione viene eseguita anche su pagine // che non hanno bisogno di avere gli annunci come dato if ( $('.js-esegui-ricerca-dinamica').length > 0 ) { avvia = location.pathname !== '/richiesta-auto' ? true : false; if (avvia) { let filtri = getDatiFiltri(); let url = componiUrlRicercaAuto(filtri, false); sessionStorage.setItem('search', location.origin + location.pathname + url); // salvo la path di ricerca $.ajax({ type: "POST", url: "/ws/annunci/ajax/get_annunci_dinamici", data: { filtri: filtri, url: url }, dataType: "json", success: function (response) { $('.js-risultati').html(null); $('.js-risultati').html(response); if ($('[name=js-calcolo]').length > 0) { $('.js-aggiorna-totale-annunci').html($('[name=js-calcolo]').val()); // aggiorno totale filtrato in testa } bindPulsantiAnnuncioIndice(); }, error: function(){ alert('ricerca non disponibile al momento, ripriovare più tardi'); } }); //riabilito il pulsante di salvataggio della ricerca $('.js-salva-ricerca').removeAttr('disabled'); $('.js-salva-ricerca').removeClass('disabled-save'); } } } // esegue il binding dei pulsanti su ogni annuncio nell'indice degli annunci auto // necessaria fn esterna per la ricerca dinamica degli articoli function bindPulsantiAnnuncioIndice() { // Binding per i tasti di comparazione annunci $('.js-btn-compare').on('click', function () { comparaAnnuncio($(this)); }); // Binding per i tasti di rimozione dalla comparazione annunci $('.js-rimuovi-confronto').on('click', function () { rimuoviComparaAnnuncio($(this)); }); // Binding per i tasti di rimozione comparazione $('.js-opziona-annuncio').on('click', function () { opzionaAnnuncio($(this)); }); // binding per rimuovere un annuncio dalla wishlist $('.js-add-remove-wishlist').on('click', function () { aggiungiRimuoviAnnuncioWishlist($(this)); }); // binding tag per il singolo annuncio $('.tag-auto').click(function () { let vista = $('.js-container-filtri').find('.js-tipo_vista').val(); let div_contenitore_filtri = $('.js-container-filtri'); componiUrlRicercaAuto( { alimentazione: 0, allestimento: 0, anno_massimo: 2020, anno_minimo: 1980, cambio: 0, carrozzeria: 0, colore_esterno: 0, condizione: 0, emissione: 0, flag_wishlist: 0, km_massimo: 1345000, km_minimo: 0, marca: '0', modello: 0, nazione: 0, nome_alimentazione: "", nome_allestimento: "", nome_colore_esterno: "", nome_marca: "", nome_modello: "", ordinamento: "nuove", nuovo: 0, usato: 0, km0: 0, incidentato: 0, porte: 0, prezzo_massimo: 200000, prezzo_minimo: 0, ricerca_testuale_marca_modello: "", tag: $(this).data('tag'), trazione: 0, url_sito: div_contenitore_filtri.find('.js-url_sito').val(), vista: vista == '' ? "lista" : vista, }); }); } /** * manda una email da utente registrato di consorzio */ function inviaContattoPrivato(){ let dati = { g_recaptcha_response : $('[name=g-recaptcha-response]').val(), id_utente : $('[name=id-utente]').val(), oggetto : $('[name=oggetto]').val(), testo : $('[name=messaggio]').val() } $.ajax({ type: "POST", url: "/ws/consorziati/ajax/invia_email_contatto_privato", data: dati, dataType: "json", success: function (data) { swal.fire({ title: data['titolo'], text: data['messaggio'], type: data['stato'], confirmButttom: 'OK' }).then(() => { window.location = '/'; // rimando in home l'utente dopo l'ok }); }, error: function () { swal.fire({ title: 'Errore', text: 'Si è presentato un errore durante l\'elaborazione della richiesta', type: 'error', confirmButttom: 'OK' }); } }); } // Equal Height function setHeight(el, val) { if (typeof val === "function") val = val(); if (typeof val === "string") el.style.height = val; else el.style.height = val + "px"; } var equalheight = function(container){ var currentTallest = 0, currentRowStart = 0, rowDivs = new Array(), $el, topPosition = 0; Array.from(document.querySelectorAll(container)).forEach((el,i) => { el.style.height = "auto"; topPostion = el.offsetTop; if(currentRowStart != topPostion){ for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) { setHeight(rowDivs[currentDiv], currentTallest) } rowDivs.length = 0; currentRowStart = topPostion; currentTallest = parseFloat(getComputedStyle(el, null).height.replace("px", "")) rowDivs.push(el); } else { rowDivs.push(el); currentTallest = (currentTallest < parseFloat(getComputedStyle(el, null).height.replace("px", ""))) ? (parseFloat(getComputedStyle(el, null).height.replace("px", ""))) : (currentTallest); } for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) { setHeight(rowDivs[currentDiv], currentTallest) } }) } window.addEventListener("load", function(){ equalheight('.eqHeight_dv') }) window.addEventListener("resize", function(){ setTimeout(function(){ equalheight('.eqHeight_dv') }) }) window.init_ajax_select2 = function (){ $(".js-data-ajax").select2({ ajax: { // url è dinamico dal front dataType: 'json', delay: 250, data: function (params) { return { search: params.term }; }, processResults: function (data, params) { // parse the results into the format expected by Select2 // since we are using custom formatting functions we do not need to // alter the remote JSON data, except to indicate that infinite // scrolling can be used // params.page = params.page || 1; return { results: data.items }; }, cache: true }, // placeholder: 'Search for a repository', minimumInputLength: 2, templateResult: formatRepo }); function formatRepo (repo) { if (repo.loading) { return repo.text; } var $container = $( "
" + "
" + "
" + "
" + "
" ); $container.find(".select2-result-repository__title").text(repo.text); return $container; } } function selfautocompilator(e){ let blocco = $('.' + $(this).data('target')); blocco.find('.parsley-errors-list').remove(); blocco.find('input').removeClass('parsley-error'); if($(this).is(':checked')) { blocco.find('input').each(function () { let default_val = $(this).data('default'); $(this).val(default_val); $(this).prop('readonly', true); }); blocco.find('select option').remove(); blocco.find('select').each(function () { let default_val = $(this).data('default'); let default_id = $(this).data('default_id'); $(this).append(new Option(default_val, default_id,default_id)); $(this).attr("readonly", "readonly"); }); }else { blocco.find('input').each(function(){ $(this).val(''); $(this).prop('readonly', false); }) blocco.find('select option').remove(); blocco.find('select').removeAttr('readonly'); } getSpesa(); } /** * esegue chiamata ajax per la ricerca globale del termine passato in firma * @param {string} termine termine parziale di ricerca */ function eseguiRicercaGlobale(termine){ if(termine.length > 1){ // $('.js-animation').show(250, "slow"); $.ajax({ type: "POST", url: "/ws/ricerca_globale/ajax/ricerca_globale", data: { termine : termine}, dataType: "json", success: function (response) { $('#js-risultati-ricerca-globale').removeAttr('style'); let html = ''; html += '
'; $('#js-risultati-ricerca-globale').html(html); $('.js-risultato-ricerca-globale').click(function(){ $('#js-ricerca-globale').val(''); //svuoto la ricerca }); } }); } else { ricreaLoader(); $('.js-animation').hide(); } } function ricreaLoader(){ let html = '
' +'
' +' loader animation' +'
' +'
'; $('#js-risultati-ricerca-globale').html(html) }