X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=htdocs%2Fjavascript%2Fsearch.js;h=52870adc0030221b427bbb40e34d07b0fc58cc93;hb=e93255ef54304c697ef7ac101247f23dc5f7b31f;hp=f2184515ee27c20610e886bd20a429a83e804d14;hpb=5c89683f4a71a2c5f3c43b695aebb925341d6e9d;p=platal.git diff --git a/htdocs/javascript/search.js b/htdocs/javascript/search.js index f218451..52870ad 100644 --- a/htdocs/javascript/search.js +++ b/htdocs/javascript/search.js @@ -21,38 +21,39 @@ // {{{ Page initialization var baseurl = $.plURL('search/'); -var address_types = new Array('country', 'administrative_area_level_1', 'administrative_area_level_2', 'administrative_area_level_3', 'locality', 'sublocality'); +var address_types = new Array('country', 'administrative_area_level_1', 'administrative_area_level_2', 'locality', 'postal_code'); +var address_types_count = address_types.length; +var autocomplete_sub = {'country': 'locality_text'}; function load_advanced_search(request) { $('.autocomplete_target').hide(); $('.autocomplete').show().each(function() { - $(this).autocomplete(baseurl + 'autocomplete/' + this.name, { - selectOnly: 1, - formatItem: make_format_autocomplete(this), - field: this.name, - onItemSelect: select_autocomplete(this.name), - matchSubset: 0, - width: $(this).width() + $(this).autocomplete({ + source: baseurl + 'autocomplete/' + this.name, + select: function(event, ui) { + select_autocomplete(this.name, ui.item.id); + }, + change: function(event, ui) { + if (ui.item != null && ui.item.field != null) { + $(this).val(ui.item.field); + } + } }); }); $('.autocomplete').change(function() { $(this).removeClass('hidden_valid'); }); if (request['country']) { - $("[name='country']").parent().load(baseurl + 'list/country', function() { - $("select[name='country']").attr('value', request['country']); - }); setAddress(0, 1, new Array(request['country'], request['administrative_area_level_1'], request['administrative_area_level_2'], - request['administrative_area_level_3'], request['locality'], - request['sublocality']) + request['postal_code']) ); } else { - for (var i = 1; i < 6; ++i) { - $('tr#' + address_types[i] + '_list').hide(); + for (var i = 1; i < address_types_count; ++i) { + $('tr#' + address_types[i]).hide(); } } @@ -86,6 +87,26 @@ function load_advanced_search(request) }); $('#only_referent').change(function() { changeOnlyReferent(); }); + + $('.delete_address_component').click(function() { + var field_name = $(this).attr('href'); + var hide = false; + var remove = false; + + for (var i = 1; i < address_types_count; ++i) { + if (field_name == address_types[i]) { + hide = true; + } + if (hide) { + if (field_name != address_types[i]) { + remove = true; + } + delete_address_component(address_types[i], remove); + } + } + + return false; + }); } function display_list(field_name) @@ -106,7 +127,7 @@ function display_list(field_name) var default_form_values = [ /&woman=0(&|$)/, /&subscriber=0(&|$)/, /&alive=0(&|$)/, /&egal[12]=[^&]*&promo[12]=(&|$)/g, /&networking_type=0(&|$)/, /&[^&=]+=(&|$)/g ]; /** Uses javascript to clean form from all empty fields */ -function cleanForm(f) +function cleanForm(f, targeturl) { var query = $(f).formSerialize(); var old_query; @@ -124,32 +145,13 @@ function cleanForm(f) alert("Aucun critère n'a été spécifié."); return false; } - document.location = baseurl + 'adv?' + query; + document.location = $.plURL(targeturl + '?' + query); return false; } // }}} // {{{ Autocomplete related functions. -// display an autocomplete row : blabla (nb of found matches) -function make_format_autocomplete(block) -{ - return function(row) { - regexp = new RegExp('(' + RegExp.escape(block.value) + ')', 'i'); - name = row[0].htmlEntities().replace(regexp, '$1<\/strong>'); - - if (row[1] === '-1') { - return '…'; - } - if (row[1] === '-2') { - return 'aucun camarade trouvé pour '+row[0].htmlEntities()+'<\/em>'; - } - - mate = (row[1] > 1) ? 'camarades' : 'camarade'; - return name + '  -  ' + row[1].htmlEntities() + ' ' + mate + '<\/em>'; - }; -} - function cancel_autocomplete(field, realfield) { $(".autocomplete[name='" + field + "']").removeClass('hidden_valid').val('').focus(); @@ -160,48 +162,45 @@ function cancel_autocomplete(field, realfield) } // when choosing autocomplete from list, must validate -function select_autocomplete(name) +function select_autocomplete(name, id) { var field_name = name.replace(/_text$/, ''); + if (autocomplete_sub[field_name] != null) { + $(".autocomplete[name='" + autocomplete_sub[field_name] + "']").autocomplete('option', 'source', baseurl + 'autocomplete/' + autocomplete_sub[field_name] + '/' + id); + } - // nothing to do if field is not a text field for a list + // just display field as valid if field is not a text field for a list if (field_name == name) { - return null; + $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); + return; } - // When changing country or locality, open next address component. - if (field_name == 'country' || field_name == 'locality') { - return function(i) { - if (i.extra[0] < 0) { - cancel_autocomplete(name, field_name); - i.extra[1] = ''; - } - $("[name='" + field_name + "']").parent().load(baseurl + 'list/' + field_name, function() { - $("select[name='" + field_name + "']").attr('value', i.extra[1]); - }); - changeAddressComponents(field_name, i.extra[1]); + // When changing country, locality or school, open next address component. + if (field_name == 'country' || field_name == 'locality' || field_name == 'school') { + if (id < 0) { + cancel_autocomplete(name, field_name); + id = ''; } - } - if (field_name == 'school') - return function(i) { - if (i.extra[0] < 0) { - cancel_autocomplete('school_text', 'school'); - i.extra[1] = ''; - } - changeSchool(i.extra[1], ''); + if (field_name == 'school') { + changeSchool(id, ''); + } else { + changeAddressComponents(field_name, id); } - // change field in list and display text field as valid - return function(i) { - if (i.extra[0] < 0) { - cancel_autocomplete(this.field, field_name); - return; - } + $(".autocomplete_target[name='" + field_name + "']").attr('value', id); + $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); + return; + } - $(".autocomplete_target[name='" + field_name + "']").attr('value', i.extra[1]); - $(".autocomplete[name='" + this.field + "']").addClass('hidden_valid'); + // change field in list and display text field as valid + if (id < 0) { + cancel_autocomplete(this.field, field_name); + return; } + + $(".autocomplete_target[name='" + field_name + "']").attr('value', id); + $(".autocomplete[name='" + name + "']").addClass('hidden_valid'); } // }}} @@ -213,22 +212,21 @@ function setAddress(i, j, values) var next_type = address_types[j]; var next_list = next_type + '_list'; - if (j == 3) { - $('tr#locality_text').hide() - $("select[name='locality_text']").attr('value', ''); - } - - $("[name='" + next_type + "']").parent().load(baseurl + 'list/' + next_type, { previous:prev_type, value:values[i] }, function() { + $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:values[i] }, function() { if ($("select[name='" + next_type + "']").children("option").size() > 1) { - $("tr#" + next_list).show(); + $("tr#" + next_type).show(); $("select[name='" + next_type + "']").attr('value', values[j]); - if (j < 6) { + if (next_type == 'locality') { + $('tr#locality_text').hide(); + $("select[name='locality_text']").attr('value', ''); + } + if (j < address_types_count) { setAddress(j, j + 1, values); } } else { - $("tr#" + next_list).hide(); + $("tr#" + next_type).hide(); $("select[name='" + next_type + "']").attr('value', ''); - if (j < 6) { + if (j < address_types_count) { setAddress(i, j + 1, values); } } @@ -241,18 +239,22 @@ function displayNextAddressComponent(i, j, value) var next_type = address_types[j]; var next_list = next_type + '_list'; - if (j == 3) { + if (next_type == 'locality') { $('tr#locality_text').hide(); $("select[name='locality_text']").attr('value', ''); } - $("[name='" + next_type + "']").parent().load(baseurl + 'list/' + next_type, { previous:prev_type, value:value }, function() { + if (autocomplete_sub[prev_type] != null) { + $(".autocomplete[name='" + autocomplete_sub[prev_type] + "']").autocomplete('option', 'source', baseurl + 'autocomplete/' + autocomplete_sub[prev_type] + '/' + value); + } + + $('#' + next_list).load(baseurl + 'list/' + next_type, { previous:prev_type, value:value }, function() { $("select[name='" + next_type + "']").attr('value', ''); if ($("select[name='" + next_type + "']").children('option').size() > 1) { - $('tr#' + next_list).show(); + $('tr#' + next_type).show(); } else { - $('tr#' + next_list).hide(); - if (j < 6) { + $('tr#' + next_type).hide(); + if (j < address_types_count) { displayNextAddressComponent(i, j + 1, value); } } @@ -261,25 +263,38 @@ function displayNextAddressComponent(i, j, value) function changeAddressComponents(type, value) { - var i = 0, j = 0; + var i = 0; - while (address_types[i] != type && i < 6) { + while (address_types[i] != type && i < address_types_count) { ++i; } - j = i + 1; - while (j < 6) { - $("select[name='" + address_types[j] + "']").attr('value', ''); - $('tr#' + address_types[j] + '_list').hide(); - ++j; + for (var j = i + 1; j < address_types_count; ++j) { + delete_address_component(address_types[j], true); } - if (value != '' && i < 5) { + if (value != '' && i < address_types_count) { $("select[name='" + type + "']").attr('value', value); displayNextAddressComponent(i, i + 1, value); } } +function delete_address_component(field_name, remove) +{ + if (remove || field_name == 'locality') { + $('tr#' + field_name).hide(); + $('#' + field_name + '_list').html(''); + + if (field_name == 'locality') { + $("input[name='locality_text']").val(''); + $('tr#locality_text').show(); + } + } else { + $("select[name='" + field_name + "']").val(''); + $("input[name='" + field_name + "']").val(''); + } +} + // when changing school, open diploma choice function changeSchool(schoolId, diploma) { @@ -296,16 +311,6 @@ function changeSchool(schoolId, diploma) }); } -// when checking/unchecking "only_referent", disable/enable some fields -function changeOnlyReferent() -{ - if ($("#only_referent").is(':checked')) { - $("input[name='entreprise']").attr('disabled', true); - } else { - $("input[name='entreprise']").removeAttr('disabled'); - } -} - // when choosing a job term in tree, hide tree and set job term field function searchForJobTerm(treeid, jtid, full_name) { @@ -314,5 +319,14 @@ function searchForJobTerm(treeid, jtid, full_name) $("input[name='jobterm']").val(jtid); } +function addressesDump() +{ + if ($('#addresses_dump:checked').length > 0) { + $('#recherche').attr('action', 'search/adv/addresses').attr('method', 'post').removeAttr('onsubmit'); + } else { + $('#recherche').attr('action', 'search/adv').attr('method', 'get'); + } +} + // }}} // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: