Skip to content
This repository has been archived by the owner on Nov 8, 2017. It is now read-only.

Fixed coding standards. #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 66 additions & 65 deletions jquery.geocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// * https://github.com/ubilabs/geocomplete/
// * by Martin Kleppe <kleppe@ubilabs.net>

(function($, window, document, undefined){
(function ($, window, document, undefined) {

// ## Options
// The default options for this plugin.
Expand All @@ -32,6 +32,7 @@
// * `maxZoom` - The maximum zoom level too zoom in after a geocoding response. Default: `16`
// * `types` - An array containing one or more of the supported types for the places request. Default: `['geocode']` See the full list [here](http://code.google.com/apis/maps/documentation/javascript/places.html#place_search_requests).

'use strict';
var defaults = {
bounds: true,
country: null,
Expand Down Expand Up @@ -86,7 +87,7 @@

// Initialize all parts of the plugin.
$.extend(GeoComplete.prototype, {
init: function(){
init: function () {
this.initMap();
this.initMarker();
this.initGeocoder();
Expand All @@ -97,10 +98,10 @@
// Initialize the map but only if the option `map` was set.
// This will create a `map` within the given container
// using the provided `mapOptions` or link to the existing map instance.
initMap: function(){
if (!this.options.map){ return; }
initMap: function () {
if (!this.options.map) { return; }

if (typeof this.options.map.setCenter == "function"){
if (typeof this.options.map.setCenter === "function") {
this.map = this.options.map;
return;
}
Expand All @@ -121,11 +122,11 @@
// Add a marker with the provided `markerOptions` but only
// if the option was set. Additionally it listens for the `dragend` event
// to notify the plugin about changes.
initMarker: function(){
if (!this.map){ return; }
initMarker: function () {
if (!this.map) { return; }
var options = $.extend(this.options.markerOptions, { map: this.map });

if (options.disabled){ return; }
if (options.disabled) { return; }

this.marker = new google.maps.Marker(options);

Expand All @@ -138,16 +139,16 @@

// Associate the input with the autocompleter and create a geocoder
// to fall back when the autocompleter does not return a value.
initGeocoder: function(){
initGeocoder: function () {

var options = {
types: this.options.types,
bounds: this.options.bounds === true ? null : this.options.bounds,
componentRestrictions: this.options.componentRestrictions
};

if (this.options.country){
options.componentRestrictions = {country: this.options.country}
if (this.options.country) {
options.componentRestrictions = {country: this.options.country};
}

this.autocomplete = new google.maps.places.Autocomplete(
Expand All @@ -158,7 +159,7 @@

// Bind autocomplete to map bounds but only if there is a map
// and `options.bindToMap` is set to true.
if (this.map && this.options.bounds === true){
if (this.map && this.options.bounds === true) {
this.autocomplete.bindTo('bounds', this.map);
}

Expand All @@ -170,20 +171,20 @@
);

// Prevent parent form from being submitted if user hit enter.
this.$input.keypress(function(event){
if (event.keyCode === 13){ return false; }
this.$input.keypress(function (event) {
if (event.keyCode === 13) { return false; }
});

// Listen for "geocode" events and trigger find action.
this.$input.bind("geocode", $.proxy(function(){
this.$input.bind("geocode", $.proxy(function () {
this.find();
}, this));

// Trigger find action when input element is blured out.
// (Usefull for typing partial location and tabing to the next field
// or clicking somewhere else.)
if (this.options.blur === true){
this.$input.blur($.proxy(function(){
if (this.options.blur === true) {
this.$input.blur($.proxy(function () {
this.find();
}, this));
}
Expand All @@ -192,23 +193,23 @@
// Prepare a given DOM structure to be populated when we got some data.
// This will cycle through the list of component types and map the
// corresponding elements.
initDetails: function(){
if (!this.options.details){ return; }
initDetails: function () {
if (!this.options.details) { return; }

var $details = $(this.options.details),
attribute = this.options.detailsAttribute,
details = {};

function setDetail(value){
function setDetail(value) {
details[value] = $details.find("[" + attribute + "=" + value + "]");
}

$.each(componentTypes, function(index, key){
$.each(componentTypes, function (index, key) {
setDetail(key);
setDetail(key + "_short");
});

$.each(placesDetails, function(index, key){
$.each(placesDetails, function (index, key) {
setDetail(key);
});

Expand All @@ -218,13 +219,13 @@

// Set the initial location of the plugin if the `location` options was set.
// This method will care about converting the value into the right format.
initLocation: function() {
initLocation: function () {

var location = this.options.location, latLng;

if (!location) { return; }

if (typeof location == 'string') {
if (typeof location === 'string') {
this.find(location);
return;
}
Expand All @@ -233,36 +234,36 @@
latLng = new google.maps.LatLng(location[0], location[1]);
}

if (location instanceof google.maps.LatLng){
if (location instanceof google.maps.LatLng) {
latLng = location;
}

if (latLng){
if (this.map){ this.map.setCenter(latLng); }
if (this.marker){ this.marker.setPosition(latLng); }
if (latLng) {
if (this.map) { this.map.setCenter(latLng); }
if (this.marker) { this.marker.setPosition(latLng); }
}
},

// Look up a given address. If no `address` was specified it uses
// the current value of the input.
find: function(address){
find: function (address) {
this.geocode({
address: address || this.$input.val()
});
},

// Requests details about a given location.
// Additionally it will bias the requests to the provided bounds.
geocode: function(request){
if (this.options.bounds && !request.bounds){
if (this.options.bounds === true){
geocode: function (request) {
if (this.options.bounds && !request.bounds) {
if (this.options.bounds === true) {
request.bounds = this.map && this.map.getBounds();
} else {
request.bounds = this.options.bounds;
}
}

if (this.options.country){
if (this.options.country) {
request.region = this.options.country;
}

Expand Down Expand Up @@ -298,13 +299,13 @@
// Handles the geocode response. If more than one results was found
// it triggers the "geocode:multiple" events. If there was an error
// the "geocode:error" event is fired.
handleGeocode: function(results, status){
handleGeocode: function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
var result = results[0];
this.$input.val(result.formatted_address);
this.update(result);

if (results.length > 1){
if (results.length > 1) {
this.trigger("geocode:multiple", results);
}

Expand All @@ -314,40 +315,40 @@
},

// Triggers a given `event` with optional `arguments` on the input.
trigger: function(event, argument){
trigger: function (event, argument) {
this.$input.trigger(event, [argument]);
},

// Set the map to a new center by passing a `geometry`.
// If the geometry has a viewport, the map zooms out to fit the bounds.
// Additionally it updates the marker position.
center: function(geometry){
center: function (geometry) {

if (geometry.viewport){
if (geometry.viewport) {
this.map.fitBounds(geometry.viewport);
if (this.map.getZoom() > this.options.maxZoom){
if (this.map.getZoom() > this.options.maxZoom) {
this.map.setZoom(this.options.maxZoom);
}
} else {
this.map.setZoom(this.options.maxZoom);
this.map.setCenter(geometry.location);
}

if (this.marker){
if (this.marker) {
this.marker.setPosition(geometry.location);
this.marker.setAnimation(this.options.markerOptions.animation);
}
},

// Update the elements based on a single places or geoocoding response
// and trigger the "geocode:result" event on the input.
update: function(result){
update: function (result) {

if (this.map){
if (this.map) {
this.center(result.geometry);
}

if (this.$details){
if (this.$details) {
this.fillDetails(result);
}

Expand All @@ -357,22 +358,22 @@
// Populate the provided elements with new `result` data.
// This will lookup all elements that has an attribute with the given
// component type.
fillDetails: function(result){
fillDetails: function (result) {

var data = {},
geometry = result.geometry,
viewport = geometry.viewport,
bounds = geometry.bounds;

// Create a simplified version of the address components.
$.each(result.address_components, function(index, object){
$.each(result.address_components, function (index, object) {
var name = object.types[0];
data[name] = object.long_name;
data[name + "_short"] = object.short_name;
});

// Add properties of the places details.
$.each(placesDetails, function(index, key){
$.each(placesDetails, function (index, key) {
data[key] = result[key];
});

Expand All @@ -388,7 +389,7 @@
});

// Set the values for all details.
$.each(this.details, $.proxy(function(key, $detail){
$.each(this.details, $.proxy(function (key, $detail) {
var value = data[key];
this.setDetail($detail, value);
}, this));
Expand All @@ -399,40 +400,40 @@
// Assign a given `value` to a single `$element`.
// If the element is an input, the value is set, otherwise it updates
// the text content.
setDetail: function($element, value){
setDetail: function ($element, value) {

if (value === undefined){
if (value === undefined) {
value = "";
} else if (typeof value.toUrlValue == "function"){
} else if (typeof value.toUrlValue === "function") {
value = value.toUrlValue();
}

if ($element.is(":input")){
if ($element.is(":input")) {
$element.val(value);
} else {
$element.text(value);
}
},

// Fire the "geocode:dragged" event and pass the new position.
markerDragged: function(event){
markerDragged: function (event) {
this.trigger("geocode:dragged", event.latLng);
},

mapClicked: function(event) {
this.trigger("geocode:click", event.latLng);
mapClicked: function (event) {
this.trigger("geocode:click", event.latLng);
},

// Restore the old position of the marker to the last now location.
resetMarker: function(){
resetMarker: function () {
this.marker.setPosition(this.data.location);
this.setDetail(this.details.lat, this.data.location.lat());
this.setDetail(this.details.lng, this.data.location.lng());
},

// Update the plugin after the user has selected an autocomplete entry.
// If the place has no geometry it passes it to the geocoder.
placeChanged: function(){
placeChanged: function () {
var place = this.autocomplete.getPlace();

if (!place.geometry){
Expand All @@ -450,33 +451,33 @@
// A plugin wrapper around the constructor.
// Pass `options` with all settings that are different from the default.
// The attribute is used to prevent multiple instantiations of the plugin.
$.fn.geocomplete = function(options) {
$.fn.geocomplete = function (options) {

var attribute = 'plugin_geocomplete';

// If you call `.geocomplete()` with a string as the first parameter
// it returns the corresponding property or calls the method with the
// following arguments.
if (typeof options == "string"){
if (typeof options === "string") {

var instance = $(this).data(attribute) || $(this).geocomplete().data(attribute),
prop = instance[options];

if (typeof prop == "function"){
if (typeof prop === "function") {
prop.apply(instance, Array.prototype.slice.call(arguments, 1));
return $(this);
} else {
if (arguments.length == 2){
prop = arguments[1];
}
}
else if (arguments.length === 2) {
prop = arguments[1];

return prop;
}
} else {
return this.each(function() {
return this.each(function () {
// Prevent against multiple instantiations.
var instance = $.data(this, attribute);
if (!instance) {
instance = new GeoComplete( this, options )
instance = new GeoComplete( this, options );
$.data(this, attribute, instance);
}
});
Expand Down