web/formwidgets.py
changeset 9821 2077c8da1893
parent 9718 927d035b6921
parent 9770 112c884b2d8d
child 10222 75d6096216d7
child 10404 797db633d60c
equal deleted inserted replaced
9819:95902c0b991b 9821:2077c8da1893
   311 
   311 
   312 
   312 
   313 # basic html widgets ###########################################################
   313 # basic html widgets ###########################################################
   314 
   314 
   315 class TextInput(Input):
   315 class TextInput(Input):
   316     """Simple <input type='text'>, will return an unicode string."""
   316     """Simple <input type='text'>, will return a unicode string."""
   317     type = 'text'
   317     type = 'text'
   318 
   318 
   319 
   319 
   320 class EmailInput(Input):
   320 class EmailInput(Input):
   321     """Simple <input type='email'>, will return a unicode string."""
   321     """Simple <input type='email'>, will return a unicode string."""
   322     type = 'email'
   322     type = 'email'
   323 
   323 
   324 
   324 
   325 class PasswordSingleInput(Input):
   325 class PasswordSingleInput(Input):
   326     """Simple <input type='password'>, will return an utf-8 encoded string.
   326     """Simple <input type='password'>, will return a utf-8 encoded string.
   327 
   327 
   328     You may prefer using the :class:`~cubicweb.web.formwidgets.PasswordInput`
   328     You may prefer using the :class:`~cubicweb.web.formwidgets.PasswordInput`
   329     widget which handles password confirmation.
   329     widget which handles password confirmation.
   330     """
   330     """
   331     type = 'password'
   331     type = 'password'
   338 
   338 
   339 
   339 
   340 class PasswordInput(Input):
   340 class PasswordInput(Input):
   341     """<input type='password'> and a confirmation input. Form processing will
   341     """<input type='password'> and a confirmation input. Form processing will
   342     fail if password and confirmation differs, else it will return the password
   342     fail if password and confirmation differs, else it will return the password
   343     as an utf-8 encoded string.
   343     as a utf-8 encoded string.
   344     """
   344     """
   345     type = 'password'
   345     type = 'password'
   346 
   346 
   347     def _render(self, form, field, renderer):
   347     def _render(self, form, field, renderer):
   348         assert self.suffix is None, 'suffix not supported'
   348         assert self.suffix is None, 'suffix not supported'
   379         # ignore value which makes no sense here (XXX even on form validation error?)
   379         # ignore value which makes no sense here (XXX even on form validation error?)
   380         return ('',)
   380         return ('',)
   381 
   381 
   382 
   382 
   383 class HiddenInput(Input):
   383 class HiddenInput(Input):
   384     """Simple <input type='hidden'> for hidden value, will return an unicode
   384     """Simple <input type='hidden'> for hidden value, will return a unicode
   385     string.
   385     string.
   386     """
   386     """
   387     type = 'hidden'
   387     type = 'hidden'
   388     setdomid = False # by default, don't set id attribute on hidden input
   388     setdomid = False # by default, don't set id attribute on hidden input
   389     settabindex = False
   389     settabindex = False
   390 
   390 
   391 
   391 
   392 class ButtonInput(Input):
   392 class ButtonInput(Input):
   393     """Simple <input type='button'>, will return an unicode string.
   393     """Simple <input type='button'>, will return a unicode string.
   394 
   394 
   395     If you want a global form button, look at the :class:`Button`,
   395     If you want a global form button, look at the :class:`Button`,
   396     :class:`SubmitButton`, :class:`ResetButton` and :class:`ImgButton` below.
   396     :class:`SubmitButton`, :class:`ResetButton` and :class:`ImgButton` below.
   397     """
   397     """
   398     type = 'button'
   398     type = 'button'
   399 
   399 
   400 
   400 
   401 class TextArea(FieldWidget):
   401 class TextArea(FieldWidget):
   402     """Simple <textarea>, will return an unicode string."""
   402     """Simple <textarea>, will return a unicode string."""
   403 
   403 
   404     def _render(self, form, field, renderer):
   404     def _render(self, form, field, renderer):
   405         values, attrs = self.values_and_attributes(form, field)
   405         values, attrs = self.values_and_attributes(form, field)
   406         attrs.setdefault('onkeyup', 'autogrow(this)')
   406         attrs.setdefault('onkeyup', 'autogrow(this)')
   407         if not values:
   407         if not values:
   419         return tags.textarea(value, name=field.input_name(form, self.suffix),
   419         return tags.textarea(value, name=field.input_name(form, self.suffix),
   420                              **attrs)
   420                              **attrs)
   421 
   421 
   422 
   422 
   423 class FCKEditor(TextArea):
   423 class FCKEditor(TextArea):
   424     """FCKEditor enabled <textarea>, will return an unicode string containing
   424     """FCKEditor enabled <textarea>, will return a unicode string containing
   425     HTML formated text.
   425     HTML formated text.
   426     """
   426     """
   427     def __init__(self, *args, **kwargs):
   427     def __init__(self, *args, **kwargs):
   428         super(FCKEditor, self).__init__(*args, **kwargs)
   428         super(FCKEditor, self).__init__(*args, **kwargs)
   429         self.attrs['cubicweb:type'] = 'wysiwyg'
   429         self.attrs['cubicweb:type'] = 'wysiwyg'
   433         return super(FCKEditor, self)._render(form, field, renderer)
   433         return super(FCKEditor, self)._render(form, field, renderer)
   434 
   434 
   435 
   435 
   436 class Select(FieldWidget):
   436 class Select(FieldWidget):
   437     """Simple <select>, for field having a specific vocabulary. Will return
   437     """Simple <select>, for field having a specific vocabulary. Will return
   438     an unicode string, or a list of unicode strings.
   438     a unicode string, or a list of unicode strings.
   439     """
   439     """
   440     vocabulary_widget = True
   440     vocabulary_widget = True
   441     default_size = 10
   441     default_size = 10
   442 
   442 
   443     def __init__(self, attrs=None, multiple=False, **kwargs):
   443     def __init__(self, attrs=None, multiple=False, **kwargs):
   629 
   629 
   630 # javascript widgets ###########################################################
   630 # javascript widgets ###########################################################
   631 
   631 
   632 class DateTimePicker(TextInput):
   632 class DateTimePicker(TextInput):
   633     """<input type='text'> + javascript date/time picker for date or datetime
   633     """<input type='text'> + javascript date/time picker for date or datetime
   634     fields. Will return the date or datetime as an unicode string.
   634     fields. Will return the date or datetime as a unicode string.
   635     """
   635     """
   636     monthnames = ('january', 'february', 'march', 'april',
   636     monthnames = ('january', 'february', 'march', 'april',
   637                   'may', 'june', 'july', 'august',
   637                   'may', 'june', 'july', 'august',
   638                   'september', 'october', 'november', 'december')
   638                   'september', 'october', 'november', 'december')
   639     daynames = ('monday', 'tuesday', 'wednesday', 'thursday',
   639     daynames = ('monday', 'tuesday', 'wednesday', 'thursday',
   671                    form._cw._('calendar'), helperid) )
   671                    form._cw._('calendar'), helperid) )
   672 
   672 
   673 
   673 
   674 class JQueryDatePicker(FieldWidget):
   674 class JQueryDatePicker(FieldWidget):
   675     """Use jquery.ui.datepicker to define a date picker. Will return the date as
   675     """Use jquery.ui.datepicker to define a date picker. Will return the date as
   676     an unicode string.
   676     a unicode string.
   677     """
   677     """
   678     needs_js = ('jquery.ui.js', )
   678     needs_js = ('jquery.ui.js', )
   679     needs_css = ('jquery.ui.css',)
   679     needs_css = ('jquery.ui.css',)
   680     default_size = 10
   680     default_size = 10
   681 
   681 
   931             fields = [f.render(form, renderer) for f in field.subfields(form)]
   931             fields = [f.render(form, renderer) for f in field.subfields(form)]
   932         return u'<div>%s</div>' % ' '.join(fields)
   932         return u'<div>%s</div>' % ' '.join(fields)
   933 
   933 
   934 
   934 
   935 class EditableURLWidget(FieldWidget):
   935 class EditableURLWidget(FieldWidget):
   936     """Custom widget to edit separatly an url path / query string (used by
   936     """Custom widget to edit separatly a URL path / query string (used by
   937     default for the `path` attribute of `Bookmark` entities).
   937     default for the `path` attribute of `Bookmark` entities).
   938 
   938 
   939     It deals with url quoting nicely so that the user edit the unquoted value.
   939     It deals with url quoting nicely so that the user edit the unquoted value.
   940     """
   940     """
   941 
   941