Moves advanced search JS into a seperate file.
authorStéphane Jacob <sj@m4x.org>
Thu, 9 Jun 2011 07:44:21 +0000 (09:44 +0200)
committerStéphane Jacob <sj@m4x.org>
Thu, 9 Jun 2011 09:12:25 +0000 (11:12 +0200)
Signed-off-by: Stéphane Jacob <sj@m4x.org>
htdocs/javascript/search.js [new file with mode: 0644]
modules/search.php
templates/search/adv.form.tpl

diff --git a/htdocs/javascript/search.js b/htdocs/javascript/search.js
new file mode 100644 (file)
index 0000000..8078a93
--- /dev/null
@@ -0,0 +1,313 @@
+/***************************************************************************
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  http://opensource.polytechnique.org/                                   *
+ *                                                                         *
+ *  This program is free software; you can redistribute it and/or modify   *
+ *  it under the terms of the GNU General Public License as published by   *
+ *  the Free Software Foundation; either version 2 of the License, or      *
+ *  (at your option) any later version.                                    *
+ *                                                                         *
+ *  This program is distributed in the hope that it will be useful,        *
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of         *
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
+ *  GNU General Public License for more details.                           *
+ *                                                                         *
+ *  You should have received a copy of the GNU General Public License      *
+ *  along with this program; if not, write to the Free Software            *
+ *  Foundation, Inc.,                                                      *
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
+ ***************************************************************************/
+
+// {{{ 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');
+
+function load_advanced_search(request)
+{
+    $('.autocompleteTarget').hide();
+    $('.autocomplete').show().each(function() {
+        targeted = $('../.autocompleteTarget', this)[0];
+
+        if (targeted && targeted.value) {
+            me = $(this);
+
+            $.get(baseurl + 'list/' + targeted.name + '/' + targeted.value, {}, function(textValue) {
+                me.attr('value', textValue);
+                me.addClass('hidden_valid');
+            });
+        }
+
+        $(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()}
+        );
+    });
+
+    $('.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'])
+        );
+    } else {
+        for (var i = 1; i < 6; ++i) {
+            $('tr#' + address_types[i] + '_list').hide();
+        }
+    }
+
+    $(".autocomplete[name='schoolTxt']").change(function() { changeSchool('', ''); });
+    changeSchool(request['school'], request['diploma']);
+
+    $(".autocompleteToSelect").each(function() {
+        var fieldName = $(this).attr('href');
+
+        $(this).attr('href', baseurl + 'list/' + fieldName).click(function() {
+            var oldval = $("input.autocompleteTarget[name='" + fieldName + "']")[0].value;
+
+            $(".autocompleteTarget[name='" + fieldName + "']").parent().load(baseurl + 'list/' + fieldName, {}, function(selectBox) {
+                $(".autocompleteTarget[name='" + fieldName + "']").remove();
+                $(".autocomplete[name='" + fieldName + "Txt']").remove();
+                $("select[name='" + fieldName + "']").attr('value', oldval);
+            });
+
+            return false;
+        });
+    }).parent().find('.autocomplete').change(function() {
+        // If we change the value in the type="text" field, then the value in the 'integer id' field must not be used,
+        // to ensure that, we unset it
+        $(this).parent().find('.autocompleteTarget').val('');
+    });
+
+    $('#only_referent').change(function() { changeOnlyReferent(); });
+}
+
+// }}}
+// {{{ 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 ];
+
+/** Uses javascript to clean form from all empty fields */
+function cleanForm(f)
+{
+    var query = $(f).formSerialize();
+    var old_query;
+    for (var i in default_form_values) {
+        var reg = default_form_values[i];
+        if (typeof(reg) != 'undefined') {
+            do {
+                old_query = query;
+                query = query.replace(reg, '$1');
+            } while (old_query != query);
+        }
+    }
+    query = query.replace(/^&*(.*)&*$/, '$1');
+    if (query == 'rechercher=Chercher') {
+        alert("Aucun critère n'a été spécifié.");
+        return false;
+    }
+    document.location = baseurl + 'adv?' + 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();
+    if (typeof(realfield) != 'undefined') {
+        $(".autocompleteTarget[name='" + realfield + "']").val('');
+    }
+    return;
+}
+
+// when choosing autocomplete from list, must validate
+function select_autocomplete(name)
+{
+    nameRealField = name.replace(/Txt$/, '');
+
+    // nothing to do if field is not a text field for a list
+    if (nameRealField == name) {
+        return null;
+    }
+
+    // When changing country or locality, open next address component.
+    if (nameRealField == 'country' || nameRealField == 'locality') {
+        return function(i) {
+            nameRealField = name.replace(/Txt$/, '');
+            if (i.extra[0] < 0) {
+                cancel_autocomplete(name, nameRealField);
+                i.extra[1] = '';
+            }
+            $("[name='" + nameRealField + "']").parent().load(baseurl + 'list/' + nameRealField, function() {
+                $("select[name='" + nameRealField + "']").attr('value', i.extra[1]);
+            });
+            changeAddressComponents(nameRealField, i.extra[1]);
+        }
+    }
+
+    if (nameRealField == 'school')
+        return function(i) {
+            if (i.extra[0] < 0) {
+                cancel_autocomplete('schoolTxt', 'school');
+                i.extra[1] = '';
+            }
+            changeSchool(i.extra[1], '');
+        }
+
+    // change field in list and display text field as valid
+    return function(i) {
+        nameRealField = this.field.replace(/Txt$/, '');
+        if (i.extra[0] < 0) {
+            cancel_autocomplete(this.field, nameRealField);
+            return;
+        }
+
+        $(".autocompleteTarget[name='"+nameRealField+"']").attr('value',i.extra[1]);
+        $(".autocomplete[name='"+this.field+"']").addClass('hidden_valid');
+    }
+}
+
+// }}}
+// {{{ Various search functions.
+
+function setAddress(i, j, values)
+{
+    var prev_type = address_types[i];
+    var next_type = address_types[j];
+    var next_list = next_type + '_list';
+
+    if (j == 3) {
+        $('tr#locality_text').hide()
+            $("select[name='localityTxt']").attr('value', '');
+    }
+
+    $("[name='" + next_type + "']").parent().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();
+            $("select[name='" + next_type + "']").attr('value', values[j]);
+            if (j < 6) {
+                setAddress(j, j + 1, values);
+            }
+        } else {
+            $("tr#" + next_list).hide();
+            $("select[name='" + next_type + "']").attr('value', '');
+            if (j < 6) {
+                setAddress(i, j + 1, values);
+            }
+        }
+    });
+}
+
+function displayNextAddressComponent(i, j, value)
+{
+    var prev_type = address_types[i];
+    var next_type = address_types[j];
+    var next_list = next_type + '_list';
+
+    if (j == 3) {
+        $('tr#locality_text').hide();
+        $("select[name='localityTxt']").attr('value', '');
+    }
+
+    $("[name='" + next_type + "']").parent().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();
+        } else {
+            $('tr#' + next_list).hide();
+            if (j < 6) {
+                displayNextAddressComponent(i, j + 1, value);
+            }
+        }
+    });
+}
+
+function changeAddressComponents(type, value)
+{
+    var i = 0, j = 0;
+
+    while (address_types[i] != type && i < 6) {
+        ++i;
+    }
+
+    j = i + 1;
+    while (j < 6) {
+        $("select[name='" + address_types[j] + "']").attr('value', '');
+        $('tr#' + address_types[j] + '_list').hide();
+        ++j;
+    }
+
+    if (value != '' && i < 5) {
+        $("select[name='" + type + "']").attr('value', value);
+        displayNextAddressComponent(i, i + 1, value);
+    }
+}
+
+// when changing school, open diploma choice
+function changeSchool(schoolId, diploma)
+{
+    $(".autocompleteTarget[name='school']").attr('value', schoolId);
+
+    if (schoolId) {
+        $(".autocomplete[name='schoolTxt']").addClass('hidden_valid');
+    } else {
+        $(".autocomplete[name='schoolTxt']").removeClass('hidden_valid');
+    }
+
+    $("[name='diploma']").parent().load(baseurl + 'list/diploma/', { school:schoolId }, function() {
+        $("select[name='diploma']").attr('value', 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)
+{
+    $(".term_tree").remove();
+    $("input[name='jobtermTxt']").val(full_name).addClass("hidden_valid").show();
+    $("input[name='jobterm']").val(jtid);
+}
+
+// }}}
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
index 7c33bd5..362e582 100644 (file)
@@ -149,6 +149,7 @@ class SearchModule extends PLModule
     function handler_advanced($page, $model = null, $byletter = null)
     {
         global $globals;
+        $page->addJsLink('search.js');
         $page->assign('advanced',1);
 
         $networks = DirEnum::getOptions(DirEnum::NETWORKS);
index c5c185b..fda5ad4 100644 (file)
 <p class="center"><strong>Voulez-vous télécharger le <a href="{$globals->baseurl}/search/adv/addresses{$plset_args}">tableau des adresses postales</a> pour la recette précédente&nbsp;?</strong></p>
 {/if}
 
-<script type="text/javascript">// <!--
-  var baseurl = $.plURL("search/");
-  {literal}
+<script type="text/javascript">//<![CDATA[
+  {literal}$(function() { load_advanced_search({{/literal}{foreach from=$smarty.request key=key item=item}"{$key}":"{$item}",{/foreach}{literal}}); });{/literal}
+//]]></script>
 
-  // 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>';
-        }
-
-        camarades = (row[1] > 1) ? "camarades" : "camarade";
-
-        return name + '<em>&nbsp;&nbsp;-&nbsp;&nbsp;' + row[1].htmlEntities() + '&nbsp;' + camarades + '<\/em>';
-      };
-  }
-
-  function setAddress(i, j, values)
-  {
-    var types = new Array('country', 'administrative_area_level_1', 'administrative_area_level_2', 'administrative_area_level_3', 'locality', 'sublocality');
-    var prev_type = types[i];
-    var next_type = types[j];
-    var next_list = next_type + '_list';
-
-    if (j == 3) {
-      $('tr#locality_text').hide()
-      $("select[name='localityTxt']").attr('value', '');
-    }
-
-    $("[name='" + next_type + "']").parent().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();
-        $("select[name='" + next_type + "']").attr('value', values[j]);
-        if (j < 6) {
-          setAddress(j, j + 1, values);
-        }
-      } else {
-        $("tr#" + next_list).hide();
-        $("select[name='" + next_type + "']").attr('value', '');
-        if (j < 6) {
-          setAddress(i, j + 1, values);
-        }
-      }
-    });
-
-  }
-
-  function displayNextAddressComponent(i, j, value)
-  {
-    var types = new Array('country', 'administrative_area_level_1', 'administrative_area_level_2', 'administrative_area_level_3', 'locality', 'sublocality');
-    var prev_type = types[i];
-    var next_type = types[j];
-    var next_list = next_type + '_list';
-
-    if (j == 3) {
-      $('tr#locality_text').hide()
-      $("select[name='localityTxt']").attr('value', '');
-    }
-
-    $("[name='" + next_type + "']").parent().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();
-      } else {
-        $("tr#" + next_list).hide();
-        if (j < 6) {
-          displayNextAddressComponent(i, j + 1, value);
-        }
-      }
-    });
-  }
-
-  function changeAddressComponents(type, value)
-  {
-    var types = new Array('country', 'administrative_area_level_1', 'administrative_area_level_2', 'administrative_area_level_3', 'locality', 'sublocality');
-    var i = 0, j = 0;
-
-    while (types[i] != type && i < 6) {
-      ++i;
-    }
-
-    j = i + 1;
-    while (j < 6) {
-      $("select[name='" + types[j] + "']").attr('value', '');
-      $("tr#" + types[j] + "_list").hide();
-      ++j;
-    }
-
-    if (value != '' && i < 5) {
-      $("select[name='" + type + "']").attr('value', value);
-      displayNextAddressComponent(i, i + 1, value);
-    }
-  }
-
-  // when changing school, open diploma choice
-  function changeSchool(schoolId) {
-    $(".autocompleteTarget[name='school']").attr('value',schoolId);
-
-    if (schoolId) {
-      $(".autocomplete[name='schoolTxt']").addClass('hidden_valid');
-    } else {
-      $(".autocomplete[name='schoolTxt']").removeClass('hidden_valid');
-    }
-
-    $("[name='diploma']").parent().load(baseurl + 'list/diploma/', { school:schoolId }, function() {
-        $("select[name='diploma']").attr('value', '{/literal}{$smarty.request.diploma}{literal}');
-      });
-  }
-
-  // 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) {
-    $(".term_tree").remove();
-    $("input[name='jobtermTxt']").val(full_name).addClass("hidden_valid").show();
-    $("input[name='jobterm']").val(jtid);
-  }
-
-  function cancel_autocomplete(field, realfield) {
-    $(".autocomplete[name='"+field+"']").removeClass('hidden_valid').val('').focus();
-    if (typeof(realfield) != "undefined") {
-      $(".autocompleteTarget[name='"+realfield+"']").val('');
-    }
-    return;
-  }
-
-  // when choosing autocomplete from list, must validate
-  function select_autocomplete(name) {
-      nameRealField = name.replace(/Txt$/, '');
-
-      // nothing to do if field is not a text field for a list
-      if (nameRealField == name)
-        return null;
-
-      // When changing country or locality, open next address component.
-      if (nameRealField == 'country' || nameRealField == 'locality') {
-        return function(i) {
-            nameRealField = name.replace(/Txt$/, '');
-            if (i.extra[0] < 0) {
-              cancel_autocomplete(name, nameRealField);
-              i.extra[1] = '';
-            }
-            $("[name='" + nameRealField + "']").parent().load(baseurl + 'list/' + nameRealField, function() {
-              $("select[name='" + nameRealField + "']").attr('value', i.extra[1]);
-            });
-            changeAddressComponents(nameRealField, i.extra[1]);
-          }
-      }
-
-      if (nameRealField == 'school')
-        return function(i) {
-            if (i.extra[0] < 0) {
-              cancel_autocomplete('schoolTxt', 'school');
-              i.extra[1] = '';
-            }
-            changeSchool(i.extra[1]);
-          }
-
-      // change field in list and display text field as valid
-      return function(i) {
-        nameRealField = this.field.replace(/Txt$/, '');
-
-        if (i.extra[0] < 0) {
-          cancel_autocomplete(this.field, nameRealField);
-          return;
-        }
-
-        $(".autocompleteTarget[name='"+nameRealField+"']").attr('value',i.extra[1]);
-
-        $(".autocomplete[name='"+this.field+"']").addClass('hidden_valid');
-      }
-    }
-
-  $(function() {
-      $(".autocompleteTarget").hide();
-      $(".autocomplete").show().each(function() {
-        targeted = $("../.autocompleteTarget",this)[0];
-
-        if (targeted && targeted.value) {
-          me = $(this);
-
-          $.get(baseurl + 'list/'+ targeted.name +'/'+targeted.value, {},function(textValue) {
-            me.attr('value', textValue);
-            me.addClass('hidden_valid');
-          });
-        }
-
-        $(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()});
-        });
-
-      $(".autocomplete").change(function() { $(this).removeClass('hidden_valid'); });
-
-      if ({/literal}'{$smarty.request.country}'{literal} != '') {
-        $("[name='country']").parent().load(baseurl + 'list/country', function() {
-          $("select[name='country']").attr('value', {/literal}'{$smarty.request.country}'{literal});
-        });
-        setAddress(0, 1, new Array({/literal}'{$smarty.request.country}'{literal},
-                                   {/literal}'{$smarty.request.administrative_area_level_1}'{literal},
-                                   {/literal}'{$smarty.request.administrative_area_level_2}'{literal},
-                                   {/literal}'{$smarty.request.administrative_area_level_3}'{literal},
-                                   {/literal}'{$smarty.request.locality}'{literal},
-                                   {/literal}'{$smarty.request.sublocality}'{literal})
-        );
-      } else {
-        var types = new Array('administrative_area_level_1', 'administrative_area_level_2', 'administrative_area_level_3', 'locality', 'sublocality');
-        for (var i = 0; i < 5; ++i) {
-          $("tr#" + types[i] + '_list').hide();
-        }
-      }
-
-      $(".autocomplete[name='schoolTxt']").change(function() { changeSchool(''); });
-
-      changeSchool({/literal}'{$smarty.request.school}'{literal});
-
-      $(".autocompleteToSelect").each(function() {
-          var fieldName = $(this).attr('href');
-
-          $(this).attr('href', baseurl + 'list/'+fieldName).click(function() {
-              var oldval = $("input.autocompleteTarget[name='"+fieldName+"']")[0].value;
-
-              $(".autocompleteTarget[name='"+fieldName+"']").parent().load(baseurl + 'list/'+fieldName,{},
-                function(selectBox) {
-                  $(".autocompleteTarget[name='"+fieldName+"']").remove();
-                  $(".autocomplete[name='"+fieldName+"Txt']").remove();
-                  $("select[name='"+fieldName+"']").attr('value', oldval);
-                });
-
-              return false;
-            });
-        }).parent().find('.autocomplete').change(function() {
-          // If we change the value in the type="text" field, then the value in the 'integer id' field must not be used,
-          // to ensure that, we unset it
-          $(this).parent().find('.autocompleteTarget').val('');
-        });
-
-      $("#only_referent").change(function() { changeOnlyReferent(); });
-
-    });
-/** 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 ];
-/** Uses javascript to clean form from all empty fields */
-function cleanForm(f) {
-  var query = $(f).formSerialize();
-  var old_query;
-  for (var i in default_form_values) {
-    var reg = default_form_values[i];
-    if (typeof(reg) != "undefined") {
-      do {
-        old_query = query;
-        query = query.replace(reg, '$1');
-      } while (old_query != query);
-    }
-  }
-  query = query.replace(/^&*(.*)&*$/, '$1');
-  if (query == "rechercher=Chercher") {
-    alert("Aucun critère n'a été spécifié");
-    return false;
-  }
-  document.location = baseurl + 'adv?' + query;
-  return false;
-}
--->
-{/literal}</script>
 <p class="center">[<a href="search">Revenir à la recherche simple</a>]</p>
 <form id="recherche" action="search/adv" method="get" onsubmit="return cleanForm(this)">
   <table class="bicol" cellpadding="3" summary="Recherche">