fields: add support for max bounds to map widget (#23061)

This commit is contained in:
Frédéric Péters 2018-05-22 15:24:10 +02:00
parent d7b938d4d7
commit c6232cf715
3 changed files with 23 additions and 0 deletions

View File

@ -597,3 +597,12 @@ def test_map_widget():
assert '<label' in str(widget.render())
assert not '<label ' in str(widget.render_widget_content())
pub.load_site_options()
pub.site_options.set('options', 'map-bounds-top-left', '1.23;2.34')
pub.site_options.set('options', 'map-bounds-bottom-right', '2.34;3.45')
widget = MapWidget('test', title='Map')
assert 'data-max-bounds-lat1=' in str(widget.render())
assert 'data-max-bounds-lat2=' in str(widget.render())
assert 'data-max-bounds-lng1=' in str(widget.render())
assert 'data-max-bounds-lng2=' in str(widget.render())

View File

@ -948,6 +948,11 @@ class QommonPublisher(Publisher, object):
def get_map_attributes(self):
attrs = {}
attrs['data-def-lat'], attrs['data-def-lng'] = self.get_default_position().split(';')
if self.get_site_option('map-bounds-top-left'):
attrs['data-max-bounds-lat1'], attrs['data-max-bounds-lng1'] = \
self.get_site_option('map-bounds-top-left').split(';')
attrs['data-max-bounds-lat2'], attrs['data-max-bounds-lng2'] = \
self.get_site_option('map-bounds-bottom-right').split(';')
attrs['data-map-attribution'] = self.get_site_option('map-attribution') or \
_("Map data &copy; "\
"<a href='https://openstreetmap.org'>OpenStreetMap</a> contributors, "\

View File

@ -19,6 +19,11 @@ $(window).on('load', function() {
map_options.zoomControl = false;
var map_tile_urltemplate = $map_widget.data('tile-urltemplate');
var map_attribution = $map_widget.data('map-attribution');
if ($map_widget.data('max-bounds-lat1')) {
map_options.maxBounds = L.latLngBounds(
L.latLng($map_widget.data('max-bounds-lat1'), $map_widget.data('max-bounds-lng1')),
L.latLng($map_widget.data('max-bounds-lat2'), $map_widget.data('max-bounds-lng2')));
}
var map = L.map($(this).attr('id'), map_options);
var map_controls_position = $('body').data('map-controls-position') || 'topleft';
new L.Control.Zoom({position: map_controls_position}).addTo(map);
@ -63,6 +68,10 @@ $(window).on('load', function() {
if (! ($map_widget.data('readonly') || is_preview) && ($map_widget.data('init-with-geoloc') || position_prefil)) {
map.on('locationfound', function(e) {
$map_widget.parent().parent().find('label').removeClass('activity');
if (map_options.maxBounds && ! map_options.maxBounds.contains(e.latlng)) {
/* ouf of bounds, keep map centered on default position */
return;
}
if (map.marker === null) {
hidden.val(e.latlng.lat + ';' + e.latlng.lng);
map.setView(e.latlng, map_options.zoom);