mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/RelatedArticles
synced 2024-12-04 12:48:44 +00:00
a3d9b22a66
Related articles are loaded when the user scrolls past half the document height, which used to be calculated on page load. This height, however, changes after all sections are collapsed on small screens, thus never loading the related articles for the page. Calculating the document height just before showing related articles allows us to correctly decide whether it's time to load the needed modules. Bug: T121263 Change-Id: I1266894ab763cfb571b14f067086445fb6be1887
54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
( function ( $, mw ) {
|
|
|
|
var config = mw.config.get( [ 'skin', 'wgNamespaceNumber', 'wgMFMode', 'wgIsMainPage' ] ),
|
|
relatedPages = new mw.relatedPages.RelatedPagesGateway(
|
|
new mw.Api(),
|
|
mw.config.get( 'wgPageName' ),
|
|
mw.config.get( 'wgRelatedArticles' ),
|
|
mw.config.get( 'wgRelatedArticlesUseCirrusSearch' ),
|
|
mw.config.get( 'wgRelatedArticlesOnlyUseCirrusSearch' )
|
|
),
|
|
LIMIT = 3,
|
|
debouncedLoad = $.debounce( 100, function () {
|
|
loadRelatedArticles();
|
|
} ),
|
|
$window = $( window );
|
|
|
|
function loadRelatedArticles() {
|
|
/**
|
|
* Threshold value to load related articles - after about half scroll
|
|
*/
|
|
var scrollThreshold = ( $( document ).height() / 2 ) - $window.height();
|
|
|
|
if ( $window.scrollTop() > scrollThreshold ) {
|
|
$.when(
|
|
// Note we load dependencies here rather than ResourceLoader
|
|
// to avoid PHP exceptions when Cards not installed
|
|
// which should never happen given the if statement.
|
|
mw.loader.using( [ 'ext.cards', 'ext.relatedArticles.readMore' ] ),
|
|
relatedPages.getForCurrentPage( LIMIT )
|
|
).done( function ( _, pages ) {
|
|
if ( pages.length ) {
|
|
mw.track( 'ext.relatedArticles.init', pages );
|
|
}
|
|
} );
|
|
// detach handler to stop subsequent loads on scroll
|
|
$window.off( 'scroll', debouncedLoad );
|
|
}
|
|
}
|
|
if (
|
|
config.wgNamespaceNumber === 0 &&
|
|
!config.wgIsMainPage &&
|
|
// T120735
|
|
mw.config.get( 'wgAction' ) === 'view' &&
|
|
// any skin except minerva stable
|
|
( config.skin !== 'minerva' || config.wgMFMode === 'beta' )
|
|
) {
|
|
// try related articles load on scroll
|
|
$window.on( 'scroll', debouncedLoad );
|
|
// try an initial load, in case of no scroll
|
|
loadRelatedArticles();
|
|
}
|
|
|
|
}( jQuery, mediaWiki ) );
|