Merge branch 'master' of github.com:jquery/jquery-ui

This commit is contained in:
Scott González 2011-05-10 12:35:19 -04:00
commit 319c5eb2c1
4 changed files with 80 additions and 30 deletions

View File

@ -16,7 +16,7 @@
<style> <style>
.ui-button { margin-left: -1px; } .ui-button { margin-left: -1px; }
.ui-button-icon-only .ui-button-text { padding: 0.35em; } .ui-button-icon-only .ui-button-text { padding: 0.35em; }
.ui-autocomplete-input { margin: 0; padding: 0.48em 0 0.47em 0.45em; } .ui-autocomplete-input { margin: 0; padding: 0.4em 0 0.4em 0.45em; }
</style> </style>
<script> <script>
(function( $ ) { (function( $ ) {
@ -26,6 +26,32 @@
select = this.element.hide(), select = this.element.hide(),
selected = select.children( ":selected" ), selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : ""; value = selected.val() ? selected.text() : "";
function removeIfInvalid(element) {
var value = $( element ).val(),
matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( element )
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
select.val( "" );
setTimeout(function() {
input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
input.data( "autocomplete" ).term = "";
return false;
}
}
var input = this.input = $( "<input>" ) var input = this.input = $( "<input>" )
.insertAfter( select ) .insertAfter( select )
.val( value ) .val( value )
@ -57,30 +83,8 @@
}); });
}, },
change: function( event, ui ) { change: function( event, ui ) {
if ( !ui.item ) { if ( !ui.item )
var value = $( this ).val(), return removeIfInvalid( this );
matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( this )
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
select.val( "" );
setTimeout(function() {
input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
input.data( "autocomplete" ).term = "";
return false;
}
}
} }
}) })
.addClass( "ui-widget ui-widget-content ui-corner-left" ); .addClass( "ui-widget ui-widget-content ui-corner-left" );
@ -109,6 +113,7 @@
// 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" );
removeIfInvalid( input );
return; return;
} }
@ -124,10 +129,9 @@
.tooltip({ .tooltip({
position: { position: {
of: this.button of: this.button
} },
}) tooltipClass: "ui-state-highlight"
.tooltip( "widget" ) });
.addClass( "ui-state-highlight" );
}, },
destroy: function() { destroy: function() {

View File

@ -203,4 +203,20 @@ test("cancel select", function() {
}, 50); }, 50);
}); });
test("blur without selection", function() {
expect(1);
var ac = $("#autocomplete").autocomplete({
delay: 0,
source: data
});
stop();
ac.val("j").keydown();
setTimeout(function() {
$( ".ui-menu-item" ).first().simulate("mouseover");
ac.simulate("keydown", { keyCode: $.ui.keyCode.TAB });
same( ac.val(), "j" );
start();
}, 50);
});
})(jQuery); })(jQuery);

View File

@ -62,6 +62,7 @@ $.widget( "ui.autocomplete", {
}) })
.bind( "keydown.autocomplete", function( event ) { .bind( "keydown.autocomplete", function( event ) {
if ( self.options.disabled || self.element.attr( "readonly" ) ) { if ( self.options.disabled || self.element.attr( "readonly" ) ) {
suppressKeyPress = true;
return; return;
} }
@ -69,17 +70,21 @@ $.widget( "ui.autocomplete", {
var keyCode = $.ui.keyCode; var keyCode = $.ui.keyCode;
switch( event.keyCode ) { switch( event.keyCode ) {
case keyCode.PAGE_UP: case keyCode.PAGE_UP:
suppressKeyPress = true;
self._move( "previousPage", event ); self._move( "previousPage", event );
break; break;
case keyCode.PAGE_DOWN: case keyCode.PAGE_DOWN:
suppressKeyPress = true;
self._move( "nextPage", event ); self._move( "nextPage", event );
break; break;
case keyCode.UP: case keyCode.UP:
suppressKeyPress = true;
self._move( "previous", event ); self._move( "previous", event );
// prevent moving cursor to beginning of text field in some browsers // prevent moving cursor to beginning of text field in some browsers
event.preventDefault(); event.preventDefault();
break; break;
case keyCode.DOWN: case keyCode.DOWN:
suppressKeyPress = true;
self._move( "next", event ); self._move( "next", event );
// prevent moving cursor to end of text field in some browsers // prevent moving cursor to end of text field in some browsers
event.preventDefault(); event.preventDefault();
@ -121,7 +126,29 @@ $.widget( "ui.autocomplete", {
if ( suppressKeyPress ) { if ( suppressKeyPress ) {
suppressKeyPress = false; suppressKeyPress = false;
event.preventDefault(); event.preventDefault();
return;
} }
// replicate some key handlers to allow them to repeat in Firefox and Opera
var keyCode = $.ui.keyCode;
switch( event.keyCode ) {
case keyCode.PAGE_UP:
self._move( "previousPage", event );
break;
case keyCode.PAGE_DOWN:
self._move( "nextPage", event );
break;
case keyCode.UP:
self._move( "previous", event );
// prevent moving cursor to beginning of text field in some browsers
event.preventDefault();
break;
case keyCode.DOWN:
self._move( "next", event );
// prevent moving cursor to end of text field in some browsers
event.preventDefault();
break;
}
}) })
.bind( "focus.autocomplete", function() { .bind( "focus.autocomplete", function() {
if ( self.options.disabled ) { if ( self.options.disabled ) {
@ -338,6 +365,7 @@ $.widget( "ui.autocomplete", {
this.menu.element.hide(); this.menu.element.hide();
this.menu.blur(); this.menu.blur();
this._trigger( "close", event ); this._trigger( "close", event );
this.menu.isNewMenu = true;
} }
}, },

View File

@ -18,6 +18,7 @@ var idIncrement = 0;
$.widget("ui.menu", { $.widget("ui.menu", {
defaultElement: "<ul>", defaultElement: "<ul>",
delay: 150, delay: 150,
isNewMenu: true,
options: { options: {
position: { position: {
my: "left top", my: "left top",
@ -54,7 +55,8 @@ $.widget("ui.menu", {
self.select( event ); self.select( event );
}) })
.bind( "mouseover.menu", function( event ) { .bind( "mouseover.menu", function( event ) {
if ( self.options.disabled ) { if ( self.options.disabled || self.isNewMenu ) {
self.isNewMenu = false;
return; return;
} }
var target = $( event.target ).closest( ".ui-menu-item" ); var target = $( event.target ).closest( ".ui-menu-item" );