web/formwidgets.py
author Aurelien Campeas <aurelien.campeas@logilab.fr>
Wed, 08 Jan 2014 14:00:31 +0100
changeset 9377 4e0d8f06efbc
parent 9255 46f41c3e1443
child 9700 da7d341cca76
child 9716 c44224387028
permissions -rw-r--r--
[js/widgets] fix the InOut widget with modern jQuery versions Several things are done there: * reduction in size and complexity of the code * the unused defaultsettings are removed * the initial `unlinked` list is now correctly populated from python-side * the unit test is adjusted because it tested an irrelevant implementation detail which is no longer true (but the widget of course still handles correctly the linkto information) Tested with ie7, ie9, chromium, firefox. Tested with jQuery 1.6 (cw 3.17.x) and 1.10. Closes #3154531.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9222
ec65ca70aac9 [date picker widgets] use bare jquery expression rather than jqNode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9134
diff changeset
     1
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
     2
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
     3
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
     4
# This file is part of CubicWeb.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
     5
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
     6
# CubicWeb is free software: you can redistribute it and/or modify it under the
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
     7
# terms of the GNU Lesser General Public License as published by the Free
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
     8
# Software Foundation, either version 2.1 of the License, or (at your option)
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
     9
# any later version.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
    10
#
5424
8ecbcbff9777 replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5421
diff changeset
    11
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT
5421
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
    12
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
    13
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
    14
# details.
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
    15
#
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
    16
# You should have received a copy of the GNU Lesser General Public License along
8167de96c523 proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5384
diff changeset
    17
# with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    18
"""
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    19
Widgets
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    20
~~~~~~~
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    21
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    22
.. Note::
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    23
   A widget is responsible for the display of a field. It may use more than one
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    24
   HTML input tags. When the form is posted, a widget is also reponsible to give
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    25
   back to the field something it can understand.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    26
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    27
   Of course you can not use any widget with any field...
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    28
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    29
.. autoclass:: cubicweb.web.formwidgets.FieldWidget
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    30
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    31
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    32
HTML <input> based widgets
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    33
''''''''''''''''''''''''''
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    34
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    35
.. autoclass:: cubicweb.web.formwidgets.HiddenInput
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    36
.. autoclass:: cubicweb.web.formwidgets.TextInput
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    37
.. autoclass:: cubicweb.web.formwidgets.PasswordSingleInput
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    38
.. autoclass:: cubicweb.web.formwidgets.FileInput
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    39
.. autoclass:: cubicweb.web.formwidgets.ButtonInput
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    40
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    41
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    42
Other standard HTML widgets
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    43
'''''''''''''''''''''''''''
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    44
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    45
.. autoclass:: cubicweb.web.formwidgets.TextArea
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    46
.. autoclass:: cubicweb.web.formwidgets.Select
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    47
.. autoclass:: cubicweb.web.formwidgets.CheckBox
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    48
.. autoclass:: cubicweb.web.formwidgets.Radio
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    49
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    50
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    51
Date and time widgets
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    52
'''''''''''''''''''''
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    53
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    54
.. autoclass:: cubicweb.web.formwidgets.DateTimePicker
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    55
.. autoclass:: cubicweb.web.formwidgets.JQueryDateTimePicker
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    56
.. autoclass:: cubicweb.web.formwidgets.JQueryDatePicker
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    57
.. autoclass:: cubicweb.web.formwidgets.JQueryTimePicker
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    58
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    59
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    60
Ajax / javascript widgets
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    61
'''''''''''''''''''''''''
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    62
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    63
.. autoclass:: cubicweb.web.formwidgets.FCKEditor
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    64
.. autoclass:: cubicweb.web.formwidgets.AjaxWidget
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    65
.. autoclass:: cubicweb.web.formwidgets.AutoCompletionWidget
6890
1b2fcb9356a3 [widgets, js] add a InOutWidget for multiple select
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 6880
diff changeset
    66
.. autoclass:: cubicweb.web.formwidgets.InOutWidget
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    67
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    68
.. kill or document StaticFileAutoCompletionWidget
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    69
.. kill or document LazyRestrictedAutoCompletionWidget
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    70
.. kill or document RestrictedAutoCompletionWidget
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    71
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    72
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    73
Other widgets
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    74
'''''''''''''
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    75
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    76
.. autoclass:: cubicweb.web.formwidgets.PasswordInput
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    77
.. autoclass:: cubicweb.web.formwidgets.IntervalWidget
8029
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
    78
.. autoclass:: cubicweb.web.formwidgets.BitSelect
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    79
.. autoclass:: cubicweb.web.formwidgets.HorizontalLayoutWidget
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    80
.. autoclass:: cubicweb.web.formwidgets.EditableURLWidget
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    81
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    82
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    83
Form controls
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    84
'''''''''''''
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    85
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    86
Those classes are not proper widget (they are not associated to field) but are
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    87
used as form controls. Their API is similar to widgets except that `field`
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
    88
argument given to :meth:`render` will be `None`.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    89
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    90
.. autoclass:: cubicweb.web.formwidgets.Button
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    91
.. autoclass:: cubicweb.web.formwidgets.SubmitButton
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    92
.. autoclass:: cubicweb.web.formwidgets.ResetButton
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
    93
.. autoclass:: cubicweb.web.formwidgets.ImgButton
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    94
"""
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    95
__docformat__ = "restructuredtext en"
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
    96
8719
539ed3fb27cb [towards py3k] import reduce() from functools (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8695
diff changeset
    97
from functools import reduce
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
    98
from datetime import date
1966
87ce7d336393 stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1934
diff changeset
    99
from warnings import warn
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   100
4160
3fbdeef9a610 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4159
diff changeset
   101
from logilab.mtconverter import xml_escape
3fbdeef9a610 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4159
diff changeset
   102
from logilab.common.deprecation import deprecated
4481
56440a1f816a hidden usage of datetime function which has been moved to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4392
diff changeset
   103
from logilab.common.date import todatetime
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   104
4481
56440a1f816a hidden usage of datetime function which has been moved to lgc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4392
diff changeset
   105
from cubicweb import tags, uilib
6449
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   106
from cubicweb.utils import json_dumps
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2549
diff changeset
   107
from cubicweb.web import stdmsgs, INTERNAL_FIELD_VALUE, ProcessFormError
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   108
4583
356f08325072 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4550
diff changeset
   109
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   110
class FieldWidget(object):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   111
    """The abstract base class for widgets.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   112
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   113
    **Attributes**
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   114
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7453
diff changeset
   115
    Here are standard attributes of a widget, that may be set on concrete class
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7453
diff changeset
   116
    to override default behaviours:
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   117
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   118
    :attr:`needs_js`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   119
       list of javascript files needed by the widget.
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
   120
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   121
    :attr:`needs_css`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   122
       list of css files needed by the widget.
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
   123
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   124
    :attr:`setdomid`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   125
       flag telling if HTML DOM identifier should be set on input.
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
   126
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   127
    :attr:`settabindex`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   128
       flag telling if HTML tabindex attribute of inputs should be set.
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
   129
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   130
    :attr:`suffix`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   131
       string to use a suffix when generating input, to ease usage as a
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   132
       sub-widgets (eg widget used by another widget)
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
   133
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   134
    :attr:`vocabulary_widget`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   135
       flag telling if this widget expect a vocabulary
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   136
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   137
    Also, widget instances takes as first argument a `attrs` dictionary which
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   138
    will be stored in the attribute of the same name. It contains HTML
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7453
diff changeset
   139
    attributes that should be set in the widget's input tag (though concrete
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   140
    classes may ignore it).
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   141
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   142
    .. currentmodule:: cubicweb.web.formwidgets
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   143
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   144
    **Form generation methods**
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   145
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   146
    .. automethod:: render
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   147
    .. automethod:: _render
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   148
    .. automethod:: values
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   149
    .. automethod:: attributes
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   150
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   151
    **Post handling methods**
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   152
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   153
    .. automethod:: process_field_data
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   154
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   155
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   156
    needs_js = ()
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   157
    needs_css = ()
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   158
    setdomid = True
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   159
    settabindex = True
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   160
    suffix = None
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1989
diff changeset
   161
    # does this widget expect a vocabulary
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1989
diff changeset
   162
    vocabulary_widget = False
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   163
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   164
    def __init__(self, attrs=None, setdomid=None, settabindex=None, suffix=None):
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   165
        if attrs is None:
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   166
            attrs = {}
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   167
        self.attrs = attrs
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   168
        if setdomid is not None:
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   169
            # override class's default value
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   170
            self.setdomid = setdomid
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   171
        if settabindex is not None:
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   172
            # override class's default value
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   173
            self.settabindex = settabindex
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   174
        if suffix is not None:
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   175
            self.suffix = suffix
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   176
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   177
    def add_media(self, form):
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   178
        """adds media (CSS & JS) required by this widget"""
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   179
        if self.needs_js:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   180
            form._cw.add_js(self.needs_js)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   181
        if self.needs_css:
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   182
            form._cw.add_css(self.needs_css)
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   183
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   184
    def render(self, form, field, renderer=None):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   185
        """Called to render the widget for the given `field` in the given
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   186
        `form`.  Return a unicode string containing the HTML snippet.
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   187
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   188
        You will usually prefer to override the :meth:`_render` method so you
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   189
        don't have to handle addition of needed javascript / css files.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   190
        """
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   191
        self.add_media(form)
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   192
        return self._render(form, field, renderer)
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   193
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   194
    def _render(self, form, field, renderer):
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7453
diff changeset
   195
        """This is the method you have to implement in concrete widget classes.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   196
        """
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   197
        raise NotImplementedError()
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   198
4304
0b53e850cdb5 refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   199
    def format_value(self, form, field, value):
0b53e850cdb5 refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   200
        return field.format_value(form._cw, value)
0b53e850cdb5 refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   201
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   202
    def attributes(self, form, field):
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   203
        """Return HTML attributes for the widget, automatically setting DOM
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   204
        identifier and tabindex when desired (see :attr:`setdomid` and
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   205
        :attr:`settabindex` attributes)
4304
0b53e850cdb5 refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   206
        """
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   207
        attrs = dict(self.attrs)
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   208
        if self.setdomid:
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   209
            attrs['id'] = field.dom_id(form, self.suffix)
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   210
        if self.settabindex and not 'tabindex' in attrs:
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   211
            attrs['tabindex'] = form._cw.next_tabindex()
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   212
        return attrs
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   213
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   214
    def values(self, form, field):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   215
        """Return the current *string* values (i.e. for display in an HTML
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   216
        string) for the given field. This method returns a list of values since
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   217
        it's suitable for all kind of widgets, some of them taking multiple
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   218
        values, but you'll get a single value in the list in most cases.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   219
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   220
        Those values are searched in:
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   221
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   222
        1. previously submitted form values if any (on validation error)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   223
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   224
        2. req.form (specified using request parameters)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   225
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   226
        3. extra form values given to form.render call (specified the code
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   227
           generating the form)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   228
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   229
        4. field's typed value (returned by its
6880
4be32427b2b9 [book] fixes some references and other doc construction pbs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6682
diff changeset
   230
           :meth:`~cubicweb.web.formfields.Field.typed_value` method)
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   231
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   232
        Values found in 1. and 2. are expected te be already some 'display
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   233
        value' (eg a string) while those found in 3. and 4. are expected to be
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   234
        correctly typed value.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   235
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   236
        3 and 4 are handle by the :meth:`typed_value` method to ease reuse in
7879
9aae456abab5 [pylint] fix pylint detected errors and tweak it so that pylint -E will be much less verbose next time (+ update some copyrights on the way)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7453
diff changeset
   237
        concrete classes.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   238
        """
4658
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   239
        values = None
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   240
        if not field.ignore_req_params:
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   241
            qname = field.input_name(form, self.suffix)
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   242
            # value from a previous post that has raised a validation error
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   243
            if qname in form.form_previous_values:
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   244
                values = form.form_previous_values[qname]
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   245
            # value specified using form parameters
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   246
            elif qname in form._cw.form:
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   247
                values = form._cw.form[qname]
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   248
            elif field.name != qname and field.name in form._cw.form:
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   249
                # XXX compat: accept attr=value in req.form to specify value of
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   250
                # attr-subject
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   251
                values = form._cw.form[field.name]
25de2eb0432b [form] add a new ignore_req_params attribute on field controlling value's retreival
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4627
diff changeset
   252
        if values is None:
4304
0b53e850cdb5 refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   253
            values = self.typed_value(form, field)
0b53e850cdb5 refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   254
            if values != INTERNAL_FIELD_VALUE:
0b53e850cdb5 refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   255
                values = self.format_value(form, field, values)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   256
        if not isinstance(values, (tuple, list)):
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   257
            values = (values,)
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   258
        return values
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   259
4660
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   260
    def typed_value(self, form, field):
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   261
        """return field's *typed* value specified in:
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   262
        3. extra form values given to render()
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   263
        4. field's typed value
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   264
        """
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   265
        qname = field.input_name(form)
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   266
        for key in ((field, form), qname):
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   267
            try:
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   268
                return form.formvalues[key]
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   269
            except KeyError:
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   270
                continue
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   271
        if field.name != qname and field.name in form.formvalues:
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   272
            return form.formvalues[field.name]
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   273
        return field.typed_value(form)
21ed77792c33 cleanup
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4658
diff changeset
   274
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2549
diff changeset
   275
    def process_field_data(self, form, field):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   276
        """Return process posted value(s) for widget and return something
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   277
        understandable by the associated `field`. That value may be correctly
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   278
        typed or a string that the field may parse.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   279
        """
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   280
        posted = form._cw.form
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   281
        val = posted.get(field.input_name(form, self.suffix))
4169
341d19ef7b7c strip string by default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4163
diff changeset
   282
        if isinstance(val, basestring):
4392
91a56a30141e by default this is not the widget responsability to turn empty string into None,
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4382
diff changeset
   283
            val = val.strip()
4163
b2747ed057e6 substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   284
        return val
b2747ed057e6 substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   285
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   286
    # XXX deprecates
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   287
    def values_and_attributes(self, form, field):
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   288
        return self.values(form, field), self.attributes(form, field)
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   289
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   290
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   291
class Input(FieldWidget):
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   292
    """abstract widget class for <input> tag based widgets"""
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   293
    type = None
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   294
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   295
    def _render(self, form, field, renderer):
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   296
        """render the widget for the given `field` of `form`.
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   297
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   298
        Generate one <input> tag for each field's value
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   299
        """
4163
b2747ed057e6 substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   300
        values, attrs = self.values_and_attributes(form, field)
1768
b59b7c422a8a ensure input widgets are displayed anyway if they get an empty list as values
sylvain.thenault@logilab.fr
parents: 1749
diff changeset
   301
        # ensure something is rendered
b59b7c422a8a ensure input widgets are displayed anyway if they get an empty list as values
sylvain.thenault@logilab.fr
parents: 1749
diff changeset
   302
        if not values:
b59b7c422a8a ensure input widgets are displayed anyway if they get an empty list as values
sylvain.thenault@logilab.fr
parents: 1749
diff changeset
   303
            values = (INTERNAL_FIELD_VALUE,)
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   304
        inputs = [tags.input(name=field.input_name(form, self.suffix),
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   305
                             type=self.type, value=value, **attrs)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   306
                  for value in values]
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   307
        return u'\n'.join(inputs)
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   308
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   309
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   310
# basic html widgets ###########################################################
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   311
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   312
class TextInput(Input):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   313
    """Simple <input type='text'>, will return an unicode string."""
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   314
    type = 'text'
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   315
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   316
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   317
class PasswordSingleInput(Input):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   318
    """Simple <input type='password'>, will return an utf-8 encoded string.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   319
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   320
    You may prefer using the :class:`~cubicweb.web.formwidgets.PasswordInput`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   321
    widget which handles password confirmation.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   322
    """
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   323
    type = 'password'
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   324
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   325
    def process_field_data(self, form, field):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   326
        value = super(PasswordSingleInput, self).process_field_data(form, field)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   327
        if value is not None:
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   328
            return value.encode('utf-8')
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   329
        return value
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   330
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   331
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   332
class PasswordInput(Input):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   333
    """<input type='password'> and a confirmation input. Form processing will
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   334
    fail if password and confirmation differs, else it will return the password
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   335
    as an utf-8 encoded string.
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   336
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   337
    type = 'password'
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   338
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   339
    def _render(self, form, field, renderer):
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   340
        assert self.suffix is None, 'suffix not supported'
4163
b2747ed057e6 substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   341
        values, attrs = self.values_and_attributes(form, field)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   342
        assert len(values) == 1
4627
54de0ddd0bf3 minor cleanup: don't use builtin 'id' as variable name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4597
diff changeset
   343
        domid = attrs.pop('id')
4156
1bbb0ee42c8e drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   344
        inputs = [tags.input(name=field.input_name(form),
4627
54de0ddd0bf3 minor cleanup: don't use builtin 'id' as variable name
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4597
diff changeset
   345
                             value=values[0], type=self.type, id=domid, **attrs),
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   346
                  '<br/>',
4156
1bbb0ee42c8e drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   347
                  tags.input(name=field.input_name(form, '-confirm'),
1bbb0ee42c8e drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   348
                             value=values[0], type=self.type, **attrs),
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   349
                  '&#160;', tags.span(form._cw._('confirm password'),
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   350
                                      **{'class': 'emphasis'})]
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   351
        return u'\n'.join(inputs)
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   352
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2549
diff changeset
   353
    def process_field_data(self, form, field):
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2549
diff changeset
   354
        passwd1 = super(PasswordInput, self).process_field_data(form, field)
4156
1bbb0ee42c8e drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   355
        passwd2 = form._cw.form.get(field.input_name(form, '-confirm'))
3387
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2549
diff changeset
   356
        if passwd1 == passwd2:
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2549
diff changeset
   357
            if passwd1 is None:
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2549
diff changeset
   358
                return None
a357d4147eee [forms] work-in-progress, big editcontroller refactoring: let fields/widgets process posted data
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 2549
diff changeset
   359
            return passwd1.encode('utf-8')
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   360
        raise ProcessFormError(form._cw._("password and confirmation don't match"))
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   361
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   362
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   363
class FileInput(Input):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   364
    """Simple <input type='file'>, will return a tuple (name, stream) where
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   365
    name is the posted file name and stream a file like object containing the
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   366
    posted file data.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   367
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   368
    type = 'file'
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   369
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   370
    def values(self, form, field):
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   371
        # ignore value which makes no sense here (XXX even on form validation error?)
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   372
        return ('',)
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   373
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   374
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   375
class HiddenInput(Input):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   376
    """Simple <input type='hidden'> for hidden value, will return an unicode
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   377
    string.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   378
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   379
    type = 'hidden'
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   380
    setdomid = False # by default, don't set id attribute on hidden input
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   381
    settabindex = False
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   382
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   383
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   384
class ButtonInput(Input):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   385
    """Simple <input type='button'>, will return an unicode string.
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   386
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   387
    If you want a global form button, look at the :class:`Button`,
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   388
    :class:`SubmitButton`, :class:`ResetButton` and :class:`ImgButton` below.
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   389
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   390
    type = 'button'
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   391
1096
e1fe98850bf7 cleanup
sylvain.thenault@logilab.fr
parents: 1081
diff changeset
   392
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   393
class TextArea(FieldWidget):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   394
    """Simple <textarea>, will return an unicode string."""
2366
e4229723b824 [formwidgets] let the textarea height be dependant on the actual content (up to a limit)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2360
diff changeset
   395
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   396
    def _render(self, form, field, renderer):
4163
b2747ed057e6 substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   397
        values, attrs = self.values_and_attributes(form, field)
2131
00e6d1cb18ea [views] call autogrow only once per key event
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2091
diff changeset
   398
        attrs.setdefault('onkeyup', 'autogrow(this)')
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   399
        if not values:
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   400
            value = u''
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   401
        elif len(values) == 1:
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   402
            value = values[0]
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   403
        else:
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   404
            raise ValueError('a textarea is not supposed to be multivalued')
2366
e4229723b824 [formwidgets] let the textarea height be dependant on the actual content (up to a limit)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2360
diff changeset
   405
        lines = value.splitlines()
e4229723b824 [formwidgets] let the textarea height be dependant on the actual content (up to a limit)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2360
diff changeset
   406
        linecount = len(lines)
e4229723b824 [formwidgets] let the textarea height be dependant on the actual content (up to a limit)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2360
diff changeset
   407
        for line in lines:
e4229723b824 [formwidgets] let the textarea height be dependant on the actual content (up to a limit)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2360
diff changeset
   408
            linecount += len(line) / 80
e4229723b824 [formwidgets] let the textarea height be dependant on the actual content (up to a limit)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2360
diff changeset
   409
        attrs.setdefault('cols', 80)
e4229723b824 [formwidgets] let the textarea height be dependant on the actual content (up to a limit)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 2360
diff changeset
   410
        attrs.setdefault('rows', min(15, linecount + 2))
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   411
        return tags.textarea(value, name=field.input_name(form, self.suffix),
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   412
                             **attrs)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   413
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   414
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   415
class FCKEditor(TextArea):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   416
    """FCKEditor enabled <textarea>, will return an unicode string containing
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   417
    HTML formated text.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   418
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   419
    def __init__(self, *args, **kwargs):
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   420
        super(FCKEditor, self).__init__(*args, **kwargs)
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   421
        self.attrs['cubicweb:type'] = 'wysiwyg'
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   422
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   423
    def _render(self, form, field, renderer):
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   424
        form._cw.fckeditor_config()
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   425
        return super(FCKEditor, self)._render(form, field, renderer)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   426
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   427
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   428
class Select(FieldWidget):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   429
    """Simple <select>, for field having a specific vocabulary. Will return
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   430
    an unicode string, or a list of unicode strings.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   431
    """
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1989
diff changeset
   432
    vocabulary_widget = True
8193
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   433
    default_size = 10
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1989
diff changeset
   434
4975
35b0dd80dc06 [widget] allow kwargs passed to Select widget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4901
diff changeset
   435
    def __init__(self, attrs=None, multiple=False, **kwargs):
35b0dd80dc06 [widget] allow kwargs passed to Select widget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4901
diff changeset
   436
        super(Select, self).__init__(attrs, **kwargs)
1541
ddddbb748355 [widgets] an option for Select to show sorted content
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 1538
diff changeset
   437
        self._multiple = multiple
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   438
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   439
    def _render(self, form, field, renderer):
4163
b2747ed057e6 substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   440
        curvalues, attrs = self.values_and_attributes(form, field)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   441
        options = []
1771
bb9538d91465 fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents: 1768
diff changeset
   442
        optgroup_opened = False
7453
84046395d2cd Fix name error introduced in c21d845836e4
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7450
diff changeset
   443
        vocab = field.vocabulary(form)
84046395d2cd Fix name error introduced in c21d845836e4
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 7450
diff changeset
   444
        for option in vocab:
2518
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   445
            try:
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   446
                label, value, oattrs = option
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   447
            except ValueError:
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   448
                label, value = option
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   449
                oattrs = {}
1336
2e552353c42a insert an optgroup as separator on None values
sylvain.thenault@logilab.fr
parents: 1330
diff changeset
   450
            if value is None:
2e552353c42a insert an optgroup as separator on None values
sylvain.thenault@logilab.fr
parents: 1330
diff changeset
   451
                # handle separator
1771
bb9538d91465 fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents: 1768
diff changeset
   452
                if optgroup_opened:
bb9538d91465 fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents: 1768
diff changeset
   453
                    options.append(u'</optgroup>')
2518
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   454
                oattrs.setdefault('label', label or '')
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   455
                options.append(u'<optgroup %s>' % uilib.sgml_attributes(oattrs))
1771
bb9538d91465 fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents: 1768
diff changeset
   456
                optgroup_opened = True
8029
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   457
            elif self.value_selected(value, curvalues):
2518
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   458
                options.append(tags.option(label, value=value,
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   459
                                           selected='selected', **oattrs))
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   460
            else:
2518
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   461
                options.append(tags.option(label, value=value, **oattrs))
1771
bb9538d91465 fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents: 1768
diff changeset
   462
        if optgroup_opened:
bb9538d91465 fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents: 1768
diff changeset
   463
            options.append(u'</optgroup>')
7450
c21d845836e4 [formwidgets] make the select size a class attribute (more easily tuned)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7411
diff changeset
   464
        if not 'size' in attrs:
c21d845836e4 [formwidgets] make the select size a class attribute (more easily tuned)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7411
diff changeset
   465
            if self._multiple:
c21d845836e4 [formwidgets] make the select size a class attribute (more easily tuned)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7411
diff changeset
   466
                size = unicode(min(self.default_size, len(vocab) or 1))
c21d845836e4 [formwidgets] make the select size a class attribute (more easily tuned)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7411
diff changeset
   467
            else:
c21d845836e4 [formwidgets] make the select size a class attribute (more easily tuned)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7411
diff changeset
   468
                size = u'1'
c21d845836e4 [formwidgets] make the select size a class attribute (more easily tuned)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7411
diff changeset
   469
            attrs['size'] = size
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   470
        return tags.select(name=field.input_name(form, self.suffix),
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   471
                           multiple=self._multiple, options=options, **attrs)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   472
8029
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   473
    def value_selected(self, value, curvalues):
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   474
        return value in curvalues
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   475
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   476
8193
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   477
class InOutWidget(Select):
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   478
    needs_js = ('cubicweb.widgets.js', )
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   479
    default_size = 10
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   480
    template = """
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   481
<table id="%(widgetid)s">
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   482
  <tr>
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   483
    <td>%(inoutinput)s</td>
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   484
    <td><div style="margin-bottom:3px">%(addinput)s</div>
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   485
        <div>%(removeinput)s</div>
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   486
    </td>
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   487
    <td>%(resinput)s</td>
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   488
  </tr>
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   489
</table>
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   490
"""
8936
010adc548b97 [form] remove hard coded id
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8732
diff changeset
   491
    add_button = ('<input type="button" class="wdgButton cwinoutadd" '
8193
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   492
                  'value="&gt;&gt;" size="10" />')
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   493
    remove_button = ('<input type="button" class="wdgButton cwinoutremove" '
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   494
                     'value="&lt;&lt;" size="10" />')
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   495
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   496
    def __init__(self, *args, **kwargs):
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   497
        super(InOutWidget, self).__init__(*args, **kwargs)
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   498
        self._multiple = True
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   499
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   500
    def render_select(self, form, field, name, selected=False):
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   501
        values, attrs = self.values_and_attributes(form, field)
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   502
        options = []
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   503
        inputs = []
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   504
        for option in field.vocabulary(form):
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   505
            try:
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   506
                label, value, _oattrs = option
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   507
            except ValueError:
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   508
                label, value = option
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   509
            if selected:
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   510
                # add values
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   511
                if value in values:
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   512
                    options.append(tags.option(label, value=value))
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   513
                    # add hidden inputs
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   514
                    inputs.append(tags.input(value=value,
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   515
                                             name=field.dom_id(form),
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   516
                                             type="hidden"))
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   517
            else:
9377
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9255
diff changeset
   518
                if value not in values:
4e0d8f06efbc [js/widgets] fix the InOut widget with modern jQuery versions
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9255
diff changeset
   519
                    options.append(tags.option(label, value=value))
8193
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   520
        if 'size' not in attrs:
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   521
            attrs['size'] = self.default_size
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   522
        if 'id' in attrs :
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   523
            attrs.pop('id')
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   524
        return tags.select(name=name, multiple=self._multiple, id=name,
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   525
                           options=options, **attrs) + '\n'.join(inputs)
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   526
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   527
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   528
    def _render(self, form, field, renderer):
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   529
        domid = field.dom_id(form)
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   530
        jsnodes = {'widgetid': domid,
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   531
                   'from': 'from_' + domid,
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   532
                   'to': 'to_' + domid}
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   533
        form._cw.add_onload(u'$(cw.jqNode("%s")).cwinoutwidget("%s", "%s");'
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   534
                            % (jsnodes['widgetid'], jsnodes['from'], jsnodes['to']))
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   535
        field.required = True
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   536
        return (self.template %
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   537
                {'widgetid': jsnodes['widgetid'],
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   538
                 # helpinfo select tag
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   539
                 'inoutinput' : self.render_select(form, field, jsnodes['from']),
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   540
                 # select tag with resultats
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   541
                 'resinput' : self.render_select(form, field, jsnodes['to'], selected=True),
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   542
                 'addinput' : self.add_button % jsnodes,
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   543
                 'removeinput': self.remove_button % jsnodes
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   544
                 })
0c64d6b75303 [web/formwidgets] make default_size a tunable, fix code layout (closes #2165936)
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 8029
diff changeset
   545
9222
ec65ca70aac9 [date picker widgets] use bare jquery expression rather than jqNode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9134
diff changeset
   546
8029
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   547
class BitSelect(Select):
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   548
    """Select widget for IntField using a vocabulary with bit masks as values.
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   549
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   550
    See also :class:`~cubicweb.web.facet.BitFieldFacet`.
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   551
    """
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   552
    def __init__(self, attrs=None, multiple=True, **kwargs):
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   553
        super(BitSelect, self).__init__(attrs, multiple=multiple, **kwargs)
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   554
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   555
    def value_selected(self, value, curvalues):
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   556
        mask = reduce(lambda x, y: int(x) | int(y), curvalues, 0)
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   557
        return int(value) & mask
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   558
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   559
    def process_field_data(self, form, field):
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   560
        """Return process posted value(s) for widget and return something
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   561
        understandable by the associated `field`. That value may be correctly
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   562
        typed or a string that the field may parse.
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   563
        """
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   564
        val = super(BitSelect, self).process_field_data(form, field)
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   565
        if isinstance(val, list):
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   566
            val = reduce(lambda x, y: int(x) | int(y), val, 0)
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   567
        elif val:
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   568
            val = int(val)
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   569
        else:
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   570
            val = 0
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   571
        return val
805d4e121b65 [ui lib] facet and form widget for Integer used to store binary mask. Closes #2054771
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7990
diff changeset
   572
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   573
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   574
class CheckBox(Input):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   575
    """Simple <input type='checkbox'>, for field having a specific
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   576
    vocabulary. One input will be generated for each possible value.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   577
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   578
    You can specify separator using the `separator` constructor argument, by
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   579
    default <br/> is used.
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   580
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   581
    type = 'checkbox'
7231
18bcb98d129a [widgets] handle no value label for tristate radios, put default separator on class for easier redefinition - closes #1624839
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7115
diff changeset
   582
    default_separator = u'<br/>\n'
2091
a7ea618e5478 don't set select widget when a vocabulary widget is already specified on the field class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1989
diff changeset
   583
    vocabulary_widget = True
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   584
7231
18bcb98d129a [widgets] handle no value label for tristate radios, put default separator on class for easier redefinition - closes #1624839
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7115
diff changeset
   585
    def __init__(self, attrs=None, separator=None, **kwargs):
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   586
        super(CheckBox, self).__init__(attrs, **kwargs)
7231
18bcb98d129a [widgets] handle no value label for tristate radios, put default separator on class for easier redefinition - closes #1624839
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 7115
diff changeset
   587
        self.separator = separator or self.default_separator
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   588
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   589
    def _render(self, form, field, renderer):
4163
b2747ed057e6 substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   590
        curvalues, attrs = self.values_and_attributes(form, field)
1367
01bb5b727fe3 set dom id on the first input of radio choices
sylvain.thenault@logilab.fr
parents: 1359
diff changeset
   591
        domid = attrs.pop('id', None)
9255
46f41c3e1443 remove 3.8 bw compat
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 9247
diff changeset
   592
        sep = self.separator
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   593
        options = []
2518
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   594
        for i, option in enumerate(field.vocabulary(form)):
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   595
            try:
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   596
                label, value, oattrs = option
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   597
            except ValueError:
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   598
                label, value = option
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   599
                oattrs = {}
1367
01bb5b727fe3 set dom id on the first input of radio choices
sylvain.thenault@logilab.fr
parents: 1359
diff changeset
   600
            iattrs = attrs.copy()
2518
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   601
            iattrs.update(oattrs)
1367
01bb5b727fe3 set dom id on the first input of radio choices
sylvain.thenault@logilab.fr
parents: 1359
diff changeset
   602
            if i == 0 and domid is not None:
2518
38c28ee40138 allow vocabulary functions to return either label/value or label/value/dict (to use as attributes of the tag for this input)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2366
diff changeset
   603
                iattrs.setdefault('id', domid)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   604
            if value in curvalues:
1367
01bb5b727fe3 set dom id on the first input of radio choices
sylvain.thenault@logilab.fr
parents: 1359
diff changeset
   605
                iattrs['checked'] = u'checked'
4371
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   606
            tag = tags.input(name=field.input_name(form, self.suffix),
0d337feb5374 [forms] new optional suffix attribute on widget objects, used to generage input name / dom id.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4305
diff changeset
   607
                             type=self.type, value=value, **iattrs)
4593
d0b5ef72a492 add space between a checkbox and its label
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4583
diff changeset
   608
            options.append(u'%s&#160;%s' % (tag, label))
2519
ac1a869e1e93 nicer checkbox widget implementation, avoid extra <br/> at the end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2518
diff changeset
   609
        return sep.join(options)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   610
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   611
1832
3384264d25cc fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents: 1771
diff changeset
   612
class Radio(CheckBox):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   613
    """Simle <input type='radio'>, for field having a specific vocabulary. One
1832
3384264d25cc fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents: 1771
diff changeset
   614
    input will be generated for each possible value.
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   615
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   616
    You can specify separator using the `separator` constructor argument, by
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   617
    default <br/> is used.
1832
3384264d25cc fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents: 1771
diff changeset
   618
    """
3384264d25cc fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents: 1771
diff changeset
   619
    type = 'radio'
5384
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
   620
2523
1d245fbbeb90 some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2522
diff changeset
   621
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   622
# javascript widgets ###########################################################
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   623
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   624
class DateTimePicker(TextInput):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   625
    """<input type='text'> + javascript date/time picker for date or datetime
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   626
    fields. Will return the date or datetime as an unicode string.
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   627
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   628
    monthnames = ('january', 'february', 'march', 'april',
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   629
                  'may', 'june', 'july', 'august',
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   630
                  'september', 'october', 'november', 'december')
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   631
    daynames = ('monday', 'tuesday', 'wednesday', 'thursday',
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   632
                'friday', 'saturday', 'sunday')
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   633
1311
4cc6e2723dc7 move ajax.js to base form class
sylvain.thenault@logilab.fr
parents: 1304
diff changeset
   634
    needs_js = ('cubicweb.calendar.js',)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   635
    needs_css = ('cubicweb.calendar_popup.css',)
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   636
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   637
    @classmethod
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   638
    def add_localized_infos(cls, req):
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   639
        """inserts JS variables defining localized months and days"""
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   640
        _ = req._
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   641
        monthnames = [_(mname) for mname in cls.monthnames]
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   642
        daynames = [_(dname) for dname in cls.daynames]
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   643
        req.html_headers.define_var('MONTHNAMES', monthnames)
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   644
        req.html_headers.define_var('DAYNAMES', daynames)
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   645
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   646
    def _render(self, form, field, renderer):
5383
fbe7416104c6 [widgets] fix super calls, bug introduced in 5367:4176a50c81c9
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5377
diff changeset
   647
        txtwidget = super(DateTimePicker, self)._render(form, field, renderer)
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   648
        self.add_localized_infos(form._cw)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   649
        cal_button = self._render_calendar_popup(form, field)
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   650
        return txtwidget + cal_button
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   651
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   652
    def _render_calendar_popup(self, form, field):
4159
6b2b20c73d59 refactor form field value handling, to get a nicer api and an easier algorithm to get field's value
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4156
diff changeset
   653
        value = field.typed_value(form)
1392
d6279efff7b3 refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents: 1389
diff changeset
   654
        if not value:
d6279efff7b3 refactor the way field value/display value are handled to avoid getting a 'display' value when expected a 'typed' value
sylvain.thenault@logilab.fr
parents: 1389
diff changeset
   655
            value = date.today()
4156
1bbb0ee42c8e drop form_field_name/form_field_id methods from form object, in favor of field.input_name(form) / field.dom_id(form)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4023
diff changeset
   656
        inputid = field.dom_id(form)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   657
        helperid = '%shelper' % inputid
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   658
        year, month = value.year, value.month
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   659
        return (u"""<a onclick="toggleCalendar('%s', '%s', %s, %s);" class="calhelper">
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   660
<img src="%s" title="%s" alt="" /></a><div class="calpopup hidden" id="%s"></div>"""
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   661
                % (helperid, inputid, year, month,
5467
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5453
diff changeset
   662
                   form._cw.uiprops['CALENDAR_ICON'],
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   663
                   form._cw._('calendar'), helperid) )
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   664
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   665
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   666
class JQueryDatePicker(FieldWidget):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   667
    """Use jquery.ui.datepicker to define a date picker. Will return the date as
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   668
    an unicode string.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   669
    """
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   670
    needs_js = ('jquery.ui.js', )
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   671
    needs_css = ('jquery.ui.css',)
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   672
    default_size = 10
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   673
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   674
    def __init__(self, datestr=None, **kwargs):
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   675
        super(JQueryDatePicker, self).__init__(**kwargs)
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   676
        self.value = datestr
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   677
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   678
    def _render(self, form, field, renderer):
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   679
        req = form._cw
6737
de49060d4be3 add localization files for jquery datepicker
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6682
diff changeset
   680
        if req.lang != 'en':
de49060d4be3 add localization files for jquery datepicker
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6682
diff changeset
   681
            req.add_js('jquery.ui.datepicker-%s.js' % req.lang)
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   682
        domid = field.dom_id(form, self.suffix)
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   683
        # XXX find a way to understand every format
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   684
        fmt = req.property_value('ui.date-format')
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   685
        fmt = fmt.replace('%Y', 'yy').replace('%m', 'mm').replace('%d', 'dd')
9247
9785c54e707e [date picker] revert #ec65ca70aac9 which breaks the date picker (closes #3182844)
David Douard <david.douard@logilab.fr>
parents: 9223
diff changeset
   686
        req.add_onload(u'cw.jqNode("%s").datepicker('
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   687
                       '{buttonImage: "%s", dateFormat: "%s", firstDay: 1,'
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   688
                       ' showOn: "button", buttonImageOnly: true})' % (
5467
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5453
diff changeset
   689
                           domid, req.uiprops['CALENDAR_ICON'], fmt))
9223
233e770293a9 [date picker widgets] properly distinguish DOM id and input name. Closes #3096575
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9222
diff changeset
   690
        return self._render_input(form, field)
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   691
9223
233e770293a9 [date picker widgets] properly distinguish DOM id and input name. Closes #3096575
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9222
diff changeset
   692
    def _render_input(self, form, field):
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   693
        if self.value is None:
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   694
            value = self.values(form, field)[0]
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   695
        else:
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   696
            value = self.value
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   697
        attrs = self.attributes(form, field)
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   698
        attrs.setdefault('size', unicode(self.default_size))
9223
233e770293a9 [date picker widgets] properly distinguish DOM id and input name. Closes #3096575
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9222
diff changeset
   699
        return tags.input(name=field.input_name(form, self.suffix),
233e770293a9 [date picker widgets] properly distinguish DOM id and input name. Closes #3096575
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9222
diff changeset
   700
                          value=value, type='text', **attrs)
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   701
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   702
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   703
class JQueryTimePicker(JQueryDatePicker):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   704
    """Use jquery.timePicker to define a time picker. Will return the time as an
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   705
    unicode string.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   706
    """
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   707
    needs_js = ('jquery.timePicker.js',)
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   708
    needs_css = ('jquery.timepicker.css',)
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   709
    default_size = 5
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   710
4846
a2fb82770fa6 [widget] allow to specify hour/minute separator on the JQueryTimePicker (vgodard patch)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4660
diff changeset
   711
    def __init__(self, timestr=None, timesteps=30, separator=u':', **kwargs):
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   712
        super(JQueryTimePicker, self).__init__(timestr, **kwargs)
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   713
        self.timesteps = timesteps
4846
a2fb82770fa6 [widget] allow to specify hour/minute separator on the JQueryTimePicker (vgodard patch)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4660
diff changeset
   714
        self.separator = separator
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   715
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   716
    def _render(self, form, field, renderer):
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   717
        domid = field.dom_id(form, self.suffix)
9247
9785c54e707e [date picker] revert #ec65ca70aac9 which breaks the date picker (closes #3182844)
David Douard <david.douard@logilab.fr>
parents: 9223
diff changeset
   718
        form._cw.add_onload(u'cw.jqNode("%s").timePicker({step: %s, separator: "%s"})' % (
8471
05992aa3fc0d [form widgets] fix JQueryDatePicker and JQueryTimePicker so they consider widget custom attributes. Closes #2423720
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 8193
diff changeset
   719
                domid, self.timesteps, self.separator))
9223
233e770293a9 [date picker widgets] properly distinguish DOM id and input name. Closes #3096575
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 9222
diff changeset
   720
        return self._render_input(form, field)
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   721
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   722
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   723
class JQueryDateTimePicker(FieldWidget):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   724
    """Compound widget using :class:`JQueryDatePicker` and
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   725
    :class:`JQueryTimePicker` widgets to define a date and time picker. Will
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   726
    return the date and time as python datetime instance.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   727
    """
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   728
    def __init__(self, initialtime=None, timesteps=15, **kwargs):
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   729
        super(JQueryDateTimePicker, self).__init__(**kwargs)
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   730
        self.initialtime = initialtime
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   731
        self.timesteps = timesteps
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   732
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   733
    def _render(self, form, field, renderer):
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   734
        """render the widget for the given `field` of `form`.
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   735
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   736
        Generate one <input> tag for each field's value
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   737
        """
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   738
        req = form._cw
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   739
        dateqname = field.input_name(form, 'date')
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   740
        timeqname = field.input_name(form, 'time')
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   741
        if dateqname in form.form_previous_values:
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   742
            datestr = form.form_previous_values[dateqname]
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   743
            timestr = form.form_previous_values[timeqname]
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   744
        else:
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   745
            datestr = timestr = u''
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   746
            if field.name in req.form:
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   747
                value = req.parse_datetime(req.form[field.name])
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   748
            else:
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   749
                value = self.typed_value(form, field)
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   750
            if value:
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   751
                datestr = req.format_date(value)
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   752
                timestr = req.format_time(value)
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   753
            elif self.initialtime:
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   754
                timestr = req.format_time(self.initialtime)
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   755
        datepicker = JQueryDatePicker(datestr=datestr, suffix='date')
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   756
        timepicker = JQueryTimePicker(timestr=timestr, timesteps=self.timesteps,
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   757
                                      suffix='time')
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   758
        return u'<div id="%s">%s%s</div>' % (field.dom_id(form),
5383
fbe7416104c6 [widgets] fix super calls, bug introduced in 5367:4176a50c81c9
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5377
diff changeset
   759
                                            datepicker.render(form, field, renderer),
fbe7416104c6 [widgets] fix super calls, bug introduced in 5367:4176a50c81c9
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5377
diff changeset
   760
                                            timepicker.render(form, field, renderer))
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   761
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   762
    def process_field_data(self, form, field):
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   763
        req = form._cw
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   764
        datestr = req.form.get(field.input_name(form, 'date')).strip() or None
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   765
        timestr = req.form.get(field.input_name(form, 'time')).strip() or None
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   766
        if datestr is None:
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   767
            return None
6260
e06750b556ad raise ProcessFormError when a problem occurs during date / time parsing
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
   768
        try:
e06750b556ad raise ProcessFormError when a problem occurs during date / time parsing
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
   769
            date = todatetime(req.parse_datetime(datestr, 'Date'))
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8471
diff changeset
   770
        except ValueError as exc:
6260
e06750b556ad raise ProcessFormError when a problem occurs during date / time parsing
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
   771
            raise ProcessFormError(unicode(exc))
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   772
        if timestr is None:
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   773
            return date
6260
e06750b556ad raise ProcessFormError when a problem occurs during date / time parsing
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
   774
        try:
e06750b556ad raise ProcessFormError when a problem occurs during date / time parsing
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
   775
            time = req.parse_datetime(timestr, 'Time')
8695
358d8bed9626 [toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 8471
diff changeset
   776
        except ValueError as exc:
6260
e06750b556ad raise ProcessFormError when a problem occurs during date / time parsing
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5909
diff changeset
   777
            raise ProcessFormError(unicode(exc))
4373
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   778
        return date.replace(hour=time.hour, minute=time.minute, second=time.second)
972143183ea3 new jquery based widgets for Time/Date/DateTime, backported from crm
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4372
diff changeset
   779
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   780
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   781
# ajax widgets ################################################################
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   782
1337
828bbf500bcc backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents: 1336
diff changeset
   783
def init_ajax_attributes(attrs, wdgtype, loadtype=u'auto'):
828bbf500bcc backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents: 1336
diff changeset
   784
    try:
4597
e872097f2287 use class, not klass, in widget.attrs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4593
diff changeset
   785
        attrs['class'] += u' widget'
1337
828bbf500bcc backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents: 1336
diff changeset
   786
    except KeyError:
4597
e872097f2287 use class, not klass, in widget.attrs
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4593
diff changeset
   787
        attrs['class'] = u'widget'
1344
930020cb134b shouldn't use unicode keys in attrs
sylvain.thenault@logilab.fr
parents: 1337
diff changeset
   788
    attrs.setdefault('cubicweb:wdgtype', wdgtype)
930020cb134b shouldn't use unicode keys in attrs
sylvain.thenault@logilab.fr
parents: 1337
diff changeset
   789
    attrs.setdefault('cubicweb:loadtype', loadtype)
1337
828bbf500bcc backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents: 1336
diff changeset
   790
828bbf500bcc backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents: 1336
diff changeset
   791
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   792
class AjaxWidget(FieldWidget):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   793
    """Simple <div> based ajax widget, requiring a `wdgtype` argument telling
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   794
    which javascript widget should be used.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   795
    """
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   796
    def __init__(self, wdgtype, inputid=None, **kwargs):
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   797
        super(AjaxWidget, self).__init__(**kwargs)
1337
828bbf500bcc backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents: 1336
diff changeset
   798
        init_ajax_attributes(self.attrs, wdgtype)
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   799
        if inputid is not None:
1344
930020cb134b shouldn't use unicode keys in attrs
sylvain.thenault@logilab.fr
parents: 1337
diff changeset
   800
            self.attrs['cubicweb:inputid'] = inputid
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   801
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   802
    def _render(self, form, field, renderer):
4163
b2747ed057e6 substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4160
diff changeset
   803
        attrs = self.values_and_attributes(form, field)[-1]
1081
f2a85f52b9e5 move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff changeset
   804
        return tags.div(**attrs)
1304
8975c8e520a9 refactor button handling
sylvain.thenault@logilab.fr
parents: 1295
diff changeset
   805
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   806
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   807
class AutoCompletionWidget(TextInput):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   808
    """<input type='text'> based ajax widget, taking a `autocomplete_initfunc`
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   809
    argument which should specify the name of a method of the json
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   810
    controller. This method is expected to return allowed values for the input,
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   811
    that the widget will use to propose matching values as you type.
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   812
    """
6448
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6260
diff changeset
   813
    needs_js = ('cubicweb.widgets.js', 'jquery.ui.js')
8590d82e9b1b [javascript] update to jQuery 1.4.2 and jQuery-ui 1.8.5
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6260
diff changeset
   814
    needs_css = ('jquery.ui.css',)
6449
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   815
    default_settings = {}
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   816
1966
87ce7d336393 stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1934
diff changeset
   817
    def __init__(self, *args, **kwargs):
6449
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   818
        self.autocomplete_settings = kwargs.pop('autocomplete_settings',
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   819
                                                self.default_settings)
7990
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   820
        self.autocomplete_initfunc = kwargs.pop('autocomplete_initfunc')
1966
87ce7d336393 stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1934
diff changeset
   821
        super(AutoCompletionWidget, self).__init__(*args, **kwargs)
87ce7d336393 stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1934
diff changeset
   822
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   823
    def values(self, form, field):
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   824
        values = super(AutoCompletionWidget, self).values(form, field)
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   825
        if not values:
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   826
            values = ('',)
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   827
        return values
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   828
6449
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   829
    def _render(self, form, field, renderer):
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   830
        entity = form.edited_entity
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   831
        domid = field.dom_id(form).replace(':', r'\\:')
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   832
        if callable(self.autocomplete_initfunc):
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   833
            data = self.autocomplete_initfunc(form, field)
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   834
        else:
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   835
            data = xml_escape(self._get_url(entity, field))
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   836
        form._cw.add_onload(u'$("#%s").cwautocomplete(%s, %s);'
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   837
                            % (domid, json_dumps(data),
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   838
                               json_dumps(self.autocomplete_settings)))
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   839
        return super(AutoCompletionWidget, self)._render(form, field, renderer)
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   840
1875
7bcb02377516 no rschema attribute on widgets
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1874
diff changeset
   841
    def _get_url(self, entity, field):
7990
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   842
        fname = self.autocomplete_initfunc
8728
75be9de9d68e [web] Use the new '/ajax' URL path to access the AjaxController (closes #2758254)
Rémi Cardona <remi.cardona@logilab.fr>
parents: 8471
diff changeset
   843
        return entity._cw.build_url('ajax', fname=fname, mode='remote',
3451
6b46d73823f5 [api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 3388
diff changeset
   844
                                    pageid=entity._cw.pageid)
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   845
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   846
6449
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   847
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   848
class StaticFileAutoCompletionWidget(AutoCompletionWidget):
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   849
    """XXX describe me"""
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   850
    wdgtype = 'StaticFileSuggestField'
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   851
1875
7bcb02377516 no rschema attribute on widgets
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 1874
diff changeset
   852
    def _get_url(self, entity, field):
7990
a673d1d9a738 [diet] drop pre 3.6 API compatibility (but attempt to keep data cmopatibility). Closes #2017916
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 7879
diff changeset
   853
        return entity._cw.data_url(self.autocomplete_initfunc)
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   854
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   855
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   856
class RestrictedAutoCompletionWidget(AutoCompletionWidget):
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   857
    """XXX describe me"""
6449
613d83588998 [formwidgets] rewrite autocomplete cw widgets to get rid of div-style declaration API
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 6448
diff changeset
   858
    default_settings = {'mustMatch': True}
1330
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   859
92343a468e2a add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents: 1329
diff changeset
   860
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   861
class LazyRestrictedAutoCompletionWidget(RestrictedAutoCompletionWidget):
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   862
    """remote autocomplete """
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   863
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   864
    def values_and_attributes(self, form, field):
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   865
        """override values_and_attributes to handle initial displayed values"""
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   866
        values, attrs = super(LazyRestrictedAutoCompletionWidget, self).values_and_attributes(form, field)
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   867
        assert len(values) == 1, "multiple selection is not supported yet by LazyWidget"
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   868
        if not values[0]:
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   869
            values = form.cw_extra_kwargs.get(field.name,'')
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   870
            if not isinstance(values, (tuple, list)):
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   871
                values = (values,)
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   872
        try:
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   873
            values = list(values)
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   874
            values[0] = int(values[0])
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   875
            attrs['cubicweb:initialvalue'] = values[0]
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   876
            values = (self.display_value_for(form, values[0]),)
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   877
        except (TypeError, ValueError):
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   878
            pass
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   879
        return values, attrs
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   880
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   881
    def display_value_for(self, form, value):
5367
4176a50c81c9 [form] small api cleanup and refactoring before documenting the form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5111
diff changeset
   882
        entity = form._cw.entity_from_eid(value)
4901
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   883
        return entity.view('combobox')
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   884
19ecbbc4f633 LazySuggestField : remote version of RestrictedSuggestField
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 4858
diff changeset
   885
4304
0b53e850cdb5 refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4252
diff changeset
   886
# more widgets #################################################################
1474
716f0742ee7f delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1425
diff changeset
   887
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   888
class IntervalWidget(FieldWidget):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   889
    """Custom widget to display an interval composed by 2 fields. This widget is
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   890
    expected to be used with a :class:`CompoundField` containing the two actual
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   891
    fields.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   892
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   893
    Exemple usage::
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   894
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   895
      class MyForm(FieldsForm):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   896
         price = CompoundField(fields=(IntField(name='minprice'),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   897
                                       IntField(name='maxprice')),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   898
                               label=_('price'),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   899
                               widget=IntervalWidget())
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   900
    """
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   901
    def _render(self, form, field, renderer):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   902
        actual_fields = field.fields
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   903
        assert len(actual_fields) == 2
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   904
        return u'<div>%s %s %s %s</div>' % (
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   905
            form._cw._('from_interval_start'),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   906
            actual_fields[0].render(form, renderer),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   907
            form._cw._('to_interval_end'),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   908
            actual_fields[1].render(form, renderer),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   909
            )
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   910
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   911
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   912
class HorizontalLayoutWidget(FieldWidget):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   913
    """Custom widget to display a set of fields grouped together horizontally in
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   914
    a form. See `IntervalWidget` for example usage.
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   915
    """
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   916
    def _render(self, form, field, renderer):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   917
        if self.attrs.get('display_label', True):
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   918
            subst = self.attrs.get('label_input_substitution', '%(label)s %(input)s')
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   919
            fields = [subst % {'label': renderer.render_label(form, f),
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   920
                              'input': f.render(form, renderer)}
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   921
                      for f in field.subfields(form)]
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   922
        else:
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   923
            fields = [f.render(form, renderer) for f in field.subfields(form)]
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   924
        return u'<div>%s</div>' % ' '.join(fields)
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   925
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   926
4375
6d34e3cf60a3 cleanup base class
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4373
diff changeset
   927
class EditableURLWidget(FieldWidget):
5368
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   928
    """Custom widget to edit separatly an url path / query string (used by
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   929
    default for the `path` attribute of `Bookmark` entities).
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   930
d321e4b62a10 [book] start documenting the HTML form system
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5367
diff changeset
   931
    It deals with url quoting nicely so that the user edit the unquoted value.
4305
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   932
    """
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   933
4372
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   934
    def _render(self, form, field, renderer):
0c44af150a49 introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4371
diff changeset
   935
        assert self.suffix is None, 'not supported'
4305
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   936
        req = form._cw
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   937
        pathqname = field.input_name(form, 'path')
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   938
        fqsqname = field.input_name(form, 'fqs') # formatted query string
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   939
        if pathqname in form.form_previous_values:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   940
            path = form.form_previous_values[pathqname]
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   941
            fqs = form.form_previous_values[fqsqname]
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   942
        else:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   943
            if field.name in req.form:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   944
                value = req.form[field.name]
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   945
            else:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   946
                value = self.typed_value(form, field)
4550
d74098d1b69f fix bug when value is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4508
diff changeset
   947
            if value:
d74098d1b69f fix bug when value is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4508
diff changeset
   948
                try:
d74098d1b69f fix bug when value is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4508
diff changeset
   949
                    path, qs = value.split('?', 1)
d74098d1b69f fix bug when value is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4508
diff changeset
   950
                except ValueError:
d74098d1b69f fix bug when value is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4508
diff changeset
   951
                    path = value
d74098d1b69f fix bug when value is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4508
diff changeset
   952
                    qs = ''
d74098d1b69f fix bug when value is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4508
diff changeset
   953
            else:
d74098d1b69f fix bug when value is None
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4508
diff changeset
   954
                path = qs = ''
4305
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   955
            fqs = u'\n'.join(u'%s=%s' % (k, v) for k, v in req.url_parse_qsl(qs))
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   956
        attrs = dict(self.attrs)
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   957
        if self.setdomid:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   958
            attrs['id'] = field.dom_id(form)
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   959
        if self.settabindex and not 'tabindex' in attrs:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   960
            attrs['tabindex'] = req.next_tabindex()
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   961
        # ensure something is rendered
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   962
        inputs = [u'<table><tr><th>',
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   963
                  req._('i18n_bookmark_url_path'),
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   964
                  u'</th><td>',
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   965
                  tags.input(name=pathqname, type='string', value=path, **attrs),
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   966
                  u'</td></tr><tr><th>',
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   967
                  req._('i18n_bookmark_url_fqs'),
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   968
                  u'</th><td>']
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   969
        if self.setdomid:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   970
            attrs['id'] = field.dom_id(form, 'fqs')
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   971
        if self.settabindex:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   972
            attrs['tabindex'] = req.next_tabindex()
5111
9f3ea34f98d1 [widget] more wide textarea on bookmark edition
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4975
diff changeset
   973
        attrs.setdefault('cols', 60)
4305
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   974
        attrs.setdefault('onkeyup', 'autogrow(this)')
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   975
        inputs += [tags.textarea(fqs, name=fqsqname, **attrs),
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   976
                   u'</td></tr></table>']
4376
839b28a3652e surrounding div necessary for proper error localization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4375
diff changeset
   977
        # surrounding div necessary for proper error localization
4508
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4481
diff changeset
   978
        return u'<div id="%s">%s</div>' % (
4376
839b28a3652e surrounding div necessary for proper error localization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4375
diff changeset
   979
            field.dom_id(form), u'\n'.join(inputs))
4305
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   980
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   981
    def process_field_data(self, form, field):
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   982
        req = form._cw
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   983
        values = {}
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   984
        path = req.form.get(field.input_name(form, 'path'))
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   985
        if isinstance(path, basestring):
6682
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6449
diff changeset
   986
            path = path.strip()
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6449
diff changeset
   987
        if path is None:
bdf9424b499c test and fix #1232202: [bookmarks] broken link for startupviews
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 6449
diff changeset
   988
            path = u''
4305
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   989
        fqs = req.form.get(field.input_name(form, 'fqs'))
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   990
        if isinstance(fqs, basestring):
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   991
            fqs = fqs.strip() or None
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   992
            if fqs:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   993
                for i, line in enumerate(fqs.split('\n')):
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   994
                    line = line.strip()
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   995
                    if line:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   996
                        try:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   997
                            key, val = line.split('=', 1)
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   998
                        except ValueError:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
   999
                            raise ProcessFormError(req._("wrong query parameter line %s") % (i+1))
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
  1000
                        # value will be url quoted by build_url_params
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
  1001
                        values.setdefault(key.encode(req.encoding), []).append(val)
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
  1002
        if not values:
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
  1003
            return path
3d731478d9a8 new field's responsibility POC: EditableURLWidget allow to edit Bookmark.path in two separated fields, displaying unquoted values which are requoted on form post processing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4304
diff changeset
  1004
        return u'%s?%s' % (path, req.build_url_params(**values))
5384
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1005
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1006
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1007
# form controls ######################################################################
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1008
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1009
class Button(Input):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1010
    """Simple <input type='button'>, base class for global form buttons.
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1011
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1012
    Note that `label` is a msgid which will be translated at form generation
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1013
    time, you should not give an already translated string.
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1014
    """
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1015
    type = 'button'
9134
a5f53b023132 [css, html] add a css_class attribute on Button, allowing to change easily default CSS class for buttons
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 8936
diff changeset
  1016
    css_class = 'validateButton'
a5f53b023132 [css, html] add a css_class attribute on Button, allowing to change easily default CSS class for buttons
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 8936
diff changeset
  1017
5384
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1018
    def __init__(self, label=stdmsgs.BUTTON_OK, attrs=None,
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1019
                 setdomid=None, settabindex=None,
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1020
                 name='', value='', onclick=None, cwaction=None):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1021
        super(Button, self).__init__(attrs, setdomid, settabindex)
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1022
        if isinstance(label, tuple):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1023
            self.label = label[0]
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1024
            self.icon = label[1]
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1025
        else:
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1026
            self.label = label
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1027
            self.icon = None
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1028
        self.name = name
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1029
        self.value = ''
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1030
        self.onclick = onclick
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1031
        self.cwaction = cwaction
9134
a5f53b023132 [css, html] add a css_class attribute on Button, allowing to change easily default CSS class for buttons
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents: 8936
diff changeset
  1032
        self.attrs.setdefault('class', self.css_class)
5384
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1033
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1034
    def render(self, form, field=None, renderer=None):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1035
        label = form._cw._(self.label)
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1036
        attrs = self.attrs.copy()
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1037
        if self.cwaction:
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1038
            assert self.onclick is None
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1039
            attrs['onclick'] = "postForm('__action_%s', \'%s\', \'%s\')" % (
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1040
                self.cwaction, self.label, form.domid)
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1041
        elif self.onclick:
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1042
            attrs['onclick'] = self.onclick
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1043
        if self.name:
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1044
            attrs['name'] = self.name
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1045
            if self.setdomid:
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1046
                attrs['id'] = self.name
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1047
        if self.settabindex and not 'tabindex' in attrs:
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1048
            attrs['tabindex'] = form._cw.next_tabindex()
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1049
        if self.icon:
5467
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5453
diff changeset
  1050
            img = tags.img(src=form._cw.uiprops[self.icon], alt=self.icon)
5384
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1051
        else:
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1052
            img = u''
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1053
        return tags.button(img + xml_escape(label), escapecontent=False,
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1054
                           value=label, type=self.type, **attrs)
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1055
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1056
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1057
class SubmitButton(Button):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1058
    """Simple <input type='submit'>, main button to submit a form"""
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1059
    type = 'submit'
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1060
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1061
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1062
class ResetButton(Button):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1063
    """Simple <input type='reset'>, main button to reset a form. You usually
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1064
    don't want to use this.
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1065
    """
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1066
    type = 'reset'
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1067
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1068
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1069
class ImgButton(object):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1070
    """Simple <img> wrapped into a <a> tag with href triggering something (usually a
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1071
    javascript call).
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1072
    """
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1073
    def __init__(self, domid, href, label, imgressource):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1074
        self.domid = domid
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1075
        self.href = href
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1076
        self.imgressource = imgressource
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1077
        self.label = label
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1078
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1079
    def render(self, form, field=None, renderer=None):
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1080
        label = form._cw._(self.label)
5467
57372dbfd114 [https] fix resource urls in https version of a site: should use the https version as well to avoid warnings from the nrowser
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5453
diff changeset
  1081
        imgsrc = form._cw.uiprops[self.imgressource]
5384
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1082
        return '<a id="%(domid)s" href="%(href)s">'\
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1083
               '<img src="%(imgsrc)s" alt="%(label)s"/>%(label)s</a>' % {
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1084
            'label': label, 'imgsrc': imgsrc,
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1085
            'domid': self.domid, 'href': self.href}
b619531ddbd2 [widgets] move form controls at the end of the file
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5383
diff changeset
  1086