diff --git a/vendor/assets/javascripts/controls.js b/vendor/assets/javascripts/controls.js index 7392fb6..8ac5bf6 100644 --- a/vendor/assets/javascripts/controls.js +++ b/vendor/assets/javascripts/controls.js @@ -211,15 +211,38 @@ Autocompleter.Base = Class.create({ }, markPrevious: function() { - if(this.index > 0) this.index--; - else this.index = this.entryCount-1; - this.getEntry(this.index).scrollIntoView(true); + if(this.index > 0) { + this.index--; + } else { + this.index = this.entryCount - 1; + } + + this.scrollIntoView(); }, markNext: function() { - if(this.index < this.entryCount-1) this.index++; - else this.index = 0; - this.getEntry(this.index).scrollIntoView(false); + if(this.index < this.entryCount-1) { + this.index++; + } else { + this.index = 0; + } + + this.scrollIntoView(); + }, + + scrollIntoView: function() { + var selection_top = this.getEntry(this.index).viewportOffset().top; + var document_top = document.viewport.getScrollOffsets().top; + + // element is above scrolled height + if(selection_top < document_top) { + this.element.scrollIntoView(true); + } + + // element is below scrolled height + else if(selection_top > document_top + document.viewport.getDimensions().height) { + this.getEntry(this.entryCount - 1).scrollIntoView(false); + } }, getEntry: function(index) {