diff options
| author | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-05-29 11:42:58 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2013-05-29 11:53:55 +0200 | 
| commit | 7b54448addd4eca66aff8b5d8f9e04ef7cbcd8fc (patch) | |
| tree | e9af762ed9a8d19adc6e57701e726470d0d9eef0 | |
| parent | 57a43595d7ddb1e1581653eb8d2a71f3d2f192ab (diff) | |
| download | Chimère-7b54448addd4eca66aff8b5d8f9e04ef7cbcd8fc.tar.bz2 Chimère-7b54448addd4eca66aff8b5d8f9e04ef7cbcd8fc.zip | |
Use a template to render PointChooser widget.
Integration of Raphaël Droz's work.
| -rw-r--r-- | chimere/templates/chimere/blocks/live_coordinates.html | 40 | ||||
| -rw-r--r-- | chimere/widgets.py | 63 | 
2 files changed, 61 insertions, 42 deletions
| diff --git a/chimere/templates/chimere/blocks/live_coordinates.html b/chimere/templates/chimere/blocks/live_coordinates.html new file mode 100644 index 0000000..4ed4e6f --- /dev/null +++ b/chimere/templates/chimere/blocks/live_coordinates.html @@ -0,0 +1,40 @@ +<script type='text/javascript'> +  OpenLayers.ImgPath = '%schimere/img/'; +  var EPSG_DISPLAY_PROJECTION = epsg_display_projection = new OpenLayers.Projection('EPSG:%s'); +  var EPSG_PROJECTION = epsg_projection = new OpenLayers.Projection('EPSG:%s'); +  var CENTER_LONLAT = centerLonLat = new OpenLayers.LonLat%s.transform(epsg_display_projection, epsg_projection); +  var DEFAULT_ZOOM = %s; +  var chimere_init_options = {}; +  chimere_init_options["default_icon"] = new OpenLayers.Icon('%schimere/img/marker-green.png', new OpenLayers.Size(21, 25), new OpenLayers.Pixel(-(21/2), -25)); +  chimere_init_options["map_layers"] = [%s]; +  chimere_init_options['dynamic_categories'] = false; +  chimere_init_options['edition'] = true; +  chimere_init_options["checked_categories"] = []; +  if({{default_area}}) chimere_init_options['selected_map_layer'] = {{default_area}}; +</script> + +</div> + + +<div id='map_edit'></div> +<div id='live_lonlat'> +  <p> +    <label for='live_latitude'>{{lat}}</label> +    <input type='texte' name='live_latitude' id='live_latitude' size='8' adisabled='true' value='{{value_y|stringformat:"f"}}'/> +  </p> +  <p> +    <label for='live_longitude'>{{lon}}</label> +    <input type='texte' name='live_longitude' id='live_longitude' size='8' adisabled='true' value='{{value_x|stringformat:"f"}}'/> +  </p> +  <input type='hidden' name='{{name}}' id='id_{{name}}' value='{{val}}'/> + +<script type="text/javascript"> +  $('#map_edit').chimere(chimere_init_options); +  if({{isvalue}}) { +    var mylonlat = new OpenLayers.LonLat({{value_x|stringformat:"f"}},{{value_y|stringformat:"f"}}); +    $("#map_edit").chimere( +        "putEditMarker", +        mylonlat.transform(EPSG_DISPLAY_PROJECTION, EPSG_PROJECTION), +        true); +  } +</script> diff --git a/chimere/widgets.py b/chimere/widgets.py index b60a661..87f797a 100644 --- a/chimere/widgets.py +++ b/chimere/widgets.py @@ -29,6 +29,7 @@ from django.core.exceptions import ObjectDoesNotExist  from django.core.urlresolvers import reverse  from django.utils.safestring import mark_safe  from django.utils.translation import ugettext as _ +from django.template.loader import render_to_string  import re @@ -182,49 +183,27 @@ class PointChooserWidget(forms.TextInput):              else:                  value = None          map_layers, default_area = get_map_layers(area_name) -        js = """ -        OpenLayers.ImgPath = '%schimere/img/'; -        var EPSG_DISPLAY_PROJECTION = epsg_display_projection = new OpenLayers.Projection('EPSG:%s'); -        var EPSG_PROJECTION = epsg_projection = new OpenLayers.Projection('EPSG:%s'); -        var CENTER_LONLAT = centerLonLat = new OpenLayers.LonLat%s.transform(epsg_display_projection, epsg_projection); -        var DEFAULT_ZOOM = %s; -        var chimere_init_options = {}; -        chimere_init_options["default_icon"] = new OpenLayers.Icon('%schimere/img/marker-green.png', new OpenLayers.Size(21, 25), new OpenLayers.Pixel(-(21/2), -25)); -        chimere_init_options["map_layers"] = [%s]; -        chimere_init_options['dynamic_categories'] = false; -        chimere_init_options['edition'] = true; -        chimere_init_options["checked_categories"] = []; -        """ % (settings.STATIC_URL, settings.CHIMERE_EPSG_DISPLAY_PROJECTION, -              settings.CHIMERE_EPSG_PROJECTION, settings.CHIMERE_DEFAULT_CENTER, -              settings.CHIMERE_DEFAULT_ZOOM, settings.STATIC_URL, -              ", ".join(map_layers)) -        if default_area: -            js += "chimere_init_options['selected_map_layer'] = %d;\n" % \ -                  default_area          #TODO: manage area -        tpl = u"<script type='text/javascript'><!--\n"\ -              u"%s// !--></script>\n" % js -        tpl += u"</div><div id='map_edit'></div>"\ -               u"<div id='live_lonlat'>"\ -               u"    <p><label for='live_latitude'>%s</label>"\ -               u" <input type='texte' name='live_latitude' id='live_latitude' "\ -               u"size='8' disabled='true' value='%f'/></p>"\ -               u"<p><label for='live_longitude'>%s</label><input type='texte' "\ -               u"name='live_longitude' id='live_longitude' size='8' "\ -               u"disabled='true' value='%f'/></p>"\ -               u"<input type='hidden' name='%s' id='id_%s' value='%s'/>" % ( -                    _("Latitude"), value_y, _("Longitude"), value_x, name, name, -                    val) -        tpl += "<script type='text/javascript'><!--\n" -        tpl += "$('#map_edit').chimere(chimere_init_options);\n" -        if value: -            tpl += u'var mylonlat = new OpenLayers.LonLat(%f,%f);\n'\ -                   u'$("#map_edit").chimere("putEditMarker", \n'\ -                   u'   mylonlat.transform(EPSG_DISPLAY_PROJECTION, \n'\ -                   u'                      EPSG_PROJECTION), true);\n' % ( -                                                             value_x, value_y) -        tpl += "// --></script>\n" -        return mark_safe(tpl) +        return mark_safe( +            render_to_string('chimere/blocks/live_coordinates.html', +                             {'lat': _("Latitude"), +                              'value_y': value_y, +                              'lon': _("Longitude"), +                              'value_x': value_x, +                              'name': name, +                              'val': val, +                              'isvalue': "true" if value else "false", +                              'default_area': "true" if default_area else "false", +                              }) % \ +            (settings.STATIC_URL, +             settings.CHIMERE_EPSG_DISPLAY_PROJECTION, +             settings.CHIMERE_EPSG_PROJECTION, +             settings.CHIMERE_DEFAULT_CENTER, +             settings.CHIMERE_DEFAULT_ZOOM, +             settings.STATIC_URL, +             ", ".join(map_layers) +             ) +            )  class PointField(models.PointField):      ''' | 
