StreamField:添加Map块

时间:2017-05-04 10:55:07

标签: wagtail wagtail-streamfield

我正在制作一个传单wagtail块集成。

https://github.com/frague59/wagtail-leaflet

当我add将新块放入流中时,我在使用窗口小部件渲染时遇到了困难:没有显示任何内容。来自django-leaflet / django-geojson的小部件涉及geojson参数' window wide'的设置,这不会反映在模板实例中。

在我的leaflet/widget.html

<script type="text/javascript">
    {% block vars %}var {{ module }} = {};
    {{ module }}.fieldid = '{{ id_css }}';
    {{ module }}.modifiable = {{ modifiable|yesno:"true,false" }};
    {{ module }}.geom_type = '{{ geom_type }}';
    {{ module }}.srid = {{ map_srid }};
    {% endblock vars %}
       function {{ id_map_callback }}(map, options) {
        window.LEAFLET_DATA['{{ module }}'].store_class = {{ field_store_class }};
        (new {{ geometry_field_class}}(window.LEAFLET_DATA['{{ module }}'])).addTo(map);
        {% block callback %}{% endblock callback %}
    };

    {% if target_map %}
        window.addEventListener('map:init', function (e) {
            var target_map = e.detail.map;
            target_map.on('map:loadfield', function (me) {
                if (me.fieldid == 'id_{{ target_map }}') {
                    setTimeout(function () {
                        {{ id_map_callback }}(target_map, e.detail.options);
                    }, 0);
                }
            });
        }, false);
    {% endif %}
</script>

来自text/template元素:

<script type="text/javascript">
    var geodjango___prefix___value = {};
    geodjango___prefix___value.fieldid = '__prefix__-value';
    geodjango___prefix___value.modifiable = true;
    geodjango___prefix___value.geom_type = 'Geometry';
    geodjango___prefix___value.srid = 4326;


    function __prefix___value_map_callback(map, options) {
        geodjango___prefix___value.store_class = L.FieldStore;
        (new L.GeometryField(geodjango___prefix___value)).addTo(map);

    };

</script>

Streamfield块API没有明确提到这个用例。

你有这种块的工作块示例吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

的要求:

django-leaflet
django-geosjon
jsonfield 

块定义:使用django-leaflet / django-geosjon

中的传单小部件
class GeoJSONBlock(FieldBlock):
    geom_type = 'POINT'

    def __init__(self, required=True, help_text=None, max_length=None, min_length=None, **kwargs):
        if self.geom_type is None:
            raise NotImplemented('You are attempting to use ``GeoJSONBlock`` directly, which *WILL* not work !')

        self.field = GeoJSONFormField(required=required,
                                      help_text=help_text,
                                      max_length=max_length,
                                      min_length=min_length,
                                      geom_type=self.geom_type)
        super(GeoJSONBlock, self).__init__(**kwargs)

    def render_form(self, value, prefix='', errors=None):
        """
        Renders ``edit`` form

        :param value: current value
        :param prefix: prefix of the form item
        :param errors: Validations errors
        :returns: HTML Fragment
        """
        logger.debug('MapBlock::render_form() value = %s', value)
        rendered = super(GeoJSONBlock, self).render_form(value=value, prefix=prefix, errors=errors)
        return rendered

    def render(self, value, context=None):
        """
        Renders the widget in the web site

        :param value: current value
        :param context: Additional render context
        :returns: HTML Fragment
        """
        logger.debug('MapBlock::render() value = %s', value)
        rendered = super(GeoJSONBlock, self).render(value=value, context=context)
        return rendered

    @property
    def media(self):
        return forms.Media(
            js=['wagtailleaflet/leaflet_init.js', ]
        )

    def js_initializer(self):
        """
        JS function to launch on start'up
        :returns: JS function name, from ``wagtailleaflet/leaflet_init.js``
        """
        output = 'drawMap'
        logger.debug('MapBlock::js_initializer() output = %s', output)
        return output

        def html_declarations(self):
            output = render_to_string('wagtailleaflet/leaflet_forms.html')
            return output

用法:

from wagtail.wagtailadmin.edit_handlers import StreamFieldPanel
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore import blocks
from wagtailleaflet.blocks import GeoJSONPointBlock

class DemoPage(Page):
    """
    Simple demo page 
    """
    body = StreamField([('text', blocks.RichTextBlock()),
                        ('map', GeoJSONPointBlock()), ])

    content_panels = Page.content_panels + [StreamFieldPanel('body')]

我发现问题来自传单实现,它使用slugify(将id设置为lower)来创建id,这会在块上的sequence.js中打破__PREFIX__替换初始化。

重新制作有点复杂,这就是为什么我通过演示wagtail项目将链接放到我的仓库中。