Commit | Line | Data |
---|---|---|
e16215c4 | 1 | /*************************************************************************** |
c441aabe | 2 | * Copyright (C) 2003-2014 Polytechnique.org * |
e16215c4 SJ |
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; |
38f35d89 | 26 | var autocomplete_sub = {'country': 'locality_text'}; |
e16215c4 SJ |
27 | |
28 | function load_advanced_search(request) | |
29 | { | |
82f06260 | 30 | $('.autocomplete_target').hide(); |
e16215c4 | 31 | $('.autocomplete').show().each(function() { |
23ccdf38 SJ |
32 | $(this).autocomplete({ |
33 | source: baseurl + 'autocomplete/' + this.name, | |
34 | select: function(event, ui) { | |
35 | select_autocomplete(this.name, ui.item.id); | |
36 | }, | |
37 | change: function(event, ui) { | |
38 | if (ui.item != null && ui.item.field != null) { | |
39 | $(this).val(ui.item.field); | |
40 | } | |
41 | } | |
82f06260 | 42 | }); |
e16215c4 SJ |
43 | }); |
44 | ||
45 | $('.autocomplete').change(function() { $(this).removeClass('hidden_valid'); }); | |
46 | ||
47 | if (request['country']) { | |
e16215c4 SJ |
48 | setAddress(0, 1, new Array(request['country'], |
49 | request['administrative_area_level_1'], | |
50 | request['administrative_area_level_2'], | |
e16215c4 | 51 | request['locality'], |
e76e94fb | 52 | request['postal_code']) |
e16215c4 SJ |
53 | ); |
54 | } else { | |
4aab5061 SJ |
55 | for (var i = 1; i < address_types_count; ++i) { |
56 | $('tr#' + address_types[i]).hide(); | |
e16215c4 SJ |
57 | } |
58 | } | |
59 | ||
82f06260 | 60 | $(".autocomplete[name='school_text']").change(function() { changeSchool('', ''); }); |
e16215c4 SJ |
61 | changeSchool(request['school'], request['diploma']); |
62 | ||
82f06260 SJ |
63 | $(".autocomplete_to_select").each(function() { |
64 | var field_name = $(this).attr('href'); | |
e16215c4 | 65 | |
82f06260 SJ |
66 | if ($(".autocomplete_target[name='" + field_name + "']").val()) { |
67 | display_list(field_name); | |
68 | } | |
e16215c4 | 69 | |
82f06260 SJ |
70 | $(this).attr('href', baseurl + 'list/' + field_name).click(function() { |
71 | if ($(this).attr('title') == 'display') { | |
72 | display_list(field_name); | |
73 | } else { | |
74 | var value = $("select[name='" + field_name + "']").val(); | |
75 | var text_value = $("select[name='" + field_name + "'] option:selected").text(); | |
76 | $('#' + field_name + '_list').html(''); | |
77 | $(".autocomplete[name='" + field_name + "_text']").show(); | |
78 | $('#' + field_name + '_table').attr('title', 'display'); | |
79 | if (value) { | |
80 | $(".autocomplete_target[name='" + field_name + "']").val(value); | |
81 | $(".autocomplete[name='" + field_name + "_text']").val(text_value).addClass('hidden_valid'); | |
82 | } | |
83 | } | |
e16215c4 SJ |
84 | |
85 | return false; | |
86 | }); | |
e16215c4 SJ |
87 | }); |
88 | ||
4aab5061 SJ |
89 | $('.delete_address_component').click(function() { |
90 | var field_name = $(this).attr('href'); | |
91 | var hide = false; | |
249510f5 | 92 | var remove = false; |
4aab5061 SJ |
93 | |
94 | for (var i = 1; i < address_types_count; ++i) { | |
95 | if (field_name == address_types[i]) { | |
96 | hide = true; | |
97 | } | |
98 | if (hide) { | |
249510f5 SJ |
99 | if (field_name != address_types[i]) { |
100 | remove = true; | |
101 | } | |
102 | delete_address_component(address_types[i], remove); | |
4aab5061 SJ |
103 | } |
104 | } | |
105 | ||
106 | return false; | |
107 | }); | |
e16215c4 SJ |
108 | } |
109 | ||
82f06260 SJ |
110 | function display_list(field_name) |
111 | { | |
112 | var value = $("input.autocomplete_target[name='" + field_name + "']").val(); | |
113 | ||
114 | $('#' + field_name + '_list').load(baseurl + 'list/' + field_name, {}, function(selectBox) { | |
115 | $(".autocomplete_target[name='" + field_name + "']").val(''); | |
116 | $(".autocomplete[name='" + field_name + "_text']").hide().val('').removeClass('hidden_valid'); | |
117 | $("select[name='" + field_name + "']").val(value); | |
118 | $('#' + field_name + '_table').attr('title', 'hide'); | |
119 | }); | |
120 | } | |
121 | ||
e16215c4 SJ |
122 | // }}} |
123 | // {{{ Regexps to wipe out from search queries | |
124 | ||
f0364fc1 SJ |
125 | var default_form_values = [ /&woman=0(&|$)/, /&subscriber=0(&|$)/, /&alive=0(&|$)/, /&egal2=[^&]*&promo2=(&|$)/, |
126 | /&egal1=[^&]*&promo1=&edu_type=(?:Ing[^n]+nieur|Master|Doctorat)(&|$)/, /&networking_type=0(&|$)/, | |
127 | /&origin_corps=0(&|$)/, /¤t_corps=0(&|$)/, | |
128 | /corps_rank=0(&|$)/, /&has_email_redirect=0(&|$)/, /&[^&=]+=(&|$)/g ]; | |
e16215c4 SJ |
129 | |
130 | /** Uses javascript to clean form from all empty fields */ | |
222195aa | 131 | function cleanForm(f, targeturl) |
e16215c4 SJ |
132 | { |
133 | var query = $(f).formSerialize(); | |
134 | var old_query; | |
135 | for (var i in default_form_values) { | |
136 | var reg = default_form_values[i]; | |
137 | if (typeof(reg) != 'undefined') { | |
138 | do { | |
139 | old_query = query; | |
140 | query = query.replace(reg, '$1'); | |
141 | } while (old_query != query); | |
142 | } | |
143 | } | |
f0364fc1 SJ |
144 | query = query.replace(/^(.*)&+$/, '$1'); |
145 | query = query.replace(/^&+(.*)$/, '$1'); | |
407c9117 SJ |
146 | |
147 | // Removes "(n camarades)" if any of them are remaining. | |
148 | query = query.replace(/\+\(\d+\+camarade(?:s)?\)/, ''); | |
e16215c4 SJ |
149 | if (query == 'rechercher=Chercher') { |
150 | alert("Aucun critère n'a été spécifié."); | |
151 | return false; | |
152 | } | |
ec35cec0 | 153 | document.location = $.plURL(targeturl + '?' + query); |
e16215c4 SJ |
154 | return false; |
155 | } | |
156 | ||
157 | // }}} | |
158 | // {{{ Autocomplete related functions. | |
159 | ||
e16215c4 SJ |
160 | function cancel_autocomplete(field, realfield) |
161 | { | |
162 | $(".autocomplete[name='" + field + "']").removeClass('hidden_valid').val('').focus(); | |
163 | if (typeof(realfield) != 'undefined') { | |
82f06260 | 164 | $(".autocomplete_target[name='" + realfield + "']").val(''); |
e16215c4 SJ |
165 | } |
166 | return; | |
167 | } | |
168 | ||
169 | // when choosing autocomplete from list, must validate | |
23ccdf38 | 170 | function select_autocomplete(name, id) |
e16215c4 | 171 | { |
5c89683f | 172 | var field_name = name.replace(/_text$/, ''); |
38f35d89 SJ |
173 | if (autocomplete_sub[field_name] != null) { |
174 | $(".autocomplete[name='" + autocomplete_sub[field_name] + "']").autocomplete('option', 'source', baseurl + 'autocomplete/' + autocomplete_sub[field_name] + '/' + id); | |
175 | } | |
e16215c4 | 176 | |
f6ce54a7 | 177 | // just display field as valid if field is not a text field for a list |
5c89683f | 178 | if (field_name == name) { |
23ccdf38 SJ |
179 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); |
180 | return; | |
e16215c4 SJ |
181 | } |
182 | ||
4dea11cc SJ |
183 | // When changing country, locality or school, open next address component. |
184 | if (field_name == 'country' || field_name == 'locality' || field_name == 'school') { | |
23ccdf38 SJ |
185 | if (id < 0) { |
186 | cancel_autocomplete(name, field_name); | |
187 | id = ''; | |
e16215c4 SJ |
188 | } |
189 | ||
23ccdf38 SJ |
190 | if (field_name == 'school') { |
191 | changeSchool(id, ''); | |
192 | } else { | |
193 | changeAddressComponents(field_name, id); | |
e16215c4 SJ |
194 | } |
195 | ||
23ccdf38 | 196 | $(".autocomplete_target[name='" + field_name + "']").attr('value', id); |
f6ce54a7 | 197 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); |
23ccdf38 | 198 | return; |
e16215c4 | 199 | } |
23ccdf38 SJ |
200 | |
201 | // change field in list and display text field as valid | |
202 | if (id < 0) { | |
203 | cancel_autocomplete(this.field, field_name); | |
204 | return; | |
205 | } | |
206 | ||
207 | $(".autocomplete_target[name='" + field_name + "']").attr('value', id); | |
208 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); | |
e16215c4 SJ |
209 | } |
210 | ||
211 | // }}} | |
212 | // {{{ Various search functions. | |
213 | ||
214 | function setAddress(i, j, values) | |
215 | { | |
216 | var prev_type = address_types[i]; | |
217 | var next_type = address_types[j]; | |
218 | var next_list = next_type + '_list'; | |
219 | ||
4aab5061 | 220 | $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:values[i] }, function() { |
e16215c4 | 221 | if ($("select[name='" + next_type + "']").children("option").size() > 1) { |
4aab5061 | 222 | $("tr#" + next_type).show(); |
e16215c4 | 223 | $("select[name='" + next_type + "']").attr('value', values[j]); |
e0088da2 SJ |
224 | if (next_type == 'locality') { |
225 | $('tr#locality_text').hide(); | |
226 | $("select[name='locality_text']").attr('value', ''); | |
227 | } | |
4aab5061 | 228 | if (j < address_types_count) { |
e16215c4 SJ |
229 | setAddress(j, j + 1, values); |
230 | } | |
231 | } else { | |
4aab5061 | 232 | $("tr#" + next_type).hide(); |
e16215c4 | 233 | $("select[name='" + next_type + "']").attr('value', ''); |
4aab5061 | 234 | if (j < address_types_count) { |
e16215c4 SJ |
235 | setAddress(i, j + 1, values); |
236 | } | |
237 | } | |
238 | }); | |
239 | } | |
240 | ||
241 | function displayNextAddressComponent(i, j, value) | |
242 | { | |
243 | var prev_type = address_types[i]; | |
244 | var next_type = address_types[j]; | |
245 | var next_list = next_type + '_list'; | |
246 | ||
4aab5061 | 247 | if (next_type == 'locality') { |
e16215c4 | 248 | $('tr#locality_text').hide(); |
82f06260 | 249 | $("select[name='locality_text']").attr('value', ''); |
e16215c4 SJ |
250 | } |
251 | ||
38f35d89 SJ |
252 | if (autocomplete_sub[prev_type] != null) { |
253 | $(".autocomplete[name='" + autocomplete_sub[prev_type] + "']").autocomplete('option', 'source', baseurl + 'autocomplete/' + autocomplete_sub[prev_type] + '/' + value); | |
254 | } | |
255 | ||
4aab5061 | 256 | $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:value }, function() { |
e16215c4 SJ |
257 | $("select[name='" + next_type + "']").attr('value', ''); |
258 | if ($("select[name='" + next_type + "']").children('option').size() > 1) { | |
4aab5061 | 259 | $('tr#' + next_type).show(); |
e16215c4 | 260 | } else { |
4aab5061 SJ |
261 | $('tr#' + next_type).hide(); |
262 | if (j < address_types_count) { | |
e16215c4 SJ |
263 | displayNextAddressComponent(i, j + 1, value); |
264 | } | |
265 | } | |
266 | }); | |
267 | } | |
268 | ||
269 | function changeAddressComponents(type, value) | |
270 | { | |
4aab5061 | 271 | var i = 0; |
e16215c4 | 272 | |
4aab5061 | 273 | while (address_types[i] != type && i < address_types_count) { |
e16215c4 SJ |
274 | ++i; |
275 | } | |
276 | ||
4aab5061 | 277 | for (var j = i + 1; j < address_types_count; ++j) { |
249510f5 | 278 | delete_address_component(address_types[j], true); |
e16215c4 SJ |
279 | } |
280 | ||
4aab5061 | 281 | if (value != '' && i < address_types_count) { |
e16215c4 SJ |
282 | $("select[name='" + type + "']").attr('value', value); |
283 | displayNextAddressComponent(i, i + 1, value); | |
284 | } | |
285 | } | |
286 | ||
249510f5 | 287 | function delete_address_component(field_name, remove) |
4aab5061 | 288 | { |
249510f5 SJ |
289 | if (remove || field_name == 'locality') { |
290 | $('tr#' + field_name).hide(); | |
291 | $('#' + field_name + '_list').html(''); | |
4aab5061 | 292 | |
249510f5 SJ |
293 | if (field_name == 'locality') { |
294 | $("input[name='locality_text']").val(''); | |
295 | $('tr#locality_text').show(); | |
296 | } | |
297 | } else { | |
298 | $("select[name='" + field_name + "']").val(''); | |
299 | $("input[name='" + field_name + "']").val(''); | |
4aab5061 SJ |
300 | } |
301 | } | |
302 | ||
e16215c4 SJ |
303 | // when changing school, open diploma choice |
304 | function changeSchool(schoolId, diploma) | |
305 | { | |
306 | $(".autocompleteTarget[name='school']").attr('value', schoolId); | |
307 | ||
308 | if (schoolId) { | |
82f06260 | 309 | $(".autocomplete[name='school_text']").addClass('hidden_valid'); |
e16215c4 | 310 | } else { |
82f06260 | 311 | $(".autocomplete[name='school_text']").removeClass('hidden_valid'); |
e16215c4 SJ |
312 | } |
313 | ||
314 | $("[name='diploma']").parent().load(baseurl + 'list/diploma/', { school:schoolId }, function() { | |
315 | $("select[name='diploma']").attr('value', diploma); | |
316 | }); | |
317 | } | |
318 | ||
e16215c4 SJ |
319 | // when choosing a job term in tree, hide tree and set job term field |
320 | function searchForJobTerm(treeid, jtid, full_name) | |
321 | { | |
322 | $(".term_tree").remove(); | |
82f06260 | 323 | $("input[name='jobterm_text']").val(full_name).addClass("hidden_valid").show(); |
e16215c4 SJ |
324 | $("input[name='jobterm']").val(jtid); |
325 | } | |
326 | ||
3ccb060d SJ |
327 | function addressesDump() |
328 | { | |
329 | if ($('#addresses_dump:checked').length > 0) { | |
330 | $('#recherche').attr('action', 'search/adv/addresses').attr('method', 'post').removeAttr('onsubmit'); | |
331 | } else { | |
332 | $('#recherche').attr('action', 'search/adv').attr('method', 'get'); | |
333 | } | |
334 | } | |
335 | ||
e16215c4 | 336 | // }}} |
448c8cdc | 337 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: |