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