s'assurer de l'existence de jQuery

This commit is contained in:
2017-05-28 00:44:05 +02:00
parent 1c05538b75
commit e9093acc8a

View File

@ -1,88 +1,91 @@
+(function($,maps){ if (typeof jQuery === 'undefined') {
$.widget("psc.googlegeocode" , { throw new Error('Bootstrap\'s JavaScript requires jQuery')
options: { }
mapId: 'map',
longId: 'Longitude',
latId: 'Latitude',
addrValidationId: 'AddressError',
formValidId: 'ValidationSummary',
locComboId: 'LocationCombo'
},
marker: null,
gmap: null,
_create: function() { +
this.element.addClass("googlegeocode"); (function($, maps) {
this.gmap = new maps.Map(document.getElementById(this.options.mapId), { $.widget("psc.googlegeocode", {
zoom: 16, options: {
center: { lat: 48.862854, lng: 2.2056466 } mapId: 'map',
}); longId: 'Longitude',
var _this =this; latId: 'Latitude',
this.element.rules("add", addrValidationId: 'AddressError',
{ formValidId: 'ValidationSummary',
remote: { locComboId: 'LocationCombo'
url: 'https://maps.googleapis.com/maps/api/geocode/json', },
type: 'get', marker: null,
data: { gmap: null,
sensor: false,
address: function () { return _this.element.val() } _create: function() {
}, this.element.addClass("googlegeocode");
dataType: 'json', this.gmap = new maps.Map(document.getElementById(this.options.mapId), {
dataFilter: function(datastr) { zoom: 16,
$('#'+_this.options.locComboId).html(""); center: { lat: 48.862854, lng: 2.2056466 }
var data = JSON.parse(datastr);
data.results.forEach(function(item) {
if (item.formatted_address !== _this.element.val()) {
$('<li>'+item.formatted_address+'</li>')
.data("geoloc",item)
.click(function() { _this.chooseLoc('user',item) })
.css('cursor','pointer')
.appendTo($('#'+_this.options.locComboId));}
else { }
});
if ((data.status === 'OK') && (data.results.length == 1))
{
// _this.chooseLoc('google',data.results[0]);
return true;
}
return false;
},
error: function()
{
// xhr, textStatus, errorThrown console.log('ajax loading error ... '+textStatus+' ... '+ errorThrown);
return false;
}
}
})},
chooseLoc: function(sender,loc) {
if (sender === 'user') this.element.val(loc.formatted_address);
var pos = loc.geometry.location;
var lat = new Number(pos.lat);
var lng = new Number(pos.lng);
$(document.getElementById(this.options.latId)).val(lat.toLocaleString('en'));
$(document.getElementById(this.options.longId)).val(lng.toLocaleString('en'));
this.gmap.setCenter(pos);
if (this.marker) { 
this.marker.setMap(null);
}
this.marker = new maps.Marker({
map: this.gmap,
draggable: true,
animation: maps.Animation.DROP,
position: pos
}); });
maps.event.addListener(this.marker, 'dragend', function() { var _this = this;
// TODO reverse geo code this.element.rules("add", {
var pos = this.marker.getPosition(); remote: {
$('#'+this.options.latId).val(pos.lat); url: 'https://maps.googleapis.com/maps/api/geocode/json',
$('#'+this.options.longId).val(pos.lng); type: 'get',
}); data: {
this.element.valid(); sensor: false,
$('#'+this.options.addrValidationId).empty(); address: function() {  return _this.element.val() }
$('#'+this.options.formValidId).empty(); },
$('#'+this.options.locComboId).empty(); dataType: 'json',
dataFilter: function(datastr) {
$('#' + _this.options.locComboId).html("");
var data = JSON.parse(datastr);
data.results.forEach(function(item) {
if (item.formatted_address !== _this.element.val()) {
$('<li>' + item.formatted_address + '</li>')
.data("geoloc", item)
.click(function() { _this.chooseLoc('user', item) })
.css('cursor', 'pointer')
.appendTo($('#' + _this.options.locComboId));
} else {}
});
if ((data.status === 'OK') && (data.results.length == 1)) {
// _this.chooseLoc('google',data.results[0]);
return true;
}
return false;
},
error: function() {
// xhr, textStatus, errorThrown console.log('ajax loading error ... '+textStatus+' ... '+ errorThrown);
return false;
}
}
})
},
chooseLoc: function(sender, loc) {
if (sender === 'user') this.element.val(loc.formatted_address);
var pos = loc.geometry.location;
var lat = new Number(pos.lat);
var lng = new Number(pos.lng);
$(document.getElementById(this.options.latId)).val(lat.toLocaleString('en'));
$(document.getElementById(this.options.longId)).val(lng.toLocaleString('en'));
this.gmap.setCenter(pos);
if (this.marker) { 
this.marker.setMap(null);
}
this.marker = new maps.Marker({
map: this.gmap,
draggable: true,
animation: maps.Animation.DROP,
position: pos
});
maps.event.addListener(this.marker, 'dragend', function() {
// TODO reverse geo code
var pos = this.marker.getPosition();
$('#' + this.options.latId).val(pos.lat);
$('#' + this.options.longId).val(pos.lng);
});
this.element.valid();
$('#' + this.options.addrValidationId).empty();
$('#' + this.options.formValidId).empty();
$('#' + this.options.locComboId).empty();
return this; return this;
} }
}) })
})(jQuery,google.maps) })(jQuery, google.maps);