author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 26 Jan 2010 16:46:12 +0100 | |
changeset 4372 | 0c44af150a49 |
parent 4371 | 0d337feb5374 |
child 4373 | 972143183ea3 |
permissions | -rw-r--r-- |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
1 |
"""widget classes for form construction |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
2 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
3 |
:organization: Logilab |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3993
diff
changeset
|
4 |
:copyright: 2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1970
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
7 |
""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
8 |
__docformat__ = "restructuredtext en" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
9 |
|
1096 | 10 |
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
|
11 |
from warnings import warn |
1096 | 12 |
|
4160 | 13 |
from logilab.mtconverter import xml_escape |
14 |
from logilab.common.deprecation import deprecated |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
15 |
|
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
16 |
from cubicweb import tags, uilib |
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
|
17 |
from cubicweb.web import stdmsgs, INTERNAL_FIELD_VALUE, ProcessFormError |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
18 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
19 |
class FieldWidget(object): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
20 |
"""abstract widget class""" |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
21 |
# javascript / css files required by the widget |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
22 |
needs_js = () |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
23 |
needs_css = () |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
24 |
# automatically set id and tabindex attributes ? |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
25 |
setdomid = True |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
26 |
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
|
27 |
# to ease usage as a sub-widgets (eg widget used by another widget) |
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
|
28 |
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
|
29 |
# 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
|
30 |
vocabulary_widget = False |
1474 | 31 |
|
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
|
32 |
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
|
33 |
if attrs is None: |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
34 |
attrs = {} |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
35 |
self.attrs = attrs |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
36 |
if setdomid is not None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
37 |
# override class's default value |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
38 |
self.setdomid = setdomid |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
39 |
if settabindex is not None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
40 |
# override class's default value |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
41 |
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
|
42 |
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
|
43 |
self.suffix = suffix |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
44 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
45 |
def add_media(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
46 |
"""adds media (CSS & JS) required by this widget""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
47 |
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
|
48 |
form._cw.add_js(self.needs_js) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
49 |
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
|
50 |
form._cw.add_css(self.needs_css) |
1474 | 51 |
|
4372
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
52 |
|
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
53 |
def render(self, form, field, renderer=None): |
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
|
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
57 |
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
|
58 |
raise NotImplementedError() |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
59 |
|
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
|
60 |
def typed_value(self, 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
|
61 |
"""return field's *typed* value specified in: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
62 |
3. extra form values given to render() |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
63 |
4. field's typed 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
|
64 |
""" |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
65 |
qname = field.input_name(form) |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
66 |
for key in (field, qname): |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
67 |
try: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
68 |
return form.formvalues[key] |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
69 |
except KeyError: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
70 |
continue |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
71 |
if field.name != qname and field.name in form.formvalues: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
72 |
return form.formvalues[field.name] |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
73 |
return field.typed_value(form) |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
74 |
|
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
75 |
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
|
76 |
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
|
77 |
|
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
78 |
def values_and_attributes(self, form, field): |
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
|
79 |
"""found field's *string* value in: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
80 |
1. previously submitted form values if any (eg on validation error) |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
81 |
2. req.form |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
82 |
3. extra form values given to render() |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
83 |
4. field's typed 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
|
84 |
|
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
85 |
values found in 1. and 2. are expected te be already some 'display' |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
86 |
value while those found in 3. and 4. are expected to be correctly typed. |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
87 |
|
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
88 |
3 and 4 are handle by the .typed_value(form, field) method |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
89 |
""" |
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
|
90 |
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
|
91 |
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
|
92 |
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
|
93 |
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
|
94 |
attrs['tabindex'] = form._cw.next_tabindex() |
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
|
95 |
return self.values(form, field), 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
|
96 |
|
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
|
97 |
def values(self, form, field): |
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
|
98 |
qname = field.input_name(form, self.suffix) |
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
|
99 |
if qname in form.form_previous_values: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
100 |
values = form.form_previous_values[qname] |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
101 |
elif qname in form._cw.form: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
102 |
values = form._cw.form[qname] |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
103 |
elif field.name != qname and field.name in form._cw.form: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
104 |
# compat: accept attr=value in req.form to specify value of attr-subject |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
105 |
values = form._cw.form[field.name] |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
106 |
else: |
0b53e850cdb5
refactor field's value retreiving from the widget (eg 'display value' concept):
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
values = self.format_value(form, field, values) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
110 |
if not isinstance(values, (tuple, list)): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
111 |
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
|
112 |
return values |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
113 |
|
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
|
114 |
def process_field_data(self, form, field): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
115 |
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
|
116 |
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
|
117 |
if isinstance(val, basestring): |
341d19ef7b7c
strip string by default
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4163
diff
changeset
|
118 |
val = val.strip() or None |
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
119 |
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
|
120 |
|
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
121 |
@deprecated('[3.6] use values_and_attributes') |
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
122 |
def _render_attrs(self, form, field): |
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
123 |
"""return html tag name, attributes and a list of values for the field |
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
124 |
""" |
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
125 |
values, attrs = self.values_and_attributes(form, field) |
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
|
126 |
return field.input_name(form, self.suffix), values, attrs |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
127 |
|
1096 | 128 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
129 |
class Input(FieldWidget): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
130 |
"""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
|
131 |
type = None |
1474 | 132 |
|
4372
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
133 |
def _render(self, form, field, renderer): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
134 |
"""render the widget for the given `field` of `form`. |
1474 | 135 |
|
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
136 |
Generate one <input> tag for each field's value |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
137 |
""" |
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
138 |
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
|
139 |
# 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
|
140 |
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
|
141 |
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
|
142 |
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
|
143 |
type=self.type, value=value, **attrs) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
144 |
for value in values] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
145 |
return u'\n'.join(inputs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
146 |
|
1096 | 147 |
|
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
148 |
# basic html widgets ########################################################### |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
149 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
150 |
class TextInput(Input): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
151 |
"""<input type='text'>""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
152 |
type = 'text' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
153 |
|
1096 | 154 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
155 |
class PasswordInput(Input): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
156 |
"""<input type='password'> and its confirmation field (using |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
157 |
<field's name>-confirm as name) |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
158 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
159 |
type = 'password' |
1474 | 160 |
|
4372
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
161 |
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
|
162 |
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
|
163 |
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
|
164 |
assert len(values) == 1 |
1110
c71997f514ba
fix password widget: do not duplicate dom id, fix confirm field name
sylvain.thenault@logilab.fr
parents:
1096
diff
changeset
|
165 |
id = 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
|
166 |
inputs = [tags.input(name=field.input_name(form), |
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
|
167 |
value=values[0], type=self.type, id=id, **attrs), |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
168 |
'<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
|
169 |
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
|
170 |
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
|
171 |
' ', tags.span(form._cw._('confirm password'), |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
172 |
**{'class': 'emphasis'})] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
173 |
return u'\n'.join(inputs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
174 |
|
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
|
175 |
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
|
176 |
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
|
177 |
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
|
178 |
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
|
179 |
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
|
180 |
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
|
181 |
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
|
182 |
raise ProcessFormError(form._cw._("password and confirmation don't match")) |
1096 | 183 |
|
184 |
||
1934
be86bb31c4c2
add single input password widget
Florent <florent@secondweb.fr>
parents:
1875
diff
changeset
|
185 |
class PasswordSingleInput(Input): |
be86bb31c4c2
add single input password widget
Florent <florent@secondweb.fr>
parents:
1875
diff
changeset
|
186 |
"""<input type='password'> without a confirmation field""" |
be86bb31c4c2
add single input password widget
Florent <florent@secondweb.fr>
parents:
1875
diff
changeset
|
187 |
type = 'password' |
be86bb31c4c2
add single input password widget
Florent <florent@secondweb.fr>
parents:
1875
diff
changeset
|
188 |
|
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
|
189 |
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
|
190 |
value = super(PasswordSingleInput, self).process_field_data(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
|
191 |
if value is not 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
|
192 |
return value.encode('utf-8') |
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
|
193 |
return value |
1934
be86bb31c4c2
add single input password widget
Florent <florent@secondweb.fr>
parents:
1875
diff
changeset
|
194 |
|
be86bb31c4c2
add single input password widget
Florent <florent@secondweb.fr>
parents:
1875
diff
changeset
|
195 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
196 |
class FileInput(Input): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
197 |
"""<input type='file'>""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
198 |
type = 'file' |
1474 | 199 |
|
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
200 |
def values_and_attributes(self, form, field): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
201 |
# ignore value which makes no sense here (XXX even on form validation error?) |
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
202 |
values, attrs = super(FileInput, self).values_and_attributes(form, field) |
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
203 |
return ('',), attrs |
1096 | 204 |
|
1474 | 205 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
206 |
class HiddenInput(Input): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
207 |
"""<input type='hidden'>""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
208 |
type = 'hidden' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
209 |
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
|
210 |
settabindex = False |
1096 | 211 |
|
1474 | 212 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
213 |
class ButtonInput(Input): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
214 |
"""<input type='button'> |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
215 |
|
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
216 |
if you want a global form button, look at the Button, SubmitButton, |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
217 |
ResetButton and ImgButton classes below. |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
218 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
219 |
type = 'button' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
220 |
|
1096 | 221 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
222 |
class TextArea(FieldWidget): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
223 |
"""<textarea>""" |
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
|
224 |
|
4372
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
225 |
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
|
226 |
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
|
227 |
attrs.setdefault('onkeyup', 'autogrow(this)') |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
228 |
if not values: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
229 |
value = u'' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
230 |
elif len(values) == 1: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
231 |
value = values[0] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
232 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
233 |
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
|
234 |
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
|
235 |
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
|
236 |
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
|
237 |
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
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
**attrs) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
242 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
243 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
244 |
class FCKEditor(TextArea): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
245 |
"""FCKEditor enabled <textarea>""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
246 |
def __init__(self, *args, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
247 |
super(FCKEditor, self).__init__(*args, **kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
248 |
self.attrs['cubicweb:type'] = 'wysiwyg' |
1474 | 249 |
|
4372
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
250 |
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
|
251 |
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
|
252 |
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
|
253 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
254 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
255 |
class Select(FieldWidget): |
1474 | 256 |
"""<select>, for field having a specific vocabulary""" |
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
|
257 |
vocabulary_widget = True |
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
|
258 |
|
1562
e6d2c07c0c58
[forms/widgets] fix relation field not sorting its vocabulary, revert hack on Select widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1541
diff
changeset
|
259 |
def __init__(self, attrs=None, multiple=False): |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
260 |
super(Select, self).__init__(attrs) |
1541
ddddbb748355
[widgets] an option for Select to show sorted content
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
1538
diff
changeset
|
261 |
self._multiple = multiple |
1474 | 262 |
|
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2519
diff
changeset
|
263 |
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
|
264 |
curvalues, attrs = self.values_and_attributes(form, field) |
1989
8c8dead642f7
set default select size to 1 into the widget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
265 |
if not 'size' in attrs: |
8c8dead642f7
set default select size to 1 into the widget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
266 |
attrs['size'] = self._multiple and '5' or '1' |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
267 |
options = [] |
1771
bb9538d91465
fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents:
1768
diff
changeset
|
268 |
optgroup_opened = False |
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
|
269 |
for option in 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
|
270 |
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
|
271 |
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
|
272 |
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
|
273 |
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
|
274 |
oattrs = {} |
1336
2e552353c42a
insert an optgroup as separator on None values
sylvain.thenault@logilab.fr
parents:
1330
diff
changeset
|
275 |
if value is None: |
2e552353c42a
insert an optgroup as separator on None values
sylvain.thenault@logilab.fr
parents:
1330
diff
changeset
|
276 |
# handle separator |
1771
bb9538d91465
fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents:
1768
diff
changeset
|
277 |
if optgroup_opened: |
bb9538d91465
fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents:
1768
diff
changeset
|
278 |
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
|
279 |
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
|
280 |
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
|
281 |
optgroup_opened = True |
1336
2e552353c42a
insert an optgroup as separator on None values
sylvain.thenault@logilab.fr
parents:
1330
diff
changeset
|
282 |
elif value in 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
|
283 |
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
|
284 |
selected='selected', **oattrs)) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
285 |
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
|
286 |
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
|
287 |
if optgroup_opened: |
bb9538d91465
fix <optgroup> tag cannot be empty according to DTD
Florent <florent@secondweb.fr>
parents:
1768
diff
changeset
|
288 |
options.append(u'</optgroup>') |
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
|
289 |
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
|
290 |
multiple=self._multiple, options=options, **attrs) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
291 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
292 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
293 |
class CheckBox(Input): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
294 |
"""<input type='checkbox'>, for field having a specific vocabulary. One |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
295 |
input will be generated for each possible value. |
1474 | 296 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
297 |
type = 'checkbox' |
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
|
298 |
vocabulary_widget = True |
1474 | 299 |
|
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2519
diff
changeset
|
300 |
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
|
301 |
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
|
302 |
domid = attrs.pop('id', None) |
2519
ac1a869e1e93
nicer checkbox widget implementation, avoid extra <br/> at the end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2518
diff
changeset
|
303 |
sep = attrs.pop('separator', u'<br/>\n') |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
304 |
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
|
305 |
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
|
306 |
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
|
307 |
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
|
308 |
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
|
309 |
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
|
310 |
oattrs = {} |
1367
01bb5b727fe3
set dom id on the first input of radio choices
sylvain.thenault@logilab.fr
parents:
1359
diff
changeset
|
311 |
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
|
312 |
iattrs.update(oattrs) |
1367
01bb5b727fe3
set dom id on the first input of radio choices
sylvain.thenault@logilab.fr
parents:
1359
diff
changeset
|
313 |
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
|
314 |
iattrs.setdefault('id', domid) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
315 |
if value in curvalues: |
1367
01bb5b727fe3
set dom id on the first input of radio choices
sylvain.thenault@logilab.fr
parents:
1359
diff
changeset
|
316 |
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
|
317 |
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
|
318 |
type=self.type, value=value, **iattrs) |
2519
ac1a869e1e93
nicer checkbox widget implementation, avoid extra <br/> at the end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2518
diff
changeset
|
319 |
options.append(tag + label) |
ac1a869e1e93
nicer checkbox widget implementation, avoid extra <br/> at the end
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2518
diff
changeset
|
320 |
return sep.join(options) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
321 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
322 |
|
1832
3384264d25cc
fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents:
1771
diff
changeset
|
323 |
class Radio(CheckBox): |
3384264d25cc
fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents:
1771
diff
changeset
|
324 |
"""<input type='radio'>, for field having a specific vocabulary. One |
3384264d25cc
fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents:
1771
diff
changeset
|
325 |
input will be generated for each possible value. |
3384264d25cc
fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents:
1771
diff
changeset
|
326 |
""" |
3384264d25cc
fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents:
1771
diff
changeset
|
327 |
type = 'radio' |
3384264d25cc
fix CheckBox multiple dom id and refactor with Radio
Florent <florent@secondweb.fr>
parents:
1771
diff
changeset
|
328 |
|
2225 | 329 |
|
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
330 |
# compound widgets ############################################################# |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
331 |
|
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
332 |
class IntervalWidget(FieldWidget): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
333 |
"""custom widget to display an interval composed by 2 fields. This widget |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
334 |
is expected to be used with a CompoundField containing the two actual |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
335 |
fields. |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
336 |
|
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
337 |
Exemple usage:: |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
338 |
|
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
339 |
from uicfg import autoform_field, autoform_section |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
340 |
autoform_field.tag_attribute(('Concert', 'minprice'), |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
341 |
CompoundField(fields=(IntField(name='minprice'), |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
342 |
IntField(name='maxprice')), |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
343 |
label=_('price'), |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
344 |
widget=IntervalWidget() |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
345 |
)) |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
346 |
# we've to hide the other field manually for now |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
347 |
autoform_section.tag_attribute(('Concert', 'maxprice'), 'generated') |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
348 |
""" |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
349 |
def render(self, form, field, renderer): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
350 |
actual_fields = field.fields |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
351 |
assert len(actual_fields) == 2 |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
352 |
return u'<div>%s %s %s %s</div>' % ( |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
353 |
form._cw._('from_interval_start'), |
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
354 |
actual_fields[0].render(form, renderer), |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
355 |
form._cw._('to_interval_end'), |
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
356 |
actual_fields[1].render(form, renderer), |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
357 |
) |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
358 |
|
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
359 |
|
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
360 |
class HorizontalLayoutWidget(FieldWidget): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
361 |
"""custom widget to display a set of fields grouped together horizontally |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
362 |
in a form. See `IntervalWidget` for example usage. |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
363 |
""" |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
364 |
def render(self, form, field, renderer): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
365 |
if self.attrs.get('display_label', True): |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
366 |
subst = self.attrs.get('label_input_substitution', '%(label)s %(input)s') |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
367 |
fields = [subst % {'label': renderer.render_label(form, f), |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
368 |
'input': f.render(form, renderer)} |
2549
3d8c62e5e2d4
[R forms] use a subfields(form) method to get a chance to adapt to context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2523
diff
changeset
|
369 |
for f in field.subfields(form)] |
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
370 |
else: |
2549
3d8c62e5e2d4
[R forms] use a subfields(form) method to get a chance to adapt to context
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2523
diff
changeset
|
371 |
fields = [f.render(form, renderer) for f in field.subfields(form)] |
2523
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
372 |
return u'<div>%s</div>' % ' '.join(fields) |
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
373 |
|
1d245fbbeb90
some new field/widgets classes: CompoundField, IntervalWidget, HorizontalLayoutWidget
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2522
diff
changeset
|
374 |
|
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
375 |
# javascript widgets ########################################################### |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
376 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
377 |
class DateTimePicker(TextInput): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
378 |
"""<input type='text' + javascript date/time picker for date or datetime |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
379 |
fields |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
380 |
""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
381 |
monthnames = ('january', 'february', 'march', 'april', |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
382 |
'may', 'june', 'july', 'august', |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
383 |
'september', 'october', 'november', 'december') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
384 |
daynames = ('monday', 'tuesday', 'wednesday', 'thursday', |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
385 |
'friday', 'saturday', 'sunday') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
386 |
|
1311
4cc6e2723dc7
move ajax.js to base form class
sylvain.thenault@logilab.fr
parents:
1304
diff
changeset
|
387 |
needs_js = ('cubicweb.calendar.js',) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
388 |
needs_css = ('cubicweb.calendar_popup.css',) |
1474 | 389 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
390 |
@classmethod |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
391 |
def add_localized_infos(cls, req): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
392 |
"""inserts JS variables defining localized months and days""" |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3998
diff
changeset
|
393 |
# import here to avoid dependancy from cubicweb to simplejson |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
394 |
_ = req._ |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
395 |
monthnames = [_(mname) for mname in cls.monthnames] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
396 |
daynames = [_(dname) for dname in cls.daynames] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
397 |
req.html_headers.define_var('MONTHNAMES', monthnames) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
398 |
req.html_headers.define_var('DAYNAMES', daynames) |
1474 | 399 |
|
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2519
diff
changeset
|
400 |
def render(self, form, field, renderer): |
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2519
diff
changeset
|
401 |
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
|
402 |
self.add_localized_infos(form._cw) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
403 |
cal_button = self._render_calendar_popup(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
404 |
return txtwidget + cal_button |
1474 | 405 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
406 |
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
|
407 |
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
|
408 |
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
|
409 |
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
|
410 |
inputid = field.dom_id(form) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
411 |
helperid = '%shelper' % inputid |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
412 |
year, month = value.year, value.month |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
413 |
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
|
414 |
<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
|
415 |
% (helperid, inputid, year, month, |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
416 |
form._cw.external_resource('CALENDAR_ICON'), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
417 |
form._cw._('calendar'), helperid) ) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
418 |
|
1474 | 419 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
420 |
|
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
421 |
# ajax widgets ################################################################ |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
422 |
|
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
423 |
def init_ajax_attributes(attrs, wdgtype, loadtype=u'auto'): |
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
424 |
try: |
1344
930020cb134b
shouldn't use unicode keys in attrs
sylvain.thenault@logilab.fr
parents:
1337
diff
changeset
|
425 |
attrs['klass'] += u' widget' |
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
426 |
except KeyError: |
1344
930020cb134b
shouldn't use unicode keys in attrs
sylvain.thenault@logilab.fr
parents:
1337
diff
changeset
|
427 |
attrs['klass'] = u'widget' |
930020cb134b
shouldn't use unicode keys in attrs
sylvain.thenault@logilab.fr
parents:
1337
diff
changeset
|
428 |
attrs.setdefault('cubicweb:wdgtype', wdgtype) |
930020cb134b
shouldn't use unicode keys in attrs
sylvain.thenault@logilab.fr
parents:
1337
diff
changeset
|
429 |
attrs.setdefault('cubicweb:loadtype', loadtype) |
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
430 |
|
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
431 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
432 |
class AjaxWidget(FieldWidget): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
433 |
"""simple <div> based ajax widget""" |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
434 |
def __init__(self, wdgtype, inputid=None, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
435 |
super(AjaxWidget, self).__init__(**kwargs) |
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
436 |
init_ajax_attributes(self.attrs, wdgtype) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
437 |
if inputid is not None: |
1344
930020cb134b
shouldn't use unicode keys in attrs
sylvain.thenault@logilab.fr
parents:
1337
diff
changeset
|
438 |
self.attrs['cubicweb:inputid'] = inputid |
1474 | 439 |
|
4372
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
440 |
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
|
441 |
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
|
442 |
return tags.div(**attrs) |
1304 | 443 |
|
1474 | 444 |
|
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
445 |
class AutoCompletionWidget(TextInput): |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
446 |
"""ajax widget for StringField, proposing matching existing values as you |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
447 |
type. |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
448 |
""" |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
449 |
needs_js = ('cubicweb.widgets.js', 'jquery.autocomplete.js') |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
450 |
needs_css = ('jquery.autocomplete.css',) |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
451 |
wdgtype = 'SuggestField' |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
452 |
loadtype = 'auto' |
1474 | 453 |
|
1966
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
454 |
def __init__(self, *args, **kwargs): |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
455 |
try: |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
456 |
self.autocomplete_initfunc = kwargs.pop('autocomplete_initfunc') |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
457 |
except KeyError: |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
458 |
warn('use autocomplete_initfunc argument of %s constructor ' |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
459 |
'instead of relying on autocomplete_initfuncs dictionary on ' |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
460 |
'the entity class' % self.__class__.__name__, |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
461 |
DeprecationWarning) |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
462 |
self.autocomplete_initfunc = None |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
463 |
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
|
464 |
|
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
465 |
def values_and_attributes(self, form, field): |
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
466 |
values, attrs = super(AutoCompletionWidget, self).values_and_attributes(form, field) |
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
467 |
init_ajax_attributes(attrs, self.wdgtype, self.loadtype) |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
468 |
# XXX entity form specific |
1875
7bcb02377516
no rschema attribute on widgets
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1874
diff
changeset
|
469 |
attrs['cubicweb:dataurl'] = self._get_url(form.edited_entity, field) |
3993
8cf7c767b134
quick & dirty fix for auto completion widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3803
diff
changeset
|
470 |
if not values: |
8cf7c767b134
quick & dirty fix for auto completion widget
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3803
diff
changeset
|
471 |
values = ('',) |
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
472 |
return values, attrs |
1474 | 473 |
|
1875
7bcb02377516
no rschema attribute on widgets
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1874
diff
changeset
|
474 |
def _get_url(self, entity, field): |
1966
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
475 |
if self.autocomplete_initfunc is None: |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
476 |
# XXX for bw compat |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
477 |
fname = entity.autocomplete_initfuncs[field.name] |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
478 |
else: |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
479 |
fname = self.autocomplete_initfunc |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
480 |
return entity._cw.build_url('json', fname=fname, mode='remote', |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
481 |
pageid=entity._cw.pageid) |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
482 |
|
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
483 |
|
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
484 |
class StaticFileAutoCompletionWidget(AutoCompletionWidget): |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
485 |
"""XXX describe me""" |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
486 |
wdgtype = 'StaticFileSuggestField' |
1474 | 487 |
|
1875
7bcb02377516
no rschema attribute on widgets
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1874
diff
changeset
|
488 |
def _get_url(self, entity, field): |
1966
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
489 |
if self.autocomplete_initfunc is None: |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
490 |
# XXX for bw compat |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
491 |
fname = entity.autocomplete_initfuncs[field.name] |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
492 |
else: |
87ce7d336393
stop using autocomplete_initfuncs dict on entity classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1934
diff
changeset
|
493 |
fname = self.autocomplete_initfunc |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
494 |
return entity._cw.datadir_url + fname |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
495 |
|
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
496 |
|
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
497 |
class RestrictedAutoCompletionWidget(AutoCompletionWidget): |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
498 |
"""XXX describe me""" |
1474 | 499 |
wdgtype = 'RestrictedSuggestField' |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
500 |
|
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
501 |
|
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
502 |
class AddComboBoxWidget(Select): |
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
503 |
def values_and_attributes(self, form, field): |
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
504 |
values, attrs = super(AddComboBoxWidget, self).values_and_attributes(form, field) |
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
505 |
init_ajax_attributes(self.attrs, 'AddComboBox') |
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
506 |
# XXX entity form specific |
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
507 |
entity = form.edited_entity |
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
508 |
attrs['cubicweb:etype_to'] = entity.e_schema |
3689
deb13e88e037
follow yams 0.25 api changes to improve performance
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3106
diff
changeset
|
509 |
etype_from = entity.e_schema.subjrels[field.name].objects(entity.e_schema)[0] |
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
510 |
attrs['cubicweb:etype_from'] = etype_from |
4163
b2747ed057e6
substitute _render_attrs by values_and_attributes method, keeping bw compat
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4160
diff
changeset
|
511 |
return values, attrs |
1474 | 512 |
|
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2519
diff
changeset
|
513 |
def render(self, form, field, renderer): |
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2519
diff
changeset
|
514 |
return super(AddComboBoxWidget, self).render(form, field, renderer) + u''' |
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
515 |
<div id="newvalue"> |
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
516 |
<input type="text" id="newopt" /> |
2996
866a2c135c33
B #345282 xhtml requires to use   instead of
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2549
diff
changeset
|
517 |
<a href="javascript:noop()" id="add_newopt"> </a></div> |
1337
828bbf500bcc
backport AddComboBoxWidget, ajax widgets refactoring
sylvain.thenault@logilab.fr
parents:
1336
diff
changeset
|
518 |
''' |
1359 | 519 |
|
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
520 |
# buttons ###################################################################### |
1304 | 521 |
|
522 |
class Button(Input): |
|
1389 | 523 |
"""<input type='button'>, base class for global form buttons |
524 |
||
525 |
note label is a msgid which will be translated at form generation time, you |
|
526 |
should not give an already translated string. |
|
527 |
""" |
|
1304 | 528 |
type = 'button' |
529 |
def __init__(self, label=stdmsgs.BUTTON_OK, attrs=None, |
|
530 |
setdomid=None, settabindex=None, |
|
531 |
name='', value='', onclick=None, cwaction=None): |
|
532 |
super(Button, self).__init__(attrs, setdomid, settabindex) |
|
3803
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
533 |
if isinstance(label, tuple): |
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
534 |
self.label = label[0] |
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
535 |
self.icon = label[1] |
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
536 |
else: |
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
537 |
self.label = label |
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
538 |
self.icon = None |
1304 | 539 |
self.name = name |
540 |
self.value = '' |
|
541 |
self.onclick = onclick |
|
542 |
self.cwaction = cwaction |
|
543 |
self.attrs.setdefault('klass', 'validateButton') |
|
1474 | 544 |
|
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2519
diff
changeset
|
545 |
def render(self, form, field=None, renderer=None): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
546 |
label = form._cw._(self.label) |
1304 | 547 |
attrs = self.attrs.copy() |
548 |
if self.cwaction: |
|
549 |
assert self.onclick is None |
|
550 |
attrs['onclick'] = "postForm('__action_%s', \'%s\', \'%s\')" % ( |
|
551 |
self.cwaction, self.label, form.domid) |
|
552 |
elif self.onclick: |
|
553 |
attrs['onclick'] = self.onclick |
|
554 |
if self.name: |
|
4250
39adce674a09
fix NameError
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
555 |
attrs['name'] = self.name |
1304 | 556 |
if self.setdomid: |
557 |
attrs['id'] = self.name |
|
558 |
if self.settabindex and not 'tabindex' in attrs: |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
559 |
attrs['tabindex'] = form._cw.next_tabindex() |
3803
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
560 |
if self.icon: |
3890
d7a270f50f54
backport stable branch (one more time painfully)
Sylvain Thénault <sylvain.thenault@logilab.fr>
diff
changeset
|
561 |
img = tags.img(src=form._cw.external_resource(self.icon), alt=self.icon) |
3803
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
562 |
else: |
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
563 |
img = u'' |
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
564 |
return tags.button(img + xml_escape(label), escapecontent=False, |
414bb8439002
[web ui] decorate form buttons with icons (at last)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
3689
diff
changeset
|
565 |
value=label, type=self.type, **attrs) |
1304 | 566 |
|
1474 | 567 |
|
1304 | 568 |
class SubmitButton(Button): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
569 |
"""<input type='submit'>, main button to submit a form""" |
1304 | 570 |
type = 'submit' |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
571 |
|
1474 | 572 |
|
1304 | 573 |
class ResetButton(Button): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
574 |
"""<input type='reset'>, main button to reset a form. |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
575 |
You usually don't want this. |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
576 |
""" |
1304 | 577 |
type = 'reset' |
578 |
||
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
579 |
|
1304 | 580 |
class ImgButton(object): |
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
581 |
"""<img> wrapped into a <a> tag with href triggering something (usually a |
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
582 |
javascript call) |
1389 | 583 |
|
584 |
note label is a msgid which will be translated at form generation time, you |
|
585 |
should not give an already translated string. |
|
1330
92343a468e2a
add some documentation, backport *CompletionWidget
sylvain.thenault@logilab.fr
parents:
1329
diff
changeset
|
586 |
""" |
1304 | 587 |
def __init__(self, domid, href, label, imgressource): |
588 |
self.domid = domid |
|
589 |
self.href = href |
|
590 |
self.imgressource = imgressource |
|
591 |
self.label = label |
|
1474 | 592 |
|
2522
562f5dcf2345
widget.render now takes the renderer as third argument (keeping minimal bw compat)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2519
diff
changeset
|
593 |
def render(self, form, field=None, renderer=None): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
594 |
label = form._cw._(self.label) |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3388
diff
changeset
|
595 |
imgsrc = form._cw.external_resource(self.imgressource) |
1389 | 596 |
return '<a id="%(domid)s" href="%(href)s">'\ |
597 |
'<img src="%(imgsrc)s" alt="%(label)s"/>%(label)s</a>' % { |
|
598 |
'label': label, 'imgsrc': imgsrc, |
|
599 |
'domid': self.domid, 'href': self.href} |
|
1474 | 600 |
|
1304 | 601 |
|
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
|
602 |
# more widgets ################################################################# |
1474 | 603 |
|
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
|
604 |
class EditableURLWidget(TextInput): |
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
|
605 |
"""custom widget to edit separatly an url path / query string (used by |
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
|
606 |
default for Bookmark.path for instance), dealing with url quoting nicely |
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
|
607 |
(eg user edit the unquoted value). |
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
|
608 |
""" |
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
|
609 |
type = 'text' |
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
|
610 |
|
4372
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
611 |
def _render(self, form, field, renderer): |
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
|
612 |
"""render the widget for the given `field` of `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
|
613 |
|
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
|
614 |
Generate one <input> tag for each field's value |
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
|
615 |
""" |
4372
0c44af150a49
introduce a default render implementation on the base widget, which
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4371
diff
changeset
|
616 |
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
|
617 |
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
|
618 |
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
|
619 |
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
|
620 |
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
|
621 |
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
|
622 |
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
|
623 |
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
|
624 |
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
|
625 |
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
|
626 |
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
|
627 |
value = self.typed_value(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
|
628 |
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
|
629 |
path, qs = value.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
|
630 |
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
|
631 |
path = value |
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
|
632 |
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
|
633 |
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
|
634 |
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
|
635 |
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
|
636 |
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
|
637 |
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
|
638 |
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
|
639 |
# 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
|
640 |
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
|
641 |
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
|
642 |
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
|
643 |
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
|
644 |
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
|
645 |
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
|
646 |
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
|
647 |
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
|
648 |
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
|
649 |
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
|
650 |
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
|
651 |
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
|
652 |
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
|
653 |
u'</td></tr></table>'] |
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
|
654 |
return u'\n'.join(inputs) |
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
|
655 |
|
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
|
656 |
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
|
657 |
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
|
658 |
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
|
659 |
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
|
660 |
if isinstance(path, 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
|
661 |
path = path.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
|
662 |
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
|
663 |
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
|
664 |
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
|
665 |
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
|
666 |
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
|
667 |
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
|
668 |
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
|
669 |
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
|
670 |
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
|
671 |
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
|
672 |
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
|
673 |
# 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
|
674 |
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
|
675 |
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
|
676 |
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
|
677 |
return u'%s?%s' % (path, req.build_url_params(**values)) |