From 6450b6c96faf2ad6db207af2acaff7903a118a39 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Wed, 3 Oct 2012 18:30:41 -1000 Subject: [PATCH] Select list wraps around when handling move-up / move-down events --- spec/extensions/select-list-spec.coffee | 11 +++++++++-- src/app/select-list.coffee | 15 +++++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/spec/extensions/select-list-spec.coffee b/spec/extensions/select-list-spec.coffee index e5083f142..874456425 100644 --- a/spec/extensions/select-list-spec.coffee +++ b/spec/extensions/select-list-spec.coffee @@ -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() diff --git a/src/app/select-list.coffee b/src/app/select-list.coffee index b2d0269d7..90eb5f1d2 100644 --- a/src/app/select-list.coffee +++ b/src/app/select-list.coffee @@ -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()