mediawiki-extensions-Relate.../resources/ext.relatedArticles.readMore.bootstrap/index.js
Jesús Martínez Novo be3385e6d5 Support other ways to get the description of the page
Currently, the extension queries the prop=description api to fetch the
description, but this api is only available if Wikibaseclient is installed.

Let's support other sources to fetch the page description from, too, or
disable it (by default) to sabe 85 bytes from each response by avoiding the
warning returned from the api when prop=description is not recognized.

A new configuration setting has been added: RelatedArticlesDescriptionSource.
It defaults to false (no description is fetched), but can be set to one of
those string values:

- wikidata: What's being used right now on WMF wikis
- textextracts: Extension:TextExtracts provides the prop=extracts api.
- pagedescription: Extension:Description2 provides a description page property, that can be queried by prop=pageprops&ppprop=description

Bug: T230947
Change-Id: I34410334ba9d6db1f686c7efb5722e2a51957145
2019-08-23 20:24:37 +02:00

60 lines
1.7 KiB
JavaScript

/* eslint-disable no-jquery/no-global-selector */
( function () {
var data = require( './data.json' ),
relatedPages = new mw.relatedPages.RelatedPagesGateway(
new mw.Api(),
mw.config.get( 'wgPageName' ),
mw.config.get( 'wgRelatedArticles' ),
data.useCirrusSearch,
data.onlyUseCirrusSearch,
data.descriptionSource
),
// Make sure this is never undefined as I'm paranoid
LIMIT = mw.config.get( 'wgRelatedArticlesCardLimit', 3 ),
debouncedLoad = $.debounce( 100, function () {
loadRelatedArticles(); // eslint-disable-line no-use-before-define
} ),
$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() * 2;
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.relatedArticles.cards',
'ext.relatedArticles.readMore'
] ),
relatedPages.getForCurrentPage( LIMIT )
).then( function ( _, pages ) {
if ( pages.length ) {
mw.track( 'ext.relatedArticles.init', pages );
} else {
$( readMore ).remove();
}
} );
// detach handler to stop subsequent loads on scroll
$window.off( 'scroll', debouncedLoad );
}
}
function showReadMore() {
// try related articles load on scroll
$window.on( 'scroll', debouncedLoad );
// try an initial load, in case of no scroll
loadRelatedArticles();
}
$( showReadMore );
}() );