1 line
3.4 KiB
JavaScript
1 line
3.4 KiB
JavaScript
|
!function(d){let p,h;const u={street_number:"long_name",route:"long_name",locality:"long_name",postal_town:"long_name",sublocality_level_2:"long_name",administrative_area_level_1:"short_name",country:"short_name",postal_code:"short_name"};function e(e){var n={componentRestrictions:{country:[e=null==e?d("#billing_country :selected").val():e]},types:["geocode"]};""!==e&&void 0!==e||delete n.componentRestrictions,(h=new google.maps.places.Autocomplete(document.getElementById("billing_address_1"),n)).addListener("place_changed",t)}function n(e){var n={componentRestrictions:{country:[e=null==e?d("#shipping_country :selected").val():e]},types:["geocode"]};""!==e&&void 0!==e||delete n.componentRestrictions,(p=new google.maps.places.Autocomplete(document.getElementById("shipping_address_1"),n)).addListener("place_changed",i)}function t(){d("#billing_address_1").val(""),d("#billing_country").val(""),d("#billing_address_2").val(""),d("#billing_city").val(""),d("#billing_state").val(""),d("#billing_postcode").val("");var e=h.getPlace(),n=["street_number","route"],t=["sublocality_level_2"];let i="",l="",s="",o="",a="",g="";if(e&&0<e.address_components.length){for(const _ of e.address_components){var r,c=_.types[0];u[c]&&(r=_[u[c]],"country"===c&&(i=r),"administrative_area_level_1"===c&&(l=r),"locality"!==c&&"postal_town"!==c||(s=r),"postal_code"===c&&(g=r),-1!==n.indexOf(c)&&(o+=r+" "),-1!==t.indexOf(c))&&(a+=r+" ")}0<d("#billing_address_1").length&&0<o.length&&d("#billing_address_1").val(o.trimEnd()).trigger("focus"),0<d("#billing_address_2").length&&d("#billing_address_2").val(a.trimEnd()).trigger("focus"),0<d("#billing_postcode").length&&d("#billing_postcode").val(g).trigger("focus"),0<d("#billing_city").length&&d("#billing_city").val(s).trigger("focus"),0<d("#billing_state").length&&d("#billing_state").val(l).trigger("focus").trigger("change"),0<d("#billing_country").length&&d("#billing_country").val(i).trigger("focus").trigger("change"),!e.name||""===e.name&&void 0===e.name||d("#billing_address_1").val(e.name)}}function i(){if(d("#ship-to-different-address-checkbox").is(":checked")){d("#shipping_address_1").val(""),d("#shipping_country").val(""),d("#shipping_address_2").val(""),d("#shipping_city").val(""),d("#shipping_state").val(""),d("#shipping_postcode").val("");var o=p.getPlace(),a=["street_number","route"],g=["sublocality_level_2"];let e="",n="",t="",i="",l="",s="";if(o&&0<o.address_components.length){for(const _ of o.address_components){var r,c=_.types[0];u[c]&&(r=_[u[c]],"country"===c&&(e=r),"administrative_area_level_1"===c&&(n=r),"locality"===c&&(t=r),"postal_code"===c&&(s=r),-1!==a.indexOf(c)&&(i+=r+" "),-1!==g.indexOf(c))&&(l+=r+" ")}0<d("#shipping_address_1").length&&0<i.length&&d("#shipping_address_1").val(i.trimEnd()).trigger("focus"),0<d("#shipping_address_2").length&&d("#shipping_address_2").val(l.trimEnd()).trigger("focus"),0<d("#shipping_postcode").length&&d("#shipping_postcode").val(s).trigger("focus"),0<d("#shipping_city").length&&d("#shipping_city").val(t).trigger("focus"),0<d("#shipping_state").length&&d("#shipping_state").val(n).trigger("focus").trigger("change"),0<d("#shipping_country").length&&d("#shipping_country").val(e).trigger("focus").trigger("change"),!o.name||""===o.name&&void 0===o.name||d("#shipping_address_1").val(o.name)}}}d(document).on("change","#billing_country",function(){e(d(this).val())}),d(document).on("change","#shipping_country",function(){n(d(this).val())}),d(document).on("ready",function(){e(),n()})}(jQuery);
|