forked from sperti/lightcheckout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lightcheckout.min.js
2 lines (2 loc) · 3.77 KB
/
jquery.lightcheckout.min.js
1
2
/*! LightCheckout.min 03-05-2015 */
!function(a){var b,c,d={},e={inputWrapper:"div",autocomplete:null,address:".address",city:".city",province:".province",zip:".zip",shortProvince:!0,noNumberError:"Please, insert also street number.",language:"en",setField:function(a,b){a.val(b)}},f=!0,g={types:["address"]};a.fn.loadedGp=function(){f&&(f=!1,d.form.length>0&&d.form.trigger("initGp"))},a.fn.lightCheckout=function(f){if(0===this.length)return this;if(this.length>1)return console.error("lightCheckout: you have to initialize 1 form at time."),this;d.form=a(this);var h=function(){d.settings=a.extend({},e,f);return d.settings.inputs={address:d.form.find(d.settings.address),city:d.form.find(d.settings.city),province:d.form.find(d.settings.province),zip:d.form.find(d.settings.zip)},1!==d.settings.inputs.address.length||1!==d.settings.inputs.city.length||1!==d.settings.inputs.province.length||1!==d.settings.inputs.zip.length?void console.error("lightCheckout: form input missed."):(d.settings.wrappers={address:d.settings.inputs.address.closest(d.settings.inputWrapper),city:d.settings.inputs.city.closest(d.settings.inputWrapper),province:d.settings.inputs.province.closest(d.settings.inputWrapper),zip:d.settings.inputs.zip.closest(d.settings.inputWrapper)},"function"!=typeof a.getScript?void console.error("lightCheckout: getScript function missed."):(null===d.settings.autocomplete?j():i(),k(),void("undefined"==typeof google||"undefined"==typeof google.maps||"undefined"==typeof google.maps.places?(a.getScript("https://maps.googleapis.com/maps/api/js?libraries=places&language="+d.settings.language+"&callback=$.fn.loadedGp"),d.form.one("initGp",m)):m())))},i=function(){d.clonedInput=d.form.find(d.settings.autocompleteInput),c=d.clonedInput.find("input").attr("id")},j=function(){d.clonedInput=d.settings.wrappers.address.clone().addClass("lcWrapper"),b=d.settings.inputs.address.attr("id"),d.clonedInput.find("input").attr("id",b+"_lcInput").attr("autocomplete","false").removeAttr("required").removeAttr("name"),d.settings.wrappers.address.before(d.clonedInput.attr("id",b+"_lcWrapper")),newInptId=b+"_lcInput"},k=function(){d.settings.wrappers.address.addClass("lc-input lc-hide"),d.settings.wrappers.city.addClass("lc-input lc-hide"),d.settings.wrappers.zip.addClass("lc-input lc-hide"),d.settings.wrappers.province.addClass("lc-input lc-hide")},l=function(){d.settings.wrappers.address.removeClass("lc-hide"),d.settings.wrappers.city.removeClass("lc-hide"),d.settings.wrappers.zip.removeClass("lc-hide"),d.settings.wrappers.province.removeClass("lc-hide"),d.clonedInput.addClass("lc-hide")},m=function(){if("undefined"!=typeof newInptId&&"undefined"!=typeof g){var a=document.getElementById(newInptId);autocomplete=new google.maps.places.Autocomplete(a,g),google.maps.event.addListener(autocomplete,"place_changed",n)}},n=function(){var b=autocomplete.getPlace(),c="",e="",f="",g="",h="",i="",j="";if("undefined"!=typeof b&&"undefined"!=typeof b.address_components){for(var k=0;k<b.address_components.length;k++){var m=b.address_components[k],n=m.types[0];"street_number"==n&&(c=m.long_name),"route"==n&&(e=m.long_name),"locality"==n&&(f=m.long_name),"administrative_area_level_3"==n&&(f=m.long_name),"administrative_area_level_2"==n&&(h=m),"postal_code"==n&&(g=m.long_name),"administrative_area_level_1"==n&&(i=m),"country"==n&&(j=m.short_name)}h="US"==j?i:h,h=d.settings.shortProvince?h.short_name:h.long_name,""===c&&(d.settings.inputs.address.after('<span class="error noNumError">'+d.settings.noNumberError+"</span>"),d.settings.inputs.address.one("focus",function(){a(this).siblings(".noNumError").remove()})),d.settings.setField(d.settings.inputs.address,e+" "+c),d.settings.setField(d.settings.inputs.city,f),d.settings.setField(d.settings.inputs.zip,g),d.settings.setField(d.settings.inputs.province,h),l()}else{l()}};h()}}(jQuery);