Scroll to selected match when moving up/down in autocomplete menu

This commit is contained in:
Corey Johnson & Nathan Sobo 2012-04-20 14:55:54 -06:00
parent 1a398ffcb9
commit 41a5f217a5
3 changed files with 48 additions and 3 deletions

View File

@ -166,7 +166,7 @@ describe "Autocomplete", ->
expect(editor.find('.autocomplete')).not.toExist() expect(editor.find('.autocomplete')).not.toExist()
describe 'move-up event', -> describe 'move-up event', ->
it 'replaces selection with previous match', -> it "highlights the previous match and replaces the selection with it", ->
editor.buffer.insert([10,0] ,"extra:t:extra") editor.buffer.insert([10,0] ,"extra:t:extra")
editor.setCursorBufferPosition([10,6]) editor.setCursorBufferPosition([10,6])
autocomplete.attach() autocomplete.attach()
@ -183,8 +183,25 @@ describe "Autocomplete", ->
expect(autocomplete.find('li:eq(7)')).not.toHaveClass('selected') expect(autocomplete.find('li:eq(7)')).not.toHaveClass('selected')
expect(autocomplete.find('li:eq(6)')).toHaveClass('selected') expect(autocomplete.find('li:eq(6)')).toHaveClass('selected')
it "scrolls to the selected match if it is out of view", ->
editor.buffer.insert([10,0] ,"t")
editor.setCursorBufferPosition([10, 0])
editor.attachToDom()
autocomplete.attach()
matchesList = autocomplete.matchesList
matchesList.height(100)
expect(matchesList.height()).toBeLessThan matchesList[0].scrollHeight
matchCount = matchesList.find('li').length
miniEditor.trigger 'move-up'
expect(matchesList.scrollBottom()).toBe matchesList[0].scrollHeight
miniEditor.trigger 'move-up' for i in [1...matchCount]
expect(matchesList.scrollTop()).toBe 0
describe 'move-down event', -> describe 'move-down event', ->
it 'replaces selection with next match', -> it "highlights the next match and replaces the selection with it", ->
editor.buffer.insert([10,0] ,"extra:s:extra") editor.buffer.insert([10,0] ,"extra:s:extra")
editor.setCursorBufferPosition([10,7]) editor.setCursorBufferPosition([10,7])
autocomplete.attach() autocomplete.attach()
@ -199,6 +216,23 @@ describe "Autocomplete", ->
expect(autocomplete.find('li:eq(0)')).toHaveClass('selected') expect(autocomplete.find('li:eq(0)')).toHaveClass('selected')
expect(autocomplete.find('li:eq(1)')).not.toHaveClass('selected') expect(autocomplete.find('li:eq(1)')).not.toHaveClass('selected')
it "scrolls to the selected match if it is out of view", ->
editor.buffer.insert([10,0] ,"t")
editor.setCursorBufferPosition([10, 0])
editor.attachToDom()
autocomplete.attach()
matchesList = autocomplete.matchesList
matchesList.height(100)
expect(matchesList.height()).toBeLessThan matchesList[0].scrollHeight
matchCount = matchesList.find('li').length
miniEditor.trigger 'move-down' for i in [1...matchCount]
expect(matchesList.scrollBottom()).toBe matchesList[0].scrollHeight
miniEditor.trigger 'move-down'
expect(matchesList.scrollTop()).toBe 0
describe "when the mini-editor receives keyboard input", -> describe "when the mini-editor receives keyboard input", ->
describe "when text is removed from the mini-editor", -> describe "when text is removed from the mini-editor", ->
it "reloads the match list based on the mini-editor's text", -> it "reloads the match list based on the mini-editor's text", ->

View File

@ -114,7 +114,17 @@ class Autocomplete extends View
selectMatchAtIndex: (index) -> selectMatchAtIndex: (index) ->
@currentMatchIndex = index @currentMatchIndex = index
@matchesList.find("li").removeClass "selected" @matchesList.find("li").removeClass "selected"
@matchesList.find("li:eq(#{index})").addClass "selected"
liToSelect = @matchesList.find("li:eq(#{index})")
liToSelect.addClass "selected"
topOfLiToSelect = liToSelect.position().top + @matchesList.scrollTop()
bottomOfLiToSelect = topOfLiToSelect + liToSelect.outerHeight()
if topOfLiToSelect < @matchesList.scrollTop()
@matchesList.scrollTop(topOfLiToSelect)
else if bottomOfLiToSelect > @matchesList.scrollBottom()
@matchesList.scrollBottom(bottomOfLiToSelect)
@replaceSelectedTextWithMatch @selectedMatch() @replaceSelectedTextWithMatch @selectedMatch()
selectedMatch: -> selectedMatch: ->

View File

@ -10,6 +10,7 @@
} }
.autocomplete ol { .autocomplete ol {
position: relative;
overflow-y: scroll; overflow-y: scroll;
max-height: 200px; max-height: 200px;
} }