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/'); | |
4aab5061 SJ |
24 | var address_types = new Array('country', 'administrative_area_level_1', 'administrative_area_level_2', 'locality', 'sublocality'); |
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']) { | |
44 | $("[name='country']").parent().load(baseurl + 'list/country', function() { | |
45 | $("select[name='country']").attr('value', request['country']); | |
46 | }); | |
47 | setAddress(0, 1, new Array(request['country'], | |
48 | request['administrative_area_level_1'], | |
49 | request['administrative_area_level_2'], | |
e16215c4 SJ |
50 | request['locality'], |
51 | request['sublocality']) | |
52 | ); | |
53 | } else { | |
4aab5061 SJ |
54 | for (var i = 1; i < address_types_count; ++i) { |
55 | $('tr#' + address_types[i]).hide(); | |
e16215c4 SJ |
56 | } |
57 | } | |
58 | ||
82f06260 | 59 | $(".autocomplete[name='school_text']").change(function() { changeSchool('', ''); }); |
e16215c4 SJ |
60 | changeSchool(request['school'], request['diploma']); |
61 | ||
82f06260 SJ |
62 | $(".autocomplete_to_select").each(function() { |
63 | var field_name = $(this).attr('href'); | |
e16215c4 | 64 | |
82f06260 SJ |
65 | if ($(".autocomplete_target[name='" + field_name + "']").val()) { |
66 | display_list(field_name); | |
67 | } | |
e16215c4 | 68 | |
82f06260 SJ |
69 | $(this).attr('href', baseurl + 'list/' + field_name).click(function() { |
70 | if ($(this).attr('title') == 'display') { | |
71 | display_list(field_name); | |
72 | } else { | |
73 | var value = $("select[name='" + field_name + "']").val(); | |
74 | var text_value = $("select[name='" + field_name + "'] option:selected").text(); | |
75 | $('#' + field_name + '_list').html(''); | |
76 | $(".autocomplete[name='" + field_name + "_text']").show(); | |
77 | $('#' + field_name + '_table').attr('title', 'display'); | |
78 | if (value) { | |
79 | $(".autocomplete_target[name='" + field_name + "']").val(value); | |
80 | $(".autocomplete[name='" + field_name + "_text']").val(text_value).addClass('hidden_valid'); | |
81 | } | |
82 | } | |
e16215c4 SJ |
83 | |
84 | return false; | |
85 | }); | |
e16215c4 SJ |
86 | }); |
87 | ||
88 | $('#only_referent').change(function() { changeOnlyReferent(); }); | |
4aab5061 SJ |
89 | |
90 | $('.delete_address_component').click(function() { | |
91 | var field_name = $(this).attr('href'); | |
92 | var hide = false; | |
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) { | |
99 | delete_address_component(address_types[i]); | |
100 | } | |
101 | } | |
102 | ||
103 | return false; | |
104 | }); | |
e16215c4 SJ |
105 | } |
106 | ||
82f06260 SJ |
107 | function display_list(field_name) |
108 | { | |
109 | var value = $("input.autocomplete_target[name='" + field_name + "']").val(); | |
110 | ||
111 | $('#' + field_name + '_list').load(baseurl + 'list/' + field_name, {}, function(selectBox) { | |
112 | $(".autocomplete_target[name='" + field_name + "']").val(''); | |
113 | $(".autocomplete[name='" + field_name + "_text']").hide().val('').removeClass('hidden_valid'); | |
114 | $("select[name='" + field_name + "']").val(value); | |
115 | $('#' + field_name + '_table').attr('title', 'hide'); | |
116 | }); | |
117 | } | |
118 | ||
e16215c4 SJ |
119 | // }}} |
120 | // {{{ Regexps to wipe out from search queries | |
121 | ||
122 | var default_form_values = [ /&woman=0(&|$)/, /&subscriber=0(&|$)/, /&alive=0(&|$)/, /&egal[12]=[^&]*&promo[12]=(&|$)/g, /&networking_type=0(&|$)/, /&[^&=]+=(&|$)/g ]; | |
123 | ||
124 | /** Uses javascript to clean form from all empty fields */ | |
125 | function cleanForm(f) | |
126 | { | |
127 | var query = $(f).formSerialize(); | |
128 | var old_query; | |
129 | for (var i in default_form_values) { | |
130 | var reg = default_form_values[i]; | |
131 | if (typeof(reg) != 'undefined') { | |
132 | do { | |
133 | old_query = query; | |
134 | query = query.replace(reg, '$1'); | |
135 | } while (old_query != query); | |
136 | } | |
137 | } | |
138 | query = query.replace(/^&*(.*)&*$/, '$1'); | |
139 | if (query == 'rechercher=Chercher') { | |
140 | alert("Aucun critère n'a été spécifié."); | |
141 | return false; | |
142 | } | |
143 | document.location = baseurl + 'adv?' + query; | |
144 | return false; | |
145 | } | |
146 | ||
147 | // }}} | |
148 | // {{{ Autocomplete related functions. | |
149 | ||
150 | // display an autocomplete row : blabla (nb of found matches) | |
151 | function make_format_autocomplete(block) | |
152 | { | |
153 | return function(row) { | |
154 | regexp = new RegExp('(' + RegExp.escape(block.value) + ')', 'i'); | |
155 | name = row[0].htmlEntities().replace(regexp, '<strong>$1<\/strong>'); | |
156 | ||
157 | if (row[1] === '-1') { | |
158 | return '…'; | |
159 | } | |
160 | if (row[1] === '-2') { | |
161 | return '<em>aucun camarade trouvé pour '+row[0].htmlEntities()+'<\/em>'; | |
162 | } | |
163 | ||
164 | mate = (row[1] > 1) ? 'camarades' : 'camarade'; | |
165 | return name + '<em> - ' + row[1].htmlEntities() + ' ' + mate + '<\/em>'; | |
166 | }; | |
167 | } | |
168 | ||
169 | function cancel_autocomplete(field, realfield) | |
170 | { | |
171 | $(".autocomplete[name='" + field + "']").removeClass('hidden_valid').val('').focus(); | |
172 | if (typeof(realfield) != 'undefined') { | |
82f06260 | 173 | $(".autocomplete_target[name='" + realfield + "']").val(''); |
e16215c4 SJ |
174 | } |
175 | return; | |
176 | } | |
177 | ||
178 | // when choosing autocomplete from list, must validate | |
179 | function select_autocomplete(name) | |
180 | { | |
5c89683f | 181 | var field_name = name.replace(/_text$/, ''); |
e16215c4 | 182 | |
f6ce54a7 | 183 | // just display field as valid if field is not a text field for a list |
5c89683f | 184 | if (field_name == name) { |
f6ce54a7 SJ |
185 | return function(i) { |
186 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); | |
187 | } | |
e16215c4 SJ |
188 | } |
189 | ||
4dea11cc SJ |
190 | // When changing country, locality or school, open next address component. |
191 | if (field_name == 'country' || field_name == 'locality' || field_name == 'school') { | |
e16215c4 | 192 | return function(i) { |
e16215c4 | 193 | if (i.extra[0] < 0) { |
5c89683f | 194 | cancel_autocomplete(name, field_name); |
e16215c4 SJ |
195 | i.extra[1] = ''; |
196 | } | |
e16215c4 | 197 | |
4dea11cc SJ |
198 | if (field_name == 'school') { |
199 | changeSchool(i.extra[1], ''); | |
200 | } else { | |
201 | changeAddressComponents(field_name, i.extra[1]); | |
e16215c4 | 202 | } |
4dea11cc SJ |
203 | |
204 | $(".autocomplete_target[name='" + field_name + "']").attr('value', i.extra[1]); | |
205 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); | |
e16215c4 | 206 | } |
4dea11cc | 207 | } |
e16215c4 SJ |
208 | |
209 | // change field in list and display text field as valid | |
210 | return function(i) { | |
e16215c4 | 211 | if (i.extra[0] < 0) { |
5c89683f | 212 | cancel_autocomplete(this.field, field_name); |
e16215c4 SJ |
213 | return; |
214 | } | |
215 | ||
5c89683f | 216 | $(".autocomplete_target[name='" + field_name + "']").attr('value', i.extra[1]); |
f6ce54a7 | 217 | $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); |
e16215c4 SJ |
218 | } |
219 | } | |
220 | ||
221 | // }}} | |
222 | // {{{ Various search functions. | |
223 | ||
224 | function setAddress(i, j, values) | |
225 | { | |
226 | var prev_type = address_types[i]; | |
227 | var next_type = address_types[j]; | |
228 | var next_list = next_type + '_list'; | |
229 | ||
230 | if (j == 3) { | |
231 | $('tr#locality_text').hide() | |
82f06260 | 232 | $("select[name='locality_text']").attr('value', ''); |
e16215c4 SJ |
233 | } |
234 | ||
4aab5061 | 235 | $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:values[i] }, function() { |
e16215c4 | 236 | if ($("select[name='" + next_type + "']").children("option").size() > 1) { |
4aab5061 | 237 | $("tr#" + next_type).show(); |
e16215c4 | 238 | $("select[name='" + next_type + "']").attr('value', values[j]); |
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 SJ |
284 | for (var j = i + 1; j < address_types_count; ++j) { |
285 | delete_address_component(address_types[j]); | |
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 | ||
4aab5061 SJ |
294 | function delete_address_component(field_name) |
295 | { | |
296 | $('tr#' + field_name).hide(); | |
297 | $('#' + field_name + '_list').html(''); | |
298 | $("input[name='" + field_name + "']").val(''); | |
299 | ||
300 | if (field_name == 'locality') { | |
301 | $("select[name='locality_text']").attr('value', ''); | |
302 | $('tr#locality_text').show(); | |
303 | } | |
304 | } | |
305 | ||
e16215c4 SJ |
306 | // when changing school, open diploma choice |
307 | function changeSchool(schoolId, diploma) | |
308 | { | |
309 | $(".autocompleteTarget[name='school']").attr('value', schoolId); | |
310 | ||
311 | if (schoolId) { | |
82f06260 | 312 | $(".autocomplete[name='school_text']").addClass('hidden_valid'); |
e16215c4 | 313 | } else { |
82f06260 | 314 | $(".autocomplete[name='school_text']").removeClass('hidden_valid'); |
e16215c4 SJ |
315 | } |
316 | ||
317 | $("[name='diploma']").parent().load(baseurl + 'list/diploma/', { school:schoolId }, function() { | |
318 | $("select[name='diploma']").attr('value', diploma); | |
319 | }); | |
320 | } | |
321 | ||
322 | // when checking/unchecking "only_referent", disable/enable some fields | |
323 | function changeOnlyReferent() | |
324 | { | |
325 | if ($("#only_referent").is(':checked')) { | |
326 | $("input[name='entreprise']").attr('disabled', true); | |
327 | } else { | |
328 | $("input[name='entreprise']").removeAttr('disabled'); | |
329 | } | |
330 | } | |
331 | ||
332 | // when choosing a job term in tree, hide tree and set job term field | |
333 | function searchForJobTerm(treeid, jtid, full_name) | |
334 | { | |
335 | $(".term_tree").remove(); | |
82f06260 | 336 | $("input[name='jobterm_text']").val(full_name).addClass("hidden_valid").show(); |
e16215c4 SJ |
337 | $("input[name='jobterm']").val(jtid); |
338 | } | |
339 | ||
340 | // }}} | |
341 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |