Commit | Line | Data |
---|---|---|
e16215c4 SJ |
1 | /*************************************************************************** |
2 | * Copyright (C) 2003-2011 Polytechnique.org * | |
3 | * http://opensource.polytechnique.org/ * | |
4 | * * | |
5 | * This program is free software; you can redistribute it and/or modify * | |
6 | * it under the terms of the GNU General Public License as published by * | |
7 | * the Free Software Foundation; either version 2 of the License, or * | |
8 | * (at your option) any later version. * | |
9 | * * | |
10 | * This program is distributed in the hope that it will be useful, * | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
13 | * GNU General Public License for more details. * | |
14 | * * | |
15 | * You should have received a copy of the GNU General Public License * | |
16 | * along with this program; if not, write to the Free Software * | |
17 | * Foundation, Inc., * | |
18 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
19 | ***************************************************************************/ | |
20 | ||
21 | // {{{ Page initialization | |
22 | ||
23 | var baseurl = $.plURL('search/'); | |
e76e94fb | 24 | var address_types = new Array('country', 'administrative_area_level_1', 'administrative_area_level_2', 'locality', 'postal_code'); |
4aab5061 | 25 | var address_types_count = address_types.length; |
e16215c4 SJ |
26 | |
27 | function load_advanced_search(request) | |
28 | { | |
82f06260 | 29 | $('.autocomplete_target').hide(); |
e16215c4 | 30 | $('.autocomplete').show().each(function() { |
e16215c4 SJ |
31 | $(this).autocomplete(baseurl + 'autocomplete/' + this.name, { |
32 | selectOnly: 1, | |
33 | formatItem: make_format_autocomplete(this), | |
34 | field: this.name, | |
35 | onItemSelect: select_autocomplete(this.name), | |
36 | matchSubset: 0, | |
82f06260 SJ |
37 | width: $(this).width() |
38 | }); | |
e16215c4 SJ |
39 | }); |
40 | ||
41 | $('.autocomplete').change(function() { $(this).removeClass('hidden_valid'); }); | |
42 | ||
43 | if (request['country']) { | |
e16215c4 SJ |
44 | setAddress(0, 1, new Array(request['country'], |
45 | request['administrative_area_level_1'], | |
46 | request['administrative_area_level_2'], | |
e16215c4 | 47 | request['locality'], |
e76e94fb | 48 | request['postal_code']) |
e16215c4 SJ |
49 | ); |
50 | } else { | |
4aab5061 SJ |
51 | for (var i = 1; i < address_types_count; ++i) { |
52 | $('tr#' + address_types[i]).hide(); | |
e16215c4 SJ |
53 | } |
54 | } | |
55 | ||
82f06260 | 56 | $(".autocomplete[name='school_text']").change(function() { changeSchool('', ''); }); |
e16215c4 SJ |
57 | changeSchool(request['school'], request['diploma']); |
58 | ||
82f06260 SJ |
59 | $(".autocomplete_to_select").each(function() { |
60 | var field_name = $(this).attr('href'); | |
e16215c4 | 61 | |
82f06260 SJ |
62 | if ($(".autocomplete_target[name='" + field_name + "']").val()) { |
63 | display_list(field_name); | |
64 | } | |
e16215c4 | 65 | |
82f06260 SJ |
66 | $(this).attr('href', baseurl + 'list/' + field_name).click(function() { |
67 | if ($(this).attr('title') == 'display') { | |
68 | display_list(field_name); | |
69 | } else { | |
70 | var value = $("select[name='" + field_name + "']").val(); | |
71 | var text_value = $("select[name='" + field_name + "'] option:selected").text(); | |
72 | $('#' + field_name + '_list').html(''); | |
73 | $(".autocomplete[name='" + field_name + "_text']").show(); | |
74 | $('#' + field_name + '_table').attr('title', 'display'); | |
75 | if (value) { | |
76 | $(".autocomplete_target[name='" + field_name + "']").val(value); | |
77 | $(".autocomplete[name='" + field_name + "_text']").val(text_value).addClass('hidden_valid'); | |
78 | } | |
79 | } | |
e16215c4 SJ |
80 | |
81 | return false; | |
82 | }); | |
e16215c4 SJ |
83 | }); |
84 | ||
85 | $('#only_referent').change(function() { changeOnlyReferent(); }); | |
4aab5061 SJ |
86 | |
87 | $('.delete_address_component').click(function() { | |
88 | var field_name = $(this).attr('href'); | |
89 | var hide = false; | |
249510f5 | 90 | var remove = false; |
4aab5061 SJ |
91 | |
92 | for (var i = 1; i < address_types_count; ++i) { | |
93 | if (field_name == address_types[i]) { | |
94 | hide = true; | |
95 | } | |
96 | if (hide) { | |
249510f5 SJ |
97 | if (field_name != address_types[i]) { |
98 | remove = true; | |
99 | } | |
100 | delete_address_component(address_types[i], remove); | |
4aab5061 SJ |
101 | } |
102 | } | |
103 | ||
104 | return false; | |
105 | }); | |
e16215c4 SJ |
106 | } |
107 | ||
82f06260 SJ |
108 | function display_list(field_name) |
109 | { | |
110 | var value = $("input.autocomplete_target[name='" + field_name + "']").val(); | |
111 | ||
112 | $('#' + field_name + '_list').load(baseurl + 'list/' + field_name, {}, function(selectBox) { | |
113 | $(".autocomplete_target[name='" + field_name + "']").val(''); | |
114 | $(".autocomplete[name='" + field_name + "_text']").hide().val('').removeClass('hidden_valid'); | |
115 | $("select[name='" + field_name + "']").val(value); | |
116 | $('#' + field_name + '_table').attr('title', 'hide'); | |
117 | }); | |
118 | } | |
119 | ||
e16215c4 SJ |
120 | // }}} |
121 | // {{{ Regexps to wipe out from search queries | |
122 | ||
123 | var default_form_values = [ /&woman=0(&|$)/, /&subscriber=0(&|$)/, /&alive=0(&|$)/, /&egal[12]=[^&]*&promo[12]=(&|$)/g, /&networking_type=0(&|$)/, /&[^&=]+=(&|$)/g ]; | |
124 | ||
125 | /** Uses javascript to clean form from all empty fields */ | |
222195aa | 126 | function cleanForm(f, targeturl) |
e16215c4 SJ |
127 | { |
128 | var query = $(f).formSerialize(); | |
129 | var old_query; | |
130 | for (var i in default_form_values) { | |
131 | var reg = default_form_values[i]; | |
132 | if (typeof(reg) != 'undefined') { | |
133 | do { | |
134 | old_query = query; | |
135 | query = query.replace(reg, '$1'); | |
136 | } while (old_query != query); | |
137 | } | |
138 | } | |
139 | query = query.replace(/^&*(.*)&*$/, '$1'); | |
140 | if (query == 'rechercher=Chercher') { | |
141 | alert("Aucun critère n'a été spécifié."); | |
142 | return false; | |
143 | } | |
222195aa | 144 | document.location = targeturl + '?' + query; |
e16215c4 SJ |
145 | return false; |
146 | } | |
147 | ||
148 | // }}} | |
149 | // {{{ Autocomplete related functions. | |
150 | ||
151 | // display an autocomplete row : blabla (nb of found matches) | |
152 | function make_format_autocomplete(block) | |
153 | { | |
154 | return function(row) { | |
155 | regexp = new RegExp('(' + RegExp.escape(block.value) + ')', 'i'); | |
156 | name = row[0].htmlEntities().replace(regexp, '<strong>$1<\/strong>'); | |
157 | ||
158 | if (row[1] === '-1') { | |
159 | return '…'; | |
160 | } | |
161 | if (row[1] === '-2') { | |
162 | return '<em>aucun camarade trouvé pour '+row[0].htmlEntities()+'<\/em>'; | |
163 | } | |
164 | ||
165 | mate = (row[1] > 1) ? 'camarades' : 'camarade'; | |
166 | return name + '<em> - ' + row[1].htmlEntities() + ' ' + mate + '<\/em>'; | |
167 | }; | |
168 | } | |
169 | ||
170 | function cancel_autocomplete(field, realfield) | |
171 | { | |
172 | $(".autocomplete[name='" + field + "']").removeClass('hidden_valid').val('').focus(); | |
173 | if (typeof(realfield) != 'undefined') { | |
82f06260 | 174 | $(".autocomplete_target[name='" + realfield + "']").val(''); |
e16215c4 SJ |
175 | } |
176 | return; | |
177 | } | |
178 | ||
179 | // when choosing autocomplete from list, must validate | |
180 | function select_autocomplete(name) | |
181 | { | |
5c89683f | 182 | var field_name = name.replace(/_text$/, ''); |
e16215c4 | 183 | |
f6ce54a7 | 184 | // just display field as valid if field is not a text field for a list |
5c89683f | 185 | if (field_name == name) { |
f6ce54a7 SJ |
186 | return function(i) { |
187 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); | |
188 | } | |
e16215c4 SJ |
189 | } |
190 | ||
4dea11cc SJ |
191 | // When changing country, locality or school, open next address component. |
192 | if (field_name == 'country' || field_name == 'locality' || field_name == 'school') { | |
e16215c4 | 193 | return function(i) { |
e16215c4 | 194 | if (i.extra[0] < 0) { |
5c89683f | 195 | cancel_autocomplete(name, field_name); |
e16215c4 SJ |
196 | i.extra[1] = ''; |
197 | } | |
e16215c4 | 198 | |
4dea11cc SJ |
199 | if (field_name == 'school') { |
200 | changeSchool(i.extra[1], ''); | |
201 | } else { | |
202 | changeAddressComponents(field_name, i.extra[1]); | |
e16215c4 | 203 | } |
4dea11cc SJ |
204 | |
205 | $(".autocomplete_target[name='" + field_name + "']").attr('value', i.extra[1]); | |
206 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); | |
e16215c4 | 207 | } |
4dea11cc | 208 | } |
e16215c4 SJ |
209 | |
210 | // change field in list and display text field as valid | |
211 | return function(i) { | |
e16215c4 | 212 | if (i.extra[0] < 0) { |
5c89683f | 213 | cancel_autocomplete(this.field, field_name); |
e16215c4 SJ |
214 | return; |
215 | } | |
216 | ||
5c89683f | 217 | $(".autocomplete_target[name='" + field_name + "']").attr('value', i.extra[1]); |
f6ce54a7 | 218 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); |
e16215c4 SJ |
219 | } |
220 | } | |
221 | ||
222 | // }}} | |
223 | // {{{ Various search functions. | |
224 | ||
225 | function setAddress(i, j, values) | |
226 | { | |
227 | var prev_type = address_types[i]; | |
228 | var next_type = address_types[j]; | |
229 | var next_list = next_type + '_list'; | |
230 | ||
4aab5061 | 231 | $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:values[i] }, function() { |
e16215c4 | 232 | if ($("select[name='" + next_type + "']").children("option").size() > 1) { |
4aab5061 | 233 | $("tr#" + next_type).show(); |
e16215c4 | 234 | $("select[name='" + next_type + "']").attr('value', values[j]); |
e0088da2 SJ |
235 | if (next_type == 'locality') { |
236 | $('tr#locality_text').hide(); | |
237 | $("select[name='locality_text']").attr('value', ''); | |
238 | } | |
4aab5061 | 239 | if (j < address_types_count) { |
e16215c4 SJ |
240 | setAddress(j, j + 1, values); |
241 | } | |
242 | } else { | |
4aab5061 | 243 | $("tr#" + next_type).hide(); |
e16215c4 | 244 | $("select[name='" + next_type + "']").attr('value', ''); |
4aab5061 | 245 | if (j < address_types_count) { |
e16215c4 SJ |
246 | setAddress(i, j + 1, values); |
247 | } | |
248 | } | |
249 | }); | |
250 | } | |
251 | ||
252 | function displayNextAddressComponent(i, j, value) | |
253 | { | |
254 | var prev_type = address_types[i]; | |
255 | var next_type = address_types[j]; | |
256 | var next_list = next_type + '_list'; | |
257 | ||
4aab5061 | 258 | if (next_type == 'locality') { |
e16215c4 | 259 | $('tr#locality_text').hide(); |
82f06260 | 260 | $("select[name='locality_text']").attr('value', ''); |
e16215c4 SJ |
261 | } |
262 | ||
4aab5061 | 263 | $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:value }, function() { |
e16215c4 SJ |
264 | $("select[name='" + next_type + "']").attr('value', ''); |
265 | if ($("select[name='" + next_type + "']").children('option').size() > 1) { | |
4aab5061 | 266 | $('tr#' + next_type).show(); |
e16215c4 | 267 | } else { |
4aab5061 SJ |
268 | $('tr#' + next_type).hide(); |
269 | if (j < address_types_count) { | |
e16215c4 SJ |
270 | displayNextAddressComponent(i, j + 1, value); |
271 | } | |
272 | } | |
273 | }); | |
274 | } | |
275 | ||
276 | function changeAddressComponents(type, value) | |
277 | { | |
4aab5061 | 278 | var i = 0; |
e16215c4 | 279 | |
4aab5061 | 280 | while (address_types[i] != type && i < address_types_count) { |
e16215c4 SJ |
281 | ++i; |
282 | } | |
283 | ||
4aab5061 | 284 | for (var j = i + 1; j < address_types_count; ++j) { |
249510f5 | 285 | delete_address_component(address_types[j], true); |
e16215c4 SJ |
286 | } |
287 | ||
4aab5061 | 288 | if (value != '' && i < address_types_count) { |
e16215c4 SJ |
289 | $("select[name='" + type + "']").attr('value', value); |
290 | displayNextAddressComponent(i, i + 1, value); | |
291 | } | |
292 | } | |
293 | ||
249510f5 | 294 | function delete_address_component(field_name, remove) |
4aab5061 | 295 | { |
249510f5 SJ |
296 | if (remove || field_name == 'locality') { |
297 | $('tr#' + field_name).hide(); | |
298 | $('#' + field_name + '_list').html(''); | |
4aab5061 | 299 | |
249510f5 SJ |
300 | if (field_name == 'locality') { |
301 | $("input[name='locality_text']").val(''); | |
302 | $('tr#locality_text').show(); | |
303 | } | |
304 | } else { | |
305 | $("select[name='" + field_name + "']").val(''); | |
306 | $("input[name='" + field_name + "']").val(''); | |
4aab5061 SJ |
307 | } |
308 | } | |
309 | ||
e16215c4 SJ |
310 | // when changing school, open diploma choice |
311 | function changeSchool(schoolId, diploma) | |
312 | { | |
313 | $(".autocompleteTarget[name='school']").attr('value', schoolId); | |
314 | ||
315 | if (schoolId) { | |
82f06260 | 316 | $(".autocomplete[name='school_text']").addClass('hidden_valid'); |
e16215c4 | 317 | } else { |
82f06260 | 318 | $(".autocomplete[name='school_text']").removeClass('hidden_valid'); |
e16215c4 SJ |
319 | } |
320 | ||
321 | $("[name='diploma']").parent().load(baseurl + 'list/diploma/', { school:schoolId }, function() { | |
322 | $("select[name='diploma']").attr('value', diploma); | |
323 | }); | |
324 | } | |
325 | ||
e16215c4 SJ |
326 | // when choosing a job term in tree, hide tree and set job term field |
327 | function searchForJobTerm(treeid, jtid, full_name) | |
328 | { | |
329 | $(".term_tree").remove(); | |
82f06260 | 330 | $("input[name='jobterm_text']").val(full_name).addClass("hidden_valid").show(); |
e16215c4 SJ |
331 | $("input[name='jobterm']").val(jtid); |
332 | } | |
333 | ||
334 | // }}} | |
335 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |