Prechádzať zdrojové kódy

HUE-6998 [frontend] Improve configurability of Leaflet maps

Enrico Berti 8 rokov pred
rodič
commit
ac9918bc3f

+ 7 - 0
desktop/conf.dist/hue.ini

@@ -147,6 +147,13 @@
   # The copyright message for the specified Leaflet maps Tile Layer
   ## leaflet_tile_layer_attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
 
+  # All the map options accordingly to http://leafletjs.com/reference-0.7.7.html#map-options
+  # To change CRS, just use the name, ie. "EPSG4326"
+  ## leaflet_map_options='{}'
+
+  # All the tile layer options, accordingly to http://leafletjs.com/reference-0.7.7.html#tilelayer
+  ## leaflet_tile_layer_options='{}'
+
   # X-Frame-Options HTTP header value. Use 'DENY' to deny framing completely
   ## http_x_frame_options=SAMEORIGIN
 

+ 7 - 0
desktop/conf/pseudo-distributed.ini.tmpl

@@ -151,6 +151,13 @@
   # The copyright message for the specified Leaflet maps Tile Layer
   ## leaflet_tile_layer_attribution='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
 
+  # All the map options accordingly to http://leafletjs.com/reference-0.7.7.html#map-options
+  # To change CRS, just use the name, ie. "EPSG4326"
+  ## leaflet_map_options='{}'
+
+  # All the tile layer options, accordingly to http://leafletjs.com/reference-0.7.7.html#tilelayer
+  ## leaflet_tile_layer_options='{}'
+
   # X-Frame-Options HTTP header value. Use 'DENY' to deny framing completely
   ## http_x_frame_options=SAMEORIGIN
 

+ 12 - 0
desktop/core/src/desktop/conf.py

@@ -390,6 +390,18 @@ LEAFLET_TILE_LAYER_ATTRIBUTION = Config(
   help=_("The copyright message for the specified Leaflet maps Tile Layer"),
   default='&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors')
 
+LEAFLET_MAP_OPTIONS = Config(
+  key="leaflet_map_options",
+  help=_("All the map options, accordingly to http://leafletjs.com/reference-0.7.7.html#map-options. To change CRS, just use the name, ie. 'EPSG4326'"),
+  type=coerce_json_dict,
+  default="{}")
+
+LEAFLET_TILE_LAYER_OPTIONS = Config(
+  key="leaflet_tile_layer_options",
+  help=_("All the tile layer options, accordingly to http://leafletjs.com/reference-0.7.7.html#tilelayer"),
+  type=coerce_json_dict,
+  default="{}")
+
 POLL_ENABLED = Config(
   key="poll_enabled",
   help=_("Use poll(2) in Hue thrift pool."),

+ 12 - 5
desktop/core/src/desktop/static/desktop/js/ko.charts.leaflet.js

@@ -86,17 +86,24 @@
         if (_hasAtLeastOneLat && _hasAtLeastOneLng) {
           try {
             if (_map == null) {
-              _map = L.map(element);
+              if (LeafletGlobals.mapOptions.crs) {
+                LeafletGlobals.mapOptions.crs = L.CRS[LeafletGlobals.mapOptions.crs];
+              }
+              _map = L.map(element, LeafletGlobals.mapOptions);
               var tileLayerOptions = {
                 layer: 'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
                 attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
               }
               if (LeafletGlobals) {
-                tileLayerOptions = LeafletGlobals;
+                if (LeafletGlobals.layer) {
+                  tileLayerOptions.layer = LeafletGlobals.layer;
+                }
+                if (LeafletGlobals.attribution) {
+                  tileLayerOptions.attribution = LeafletGlobals.attribution;
+                }
+                tileLayerOptions = $.extend(tileLayerOptions, LeafletGlobals.layerOptions);
               }
-              L.tileLayer(tileLayerOptions.layer, {
-                attribution: $(element).width() > 300 ? tileLayerOptions.attribution : ''
-              }).addTo(_map);
+              L.tileLayer(tileLayerOptions.layer, tileLayerOptions).addTo(_map);
 
               if (L.control.zoomBox) {
                 var _zoomBox = L.control.zoomBox({

+ 3 - 1
desktop/core/src/desktop/templates/common_header_footer_components.mako

@@ -142,7 +142,9 @@ from metadata.conf import has_optimizer, OPTIMIZER
 
     var LeafletGlobals = {
       layer: '${ leaflet['layer'] |n,unicode }',
-      attribution: '${ leaflet['attribution'] |n,unicode }'
+      attribution: '${ leaflet['attribution'] |n,unicode }',
+      mapOptions: JSON.parse('${ leaflet['map_options'] |n,unicode }'),
+      layerOptions: JSON.parse('${ leaflet['layer_options'] |n,unicode }')
     };
 
     var ApiHelperGlobals = {

+ 6 - 2
desktop/core/src/desktop/views.py

@@ -77,7 +77,9 @@ def hue(request):
     'is_ldap_setup': 'desktop.auth.backend.LdapBackend' in desktop.conf.AUTH.BACKEND.get(),
     'leaflet': {
       'layer': desktop.conf.LEAFLET_TILE_LAYER.get(),
-      'attribution': desktop.conf.LEAFLET_TILE_LAYER_ATTRIBUTION.get()
+      'attribution': desktop.conf.LEAFLET_TILE_LAYER_ATTRIBUTION.get(),
+      'map_options': json.dumps(desktop.conf.LEAFLET_MAP_OPTIONS.get()),
+      'layer_options': json.dumps(desktop.conf.LEAFLET_TILE_LAYER_OPTIONS.get()),
     },
     'is_demo': desktop.conf.DEMO_ENABLED.get(),
     'banner_message': get_banner_message(request),
@@ -458,7 +460,9 @@ def commonheader(title, section, user, request=None, padding="90px", skip_topbar
     'skip_idle_timeout': skip_idle_timeout,
     'leaflet': {
       'layer': desktop.conf.LEAFLET_TILE_LAYER.get(),
-      'attribution': desktop.conf.LEAFLET_TILE_LAYER_ATTRIBUTION.get()
+      'attribution': desktop.conf.LEAFLET_TILE_LAYER_ATTRIBUTION.get(),
+      'map_options': json.dumps(desktop.conf.LEAFLET_MAP_OPTIONS.get()),
+      'layer_options': json.dumps(desktop.conf.LEAFLET_TILE_LAYER_OPTIONS.get()),
     },
     'is_demo': desktop.conf.DEMO_ENABLED.get(),
     'is_ldap_setup': 'desktop.auth.backend.LdapBackend' in desktop.conf.AUTH.BACKEND.get(),