Skip to content

Commit

Permalink
Modify #209 to work correctly with margins/paddings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Harding committed Apr 25, 2013
1 parent 1611f46 commit 6b0f086
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/dropdown_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ var DropdownView = (function() {

$underCursor = $suggestions.eq(nextIndex).addClass('tt-is-under-cursor');

// make the cursor visible in view
this._ensureCursorVisible($cur, $underCursor);
// in the case of scrollable overflow
// make sure the cursor is visible in the menu
this._ensureVisibility($underCursor);

this.trigger('cursorMoved', extractSuggestion($underCursor));
},
Expand All @@ -105,20 +106,20 @@ var DropdownView = (function() {
return this.$menu.find('.tt-suggestions > .tt-suggestion');
},

_ensureCursorVisible: function($cur, $underCursor) {
var $view, viewHeight, cursorTop, cursorHeight, cursorBottom;

cursorTop = $underCursor.position().top;
$view = $underCursor.closest('.tt-suggestions');
if (cursorTop < 0) {
$view.scrollTop($view.scrollTop() + cursorTop);
} else {
viewHeight = $view.height();
cursorHeight = $underCursor.outerHeight();
cursorBottom = cursorTop + cursorHeight;
if (viewHeight < cursorBottom) {
$view.scrollTop($view.scrollTop() + (cursorBottom - viewHeight));
}
_ensureVisibility: function($el) {
var menuHeight = this.$menu.height() +
parseInt(this.$menu.css('paddingTop'), 10) +
parseInt(this.$menu.css('paddingBottom'), 10),
menuScrollTop = this.$menu.scrollTop(),
elTop = $el.position().top,
elBottom = elTop + $el.outerHeight(true);

if (elTop < 0) {
this.$menu.scrollTop(menuScrollTop + elTop);
}

else if (menuHeight < elBottom) {
this.$menu.scrollTop(menuScrollTop + (elBottom - menuHeight));
}
},

Expand Down
12 changes: 12 additions & 0 deletions test/dropdown_view_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ describe('DropdownView', function() {
renderTestDataset(this.dropdownView, true);
});

it('should ensure the cursor is visible', function() {
spyOn(this.dropdownView, '_ensureVisibility');
this.dropdownView.moveCursorUp();
expect(this.dropdownView._ensureVisibility).toHaveBeenCalled();
});

describe('if no suggestion is under the cursor', function() {
beforeEach(function() {
this.dropdownView.moveCursorUp();
Expand Down Expand Up @@ -346,6 +352,12 @@ describe('DropdownView', function() {
renderTestDataset(this.dropdownView, true);
});

it('should ensure the cursor is visible', function() {
spyOn(this.dropdownView, '_ensureVisibility');
this.dropdownView.moveCursorUp();
expect(this.dropdownView._ensureVisibility).toHaveBeenCalled();
});

describe('if no suggestion is under the cursor', function() {
beforeEach(function() {
this.dropdownView.moveCursorDown();
Expand Down

0 comments on commit 6b0f086

Please # to comment.