summaryrefslogtreecommitdiff
path: root/chimere/widgets.py
diff options
context:
space:
mode:
Diffstat (limited to 'chimere/widgets.py')
-rw-r--r--chimere/widgets.py164
1 files changed, 31 insertions, 133 deletions
diff --git a/chimere/widgets.py b/chimere/widgets.py
index 6d61965..b4bbee6 100644
--- a/chimere/widgets.py
+++ b/chimere/widgets.py
@@ -26,7 +26,6 @@ from json import JSONEncoder
from django import forms
from django.conf import settings
from django.contrib.gis.db import models
-from django.contrib.gis.geos import fromstr
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.forms.widgets import RadioInput, RadioFieldRenderer, flatatt
@@ -401,13 +400,11 @@ class PointChooserWidget(forms.TextInput):
'default_area': "true" if default_area
else "false",
}) %
- (settings.STATIC_URL,
- settings.CHIMERE_EPSG_DISPLAY_PROJECTION,
+ (settings.CHIMERE_EPSG_DISPLAY_PROJECTION,
settings.CHIMERE_EPSG_PROJECTION,
"[{}, {}]".format(settings.CHIMERE_DEFAULT_CENTER[0],
settings.CHIMERE_DEFAULT_CENTER[1]),
settings.CHIMERE_DEFAULT_ZOOM,
- settings.STATIC_URL,
", ".join(map_layers),
zoom
)
@@ -454,137 +451,37 @@ class RouteChooserWidget(forms.TextInput):
["%schimere/css/forms.css" % settings.STATIC_URL]
}
js = settings.MAP_JS_URLS + list(settings.JQUERY_JS_URLS) + \
- ["%schimere/js/jquery.chimere.js" % settings.STATIC_URL,
- "%schimere/js/edit_route_map.js" % settings.STATIC_URL,
- "%schimere/js/base.js" % settings.STATIC_URL]
+ ["%schimere/js/jquery.chimere.js" % settings.STATIC_URL]
- def render(self, name, value, attrs=None, area_name='', routefile_id=None):
- '''
- Render a map and latitude, longitude information field
- '''
- tpl = getMapJS(area_name)
- map_layers, default_area = get_map_layers(area_name)
+ def render(self, name, value, attrs=None, area_name='', routefile_id=None,
+ initialized=True):
+ val = ''
+ if value:
+ val = str(value)
+ map_layers, default_area, zoom = get_map_layers(area_name,
+ get_area_zoom=True)
extra_js = [extra_js for n, js, default, extra_js in map_layers
if extra_js]
- map_layers = [js for nm, js, default, ext_js in map_layers]
- js = u"""
- var resolutions;
- var zoomOffset;
-
- var extra_url = "%s";
- epsg_display_projection = 'EPSG:%s';
- var EPSG_DISPLAY_PROJECTION = epsg_display_projection;
- epsg_projection = 'EPSG:%s';
- var EPSG_PROJECTION = epsg_projection;
- centerLonLat = ol.proj.transform([%f %f],
- epsg_display_projection, epsg_projection);
- var CENTER_LONLAT = centerLonLat;
- var DEFAULT_ZOOM = %s;
- var chimere_init_options = {};
- %s
- chimere_init_options["map_layers"] = [%s];
- chimere_init_options['dynamic_categories'] = false;
- chimere_init_options['edition'] = true;
- chimere_init_options['edition_type_is_route'] = true;
- chimere_init_options["checked_categories"] = [];
- """ % (reverse("chimere:index"),
- settings.CHIMERE_EPSG_DISPLAY_PROJECTION,
- settings.CHIMERE_EPSG_PROJECTION,
- settings.CHIMERE_DEFAULT_CENTER[0],
- settings.CHIMERE_DEFAULT_CENTER[1],
- settings.CHIMERE_DEFAULT_ZOOM,
- u"\n".join(extra_js),
- u", ".join(map_layers))
- if default_area:
- js += u"chimere_init_options['selected_map_layer'] = %d;\n" % \
- default_area
- tpl = u"<script type='text/javascript'><!--\n"\
- u"%s// !--></script>\n" % js
- # TODO: manage area
- help_create = ''
- if not value:
- help_create = u"<h3>%s</h3>\n"\
- u"<p>%s</p>\n"\
- u"<p>%s</p>\n"\
- u"<p>%s</p>\n"\
- u"<p>%s</p>\n"\
- u"<p>%s</p>\n" % (
- _(u"Creation mode"),
- _(u"To start drawing the route click on the toggle "
- u"button: \"Draw\"."),
- _(u"Then click on the map to begin the drawing."),
- _(u"You can add points by clicking again."),
- _(u"To finish the drawing double click. When the drawing "
- u"is finished you can edit it."),
- _(u"While creating to undo a drawing click again on the "
- u"toggle button \"Stop drawing\"."))
- help_modify = u"<h3>%s</h3>\n"\
- u"<p>%s</p>\n"\
- u"<p>%s</p>\n"\
- u"<p>%s</p>\n" % (
- _(u"Modification mode"),
- _(u"To move a point click on it and drag it to the desired "
- u"position."),
- _(u"To delete a point move the mouse cursor over it and press "
- u"the \"d\" or \"Del\" key."),
- _(u"To add a point click in the middle of a segment and drag "
- u"the new point to the desired position"))
- if not value:
- # upload a file
- tpl += u"<script type='text/javascript'><!--\n"\
- u" var error_msg = \"%s\";"\
- u"// --></script>" % \
- _(u"Give a name and set category before uploading a file.")
- tpl += u'<div id="upload"><a href="#" class="upload-button" '\
- u'onclick="uploadFile(error_msg);return false;">%s</a></div>' \
- % (_(u"Upload a route file (GPX or KML)"))
- tpl += u"\n<p id='draw-or'>%s</p>\n" % _(u"or")
- tpl += u"<div id='draw-label'><div id='draw-toggle-off' "\
- u"onclick='toggleDraw();'>\n"\
- u"<a href='#' onclick='return false;'>%s</a></div>"\
- u"</div>\n"\
- u"<hr class='spacer'/>" % (_(u"Start \"hand\" drawing"))
- style = ''
- if value:
- style = " style='display:block'"
- tpl += u"\n<div class='help-route' id='help-route-modify'%s>%s</div>"\
- u"\n<hr class='spacer'/>\n"\
- u"<input type='hidden' name='%s' id='id_%s' value=\"%s\"/>\n"\
- u"<input type='hidden' name='associated_file_id' "\
- u"id='id_associated_file_id' value=\"%s\"/>\n" % (
- style, help_modify, name, name, value, routefile_id)
- if value:
- tpl += u"\n<div id='map_edit'></div>"
- else:
- tpl += "\n<div class='help-route' id='help-route-create'>%s</div>"\
- % help_create
- tpl += \
- u"\n<div id='layerSwitcher'></div>\n<div id='map_edit'>\n"\
- u" <div class='map_button'>\n"\
- u" <a href='#' id='button-move-map' class='toggle-button "\
- u"toggle-button-active' "\
- u"onclick='toggleDrawOff();return false;'>%s</a>\n"\
- u"<a href='#' id='button-draw-map' class='toggle-button "\
- u"toggle-button-inactive' "\
- u"onclick='toggleDrawOn();return false;'>%s</a></div>\n"\
- u" </div>" % (_(u"Move on the map"), _(u"Draw"))
- tpl += u"<script type='text/javascript'><!--\n"
- if not value:
- tpl += u"jQuery('#map_edit').hide();"
- if value:
- tpl += u"jQuery('#map_edit').chimere(chimere_init_options);\n"
- val = value
- if type(value) == unicode:
- try:
- val = fromstr(value)
- except:
- pass
- if hasattr(val, 'json'):
- tpl += u"\nvar geometry='%s';\n"\
- u"jQuery('#map_edit').chimere('initFeature', geometry);"\
- % val.json
- tpl += u"\n// --></script>\n"
- return mark_safe(tpl)
+ map_layers = [js for n, js, default, ext_js in map_layers
+ if 'OpenLayers' not in js]
+ tpl = render_to_string(
+ 'chimere/blocks/edit_widget.html',
+ {'name': name, 'val': val, 'initialized': initialized,
+ 'isvalue': bool(value),
+ 'default_area': "true" if default_area else "false",
+ 'value': value
+ }
+ )
+ return mark_safe(tpl.format(
+ static_url=settings.STATIC_URL,
+ display_projection=settings.CHIMERE_EPSG_DISPLAY_PROJECTION,
+ projection=settings.CHIMERE_EPSG_PROJECTION,
+ center=list(settings.CHIMERE_DEFAULT_CENTER),
+ zoom=zoom,
+ edition_type="route",
+ map_layers=u", ".join(map_layers),
+ extra_js=u"\n".join(extra_js),
+ ))
class RouteField(models.LineStringField):
@@ -717,7 +614,7 @@ class PolygonChooserWidget(forms.TextInput):
map_layers = [js for n, js, default, ext_js in map_layers
if 'OpenLayers' not in js]
tpl = render_to_string(
- 'chimere/blocks/polygon_edit.html',
+ 'chimere/blocks/edit_widget.html',
{'name': name, 'val': val, 'initialized': initialized,
'isvalue': bool(value),
'default_area': "true" if default_area else "false",
@@ -730,6 +627,7 @@ class PolygonChooserWidget(forms.TextInput):
projection=settings.CHIMERE_EPSG_PROJECTION,
center=list(settings.CHIMERE_DEFAULT_CENTER),
zoom=zoom,
+ edition_type="polygon",
map_layers=u", ".join(map_layers),
extra_js=u"\n".join(extra_js),
))