1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
import tw2.core as twc |
3
|
|
|
import tw2.jqplugins.select2 as twsel |
4
|
|
|
from tw2.core.validation import Validator |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
select2_persistence_js = twc.JSSource(src=''' |
8
|
|
|
// This function returns the value of the specified GET parameter. |
9
|
|
|
// The code has been found at the following url: |
10
|
|
|
// http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript |
11
|
|
|
// Big Kudos to you mister Jonah. |
12
|
|
|
var params = function() { |
13
|
|
|
function urldecode(str) { |
14
|
|
|
return decodeURIComponent((str+'').replace(/\+/g, '%20')); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
function transformToAssocArray( prmstr ) { |
18
|
|
|
var params = {}; |
19
|
|
|
var prmarr = prmstr.split("&"); |
20
|
|
|
for ( var i = 0; i < prmarr.length; i++) { |
21
|
|
|
var tmparr = prmarr[i].split("="); |
22
|
|
|
params[tmparr[0]] = urldecode(tmparr[1]); |
23
|
|
|
} |
24
|
|
|
return params; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
var prmstr = window.location.search.substr(1); |
28
|
|
|
return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {}; |
29
|
|
|
}(); |
30
|
|
|
''') |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
class PersistentSelect2MultipleSelect(twsel.Select2MultipleSelectField): |
34
|
|
|
def __init__(self, **kwargs): |
35
|
|
|
super(PersistentSelect2MultipleSelect, self).__init__(**kwargs) |
36
|
|
|
self.resources = [select2_persistence_js] |
37
|
|
|
self.ondemand = True |
38
|
|
|
self.validator = self.validator or Validator() |
39
|
|
|
|
40
|
|
|
self.options = [] |
41
|
|
|
|
42
|
|
|
self.opts = self.opts.copy() |
43
|
|
|
self.opts['initSelection'] = twc.js_callback( |
44
|
|
|
''' |
45
|
|
|
function(element, callback) { |
46
|
|
|
var init_data = []; |
47
|
|
|
|
48
|
|
|
param = params['%(param)s']; |
49
|
|
|
|
50
|
|
|
if(typeof param !== "undefined" && param) { |
51
|
|
|
$.each(param.split(','), function(i, v) { |
52
|
|
|
var elem = {}; |
53
|
|
|
elem.id = v; |
54
|
|
|
elem.text = v; |
55
|
|
|
init_data.push(elem); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
callback(init_data); |
60
|
|
|
} |
61
|
|
|
''' % dict(param=self.name) |
62
|
|
|
) |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
class PersistentSelect2SingleSelect(twsel.Select2SingleSelectField): |
66
|
|
|
def __init__(self, **kwargs): |
67
|
|
|
super(PersistentSelect2SingleSelect, self).__init__(**kwargs) |
68
|
|
|
self.resources = [select2_persistence_js] |
69
|
|
|
|
70
|
|
|
self.ondemand = True |
71
|
|
|
self.validator = self.validator or Validator() |
72
|
|
|
|
73
|
|
|
self.js_dict_entries = ['%s: "%s"' % (key, value) |
74
|
|
|
for key, value in self.options] |
75
|
|
|
self.js_opts_dict = '{%s}' % ', '.join(self.js_dict_entries) |
76
|
|
|
|
77
|
|
|
self.opts = self.opts.copy() |
78
|
|
|
self.opts['initSelection'] = twc.js_callback( |
79
|
|
|
''' |
80
|
|
|
function (element, callback) { |
81
|
|
|
var init_data; |
82
|
|
|
var list_opts = %(options)s; |
83
|
|
|
var param = params['%(param)s']; |
84
|
|
|
|
85
|
|
|
if(typeof param !== "undefined" && param) { |
86
|
|
|
var elem = {}; |
87
|
|
|
elem.id = param; |
88
|
|
|
|
89
|
|
|
if(param in list_opts) { |
90
|
|
|
elem.text = list_opts[param]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
init_data = elem; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
callback(init_data); |
97
|
|
|
} |
98
|
|
|
''' % dict(options=self.js_opts_dict, param=self.name) |
99
|
|
|
) |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
class GeocompleteField(twsel.Select2AjaxSingleSelectField): |
103
|
|
|
def __init__(self, **kwargs): |
104
|
|
|
super(GeocompleteField, self).__init__(**kwargs) |
105
|
|
|
self.resources = [ |
106
|
|
|
select2_persistence_js, |
107
|
|
|
twc.JSSource( |
108
|
|
|
src=''' |
109
|
|
|
function format_name(name, complement, postal_code, country) { |
110
|
|
|
var display_name = name; |
111
|
|
|
display_name += ' '; |
112
|
|
|
|
113
|
|
|
if(complement) { |
114
|
|
|
display_name += complement; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
display_name += ' - '; |
118
|
|
|
display_name += postal_code; |
119
|
|
|
display_name += ', '; |
120
|
|
|
display_name += country; |
121
|
|
|
|
122
|
|
|
return display_name.toUpperCase(); |
123
|
|
|
} |
124
|
|
|
''' |
125
|
|
|
) |
126
|
|
|
] |
127
|
|
|
self.options = [] |
128
|
|
|
self.ondemand = True |
129
|
|
|
self.validator = self.validator or Validator() |
130
|
|
|
|
131
|
|
|
self.opts.copy() |
132
|
|
|
self.opts['minimumInputLength'] = 1 |
133
|
|
|
self.opts['maximumInputLength'] = 125 |
134
|
|
|
self.opts['allowClear'] = True |
135
|
|
|
self.opts['dropdownAutoWidth'] = True |
136
|
|
|
|
137
|
|
|
self.opts['initSelection'] = twc.js_callback( |
138
|
|
|
""" |
139
|
|
|
function (element, callback) { |
140
|
|
|
var init_data; |
141
|
|
|
|
142
|
|
|
param = params['%(name)s']; |
143
|
|
|
|
144
|
|
|
if(typeof param !== "undefined" && param) { |
145
|
|
|
var elem = {}; |
146
|
|
|
param_dict = JSON.parse(param); |
147
|
|
|
elem.id = param; |
148
|
|
|
|
149
|
|
|
name = format_name( |
150
|
|
|
param_dict['name'], |
151
|
|
|
param_dict['complement'], |
152
|
|
|
param_dict['postal_code'], |
153
|
|
|
param_dict['country'] |
154
|
|
|
); |
155
|
|
|
|
156
|
|
|
elem.name = name; |
157
|
|
|
elem.value = name; |
158
|
|
|
|
159
|
|
|
init_data = elem; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
callback(init_data); |
163
|
|
|
} |
164
|
|
|
""" % dict(name=self.name) |
165
|
|
|
) |
166
|
|
|
self.opts['escapeMarkup'] = twc.js_callback( |
167
|
|
|
""" |
168
|
|
|
function(markup) { |
169
|
|
|
return markup; |
170
|
|
|
} |
171
|
|
|
""" |
172
|
|
|
) |
173
|
|
|
self.opts['formatResult'] = twc.js_callback( |
174
|
|
|
""" |
175
|
|
|
function(location) { |
176
|
|
|
var markup = '<option value="' + location.value + '">' |
177
|
|
|
+ location.name |
178
|
|
|
+ '</option>'; |
179
|
|
|
return markup; |
180
|
|
|
} |
181
|
|
|
""" |
182
|
|
|
) |
183
|
|
|
self.opts['formatSelection'] = twc.js_callback( |
184
|
|
|
""" |
185
|
|
|
function(location) { |
186
|
|
|
if(typeof location !== "undefined") { |
187
|
|
|
return location.value || location.text; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
""" |
191
|
|
|
) |
192
|
|
|
|
193
|
|
|
self.opts['ajax'] = dict( |
194
|
|
|
url='/geocomplete', |
195
|
|
|
dataType='json', |
196
|
|
|
type='POST', |
197
|
|
|
quietMillis=100, |
198
|
|
|
cache=True, |
199
|
|
|
data=twc.js_callback( |
200
|
|
|
""" |
201
|
|
|
function(term) { |
202
|
|
|
return {address: term}; |
203
|
|
|
} |
204
|
|
|
""" |
205
|
|
|
), |
206
|
|
|
results=twc.js_callback( |
207
|
|
|
""" |
208
|
|
|
function(data) { |
209
|
|
|
var results = []; |
210
|
|
|
|
211
|
|
|
if ('results' in data) { |
212
|
|
|
$.each(data['results'], function(i, v) { |
213
|
|
|
var o = {}; |
214
|
|
|
o.id = JSON.stringify(v); |
215
|
|
|
name = format_name( |
216
|
|
|
v['name'], |
217
|
|
|
v['complement'], |
218
|
|
|
v['postal_code'], |
219
|
|
|
v['country'] |
220
|
|
|
); |
221
|
|
|
o.name = name; |
222
|
|
|
o.value = name; |
223
|
|
|
results.push(o); |
224
|
|
|
}); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return {results: results}; |
228
|
|
|
} |
229
|
|
|
""" |
230
|
|
|
) |
231
|
|
|
) |
232
|
|
|
|
233
|
|
|
def prepare(self): |
234
|
|
|
# Temporary workaround a bug in Toscawidget2 Select2 widget prepare, |
235
|
|
|
# which would make the persistence persistence bug (due to bugous values |
236
|
|
|
# introduced in $().select2("val", ...)). |
237
|
|
|
value = None |
238
|
|
|
try: |
239
|
|
|
value = self.value |
240
|
|
|
delattr(self, 'value') |
241
|
|
|
except AttributeError: |
242
|
|
|
pass |
243
|
|
|
|
244
|
|
|
super(GeocompleteField, self).prepare() |
245
|
|
|
|
246
|
|
|
if value: |
247
|
|
|
setattr(self, 'value', value) |
248
|
|
|
|