2015-12-07 18:19:16 +00:00
|
|
|
( function ( $, mw ) {
|
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-09-15 22:18:45 +00:00
|
|
|
var relatedPages = new mw.relatedPages.RelatedPagesGateway(
|
2015-11-11 15:30:19 +00:00
|
|
|
new mw.Api(),
|
|
|
|
mw.config.get( 'wgPageName' ),
|
|
|
|
mw.config.get( 'wgRelatedArticles' ),
|
|
|
|
mw.config.get( 'wgRelatedArticlesUseCirrusSearch' ),
|
|
|
|
mw.config.get( 'wgRelatedArticlesOnlyUseCirrusSearch' )
|
|
|
|
),
|
2017-05-25 18:02:33 +00:00
|
|
|
LIMIT = mw.config.get( 'wgRelatedArticlesCardLimit' ),
|
2015-12-05 17:44:15 +00:00
|
|
|
debouncedLoad = $.debounce( 100, function () {
|
2017-04-07 20:21:12 +00:00
|
|
|
loadRelatedArticles(); // eslint-disable-line
|
2015-12-05 17:44:15 +00:00
|
|
|
} ),
|
2017-03-01 21:36:49 +00:00
|
|
|
$window = $( window ),
|
|
|
|
shouldShowReadMore;
|
2015-12-14 10:34:17 +00:00
|
|
|
|
2017-01-27 21:10:46 +00:00
|
|
|
/**
|
|
|
|
* Is RelatedArticles extension enabled for current user
|
|
|
|
*
|
2017-02-07 00:53:52 +00:00
|
|
|
* Returns true if the user opted into the beta feature, otherwise
|
|
|
|
* user's session ID is used to determine the eligibility for RelatedArticles functionality,
|
|
|
|
* based on the value of wgRelatedArticlesEnabledSamplingRate
|
2017-01-27 21:10:46 +00:00
|
|
|
* thus the function will result the same outcome as long as the browser
|
|
|
|
* hasn't been restarted or the cookie hasn't been cleared.
|
|
|
|
*
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isEnabledForCurrentUser() {
|
|
|
|
var bucket,
|
2017-02-07 00:53:52 +00:00
|
|
|
samplingRate = mw.config.get( 'wgRelatedArticlesEnabledSamplingRate', 1 );
|
|
|
|
|
2017-01-27 21:10:46 +00:00
|
|
|
bucket = mw.experiments.getBucket( {
|
|
|
|
name: 'ext.relatedArticles.visibility',
|
|
|
|
enabled: true,
|
|
|
|
buckets: {
|
|
|
|
control: 1 - samplingRate,
|
|
|
|
A: samplingRate
|
|
|
|
}
|
|
|
|
}, mw.user.sessionId() );
|
|
|
|
return bucket === 'A';
|
|
|
|
}
|
|
|
|
|
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 )
|
|
|
|
).done( function ( _, pages ) {
|
|
|
|
if ( pages.length ) {
|
|
|
|
mw.track( 'ext.relatedArticles.init', pages );
|
2016-12-10 12:03:32 +00:00
|
|
|
} else {
|
|
|
|
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
|
|
|
|
2017-03-01 21:36:49 +00:00
|
|
|
shouldShowReadMore = isEnabledForCurrentUser();
|
|
|
|
|
|
|
|
if ( shouldShowReadMore ) {
|
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 ) {
|
|
|
|
$( '<div class="read-more-container" />' ).prependTo( '.footer-content' );
|
|
|
|
} else {
|
|
|
|
$( '<div class="read-more-container post-content" />' )
|
|
|
|
.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
|
|
|
|
2017-03-06 18:39:57 +00:00
|
|
|
mw.track( 'ext.relatedArticles.logEnabled', { isEnabled: shouldShowReadMore } );
|
2015-12-07 18:19:16 +00:00
|
|
|
}( jQuery, mediaWiki ) );
|