Skip to content

Commit 896fca6

Browse files
authored
Merge pull request pallets-eco#2220 from TomGoBravo/geoatileattributionurl
In contrib/geoa factor out tileLayer(...).addTo(map) and update mapbox attributes
2 parents 46d5a6e + c2e9a5e commit 896fca6

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

flask_admin/contrib/geoa/typefmt.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@
77

88

99
def geom_formatter(view, value):
10-
params = html_params(**{
10+
kwargs = {
1111
"data-role": "leaflet",
1212
"disabled": "disabled",
1313
"data-width": 100,
1414
"data-height": 70,
1515
"data-geometry-type": to_shape(value).geom_type,
1616
"data-zoom": 15,
17-
"data-tile-layer-url": view.tile_layer_url,
18-
"data-tile-layer-attribution": view.tile_layer_attribution
19-
})
17+
}
18+
# html_params will serialize None as a string literal "None" so only put tile-layer-url
19+
# and tile-layer-attribution in kwargs when they have a meaningful value.
20+
# flask_admin/static/admin/js/form.js uses its default values when these are not passed
21+
# as textarea attributes.
22+
if view.tile_layer_url:
23+
kwargs["data-tile-layer-url"] = view.tile_layer_url
24+
if view.tile_layer_attribution:
25+
kwargs["data-tile-layer-attribution"] = view.tile_layer_attribution
26+
params = html_params(**kwargs)
2027

2128
if value.srid == -1:
2229
value.srid = 4326

flask_admin/contrib/geoa/view.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
class ModelView(SQLAModelView):
66
model_form_converter = form.AdminModelConverter
77
column_type_formatters = typefmt.DEFAULT_FORMATTERS
8+
# tile_layer_url is prefixed with '//' in flask_admin/static/admin/js/form.js
9+
# Leave it as None or set it to a string starting with a hostname, NOT "http".
810
tile_layer_url = None
911
tile_layer_attribution = None

flask_admin/static/admin/js/form.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,16 @@
157157
}
158158

159159
// set up tiles
160-
if($el.data('tile-layer-url')){
161-
var attribution = $el.data('tile-layer-attribution') || ''
162-
L.tileLayer('//'+$el.data('tile-layer-url'), {
163-
attribution: attribution,
164-
maxZoom: 18
165-
}).addTo(map)
166-
} else {
167-
var mapboxUrl = 'https://api.mapbox.com/styles/v1/mapbox/'+window.MAPBOX_MAP_ID+'/tiles/{z}/{x}/{y}?access_token='+window.MAPBOX_ACCESS_TOKEN
168-
L.tileLayer(mapboxUrl, {
169-
attribution: 'Map data &copy; <a href="//openstreetmap.org">OpenStreetMap</a> contributors, <a href="//creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="//mapbox.com">Mapbox</a>',
170-
maxZoom: 18
171-
}).addTo(map);
172-
}
173-
160+
var mapboxHostnameAndPath = $el.data('tile-layer-url') || 'api.mapbox.com/styles/v1/mapbox/'+window.MAPBOX_MAP_ID+'/tiles/{z}/{x}/{y}?access_token={accessToken}';
161+
var attribution = $el.data('tile-layer-attribution') || 'Map data &copy; <a href="//openstreetmap.org">OpenStreetMap</a> contributors, <a href="//creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="//mapbox.com">Mapbox</a>';
162+
L.tileLayer('//' + mapboxHostnameAndPath, {
163+
// Attributes from https://docs.mapbox.com/help/troubleshooting/migrate-legacy-static-tiles-api/
164+
attribution: attribution,
165+
maxZoom: 18,
166+
tileSize: 512,
167+
zoomOffset: -1,
168+
accessToken: window.MAPBOX_ACCESS_TOKEN
169+
}).addTo(map);
174170

175171
// everything below here is to set up editing, so if we're not editable,
176172
// we can just return early.

0 commit comments

Comments
 (0)