google-address-autocomplete
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -1,1 +0,1 @@ | ||
var AddressAutocomplete=function(){"use strict";function e(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var t=function(){function e(e,t){for(var a=0;a<t.length;a++){var n=t[a];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,a,n){return a&&e(t.prototype,a),n&&e(t,n),t}}();return function(){function a(t,n){if(e(this,a),this.element=document.querySelector(t),!this.element)throw new Error("The element you specified is not a valid element. You should attach an input using a class '.some-class' or an ID '#some-id'.");this.callback=n,this.extractAddress=this.extractAddress.bind(this),this.getUsersLocation=this.getUsersLocation.bind(this),this.handle()}return t(a,[{key:"handle",value:function(){var e=this;document.addEventListener("readystatechange",function(){e.initializeAutocomplete(),e.element.addEventListener("focus",e.getUsersLocation)})}},{key:"initializeAutocomplete",value:function(){this.autocomplete=new google.maps.places.Autocomplete(this.element,{types:["geocode"]}),this.autocomplete.addListener("place_changed",this.extractAddress)}},{key:"extractAddress",value:function(){for(var e={street_number:"short_name",route:"long_name",locality:"long_name",administrative_area_level_1:"short_name",country:"long_name",postal_code:"short_name"},t=this.autocomplete.getPlace(),a=t.address_components,n=t.formatted_address,o=t.geometry.location,r=o.lat,s=o.lng,i={streetNumber:"",streetName:"",cityName:"",stateAbbr:"",zipCode:"",coordinates:{lat:r(),lng:s()}},c=0;c<a.length;c++){var l=a[c].types[0];if(e[l])switch(l){case"street_number":i.streetNumber=a[c].long_name;break;case"route":i.streetName=a[c].long_name;break;case"locality":i.cityName=a[c].long_name;break;case"administrative_area_level_1":i.stateAbbr=a[c].short_name,i.state=a[c].long_name;break;case"postal_code":i.zipCode=a[c].long_name;break;case"country":i.countryAbbr=a[c].short_name,i.country=a[c].long_name}}var u=Object.assign({},i,{formattedAddress:n});this.callback(u,t)}},{key:"getUsersLocation",value:function(){var e=this;navigator.geolocation&&navigator.geolocation.getCurrentPosition(function(t){var a={lat:t.coords.latitude,lng:t.coords.longitude},n=new google.maps.Circle({center:a,radius:t.coords.accuracy});e.autocomplete.setBounds(n.getBounds())})}}]),a}()}(); | ||
var AddressAutocomplete=function(){"use strict";var e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},t=(function(){function e(e){this.value=e}function t(t){function o(r,a){try{var i=t[r](a),s=i.value;s instanceof e?Promise.resolve(s.value).then(function(e){o("next",e)},function(e){o("throw",e)}):n(i.done?"return":"normal",i.value)}catch(e){n("throw",e)}}function n(e,t){switch(e){case"return":r.resolve({value:t,done:!0});break;case"throw":r.reject(t);break;default:r.resolve({value:t,done:!1})}(r=r.next)?o(r.key,r.arg):a=null}var r,a;this._invoke=function(e,t){return new Promise(function(n,i){var s={key:e,arg:t,resolve:n,reject:i,next:null};a?a=a.next=s:(r=a=s,o(e,t))})},"function"!=typeof t.return&&(this.return=void 0)}"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)}}(),function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}),o=function(){function e(e,t){for(var o=0;o<t.length;o++){var n=t[o];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,o,n){return o&&e(t.prototype,o),n&&e(t,n),t}}();return function(){function n(o,r){var a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if(t(this,n),this.element=document.querySelector(o),!this.element)throw new Error("The element you specified is not a valid element. You should attach an input using a class '.some-class' or an ID '#some-id'.");var i={types:["geocode"]};if("function"==typeof r)this.callback=r,this.options=i;else{if("object"!==(void 0===r?"undefined":e(r)))throw new Error('To be able to use extra options, the type of the second parameter must be "object" and the type of the third parameter must be "function".');this.options=Object.assign({},i,r),this.callback=a}this.extractAddress=this.extractAddress.bind(this),this.getUsersLocation=this.getUsersLocation.bind(this),this.handle()}return o(n,[{key:"handle",value:function(){var e=this;document.addEventListener("readystatechange",function(){e.initializeAutocomplete(),e.element.addEventListener("focus",e.getUsersLocation)})}},{key:"initializeAutocomplete",value:function(){this.autocomplete=new google.maps.places.Autocomplete(this.element,this.options),this.autocomplete.addListener("place_changed",this.extractAddress)}},{key:"extractAddress",value:function(){for(var e={street_number:"short_name",route:"long_name",locality:"long_name",administrative_area_level_1:"short_name",country:"long_name",postal_code:"short_name"},t=this.autocomplete.getPlace(),o=t.address_components,n=t.formatted_address,r=t.geometry.location,a=r.lat,i=r.lng,s={streetNumber:"",streetName:"",cityName:"",stateAbbr:"",zipCode:"",coordinates:{lat:a(),lng:i()}},c=0;c<o.length;c++){var l=o[c].types[0];if(e[l])switch(l){case"street_number":s.streetNumber=o[c].long_name;break;case"route":s.streetName=o[c].long_name;break;case"locality":s.cityName=o[c].long_name;break;case"administrative_area_level_1":s.stateAbbr=o[c].short_name,s.state=o[c].long_name;break;case"postal_code":s.zipCode=o[c].long_name;break;case"country":s.countryAbbr=o[c].short_name,s.country=o[c].long_name}}var u=Object.assign({},s,{formattedAddress:n});this.callback(u,t)}},{key:"getUsersLocation",value:function(){var e=this;navigator.geolocation&&navigator.geolocation.getCurrentPosition(function(t){var o={lat:t.coords.latitude,lng:t.coords.longitude},n=new google.maps.Circle({center:o,radius:t.coords.accuracy});e.autocomplete.setBounds(n.getBounds())})}}]),n}()}(); |
@@ -1,2 +0,2 @@ | ||
"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var a=0;a<t.length;a++){var o=t[a];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,a,o){return a&&e(t.prototype,a),o&&e(t,o),t}}(),AddressAutocomplete=function(){function e(t,a){if(_classCallCheck(this,e),this.element=document.querySelector(t),!this.element)throw new Error("The element you specified is not a valid element. You should attach an input using a class '.some-class' or an ID '#some-id'.");this.callback=a,this.extractAddress=this.extractAddress.bind(this),this.getUsersLocation=this.getUsersLocation.bind(this),this.handle()}return _createClass(e,[{key:"handle",value:function(){var e=this;document.addEventListener("readystatechange",function(){e.initializeAutocomplete(),e.element.addEventListener("focus",e.getUsersLocation)})}},{key:"initializeAutocomplete",value:function(){this.autocomplete=new google.maps.places.Autocomplete(this.element,{types:["geocode"]}),this.autocomplete.addListener("place_changed",this.extractAddress)}},{key:"extractAddress",value:function(){for(var e={street_number:"short_name",route:"long_name",locality:"long_name",administrative_area_level_1:"short_name",country:"long_name",postal_code:"short_name"},t=this.autocomplete.getPlace(),a=t.address_components,o=t.formatted_address,n=t.geometry.location,s=n.lat,r=n.lng,c={streetNumber:"",streetName:"",cityName:"",stateAbbr:"",zipCode:"",coordinates:{lat:s(),lng:r()}},i=0;i<a.length;i++){var l=a[i].types[0];if(e[l])switch(l){case"street_number":c.streetNumber=a[i].long_name;break;case"route":c.streetName=a[i].long_name;break;case"locality":c.cityName=a[i].long_name;break;case"administrative_area_level_1":c.stateAbbr=a[i].short_name,c.state=a[i].long_name;break;case"postal_code":c.zipCode=a[i].long_name;break;case"country":c.countryAbbr=a[i].short_name,c.country=a[i].long_name}}var u=Object.assign({},c,{formattedAddress:o});this.callback(u,t)}},{key:"getUsersLocation",value:function(){var e=this;navigator.geolocation&&navigator.geolocation.getCurrentPosition(function(t){var a={lat:t.coords.latitude,lng:t.coords.longitude},o=new google.maps.Circle({center:a,radius:t.coords.accuracy});e.autocomplete.setBounds(o.getBounds())})}}]),e}();module.exports=AddressAutocomplete; | ||
"use strict";var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},asyncGenerator=function(){function e(e){this.value=e}function t(t){function n(r,a){try{var s=t[r](a),i=s.value;i instanceof e?Promise.resolve(i.value).then(function(e){n("next",e)},function(e){n("throw",e)}):o(s.done?"return":"normal",s.value)}catch(e){o("throw",e)}}function o(e,t){switch(e){case"return":r.resolve({value:t,done:!0});break;case"throw":r.reject(t);break;default:r.resolve({value:t,done:!1})}(r=r.next)?n(r.key,r.arg):a=null}var r,a;this._invoke=function(e,t){return new Promise(function(o,s){var i={key:e,arg:t,resolve:o,reject:s,next:null};a?a=a.next=i:(r=a=i,n(e,t))})},"function"!=typeof t.return&&(this.return=void 0)}return"function"==typeof Symbol&&Symbol.asyncIterator&&(t.prototype[Symbol.asyncIterator]=function(){return this}),t.prototype.next=function(e){return this._invoke("next",e)},t.prototype.throw=function(e){return this._invoke("throw",e)},t.prototype.return=function(e){return this._invoke("return",e)},{wrap:function(e){return function(){return new t(e.apply(this,arguments))}},await:function(t){return new e(t)}}}(),classCallCheck=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},createClass=function(){function e(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}return function(t,n,o){return n&&e(t.prototype,n),o&&e(t,o),t}}(),AddressAutocomplete=function(){function e(t,n){var o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if(classCallCheck(this,e),this.element=document.querySelector(t),!this.element)throw new Error("The element you specified is not a valid element. You should attach an input using a class '.some-class' or an ID '#some-id'.");var r={types:["geocode"]};if("function"==typeof n)this.callback=n,this.options=r;else{if("object"!==(void 0===n?"undefined":_typeof(n)))throw new Error('To be able to use extra options, the type of the second parameter must be "object" and the type of the third parameter must be "function".');this.options=Object.assign({},r,n),this.callback=o}this.extractAddress=this.extractAddress.bind(this),this.getUsersLocation=this.getUsersLocation.bind(this),this.handle()}return createClass(e,[{key:"handle",value:function(){var e=this;document.addEventListener("readystatechange",function(){e.initializeAutocomplete(),e.element.addEventListener("focus",e.getUsersLocation)})}},{key:"initializeAutocomplete",value:function(){this.autocomplete=new google.maps.places.Autocomplete(this.element,this.options),this.autocomplete.addListener("place_changed",this.extractAddress)}},{key:"extractAddress",value:function(){for(var e={street_number:"short_name",route:"long_name",locality:"long_name",administrative_area_level_1:"short_name",country:"long_name",postal_code:"short_name"},t=this.autocomplete.getPlace(),n=t.address_components,o=t.formatted_address,r=t.geometry.location,a=r.lat,s=r.lng,i={streetNumber:"",streetName:"",cityName:"",stateAbbr:"",zipCode:"",coordinates:{lat:a(),lng:s()}},c=0;c<n.length;c++){var l=n[c].types[0];if(e[l])switch(l){case"street_number":i.streetNumber=n[c].long_name;break;case"route":i.streetName=n[c].long_name;break;case"locality":i.cityName=n[c].long_name;break;case"administrative_area_level_1":i.stateAbbr=n[c].short_name,i.state=n[c].long_name;break;case"postal_code":i.zipCode=n[c].long_name;break;case"country":i.countryAbbr=n[c].short_name,i.country=n[c].long_name}}var u=Object.assign({},i,{formattedAddress:o});this.callback(u,t)}},{key:"getUsersLocation",value:function(){var e=this;navigator.geolocation&&navigator.geolocation.getCurrentPosition(function(t){var n={lat:t.coords.latitude,lng:t.coords.longitude},o=new google.maps.Circle({center:n,radius:t.coords.accuracy});e.autocomplete.setBounds(o.getBounds())})}}]),e}();module.exports=AddressAutocomplete; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "google-address-autocomplete", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "This is a package that gives an input address autocomplete by using Google's lookup.", | ||
@@ -14,5 +14,9 @@ "keywords": [ | ||
"author": "deric.cain@gmail.com", | ||
"contributors": [ | ||
"Jonathan SARDO <sardoj@gmail.com>" | ||
], | ||
"license": "MIT", | ||
"devDependencies": { | ||
"babel-core": "^6.26.0", | ||
"babel-plugin-external-helpers": "^6.22.0", | ||
"babel-preset-env": "^1.6.0", | ||
@@ -33,14 +37,6 @@ "eslint": "^4.8.0", | ||
"start": "rollup --config rollup.config.js --watch", | ||
"start:contrib": "rollup --config contrib/rollup.config.js --watch", | ||
"test": "eslint ./src", | ||
"lint": "npm test", | ||
"lint:fix": "eslint --fix ./src", | ||
"precommit": "lint-staged" | ||
}, | ||
"lint-staged": { | ||
"*.{js}": [ | ||
"prettier --write", | ||
"git add" | ||
] | ||
"lint:fix": "eslint --fix ./src" | ||
} | ||
} |
@@ -18,3 +18,3 @@ # Google Address Autocomplete | ||
#### 1. As a JS module | ||
You can import the libarary using the `import` keyword. You will need to make sure that you are using something like Weback to transpile your code, because at this time, browsers do not support import modules. | ||
You can import the library using the `import` keyword. You will need to make sure that you are using something like Weback to transpile your code, because at this time, browsers do not support import modules. | ||
@@ -25,3 +25,3 @@ ```JavaScript | ||
// Use a callback here to get the results | ||
new AddressAutocomplete('#my-input-id-or-class-name', results => { | ||
new AddressAutocomplete('#my-input-id-or-class-name', (results) => { | ||
const addressObject = results; | ||
@@ -48,7 +48,18 @@ | ||
> **NOTE:** There is an optional second parameter that will dump out the raw response from Google. So, it | ||
> **NOTE:** There is an optional parameter for adding custom options to the API call. So, it | ||
> look like this: | ||
```JavaScript | ||
new AddressAutocomplete('#my-input-id-or-class-name', results, rawResults); | ||
const options = { | ||
componentRestrictions: {country: "us"} | ||
}; | ||
new AddressAutocomplete('#my-input-id-or-class-name', options, results => {...}); | ||
``` | ||
You can use custom options for example, to restrict the search to a specific area (country, region, city, etc.). | ||
> **NOTE:** There is an optional second parameter in the callback function, that will dump out the raw response from Google. | ||
> So, it look like this: | ||
```JavaScript | ||
new AddressAutocomplete('#my-input-id-or-class-name', (results, rawResults) => {...}); | ||
``` | ||
You can use a callback for both of them to get the results of each. | ||
@@ -98,4 +109,4 @@ | ||
<script src="https://maps.googleapis.com/maps/api/js?key=YOU_GOOGLE_API_KEY_GOES_HERE&libraries=places"></script> | ||
<!-- | ||
You can either point to the node_modules folder or you can copy the file from the node_modules | ||
<!-- | ||
You can either point to the node_modules folder or you can copy the file from the node_modules | ||
and place it in your public directory and use it there. Here, we are pointing to the | ||
@@ -106,3 +117,3 @@ node_modules folder. | ||
<script> | ||
// Now you can use the library as you normally would | ||
@@ -109,0 +120,0 @@ new AddressAutocomplete('#address1', function (result) { |
@@ -1,4 +0,6 @@ | ||
import moduleConfig from './scripts/module-config'; | ||
import onPageAssetConfig from './scripts/on-page-config'; | ||
import config from './scripts/config'; | ||
export default [moduleConfig, onPageAssetConfig]; | ||
const moduleConfig = config(); | ||
const browserConfig = config(false); | ||
export default [moduleConfig, browserConfig]; |
@@ -6,2 +6,3 @@ /** | ||
* @contributor Braunson Yager <braunson@gmail.com> | ||
* @contributor Jonathan Sardo <sardoj@gmail.com> | ||
*/ | ||
@@ -12,7 +13,8 @@ export default class AddressAutocomplete { | ||
* @param {string} element - This should be in the form of either '.address' or '#address' | ||
* @param {function} callback - This callback will have the result passed as the first param | ||
* @param {object|function} optionsOrCallback - This object contains options to add to the API call or a callback | ||
* @param {function|null} callback - This callback will have the result passed as the first param | ||
* @throws Error - If we don't have a valid element | ||
* @memberof AddressAutocomplete | ||
*/ | ||
constructor(element, callback) { | ||
constructor(element, optionsOrCallback, callback = null) { | ||
// Can take element as '.class-name' or '#id-name' | ||
@@ -28,4 +30,26 @@ this.element = document.querySelector(element); | ||
this.callback = callback; | ||
// Default options | ||
const defaultOptions = { | ||
types: ['geocode'], | ||
}; | ||
if (typeof optionsOrCallback === 'function') { | ||
// Compatible with previous versions | ||
// Second parameter is a callback function | ||
this.callback = optionsOrCallback; | ||
// There is not extra options | ||
this.options = defaultOptions; | ||
} else if (typeof optionsOrCallback === 'object') { | ||
// Second parameter is an options list | ||
this.options = Object.assign({}, defaultOptions, optionsOrCallback); | ||
// Third parameter is a callback function | ||
this.callback = callback; | ||
} else { | ||
throw new Error( | ||
'To be able to use extra options, the type of the second parameter must be "object" and the type of the third parameter must be "function".' | ||
); | ||
} | ||
// We are binding the context of 'this' to this class instance | ||
@@ -51,3 +75,3 @@ this.extractAddress = this.extractAddress.bind(this); | ||
/** | ||
* This method takes care of getting the autocomplete up and running | ||
* This method takes care of getting the autocomplete up and running with custom options | ||
* | ||
@@ -57,5 +81,6 @@ * @memberof AddressAutocomplete | ||
initializeAutocomplete() { | ||
this.autocomplete = new google.maps.places.Autocomplete(this.element, { | ||
types: ['geocode'], | ||
}); | ||
this.autocomplete = new google.maps.places.Autocomplete( | ||
this.element, | ||
this.options | ||
); | ||
this.autocomplete.addListener('place_changed', this.extractAddress); | ||
@@ -83,8 +108,3 @@ } | ||
formatted_address, | ||
geometry: { | ||
location: { | ||
lat, | ||
lng | ||
} | ||
} | ||
geometry: { location: { lat, lng } }, | ||
} = resultRaw; | ||
@@ -132,3 +152,3 @@ const addressObject = { | ||
const resultFormatted = Object.assign({}, addressObject, { | ||
formattedAddress: formatted_address | ||
formattedAddress: formatted_address, | ||
}); | ||
@@ -135,0 +155,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
242
140
546366
13
13