formwidgets.py 1.43 KB
Newer Older
1
2
3
"""bootstrap implementation of formwidgets

:organization: Logilab
4
:copyright: 2013-2022 LOGILAB S.A. (Paris, FRANCE), license is LGPL.
Nicola Spanti's avatar
Nicola Spanti committed
5
:contact: https://www.logilab.fr/ -- mailto:contact@logilab.fr
6
7
8
9
10
"""

__docformat__ = "restructuredtext en"

from logilab.common.decorators import monkeypatch
11
from logilab.mtconverter import xml_escape
12

13
from cubicweb import tags, __version__ as cwver
14
15
from cubicweb.web import formwidgets

16
from cubicweb_bootstrap import CW_325
17

18

19
20
21
22
23
# Buttons
@monkeypatch(formwidgets.Button)
def render(self, form, field=None, renderer=None):
    label = form._cw._(self.label)
    attrs = self.attrs.copy()
24
25
26
    if not cwver.startswith('3.20'):
        # 3.21 commit b0417cacecd9
        attrs.setdefault('class', self.css_class)
27
28
29
30
31
32
33
34
35
36
    if self.cwaction:
        assert self.onclick is None
        attrs['onclick'] = "postForm('__action_%s', \'%s\', \'%s\')" % (
            self.cwaction, self.label, form.domid)
    elif self.onclick:
        attrs['onclick'] = self.onclick
    if self.name:
        attrs['name'] = self.name
        if self.setdomid:
            attrs['id'] = self.name
37
    if not CW_325:
38
39
        if self.settabindex and 'tabindex' not in attrs:
            attrs['tabindex'] = form._cw.next_tabindex()
40
41
42
43
44
45
    if self.icon:
        img = u'<i class="%s"> </i>' % self.icon
    else:
        img = u''
    return tags.button(img + xml_escape(label), escapecontent=False,
                       value=label, type=self.type, **attrs)