2019-04-03 23:20:58 +00:00
|
|
|
/* eslint-disable no-jquery/no-global-selector */
|
2018-09-14 05:30:46 +00:00
|
|
|
( function () {
|
Add Related Articles section to Minerva
If the page has related articles, is in mainspace, isn't the main page,
and the output is being rendered with the MinervaBeta skin then a
"Related Articles" section is added to the page just before the footer.
Separate loading the information necessary to render the pages, choosing
the renderer, and rendering the data so that multiple skins - currently
Minerva and Vector per the mocks - not just multiple resolutions can all
be handled the same way:
* The bootstrap script (ext.relatedArticles.readMore.bootstrap/index.js)
for fetches the page image and Wikidata description; loading the
renderer module; and, finally, notifying the renderer module that it
should render the data, which it does by emitting
"ext.relatedArticles.readMore.init" event using mw#track
* The Minerva renderer subscribes to the event and, when it's fired,
renders the data by passing it to the WatchstarPageList view
Bug: T113635
Change-Id: I651342bdf9796938fa7051828dd13bc6fe774783
2015-10-07 13:12:42 +00:00
|
|
|
|
2019-05-25 21:18:58 +00:00
|
|
|
var data = require( './data.json' ),
|
|
|
|
relatedPages = new mw.relatedPages.RelatedPagesGateway(
|
2015-11-11 15:30:19 +00:00
|
|
|
new mw.Api(),
|
|
|
|
mw.config.get( 'wgPageName' ),
|
|
|
|
mw.config.get( 'wgRelatedArticles' ),
|
2019-05-25 21:18:58 +00:00
|
|
|
data.useCirrusSearch,
|
|
|
|
data.onlyUseCirrusSearch
|
2015-11-11 15:30:19 +00:00
|
|
|
),
|
2017-07-14 23:40:47 +00:00
|
|
|
// Make sure this is never undefined as I'm paranoid
|
|
|
|
LIMIT = mw.config.get( 'wgRelatedArticlesCardLimit', 3 ),
|
2015-12-05 17:44:15 +00:00
|
|
|
debouncedLoad = $.debounce( 100, function () {
|
2018-08-17 11:10:34 +00:00
|
|
|
loadRelatedArticles(); // eslint-disable-line no-use-before-define
|
2015-12-05 17:44:15 +00:00
|
|
|
} ),
|
2018-09-13 18:40:42 +00:00
|
|
|
$window = $( window );
|
2017-01-27 21:10:46 +00:00
|
|
|
|
2015-12-29 02:55:43 +00:00
|
|
|
/**
|
|
|
|
* Load related articles when the user scrolls past half of the window height.
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
*/
|
2015-12-14 10:34:17 +00:00
|
|
|
function loadRelatedArticles() {
|
2016-08-30 18:01:48 +00:00
|
|
|
var readMore = $( '.read-more-container' ).get( 0 ),
|
2016-09-15 19:10:24 +00:00
|
|
|
scrollThreshold = $window.height() * 2;
|
Add Related Articles section to Minerva
If the page has related articles, is in mainspace, isn't the main page,
and the output is being rendered with the MinervaBeta skin then a
"Related Articles" section is added to the page just before the footer.
Separate loading the information necessary to render the pages, choosing
the renderer, and rendering the data so that multiple skins - currently
Minerva and Vector per the mocks - not just multiple resolutions can all
be handled the same way:
* The bootstrap script (ext.relatedArticles.readMore.bootstrap/index.js)
for fetches the page image and Wikidata description; loading the
renderer module; and, finally, notifying the renderer module that it
should render the data, which it does by emitting
"ext.relatedArticles.readMore.init" event using mw#track
* The Minerva renderer subscribes to the event and, when it's fired,
renders the data by passing it to the WatchstarPageList view
Bug: T113635
Change-Id: I651342bdf9796938fa7051828dd13bc6fe774783
2015-10-07 13:12:42 +00:00
|
|
|
|
2016-08-30 18:01:48 +00:00
|
|
|
if ( mw.viewport.isElementCloseToViewport( readMore, scrollThreshold ) ) {
|
2015-12-05 17:44:15 +00:00
|
|
|
$.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.
|
2017-02-23 22:02:50 +00:00
|
|
|
mw.loader.using( [
|
2017-06-06 23:38:57 +00:00
|
|
|
'ext.relatedArticles.cards',
|
2017-03-06 18:39:57 +00:00
|
|
|
'ext.relatedArticles.readMore'
|
2017-02-23 22:02:50 +00:00
|
|
|
] ),
|
2015-12-05 17:44:15 +00:00
|
|
|
relatedPages.getForCurrentPage( LIMIT )
|
2018-06-19 18:41:25 +00:00
|
|
|
).then( function ( _, pages ) {
|
2015-12-05 17:44:15 +00:00
|
|
|
if ( pages.length ) {
|
|
|
|
mw.track( 'ext.relatedArticles.init', pages );
|
2016-12-10 12:03:32 +00:00
|
|
|
} else {
|
2018-06-06 10:27:06 +00:00
|
|
|
$( readMore ).remove();
|
2015-12-05 17:44:15 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
// detach handler to stop subsequent loads on scroll
|
|
|
|
$window.off( 'scroll', debouncedLoad );
|
|
|
|
}
|
|
|
|
}
|
2015-12-29 02:55:43 +00:00
|
|
|
|
2018-02-21 16:13:33 +00:00
|
|
|
function showReadMore() {
|
2017-01-27 21:10:46 +00:00
|
|
|
// 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 ) {
|
2019-02-10 16:46:48 +00:00
|
|
|
$( '<div>' ).addClass( 'read-more-container' ).prependTo( '.footer-content' );
|
2017-01-27 21:10:46 +00:00
|
|
|
} else {
|
2019-02-10 16:46:48 +00:00
|
|
|
$( '<div>' ).addClass( 'read-more-container post-content' )
|
2017-01-27 21:10:46 +00:00
|
|
|
.insertAfter( '#content' );
|
|
|
|
}
|
2016-12-10 12:03:32 +00:00
|
|
|
|
2017-01-27 21:10:46 +00:00
|
|
|
// try related articles load on scroll
|
|
|
|
$window.on( 'scroll', debouncedLoad );
|
|
|
|
// try an initial load, in case of no scroll
|
|
|
|
loadRelatedArticles();
|
|
|
|
}
|
Add Related Articles section to Minerva
If the page has related articles, is in mainspace, isn't the main page,
and the output is being rendered with the MinervaBeta skin then a
"Related Articles" section is added to the page just before the footer.
Separate loading the information necessary to render the pages, choosing
the renderer, and rendering the data so that multiple skins - currently
Minerva and Vector per the mocks - not just multiple resolutions can all
be handled the same way:
* The bootstrap script (ext.relatedArticles.readMore.bootstrap/index.js)
for fetches the page image and Wikidata description; loading the
renderer module; and, finally, notifying the renderer module that it
should render the data, which it does by emitting
"ext.relatedArticles.readMore.init" event using mw#track
* The Minerva renderer subscribes to the event and, when it's fired,
renders the data by passing it to the WatchstarPageList view
Bug: T113635
Change-Id: I651342bdf9796938fa7051828dd13bc6fe774783
2015-10-07 13:12:42 +00:00
|
|
|
|
2018-09-13 18:40:42 +00:00
|
|
|
$( showReadMore );
|
2018-09-14 05:30:46 +00:00
|
|
|
}() );
|