don't double-scroll in scrollToVisible

fixes CNVS-5986

as far as I can tell, then called on $("html,body"), outerOffset should
*not* be included in the calculation. it's equal to the current scroll,
but negative, so including it effectively doubled the intended scroll.

i.e. if the current scroll was 500px into the document, and $obj's top
offset was 800px, it would scroll to 1300px instead of 800px (absolute
and correct) or 300px (incorrect, but a reasonable assumption at
relative...)

test-plan:
 - create a discussion with a lot of replies (at least double the
   browser height when rendered)
 - choose a reply in the middle (so that the browser's scrolled), start
   a response and add a link to that response
 - the window shouldn't scroll away from the yellow indicator placed
   over the new link
 - repeat adding a link but with just the button bar for the RCE visible
   in the window (insertion point for the new link below the visible
   section of window)
 - the link + indicator should be scrolled into view correctly
 - double check that other uses of scrollToVisible still behave as
   expected (or better, if they had the same bug):
   * after enrolling a user in a class via the course's People page,
     scrolls the new enrollment into view
   * keyboard navigation on the attendance tool datagrid scrolls the
     window when moving off an edge
   * clicking link to add file in the files' folder panel scrolls to add
     file form (if necessary)
   * ditto for add folder link in the folder panel

Change-Id: I95efff96c50cd11bd584e01045de5c1a9f8cde04
Reviewed-on: https://gerrit.instructure.com/53959
Tested-by: Jenkins
Reviewed-by: Ethan Vizitei <evizitei@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Jacob Fugal <jacob@instructure.com>
This commit is contained in:
Jacob Fugal 2015-05-11 22:19:48 -06:00
parent 8d17f45ef3
commit 9f4f0cb187
1 changed files with 18 additions and 11 deletions

View File

@ -27,27 +27,34 @@ define([
$.fn.scrollToVisible = function(obj) {
var options = {};
var $obj = $(obj);
var outerOffset = $("body").offset();
this.each(function() {
try {
outerOffset = $(this).offset();
return false;
} catch(e) {}
});
if ($obj.length === 0) { return; }
var innerOffset = $obj.offset(),
width = $obj.outerWidth(),
height = $obj.outerHeight(),
top = innerOffset.top - outerOffset.top,
top = innerOffset.top,
bottom = top + height,
left = innerOffset.left - outerOffset.left,
left = innerOffset.left,
right = left + width,
currentTop = (this.selector == "html,body" ? $.windowScrollTop() : this.scrollTop()),
currentLeft = this.scrollLeft(),
currentHeight = this.outerHeight(),
currentWidth = this.outerWidth();
if (this.selector != "html,body") {
var outerOffset = $("body").offset();
this.each(function() {
try {
outerOffset = $(this).offset();
return false;
} catch(e) {}
});
top -= outerOffset.top;
bottom -= outerOffset.top;
left -= outerOffset.left;
right -= outerOffset.left;
}
if (this[0].tagName == "HTML" || this[0].tagName == "BODY") {
currentHeight = $(window).height();
if($("#wizard_box:visible").length > 0) {