web/formwidgets.py
changeset 3451 6b46d73823f5
parent 3388 b8be8fc77c27
child 3720 5376aaadd16b
equal deleted inserted replaced
3448:495862266785 3451:6b46d73823f5
    37             self.settabindex = settabindex
    37             self.settabindex = settabindex
    38 
    38 
    39     def add_media(self, form):
    39     def add_media(self, form):
    40         """adds media (CSS & JS) required by this widget"""
    40         """adds media (CSS & JS) required by this widget"""
    41         if self.needs_js:
    41         if self.needs_js:
    42             form.req.add_js(self.needs_js)
    42             form._cw.add_js(self.needs_js)
    43         if self.needs_css:
    43         if self.needs_css:
    44             form.req.add_css(self.needs_css)
    44             form._cw.add_css(self.needs_css)
    45 
    45 
    46     def render(self, form, field, renderer):
    46     def render(self, form, field, renderer):
    47         """render the widget for the given `field` of `form`.
    47         """render the widget for the given `field` of `form`.
    48         To override in concrete class
    48         To override in concrete class
    49         """
    49         """
    58             values = (values,)
    58             values = (values,)
    59         attrs = dict(self.attrs)
    59         attrs = dict(self.attrs)
    60         if self.setdomid:
    60         if self.setdomid:
    61             attrs['id'] = form.context[field]['id']
    61             attrs['id'] = form.context[field]['id']
    62         if self.settabindex and not 'tabindex' in attrs:
    62         if self.settabindex and not 'tabindex' in attrs:
    63             attrs['tabindex'] = form.req.next_tabindex()
    63             attrs['tabindex'] = form._cw.next_tabindex()
    64         return name, values, attrs
    64         return name, values, attrs
    65 
    65 
    66     def process_field_data(self, form, field):
    66     def process_field_data(self, form, field):
    67         formkey = form.form_field_name(field)
    67         formkey = form.form_field_name(field)
    68         posted = form.req.form
    68         posted = form._cw.form
    69         return posted.get(formkey)
    69         return posted.get(formkey)
    70 
    70 
    71 class Input(FieldWidget):
    71 class Input(FieldWidget):
    72     """abstract widget class for <input> tag based widgets"""
    72     """abstract widget class for <input> tag based widgets"""
    73     type = None
    73     type = None
   112         inputs = [tags.input(name=name, value=values[0], type=self.type, id=id,
   112         inputs = [tags.input(name=name, value=values[0], type=self.type, id=id,
   113                              **attrs),
   113                              **attrs),
   114                   '<br/>',
   114                   '<br/>',
   115                   tags.input(name=confirmname, value=values[0], type=self.type,
   115                   tags.input(name=confirmname, value=values[0], type=self.type,
   116                              **attrs),
   116                              **attrs),
   117                   '&#160;', tags.span(form.req._('confirm password'),
   117                   '&#160;', tags.span(form._cw._('confirm password'),
   118                                       **{'class': 'emphasis'})]
   118                                       **{'class': 'emphasis'})]
   119         return u'\n'.join(inputs)
   119         return u'\n'.join(inputs)
   120 
   120 
   121     def process_field_data(self, form, field):
   121     def process_field_data(self, form, field):
   122         passwd1 = super(PasswordInput, self).process_field_data(form, field)
   122         passwd1 = super(PasswordInput, self).process_field_data(form, field)
   123         fieldname = form.form_field_name(field)
   123         fieldname = form.form_field_name(field)
   124         passwd2 = form.req.form[fieldname+'-confirm']
   124         passwd2 = form._cw.form[fieldname+'-confirm']
   125         if passwd1 == passwd2:
   125         if passwd1 == passwd2:
   126             if passwd1 is None:
   126             if passwd1 is None:
   127                 return None
   127                 return None
   128             return passwd1.encode('utf-8')
   128             return passwd1.encode('utf-8')
   129         raise ProcessFormError(form.req._("password and confirmation don't match"))
   129         raise ProcessFormError(form._cw._("password and confirmation don't match"))
   130 
   130 
   131 class PasswordSingleInput(Input):
   131 class PasswordSingleInput(Input):
   132     """<input type='password'> without a confirmation field"""
   132     """<input type='password'> without a confirmation field"""
   133     type = 'password'
   133     type = 'password'
   134 
   134 
   190     def __init__(self, *args, **kwargs):
   190     def __init__(self, *args, **kwargs):
   191         super(FCKEditor, self).__init__(*args, **kwargs)
   191         super(FCKEditor, self).__init__(*args, **kwargs)
   192         self.attrs['cubicweb:type'] = 'wysiwyg'
   192         self.attrs['cubicweb:type'] = 'wysiwyg'
   193 
   193 
   194     def render(self, form, field, renderer):
   194     def render(self, form, field, renderer):
   195         form.req.fckeditor_config()
   195         form._cw.fckeditor_config()
   196         return super(FCKEditor, self).render(form, field, renderer)
   196         return super(FCKEditor, self).render(form, field, renderer)
   197 
   197 
   198 
   198 
   199 class Select(FieldWidget):
   199 class Select(FieldWidget):
   200     """<select>, for field having a specific vocabulary"""
   200     """<select>, for field having a specific vocabulary"""
   291     """
   291     """
   292     def render(self, form, field, renderer):
   292     def render(self, form, field, renderer):
   293         actual_fields = field.fields
   293         actual_fields = field.fields
   294         assert len(actual_fields) == 2
   294         assert len(actual_fields) == 2
   295         return u'<div>%s %s %s %s</div>' % (
   295         return u'<div>%s %s %s %s</div>' % (
   296             form.req._('from_interval_start'),
   296             form._cw._('from_interval_start'),
   297             actual_fields[0].render(form, renderer),
   297             actual_fields[0].render(form, renderer),
   298             form.req._('to_interval_end'),
   298             form._cw._('to_interval_end'),
   299             actual_fields[1].render(form, renderer),
   299             actual_fields[1].render(form, renderer),
   300             )
   300             )
   301 
   301 
   302 
   302 
   303 class HorizontalLayoutWidget(FieldWidget):
   303 class HorizontalLayoutWidget(FieldWidget):
   340         req.html_headers.define_var('MONTHNAMES', monthnames)
   340         req.html_headers.define_var('MONTHNAMES', monthnames)
   341         req.html_headers.define_var('DAYNAMES', daynames)
   341         req.html_headers.define_var('DAYNAMES', daynames)
   342 
   342 
   343     def render(self, form, field, renderer):
   343     def render(self, form, field, renderer):
   344         txtwidget = super(DateTimePicker, self).render(form, field, renderer)
   344         txtwidget = super(DateTimePicker, self).render(form, field, renderer)
   345         self.add_localized_infos(form.req)
   345         self.add_localized_infos(form._cw)
   346         cal_button = self._render_calendar_popup(form, field)
   346         cal_button = self._render_calendar_popup(form, field)
   347         return txtwidget + cal_button
   347         return txtwidget + cal_button
   348 
   348 
   349     def _render_calendar_popup(self, form, field):
   349     def _render_calendar_popup(self, form, field):
   350         value = form.form_field_value(field)
   350         value = form.form_field_value(field)
   354         helperid = '%shelper' % inputid
   354         helperid = '%shelper' % inputid
   355         year, month = value.year, value.month
   355         year, month = value.year, value.month
   356         return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
   356         return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
   357 <img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
   357 <img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
   358                 % (helperid, inputid, year, month,
   358                 % (helperid, inputid, year, month,
   359                    form.req.external_resource('CALENDAR_ICON'),
   359                    form._cw.external_resource('CALENDAR_ICON'),
   360                    form.req._('calendar'), helperid) )
   360                    form._cw._('calendar'), helperid) )
   361 
   361 
   362 
   362 
   363 
   363 
   364 # ajax widgets ################################################################
   364 # ajax widgets ################################################################
   365 
   365 
   417         if self.autocomplete_initfunc is None:
   417         if self.autocomplete_initfunc is None:
   418             # XXX for bw compat
   418             # XXX for bw compat
   419             fname = entity.autocomplete_initfuncs[field.name]
   419             fname = entity.autocomplete_initfuncs[field.name]
   420         else:
   420         else:
   421             fname = self.autocomplete_initfunc
   421             fname = self.autocomplete_initfunc
   422         return entity.req.build_url('json', fname=fname, mode='remote',
   422         return entity._cw.build_url('json', fname=fname, mode='remote',
   423                                     pageid=entity.req.pageid)
   423                                     pageid=entity._cw.pageid)
   424 
   424 
   425 
   425 
   426 class StaticFileAutoCompletionWidget(AutoCompletionWidget):
   426 class StaticFileAutoCompletionWidget(AutoCompletionWidget):
   427     """XXX describe me"""
   427     """XXX describe me"""
   428     wdgtype = 'StaticFileSuggestField'
   428     wdgtype = 'StaticFileSuggestField'
   431         if self.autocomplete_initfunc is None:
   431         if self.autocomplete_initfunc is None:
   432             # XXX for bw compat
   432             # XXX for bw compat
   433             fname = entity.autocomplete_initfuncs[field.name]
   433             fname = entity.autocomplete_initfuncs[field.name]
   434         else:
   434         else:
   435             fname = self.autocomplete_initfunc
   435             fname = self.autocomplete_initfunc
   436         return entity.req.datadir_url + fname
   436         return entity._cw.datadir_url + fname
   437 
   437 
   438 
   438 
   439 class RestrictedAutoCompletionWidget(AutoCompletionWidget):
   439 class RestrictedAutoCompletionWidget(AutoCompletionWidget):
   440     """XXX describe me"""
   440     """XXX describe me"""
   441     wdgtype = 'RestrictedSuggestField'
   441     wdgtype = 'RestrictedSuggestField'
   478         self.onclick = onclick
   478         self.onclick = onclick
   479         self.cwaction = cwaction
   479         self.cwaction = cwaction
   480         self.attrs.setdefault('klass', 'validateButton')
   480         self.attrs.setdefault('klass', 'validateButton')
   481 
   481 
   482     def render(self, form, field=None, renderer=None):
   482     def render(self, form, field=None, renderer=None):
   483         label = form.req._(self.label)
   483         label = form._cw._(self.label)
   484         attrs = self.attrs.copy()
   484         attrs = self.attrs.copy()
   485         if self.cwaction:
   485         if self.cwaction:
   486             assert self.onclick is None
   486             assert self.onclick is None
   487             attrs['onclick'] = "postForm('__action_%s', \'%s\', \'%s\')" % (
   487             attrs['onclick'] = "postForm('__action_%s', \'%s\', \'%s\')" % (
   488                 self.cwaction, self.label, form.domid)
   488                 self.cwaction, self.label, form.domid)
   491         if self.name:
   491         if self.name:
   492             attrs['name'] = name
   492             attrs['name'] = name
   493             if self.setdomid:
   493             if self.setdomid:
   494                 attrs['id'] = self.name
   494                 attrs['id'] = self.name
   495         if self.settabindex and not 'tabindex' in attrs:
   495         if self.settabindex and not 'tabindex' in attrs:
   496             attrs['tabindex'] = form.req.next_tabindex()
   496             attrs['tabindex'] = form._cw.next_tabindex()
   497         return tags.input(value=label, type=self.type, **attrs)
   497         return tags.input(value=label, type=self.type, **attrs)
   498 
   498 
   499 
   499 
   500 class SubmitButton(Button):
   500 class SubmitButton(Button):
   501     """<input type='submit'>, main button to submit a form"""
   501     """<input type='submit'>, main button to submit a form"""
   521         self.href = href
   521         self.href = href
   522         self.imgressource = imgressource
   522         self.imgressource = imgressource
   523         self.label = label
   523         self.label = label
   524 
   524 
   525     def render(self, form, field=None, renderer=None):
   525     def render(self, form, field=None, renderer=None):
   526         label = form.req._(self.label)
   526         label = form._cw._(self.label)
   527         imgsrc = form.req.external_resource(self.imgressource)
   527         imgsrc = form._cw.external_resource(self.imgressource)
   528         return '<a id="%(domid)s" href="%(href)s">'\
   528         return '<a id="%(domid)s" href="%(href)s">'\
   529                '<img src="%(imgsrc)s" alt="%(label)s"/>%(label)s</a>' % {
   529                '<img src="%(imgsrc)s" alt="%(label)s"/>%(label)s</a>' % {
   530             'label': label, 'imgsrc': imgsrc,
   530             'label': label, 'imgsrc': imgsrc,
   531             'domid': self.domid, 'href': self.href}
   531             'domid': self.domid, 'href': self.href}
   532 
   532