From bbfc0a63e1e682560864fa033b197ce65e65b839 Mon Sep 17 00:00:00 2001 From: Steven Peckins Date: Tue, 10 Apr 2012 18:49:03 -0400 Subject: [PATCH 1/2] Add Element.scrollIntoViewIfNecessary(); use in markNext/markPrevious Element.scrollIntoViewIfNecessary() only scrolls the parent container if the element is not already entirely within view. This solves two related issues for autocomplete dropdowns when there are scrollbars. First, "changing direction" while scrolling through choices is no longer a jarring user experience. Second, when the autocomplete element is within a containing element with its own scrollbars, scrollIntoView would scroll the outer scroll bars, moving the LI.selected to the top and obscuring the INPUT element. --- src/controls.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/controls.js b/src/controls.js index cde682f..8484422 100644 --- a/src/controls.js +++ b/src/controls.js @@ -211,13 +211,13 @@ Autocompleter.Base = Class.create({ markPrevious: function() { if(this.index > 0) this.index--; else this.index = this.entryCount-1; - this.getEntry(this.index).scrollIntoView(true); + this.getEntry(this.index).scrollIntoViewIfNecessary(true); }, markNext: function() { if(this.index < this.entryCount-1) this.index++; else this.index = 0; - this.getEntry(this.index).scrollIntoView(false); + this.getEntry(this.index).scrollIntoViewIfNecessary(false); }, getEntry: function(index) { @@ -960,4 +960,16 @@ Form.Element.DelayedObserver = Class.create({ this.timer = null; this.callback(this.element, $F(this.element)); } -}); \ No newline at end of file +}); + +Element.addMethods({ + scrollIntoViewIfNecessary: function(element, showAtTop) { + element = $(element); + var parent = element.getOffsetParent(), y = element.offsetTop; + if (parent.scrollTop > element.offsetTop || parent.scrollTop < (element.offsetTop - parent.offsetHeight + element.offsetHeight)) { + parent.scrollTop = (showAtTop || typeof(showAtTop) == 'undefined') + ? element.offsetTop + : element.offsetTop - parent.offsetHeight + element.offsetHeight; + } + } +}); From 83a996ef4c79c5b71778839305d067ed5f330852 Mon Sep 17 00:00:00 2001 From: Steven Peckins Date: Tue, 8 May 2012 16:08:57 -0400 Subject: [PATCH 2/2] Add 'return element' to scrollIntoViewIfNecessary --- src/controls.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/controls.js b/src/controls.js index 8484422..553a3ad 100644 --- a/src/controls.js +++ b/src/controls.js @@ -971,5 +971,6 @@ Element.addMethods({ ? element.offsetTop : element.offsetTop - parent.offsetHeight + element.offsetHeight; } + return element; } });