-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMovingCostCalculator.min.js
1 lines (1 loc) · 20.4 KB
/
MovingCostCalculator.min.js
1
class MovingCostCalculator{constructor(e,t){if(this._elements={wrapper:null,departure:{address:null,optionToggler:null,options:{floor:null,lift:null,porterageDistance:null}},arrival:{address:null,optionToggler:null,options:{floor:null,lift:null,porterageDistance:null}},volume:null,contact:null,loader:null,validation:{addresses:null,contact:null}},this._elements.wrapper=e instanceof Element?e:document.querySelector(e),!this._elements.wrapper)throw new Error("MovingCostCalculator: "+("string"==typeof e?"The selector `"+e+"` didn't match any element.":"The element you provided was undefined"));if(this._elements.wrapper.classList.contains("mcc-wrapper"))throw new Error("MovingCostCalculator: The element has already been initialized.");this._parameters={lang:"en",debug:!1,askForContact:!0,...t,options:{floor:!0,lift:!0,porterageDistance:!0,...t.options}},this.data={addresses:{departure:{value:"",components:{streetNumber:"",route:"",zipCode:"",locality:"",country:"",countryCode:"",department:"",region:""},location:{lat:0,lng:0,placeId:""},options:{floor:0,lift:!1,porterageDistance:0}},arrival:{value:"",components:{streetNumber:"",route:"",zipCode:"",locality:"",country:"",countryCode:"",department:"",region:""},location:{lat:0,lng:0,placeId:""},options:{floor:0,lift:!1,porterageDistance:0}}},volume:0,volumeData:{},contact:""},this._loadDictionary(),this._loadDependencies().then(()=>{this._parameters.debug&&console.log("MovingCostCalculator: DEPENDENCIES LOADED !"),this._build(),this._listen()})}_loadDependencies(){this._parameters.debug&&console.log("MovingCostCalculator: LOADING DEPENDENCIES ...");const e=new Promise(e=>{if("function"==typeof MovingVolumeCalculator)e();else{const t=new Promise(e=>{this._loadResource("script","https://unpkg.com/moving-volume-calculator@0.4.2/MovingVolumeCalculator.min.js",()=>{this._parameters.debug&&console.log("DEPENDENCIES: MovingVolumeCalculator script LOADED !"),e()})}),s=new Promise(e=>{this._loadResource("style","https://unpkg.com/moving-volume-calculator@0.4.2/MovingVolumeCalculator.min.css",()=>{this._parameters.debug&&console.log("DEPENDENCIES: MovingVolumeCalculator style LOADED !"),e()})});Promise.all([t,s]).then(()=>{e()})}}),t=new Promise(e=>{if(window.google&&window.google.maps)e();else{const t=document.querySelectorAll('script[src^="https://maps.googleapis.com"]');t.length&&(t.forEach(e=>{e.remove()}),google&&Reflect.deleteProperty(google,"maps"));const s=document.createElement("script");if(!this._parameters.googleAPIKey)throw new Error("MovingCostCalculator: You didn't provide your Google Maps API key. Please either pass it via the options' googleAPIKey attribute OR import the Google Maps API script on your own.");s.src="https://maps.googleapis.com/maps/api/js?libraries=places&key="+this._parameters.googleAPIKey+"&language="+this._parameters.lang+"&callback=__mccGmapApiLoader",window.__mccGmapApiLoader=()=>{this._parameters.debug&&console.log("DEPENDENCIES: Google Maps API script LOADED !"),e()},document.querySelector("head").appendChild(s)}}),s=new Promise(e=>{if("function"==typeof AddressSearch)e();else{const t=new Promise(e=>{this._loadResource("script","https://unpkg.com/address-searcher@1.11.1/address-search.min.js",()=>{this._parameters.debug&&console.log("DEPENDENCIES: AddressSearch script LOADED !"),e()})}),s=new Promise(e=>{this._loadResource("style","https://unpkg.com/address-searcher@1.11.1/address-search.min.css",()=>{this._parameters.debug&&console.log("DEPENDENCIES: AddressSearch style LOADED !"),e()})});Promise.all([t,s]).then(()=>{e()})}}),a=new Promise(e=>{if("function"==typeof AjaxSender)e();else{const t=new Promise(e=>{this._loadResource("script","https://unpkg.com/ajax-sender@1.2.2/AjaxSender.min.js",()=>{this._parameters.debug&&console.log("DEPENDENCIES: AjaxSender script LOADED !"),e()})});t.then(()=>{e()})}}),r=new Promise(e=>{if("object"==typeof SlickLoader)e();else{const t=new Promise(e=>{this._loadResource("script","https://unpkg.com/slick-loader@1.1.24/slick-loader.min.js",()=>{this._parameters.debug&&console.log("DEPENDENCIES: SlickLoader script LOADED !"),e()})}),s=new Promise(e=>{this._loadResource("style","https://unpkg.com/slick-loader@1.1.24/slick-loader.min.css",()=>{this._parameters.debug&&console.log("DEPENDENCIES: SlickLoader style LOADED !"),e()})});Promise.all([t,s]).then(()=>{e()})}});return Promise.all([e,t,s,a,r])}_loadResource(e,t,s){const[a]=document.getElementsByTagName("head");if("script"==e){const e=document.createElement("script");e.src=t,e.onload=s,a.appendChild(e)}else{const e=document.createElement("link");e.href=t,e.rel="stylesheet",e.type="text/css",e.onload=s,a.appendChild(e)}}_loadDictionary(){this._dictionary={en:{title:"Estimate your moving cost",addressesTitle:"Your addresses",volumeTitle:"Your volume",estimationsTitle:"Estimations",departureAddress:"Departure address",arrivalAddress:"Arrival address",moreAddressOptions:"More options",lessAddressOptions:"Less options",floor:"Floor",lift:"Lift",porterageDistance:"Porterage Distance",yes:"Yes",no:"No",enterContact:"Please enter your mail to access your estimations",loading:"Loading ...",next:"Next",priceFrom:"From",services:"Services"},fr:{title:"Estimez le coût de votre déménagement",addressesTitle:"Vos addresses",volumeTitle:"Votre volume",estimationsTitle:"Estimations",departureAddress:"Adresse de départ",arrivalAddress:"Adresse d'arrivée",moreAddressOptions:"Plus d'options",lessAddressOptions:"Moins d'options",floor:"Etage",lift:"Ascenseur",porterageDistance:"Distance de portage",yes:"Oui",no:"Non",enterContact:"Veuillez renseigner votre adresse mail pour accéder à vos estimations",loading:"Chargement ...",next:"Suivant",priceFrom:"A partir de",services:"Services"}},this._dictionary=Object.assign(this._dictionary,this._parameters.dictionary||{})}_build(){this._elements.wrapper.classList.add("mcc-wrapper","mcc-stylized");const e=document.createElement("h3");e.innerHTML=this._translated().title,this._elements.wrapper.appendChild(e),this._buildAddresses(),this._buildVolume(),this._parameters.askForContact&&this._buildContact(),this._buildLoader(),this._buildEstimations()}_buildAddresses(){let e=document.createElement("h4"),t=document.createElement("section"),s=document.createElement("p"),a=document.createElement("input"),r=document.createElement("div");t=document.createElement("section"),t.classList.add("mcc-addresses"),this._elements.wrapper.appendChild(t),e=document.createElement("h4"),e.innerHTML=this._translated().addressesTitle,t.appendChild(e);const o=document.createElement("div");if(t.appendChild(o),o.appendChild(r),s.innerText=this._translated().departureAddress,r.appendChild(s),a.classList.add("mcc-address","mcc-departure"),r.appendChild(a),this._elements.departure.address=new AddressSearch(a),Object.values(this._parameters.options).some(e=>1==e)){s=document.createElement("p"),s.classList.add("mcc-address-options-toggler"),r.appendChild(s),this._elements.departure.optionToggler=document.createElement("a"),this._elements.departure.optionToggler.classList.add("mcc-address-options-enable"),this._elements.departure.optionToggler.innerText=this._translated().moreAddressOptions,this._elements.departure.optionToggler.href="",s.appendChild(this._elements.departure.optionToggler);const e=document.createElement("div");e.classList.add("mcc-address-options","mcc-departure","mcc-hidden"),r.appendChild(e),this._buildAddressOptions(this._elements.departure.options,e)}if(r=document.createElement("div"),o.appendChild(r),s=document.createElement("p"),s.innerText=this._translated().arrivalAddress,r.appendChild(s),a=document.createElement("input"),a.classList.add("mcc-address","mcc-arrival"),r.appendChild(a),this._elements.arrival.address=new AddressSearch(a),Object.values(this._parameters.options).some(e=>1==e)){s=document.createElement("p"),s.classList.add("mcc-address-options-toggler"),r.appendChild(s),this._elements.arrival.optionToggler=document.createElement("a"),this._elements.arrival.optionToggler.classList.add("mcc-address-options-enable"),this._elements.arrival.optionToggler.innerText=this._translated().moreAddressOptions,this._elements.arrival.optionToggler.href="",s.appendChild(this._elements.arrival.optionToggler);const e=document.createElement("div");e.classList.add("mcc-address-options","mcc-arrival","mcc-hidden"),r.appendChild(e),this._buildAddressOptions(this._elements.arrival.options,e)}s=document.createElement("p"),s.classList.add("mcc-validation"),this._elements.validation.addresses=document.createElement("button"),this._elements.validation.addresses.disabled=!0,this._elements.validation.addresses.innerHTML=this._translated().next,s.appendChild(this._elements.validation.addresses),t.appendChild(s)}_buildAddressOptions(e,t){Object.keys(e).forEach(s=>{if(this._parameters.options[s]){const a=document.createElement("p"),r=document.createElement("span");if(a.classList.add("mcc-address-option-"+s),["lift"].includes(s)){e[s]=document.createElement("select");let t=document.createElement("option");t.value="false",t.innerText=this._translated().no,e[s].appendChild(t),t=document.createElement("option"),t.value="true",t.innerText=this._translated().yes,e[s].appendChild(t)}else e[s]=document.createElement("input"),"porterageDistance"==s&&e[s].setAttribute("placeholder","0 m");r.innerText=this._translated()[s],a.appendChild(r),a.appendChild(e[s]),t.appendChild(a)}})}_buildVolume(){const e=document.createElement("h4"),t=document.createElement("section"),s=document.createElement("div");t.classList.add("mcc-volume","mcc-hidden"),this._elements.wrapper.appendChild(t),e.innerHTML=this._translated().volumeTitle,t.appendChild(e),t.appendChild(s),this._elements.volume=new MovingVolumeCalculator(s,{lang:this._parameters.lang,rooms:this._parameters.options.rooms})}_buildContact(){if(this._parameters.askForContact){const e=document.createElement("h4"),t=document.createElement("section"),s=document.createElement("p");t.classList.add("mcc-contact","mcc-hidden"),this._elements.wrapper.appendChild(t),e.innerHTML=this._translated().enterContact,t.appendChild(e),this._elements.contact=document.createElement("input"),t.appendChild(this._elements.contact),s.classList.add("mcc-validation"),this._elements.validation.contact=document.createElement("button"),this._elements.validation.contact.disabled=!0,this._elements.validation.contact.innerHTML=this._translated().next,s.appendChild(this._elements.validation.contact),t.appendChild(s)}}_buildLoader(){const e=document.createElement("section");e.classList.add("mcc-loader","mcc-hidden"),this._elements.wrapper.appendChild(e)}_buildEstimations(){let e=document.createElement("title"),t=document.createElement("section");t=document.createElement("section"),t.classList.add("mcc-estimations","mcc-hidden"),this._elements.wrapper.appendChild(t),e=document.createElement("h4"),e.innerHTML=this._translated().estimationsTitle,t.appendChild(e)}_listen(){this._createDepartureAddressListeners(),this._createArrivalAddressListeners(),this._elements.validation.addresses.addEventListener("click",()=>{this._elements.wrapper.querySelector("section.mcc-volume").classList.remove("mcc-hidden"),this._elements.wrapper.querySelector("section.mcc-addresses").classList.add("mcc-hidden")}),this._elements.volume.onChange(e=>{this._elements.volume.isValid()&&(this.data.volume=e,this.data.volumeData=this._elements.volume.data)}).onValidate(e=>{this.data.volume=this._elements.volume.volume,this.data.volumeData=e,this._parameters.askForContact?(this._elements.wrapper.querySelector("section.mcc-contact").classList.remove("mcc-hidden"),this._elements.wrapper.querySelector("section.mcc-volume").classList.add("mcc-hidden")):(this._toggleLoader(!0),this._elements.wrapper.querySelector("section.mcc-loader").classList.remove("mcc-hidden"),this._elements.wrapper.querySelector("section.mcc-volume").classList.add("mcc-hidden"),this.validate())}),this._parameters.askForContact&&(this._elements.contact.addEventListener("input",()=>{const e=/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;e.test(this._elements.contact.value)?(this.data.contact=this._elements.contact.value,this._elements.validation.contact.disabled=!1):this._elements.validation.contact.disabled=!0}),this._elements.contact.addEventListener("keyup",e=>{"Enter"==e.key&&(this._toggleLoader(!0),this._elements.wrapper.querySelector("section.mcc-loader").classList.remove("mcc-hidden"),this._elements.wrapper.querySelector("section.mcc-contact").classList.add("mcc-hidden"),this.validate())}),this._elements.validation.contact.addEventListener("click",()=>{this._toggleLoader(!0),this._elements.wrapper.querySelector("section.mcc-loader").classList.remove("mcc-hidden"),this._elements.wrapper.querySelector("section.mcc-contact").classList.add("mcc-hidden"),this.validate()}))}_createDepartureAddressListeners(){this._elements.departure.address.onSelect(e=>{this.data.addresses.departure.value=e.formatted_address,this.data.addresses.departure.components.streetNumber=this._getAddressComponent(e,"street_number"),this.data.addresses.departure.components.route=this._getAddressComponent(e,"route"),this.data.addresses.departure.components.zipCode=this._getAddressComponent(e,"postal_code"),this.data.addresses.departure.components.locality=this._getAddressComponent(e,"locality"),this.data.addresses.departure.components.country=this._getAddressComponent(e,"country"),this.data.addresses.departure.components.countryCode=this._getAddressComponent(e,"country",!0),this.data.addresses.departure.components.department=this._getAddressComponent(e,"administrative_area_level_2"),this.data.addresses.departure.components.region=this._getAddressComponent(e,"administrative_area_level_1"),this.data.addresses.departure.location.lat=e.geometry.location.lat(),this.data.addresses.departure.location.lng=e.geometry.location.lng(),this.data.addresses.departure.location.placeId=e.place_id,this._elements.departure.address.value.formatted_address&&this._elements.arrival.address.value.formatted_address?this._elements.validation.addresses.disabled=!1:this._elements.validation.addresses.disabled=!0}),Object.values(this._parameters.options).some(e=>1==e)&&(this._elements.departure.optionToggler.addEventListener("click",e=>{e.preventDefault(),this._elements.departure.optionToggler.classList.contains("mcc-address-options-enable")?(this._elements.departure.optionToggler.classList.remove("mcc-address-options-enable"),this._elements.departure.optionToggler.classList.add("mcc-address-options-disable"),this._elements.departure.optionToggler.innerText=this._translated().lessAddressOptions,this._elements.wrapper.querySelector(".mcc-departure.mcc-address-options").classList.remove("mcc-hidden")):(this._elements.departure.optionToggler.classList.remove("mcc-address-options-disable"),this._elements.departure.optionToggler.classList.add("mcc-address-options-enable"),this._elements.departure.optionToggler.innerText=this._translated().moreAddressOptions,this._elements.wrapper.querySelector(".mcc-departure.mcc-address-options").classList.add("mcc-hidden"))}),this._parameters.options.floor&&this._elements.departure.options.floor.addEventListener("change",()=>{this.data.addresses.departure.options.floor=+this._elements.departure.options.floor.value}),this._parameters.options.lift&&this._elements.departure.options.lift.addEventListener("change",()=>{this.data.addresses.departure.options.lift="true"==this._elements.departure.options.lift.value}),this._parameters.options.porterageDistance&&this._elements.departure.options.porterageDistance.addEventListener("change",()=>{this.data.addresses.departure.options.porterageDistance=+this._elements.departure.options.porterageDistance.value}))}_createArrivalAddressListeners(){this._elements.arrival.address.onSelect(e=>{this.data.addresses.arrival.value=e.formatted_address,this.data.addresses.arrival.components.streetNumber=this._getAddressComponent(e,"street_number"),this.data.addresses.arrival.components.route=this._getAddressComponent(e,"route"),this.data.addresses.arrival.components.zipCode=this._getAddressComponent(e,"postal_code"),this.data.addresses.arrival.components.locality=this._getAddressComponent(e,"locality"),this.data.addresses.arrival.components.country=this._getAddressComponent(e,"country"),this.data.addresses.arrival.components.countryCode=this._getAddressComponent(e,"country",!0),this.data.addresses.arrival.components.department=this._getAddressComponent(e,"administrative_area_level_2"),this.data.addresses.arrival.components.region=this._getAddressComponent(e,"administrative_area_level_1"),this.data.addresses.arrival.location.lat=e.geometry.location.lat(),this.data.addresses.arrival.location.lng=e.geometry.location.lng(),this.data.addresses.arrival.location.placeId=e.place_id,this._elements.departure.address.value.formatted_address&&this._elements.arrival.address.value.formatted_address?this._elements.validation.addresses.disabled=!1:this._elements.validation.addresses.disabled=!0}),Object.values(this._parameters.options).some(e=>1==e)&&(this._elements.arrival.optionToggler.addEventListener("click",e=>{e.preventDefault(),this._elements.arrival.optionToggler.classList.contains("mcc-address-options-enable")?(this._elements.arrival.optionToggler.classList.remove("mcc-address-options-enable"),this._elements.arrival.optionToggler.classList.add("mcc-address-options-disable"),this._elements.arrival.optionToggler.innerText=this._translated().lessAddressOptions,this._elements.wrapper.querySelector(".mcc-arrival.mcc-address-options").classList.remove("mcc-hidden")):(this._elements.arrival.optionToggler.classList.remove("mcc-address-options-disable"),this._elements.arrival.optionToggler.classList.add("mcc-address-options-enable"),this._elements.arrival.optionToggler.innerText=this._translated().moreAddressOptions,this._elements.wrapper.querySelector(".mcc-arrival.mcc-address-options").classList.add("mcc-hidden"))}),this._parameters.options.floor&&this._elements.arrival.options.floor.addEventListener("change",()=>{this.data.addresses.arrival.options.floor=+this._elements.arrival.options.floor.value}),this._parameters.options.lift&&this._elements.arrival.options.lift.addEventListener("change",()=>{this.data.addresses.arrival.options.lift="true"==this._elements.arrival.options.lift.value}),this._parameters.options.porterageDistance&&this._elements.arrival.options.porterageDistance.addEventListener("change",()=>{this.data.addresses.arrival.options.porterageDistance=+this._elements.arrival.options.porterageDistance.value}))}_toggleLoader(e){this._elements.loader||(this._elements.loader=new Slickloader(this._elements.wrapper.querySelector(".mcc-loader")));const t=void 0===e?!this._elements.loader.element.classList.contains("active"):e;t?this._elements.loader.enable():this._elements.loader.disable()}_translated(){return this._dictionary[this._parameters.lang]}_getAddressComponent(e,t,s){const a=e.address_components.find(e=>e.types.includes(t));return a?s?a.short_name:a.long_name:""}setLang(e){return this._parameters.lang=e||"en",this}validate(){return this._parameters.remoteCalculator?new AjaxSender(this._parameters.remoteCalculator,{headers:{"Content-Type":"application/json"},data:this.data,method:"POST",responseType:"json",load:e=>{this._elements.wrapper.querySelector("section.mcc-estimations").innerHTML=`\n\t\t\t\t\t\t<ul>\n\t\t\t\t\t\t\t${e.map(e=>`<li>\n\t\t\t\t\t\t\t\t<p class="mcc-offer-name">${e.name}</p>\n\t\t\t\t\t\t\t\t<p>${this._translated().priceFrom} <span class="mcc-offer-price">${e.price} €</span></p>\n\t\t\t\t\t\t\t\t<p>${this._translated().services}:</p>\n\t\t\t\t\t\t\t\t${e.services.map(e=>`\n\t\t\t\t\t\t\t\t\t<p class="mcc-offer-service">${e}</p>\n\t\t\t\t\t\t\t\t`).join("")}\n\t\t\t\t\t\t\t</li>`).join("")}\n\t\t\t\t\t\t</ul>\n\t\t\t\t\t`,this._elements.wrapper.querySelector("section.mcc-estimations").innerHTML+=`\n\t\t\t\t\t\t<div class="mcc-logs">${e[0].logs.map(e=>`${e}<br />`).join("")}</div>\n\t\t\t\t\t`;const t=e=>{76==e.keyCode&&this._elements.wrapper.querySelector(".mcc-logs").classList.toggle("debug")};window.removeEventListener("keyup",t),window.addEventListener("keyup",t),this._elements.wrapper.querySelector("section.mcc-loader").classList.add("mcc-hidden"),this._elements.wrapper.querySelector("section.mcc-estimations").classList.remove("mcc-hidden")},error:e=>{console.log(e),console.log(e.response)}}):(this._elements.wrapper.querySelector("section.mcc-loader").classList.add("mcc-hidden"),this._elements.wrapper.querySelector("section.mcc-estimations").classList.remove("mcc-hidden")),this}destroy(){this._elements.wrapper.innerHTML="",this._elements.wrapper.classList.remove("mcc-wrapper")}static destroy(e){const t=document.querySelector(e);t&&t.classList.contains("mcc-wrapper")&&(t.innerHTML="",t.classList.remove("mcc-wrapper"))}}