Autocomplete demo (Combobox): Split code into smaller functions.

This commit is contained in:
Scott González 2013-02-19 15:10:02 -05:00
parent 5546e76354
commit c1d04def42

View File

@ -37,79 +37,52 @@
(function( $ ) { (function( $ ) {
$.widget( "ui.combobox", { $.widget( "ui.combobox", {
_create: function() { _create: function() {
var input, this.wrapper = $( "<span>" )
that = this,
wasOpen = false,
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "",
wrapper = this.wrapper = $( "<span>" )
.addClass( "ui-combobox" ) .addClass( "ui-combobox" )
.insertAfter( select ); .insertAfter( this.element );
function removeIfInvalid( element ) { this._createAutocomplete();
var value = $( element ).val().toLowerCase(), this._createShowAllButton();
valid = false; },
select.children( "option" ).each(function() {
if ( $( this ).text().toLowerCase() === value ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) { _createAutocomplete: function() {
// remove invalid value, as it didn't match anything var selected = this.element.children( ":selected" ),
$( element ) value = selected.val() ? selected.text() : "";
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
select.val( "" );
setTimeout(function() {
input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
input.data( "ui-autocomplete" ).term = "";
}
}
input = $( "<input>" ) this.input = $( "<input>" )
.appendTo( wrapper ) .appendTo( this.wrapper )
.val( value ) .val( value )
.attr( "title", "" ) .attr( "title", "" )
.addClass( "ui-state-default ui-combobox-input" ) .addClass( "ui-state-default ui-combobox-input ui-widget ui-widget-content ui-corner-left" )
.autocomplete({ .autocomplete({
delay: 0, delay: 0,
minLength: 0, minLength: 0,
source: function( request, response ) { source: $.proxy( this, "_source" )
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" ); })
response( select.children( "option" ).map(function() { .tooltip({
var text = $( this ).text(); tooltipClass: "ui-state-highlight"
if ( this.value && ( !request.term || matcher.test(text) ) ) });
return {
label: text, this._on( this.input, {
value: text, autocompleteselect: function( event, ui ) {
option: this
};
}) );
},
select: function( event, ui ) {
ui.item.option.selected = true; ui.item.option.selected = true;
that._trigger( "select", event, { this._trigger( "select", event, {
item: ui.item.option item: ui.item.option
}); });
}, },
change: function( event, ui ) {
if ( !ui.item ) { autocompletechange: "_removeIfInvalid"
removeIfInvalid( this ); });
} },
}
}) _createShowAllButton: function() {
.addClass( "ui-widget ui-widget-content ui-corner-left" ); var wasOpen = false;
$( "<a>" ) $( "<a>" )
.attr( "tabIndex", -1 ) .attr( "tabIndex", -1 )
.attr( "title", "Show All Items" ) .attr( "title", "Show All Items" )
.tooltip() .tooltip()
.appendTo( wrapper ) .appendTo( this.wrapper )
.button({ .button({
icons: { icons: {
primary: "ui-icon-triangle-1-s" primary: "ui-icon-triangle-1-s"
@ -124,18 +97,62 @@
.click(function() { .click(function() {
input.focus(); input.focus();
// close if already visible // Close if already visible
if ( wasOpen ) { if ( wasOpen ) {
return; return;
} }
// pass empty string as value to search for, displaying all results // Pass empty string as value to search for, displaying all results
input.autocomplete( "search", "" ); input.autocomplete( "search", "" );
}); });
},
input.tooltip({ _source: function( request, response ) {
tooltipClass: "ui-state-highlight" var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
response( this.element.children( "option" ).map(function() {
var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
return {
label: text,
value: text,
option: this
};
}) );
},
_removeIfInvalid: function( event, ui ) {
// Selected an item, nothing to do
if ( ui.item ) {
return;
}
// Search for a match (case-insensitive)
var value = this.input.val(),
valueLowerCase = value.toLowerCase(),
valid = false;
this.element.children( "option" ).each(function() {
if ( $( this ).text().toLowerCase() === valueLowerCase ) {
this.selected = valid = true;
return false;
}
}); });
// Found a match, nothing to do
if ( valid ) {
return;
}
// Remove invalid value
this.input
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
this.element.val( "" );
this._delay(function() {
this.input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
this.input.data( "ui-autocomplete" ).term = "";
}, },
_destroy: function() { _destroy: function() {