(function () { $(document).ready(function () { AxStoreLocatorStock.init(); }); })(window.jQuery); var AxStoreLocatorStock = { map: null, mapType: null, markers: [], geocoder: null, infoWindow: null, markerClusterer: null, storeSearchCount: 0, storesListContainer: null, storeListItemTemplate: null, storeInfoWindowTemplate: null, productId: null, productRef: null, /** * Constructeur * * @return {void} */ init: function () { if($('body').attr('id') == 'product') { var t_parent = this; var location = $('#axstorelocator-widget-popin-stock input[name="location"]').val(); var t_variantsData = $(document).find('#widget-fm-product-stocks-variants ul'); this.productId = parseInt($(document).find('form#add-to-cart-or-refresh input#product_page_product_id').val()); this.productRef = $(document).find('#product-details').data('reference'); this.initHandebarsTemplates(); this.storesListContainer = $('#axstorelocator-widget-popin-stock .stores-list'); $(document).on('click', '.btn-product-stock', function (e) { e.preventDefault(); t_parent.showLoading(); // get variants of product var t_variants = $(document).find('#add-to-cart-or-refresh .product-variants .prod-variant ul').html(); t_variantsData.html(t_variants); t_variantsData.find('li').each(function(){ $(this).attr('size', $(this).find('label').text()); }); t_parent.initGoogleMaps(); t_parent.searchByAddress("Paris"); t_parent.initSelectStoreStock(); }); $(document).on('click', '#axstorelocator-widget-popin-stock .store-stock.prod-variant li', function(){ $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.stock-current').removeClass('stock-current'); $(this).addClass('stock-current'); /* ICC Digital - JTD - 2020-05-10 - Ajoute message si pas de stock */ //$(this).parent().parent().parent().parent().find('.message-pas-stock').removeClass('affiche-message'); //$(this).parent().parent().find('.message-pas-stock').addClass('affiche-message'); var indispoWeb = $('#group_4 li.selected'); if (indispoWeb.hasClass('indispo')) { /* Click&Collect V2 */ $(this).parent().parent().find('.clickandcollect-v2').removeClass('dispo-4j'); /* Click&Collect V2 */ $(this).parent().parent().find('.clickandcollect-v2').removeClass('dispo-2h'); /* Click&Collect V2 */ $(this).parent().parent().find('.clickandcollect-v2').addClass('dispo-no'); /* Click&Collect V2 */ } /* Click&Collect V2 */ else { /* Click&Collect V2 */ $(this).parent().parent().find('.clickandcollect-v2').addClass('dispo-4j'); $(this).parent().parent().find('.clickandcollect-v2').removeClass('dispo-2h'); $(this).parent().parent().find('.clickandcollect-v2').removeClass('dispo-no'); /* Click&Collect V2 */ } /* Click&Collect V2 */ /* ICC Digital - JTD - 2020-05-10 - Ajoute message si pas de stock */ }); /* ICC Digital - JTD - Ajoute la sélection de la déclinaison */ $(document).on('click', '#axstorelocator-widget-popin-stock .store-stock.prod-variant li.stock-isavailable', function () { $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected-variant').removeClass('selected-variant'); $(this).addClass('selected-variant'); $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant button.btn-popup-clicandcollect').removeClass('selected-store'); $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected-variant').parent().parent().find('button.btn-popup-clicandcollect').addClass('selected-store'); //var input_variant_selected = $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected-variant input'); /* Click&Collect V2 */ var input_variant_selected = $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected input'); /* Click&Collect V2 */ //$(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected-variant').parent().parent().find('#hidden-variant').attr("name", input_variant_selected.attr("name")); /* Click&Collect V2 */ //$(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected-variant').parent().parent().find('#hidden-variant').attr("value", input_variant_selected.attr("value")); /* Click&Collect V2 */ $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected').parent().parent().find('#hidden-variant').attr("name", input_variant_selected.attr("name")); /* Click&Collect V2 */ $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected').parent().parent().find('#hidden-variant').attr("value", input_variant_selected.attr("value")); /* Click&Collect V2 */ if ($(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected').parent().parent().find('#store_hidden').attr("value") == "") { /* Click&Collect V2 */ var storeida = $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected').parent().parent().parent().attr("data-storeid"); /* Click&Collect V2 */ $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected').parent().parent().find('#store_hidden').attr("value", storeida); /* Click&Collect V2 */ } /* Click&Collect V2 */ /* ICC Digital - JTD - 2020-05-10 - Ajoute message si pas de stock */ //$(this).parent().parent().find('.message-pas-stock').removeClass('affiche-message'); $(this).parent().parent().find('.clickandcollect-v2').addClass('dispo-2h'); $(this).parent().parent().find('.clickandcollect-v2').removeClass('dispo-4j'); $(this).parent().parent().find('.clickandcollect-v2').removeClass('dispo-no'); /* Click&Collect V2 */ /* ICC Digital - JTD - 2020-05-10 - Ajoute message si pas de stock */ }); /* ICC Digital - JTD - Ajoute la sélection de la déclinaison */ } }, /** * Map, Style, InfoWindow, MarkerClusterer * * @return {void} */ initGoogleMaps: function () { if (typeof google === 'undefined') { return; } // Map this.map = new google.maps.Map(document.getElementById('map'), { "zoom": 15, "center": new google.maps.LatLng(defaultLat, defaultLng), "mapTypeId": google.maps.MapTypeId.ROADMAP, "mapTypeControl": false, "panControl": false, "streetViewControl": false, "gestureHandling": 'cooperative', "zoomControlOptions": { "style": google.maps.ZoomControlStyle.SMALL } }); // Style if (typeof mapStyleGrayscale !== 'undefined' && mapStyleGrayscale) { this.mapType = new google.maps.StyledMapType([{ "stylers": [{ "saturation": -100 }] }], { "name": 'Grayscale' }); } else if (typeof mapStyleCustom !== 'undefined' && mapStyleCustom) { this.mapType = new google.maps.StyledMapType(JSON.parse(mapStyleCustomJson), { "name": 'Custom' }); } if (this.mapType) { this.map.mapTypes.set('styled_map', this.mapType); this.map.setMapTypeId('styled_map'); } // InfoWindow this.infoWindow = new google.maps.InfoWindow({ "maxWidth": 500 }); // MarkerClusterer if (typeof clustersEnabled !== 'undefined' && clustersEnabled) { var markerClustererOptions = { maxZoom: 12, imagePath: clustersIconsBaseUri + '/' + clustersIconsTheme + '/m' }; if (clustersIconsTheme === 'black') { markerClustererOptions.styles = [ { textColor: 'white', height: 30, width: 30, url: clustersIconsBaseUri + '/' + clustersIconsTheme + '/m1.png' }, { textColor: 'white', height: 30, width: 30, url: clustersIconsBaseUri + '/' + clustersIconsTheme + '/m2.png' }, { textColor: 'white', height: 30, width: 30, url: clustersIconsBaseUri + '/' + clustersIconsTheme + '/m3.png' }, { textColor: 'white', height: 30, width: 30, url: clustersIconsBaseUri + '/' + clustersIconsTheme + '/m4.png' }, { textColor: 'white', height: 30, width: 30, url: clustersIconsBaseUri + '/' + clustersIconsTheme + '/m5.png' } ]; } this.markerClusterer = new MarkerClusterer(this.map, [], markerClustererOptions); } $(document).find('#map').parent().css('display', 'block'); }, /** * Templating JS avec Handlebars pour les résultats de recherche et l'infoWindow * * @return {void} */ initHandebarsTemplates: function () { var $storeInfoWindowTemplate = $('#store-info-window-template-stock'); if ($storeInfoWindowTemplate.length) { this.storeInfoWindowTemplate = Handlebars.compile($storeInfoWindowTemplate.html()); } var $storeListItemTemplate = $('#store-list-item-template-stock'); if ($storeListItemTemplate.length) { this.storeListItemTemplate = Handlebars.compile($storeListItemTemplate.html()); } }, /** * Vider les résultats de recherche (points sur la carte, liste des magasins, etc.) * * @return {void} */ clearLocations: function () { // Results list if (this.storesListContainer.length) { this.storesListContainer.empty(); } if (this.map) { // InfoWindow this.infoWindow.close(); // Markers for (var i = 0; i < this.markers.length; i++) { this.markers[i].setMap(null); } this.markers.length = 0; } }, /** * Recherche par adresse : * 1. On cherche les coordonnées GPS correspondant à l'adresse * 2. On déclenche la recherche en BDD à partir des coordonnées GPS (searchByCoordinates) * * @param {string} address * * @return {void} */ searchByAddress: function (address) { if (!address) { return; } //this.showLoading(); this.geocoder = this.geocoder || new google.maps.Geocoder(); this.geocoder.geocode({ "address": address }, function (results, status) { if (status === google.maps.GeocoderStatus.OK) { this.searchByCoordinates( results[0].geometry.location.lat(), results[0].geometry.location.lng(), false ); } else { this.showErrorMessage(errorNoStoresFound); this.hideLoading(); } }.bind(this)); }, /** * Recherche par coordonnées GPS * * @param {Number} latitude * @param {Number} longitude * @param {Boolean} all * * @return {void} */ searchByCoordinates: function (latitude, longitude, all) { if (!latitude || !longitude) { return; } //this.showLoading(); this.storeSearchCount++; var $radius = $('[name="radius"]'); // ICC Digital //var radius = $radius.length ? $radius.val() : 100; var radius = $radius.length ? $radius.val() : 500; var params = { "ajax": 1, "product_id": this.productId, "product_ref": this.productRef }; if (typeof all !== 'undefined' && all) { params = $.extend(params, { "all": 1 }); } else { params = $.extend(params, { "latitude": latitude, "longitude": longitude, "radius": radius }); } if (typeof storeId === 'number' && parseInt(storeId) > 0) { params = $.extend(params, { "store_id": parseInt(storeId) }); } $.ajax({ "url": stockUrl, "data": params, "dataType": "json", "method": "GET" }) .done(function (response) { this.ajaxProcessStores(response); }.bind(this)) .fail(function () { this.hideLoading(); this.showErrorMessage(errorGeneric); }.bind(this)); }, /** * @param {Object} response * * @return {void} */ ajaxProcessStores: function (response) { if (response.hasOwnProperty('success') && !!response.success) { if (this.markerClusterer) { this.markerClusterer.clearMarkers(); } if (this.storesListContainer.length) { this.clearLocations(); } var nb_results = response.data.pagination.total_count; // Info pagination if (response.data.pagination && response.data.pagination.total_count > response.data.pagination.per_page) { this.showInfoMessage(infoMoreResults.replace(/\$NB_RESULTS/g, nb_results)); } // Résumé de la recherche (popin de géolocalisation) if ($('#axstorelocator-widget-popin-stock .stores-search-summary-and-map-link').length) { $('#axstorelocator-widget-popin-stock .stores-search-summary-and-map-link').removeClass('d-none'); if (nb_results === 0) { $('#axstorelocator-widget-popin-stock .stores-nb-results-0').removeClass('d-none'); $('#axstorelocator-widget-popin-stock .stores-nb-results-1').addClass('d-none'); $('#axstorelocator-widget-popin-stock .stores-nb-results-n').addClass('d-none'); } else if (nb_results === 1) { $('#axstorelocator-widget-popin-stock .stores-nb-results-0').addClass('d-none'); $('#axstorelocator-widget-popin-stock .stores-nb-results-1').removeClass('d-none'); $('#axstorelocator-widget-popin-stock .stores-nb-results-n').addClass('d-none'); } else if (nb_results > 1) { $('#axstorelocator-widget-popin-stock .stores-nb-results-0').addClass('d-none'); $('#axstorelocator-widget-popin-stock .stores-nb-results-1').addClass('d-none'); $('#axstorelocator-widget-popin-stock .stores-nb-results-n').removeClass('d-none').text($('.stores-nb-results-n').data('text').replace(/\$NB_RESULTS/g, nb_results)); } } if (this.map) { var bounds = new google.maps.LatLngBounds(); } $.each(response.data.stores, function (i, store) { if (this.map) { var latlng = new google.maps.LatLng( parseFloat(store.latitude), parseFloat(store.longitude) ); this.createMarker(latlng, store, i); bounds.extend(latlng); } if (this.storesListContainer.length) { this.storesListContainer.append(this.storeListItemTemplate({ "store": store, "markerid": i, "options": { "favoriteStoreEnabled": false } })); if (this.storeSearchCount > 1) { this.storesListContainer.parent().show(); this.storesListContainer.parent().find('.list-group-item .store-stock').html($(document).find('#widget-fm-product-stocks-variants').html()); } } }.bind(this)); $.each(response.data.stocks, function (i, mag) { $.each(mag, function (j, stock) { //console.log(stock.store_id, stock.size, stock.in_stock); var t_store = $(document).find('#axstorelocator-widget-popin-stock .stores-list').find('a[data-storeid="' + stock.store_id + '"]'); if(t_store.length > 0 && stock.in_stock) { var t_size = t_store.find('.store-stock.prod-variant ul li[size="'+stock.size+'"]'); if(t_size.length > 0) { t_size.addClass('stock-isavailable'); } // ICC Digital - JTD - 2020-04-21 - Ajoute le store id t_store.find('#store_hidden').attr('value', stock.store_id); var id_store_selected = t_store.find('#id_store_selected').attr('value'); /*if (id_store_selected != 0 && id_store_selected != stock.store_id) { t_store.find('.form-add-click-and-collect').addClass("inavailable-store"); t_store.find('.alert_message_store').addClass("inavailable-store"); }*/ /* ICC Digital - JTD - Click&Collect V2 */ $('#axstorelocator-widget-popin-stock li.selected').click(); // JTD V2 // ICC Digital - JTD - 2020-04-21 - Ajoute le store id } }.bind(this)); }.bind(this)); if (this.map) { this.map.fitBounds(bounds); var t_parent = this; // Trigger a click event on each marker when the corresponding marker link is clicked $('#axstorelocator-widget-popin-stock .stores-list a.list-group-item').on('click', function () { google.maps.event.trigger(t_parent.markers[$(this).data('markerid')], 'click'); $(this).find('input[type="radio"]').prop('checked', true); /* ICC Digital - JTD - Ajoute la sélection de la déclinaison */ $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant li.selected-variant').removeClass('selected-variant'); $(document).find('#axstorelocator-widget-popin-stock .store-stock.prod-variant button.btn-popup-clicandcollect').removeClass('selected-store'); /* ICC Digital - JTD - Ajoute la sélection de la déclinaison */ }); this.map.setZoom(12); // ICC Digital this.map.setCenter(new google.maps.LatLng(48.870255, 2.337525)); var listener = google.maps.event.addListener(this.map, 'idle', function () { if (this.map.getZoom() < 12) { this.map.setZoom(12); } google.maps.event.removeListener(listener); }.bind(this)); } } else { this.showErrorMessage(errorGeneric); } this.hideLoading(); /* ICC Digital - JTD - Click&Collect V2 */ var allSizes = document.getElementsByClassName('sizes-popup'); for (i = 0; i < allSizes.length; i++) { allSizes[i].classList.remove('select'); } var btns = document.getElementsByClassName('btn-popup-clicandcollect'); for (i = 0; i < btns.length; i++) { btns[i].disabled = true; } var allDispos = document.getElementsByClassName('all-dispos'); for (i = 0; i < allDispos.length; i++) { allDispos[i].style.visibility = 'hidden'; } /* ICC Digital - JTD - Click&Collect V2 */ }, /** * @param {google.maps.LatLng} latlng * @param {Object} store * * @return {void} */ createMarker: function (latlng, store, i) { var html = this.storeInfoWindowTemplate({ "latlng": latlng, "store": store }); var t_parent = this; // Hook JS :: Changement de l'icone Marker //var image = new google.maps.MarkerImage(); /*var image = (typeof filterStoreLocatorMarkerImage === 'function') ? filterStoreLocatorMarkerImage(store) : new google.maps.MarkerImage(logoStore);*/ /*var marker = hasStoreIcon ? new google.maps.Marker({map: this.map, icon: image, position: latlng}) : new google.maps.Marker({map: this.map, position: latlng});*/ var marker = new google.maps.Marker({map: this.map, position: latlng}); // Register a click event listener on the marker to display the corresponding infowindow content google.maps.event.addListener(marker, 'click', (function (marker, i) { return function () { t_parent.infoWindow.setContent(html); t_parent.infoWindow.open(map, marker); } })(marker, i)); /*google.maps.event.addListener(marker, 'click', function () { this.infoWindow.setContent(html); this.infoWindow.open(map, marker); }.bind(this));*/ this.markers.push(marker); if (this.markerClusterer) { this.markerClusterer.addMarker(marker); } }, /** * @return {void} */ initSelectStoreStock: function () { // Nécessaire pour forcer l'affichage des résultats dès la 1ère recherche depuis la popin this.storeSearchCount++; $.fancybox.open({ "src": "#axstorelocator-widget-popin-stock", "type": "inline", "opts": { "modal": false, "touch": false } }); }, /** * @return {void} */ showLoading: function () { this.hideErrorMessage(); $('body').addClass('axstorelocator-loading'); }, /** * @return {void} */ hideLoading: function () { $('body').removeClass('axstorelocator-loading'); $(document).find('#axstorelocator-widget-popin-stock .row.stores-map-and-list').fadeIn(500); $(document).find('#axstorelocator-widget-popin-stock .widget-fm-product-stocks-footer').fadeIn(500); }, /** * @param {string} msg * * @return {void} */ showErrorMessage: function (msg) { this.hideErrorMessage(); $('#content').prepend('
' + msg + '
'); }, /** * @return {void} */ hideErrorMessage: function () { $('#content .axstorelocator-error').remove(); }, /** * @param {string} msg * * @return {void} */ showInfoMessage: function (msg) { this.hideInfoMessage(); $('#content').prepend('
' + msg + '
'); }, /** * @return {void} */ hideInfoMessage: function () { $('#content .axstorelocator-info').remove(); } };