Rev 13566 | 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(){ |
||
13566 | obado | 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 | ]; |
||
14456 | obado | 21 | var noaccent = [ |
22 | 'A','a', |
||
23 | 'E','e', |
||
24 | 'I','i', |
||
25 | 'O','o', |
||
26 | 'U','u', |
||
27 | 'N','n', |
||
28 | 'C','c']; |
||
10454 | obado | 29 | |
13566 | obado | 30 | var str = this; |
31 | for(var i = 0; i < accent.length; i++){ |
||
32 | str = str.replace(accent[i], noaccent[i]); |
||
33 | } |
||
10454 | obado | 34 | |
13566 | obado | 35 | return str; |
7554 | obado | 36 | }; |
6939 | obado | 37 | |
7554 | obado | 38 | |
6939 | obado | 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 |
||
7554 | obado | 63 | keyword = extractLast( request.term ).deAccent(); |
64 | //alert(keyword); |
||
65 | response( $.ui.autocomplete.filter(tags, keyword ) ); |
||
6939 | obado | 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 | } |
||
6486 | obado | 78 | }); |
6939 | obado | 79 | } |
6486 | obado | 80 | |
6939 | obado | 81 | function set_autocomplete(tags){ |
13566 | obado | 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 | }); |
||
6939 | obado | 90 | } |