Fix login on X.net with only 'groups' perm.
[platal.git] / htdocs / javascript / search.js
index 798c97e..94dc9ab 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
 var baseurl = $.plURL('search/');
 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);
+                }
+            }
         });
     });
 
@@ -82,8 +86,6 @@ function load_advanced_search(request)
         });
     });
 
-    $('#only_referent').change(function() { changeOnlyReferent(); });
-
     $('.delete_address_component').click(function() {
         var field_name = $(this).attr('href');
         var hide = false;
@@ -120,7 +122,10 @@ function display_list(field_name)
 // }}}
 // {{{ Regexps to wipe out from search queries
 
-var default_form_values = [ /&woman=0(&|$)/, /&subscriber=0(&|$)/, /&alive=0(&|$)/, /&egal[12]=[^&]*&promo[12]=(&|$)/g, /&networking_type=0(&|$)/, /&[^&=]+=(&|$)/g ];
+var default_form_values = [ /&woman=0(&|$)/, /&subscriber=0(&|$)/, /&alive=0(&|$)/, /&egal2=[^&]*&promo2=(&|$)/,
+                            /&egal1=[^&]*&promo1=&edu_type=(?:Ing[^n]+nieur|Master|Doctorat)(&|$)/, /&networking_type=0(&|$)/,
+                            /&origin_corps=0(&|$)/, /&current_corps=0(&|$)/,
+                            /corps_rank=0(&|$)/, /&has_email_redirect=0(&|$)/, /&[^&=]+=(&|$)/g ];
 
 /** Uses javascript to clean form from all empty fields */
 function cleanForm(f, targeturl)
@@ -136,37 +141,22 @@ function cleanForm(f, targeturl)
             } while (old_query != query);
         }
     }
-    query = query.replace(/^&*(.*)&*$/, '$1');
+    query = query.replace(/^(.*)&+$/, '$1');
+    query = query.replace(/^&+(.*)$/, '$1');
+
+    // Removes "(n camarades)" if any of them are remaining.
+    query = query.replace(/\+\(\d+\+camarade(?:s)?\)/, '');
     if (query == 'rechercher=Chercher') {
         alert("Aucun critère n'a été spécifié.");
         return false;
     }
-    document.location = targeturl + '?' + 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, '<strong>$1<\/strong>');
-
-        if (row[1] === '-1') {
-            return '&hellip;';
-        }
-        if (row[1] === '-2') {
-            return '<em>aucun camarade trouvé pour '+row[0].htmlEntities()+'<\/em>';
-        }
-
-        mate = (row[1] > 1) ? 'camarades' : 'camarade';
-        return name + '<em>&nbsp;&nbsp;-&nbsp;&nbsp;' + row[1].htmlEntities() + '&nbsp;' + mate + '<\/em>';
-    };
-}
-
 function cancel_autocomplete(field, realfield)
 {
     $(".autocomplete[name='" + field + "']").removeClass('hidden_valid').val('').focus();
@@ -177,46 +167,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);
+    }
 
     // just display field as valid if field is not a text field for a list
     if (field_name == name) {
-        return function(i) {
-            $(".autocomplete[name='" + name + "']").addClass('hidden_valid');
-        }
+        $(".autocomplete[name='" + name + "']").addClass('hidden_valid');
+        return;
     }
 
     // When changing country, locality or school, open next address component.
     if (field_name == 'country' || field_name == 'locality' || field_name == 'school') {
-        return function(i) {
-            if (i.extra[0] < 0) {
-                cancel_autocomplete(name, field_name);
-                i.extra[1] = '';
-            }
-
-            if (field_name == 'school') {
-                changeSchool(i.extra[1], '');
-            } else {
-                changeAddressComponents(field_name, i.extra[1]);
-            }
-
-            $(".autocomplete_target[name='" + field_name + "']").attr('value', i.extra[1]);
-            $(".autocomplete[name='" + name + "']").addClass('hidden_valid');
+        if (id < 0) {
+            cancel_autocomplete(name, 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;
+        if (field_name == 'school') {
+            changeSchool(id, '');
+        } else {
+            changeAddressComponents(field_name, id);
         }
 
-        $(".autocomplete_target[name='" + field_name + "']").attr('value', i.extra[1]);
+        $(".autocomplete_target[name='" + field_name + "']").attr('value', id);
         $(".autocomplete[name='" + name + "']").addClass('hidden_valid');
+        return;
+    }
+
+    // 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');
 }
 
 // }}}
@@ -260,6 +249,10 @@ function displayNextAddressComponent(i, j, value)
         $("select[name='locality_text']").attr('value', '');
     }
 
+    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) {
@@ -331,5 +324,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:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8: