Select list wraps around when handling move-up / move-down events

This commit is contained in:
Nathan Sobo
2012-10-03 18:30:41 -10:00
parent 149889abfc
commit 6450b6c96f
2 changed files with 18 additions and 8 deletions

View File

@@ -2,7 +2,7 @@ SelectList = require 'select-list'
{$$} = require 'space-pen'
$ = require 'jquery'
describe "SelectList", ->
fdescribe "SelectList", ->
[selectList, array, list, miniEditor] = []
beforeEach ->
@@ -37,12 +37,18 @@ describe "SelectList", ->
expect(list.find('li:contains(Delta)')).toExist()
describe "when move-up / move-down are triggered on the miniEditor", ->
it "selects the previous / next item in the list, if there is one", ->
it "selects the previous / next item in the list, or wraps around to the other side", ->
expect(list.find('li:first')).toHaveClass 'selected'
miniEditor.trigger 'move-up'
expect(list.find('li:first')).not.toHaveClass 'selected'
expect(list.find('li:last')).toHaveClass 'selected'
miniEditor.trigger 'move-down'
expect(list.find('li:first')).toHaveClass 'selected'
expect(list.find('li:last')).not.toHaveClass 'selected'
miniEditor.trigger 'move-down'
@@ -59,6 +65,7 @@ describe "SelectList", ->
expect(list.find('li:eq(2)')).not.toHaveClass 'selected'
expect(list.find('li:eq(1)')).toHaveClass 'selected'
it "scrolls to keep the selected item in view", ->
selectList.attachToDom()
itemHeight = list.find('li').outerHeight()

View File

@@ -42,16 +42,19 @@ class SelectList extends View
@list.append(item)
selectPreviousItem: ->
@selectItem(@getSelectedItem().prev())
item = @getSelectedItem().prev()
item = @list.find('li:last') unless item.length
@selectItem(item)
selectNextItem: ->
@selectItem(@getSelectedItem().next())
item = @getSelectedItem().next()
item = @list.find('li:first') unless item.length
@selectItem(item)
selectItem: (item) ->
if item.length
@list.find('.selected').removeClass('selected')
item.addClass 'selected'
@scrollToItem(item)
@list.find('.selected').removeClass('selected')
item.addClass 'selected'
@scrollToItem(item)
scrollToItem: (item) ->
scrollTop = @list.scrollTop()