-
Notifications
You must be signed in to change notification settings - Fork 2
/
autocity.min.js
10 lines (10 loc) · 1.91 KB
/
autocity.min.js
1
2
3
4
5
6
7
8
9
10
/**
* AutoCity 0.0.1
* An autocomplete input field to search city based on Google Map Places
*
* @author Tu Truong (<https://github.com/lufutu>)
* @version 0.0.1
* @license MIT
* @see <https://github.com/lufutu/AutoCity>
*/
"use strict";if(!(typeof window.google==="object"&&window.google.maps)){throw"Google Maps API is required. Please register the following JavaScript library https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places."}var extend_object=function(b,c){var a;if(b===c){return b}for(a in c){b[a]=c[a]}return b};var insert_after=function(b,a){a.parentNode.insertBefore(b,a.nextSibling)};var getElementsByClassName=function(c){var b,a=c.replace(".","");b=document.getElementsByClassName(a)[0];return b};var getElementById=function(b){var a,b=b.replace("#","");a=document.getElementById(b);return a};var getElementByIdentifier=function(a){if(a.indexOf("#")>-1){return getElementById(a)}else{return getElementsByClassName(a)}};var AutoCity=function(d,f){var b,e,c;b={element:".autocity",element_custom_class:"form-control",place_holder:null,data:"string",country:"all"};b=extend_object(b,d);if(getElementByIdentifier(b.element)==="undefined"||getElementByIdentifier(b.element)===null){throw ("Must set 'autocity' class for at least one field or set your own field class")}else{e=getElementByIdentifier(b.element)}e.type="hidden";var a=document.createElement("input");a.type="text";a.className=b.element_custom_class;if(b.place_holder!=null){if(b.place_holder.trim()!=""){a.placeholder=b.place_holder}}insert_after(a,e);if(b.country=="all"){c=new google.maps.places.Autocomplete((a),{types:["(cities)"]})}else{c=new google.maps.places.Autocomplete((a),{types:["(cities)"],componentRestrictions:{country:b.country}})}google.maps.event.addListener(c,"place_changed",function(){var g=c.getPlace();if(b.data=="object"){e.value=JSON.stringify(g)}else{e.value=g.name}if(typeof f=="function"){f.call(this,g)}})};