mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/RelatedArticles
synced 2024-12-13 08:59:40 +00:00
6941c6df9a
The threshold used to test whether related articles need to be loaded has been changed to the viewport height * 1.5 from the more complicated formula used earlier. The change should help with loading related articles more smoothly. To eliminate the lag completely the related articles need to be rendered in the back-end. Bug: T144822 Change-Id: I9d189163b95533ca43fa3d8632f65b280470f12a
84 lines
2.4 KiB
JavaScript
84 lines
2.4 KiB
JavaScript
( function ( $, mw ) {
|
|
|
|
var config = mw.config.get( [ 'skin', 'wgNamespaceNumber', 'wgMFMode',
|
|
'wgIsMainPage', 'wgAction' ] ),
|
|
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 );
|
|
|
|
/**
|
|
* Load related articles when the user scrolls past half of the window height.
|
|
*
|
|
* @ignore
|
|
*/
|
|
function loadRelatedArticles() {
|
|
var readMore = $( '.read-more-container' ).get( 0 ),
|
|
scrollThreshold = $window.height() * 1.5;
|
|
|
|
if ( mw.viewport.isElementCloseToViewport( readMore, 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 );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Is the current page a diff page?
|
|
*
|
|
* @ignore
|
|
* @return {boolean}
|
|
*/
|
|
function isDiffPage() {
|
|
var queryParams = new mw.Uri( window.location.href ).query;
|
|
|
|
return !!(
|
|
queryParams.type === 'revision' ||
|
|
queryParams.hasOwnProperty( 'diff' ) ||
|
|
queryParams.hasOwnProperty( 'oldid' )
|
|
);
|
|
}
|
|
|
|
if (
|
|
config.wgNamespaceNumber === 0 &&
|
|
!config.wgIsMainPage &&
|
|
// T120735
|
|
config.wgAction === 'view' &&
|
|
!isDiffPage() &&
|
|
// any skin except minerva stable
|
|
( config.skin !== 'minerva' || config.wgMFMode === 'beta' )
|
|
) {
|
|
// Add container to DOM for checking distance on scroll
|
|
// If a skin has marked up a footer content area prepend it there
|
|
if ( $( '.footer-content' ).length ) {
|
|
$( '<div class="read-more-container" />' ).prependTo( '.footer-content' );
|
|
} else {
|
|
$( '<div class="read-more-container post-content" />' )
|
|
.insertAfter( '#content' );
|
|
}
|
|
// try related articles load on scroll
|
|
$window.on( 'scroll', debouncedLoad );
|
|
// try an initial load, in case of no scroll
|
|
loadRelatedArticles();
|
|
}
|
|
|
|
}( jQuery, mediaWiki ) );
|