DatePicker widget tls-sprint
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Thu, 19 Feb 2009 21:36:39 +0100
branchtls-sprint
changeset 865 90a1aea85637
parent 864 e0947007542a
child 866 6fdb029663ca
DatePicker widget
web/form.py
--- a/web/form.py	Thu Feb 19 21:36:22 2009 +0100
+++ b/web/form.py	Thu Feb 19 21:36:39 2009 +0100
@@ -318,7 +318,51 @@
 
 class CheckBox(FieldWidget):
 
-class DateTimePicker: pass
+    def _render_attrs(self, form, field):
+        name, value, attrs = super(CheckBox, self)._render_attrs(form, field)
+        if value:
+            attrs['checked'] = u'checked'
+        return name, None, attrs
+        
+class Radio(FieldWidget):
+    pass
+
+class DateTimePicker(TextInput):
+    monthnames = ("january", "february", "march", "april",
+                  "may", "june", "july", "august",
+                  "september", "october", "november", "december")
+    
+    daynames = ("monday", "tuesday", "wednesday", "thursday",
+                "friday", "saturday", "sunday")
+
+    @classmethod
+    def add_localized_infos(cls, req):
+        """inserts JS variables defining localized months and days"""
+        # import here to avoid dependancy from cubicweb-common to simplejson
+        _ = req._
+        monthnames = [_(mname) for mname in cls.monthnames]
+        daynames = [_(dname) for dname in cls.daynames]
+        req.html_headers.define_var('MONTHNAMES', monthnames)
+        req.html_headers.define_var('DAYNAMES', daynames)
+    
+    def render(self, form, field):
+        txtwidget = super(DateTimePicker, self).render(form, field)
+        cal_button - self._render_calendar_popup(form, field)
+        return txtwidget + cal_button
+    
+    def _render_calendar_popup(self, form, field):
+        req = form.req
+        name, value, attrs = self._render_attrs(form, field)
+        helperid = '%shelper' % name
+        if not value:
+            value = _today()
+        year, month = value.year, value.month
+        onclick = "toggleCalendar('%s', '%s', %s, %s);" % (
+            helperid, name, year, month)
+        return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
+<img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
+                % (helperid, inputid, year, month,
+                   self.iconurl, req._('calendar'), helperid) )
 
 
 # fields ############