|
16 | 16 | <style> |
17 | 17 | .ui-button { margin-left: -1px; } |
18 | 18 | .ui-button-icon-only .ui-button-text { padding: 0.35em; } |
19 | | - .ui-autocomplete-input { margin: 0; padding: 0.48em 0 0.47em 0.45em; } |
| 19 | + .ui-autocomplete-input { margin: 0; padding: 0.4em 0 0.4em 0.45em; } |
20 | 20 | </style> |
21 | 21 | <script> |
22 | 22 | (function( $ ) { |
|
26 | 26 | select = this.element.hide(), |
27 | 27 | selected = select.children( ":selected" ), |
28 | 28 | value = selected.val() ? selected.text() : ""; |
| 29 | + |
| 30 | + function removeIfInvalid(element) { |
| 31 | + var value = $( element ).val(), |
| 32 | + matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ), |
| 33 | + valid = false; |
| 34 | + select.children( "option" ).each(function() { |
| 35 | + if ( $( this ).text().match( matcher ) ) { |
| 36 | + this.selected = valid = true; |
| 37 | + return false; |
| 38 | + } |
| 39 | + }); |
| 40 | + if ( !valid ) { |
| 41 | + // remove invalid value, as it didn't match anything |
| 42 | + $( element ) |
| 43 | + .val( "" ) |
| 44 | + .attr( "title", value + " didn't match any item" ) |
| 45 | + .tooltip( "open" ); |
| 46 | + select.val( "" ); |
| 47 | + setTimeout(function() { |
| 48 | + input.tooltip( "close" ).attr( "title", "" ); |
| 49 | + }, 2500 ); |
| 50 | + input.data( "autocomplete" ).term = ""; |
| 51 | + return false; |
| 52 | + } |
| 53 | + } |
| 54 | + |
29 | 55 | var input = this.input = $( "<input>" ) |
30 | 56 | .insertAfter( select ) |
31 | 57 | .val( value ) |
|
57 | 83 | }); |
58 | 84 | }, |
59 | 85 | change: function( event, ui ) { |
60 | | - if ( !ui.item ) { |
61 | | - var value = $( this ).val(), |
62 | | - matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ), |
63 | | - valid = false; |
64 | | - select.children( "option" ).each(function() { |
65 | | - if ( $( this ).text().match( matcher ) ) { |
66 | | - this.selected = valid = true; |
67 | | - return false; |
68 | | - } |
69 | | - }); |
70 | | - if ( !valid ) { |
71 | | - // remove invalid value, as it didn't match anything |
72 | | - $( this ) |
73 | | - .val( "" ) |
74 | | - .attr( "title", value + " didn't match any item" ) |
75 | | - .tooltip( "open" ); |
76 | | - select.val( "" ); |
77 | | - setTimeout(function() { |
78 | | - input.tooltip( "close" ).attr( "title", "" ); |
79 | | - }, 2500 ); |
80 | | - input.data( "autocomplete" ).term = ""; |
81 | | - return false; |
82 | | - } |
83 | | - } |
| 86 | + if ( !ui.item ) |
| 87 | + return removeIfInvalid( this ); |
84 | 88 | } |
85 | 89 | }) |
86 | 90 | .addClass( "ui-widget ui-widget-content ui-corner-left" ); |
|
109 | 113 | // close if already visible |
110 | 114 | if ( input.autocomplete( "widget" ).is( ":visible" ) ) { |
111 | 115 | input.autocomplete( "close" ); |
| 116 | + removeIfInvalid( input ); |
112 | 117 | return; |
113 | 118 | } |
114 | 119 |
|
|
124 | 129 | .tooltip({ |
125 | 130 | position: { |
126 | 131 | of: this.button |
127 | | - } |
128 | | - }) |
129 | | - .tooltip( "widget" ) |
130 | | - .addClass( "ui-state-highlight" ); |
| 132 | + }, |
| 133 | + tooltipClass: "ui-state-highlight" |
| 134 | + }); |
131 | 135 | }, |
132 | 136 |
|
133 | 137 | destroy: function() { |
|
0 commit comments