add needs_css and needs_js on widgets tls-sprint
authorAdrien Di Mascio <Adrien.DiMascio@logilab.fr>
Fri, 20 Feb 2009 17:20:29 +0100
branchtls-sprint
changeset 905 64fd6eaaa9a4
parent 904 4f1ce95aa686
child 906 c26156f0885e
add needs_css and needs_js on widgets
web/form.py
--- a/web/form.py	Fri Feb 20 17:18:54 2009 +0100
+++ b/web/form.py	Fri Feb 20 17:20:29 2009 +0100
@@ -250,9 +250,20 @@
 # widgets ############
 
 class FieldWidget(object):
+    needs_js = ()
+    needs_css = ()
+    
     def __init__(self, attrs=None):
         self.attrs = attrs or {}
 
+    def add_media(self, form):
+        """adds media (CSS & JS) required by this widget"""
+        req = form.req
+        if self.needs_js:
+            req.add_js(self.needs_js)
+        if self.needs_css:
+            req.add_css(self.needs_css)
+        
     def render(self, form, field):
         raise NotImplementedError
 
@@ -269,6 +280,7 @@
     type = None
     
     def render(self, form, field):
+        self.add_media(form)
         name, values, attrs = self._render_attrs(form, field)
         inputs = [tags.input(name=name, value=value, type=self.type, **attrs)
                   for value in values]
@@ -356,6 +368,9 @@
     daynames = ("monday", "tuesday", "wednesday", "thursday",
                 "friday", "saturday", "sunday")
 
+    needs_js = ('cubicweb.ajax.js', 'cubicweb.calendar.js')
+    needs_css = ('cubicweb.calendar_popup.css',)
+    
     @classmethod
     def add_localized_infos(cls, req):
         """inserts JS variables defining localized months and days"""
@@ -537,6 +552,7 @@
 
 class DateField(StringField):
     format_prop = 'ui.date-format'
+    widget = DateTimePicker
     
     def format_single_value(self, req, value):
         return value and ustrftime(value, req.property_value(self.format_prop)) or u''
@@ -898,7 +914,6 @@
         self.forms.append(subform)
         
 # form renderers ############
-
 class FormRenderer(object):
     
     def render(self, form, values):