-
Notifications
You must be signed in to change notification settings - Fork 283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] add map to admin's change list (WIP - DO NOT MERGE) #124
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
{% extends 'admin/change_list.html' %} | ||
{% load leaflet_tags %} | ||
{% load admin_list_leaflet %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
|
||
{% block stylesheets %} | ||
{{ block.super }} | ||
{% include 'leaflet/css.html' %} | ||
|
||
<style> | ||
.leaflet-container-default{ | ||
min-width: 300px; | ||
} | ||
</style> | ||
|
||
{% endblock %} | ||
|
||
{% block javascripts %} | ||
{{ block.super }} | ||
{% include 'leaflet/js.html' %} | ||
{% endblock %} | ||
|
||
|
||
{% block result_list %} | ||
{% if cl.model_admin.list_geom_field %} | ||
|
||
{% leaflet_map "map" %} | ||
|
||
<script type="text/javascript"> | ||
|
||
(function($) { | ||
|
||
$('#map').addClass('grp-module'); | ||
|
||
// GeoJSON object | ||
var geojsonFeatures = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{% for result in cl.result_list %} | ||
{ | ||
"type": "Feature", | ||
"geometry": {{result|geom_field:cl.model_admin.list_geom_field|safe}}, | ||
"properties": {"pk": "{{result.pk}}",} | ||
} | ||
{% if not forloop.last %},{% endif %} | ||
{% endfor %} | ||
] | ||
} | ||
|
||
|
||
window.addEventListener('map:init', function (e) { | ||
|
||
var target_map = e.detail.map; | ||
|
||
|
||
// 1. We create and add the layer to the map | ||
var featuresLayer = L.geoJson(geojsonFeatures, { | ||
onEachFeature: onEachFeature | ||
}); | ||
featuresLayer.addTo(target_map); | ||
target_map.fitBounds(featuresLayer.getBounds()); | ||
|
||
// 2. We hook into _selection_actions checkboxes to provide selection features | ||
$('input[name="_selected_action"]').click(function(){ | ||
var id = $(this).val(); | ||
var checkbox = $('input[name="_selected_action"][value="'+id+'"]'); | ||
var checked = $(checkbox).is(':checked'); | ||
featuresLayer.eachLayer(function (layer) { | ||
if(layer.feature.properties.pk == id) { | ||
if(!checked){ | ||
layer.setStyle({fillColor :'blue'}) | ||
} | ||
else{ | ||
layer.setStyle({fillColor :'yellow'}) | ||
} | ||
} | ||
}); | ||
}); | ||
}, false); | ||
|
||
// This functions add click and doubleclick handlers to the layer | ||
function onEachFeature(feature, layer) { | ||
layer.on('click',function(){ | ||
var checkbox = $('input[name="_selected_action"][value="'+this.feature.properties.pk+'"]'); | ||
checkbox.click(); | ||
}); | ||
layer.on('dblclick',function(){ | ||
window.location = "{% url 'admin:index' %}{{cl.opts.app_label}}/{{cl.opts.model_name}}/"+this.feature.properties.pk+'/'; | ||
}); | ||
} | ||
|
||
})(grp.jQuery); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should try to avoid inline Javascript in templates. Instead, you could prepare the geojson in the admin class, and put the JS in a separated file, taking some ready-to-use data. As for the window location, it could be a geojson property computed on the python side too |
||
</script> | ||
|
||
|
||
{% endif %} | ||
{{block.super}} | ||
{% endblock %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,26 @@ def leaflet_json_config(): | |
|
||
return json.dumps(settings_as_json) | ||
|
||
@register.filter | ||
def geom_field(instance, field_name): | ||
""" | ||
Returns the geojson representation for a model instance given the geometry field name. Works accross relations with the objA__objB notation. | ||
""" | ||
fields = field_name.split("__") | ||
try: | ||
obj = instance | ||
for field in fields: | ||
obj = getattr( obj, field ) | ||
geom_field = obj | ||
except AttributeError: | ||
geom_field = None | ||
|
||
# geom_field = getattr(instance, field_name) | ||
if geom_field: | ||
return geom_field.json | ||
else: | ||
return 'null' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would definitely deserve some tests :) |
||
|
||
|
||
def _get_plugin_names(plugin_names_from_tag_parameter): | ||
""" | ||
|
@@ -137,4 +157,4 @@ def _get_all_resources_for_plugins(plugin_names, resource_type): | |
if plugin_name in PLUGINS: | ||
result.extend(PLUGINS[plugin_name].get(resource_type, [])) | ||
|
||
return result | ||
return result |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is a rather radical change, I would do another admin class instead, at least the time to stabilize the whole thing