From 0393a72c910797505815e38ff63ee880851ae18f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Thu, 13 Jul 2017 23:37:48 +0200 Subject: [PATCH] fix editor bounds when no sources area accessible for the user --- src/c3nav/editor/api.py | 8 +++++++- src/c3nav/editor/static/editor/js/editor.js | 22 +++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/c3nav/editor/api.py b/src/c3nav/editor/api.py index d3cc1ade..f59709f2 100644 --- a/src/c3nav/editor/api.py +++ b/src/c3nav/editor/api.py @@ -9,7 +9,7 @@ from rest_framework.viewsets import ReadOnlyModelViewSet, ViewSet from shapely.ops import cascaded_union from c3nav.editor.models import ChangeSet -from c3nav.mapdata.models import Area +from c3nav.mapdata.models import Area, Source from c3nav.mapdata.models.geometry.space import POI @@ -173,6 +173,12 @@ class EditorViewSet(ViewSet): 'shadow': '#000000', }) + @list_route(methods=['get']) + def bounds(self, request, *args, **kwargs): + return Response({ + 'bounds': Source.max_bounds(), + }) + class ChangeSetViewSet(ReadOnlyModelViewSet): """ diff --git a/src/c3nav/editor/static/editor/js/editor.js b/src/c3nav/editor/static/editor/js/editor.js index 3c23bb70..0f5c4997 100644 --- a/src/c3nav/editor/static/editor/js/editor.js +++ b/src/c3nav/editor/static/editor/js/editor.js @@ -54,28 +54,19 @@ editor = { // sources sources: {}, - get_sources: function (init_sidebar) { + get_sources: function () { // load sources - editor._sources_control = L.control.layers([], [], {autoZIndex: true}).addTo(editor.map); + editor._sources_control = L.control.layers([], [], {autoZIndex: true}); $.getJSON('/api/sources/', function (sources) { - var bounds = [[0, 0], [0, 0]]; var source; for (var i = 0; i < sources.length; i++) { source = sources[i]; editor.sources[source.id] = source; source.layer = L.imageOverlay('/api/sources/'+source.id+'/image/', source.bounds, {opacity: 0.3}); editor._sources_control.addOverlay(source.layer, source.name); - bounds[0][0] = Math.min(source.bounds[0][0], bounds[0][0]); - bounds[0][1] = Math.min(source.bounds[0][1], bounds[0][1]); - bounds[1][0] = Math.max(source.bounds[1][0], bounds[1][0]); - bounds[1][1] = Math.max(source.bounds[1][1], bounds[1][1]); - } - editor.map.setMaxBounds(bounds); - if (init_sidebar) { - editor.map.fitBounds(bounds, {padding: [30, 50]}); - editor.init_sidebar(); } + if (sources.length) editor._sources_control.addTo(editor.map); }); }, @@ -270,8 +261,13 @@ editor = { $.getJSON('/api/editor/geometrystyles/', function(geometrystyles) { editor.geometrystyles = geometrystyles; - editor.get_sources(true); + $.getJSON('/api/editor/bounds/', function(bounds) { + editor.map.setMaxBounds(bounds.bounds); + editor.map.fitBounds(bounds.bounds, {padding: [30, 50]}); + editor.init_sidebar(); + }); }); + editor.get_sources(); }, _last_geometry_url: null, load_geometries: function (geometry_url, highlight_type, editing_id) {