forms: add prefilling of address autocompletion by reverse geocoding (#54836)

This commit is contained in:
Frédéric Péters 2021-06-14 15:38:48 +02:00
parent 467ebb16ca
commit 4fb8aee1c8
2 changed files with 15 additions and 3 deletions

View File

@ -181,6 +181,8 @@ class PrefillSelectionWidget(CompositeWidget):
('city', _('City')),
('country', _('Country')),
]
if field and field.type == 'item':
geoloc_fields.append(('address-id', _('Address Identifier')))
self.add(
SingleSelectWidget,
'value_geolocation',

View File

@ -27,6 +27,16 @@ function geoloc_prefill(element_type, element_value)
$(document).on('set-geolocation', function(event, coords, options) {
$.getJSON(WCS_ROOT_URL + '/api/reverse-geocoding?lat=' + coords.lat + '&lon=' + coords.lng, function(data) {
unset_sync_callback()
var $prefilled_by_id = $('div[data-geolocation="address-id"]');
if ($prefilled_by_id.length) {
// prefill by id first as the .change() event will empty the other
// address fields.
var option = $('<option></option>', {value: data.id, text: data.display_name});
option.appendTo($prefilled_by_id.find('select'));
$prefilled_by_id.find('select').val(data.id).change();
}
if (typeof(options) == 'undefined' || !options.force_house_number === false || data.address.house_number) {
geoloc_prefill('house', data.address.house_number);
}
@ -106,16 +116,16 @@ 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')
$('div[data-geolocation]:not(.template-address)').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)
$('div[data-geolocation]:not(.template-address)').attr('hidden', null)
} else if ($('.JsonpSingleSelectWidget.template-address.hide-address-parts').length) {
$('div[data-geolocation]').attr('hidden', 'hidden')
$('div[data-geolocation]:not(.template-address)').attr('hidden', 'hidden')
}
});