2023-11-08 22:06:07 +00:00
|
|
|
// eslint-disable-next-line spaced-comment
|
|
|
|
/// <reference path="./codex.ts" />
|
|
|
|
const RelatedArticles = require( './RelatedArticles.js' );
|
|
|
|
const data = require( './data.json' );
|
|
|
|
const RelatedPagesGateway = require( './RelatedPagesGateway.js' );
|
|
|
|
const relatedPages = new RelatedPagesGateway(
|
|
|
|
new mw.Api( {
|
|
|
|
ajax: {
|
|
|
|
url: data.searchUrl
|
|
|
|
}
|
|
|
|
} ),
|
|
|
|
mw.config.get( 'wgPageName' ),
|
|
|
|
mw.config.get( 'wgRelatedArticles' ),
|
|
|
|
data.useCirrusSearch,
|
|
|
|
data.onlyUseCirrusSearch,
|
|
|
|
data.descriptionSource
|
|
|
|
);
|
|
|
|
const LIMIT = mw.config.get( 'wgRelatedArticlesCardLimit', 3 );
|
|
|
|
|
2022-04-15 16:24:07 +00:00
|
|
|
/**
|
2023-11-08 22:06:07 +00:00
|
|
|
* Generates suggestion objects from pages
|
2022-04-15 16:24:07 +00:00
|
|
|
*
|
2023-11-08 22:06:07 +00:00
|
|
|
* @param {MwApiPageObject[]} pages
|
|
|
|
* @return {Codex.ListTitleObject[]}
|
2022-04-15 16:24:07 +00:00
|
|
|
*/
|
|
|
|
function getCards( pages ) {
|
|
|
|
return pages.map( function ( page ) {
|
Limit RelatedArticles feature to ES6 browsers
We currently require support for IntersectionObserver.
which is supported on Edge >= 15 (15 has partial support),
Firefox >55, Chrome >58, Safari 12.1, Opera >=38,
iOS Safari >=12.2, Android 100
Full ES6 is supported in Edge >=15, Firefox >=54, Chrome >=51,
Safari >=10, Opera >=38, iOS Safari >=10, so such a change
would only drop support for Edge 15 and Firefox 54.
CSS.escape is guaranteed in all these browsers according to
caniuse, with the only discrepancy being the Edge browser (versions
16-18) so it is also suggested we remove support for those browsers.
Firefox 54 accounts for 0.0026% of page views
Edge 15-18 accounts for 0.069% of page views
Bug: T306355
Change-Id: Id2987e3456607b610c38da9ee157a026d1d00ada
2022-04-18 22:20:04 +00:00
|
|
|
const result = {
|
2023-11-08 22:06:07 +00:00
|
|
|
id: page.title,
|
|
|
|
label: page.title,
|
2024-04-09 18:08:26 +00:00
|
|
|
url: mw.util.getUrl( page.title, {
|
|
|
|
wprov: 'rarw1'
|
|
|
|
} ),
|
2023-11-08 22:06:07 +00:00
|
|
|
thumbnail: page.thumbnail ? {
|
|
|
|
width: page.thumbnail.width,
|
|
|
|
height: page.thumbnail.height,
|
|
|
|
url: page.thumbnail.source
|
|
|
|
} : undefined,
|
|
|
|
description: ( page.description || page.extract ||
|
2022-04-15 16:24:07 +00:00
|
|
|
( page.pageprops ? page.pageprops.description : '' ) )
|
|
|
|
};
|
|
|
|
|
2023-11-08 22:06:07 +00:00
|
|
|
return result;
|
2022-04-15 16:24:07 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-08 22:06:07 +00:00
|
|
|
* @param {MwApiPageObject[]} pages
|
2022-04-15 16:24:07 +00:00
|
|
|
* @param {Element} el
|
2023-11-08 22:06:07 +00:00
|
|
|
* @param {string} heading
|
|
|
|
* @param {boolean} isContainerSmall
|
2022-04-15 16:24:07 +00:00
|
|
|
*/
|
2023-11-08 22:06:07 +00:00
|
|
|
function render( pages, el, heading, isContainerSmall ) {
|
|
|
|
el.innerHTML = RelatedArticles( {
|
|
|
|
isContainerSmall,
|
|
|
|
heading,
|
|
|
|
cards: getCards( pages )
|
|
|
|
} );
|
|
|
|
}
|
2022-04-15 16:24:07 +00:00
|
|
|
|
2023-11-08 22:06:07 +00:00
|
|
|
/**
|
|
|
|
* @param {HTMLElement} container to initialize into
|
|
|
|
*/
|
|
|
|
function init( container ) {
|
|
|
|
relatedPages.getForCurrentPage( LIMIT ).then( ( /** @type {MwApiPageObject[]} */ pages ) => {
|
|
|
|
if ( pages.length ) {
|
|
|
|
render(
|
|
|
|
pages,
|
|
|
|
container,
|
|
|
|
mw.msg( 'relatedarticles-read-more-heading' ),
|
|
|
|
// Take up multiple columns if possible
|
|
|
|
false
|
|
|
|
);
|
|
|
|
} else if ( container.parentNode ) {
|
|
|
|
container.parentNode.removeChild( container );
|
|
|
|
}
|
|
|
|
} );
|
2022-04-15 16:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
2023-11-08 22:06:07 +00:00
|
|
|
init,
|
Limit RelatedArticles feature to ES6 browsers
We currently require support for IntersectionObserver.
which is supported on Edge >= 15 (15 has partial support),
Firefox >55, Chrome >58, Safari 12.1, Opera >=38,
iOS Safari >=12.2, Android 100
Full ES6 is supported in Edge >=15, Firefox >=54, Chrome >=51,
Safari >=10, Opera >=38, iOS Safari >=10, so such a change
would only drop support for Edge 15 and Firefox 54.
CSS.escape is guaranteed in all these browsers according to
caniuse, with the only discrepancy being the Edge browser (versions
16-18) so it is also suggested we remove support for those browsers.
Firefox 54 accounts for 0.0026% of page views
Edge 15-18 accounts for 0.069% of page views
Bug: T306355
Change-Id: Id2987e3456607b610c38da9ee157a026d1d00ada
2022-04-18 22:20:04 +00:00
|
|
|
render,
|
2023-11-08 22:06:07 +00:00
|
|
|
getCards,
|
|
|
|
test: {
|
|
|
|
relatedPages
|
|
|
|
}
|
2022-04-15 16:24:07 +00:00
|
|
|
};
|