Only scroll to the top when opening the lightbox

Not when going prev/next.
The saved position feature was probably broken as well if you
happened to press prev/next.
This was my mistake for not noticing that attach() runs on
prev/next.

Bug: 59861
Change-Id: Ic6ff4b15a54178fb5d38640317650f5676293083
This commit is contained in:
Gilles Dubuc 2014-01-09 17:44:00 +01:00
parent 0f719f999d
commit 380612c4bf
2 changed files with 24 additions and 6 deletions

View file

@ -94,10 +94,14 @@
// regardless of what the scroll value was prior to opening the lightbox
var $document = $( document );
// Save the scrollTop value because we want below to be back to where they were
// before opening the lightbox
this.scrollTopBeforeAttach = $document.scrollTop();
$document.scrollTop( 0 );
// Only scroll and save the position if it's the first attach
// Otherwise it could be an attach event happening because of prev/next
if ( this.scrollTopBeforeAttach === undefined ) {
// Save the scrollTop value because we want below to be back to where they were
// before opening the lightbox
this.scrollTopBeforeAttach = $document.scrollTop();
$document.scrollTop( 0 );
}
MLBInterface.prototype.attach.call( this, parentId );
};
@ -108,6 +112,7 @@
// Restore the scrollTop as it was before opening the lightbox
if ( this.scrollTopBeforeAttach !== undefined ) {
$( document ).scrollTop( this.scrollTopBeforeAttach );
this.scrollTopBeforeAttach = undefined;
}
};

View file

@ -22,8 +22,10 @@
QUnit.module( 'ext.multimediaViewer.lightboxInterface', QUnit.newMwEnvironment() );
QUnit.test( 'Sanity test, object creation and ui construction', 12, function ( assert ) {
var lightbox = new mw.LightboxInterface();
QUnit.test( 'Sanity test, object creation and ui construction', 14, function ( assert ) {
var lightbox = new mw.LightboxInterface(),
$document = $( document ),
scrollTopBeforeOpeningLightbox;
function checkIfUIAreasAttachedToDocument( inDocument ) {
var msg = inDocument === 1 ? ' ' : ' not ';
@ -33,18 +35,29 @@
assert.strictEqual( $( '.mw-mlb-image-links' ).length, inDocument, 'Links area' + msg + 'attached.' );
}
// Scroll down a little bit to check that the scroll memory works
$document.scrollTop( 10 );
scrollTopBeforeOpeningLightbox = $document.scrollTop();
// UI areas not attached to the document yet.
checkIfUIAreasAttachedToDocument(0);
// Attach lightbox to testing fixture to avoid interference with other tests.
lightbox.attach( '#qunit-fixture' );
// To make sure that the details are out of view, the lightbox is supposed to scroll to the top when open
assert.strictEqual( $document.scrollTop(), 0, 'Document scrollTop should be set to 0' );
// UI areas should now be attached to the document.
checkIfUIAreasAttachedToDocument(1);
// Unattach lightbox from document
lightbox.unattach();
// Lightbox is supposed to restore the document scrollTop value that was set prior to opening it
assert.strictEqual( $document.scrollTop(), scrollTopBeforeOpeningLightbox, 'document scrollTop value has been restored correctly' );
// UI areas not attached to the document anymore.
checkIfUIAreasAttachedToDocument(0);
} );