Updated map paragraph renderer
authorThierry Florac <tflorac@ulthar.net>
Wed, 27 Jan 2021 15:52:09 +0100
changeset 543 75d913d682d4
parent 542 39aef8542b47
child 544 fed1815bb920
Updated map paragraph renderer
src/pyams_default_theme/component/paragraph/interfaces/map.py
src/pyams_default_theme/component/paragraph/map.py
src/pyams_default_theme/component/paragraph/zmi/map.py
--- a/src/pyams_default_theme/component/paragraph/interfaces/map.py	Wed Jan 13 14:14:02 2021 +0100
+++ b/src/pyams_default_theme/component/paragraph/interfaces/map.py	Wed Jan 27 15:52:09 2021 +0100
@@ -23,7 +23,7 @@
 
     # import packages
     from zope.interface import Attribute
-    from zope.schema import Bool
+    from zope.schema import Bool, Int
 
     from pyams_default_theme import _
 
@@ -40,3 +40,8 @@
                                              default=True)
 
         configuration = Attribute("Map configuration")
+
+        map_height = Int(title=_("Map height"),
+                         description=_("Map height, in pixels"),
+                         required=True,
+                         default=400)
--- a/src/pyams_default_theme/component/paragraph/map.py	Wed Jan 13 14:14:02 2021 +0100
+++ b/src/pyams_default_theme/component/paragraph/map.py	Wed Jan 27 15:52:09 2021 +0100
@@ -12,15 +12,24 @@
 
 __docformat__ = 'restructuredtext'
 
+import json
 
+from onf_website.component.location import ILocationInfo, ILocationTarget
 from pyams_content.component.paragraph.interfaces.map import have_gis
+from pyams_gis.layer import GeoJSONLayer
+from pyams_utils.interfaces import ICacheKeyValue
+from pyams_utils.traversing import get_parent
+from pyams_utils.url import absolute_url
+
+
 if have_gis:
 
     from zope.schema.fieldproperty import FieldProperty
 
     from pyams_content.component.paragraph.interfaces.map import IMapParagraph
     from pyams_content.features.renderer.interfaces import IContentRenderer
-    from pyams_default_theme.component.paragraph.interfaces.map import IMapParagraphDefaultRendererSettings
+    from pyams_default_theme.component.paragraph.interfaces.map import \
+        IMapParagraphDefaultRendererSettings
     from pyams_default_theme.features.renderer import BaseContentRenderer
     from pyams_gis.configuration import MapConfiguration
     from pyams_gis.interfaces.configuration import IMapConfiguration
@@ -48,6 +57,8 @@
         _use_default_map_configuration = FieldProperty(IMapParagraphDefaultRendererSettings[
                                                            'use_default_map_configuration'])
 
+        map_height = FieldProperty(IMapParagraphDefaultRendererSettings['map_height'])
+
         @property
         def use_default_map_configuration(self):
             return self._use_default_map_configuration
@@ -68,12 +79,12 @@
         def configuration(self):
             if self.use_default_map_configuration:
                 manager = get_utility(IMapManager)
-                return IMapConfiguration(manager)
-            else:
-                return self
+                return IMapConfiguration(manager).get_configuration()
+            return self.get_configuration()
 
 
-    @adapter_config(context=IMapParagraph, provides=IMapParagraphDefaultRendererSettings)
+    @adapter_config(context=IMapParagraph,
+                    provides=IMapParagraphDefaultRendererSettings)
     def map_paragraph_default_renderer_settings_factory(context):
         """Map paragraph default renderer settings factory"""
         return get_annotation_adapter(context, MAP_DEFAULT_RENDERER_SETTINGS_KEY,
@@ -84,7 +95,9 @@
     # Map paragraph default renderer
     #
 
-    @adapter_config(name='default', context=(IMapParagraph, IPyAMSLayer), provides=IContentRenderer)
+    @adapter_config(name='default',
+                    context=(IMapParagraph, IPyAMSLayer),
+                    provides=IContentRenderer)
     @template_config(template='templates/map-default.pt', layer=IPyAMSLayer)
     class MapParagraphDefaultRenderer(BaseContentRenderer):
         """Map paragraph default renderer"""
@@ -94,3 +107,57 @@
         settings_interface = IMapParagraphDefaultRendererSettings
 
         i18n_context_attrs = ('title', )
+
+        def get_map_configuration(self):
+            configuration = self.settings.configuration
+            settings = self.settings
+            if settings.display_context_forests:
+                parent = get_parent(self.context, ILocationTarget)
+                if parent is not None:
+                    key = ICacheKeyValue(parent)
+                    layer = GeoJSONLayer()
+                    layer.name = '{}-forests'.format(key)
+                    layer.url = absolute_url(parent, self.request, 'get-location-forests.json')
+                    layer.style = json.dumps({
+                        'color': '#00dd00',
+                        'weight': '3',
+                        'opacity': '0.5'
+                    })
+                    configuration.setdefault('layers', []).append(layer.get_configuration())
+                    if settings.set_bounds_to_forests:
+                        configuration['fitLayer'] = layer.name
+            return json.dumps(configuration)
+
+        def get_markers(self):
+            if not self.context.display_marker:
+                return None
+            if self.context.use_context_location:
+                parent = get_parent(self.context, ILocationTarget)
+                if parent is None:
+                    return None
+                locations = ILocationInfo(parent).locations
+                if not locations:
+                    return None
+            else:
+                if not self.context.gps_location:
+                    return None
+                locations = [self.context.gps_location]
+            if not locations:
+                return None
+            config = {
+                'icon': {
+                    'url': '/--static--/onf_website/images/leaflet-marker.png',
+                    'size': [47, 59],
+                    'anchor': [23, 58]
+                },
+                'clusterClass': 'onfCluster',
+                'tooltipClass': 'onfTooltip',
+                'markers': []
+            }
+            append = config['markers'].append
+            for idx, location in enumerate(locations):
+                append({
+                    'id': '{}::{}'.format(ICacheKeyValue(self.context), idx),
+                    'point': location.to_json()
+                })
+            return json.dumps(config)
\ No newline at end of file
--- a/src/pyams_default_theme/component/paragraph/zmi/map.py	Wed Jan 13 14:14:02 2021 +0100
+++ b/src/pyams_default_theme/component/paragraph/zmi/map.py	Wed Jan 27 15:52:09 2021 +0100
@@ -58,7 +58,7 @@
         def updateGroups(self):
             form = self.view
             form.add_group(NamedWidgetsGroup(form, 'configuration', form.widgets,
-                                             form.fields.keys(),
+                                             list(form.fields.keys()[:-1]),
                                              legend=_("Don't use default map configuration"),
                                              css_class='inner',
                                              switch=True,