google-address-autocomplete
Advanced tools
Comparing version 1.0.0 to 1.0.2
@@ -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 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}}(),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.onreadystatechange=function(){e.init(),e.element.addEventListener("focus",e.getUsersLocation)}}},{key:"init",value:function(e){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={streetNumber:"",streetName:"",cityName:"",stateAbbr:"",zipCode:""},s=0;s<a.length;s++){var r=a[s].types[0];if(e[r])switch(r){case"street_number":o.streetNumber=a[s].long_name;break;case"route":o.streetName=a[s].long_name;break;case"locality":o.cityName=a[s].long_name;break;case"administrative_area_level_1":o.stateAbbr=a[s].short_name,o.state=a[s].long_name;break;case"postal_code":o.zipCode=a[s].long_name;break;case"country":o.countryAbbr=a[s].short_name,o.country=a[s].long_name}}var c=Object.assign({},o,{formattedAddress:n});this.callback&&this.callback(c)}},{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())})}}]),e}();module.exports=AddressAutocomplete; | ||
"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.onreadystatechange=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={streetNumber:"",streetName:"",cityName:"",stateAbbr:"",zipCode:""},s=0;s<a.length;s++){var r=a[s].types[0];if(e[r])switch(r){case"street_number":n.streetNumber=a[s].long_name;break;case"route":n.streetName=a[s].long_name;break;case"locality":n.cityName=a[s].long_name;break;case"administrative_area_level_1":n.stateAbbr=a[s].short_name,n.state=a[s].long_name;break;case"postal_code":n.zipCode=a[s].long_name;break;case"country":n.countryAbbr=a[s].short_name,n.country=a[s].long_name}}var c=Object.assign({},n,{formattedAddress:o});this.callback(c)}},{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; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "google-address-autocomplete", | ||
"version": "1.0.0", | ||
"version": "1.0.2", | ||
"description": "This is a package that gives an input address autocomplete by using Google's lookup.", | ||
"keywords": [ | ||
"autocomplete", | ||
"address", | ||
"google", | ||
"autocomplete", | ||
"places" | ||
], | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "./node_modules/rollup/bin/rollup --config rollup.config.js", | ||
"start": "./node_modules/rollup/bin/rollup --config rollup.config.js --watch" | ||
}, | ||
"author": "deric.cain@gmail.com", | ||
@@ -15,6 +18,28 @@ "license": "MIT", | ||
"babel-preset-env": "^1.6.0", | ||
"eslint": "^4.8.0", | ||
"eslint-config-airbnb-base": "^12.0.2", | ||
"eslint-config-prettier": "^2.6.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-prettier": "^2.3.1", | ||
"husky": "^0.14.3", | ||
"prettier": "^1.7.4", | ||
"rollup": "^0.50.0", | ||
"rollup-plugin-babel": "^3.0.2", | ||
"rollup-plugin-uglify": "^2.0.1" | ||
}, | ||
"scripts": { | ||
"build": "./node_modules/rollup/bin/rollup --config rollup.config.js", | ||
"start": "./node_modules/rollup/bin/rollup --config rollup.config.js --watch", | ||
"start:contrib": "./node_modules/rollup/bin/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" | ||
] | ||
} | ||
} |
@@ -30,3 +30,3 @@ # Google Address Autocomplete | ||
<script src="https://maps.googleapis.com/maps/api/js?key=YOU_GOOGLE_API_KEY_GOES_HERE&libraries=places"></script> | ||
<script src="js/your-compiled-script-here.js"></script> | ||
</body> | ||
@@ -59,5 +59,5 @@ </html> | ||
## Contributing | ||
This is something that I hacked at really quickly and can be made better. Please, if you would like to contribute, do not hesitate. I am very open to making this a better package. | ||
See [the contributing guide](CONTRIBUTING.ms) | ||
## Issues | ||
If you find an issue, submit it and let's fix it! |
@@ -0,8 +1,22 @@ | ||
/** | ||
* @export | ||
* @class AddressAutocomplete | ||
* @author Deric Cain <deric.cain@gmail.com> | ||
*/ | ||
export default class AddressAutocomplete { | ||
/** | ||
* Creates an instance of 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 | ||
* @throws Error - If we don't have a valid element | ||
* @memberof AddressAutocomplete | ||
*/ | ||
constructor(element, callback) { | ||
// Can take element as '.class-name' or '#id-name' | ||
this.element = document.querySelector(element); | ||
// If we do not find the element, then we need to throw an error | ||
if (!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'.` | ||
"The element you specified is not a valid element. You should attach an input using a class '.some-class' or an ID '#some-id'." | ||
); | ||
@@ -12,2 +26,4 @@ } | ||
this.callback = callback; | ||
// We are binding the context of 'this' to this class instance | ||
this.extractAddress = this.extractAddress.bind(this); | ||
@@ -18,6 +34,11 @@ this.getUsersLocation = this.getUsersLocation.bind(this); | ||
// When the document is ready, we need to fire everything off. | ||
/** | ||
* This takes care of make everything happen | ||
* | ||
* @memberof AddressAutocomplete | ||
*/ | ||
handle() { | ||
// When the document is ready, we need to fire everything off. | ||
document.onreadystatechange = () => { | ||
this.init(); | ||
this.initializeAutocomplete(); | ||
this.element.addEventListener('focus', this.getUsersLocation); | ||
@@ -27,3 +48,8 @@ }; | ||
init(element) { | ||
/** | ||
* This method takes care of getting the autocomplete up and running | ||
* | ||
* @memberof AddressAutocomplete | ||
*/ | ||
initializeAutocomplete() { | ||
this.autocomplete = new google.maps.places.Autocomplete(this.element, { | ||
@@ -35,2 +61,7 @@ types: ['geocode'], | ||
/** | ||
* Here, we are taking care of getting the address from the results. | ||
* | ||
* @memberof AddressAutocomplete | ||
*/ | ||
extractAddress() { | ||
@@ -58,2 +89,3 @@ const componentForm = { | ||
// Need to loop over the results and create a friendly object | ||
for (let i = 0; i < address_components.length; i++) { | ||
@@ -89,8 +121,17 @@ const addressType = address_components[i].types[0]; | ||
const result = Object.assign({}, addressObject, {formattedAddress: formatted_address}); | ||
const result = Object.assign({}, addressObject, { | ||
formattedAddress: formatted_address, | ||
}); | ||
this.callback && this.callback(result); | ||
// This is where we check for the callback and then call it, passing our resutls | ||
this.callback(result); | ||
} | ||
/** | ||
* This will help us narrow down the results of the autocomplete to a user's location | ||
* | ||
* @memberof AddressAutocomplete | ||
*/ | ||
getUsersLocation() { | ||
// Using feature detection to make sure the browser supports geolocation | ||
if (navigator.geolocation) { | ||
@@ -97,0 +138,0 @@ navigator.geolocation.getCurrentPosition(position => { |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
24190
12
332
12
1