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