aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorScott González <scott.gonzalez@gmail.com>2011-05-10 12:35:19 -0400
committerScott González <scott.gonzalez@gmail.com>2011-05-10 12:35:19 -0400
commit319c5eb2c123c24983e38cc9d9fe3058ab045cf4 (patch)
tree2ab6e914ebdb7b2fe25e58369a4a02a7239e225d
parent7e239e2ab2c1147c4a14f6de36e7ecbbdf3d97e9 (diff)
parent7f8ccc523a7879428913e64f4b7eb4ace4d17a08 (diff)
downloadjquery-ui-319c5eb2c123c24983e38cc9d9fe3058ab045cf4.tar.gz
jquery-ui-319c5eb2c123c24983e38cc9d9fe3058ab045cf4.zip
Merge branch 'master' of github.com:jquery/jquery-ui
-rw-r--r--demos/autocomplete/combobox.html62
-rw-r--r--tests/unit/autocomplete/autocomplete_events.js16
-rw-r--r--ui/jquery.ui.autocomplete.js28
-rw-r--r--ui/jquery.ui.menu.js4
4 files changed, 80 insertions, 30 deletions
diff --git a/demos/autocomplete/combobox.html b/demos/autocomplete/combobox.html
index ded809e1e..5fb3ffef9 100644
--- a/demos/autocomplete/combobox.html
+++ b/demos/autocomplete/combobox.html
@@ -16,7 +16,7 @@
<style>
.ui-button { margin-left: -1px; }
.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>
<script>
(function( $ ) {
@@ -26,6 +26,32 @@
select = this.element.hide(),
selected = select.children( ":selected" ),
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>" )
.insertAfter( select )
.val( value )
@@ -57,30 +83,8 @@
});
},
change: function( event, ui ) {
- if ( !ui.item ) {
- var value = $( this ).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
- $( 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;
- }
- }
+ if ( !ui.item )
+ return removeIfInvalid( this );
}
})
.addClass( "ui-widget ui-widget-content ui-corner-left" );
@@ -109,6 +113,7 @@
// close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
input.autocomplete( "close" );
+ removeIfInvalid( input );
return;
}
@@ -124,10 +129,9 @@
.tooltip({
position: {
of: this.button
- }
- })
- .tooltip( "widget" )
- .addClass( "ui-state-highlight" );
+ },
+ tooltipClass: "ui-state-highlight"
+ });
},
destroy: function() {
diff --git a/tests/unit/autocomplete/autocomplete_events.js b/tests/unit/autocomplete/autocomplete_events.js
index 35103d89b..eb009064c 100644
--- a/tests/unit/autocomplete/autocomplete_events.js
+++ b/tests/unit/autocomplete/autocomplete_events.js
@@ -203,4 +203,20 @@ test("cancel select", function() {
}, 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);
diff --git a/ui/jquery.ui.autocomplete.js b/ui/jquery.ui.autocomplete.js
index 526eb3869..b3d7598c1 100644
--- a/ui/jquery.ui.autocomplete.js
+++ b/ui/jquery.ui.autocomplete.js
@@ -62,6 +62,7 @@ $.widget( "ui.autocomplete", {
})
.bind( "keydown.autocomplete", function( event ) {
if ( self.options.disabled || self.element.attr( "readonly" ) ) {
+ suppressKeyPress = true;
return;
}
@@ -69,17 +70,21 @@ $.widget( "ui.autocomplete", {
var keyCode = $.ui.keyCode;
switch( event.keyCode ) {
case keyCode.PAGE_UP:
+ suppressKeyPress = true;
self._move( "previousPage", event );
break;
case keyCode.PAGE_DOWN:
+ suppressKeyPress = true;
self._move( "nextPage", event );
break;
case keyCode.UP:
+ suppressKeyPress = true;
self._move( "previous", event );
// prevent moving cursor to beginning of text field in some browsers
event.preventDefault();
break;
case keyCode.DOWN:
+ suppressKeyPress = true;
self._move( "next", event );
// prevent moving cursor to end of text field in some browsers
event.preventDefault();
@@ -121,7 +126,29 @@ $.widget( "ui.autocomplete", {
if ( suppressKeyPress ) {
suppressKeyPress = false;
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() {
if ( self.options.disabled ) {
@@ -338,6 +365,7 @@ $.widget( "ui.autocomplete", {
this.menu.element.hide();
this.menu.blur();
this._trigger( "close", event );
+ this.menu.isNewMenu = true;
}
},
diff --git a/ui/jquery.ui.menu.js b/ui/jquery.ui.menu.js
index 0841018af..3cc25062c 100644
--- a/ui/jquery.ui.menu.js
+++ b/ui/jquery.ui.menu.js
@@ -18,6 +18,7 @@ var idIncrement = 0;
$.widget("ui.menu", {
defaultElement: "<ul>",
delay: 150,
+ isNewMenu: true,
options: {
position: {
my: "left top",
@@ -54,7 +55,8 @@ $.widget("ui.menu", {
self.select( event );
})
.bind( "mouseover.menu", function( event ) {
- if ( self.options.disabled ) {
+ if ( self.options.disabled || self.isNewMenu ) {
+ self.isNewMenu = false;
return;
}
var target = $( event.target ).closest( ".ui-menu-item" );