2 $.widget( "custom.combobox", {
4 this.wrapper = $( "<span>" )
5 .addClass( "custom-combobox" )
6 .insertAfter( this.element );
9 this._createAutocomplete();
10 this._createShowAllButton();
13 _createAutocomplete: function() {
14 var selected = this.element.children( ":selected" ),
15 value = selected.val() ? selected.text() : "";
17 this.input = $( "<input>" )
18 .appendTo( this.wrapper )
21 .addClass( "custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left" )
25 source: $.proxy( this, "_source" )
28 tooltipClass: "ui-state-highlight"
31 this._on( this.input, {
32 autocompleteselect: function( event, ui ) {
33 ui.item.option.selected = true;
34 this._trigger( "select", event, {
39 autocompletechange: "_removeIfInvalid"
43 _createShowAllButton: function() {
44 var input = this.input,
48 .attr( "tabIndex", -1 )
49 .attr( "title", "Show All Items" )
51 .appendTo( this.wrapper )
54 primary: "ui-icon-triangle-1-s"
58 .removeClass( "ui-corner-all" )
59 .addClass( "custom-combobox-toggle ui-corner-right" )
60 .mousedown(function() {
61 wasOpen = input.autocomplete( "widget" ).is( ":visible" );
66 // Close if already visible
71 // Pass empty string as value to search for, displaying all results
72 input.autocomplete( "search", "" );
76 _source: function( request, response ) {
77 var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
78 response( this.element.children( "option" ).map(function() {
79 var text = $( this ).text();
80 if ( this.value && ( !request.term || matcher.test(text) ) )
89 _removeIfInvalid: function( event, ui ) {
91 // Selected an item, nothing to do
96 // Search for a match (case-insensitive)
97 var value = this.input.val(),
98 valueLowerCase = value.toLowerCase(),
100 this.element.children( "option" ).each(function() {
101 if ( $( this ).text().toLowerCase() === valueLowerCase ) {
102 this.selected = valid = true;
107 // Found a match, nothing to do
112 // Remove invalid value
115 .attr( "title", value + " didn't match any item" )
117 this.element.val( "" );
118 this._delay(function() {
119 this.input.tooltip( "close" ).attr( "title", "" );
121 this.input.autocomplete( "instance" ).term = "";
124 _destroy: function() {
125 this.wrapper.remove();