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