1 /***************************************************************************
2 * Copyright (C) 2003-2011 Polytechnique.org *
3 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
18 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
19 ***************************************************************************/
21 // {{{ Page initialization
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');
26 function load_advanced_search(request
)
28 $('.autocompleteTarget').hide();
29 $('.autocomplete').show().each(function() {
30 targeted
= $('../.autocompleteTarget', this)[0];
32 if (targeted
&& targeted
.value
) {
35 $.get(baseurl
+ 'list/' + targeted.name + '/' + targeted
.value
, {}, function(textValue
) {
36 me
.attr('value', textValue
);
37 me
.addClass('hidden_valid');
41 $(this).autocomplete(baseurl
+ 'autocomplete/' + this.name
, {
43 formatItem
: make_format_autocomplete(this),
45 onItemSelect
: select_autocomplete(this.name
),
47 width
: $(this).width()}
51 $('.autocomplete').change(function() { $(this).removeClass('hidden_valid'); });
53 if (request
['country']) {
54 $("[name='country']").parent().load(baseurl
+ 'list/country', function() {
55 $("select[name='country']").attr('value', request
['country']);
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'],
62 request
['sublocality'])
65 for (var i
= 1; i
< 6; ++i
) {
66 $('tr#' + address_types
[i
] + '_list').hide();
70 $(".autocomplete[name='schoolTxt']").change(function() { changeSchool('', ''); });
71 changeSchool(request
['school'], request
['diploma']);
73 $(".autocompleteToSelect").each(function() {
74 var fieldName
= $(this).attr('href');
76 $(this).attr('href', baseurl
+ 'list/' + fieldName
).click(function() {
77 var oldval
= $("input.autocompleteTarget[name='" + fieldName
+ "']")[0].value
;
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
);
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('');
93 $('#only_referent').change(function() { changeOnlyReferent(); });
97 // {{{ Regexps to wipe out from search queries
99 var default_form_values
= [ /&woman=0(&|$)/, /&subscriber=0(&|$)/, /&alive=0(&|$)/, /&egal[12]=[^&]*&promo[12]=(&|$)/g, /&networking_type=0(&|$)/, /&[^&=]+=(&|$)/g ];
101 /** Uses javascript to clean form from all empty fields */
102 function cleanForm(f
)
104 var query
= $(f
).formSerialize();
106 for (var i
in default_form_values
) {
107 var reg
= default_form_values
[i
];
108 if (typeof(reg
) != 'undefined') {
111 query
= query
.replace(reg
, '$1');
112 } while (old_query
!= query
);
115 query
= query
.replace(/^&*(.*)&*$/, '$1');
116 if (query
== 'rechercher=Chercher') {
117 alert("Aucun critère n'a été spécifié.");
120 document
.location
= baseurl
+ 'adv?' + query
;
125 // {{{ Autocomplete related functions.
127 // display an autocomplete row : blabla (nb of found matches)
128 function make_format_autocomplete(block
)
130 return function(row
) {
131 regexp
= new RegExp('(' + RegExp
.escape(block
.value
) + ')', 'i');
132 name
= row
[0].htmlEntities().replace(regexp
, '<strong>$1<\/strong>');
134 if (row
[1] === '-1') {
137 if (row
[1] === '-2') {
138 return '<em>aucun camarade trouvé pour '+row
[0].htmlEntities()+'<\/em>';
141 mate
= (row
[1] > 1) ? 'camarades' : 'camarade';
142 return name
+ '<em> - ' + row
[1].htmlEntities() + ' ' + mate
+ '<\/em>';
146 function cancel_autocomplete(field
, realfield
)
148 $(".autocomplete[name='" + field
+ "']").removeClass('hidden_valid').val('').focus();
149 if (typeof(realfield
) != 'undefined') {
150 $(".autocompleteTarget[name='" + realfield
+ "']").val('');
155 // when choosing autocomplete from list, must validate
156 function select_autocomplete(name
)
158 nameRealField
= name
.replace(/Txt$/, '');
160 // nothing to do if field is not a text field for a list
161 if (nameRealField
== name
) {
165 // When changing country or locality, open next address component.
166 if (nameRealField
== 'country' || nameRealField
== 'locality') {
168 nameRealField
= name
.replace(/Txt$/, '');
169 if (i
.extra
[0] < 0) {
170 cancel_autocomplete(name
, nameRealField
);
173 $("[name='" + nameRealField
+ "']").parent().load(baseurl
+ 'list/' + nameRealField
, function() {
174 $("select[name='" + nameRealField
+ "']").attr('value', i
.extra
[1]);
176 changeAddressComponents(nameRealField
, i
.extra
[1]);
180 if (nameRealField
== 'school')
182 if (i
.extra
[0] < 0) {
183 cancel_autocomplete('schoolTxt', 'school');
186 changeSchool(i
.extra
[1], '');
189 // change field in list and display text field as valid
191 nameRealField
= this.field
.replace(/Txt$/, '');
192 if (i
.extra
[0] < 0) {
193 cancel_autocomplete(this.field
, nameRealField
);
197 $(".autocompleteTarget[name='"+nameRealField
+"']").attr('value',i
.extra
[1]);
198 $(".autocomplete[name='"+this.field
+"']").addClass('hidden_valid');
203 // {{{ Various search functions.
205 function setAddress(i
, j
, values
)
207 var prev_type
= address_types
[i
];
208 var next_type
= address_types
[j
];
209 var next_list
= next_type
+ '_list';
212 $('tr#locality_text').hide()
213 $("select[name='localityTxt']").attr('value', '');
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
]);
221 setAddress(j
, j
+ 1, values
);
224 $("tr#" + next_list
).hide();
225 $("select[name='" + next_type
+ "']").attr('value', '');
227 setAddress(i
, j
+ 1, values
);
233 function displayNextAddressComponent(i
, j
, value
)
235 var prev_type
= address_types
[i
];
236 var next_type
= address_types
[j
];
237 var next_list
= next_type
+ '_list';
240 $('tr#locality_text').hide();
241 $("select[name='localityTxt']").attr('value', '');
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();
249 $('tr#' + next_list
).hide();
251 displayNextAddressComponent(i
, j
+ 1, value
);
257 function changeAddressComponents(type
, value
)
261 while (address_types
[i
] != type
&& i
< 6) {
267 $("select[name='" + address_types
[j
] + "']").attr('value', '');
268 $('tr#' + address_types
[j
] + '_list').hide();
272 if (value
!= '' && i
< 5) {
273 $("select[name='" + type
+ "']").attr('value', value
);
274 displayNextAddressComponent(i
, i
+ 1, value
);
278 // when changing school, open diploma choice
279 function changeSchool(schoolId
, diploma
)
281 $(".autocompleteTarget[name='school']").attr('value', schoolId
);
284 $(".autocomplete[name='schoolTxt']").addClass('hidden_valid');
286 $(".autocomplete[name='schoolTxt']").removeClass('hidden_valid');
289 $("[name='diploma']").parent().load(baseurl
+ 'list/diploma/', { school
:schoolId
}, function() {
290 $("select[name='diploma']").attr('value', diploma
);
294 // when checking/unchecking "only_referent", disable
/enable some fields
295 function changeOnlyReferent()
297 if ($("#only_referent").is(':checked')) {
298 $("input[name='entreprise']").attr('disabled', true);
300 $("input[name='entreprise']").removeAttr('disabled');
304 // when choosing a job term in tree, hide tree and set job term field
305 function searchForJobTerm(treeid
, jtid
, full_name
)
307 $(".term_tree").remove();
308 $("input[name='jobtermTxt']").val(full_name
).addClass("hidden_valid").show();
309 $("input[name='jobterm']").val(jtid
);
313 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: