Rev 7554 | Rev 13566 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
6481 | obado | 1 | |
6939 | obado | 2 | /********* Autocomplete.js ************************************************************************ |
10454 | obado | 3 | Autocompletion d'un champ texte |
4 | ajoutez la classe "multicomplete" ou "autocomplete" au choix sur l'input, selon que vous |
||
6942 | bpr | 5 | souhaitez autoriser un ou plusieurs mot-cles. |
6 | Puis lancez set_multicomplete, ou set_autocomplete avec la liste des mots cles en parametre |
||
6939 | obado | 7 | ****************************************************************************************************/ |
6486 | obado | 8 | |
7554 | obado | 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']; |
||
10454 | obado | 28 | |
7554 | obado | 29 | var str = this; |
30 | for(var i = 0; i < accent.length; i++){ |
||
31 | str = str.replace(accent[i], noaccent[i]); |
||
32 | } |
||
10454 | obado | 33 | |
7554 | obado | 34 | return str; |
35 | }; |
||
6939 | obado | 36 | |
7554 | obado | 37 | |
6939 | obado | 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 |
||
7554 | obado | 62 | keyword = extractLast( request.term ).deAccent(); |
63 | //alert(keyword); |
||
64 | response( $.ui.autocomplete.filter(tags, keyword ) ); |
||
6939 | obado | 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 | } |
||
6486 | obado | 77 | }); |
6939 | obado | 78 | } |
6486 | obado | 79 | |
6939 | obado | 80 | function set_autocomplete(tags){ |
6486 | obado | 81 | $(".autocomplete").autocomplete({ |
6487 | obado | 82 | source: function( request, response ) { |
83 | var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" ); |
||
84 | response( $.grep( tags, function( item ){ |
||
6481 | obado | 85 | return matcher.test( item ); |
6487 | obado | 86 | }) ); |
87 | } |
||
6481 | obado | 88 | }); |
6939 | obado | 89 | } |