New file |
0,0 → 1,140 |
import {valeurOk} from './utils.js'; |
|
export const initGeoloc = () => { |
onGeoloc(); |
onGeolocManuelle(); |
geolocValidationEtStockage(); |
}; |
|
const onGeoloc = () => { |
// Empêcher que le module carto ne bind ses events partout |
$('#tb-geolocation').on('submit blur click focus mousedown mouseleave mouseup change', '*', evt => { |
evt.preventDefault(); |
return false; |
}); |
// evenement location |
$('#tb-geolocation').on('location', location => { |
const locDatas = location.originalEvent.detail; |
|
if (valeurOk( locDatas )) { |
let latitude = '', |
longitude = ''; |
|
console.dir( locDatas ); |
if (valeurOk( locDatas.geometry.coordinates)) { |
if ('Point' === locDatas.geometry.type) { |
if (valeurOk( locDatas.geometry.coordinates[0])) { |
longitude = locDatas.geometry.coordinates[0].toFixed(5); |
} |
if (valeurOk( locDatas.geometry.coordinates[1])) { |
latitude = locDatas.geometry.coordinates[1].toFixed(5); |
} |
} else if ('LineString' === locDatas.geometry.type) { |
if (valeurOk( locDatas.centroid.coordinates )){ |
if (valeurOk( locDatas.centroid.coordinates[0])) { |
longitude = locDatas.centroid.coordinates[0]; |
} |
if (valeurOk( locDatas.centroid.coordinates[1])) { |
latitude = locDatas.centroid.coordinates[1]; |
} |
} else {// on ne prend qu'un point de la ligne |
if (valeurOk( locDatas.geometry.coordinates[0][0])) { |
longitude = locDatas.geometry.coordinates[0][0]; |
longitude = longitude.toFixed(5); |
} |
if (valeurOk( locDatas.geometry.coordinates[0][1])){ |
latitude = locDatas.geometry.coordinates[0][1]; |
latitude = latitude.toFixed(5); |
} |
} |
} |
} |
if (valeurOk(latitude) && valeurOk(longitude)) { |
$('#latitude').val(latitude); |
$('#longitude').val(longitude); |
} |
} |
}); |
} |
|
const onGeolocManuelle = () => { |
const coords = ['latitude','longitude','zoom']; |
|
$('#latitude,#longitude,#zoom').on('change', function(event) { |
const thisCoord = this.id, |
thisVal = $(this).val(); |
let valeur = ''; |
|
if ($.isNumeric(thisVal)) { |
switch(thisCoord) { |
case 'zoom': |
if (0 < parseInt(thisVal, 10) && 18 >= parseInt(thisVal, 10)) { |
valeur = thisVal; |
} |
break; |
case 'latitude': |
if ( 90 > Math.abs( parseInt(thisVal, 10))) { |
valeur = parseFloat(thisVal, 10).toFixed(5); |
$('#latitude').val(valeur); |
} |
break; |
case 'longitude': |
if ( 180 >= Math.abs( parseInt(thisVal, 10))) { |
valeur = parseFloat(thisVal, 10).toFixed(5); |
$('#longitude').val(valeur); |
} |
break; |
default: |
break; |
} |
} |
//un champ vide n'est pas une erreur |
if ($.isNumeric(valeur)) { |
$(this).siblings('span.error').remove(); |
} else if (0 <= $.inArray(thisCoord, coords)) { |
// on ne signale pas d'erreur si il n'y a rien dans le champ |
if (!valeurOk($(this).siblings('span.error')) && valeurOk($(this).val())) { |
$(this).after('<span class="error">mauvais format pour ce champ<span>'); |
} else { |
$(this).siblings('span.error').remove(); |
} |
} |
}); |
} |
|
const geolocValidationEtStockage = () => { |
$('#signup_submit').off().on('click', event => { |
let localisation = ''; |
const latitude = $('#latitude').val(), |
longitude = $('#longitude').val(), |
zoom = $('#zoom').val(), |
$warning = $('.warning-carto'); |
|
if ((valeurOk(longitude) && !valeurOk(latitude)) || (!valeurOk(longitude) && valeurOk(latitude))) { |
if (!valeurOk($warning)) { |
$('#new-fields-buttons').after( |
`<p class="message warning-carto"> |
<i class="fa fa-exclamation-triangle" aria-hidden="true" style="color:#ff5d55"></i> Vous avez entré des coordonnées incomplètes<br> |
Complétez latitude ou longitude, ou placez un marqueur sur la carto, ou effacez ces deux champs |
</p>` |
); |
} |
return false; |
} else if (valeurOk($warning)) { |
$warning.remove(); |
} |
if (valeurOk(latitude) && valeurOk(longitude)) { |
localisation += 'latitude:'+latitude+';'; |
localisation += 'longitude:'+longitude; |
} |
if (valeurOk(zoom)) { |
if (valeurOk(localisation)) { |
localisation += ';'; |
} |
localisation += 'zoom:'+zoom; |
} |
if (valeurOk(localisation)) { |
$('#localisation').val(localisation); |
} |
}); |
}; |