Subversion Repositories wimsdev

Rev

Rev 10454 | Go to most recent revision | 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 = ['A','a',
  22.                   'E','e',
  23.                   'I','i',
  24.                   'O','o',
  25.                   'U','u',
  26.                   'N','n',
  27.                   'C','c'];
  28.  
  29.   var str = this;
  30.   for(var i = 0; i < accent.length; i++){
  31.     str = str.replace(accent[i], noaccent[i]);
  32.   }
  33.  
  34.   return str;
  35. };
  36.  
  37.  
  38. function comma_split( val ) {
  39.   return val.split( /,\s*/ );
  40. }
  41.  
  42. function extractLast( term ) {
  43.   return comma_split( term ).pop();
  44. }
  45.  
  46. function set_multicomplete(tags){
  47.   $(".multicomplete")
  48.     // don't navigate away from the field on tab when selecting an item
  49.     .bind( "keydown", function( event ) {
  50.       if (event.keyCode === $.ui.keyCode.TAB &&  $( this ).data( "ui-autocomplete" ).menu.active ) {
  51.         event.preventDefault();
  52.       }
  53.     })
  54.     //Adds Autocompletion
  55.     .autocomplete({
  56.       focus: function() {
  57.         // prevent value inserted on focus
  58.         return false;
  59.       },
  60.       source: function( request, response ) {
  61.         // delegate back to autocomplete, but extract the last term
  62.         keyword = extractLast( request.term ).deAccent();
  63.         //alert(keyword);
  64.         response( $.ui.autocomplete.filter(tags, keyword ) );
  65.       },
  66.       select: function( event, ui ) {
  67.         var terms = comma_split( this.value );
  68.         // remove the current input
  69.         terms.pop();
  70.         // add the selected item
  71.         terms.push( ui.item.value );
  72.         // add placeholder to get the comma-and-space at the end
  73.         terms.push("");
  74.         this.value = terms.join(", ");
  75.         return false;
  76.       }
  77.     });
  78. }
  79.  
  80. function set_autocomplete(tags){
  81.   $(".autocomplete").autocomplete({
  82.     source: function( request, response ) {
  83.       var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
  84.       response( $.grep( tags, function( item ){
  85.         return matcher.test( item );
  86.       }) );
  87.     }
  88.   });
  89. }
  90.