Subversion Repositories wimsdev

Rev

Rev 13566 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1.  
  2. /********* Autocomplete.js  ************************************************************************
  3.     Autocompletion d'un champ texte
  4.     ajoutez la classe "multicomplete" ou "autocomplete" au choix sur l'input, selon que vous
  5.     souhaitez autoriser un ou plusieurs mot-cles.
  6.     Puis lancez set_multicomplete, ou set_autocomplete avec la liste des mots cles en parametre
  7. ****************************************************************************************************/
  8.  
  9. /** String function to replace accented latin letters by their normal ones, using RegExp.
  10.     based on js-replace-diacritics "https://github.com/yvg/js-replace-diacritics" **/
  11. String.prototype.deAccent = function(){
  12.   var accent = [
  13.     /[\300-\306]/g, /[\340-\346]/g, // A, a
  14.     /[\310-\313]/g, /[\350-\353]/g, // E, e
  15.     /[\314-\317]/g, /[\354-\357]/g, // I, i
  16.     /[\322-\330]/g, /[\362-\370]/g, // O, o
  17.     /[\331-\334]/g, /[\371-\374]/g, // U, u
  18.     /[\321]/g, /[\361]/g, // N, n
  19.     /[\307]/g, /[\347]/g, // C, c
  20.   ];
  21.   var noaccent = [
  22.     'A','a',
  23.     'E','e',
  24.     'I','i',
  25.     'O','o',
  26.     'U','u',
  27.     'N','n',
  28.     'C','c'];
  29.  
  30.   var str = this;
  31.   for(var i = 0; i < accent.length; i++){
  32.     str = str.replace(accent[i], noaccent[i]);
  33.   }
  34.  
  35.   return str;
  36. };
  37.  
  38.  
  39. function comma_split( val ) {
  40.   return val.split( /,\s*/ );
  41. }
  42.  
  43. function extractLast( term ) {
  44.   return comma_split( term ).pop();
  45. }
  46.  
  47. function set_multicomplete(tags){
  48.   $(".multicomplete")
  49.     // don't navigate away from the field on tab when selecting an item
  50.     .bind( "keydown", function( event ) {
  51.       if (event.keyCode === $.ui.keyCode.TAB &&  $( this ).data( "ui-autocomplete" ).menu.active ) {
  52.         event.preventDefault();
  53.       }
  54.     })
  55.     //Adds Autocompletion
  56.     .autocomplete({
  57.       focus: function() {
  58.         // prevent value inserted on focus
  59.         return false;
  60.       },
  61.       source: function( request, response ) {
  62.         // delegate back to autocomplete, but extract the last term
  63.         keyword = extractLast( request.term ).deAccent();
  64.         //alert(keyword);
  65.         response( $.ui.autocomplete.filter(tags, keyword ) );
  66.       },
  67.       select: function( event, ui ) {
  68.         var terms = comma_split( this.value );
  69.         // remove the current input
  70.         terms.pop();
  71.         // add the selected item
  72.         terms.push( ui.item.value );
  73.         // add placeholder to get the comma-and-space at the end
  74.         terms.push("");
  75.         this.value = terms.join(", ");
  76.         return false;
  77.       }
  78.     });
  79. }
  80.  
  81. function set_autocomplete(tags){
  82.   $(".autocomplete").autocomplete({
  83.     source: function( request, response ) {
  84.       var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
  85.       response( $.grep( tags, function( item ){
  86.         return matcher.test( item );
  87.       }) );
  88.     }
  89.   });
  90. }
  91.