e54441004a6b76a9b3173bcf2955c4af99df4fee
[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', 'postal_code');
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['postal_code'])
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 $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:values[i] }, function() {
228 if ($("select[name='" + next_type + "']").children("option").size() > 1) {
229 $("tr#" + next_type).show();
230 $("select[name='" + next_type + "']").attr('value', values[j]);
231 if (next_type == 'locality') {
232 $('tr#locality_text').hide();
233 $("select[name='locality_text']").attr('value', '');
234 }
235 if (j < address_types_count) {
236 setAddress(j, j + 1, values);
237 }
238 } else {
239 $("tr#" + next_type).hide();
240 $("select[name='" + next_type + "']").attr('value', '');
241 if (j < address_types_count) {
242 setAddress(i, j + 1, values);
243 }
244 }
245 });
246 }
247
248 function displayNextAddressComponent(i, j, value)
249 {
250 var prev_type = address_types[i];
251 var next_type = address_types[j];
252 var next_list = next_type + '_list';
253
254 if (next_type == 'locality') {
255 $('tr#locality_text').hide();
256 $("select[name='locality_text']").attr('value', '');
257 }
258
259 $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:value }, function() {
260 $("select[name='" + next_type + "']").attr('value', '');
261 if ($("select[name='" + next_type + "']").children('option').size() > 1) {
262 $('tr#' + next_type).show();
263 } else {
264 $('tr#' + next_type).hide();
265 if (j < address_types_count) {
266 displayNextAddressComponent(i, j + 1, value);
267 }
268 }
269 });
270 }
271
272 function changeAddressComponents(type, value)
273 {
274 var i = 0;
275
276 while (address_types[i] != type && i < address_types_count) {
277 ++i;
278 }
279
280 for (var j = i + 1; j < address_types_count; ++j) {
281 delete_address_component(address_types[j]);
282 }
283
284 if (value != '' && i < address_types_count) {
285 $("select[name='" + type + "']").attr('value', value);
286 displayNextAddressComponent(i, i + 1, value);
287 }
288 }
289
290 function delete_address_component(field_name)
291 {
292 $('tr#' + field_name).hide();
293 $('#' + field_name + '_list').html('');
294 $("input[name='" + field_name + "']").val('');
295
296 if (field_name == 'locality') {
297 $("select[name='locality_text']").attr('value', '');
298 $('tr#locality_text').show();
299 }
300 }
301
302 // when changing school, open diploma choice
303 function changeSchool(schoolId, diploma)
304 {
305 $(".autocompleteTarget[name='school']").attr('value', schoolId);
306
307 if (schoolId) {
308 $(".autocomplete[name='school_text']").addClass('hidden_valid');
309 } else {
310 $(".autocomplete[name='school_text']").removeClass('hidden_valid');
311 }
312
313 $("[name='diploma']").parent().load(baseurl + 'list/diploma/', { school:schoolId }, function() {
314 $("select[name='diploma']").attr('value', diploma);
315 });
316 }
317
318 // when checking/unchecking "only_referent", disable/enable some fields
319 function changeOnlyReferent()
320 {
321 if ($("#only_referent").is(':checked')) {
322 $("input[name='entreprise']").attr('disabled', true);
323 } else {
324 $("input[name='entreprise']").removeAttr('disabled');
325 }
326 }
327
328 // when choosing a job term in tree, hide tree and set job term field
329 function searchForJobTerm(treeid, jtid, full_name)
330 {
331 $(".term_tree").remove();
332 $("input[name='jobterm_text']").val(full_name).addClass("hidden_valid").show();
333 $("input[name='jobterm']").val(jtid);
334 }
335
336 // }}}
337 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: