author | sylvain.thenault@logilab.fr |
Fri, 27 Mar 2009 12:30:19 +0100 | |
branch | tls-sprint |
changeset 1179 | 70825477c6ce |
parent 1152 | c99ef2a2535c |
child 1273 | 64e1db70161b |
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 |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
4 |
:copyright: 2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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 |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
6 |
""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
7 |
__docformat__ = "restructuredtext en" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
8 |
|
1096 | 9 |
from datetime import date |
10 |
||
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
11 |
from cubicweb.common import tags |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
12 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
13 |
class FieldWidget(object): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
14 |
needs_js = () |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
15 |
needs_css = () |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
16 |
setdomid = True |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
17 |
settabindex = True |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
18 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
19 |
def __init__(self, attrs=None, setdomid=None, settabindex=None): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
20 |
self.attrs = attrs or {} |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
21 |
if setdomid is not None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
22 |
# override class's default value |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
23 |
self.setdomid = setdomid |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
24 |
if settabindex is not None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
25 |
# override class's default value |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
26 |
self.settabindex = settabindex |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
27 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
28 |
def add_media(self, form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
29 |
"""adds media (CSS & JS) required by this widget""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
30 |
if self.needs_js: |
1096 | 31 |
form.req.add_js(self.needs_js) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
32 |
if self.needs_css: |
1096 | 33 |
form.req.add_css(self.needs_css) |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
34 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
35 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
36 |
raise NotImplementedError |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
37 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
38 |
def _render_attrs(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
39 |
name = form.context[field]['name'] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
40 |
values = form.context[field]['value'] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
41 |
if not isinstance(values, (tuple, list)): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
42 |
values = (values,) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
43 |
attrs = dict(self.attrs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
44 |
if self.setdomid: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
45 |
attrs['id'] = form.context[field]['id'] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
46 |
if self.settabindex: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
47 |
attrs['tabindex'] = form.req.next_tabindex() |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
48 |
return name, values, attrs |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
49 |
|
1096 | 50 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
51 |
class Input(FieldWidget): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
52 |
type = None |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
53 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
54 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
55 |
self.add_media(form) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
56 |
name, values, attrs = self._render_attrs(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
57 |
inputs = [tags.input(name=name, value=value, type=self.type, **attrs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
58 |
for value in values] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
59 |
return u'\n'.join(inputs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
60 |
|
1096 | 61 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
62 |
class TextInput(Input): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
63 |
type = 'text' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
64 |
|
1096 | 65 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
66 |
class PasswordInput(Input): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
67 |
type = 'password' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
68 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
69 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
70 |
self.add_media(form) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
71 |
name, values, attrs = self._render_attrs(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
72 |
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
|
73 |
id = attrs.pop('id') |
c71997f514ba
fix password widget: do not duplicate dom id, fix confirm field name
sylvain.thenault@logilab.fr
parents:
1096
diff
changeset
|
74 |
confirmname = '%s-confirm:%s' % tuple(name.rsplit(':', 1)) |
c71997f514ba
fix password widget: do not duplicate dom id, fix confirm field name
sylvain.thenault@logilab.fr
parents:
1096
diff
changeset
|
75 |
inputs = [tags.input(name=name, 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
|
76 |
'<br/>', |
1110
c71997f514ba
fix password widget: do not duplicate dom id, fix confirm field name
sylvain.thenault@logilab.fr
parents:
1096
diff
changeset
|
77 |
tags.input(name=confirmname, type=self.type, **attrs), |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
78 |
' ', tags.span(form.req._('confirm password'), |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
79 |
**{'class': 'emphasis'})] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
80 |
return u'\n'.join(inputs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
81 |
|
1096 | 82 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
83 |
class FileInput(Input): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
84 |
type = 'file' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
85 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
86 |
def _render_attrs(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
87 |
# ignore value which makes no sense here (XXX even on form validation error?) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
88 |
name, values, attrs = super(FileInput, self)._render_attrs(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
89 |
return name, ('',), attrs |
1096 | 90 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
91 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
92 |
class HiddenInput(Input): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
93 |
type = 'hidden' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
94 |
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
|
95 |
settabindex = False |
1096 | 96 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
97 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
98 |
class ButtonInput(Input): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
99 |
type = 'button' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
100 |
|
1096 | 101 |
|
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
102 |
class TextArea(FieldWidget): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
103 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
104 |
name, values, attrs = self._render_attrs(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
105 |
attrs.setdefault('onkeypress', 'autogrow(this)') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
106 |
if not values: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
107 |
value = u'' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
108 |
elif len(values) == 1: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
109 |
value = values[0] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
110 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
111 |
raise ValueError('a textarea is not supposed to be multivalued') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
112 |
return tags.textarea(value, name=name, **attrs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
113 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
114 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
115 |
class FCKEditor(TextArea): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
116 |
def __init__(self, *args, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
117 |
super(FCKEditor, self).__init__(*args, **kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
118 |
self.attrs['cubicweb:type'] = 'wysiwyg' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
119 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
120 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
121 |
form.req.fckeditor_config() |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
122 |
return super(FCKEditor, self).render(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
123 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
124 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
125 |
class Select(FieldWidget): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
126 |
def __init__(self, attrs=None, multiple=False): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
127 |
super(Select, self).__init__(attrs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
128 |
self.multiple = multiple |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
129 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
130 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
131 |
name, curvalues, attrs = self._render_attrs(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
132 |
options = [] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
133 |
for label, value in field.vocabulary(form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
134 |
if value in curvalues: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
135 |
options.append(tags.option(label, value=value, selected='selected')) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
136 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
137 |
options.append(tags.option(label, value=value)) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
138 |
return tags.select(name=name, multiple=self.multiple, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
139 |
options=options, **attrs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
140 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
141 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
142 |
class CheckBox(Input): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
143 |
type = 'checkbox' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
144 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
145 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
146 |
name, curvalues, attrs = self._render_attrs(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
147 |
options = [] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
148 |
for label, value in field.vocabulary(form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
149 |
if value in curvalues: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
150 |
tag = tags.input(name=name, value=value, type=self.type, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
151 |
checked='checked', **attrs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
152 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
153 |
tag = tags.input(name=name, value=value, type=self.type, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
154 |
**attrs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
155 |
options.append(tag + label) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
156 |
return '<br/>\n'.join(options) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
157 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
158 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
159 |
class Radio(Input): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
160 |
type = 'radio' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
161 |
setdomid = False |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
162 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
163 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
164 |
name, curvalues, attrs = self._render_attrs(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
165 |
options = [] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
166 |
for label, value in field.vocabulary(form): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
167 |
if value in curvalues: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
168 |
options.append(tags.input(name=name, type=self.type, value=value, checked='checked', **attrs)) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
169 |
else: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
170 |
options.append(tags.option(name=name, type=self.type, value=value, **attrs)) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
171 |
options[-1] += label + '<br/>' |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
172 |
return '\n'.join(options) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
173 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
174 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
175 |
class DateTimePicker(TextInput): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
176 |
monthnames = ('january', 'february', 'march', 'april', |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
177 |
'may', 'june', 'july', 'august', |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
178 |
'september', 'october', 'november', 'december') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
179 |
daynames = ('monday', 'tuesday', 'wednesday', 'thursday', |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
180 |
'friday', 'saturday', 'sunday') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
181 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
182 |
needs_js = ('cubicweb.ajax.js', 'cubicweb.calendar.js') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
183 |
needs_css = ('cubicweb.calendar_popup.css',) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
184 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
185 |
@classmethod |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
186 |
def add_localized_infos(cls, req): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
187 |
"""inserts JS variables defining localized months and days""" |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
188 |
# import here to avoid dependancy from cubicweb-common to simplejson |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
189 |
_ = req._ |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
190 |
monthnames = [_(mname) for mname in cls.monthnames] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
191 |
daynames = [_(dname) for dname in cls.daynames] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
192 |
req.html_headers.define_var('MONTHNAMES', monthnames) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
193 |
req.html_headers.define_var('DAYNAMES', daynames) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
194 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
195 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
196 |
txtwidget = super(DateTimePicker, self).render(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
197 |
self.add_localized_infos(form.req) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
198 |
cal_button = self._render_calendar_popup(form, field) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
199 |
return txtwidget + cal_button |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
200 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
201 |
def _render_calendar_popup(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
202 |
req = form.req |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
203 |
value = form.context[field]['rawvalue'] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
204 |
inputid = form.context[field]['id'] |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
205 |
helperid = '%shelper' % inputid |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
206 |
if not value: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
207 |
value = date.today() |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
208 |
year, month = value.year, value.month |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
209 |
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
|
210 |
<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
|
211 |
% (helperid, inputid, year, month, |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
212 |
req.external_resource('CALENDAR_ICON'), |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
213 |
req._('calendar'), helperid) ) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
214 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
215 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
216 |
class AjaxWidget(FieldWidget): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
217 |
def __init__(self, wdgtype, inputid=None, **kwargs): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
218 |
super(AjaxWidget, self).__init__(**kwargs) |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
219 |
self.attrs.setdefault('class', 'widget') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
220 |
self.attrs.setdefault('cubicweb:loadtype', 'auto') |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
221 |
self.attrs['cubicweb:wdgtype'] = wdgtype |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
222 |
if inputid is not None: |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
223 |
self.attrs['cubicweb:inputid'] = inputid |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
224 |
|
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
225 |
def render(self, form, field): |
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
226 |
self.add_media(form) |
1147 | 227 |
attrs = self._render_attrs(form, field)[-1] |
1081
f2a85f52b9e5
move fields and widgets to their own module
sylvain.thenault@logilab.fr
parents:
diff
changeset
|
228 |
return tags.div(**attrs) |
1152 | 229 |
|
230 |
# XXX backport PropertyKeyWidget and PropertyValueWidget |