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