Skip to content

Commit

Permalink
Ability to target field edition towards different map
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Jul 29, 2014
1 parent 4dc6ac0 commit 6384057
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions leaflet/forms/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def render(self, name, value, attrs=None):
attrs.update(id_map=map_id + '_map',
id_map_callback=map_id + '_map_callback',
modifiable=self.modifiable,
target_map=attrs.get('target_map', getattr(self, 'target_map', None)),
geometry_field_class=attrs.get('geometry_field_class', 'L.GeometryField'),
field_store_class=attrs.get('field_store_class', 'L.FieldStore'))
return super(LeafletWidget, self).render(name, value, attrs)
2 changes: 1 addition & 1 deletion leaflet/static/leaflet/leaflet.extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ L.Map.djangoMap = function (id, options) {
/*
* Trigger custom map:init Event
*/
triggerEvent(window, 'map:init', {map: map, options: options});
triggerEvent(window, 'map:init', {id: id, map: map, options: options});

/*
* Run callback if specified
Expand Down
14 changes: 14 additions & 0 deletions leaflet/templates/leaflet/widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,25 @@
(new {{ geometry_field_class}}({{ module }})).addTo(map);
{% block callback %}{% endblock callback %}
};

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

{% if not target_map %}
{% block map %}
{% leaflet_map id_map callback=id_map_callback %}
{% endblock map %}
{% endif %}

{% if display_raw %}<p> Geometry:</p>{% endif %}
<textarea id="{{ id }}" class="required" cols="150" rows="10" name="{{ name }}">{{ serialized }}</textarea>

0 comments on commit 6384057

Please sign in to comment.