cubicweb/web/formwidgets.py
changeset 11875 011730a4af73
parent 11767 432f87a63057
child 11929 fcbd6b251d81
equal deleted inserted replaced
11874:ea1d92b677b5 11875:011730a4af73
   123        list of css files needed by the widget.
   123        list of css files needed by the widget.
   124 
   124 
   125     :attr:`setdomid`
   125     :attr:`setdomid`
   126        flag telling if HTML DOM identifier should be set on input.
   126        flag telling if HTML DOM identifier should be set on input.
   127 
   127 
   128     :attr:`settabindex`
       
   129        flag telling if HTML tabindex attribute of inputs should be set.
       
   130 
       
   131     :attr:`suffix`
   128     :attr:`suffix`
   132        string to use a suffix when generating input, to ease usage as a
   129        string to use a suffix when generating input, to ease usage as a
   133        sub-widgets (eg widget used by another widget)
   130        sub-widgets (eg widget used by another widget)
   134 
   131 
   135     :attr:`vocabulary_widget`
   132     :attr:`vocabulary_widget`
   155 
   152 
   156     """
   153     """
   157     needs_js = ()
   154     needs_js = ()
   158     needs_css = ()
   155     needs_css = ()
   159     setdomid = True
   156     setdomid = True
   160     settabindex = True
       
   161     suffix = None
   157     suffix = None
   162     # does this widget expect a vocabulary
   158     # does this widget expect a vocabulary
   163     vocabulary_widget = False
   159     vocabulary_widget = False
   164 
   160 
   165     def __init__(self, attrs=None, setdomid=None, settabindex=None, suffix=None):
   161     def __init__(self, attrs=None, setdomid=None, suffix=None):
   166         if attrs is None:
   162         if attrs is None:
   167             attrs = {}
   163             attrs = {}
   168         self.attrs = attrs
   164         self.attrs = attrs
   169         if setdomid is not None:
   165         if setdomid is not None:
   170             # override class's default value
   166             # override class's default value
   171             self.setdomid = setdomid
   167             self.setdomid = setdomid
   172         if settabindex is not None:
       
   173             # override class's default value
       
   174             self.settabindex = settabindex
       
   175         if suffix is not None:
   168         if suffix is not None:
   176             self.suffix = suffix
   169             self.suffix = suffix
   177 
   170 
   178     def add_media(self, form):
   171     def add_media(self, form):
   179         """adds media (CSS & JS) required by this widget"""
   172         """adds media (CSS & JS) required by this widget"""
   200     def format_value(self, form, field, value):
   193     def format_value(self, form, field, value):
   201         return field.format_value(form._cw, value)
   194         return field.format_value(form._cw, value)
   202 
   195 
   203     def attributes(self, form, field):
   196     def attributes(self, form, field):
   204         """Return HTML attributes for the widget, automatically setting DOM
   197         """Return HTML attributes for the widget, automatically setting DOM
   205         identifier and tabindex when desired (see :attr:`setdomid` and
   198         identifier when desired (see :attr:`setdomid` attribute)
   206         :attr:`settabindex` attributes)
       
   207         """
   199         """
   208         attrs = dict(self.attrs)
   200         attrs = dict(self.attrs)
   209         if self.setdomid:
   201         if self.setdomid:
   210             attrs['id'] = field.dom_id(form, self.suffix)
   202             attrs['id'] = field.dom_id(form, self.suffix)
   211         if self.settabindex and 'tabindex' not in attrs:
       
   212             attrs['tabindex'] = form._cw.next_tabindex()
       
   213         if 'placeholder' in attrs:
   203         if 'placeholder' in attrs:
   214             attrs['placeholder'] = form._cw._(attrs['placeholder'])
   204             attrs['placeholder'] = form._cw._(attrs['placeholder'])
   215         return attrs
   205         return attrs
   216 
   206 
   217     def values(self, form, field):
   207     def values(self, form, field):
   384     """Simple <input type='hidden'> for hidden value, will return a unicode
   374     """Simple <input type='hidden'> for hidden value, will return a unicode
   385     string.
   375     string.
   386     """
   376     """
   387     type = 'hidden'
   377     type = 'hidden'
   388     setdomid = False  # by default, don't set id attribute on hidden input
   378     setdomid = False  # by default, don't set id attribute on hidden input
   389     settabindex = False
       
   390 
   379 
   391 
   380 
   392 class ButtonInput(Input):
   381 class ButtonInput(Input):
   393     """Simple <input type='button'>, will return a unicode string.
   382     """Simple <input type='button'>, will return a unicode string.
   394 
   383 
   994                 path = qs = ''
   983                 path = qs = ''
   995             fqs = u'\n'.join(u'%s=%s' % (k, v) for k, v in req.url_parse_qsl(qs))
   984             fqs = u'\n'.join(u'%s=%s' % (k, v) for k, v in req.url_parse_qsl(qs))
   996         attrs = dict(self.attrs)
   985         attrs = dict(self.attrs)
   997         if self.setdomid:
   986         if self.setdomid:
   998             attrs['id'] = field.dom_id(form)
   987             attrs['id'] = field.dom_id(form)
   999         if self.settabindex and 'tabindex' not in attrs:
       
  1000             attrs['tabindex'] = req.next_tabindex()
       
  1001         # ensure something is rendered
   988         # ensure something is rendered
  1002         inputs = [u'<table><tr><th>',
   989         inputs = [u'<table><tr><th>',
  1003                   req._('i18n_bookmark_url_path'),
   990                   req._('i18n_bookmark_url_path'),
  1004                   u'</th><td>',
   991                   u'</th><td>',
  1005                   tags.input(name=pathqname, type='string', value=path, **attrs),
   992                   tags.input(name=pathqname, type='string', value=path, **attrs),
  1006                   u'</td></tr><tr><th>',
   993                   u'</td></tr><tr><th>',
  1007                   req._('i18n_bookmark_url_fqs'),
   994                   req._('i18n_bookmark_url_fqs'),
  1008                   u'</th><td>']
   995                   u'</th><td>']
  1009         if self.setdomid:
   996         if self.setdomid:
  1010             attrs['id'] = field.dom_id(form, 'fqs')
   997             attrs['id'] = field.dom_id(form, 'fqs')
  1011         if self.settabindex:
       
  1012             attrs['tabindex'] = req.next_tabindex()
       
  1013         attrs.setdefault('cols', 60)
   998         attrs.setdefault('cols', 60)
  1014         attrs.setdefault('onkeyup', 'autogrow(this)')
   999         attrs.setdefault('onkeyup', 'autogrow(this)')
  1015         inputs += [tags.textarea(fqs, name=fqsqname, **attrs),
  1000         inputs += [tags.textarea(fqs, name=fqsqname, **attrs),
  1016                    u'</td></tr></table>']
  1001                    u'</td></tr></table>']
  1017         # surrounding div necessary for proper error localization
  1002         # surrounding div necessary for proper error localization
  1055     """
  1040     """
  1056     type = 'button'
  1041     type = 'button'
  1057     css_class = 'validateButton'
  1042     css_class = 'validateButton'
  1058 
  1043 
  1059     def __init__(self, label=stdmsgs.BUTTON_OK, attrs=None,
  1044     def __init__(self, label=stdmsgs.BUTTON_OK, attrs=None,
  1060                  setdomid=None, settabindex=None,
  1045                  setdomid=None,
  1061                  name='', value='', onclick=None, cwaction=None):
  1046                  name='', value='', onclick=None, cwaction=None):
  1062         super(Button, self).__init__(attrs, setdomid, settabindex)
  1047         super(Button, self).__init__(attrs, setdomid)
  1063         if isinstance(label, tuple):
  1048         if isinstance(label, tuple):
  1064             self.label = label[0]
  1049             self.label = label[0]
  1065             self.icon = label[1]
  1050             self.icon = label[1]
  1066         else:
  1051         else:
  1067             self.label = label
  1052             self.label = label
  1083             attrs['onclick'] = self.onclick
  1068             attrs['onclick'] = self.onclick
  1084         if self.name:
  1069         if self.name:
  1085             attrs['name'] = self.name
  1070             attrs['name'] = self.name
  1086             if self.setdomid:
  1071             if self.setdomid:
  1087                 attrs['id'] = self.name
  1072                 attrs['id'] = self.name
  1088         if self.settabindex and 'tabindex' not in attrs:
       
  1089             attrs['tabindex'] = form._cw.next_tabindex()
       
  1090         if self.icon:
  1073         if self.icon:
  1091             img = tags.img(src=form._cw.uiprops[self.icon], alt=self.icon)
  1074             img = tags.img(src=form._cw.uiprops[self.icon], alt=self.icon)
  1092         else:
  1075         else:
  1093             img = u''
  1076             img = u''
  1094         return tags.button(img + xml_escape(label), escapecontent=False,
  1077         return tags.button(img + xml_escape(label), escapecontent=False,