Edited combobox to adhere to coding standards and added a dblclick handler.

This commit is contained in:
Thomas Tanghus 2012-05-12 13:42:41 +02:00
parent a36a8faad4
commit 19b55d3fce
1 changed files with 24 additions and 19 deletions

View File

@ -23,16 +23,16 @@
minLength: 0, minLength: 0,
source: function( request, response ) { source: function( request, response ) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" ); var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
response( select.children( "option" ).map(function() { response( select.children('option').map(function() {
var text = $( this ).text(); var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) ) if ( this.value && ( !request.term || matcher.test(text) ) )
return { return {
label: text.replace( label: text.replace(
new RegExp( new RegExp(
"(?![^&;]+;)(?!<[^<>]*)(" + '(?![^&;]+;)(?!<[^<>]*)(' +
$.ui.autocomplete.escapeRegex(request.term) + $.ui.autocomplete.escapeRegex(request.term) +
")(?![^<>]*>)(?![^&;]+;)", "gi" ')(?![^<>]*>)(?![^&;]+;)', 'gi'
), "<strong>$1</strong>" ), ), '<strong>$1</strong>'),
value: text, value: text,
option: this option: this
}; };
@ -42,17 +42,17 @@
self.input.val($(ui.item.option).text()); self.input.val($(ui.item.option).text());
self.input.trigger('change'); self.input.trigger('change');
ui.item.option.selected = true; ui.item.option.selected = true;
self._trigger( "selected", event, { self._trigger('selected', event, {
item: ui.item.option item: ui.item.option
}); });
}, },
change: function( event, ui ) { change: function( event, ui ) {
if ( !ui.item ) { if ( !ui.item ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ), var matcher = new RegExp( '^' + $.ui.autocomplete.escapeRegex( $(this).val() ) + '$', 'i' ),
valid = false; valid = false;
self.input.val($(this).val()); self.input.val($(this).val());
//self.input.trigger('change'); //self.input.trigger('change');
select.children( "option" ).each(function() { select.children('option').each(function() {
if ( $( this ).text().match( matcher ) ) { if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true; this.selected = valid = true;
return false; return false;
@ -62,36 +62,41 @@
// remove invalid value, as it didn't match anything // remove invalid value, as it didn't match anything
$( this ).val( "" ); $( this ).val( "" );
select.val( "" ); select.val( "" );
input.data( "autocomplete" ).term = ""; input.data('autocomplete').term = '';
return false; return false;
} }
} }
} }
}) })
.addClass( "ui-widget ui-widget-content ui-corner-left" ); .addClass('ui-widget ui-widget-content ui-corner-left');
input.data( "autocomplete" )._renderItem = function( ul, item ) { input.data('autocomplete')._renderItem = function( ul, item ) {
return $( "<li></li>" ) return $('<li></li>')
.data( "item.autocomplete", item ) .data('item.autocomplete', item )
.append( "<a>" + item.label + "</a>" ) .append('<a>' + item.label + '</a>')
.appendTo( ul ); .appendTo( ul );
}; };
$.each(this.options, function(key, value) { $.each(this.options, function(key, value) {
self._setOption(key, value); self._setOption(key, value);
}); });
input.dblclick(function() {
// pass empty string as value to search for, displaying all results
input.autocomplete('search', '');
});
if(this.options['showButton']) { if(this.options['showButton']) {
this.button = $( "<button type='button'>&nbsp;</button>" ) this.button = $('<button type="button">&nbsp;</button>')
.attr( "tabIndex", -1 ) .attr('tabIndex', -1 )
.attr( "title", "Show All Items" ) .attr('title', 'Show All Items')
.insertAfter( input ) .insertAfter( input )
.addClass('svg') .addClass('svg')
.addClass('action') .addClass('action')
.addClass('combo-button') .addClass('combo-button')
.click(function() { .click(function() {
// close if already visible // close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) { if ( input.autocomplete('widget').is(':visible') ) {
input.autocomplete( "close" ); input.autocomplete('close');
return; return;
} }
@ -99,7 +104,7 @@
$( this ).blur(); $( this ).blur();
// 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.focus(); input.focus();
}); });
} }