/** * TNT OFFICIAL MODULE FOR PRESTASHOP * * @author GFI Informatique * @copyright 2016 GFI Informatique, 2016 TNT * @license https://opensource.org/licenses/MIT MIT License */ $(document).ready(function () { // Create the FancyBox. createFancyBox(); var boolIsCityValid = false; // When a city is selected. $("#validateCity").on('click', function () { var objJqXHR = $.ajax({ "url": window.TNTOfficiel.link.front.module.updateAddressDelivery, "method": 'POST', "data": { "city": $("#helper_city").val() }, "async": false }); objJqXHR .done(function (mxdData, strTextStatus, objJqXHR) { }) .fail(function (objJqXHR, strTextStatus, strErrorThrown) { // console.error( objJqXHR.status + ' ' + objJqXHR.statusText ); }) .always(function () { // Close the FancyBox. $.fancybox.close(); boolIsCityValid = true; }); }); $("[name='processAddress']").on('click', function (objEvent) { if (!boolIsCityValid) { objEvent.preventDefault(); var objJqXHR = $.ajax({ "url": window.TNTOfficiel.link.front.module.getAddressCities, "method": 'POST', "async": false }); objJqXHR .done(function (mxdData, strTextStatus, objJqXHR) { var response = JSON.parse(mxdData); displayFancyBox(response.cities, response.postcode) }) .fail(function (objJqXHR, strTextStatus, strErrorThrown) { // console.error( objJqXHR.status + ' ' + objJqXHR.statusText ); }) .always(function () {}); } }); }); /** * insert the fancy box html into the page */ function createFancyBox() { $("#page").append('\ \
\
\

Sélectionnez votre ville

\
\ \ \
\
\ \ \
\

\ \

\
\
' ); } /** * Display the fancybox */ function displayFancyBox(arrArgCities, strArgPostCode) { // fancybox configuration $("a#fancybox_city_helper").fancybox({ "transitionIn": 'elastic', "transitionOut": 'elastic', "type": 'inline', "speedIn": 600, "speedOut": 200, "overlayShow": false, "autoDimensions": true, "helpers": { overlay: {closeClick: false} } }); // Generate the options to be put in the city select field. var strHTMLOptions = ""; $.each(arrArgCities, function (index, city) { strHTMLOptions += ""; }); $("#helper_city").html(strHTMLOptions); $("#helper_city").addClass("form-control"); $("#helper_postcode").val(strArgPostCode); $("a#fancybox_city_helper").trigger("click"); }