-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidgets.py
254 lines (206 loc) · 8.91 KB
/
widgets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# -*- coding: UTF-8 -*-
# Copyright (C) 2010 Sylvain Taverne <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from itools
from itools.datatypes import Integer
from itools.gettext import MSG
from itools.web import get_context
from itools.xml import XMLParser
# Import from ikaaro
from ikaaro.forms import SelectRadio, Widget, stl_namespaces
from ikaaro.forms import DateWidget, RTEWidget, BooleanCheckBox
from ikaaro.forms import MultilineWidget, SelectWidget
# Import from shop
from datatypes import Days, Months, Years
from registry import register_widget
class SelectRadioList(SelectRadio):
template = list(XMLParser("""
<ul>
<li stl:if="has_empty_option">
<input type="radio" name="${name}" value="" checked="checked"
stl:if="none_selected"/>
<input type="radio" name="${name}" value=""
stl:if="not none_selected"/>
<stl:block stl:if="not is_inline"><br/></stl:block>
</li>
<li stl:repeat="option options">
<input type="radio" id="${id}-${option/name}" name="${name}"
value="${option/name}" checked="checked"
stl:if="option/selected"/>
<input type="radio" id="${id}-${option/name}" name="${name}"
value="${option/name}" stl:if="not option/selected"/>
<label for="${id}-${option/name}">${option/value}</label>
<stl:block stl:if="not is_inline"><br/></stl:block>
</li>
</ul>
""", stl_namespaces))
class SelectRadioImages(SelectRadio):
template = list(XMLParser("""
<ul style="list-style-type:none;margin:0;padding:0;">
<li stl:if="has_empty_option" style="width:110px;height=110px;float:left;">
<input id="${id}" type="radio" name="${name}" value="" checked="checked"
stl:if="none_selected"/>
<input id="${id}" type="radio" name="${name}" value=""
stl:if="not none_selected"/>
<label for="${id}"
style="width:60px;height:60px;display:block;border:1px dashed gray;
padding:20px;">
No picture
</label>
</li>
<li style="float:left;width:110px;height=110px;" stl:repeat="option options">
<input type="radio" id="${id}-${option/name}" name="${name}"
value="${option/name}" checked="checked"
stl:if="option/selected"/>
<input type="radio" id="${id}-${option/name}" name="${name}"
value="${option/name}" stl:if="not option/selected"/>
<label for="${id}-${option/name}">
<img src="${option/link}/;thumb?width=100&height=100" title=" ${option/value}"/>
</label>
</li>
</ul>
""", stl_namespaces))
class SelectRadioColor(SelectRadio):
template = list(XMLParser("""
<ul class="select-radio-color">
<li stl:repeat="option options" class="${id}-opt-color">
<div id="opt-${id}-${option/name}"
style="background-color:${option/color}"
title="${option/value}" stl:omit-tag="option/selected">
<div id="opt-${id}-${option/name}"
style="background-color:${option/color}"
title="${option/value}" stl:omit-tag="not option/selected" class="selected">
<input
type="radio" id="${id}-${option/name}" name="${name}"
value="${option/name}" checked="checked"
stl:if="option/selected"/>
<input
type="radio" id="${id}-${option/name}" name="${name}"
value="${option/name}" stl:if="not option/selected"/>
<span>${option/value}</span>
</div>
</div>
</li>
</ul>
<script>
$(document).ready(function() {
$(".${id}-opt-color div").each(function(){
$(this).click(function(){
$(".${id}-opt-color div").removeClass('selected');
$(this).addClass('selected');
$(this).children('input').attr('checked', 'checked');
})
});
});
</script>
""", stl_namespaces))
class NumberRangeWidget(Widget):
template = list(XMLParser("""
Min:
<input type="text" name="${name}" value="${value1}" id="${name}-min"
size="${size}" />
Max:
<input type="text" name="${name}" value="${value2}" id="${name}-max"
size="${size}" />
""", stl_namespaces))
def get_namespace(self, datatype, value):
namespace = Widget.get_namespace(self, datatype, value)
namespace['value1'], namespace['value2'] = datatype.default
return namespace
class FilesWidget(Widget):
template = list(XMLParser(
"""
<input type="file" id="${id}" name="${name}" value="${value}"/><br/>
<input type="file" id="${id}" name="${name}" value="${value}"/><br/>
<input type="file" id="${id}" name="${name}" value="${value}"/><br/>
<input type="file" id="${id}" name="${name}" value="${value}"/><br/>
""", stl_namespaces))
class RangeSlider(Widget):
template = list(XMLParser("""
<input type="text" id="${id}-amount" style="border:0; color:#f6931f; font-weight:bold;" />
<div id="${id}"/>
<script type="text/javascript" src="/ui/shop/js/jquery.slider.js"/>
<script type="text/javascript">
$(function() {
$("#${id}").slider({
range: true,
min: 0,
max: 5000,
values: [0, 10],
slide: function(event, ui) {
$("#${id}-amount").val(ui.values[0] + ' - ' + ui.values[1]);
}
});
});
</script>
""", stl_namespaces))
def get_namespace(self, datatype, value):
namespace = Widget.get_namespace(self, datatype, value)
namespace['title'] = self.title
return namespace
class FrenchDateWidget(DateWidget):
format = '%d/%m/%Y'
tip = MSG(u'Format: 12/04/1985')
class UnicodeOnePerLineWidget(MultilineWidget):
pass
class BooleanCheckBox_CGU(BooleanCheckBox):
description = MSG(u"I'm agree with the conditions general of use")
link = './cgu'
template = list(XMLParser("""
<input type="checkbox" id="${id}" name="${name}" value="1"
checked="${is_selected}" />
<a href="${link}" target="_blank">${description}</a>
""", stl_namespaces))
def get_namespace(self, datatype, value):
namespace = BooleanCheckBox.get_namespace(self, datatype, value)
namespace['description'] = self.description
namespace['link'] = self.link
return namespace
class RTEWidget_Iframe(RTEWidget):
extended_valid_elements = "iframe[src|name|id|class|style|frameborder|width|height]"
class SIRETWidget(Widget):
template = list(XMLParser("""
<input type="text" id="${id}" name="${name}" value="${value}"/>
<script type="text/javascript" src="/ui/shop/js/jquery.maskedinput-1.2.2.min.js"/>
<script type="text/javascript">
$("#${id}").mask("999 999 999 99999");
</script>
""", stl_namespaces))
class BirthdayWidget(Widget):
template = list(XMLParser("""
${day} ${month} ${year}
<input type="hidden" name="${name}" value="1"/>
""", stl_namespaces))
def get_namespace(self, datatype, value):
namespace = Widget.get_namespace(self, datatype, value)
context = get_context()
if value and value != '1':
try:
day, month, year = value.split('/')
day = int(day)
month = int(month)
year = int(year)
except:
day = month = year = None
else:
day = context.get_form_value('day', type=Integer)
month = context.get_form_value('month', type=Integer)
year = context.get_form_value('year', type=Integer)
namespace['day'] = SelectWidget('day').to_html(Days, day)
namespace['month'] = SelectWidget('month').to_html(Months, month)
namespace['year'] = SelectWidget('year').to_html(Years, year)
return namespace
register_widget('siret', MSG(u'SIRET Widget'), SIRETWidget)
register_widget('birthday', MSG(u'Birthday Widget'), BirthdayWidget)