diff options
author | Tim Buschtöns | 2013-08-20 09:36:09 +0000 |
---|---|---|
committer | Tim Buschtöns | 2013-08-20 09:36:09 +0000 |
commit | 5d4eae75053e40831f232be7777aa99ccbb4e197 (patch) | |
tree | 8b12434bab35212f9d33b3bda4969c1d526700cd | |
parent | f77018941bc47310c6db1844972be5fb5765c926 (diff) | |
download | org.eclipse.rap.incubator.dropdown-5d4eae75053e40831f232be7777aa99ccbb4e197.tar.gz org.eclipse.rap.incubator.dropdown-5d4eae75053e40831f232be7777aa99ccbb4e197.tar.xz org.eclipse.rap.incubator.dropdown-5d4eae75053e40831f232be7777aa99ccbb4e197.zip |
Prevent unwanted selection events in AutoSuggest
3 files changed, 62 insertions, 14 deletions
diff --git a/bundles/org.eclipse.rap.addons.autosuggest/src/org/eclipse/rap/addons/autosuggest/internal/resources/AutoSuggest.js b/bundles/org.eclipse.rap.addons.autosuggest/src/org/eclipse/rap/addons/autosuggest/internal/resources/AutoSuggest.js index 4ee5315..9934783 100644 --- a/bundles/org.eclipse.rap.addons.autosuggest/src/org/eclipse/rap/addons/autosuggest/internal/resources/AutoSuggest.js +++ b/bundles/org.eclipse.rap.addons.autosuggest/src/org/eclipse/rap/addons/autosuggest/internal/resources/AutoSuggest.js @@ -184,6 +184,9 @@ function onChangeSelectedSuggestionIndex( event ) { suggestion = this.get( "currentSuggestions" )[ event.value ] || ""; } var replacementText = getReplacementText( suggestion ); + if( replacementText != null ) { + this.set( "replacementText", null, { "action" : "sync" } ); + } this.set( "replacementText", replacementText, { "action" : "selection" } ); } @@ -210,7 +213,9 @@ function onAcceptSuggestion( event ) { if( currentSuggestions ) { var index = this.get( "selectedSuggestionIndex" ); var suggestionSelected = typeof index === "number" && index > -1; - var autoCompleteAccepted = this.get( "autoComplete" ) && currentSuggestions.length === 1; + var autoCompleteAccepted = this.get( "autoComplete" ) + && currentSuggestions.length === 1 + && currentSuggestions[ 0 ] == this.get( "text" ); if( suggestionSelected || autoCompleteAccepted ) { this.notify( "suggestionSelected" ); this.set( "suggestionsVisible", false ); diff --git a/examples/org.eclipse.rap.addons.dropdown.demo/src/org/eclipse/rap/addons/dropdown/demo/scripts/CustomAutoSuggest.js b/examples/org.eclipse.rap.addons.dropdown.demo/src/org/eclipse/rap/addons/dropdown/demo/scripts/CustomAutoSuggest.js index ccde3ed..2b24d8a 100644 --- a/examples/org.eclipse.rap.addons.dropdown.demo/src/org/eclipse/rap/addons/dropdown/demo/scripts/CustomAutoSuggest.js +++ b/examples/org.eclipse.rap.addons.dropdown.demo/src/org/eclipse/rap/addons/dropdown/demo/scripts/CustomAutoSuggest.js @@ -184,6 +184,9 @@ function onChangeSelectedSuggestionIndex( event ) { suggestion = this.get( "currentSuggestions" )[ event.value ] || ""; } var replacementText = getReplacementText( suggestion ); + if( replacementText != null ) { + this.set( "replacementText", null, { "action" : "sync" } ); + } this.set( "replacementText", replacementText, { "action" : "selection" } ); } @@ -210,7 +213,9 @@ function onAcceptSuggestion( event ) { if( currentSuggestions ) { var index = this.get( "selectedSuggestionIndex" ); var suggestionSelected = typeof index === "number" && index > -1; - var autoCompleteAccepted = this.get( "autoComplete" ) && currentSuggestions.length === 1; + var autoCompleteAccepted = this.get( "autoComplete" ) + && currentSuggestions.length === 1 + && currentSuggestions[ 0 ] == this.get( "text" ); if( suggestionSelected || autoCompleteAccepted ) { this.notify( "suggestionSelected" ); this.set( "suggestionsVisible", false ); @@ -231,7 +236,7 @@ function filterSuggestions( options ) { }; var currentSuggestions = filterArray( this.get( "suggestions" ), filterWrapper ); this.set( "currentSuggestions", currentSuggestions, { "action" : options.action } ); - if( currentSuggestions.length === 1 ) { + if( options.action === "typing" && currentSuggestions.length === 1 ) { onAcceptSuggestion.apply( this, [ { "options" : options } ] ); } } diff --git a/tests/org.eclipse.rap.addons.autosuggest.test/jasmine/jasmine/specs/AutoSuggestSpec.js b/tests/org.eclipse.rap.addons.autosuggest.test/jasmine/jasmine/specs/AutoSuggestSpec.js index fd83554..3fe531b 100644 --- a/tests/org.eclipse.rap.addons.autosuggest.test/jasmine/jasmine/specs/AutoSuggestSpec.js +++ b/tests/org.eclipse.rap.addons.autosuggest.test/jasmine/jasmine/specs/AutoSuggestSpec.js @@ -384,7 +384,31 @@ model.set( "selectedSuggestionIndex", 1 ); - expect( log[ 0 ][ 0 ].options.action ).toBe( "selection" ); + expect( log[ log.length - 1 ][ 0 ].options.action ).toBe( "selection" ); + } ); + + it( "fires replacementText change event even if value is unchanged", function() { + model.addListener( "change:selectedSuggestionIndex", createClientListener( "AutoSuggest.js" ) ); + model.set( "currentSuggestions", [ "bar", "banana" ] ); + model.set( "replacementText", "banana" ); + model.addListener( "change:replacementText", logger ); + + model.set( "selectedSuggestionIndex", 1 ); + + expect( model.get( "replacementText" ) ).toEqual( "banana" ); + expect( log[ log.length - 1 ][ 0 ].options.action ).toBe( "selection" ); + } ); + + it( "fires replacementText change event with action selection when restting", function() { + model.addListener( "change:selectedSuggestionIndex", createClientListener( "AutoSuggest.js" ) ); + model.set( "currentSuggestions", [ "bar", "banana" ] ); + model.set( "replacementText", "banana" ); + model.addListener( "change:replacementText", logger ); + + model.set( "selectedSuggestionIndex", -1 ); + + expect( model.get( "replacementText" ) ).toBeNull(); + expect( log[ log.length - 1 ][ 0 ].options.action ).toBe( "selection" ); } ); } ); @@ -582,11 +606,17 @@ describe( "accept", function() { - it( "fires suggestionSelected for selectedSuggestionIndex", function() { + beforeEach( function() { + model.set( "suggestionsVisible", true ); + model.set( "selectedSuggestionIndex", -1 ); model.addListener( "accept", createClientListener( "AutoSuggest.js" ) ); + model.addListener( "suggestionSelected", logger ); + + } ); + + it( "fires suggestionSelected for selectedSuggestionIndex", function() { model.set( "currentSuggestions", [ "bar", "banana" ] ); model.set( "selectedSuggestionIndex", 1 ); - model.addListener( "suggestionSelected", logger ); model.notify( "accept", { source : model, type : "accept" } ); @@ -595,11 +625,10 @@ } ); it( "fires suggestionSelected when full auto complete is accepted", function() { - model.addListener( "accept", createClientListener( "AutoSuggest.js" ) ); model.set( "currentSuggestions", [ "banana" ] ); - model.set( "selectedSuggestionIndex", -1 ); model.set( "autoComplete", true ); - model.addListener( "suggestionSelected", logger ); + model.set( "userText", "ban" ); + model.set( "text", "banana" ); model.notify( "accept", { source : model, type : "accept" } ); @@ -608,11 +637,21 @@ } ); it( "does nothing when attempting accepting without selected suggestion or auto complete", function() { - model.addListener( "accept", createClientListener( "AutoSuggest.js" ) ); model.set( "currentSuggestions", [ "banana" ] ); - model.set( "selectedSuggestionIndex", -1 ); - model.set( "suggestionsVisible", true ); - model.addListener( "suggestionSelected", logger ); + model.set( "userText", "ban" ); + model.set( "text", "ban" ); + + model.notify( "accept", { source : model, type : "accept" } ); + + expect( log.length ).toBe( 0 ); + expect( model.get( "suggestionsVisible" ) ).toBe( true ); + } ); + + it( "does nothing if inserted replacement text has been deleted", function() { + model.set( "currentSuggestions", [ "banana" ] ); + model.set( "autoComplete", true ); + model.set( "userText", "ban" ); + model.set( "text", "Ban" ); model.notify( "accept", { source : model, type : "accept" } ); @@ -621,7 +660,6 @@ } ); it( "clears text selection", function() { - model.addListener( "accept", createClientListener( "AutoSuggest.js" ) ); model.set( "text", "foobar" ); model.notify( "accept", { source : model, type : "accept" } ); |