mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/RelatedArticles
synced 2024-11-27 17:50:25 +00:00
RelatedArticles load after half window scroll
Only triggers RelatedArticles module load and query after user has scrolled through half the document height. Bug: T116838 Change-Id: If290988fb7cc187cad37dd4a8a2c4f38abed9bb9
This commit is contained in:
parent
d843523fcf
commit
9623c1cc42
|
@ -88,7 +88,8 @@
|
|||
],
|
||||
"dependencies": [
|
||||
"mediawiki.api",
|
||||
"ext.relatedArticles.readMore.gateway"
|
||||
"ext.relatedArticles.readMore.gateway",
|
||||
"jquery.throttle-debounce"
|
||||
],
|
||||
"targets": [
|
||||
"mobile",
|
||||
|
|
|
@ -8,25 +8,43 @@
|
|||
mw.config.get( 'wgRelatedArticlesUseCirrusSearch' ),
|
||||
mw.config.get( 'wgRelatedArticlesOnlyUseCirrusSearch' )
|
||||
),
|
||||
LIMIT = 3;
|
||||
LIMIT = 3,
|
||||
debouncedLoad = $.debounce( 100, function () {
|
||||
loadRelatedArticles();
|
||||
} ),
|
||||
$window = $( window ),
|
||||
/**
|
||||
* Threshold value to load related articles - after about half scroll
|
||||
*/
|
||||
scrollThreshold = ( $( document ).height() / 2 ) - $window.height();
|
||||
|
||||
function loadRelatedArticles() {
|
||||
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 &&
|
||||
// any skin except minerva stable
|
||||
( config.skin !== 'minerva' || config.wgMFMode === 'beta' )
|
||||
) {
|
||||
$.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 );
|
||||
}
|
||||
} );
|
||||
// try related articles load on scroll
|
||||
$window.on( 'scroll', debouncedLoad );
|
||||
// try an initial load, in case of no scroll
|
||||
loadRelatedArticles();
|
||||
}
|
||||
|
||||
}( jQuery ) );
|
||||
|
|
Loading…
Reference in a new issue