misc: add special template for address autocompletion (#40383)

This commit is contained in:
Frédéric Péters 2020-02-08 18:50:20 +01:00
parent c504b46f84
commit 3ca4c15041
2 changed files with 43 additions and 1 deletions

View File

@ -28,6 +28,7 @@ function geoloc_prefill(element_type, element_value)
$.getJSON(WCS_ROOT_URL + '/api/reverse-geocoding?lat=' + coords.lat + '&lon=' + coords.lng, function(data) {
unset_sync_callback()
geoloc_prefill('house', data.address.house_number);
var number_and_street = null;
var street = data.address.road;
if (!street && data.address.pedestrian) {
street = data.address.pedestrian;
@ -54,7 +55,23 @@ function geoloc_prefill(element_type, element_value)
set_sync_callback()
});
});
if ($('.qommon-map').length == 0) {
$('.JsonpSingleSelectWidget.template-address select').on('change', function() {
var data = $('.JsonpSingleSelectWidget.template-address select').select2('data');
if (data) {
var number_and_street = null;
if (data[0].address.house_number && data[0].address.road) {
number_and_street = data[0].address.house_number + ' ' + data[0].address.road;
} else {
number_and_street = data[0].address.road;
}
geoloc_prefill('number-and-street', number_and_street);
geoloc_prefill('house', data[0].address.house_number);
geoloc_prefill('road', data[0].address.road);
geoloc_prefill('city', data[0].address.city);
geoloc_prefill('postcode', data[0].address.postcode);
}
});
if ($('.qommon-map').length == 0 && $('.JsonpSingleSelectWidget.template-address').length == 0) {
/* if there's no map on the page, we do the geolocation without leaflet. */
if (navigator.geolocation) {
$('div[data-geolocation] label').addClass('activity');
@ -73,6 +90,21 @@ function geoloc_prefill(element_type, element_value)
);
}
}
if ($('.JsonpSingleSelectWidget.template-address').length) {
$('div[data-geolocation] input, div[data-geolocation] textarea').attr('readonly', 'readonly')
if ($('.JsonpSingleSelectWidget.template-address.hide-address-parts').length) {
$('div[data-geolocation]').attr('hidden', 'hidden')
}
}
$('#wcs-manual-address').on('change', function() {
$('div[data-geolocation] input, div[data-geolocation] textarea').attr('readonly', this.checked ? null : 'readonly');
if (this.checked) {
$('div[data-geolocation]').attr('hidden', null)
} else if ($('.JsonpSingleSelectWidget.template-address.hide-address-parts').length) {
$('div[data-geolocation]').attr('hidden', 'hidden')
}
});
function set_sync_callback() {
var $map = $('.qommon-map');

View File

@ -0,0 +1,10 @@
{% extends "qommon/forms/widgets/select_jsonp.html" %}
{% load i18n %}
{% block widget-control %}
{{ block.super }}
{% if not widget.readonly %}
<div>
<label><input id="wcs-manual-address" type="checkbox">{% trans "Manually enter the address" %}</label>
</div>
{% endif %}
{% endblock %}