summaryrefslogtreecommitdiff
path: root/main/widgets.py
diff options
context:
space:
mode:
Diffstat (limited to 'main/widgets.py')
-rw-r--r--main/widgets.py81
1 files changed, 80 insertions, 1 deletions
diff --git a/main/widgets.py b/main/widgets.py
index bd0a72b..1e12c19 100644
--- a/main/widgets.py
+++ b/main/widgets.py
@@ -23,7 +23,7 @@ Extra widgets and fields
from django import forms
from django.utils.safestring import mark_safe
-from django.utils.translation import ugettext_lazy as _
+from django.utils.translation import ugettext as _
from chimere import settings
from django.contrib.gis.db import models
@@ -112,3 +112,82 @@ class PointField(models.PointField):
defaults = {'widget': PointChooserWidget}
keys.update(defaults)
return super(PointField, self).formfield(**keys)
+
+class RouteChooserWidget(forms.TextInput):
+ """
+ Manage the edition of route on a map
+ """
+ class Media:
+ css = {
+ "all": ("%sforms.css" % settings.MEDIA_URL,)
+ }
+ js = ["%sedit_route_map.js" % settings.MEDIA_URL] + URL_OSM_JS
+
+ def render(self, name, value, attrs=None):
+ '''
+ Render a map and latitude, longitude information field
+ '''
+ tpl = getMapJS()
+ help_create = ''
+ if not value:
+ help_create = """<h3>%s</h3>
+<p>%s</p>
+<p>%s</p>
+<p>%s</p>
+<p>%s</p>
+<p>%s</p>""" % (_("Creation mode"),
+_("To start drawing the route click on the toggle button : \"Start drawing\"."),
+_("Then click on the map to begin the drawing."),
+_("You can add points by clicking again."),
+_("To finish the drawing double click. When the drawing is finished you can \
+edit it."),
+_("While creating to undo a drawing click again on the toggle button \"Stop \
+drawing\"."))
+ help_modify = """<h3>%s</h3>
+<p>%s</p>
+<p>%s</p>
+<p>%s</p>""" % (_("Modification mode"),
+_("To move a point click on it and drag it to the desired position."),
+_("To delete a point move the mouse cursor over it and press the \"d\" key."),
+_("To add a point click in the middle of a segment and drag the new point to \
+the desired position"))
+ tpl += u'<script src="%sedit_route_map.js"></script>\n' % \
+ settings.MEDIA_URL
+ if not value:
+ tpl += u"""<div id='draw-toggle-off' class='toggle-button' \
+onclick='toggleDrawOn();'>%s</div>
+<div id='draw-toggle-on' class='toggle-button' \
+onclick='toggleDrawOff();'>%s</div>
+<hr class='spacer'/>""" % (_("Start drawing"), _("Stop drawing"))
+ tpl += """
+<div id='map_edit'></div>"""
+ if not value:
+ tpl += '''
+<div class='help-route' id='help-route-create'>%s</div>''' % help_create
+ style = ''
+ if value:
+ style = " style='display:block'"
+ tpl += """
+<div class='help-route' id='help-route-modify'%s>%s</div>
+<hr class='spacer'/>
+<input type='hidden' name='%s' id='id_%s' value="%s"/>
+""" % (style, help_modify, name, name, value)
+ tpl += """<script type='text/javascript'><!--
+init();"""
+ if value:
+ tpl += """
+var geometry='%s';
+initFeature(geometry);""" % value.json
+ tpl += """
+// --></script>
+"""
+ return mark_safe(tpl)
+
+class RouteField(models.LineStringField):
+ '''
+ Set the widget for the form field
+ '''
+ def formfield(self, **keys):
+ defaults = {'widget': RouteChooserWidget}
+ keys.update(defaults)
+ return super(RouteField, self).formfield(**keys)