s'assurer de l'existence de jQuery

main
Paul Schneider 9 years ago
parent 1c05538b75
commit e9093acc8a
1 changed files with 88 additions and 85 deletions

@ -1,5 +1,10 @@
+(function($,maps){ if (typeof jQuery === 'undefined') {
$.widget("psc.googlegeocode" , { throw new Error('Bootstrap\'s JavaScript requires jQuery')
}
+
(function($, maps) {
$.widget("psc.googlegeocode", {
options: { options: {
mapId: 'map', mapId: 'map',
longId: 'Longitude', longId: 'Longitude',
@ -17,44 +22,42 @@ $.widget("psc.googlegeocode" , {
zoom: 16, zoom: 16,
center: { lat: 48.862854, lng: 2.2056466 } center: { lat: 48.862854, lng: 2.2056466 }
}); });
var _this =this; var _this = this;
this.element.rules("add", this.element.rules("add", {
{
remote: { remote: {
url: 'https://maps.googleapis.com/maps/api/geocode/json', url: 'https://maps.googleapis.com/maps/api/geocode/json',
type: 'get', type: 'get',
data: { data: {
sensor: false, sensor: false,
address: function () { return _this.element.val() } address: function() {  return _this.element.val() }
}, },
dataType: 'json', dataType: 'json',
dataFilter: function(datastr) { dataFilter: function(datastr) {
$('#'+_this.options.locComboId).html(""); $('#' + _this.options.locComboId).html("");
var data = JSON.parse(datastr); var data = JSON.parse(datastr);
data.results.forEach(function(item) { data.results.forEach(function(item) {
if (item.formatted_address !== _this.element.val()) { if (item.formatted_address !== _this.element.val()) {
$('<li>'+item.formatted_address+'</li>') $('<li>' + item.formatted_address + '</li>')
.data("geoloc",item) .data("geoloc", item)
.click(function() { _this.chooseLoc('user',item) }) .click(function() { _this.chooseLoc('user', item) })
.css('cursor','pointer') .css('cursor', 'pointer')
.appendTo($('#'+_this.options.locComboId));} .appendTo($('#' + _this.options.locComboId));
else { } } else {}
}); });
if ((data.status === 'OK') && (data.results.length == 1)) if ((data.status === 'OK') && (data.results.length == 1)) {
{
// _this.chooseLoc('google',data.results[0]); // _this.chooseLoc('google',data.results[0]);
return true; return true;
} }
return false; return false;
}, },
error: function() error: function() {
{
// xhr, textStatus, errorThrown console.log('ajax loading error ... '+textStatus+' ... '+ errorThrown); // xhr, textStatus, errorThrown console.log('ajax loading error ... '+textStatus+' ... '+ errorThrown);
return false; return false;
} }
} }
})}, })
chooseLoc: function(sender,loc) { },
chooseLoc: function(sender, loc) {
if (sender === 'user') this.element.val(loc.formatted_address); if (sender === 'user') this.element.val(loc.formatted_address);
var pos = loc.geometry.location; var pos = loc.geometry.location;
var lat = new Number(pos.lat); var lat = new Number(pos.lat);
@ -74,15 +77,15 @@ $.widget("psc.googlegeocode" , {
maps.event.addListener(this.marker, 'dragend', function() { maps.event.addListener(this.marker, 'dragend', function() {
// TODO reverse geo code // TODO reverse geo code
var pos = this.marker.getPosition(); var pos = this.marker.getPosition();
$('#'+this.options.latId).val(pos.lat); $('#' + this.options.latId).val(pos.lat);
$('#'+this.options.longId).val(pos.lng); $('#' + this.options.longId).val(pos.lng);
}); });
this.element.valid(); this.element.valid();
$('#'+this.options.addrValidationId).empty(); $('#' + this.options.addrValidationId).empty();
$('#'+this.options.formValidId).empty(); $('#' + this.options.formValidId).empty();
$('#'+this.options.locComboId).empty(); $('#' + this.options.locComboId).empty();
return this; return this;
} }
}) })
})(jQuery,google.maps) })(jQuery, google.maps);
Loading…