web/formwidgets.py
changeset 2522 562f5dcf2345
parent 2519 ac1a869e1e93
child 2523 1d245fbbeb90
equal deleted inserted replaced
2521:782360fa6cc0 2522:562f5dcf2345
    41         if self.needs_js:
    41         if self.needs_js:
    42             form.req.add_js(self.needs_js)
    42             form.req.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.req.add_css(self.needs_css)
    45 
    45 
    46     def render(self, form, field):
    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         """
    50         raise NotImplementedError
    50         raise NotImplementedError
    51 
    51 
    66 
    66 
    67 class Input(FieldWidget):
    67 class Input(FieldWidget):
    68     """abstract widget class for <input> tag based widgets"""
    68     """abstract widget class for <input> tag based widgets"""
    69     type = None
    69     type = None
    70 
    70 
    71     def render(self, form, field):
    71     def render(self, form, field, renderer):
    72         """render the widget for the given `field` of `form`.
    72         """render the widget for the given `field` of `form`.
    73 
    73 
    74         Generate one <input> tag for each field's value
    74         Generate one <input> tag for each field's value
    75         """
    75         """
    76         self.add_media(form)
    76         self.add_media(form)
    94     """<input type='password'> and its confirmation field (using
    94     """<input type='password'> and its confirmation field (using
    95     <field's name>-confirm as name)
    95     <field's name>-confirm as name)
    96     """
    96     """
    97     type = 'password'
    97     type = 'password'
    98 
    98 
    99     def render(self, form, field):
    99     def render(self, form, field, renderer):
   100         self.add_media(form)
   100         self.add_media(form)
   101         name, values, attrs = self._render_attrs(form, field)
   101         name, values, attrs = self._render_attrs(form, field)
   102         assert len(values) == 1
   102         assert len(values) == 1
   103         id = attrs.pop('id')
   103         id = attrs.pop('id')
   104         try:
   104         try:
   105             confirmname = '%s-confirm:%s' % tuple(name.rsplit(':', 1))
   105             confirmname = '%s-confirm:%s' % tuple(name.rsplit(':', 1))
   106         except TypeError:
   106         except TypeError:
   107             confirmname = '%s-confirm' % name
   107             confirmname = '%s-confirm' % name
   108         inputs = [tags.input(name=name, value=values[0], type=self.type, id=id, **attrs),
   108         inputs = [tags.input(name=name, value=values[0], type=self.type, id=id,
       
   109                              **attrs),
   109                   '<br/>',
   110                   '<br/>',
   110                   tags.input(name=confirmname, value=values[0], type=self.type, **attrs),
   111                   tags.input(name=confirmname, value=values[0], type=self.type,
       
   112                              **attrs),
   111                   '&nbsp;', tags.span(form.req._('confirm password'),
   113                   '&nbsp;', tags.span(form.req._('confirm password'),
   112                                       **{'class': 'emphasis'})]
   114                                       **{'class': 'emphasis'})]
   113         return u'\n'.join(inputs)
   115         return u'\n'.join(inputs)
   114 
   116 
   115 
   117 
   145 
   147 
   146 
   148 
   147 class TextArea(FieldWidget):
   149 class TextArea(FieldWidget):
   148     """<textarea>"""
   150     """<textarea>"""
   149 
   151 
   150     def render(self, form, field):
   152     def render(self, form, field, renderer):
   151         name, values, attrs = self._render_attrs(form, field)
   153         name, values, attrs = self._render_attrs(form, field)
   152         attrs.setdefault('onkeyup', 'autogrow(this)')
   154         attrs.setdefault('onkeyup', 'autogrow(this)')
   153         if not values:
   155         if not values:
   154             value = u''
   156             value = u''
   155         elif len(values) == 1:
   157         elif len(values) == 1:
   169     """FCKEditor enabled <textarea>"""
   171     """FCKEditor enabled <textarea>"""
   170     def __init__(self, *args, **kwargs):
   172     def __init__(self, *args, **kwargs):
   171         super(FCKEditor, self).__init__(*args, **kwargs)
   173         super(FCKEditor, self).__init__(*args, **kwargs)
   172         self.attrs['cubicweb:type'] = 'wysiwyg'
   174         self.attrs['cubicweb:type'] = 'wysiwyg'
   173 
   175 
   174     def render(self, form, field):
   176     def render(self, form, field, renderer):
   175         form.req.fckeditor_config()
   177         form.req.fckeditor_config()
   176         return super(FCKEditor, self).render(form, field)
   178         return super(FCKEditor, self).render(form, field, renderer)
   177 
   179 
   178 
   180 
   179 class Select(FieldWidget):
   181 class Select(FieldWidget):
   180     """<select>, for field having a specific vocabulary"""
   182     """<select>, for field having a specific vocabulary"""
   181     vocabulary_widget = True
   183     vocabulary_widget = True
   182 
   184 
   183     def __init__(self, attrs=None, multiple=False):
   185     def __init__(self, attrs=None, multiple=False):
   184         super(Select, self).__init__(attrs)
   186         super(Select, self).__init__(attrs)
   185         self._multiple = multiple
   187         self._multiple = multiple
   186 
   188 
   187     def render(self, form, field):
   189     def render(self, form, field, renderer):
   188         name, curvalues, attrs = self._render_attrs(form, field)
   190         name, curvalues, attrs = self._render_attrs(form, field)
   189         if not 'size' in attrs:
   191         if not 'size' in attrs:
   190             attrs['size'] = self._multiple and '5' or '1'
   192             attrs['size'] = self._multiple and '5' or '1'
   191         options = []
   193         options = []
   192         optgroup_opened = False
   194         optgroup_opened = False
   219     input will be generated for each possible value.
   221     input will be generated for each possible value.
   220     """
   222     """
   221     type = 'checkbox'
   223     type = 'checkbox'
   222     vocabulary_widget = True
   224     vocabulary_widget = True
   223 
   225 
   224     def render(self, form, field):
   226     def render(self, form, field, renderer):
   225         name, curvalues, attrs = self._render_attrs(form, field)
   227         name, curvalues, attrs = self._render_attrs(form, field)
   226         domid = attrs.pop('id', None)
   228         domid = attrs.pop('id', None)
   227         sep = attrs.pop('separator', u'<br/>\n')
   229         sep = attrs.pop('separator', u'<br/>\n')
   228         options = []
   230         options = []
   229         for i, option in enumerate(field.vocabulary(form)):
   231         for i, option in enumerate(field.vocabulary(form)):
   273         monthnames = [_(mname) for mname in cls.monthnames]
   275         monthnames = [_(mname) for mname in cls.monthnames]
   274         daynames = [_(dname) for dname in cls.daynames]
   276         daynames = [_(dname) for dname in cls.daynames]
   275         req.html_headers.define_var('MONTHNAMES', monthnames)
   277         req.html_headers.define_var('MONTHNAMES', monthnames)
   276         req.html_headers.define_var('DAYNAMES', daynames)
   278         req.html_headers.define_var('DAYNAMES', daynames)
   277 
   279 
   278     def render(self, form, field):
   280     def render(self, form, field, renderer):
   279         txtwidget = super(DateTimePicker, self).render(form, field)
   281         txtwidget = super(DateTimePicker, self).render(form, field, renderer)
   280         self.add_localized_infos(form.req)
   282         self.add_localized_infos(form.req)
   281         cal_button = self._render_calendar_popup(form, field)
   283         cal_button = self._render_calendar_popup(form, field)
   282         return txtwidget + cal_button
   284         return txtwidget + cal_button
   283 
   285 
   284     def _render_calendar_popup(self, form, field):
   286     def _render_calendar_popup(self, form, field):
   313         super(AjaxWidget, self).__init__(**kwargs)
   315         super(AjaxWidget, self).__init__(**kwargs)
   314         init_ajax_attributes(self.attrs, wdgtype)
   316         init_ajax_attributes(self.attrs, wdgtype)
   315         if inputid is not None:
   317         if inputid is not None:
   316             self.attrs['cubicweb:inputid'] = inputid
   318             self.attrs['cubicweb:inputid'] = inputid
   317 
   319 
   318     def render(self, form, field):
   320     def render(self, form, field, renderer):
   319         self.add_media(form)
   321         self.add_media(form)
   320         attrs = self._render_attrs(form, field)[-1]
   322         attrs = self._render_attrs(form, field)[-1]
   321         return tags.div(**attrs)
   323         return tags.div(**attrs)
   322 
   324 
   323 
   325 
   384         entity = form.edited_entity
   386         entity = form.edited_entity
   385         attrs['cubicweb:etype_to'] = entity.e_schema
   387         attrs['cubicweb:etype_to'] = entity.e_schema
   386         etype_from = entity.e_schema.subject_relation(self.name).objects(entity.e_schema)[0]
   388         etype_from = entity.e_schema.subject_relation(self.name).objects(entity.e_schema)[0]
   387         attrs['cubicweb:etype_from'] = etype_from
   389         attrs['cubicweb:etype_from'] = etype_from
   388 
   390 
   389     def render(self, form, field):
   391     def render(self, form, field, renderer):
   390         return super(AddComboBoxWidget, self).render(form, field) + u'''
   392         return super(AddComboBoxWidget, self).render(form, field, renderer) + u'''
   391 <div id="newvalue">
   393 <div id="newvalue">
   392   <input type="text" id="newopt" />
   394   <input type="text" id="newopt" />
   393   <a href="javascript:noop()" id="add_newopt">&nbsp;</a></div>
   395   <a href="javascript:noop()" id="add_newopt">&nbsp;</a></div>
   394 '''
   396 '''
   395 
   397 
   411         self.value = ''
   413         self.value = ''
   412         self.onclick = onclick
   414         self.onclick = onclick
   413         self.cwaction = cwaction
   415         self.cwaction = cwaction
   414         self.attrs.setdefault('klass', 'validateButton')
   416         self.attrs.setdefault('klass', 'validateButton')
   415 
   417 
   416     def render(self, form, field=None):
   418     def render(self, form, field=None, renderer=None):
   417         label = form.req._(self.label)
   419         label = form.req._(self.label)
   418         attrs = self.attrs.copy()
   420         attrs = self.attrs.copy()
   419         if self.cwaction:
   421         if self.cwaction:
   420             assert self.onclick is None
   422             assert self.onclick is None
   421             attrs['onclick'] = "postForm('__action_%s', \'%s\', \'%s\')" % (
   423             attrs['onclick'] = "postForm('__action_%s', \'%s\', \'%s\')" % (
   454         self.domid = domid
   456         self.domid = domid
   455         self.href = href
   457         self.href = href
   456         self.imgressource = imgressource
   458         self.imgressource = imgressource
   457         self.label = label
   459         self.label = label
   458 
   460 
   459     def render(self, form, field=None):
   461     def render(self, form, field=None, renderer=None):
   460         label = form.req._(self.label)
   462         label = form.req._(self.label)
   461         imgsrc = form.req.external_resource(self.imgressource)
   463         imgsrc = form.req.external_resource(self.imgressource)
   462         return '<a id="%(domid)s" href="%(href)s">'\
   464         return '<a id="%(domid)s" href="%(href)s">'\
   463                '<img src="%(imgsrc)s" alt="%(label)s"/>%(label)s</a>' % {
   465                '<img src="%(imgsrc)s" alt="%(label)s"/>%(label)s</a>' % {
   464             'label': label, 'imgsrc': imgsrc,
   466             'label': label, 'imgsrc': imgsrc,