2018-11-16 15:16:25 +00:00
|
|
|
( function ( M ) {
|
2019-02-04 15:48:07 +00:00
|
|
|
/** @typedef {Object.<number | 'all', IssueSummary[]>} IssueSummaryMap */
|
|
|
|
|
2019-02-07 16:34:18 +00:00
|
|
|
var Page = M.require( 'mobile.startup' ).Page,
|
2018-07-02 22:12:37 +00:00
|
|
|
KEYWORD_ALL_SECTIONS = 'all',
|
2018-07-11 15:16:21 +00:00
|
|
|
config = mw.config,
|
2018-07-02 22:12:37 +00:00
|
|
|
NS_MAIN = 0,
|
|
|
|
NS_TALK = 1,
|
|
|
|
NS_CATEGORY = 14,
|
2018-07-11 15:16:21 +00:00
|
|
|
CURRENT_NS = config.get( 'wgNamespaceNumber' ),
|
2018-11-12 20:32:28 +00:00
|
|
|
features = mw.config.get( 'wgMinervaFeatures', {} ),
|
2018-08-16 19:00:38 +00:00
|
|
|
pageIssuesParser = M.require( 'skins.minerva.scripts/pageIssuesParser' ),
|
2018-11-24 01:49:47 +00:00
|
|
|
pageIssuesOverlay = M.require( 'skins.minerva.scripts/pageIssuesOverlay' ),
|
2019-01-23 20:16:11 +00:00
|
|
|
pageIssueFormatter = M.require( 'skins.minerva.scripts/page-issues/page/pageIssueFormatter' ),
|
2018-11-12 20:32:28 +00:00
|
|
|
// When the query string flag is set force on new treatment.
|
|
|
|
// When wgMinervaPageIssuesNewTreatment is the default this line can be removed.
|
2018-09-19 22:21:32 +00:00
|
|
|
QUERY_STRING_FLAG = mw.util.getParamValue( 'minerva-issues' ),
|
2018-11-12 20:32:28 +00:00
|
|
|
newTreatmentEnabled = features.pageIssues || QUERY_STRING_FLAG;
|
2018-08-17 19:00:57 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
/**
|
|
|
|
* Render a banner in a containing element.
|
|
|
|
* if in group B, a learn more link will be append to any amboxes inside $container
|
|
|
|
* if in group A or control, any amboxes in container will be removed and a link "page issues"
|
|
|
|
* will be rendered above the heading.
|
|
|
|
* This function comes with side effects. It will populate a global "allIssues" object which
|
|
|
|
* will link section numbers to issues.
|
2018-07-26 10:13:33 +00:00
|
|
|
* @param {Page} page to search for page issues inside
|
2018-07-30 11:33:34 +00:00
|
|
|
* @param {string} labelText what the label of the page issues banner should say
|
2018-08-23 20:57:47 +00:00
|
|
|
* @param {string} section that the banner and its issues belong to.
|
2018-07-30 11:33:34 +00:00
|
|
|
* If string KEYWORD_ALL_SECTIONS banner should apply to entire page.
|
2018-09-13 15:33:20 +00:00
|
|
|
* @param {boolean} inline - if true the first ambox in the section will become the entry point
|
|
|
|
* for the issues overlay
|
2018-07-30 11:33:34 +00:00
|
|
|
* and if false, a link will be rendered under the heading.
|
2018-07-30 14:45:44 +00:00
|
|
|
* @param {OverlayManager} overlayManager
|
2018-07-30 11:33:34 +00:00
|
|
|
* @ignore
|
2018-07-30 14:35:54 +00:00
|
|
|
*
|
2019-02-04 16:22:09 +00:00
|
|
|
* @return {{ambox: JQuery.Object, issueSummaries: IssueSummary[]}}
|
2018-07-30 11:33:34 +00:00
|
|
|
*/
|
2019-01-23 20:16:11 +00:00
|
|
|
function insertBannersOrNotice( page, labelText, section, inline, overlayManager ) {
|
2018-12-06 11:32:59 +00:00
|
|
|
var
|
|
|
|
$metadata,
|
2018-07-30 11:33:34 +00:00
|
|
|
issueUrl = section === KEYWORD_ALL_SECTIONS ? '#/issues/' + KEYWORD_ALL_SECTIONS : '#/issues/' + section,
|
2019-02-05 22:00:43 +00:00
|
|
|
selector = [ '.ambox', '.tmbox', '.cmbox', '.fmbox' ].join( ',' ),
|
2018-12-06 11:32:59 +00:00
|
|
|
issueSummaries = [];
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-26 10:13:33 +00:00
|
|
|
if ( section === KEYWORD_ALL_SECTIONS ) {
|
|
|
|
$metadata = page.$( selector );
|
|
|
|
} else {
|
|
|
|
// find heading associated with the section
|
|
|
|
$metadata = page.findChildInSectionLead( parseInt( section, 10 ), selector );
|
|
|
|
}
|
2018-07-30 11:33:34 +00:00
|
|
|
// clean it up a little
|
|
|
|
$metadata.find( '.NavFrame' ).remove();
|
|
|
|
$metadata.each( function () {
|
2018-12-06 11:32:59 +00:00
|
|
|
var issueSummary,
|
2018-07-30 11:33:34 +00:00
|
|
|
$this = $( this );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
if ( $this.find( selector ).length === 0 ) {
|
2018-12-06 11:32:59 +00:00
|
|
|
issueSummary = pageIssuesParser.extract( $this );
|
2018-10-16 23:02:22 +00:00
|
|
|
// Some issues after "extract" has been run will have no text.
|
2018-07-30 11:33:34 +00:00
|
|
|
// For example in Template:Talk header the table will be removed and no issue found.
|
|
|
|
// These should not be rendered.
|
2018-12-06 11:32:59 +00:00
|
|
|
if ( issueSummary.text ) {
|
|
|
|
issueSummaries.push( issueSummary );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
2018-07-30 11:33:34 +00:00
|
|
|
}
|
|
|
|
} );
|
2018-12-06 11:32:59 +00:00
|
|
|
|
|
|
|
if ( inline ) {
|
|
|
|
issueSummaries.forEach( function ( issueSummary, i ) {
|
|
|
|
var isGrouped = issueSummary.issue.grouped,
|
2018-11-27 12:29:39 +00:00
|
|
|
lastIssueIsGrouped = issueSummaries[ i - 1 ] &&
|
2019-01-23 20:16:11 +00:00
|
|
|
issueSummaries[ i - 1 ].issue.grouped,
|
|
|
|
multiple = isGrouped && !lastIssueIsGrouped;
|
2018-12-06 11:32:59 +00:00
|
|
|
// only render the first grouped issue of each group
|
2019-02-04 15:48:07 +00:00
|
|
|
pageIssueFormatter.insertPageIssueBanner(
|
|
|
|
issueSummary,
|
|
|
|
mw.msg( 'skin-minerva-issue-learn-more' ),
|
|
|
|
issueUrl,
|
|
|
|
overlayManager,
|
|
|
|
multiple
|
|
|
|
);
|
2018-07-30 11:33:34 +00:00
|
|
|
} );
|
2018-12-06 11:32:59 +00:00
|
|
|
} else if ( issueSummaries.length ) {
|
2019-01-23 20:16:11 +00:00
|
|
|
pageIssueFormatter.insertPageIssueNotice( labelText, section );
|
2018-07-02 22:12:37 +00:00
|
|
|
}
|
2018-07-30 14:35:54 +00:00
|
|
|
|
2019-02-04 16:22:09 +00:00
|
|
|
return {
|
|
|
|
ambox: $metadata,
|
|
|
|
issueSummaries: issueSummaries
|
|
|
|
};
|
2018-07-30 11:33:34 +00:00
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
/**
|
|
|
|
* Obtains the list of issues for the current page and provided section
|
2019-02-04 16:22:09 +00:00
|
|
|
* @param {IssueSummaryMap} allIssues mapping section {number} to {IssueSummary}
|
2018-09-13 15:33:20 +00:00
|
|
|
* @param {number|string} section either KEYWORD_ALL_SECTIONS or a number relating to the
|
|
|
|
* section the issues belong to
|
2018-07-30 11:33:34 +00:00
|
|
|
* @return {jQuery.Object[]} array of all issues.
|
|
|
|
*/
|
2019-02-04 16:22:09 +00:00
|
|
|
function getIssues( allIssues, section ) {
|
2018-08-31 22:48:34 +00:00
|
|
|
if ( section !== KEYWORD_ALL_SECTIONS ) {
|
2018-11-27 12:29:39 +00:00
|
|
|
return allIssues[ section ] || [];
|
2018-07-02 22:12:37 +00:00
|
|
|
}
|
2018-08-31 22:48:34 +00:00
|
|
|
// Note section.all may not exist, depending on the structure of the HTML page.
|
|
|
|
// It will only exist when Minerva has been run in desktop mode.
|
|
|
|
// If it's absent, we'll reduce all the other lists into one.
|
2019-02-04 15:48:07 +00:00
|
|
|
return allIssues[ KEYWORD_ALL_SECTIONS ] || Object.keys( allIssues ).reduce(
|
2018-08-31 22:48:34 +00:00
|
|
|
function ( all, key ) {
|
2018-11-27 12:29:39 +00:00
|
|
|
return all.concat( allIssues[ key ] );
|
2018-08-31 22:48:34 +00:00
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
2018-07-30 11:33:34 +00:00
|
|
|
}
|
2018-07-02 22:12:37 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
/**
|
|
|
|
* Scan an element for any known cleanup templates and replace them with a button
|
|
|
|
* that opens them in a mobile friendly overlay.
|
|
|
|
* @ignore
|
2018-07-30 14:45:44 +00:00
|
|
|
* @param {OverlayManager} overlayManager
|
|
|
|
* @param {Page} page
|
2018-07-30 11:33:34 +00:00
|
|
|
*/
|
2018-07-30 14:45:44 +00:00
|
|
|
function initPageIssues( overlayManager, page ) {
|
2019-02-04 16:22:09 +00:00
|
|
|
var
|
|
|
|
section,
|
|
|
|
/** @type {IssueSummary[]} */
|
|
|
|
issueSummaries = [],
|
|
|
|
/** @type {IssueSummaryMap} */
|
|
|
|
allIssues = {},
|
|
|
|
label,
|
2018-07-30 11:33:34 +00:00
|
|
|
$lead = page.getLeadSectionElement(),
|
2018-08-16 18:43:28 +00:00
|
|
|
issueOverlayShowAll = CURRENT_NS === NS_CATEGORY || CURRENT_NS === NS_TALK || !$lead,
|
2018-07-26 10:13:33 +00:00
|
|
|
inline = newTreatmentEnabled && CURRENT_NS === 0;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
// set A-B test class.
|
2018-11-12 20:32:28 +00:00
|
|
|
// When wgMinervaPageIssuesNewTreatment is the default this can be removed.
|
|
|
|
if ( newTreatmentEnabled ) {
|
2019-01-09 13:57:26 +00:00
|
|
|
// eslint-disable-next-line jquery/no-global-selector
|
2018-11-12 20:32:28 +00:00
|
|
|
$( 'html' ).addClass( 'issues-group-B' );
|
|
|
|
}
|
2018-07-02 22:12:37 +00:00
|
|
|
|
2018-08-16 18:43:28 +00:00
|
|
|
if ( CURRENT_NS === NS_TALK || CURRENT_NS === NS_CATEGORY ) {
|
2019-02-04 16:22:09 +00:00
|
|
|
section = KEYWORD_ALL_SECTIONS;
|
2018-07-30 11:33:34 +00:00
|
|
|
// e.g. Template:English variant category; Template:WikiProject
|
2019-02-04 16:22:09 +00:00
|
|
|
issueSummaries = insertBannersOrNotice( page, mw.msg( 'mobile-frontend-meta-data-issues-header-talk' ),
|
|
|
|
section, inline, overlayManager ).issueSummaries;
|
|
|
|
allIssues[ section ] = issueSummaries;
|
2018-07-11 15:16:21 +00:00
|
|
|
} else if ( CURRENT_NS === NS_MAIN ) {
|
2018-07-30 11:33:34 +00:00
|
|
|
label = mw.msg( 'mobile-frontend-meta-data-issues-header' );
|
|
|
|
if ( issueOverlayShowAll ) {
|
2019-02-04 16:22:09 +00:00
|
|
|
section = KEYWORD_ALL_SECTIONS;
|
|
|
|
issueSummaries = insertBannersOrNotice(
|
|
|
|
page, label, section, inline, overlayManager
|
|
|
|
).issueSummaries;
|
|
|
|
allIssues[ section ] = issueSummaries;
|
2018-07-30 11:33:34 +00:00
|
|
|
} else {
|
|
|
|
// parse lead
|
2019-02-04 16:22:09 +00:00
|
|
|
section = '0';
|
|
|
|
issueSummaries = insertBannersOrNotice(
|
|
|
|
page, label, section, inline, overlayManager
|
|
|
|
).issueSummaries;
|
|
|
|
allIssues[ section ] = issueSummaries;
|
2018-08-13 21:54:24 +00:00
|
|
|
if ( newTreatmentEnabled ) {
|
2018-09-13 15:33:20 +00:00
|
|
|
// parse other sections but only in group B. In treatment A no issues are shown
|
|
|
|
// for sections.
|
2018-07-26 10:13:33 +00:00
|
|
|
page.$( Page.HEADING_SELECTOR ).each( function ( i, headingEl ) {
|
2018-07-30 11:33:34 +00:00
|
|
|
var $headingEl = $( headingEl ),
|
|
|
|
sectionNum = $headingEl.find( '.edit-page' ).data( 'section' );
|
2018-07-13 20:23:55 +00:00
|
|
|
|
2018-09-13 15:33:20 +00:00
|
|
|
// Note certain headings matched using Page.HEADING_SELECTOR may not be
|
|
|
|
// headings and will not have a edit link. E.g. table of contents.
|
2018-08-23 20:57:47 +00:00
|
|
|
if ( sectionNum ) {
|
2018-09-13 15:33:20 +00:00
|
|
|
// Render banner for sectionNum associated with headingEl inside
|
|
|
|
// Page
|
2019-02-04 16:22:09 +00:00
|
|
|
section = sectionNum.toString();
|
|
|
|
issueSummaries = insertBannersOrNotice(
|
|
|
|
page, label, section, inline, overlayManager
|
|
|
|
).issueSummaries;
|
|
|
|
allIssues[ section ] = issueSummaries;
|
2018-08-23 20:57:47 +00:00
|
|
|
}
|
2018-07-30 11:33:34 +00:00
|
|
|
} );
|
2018-07-02 22:12:37 +00:00
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
2018-07-30 11:33:34 +00:00
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-30 11:33:34 +00:00
|
|
|
// Setup the overlay route.
|
|
|
|
overlayManager.add( new RegExp( '^/issues/(\\d+|' + KEYWORD_ALL_SECTIONS + ')$' ), function ( section ) {
|
2018-11-24 01:49:47 +00:00
|
|
|
return pageIssuesOverlay(
|
2019-02-04 16:22:09 +00:00
|
|
|
getIssues( allIssues, section ), section, CURRENT_NS
|
2018-11-24 01:49:47 +00:00
|
|
|
);
|
2018-07-30 11:33:34 +00:00
|
|
|
} );
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-08-16 19:00:38 +00:00
|
|
|
M.define( 'skins.minerva.scripts/pageIssues', {
|
2018-07-30 14:35:54 +00:00
|
|
|
init: initPageIssues,
|
|
|
|
test: {
|
2019-01-23 20:16:11 +00:00
|
|
|
insertBannersOrNotice: insertBannersOrNotice
|
2018-07-30 14:35:54 +00:00
|
|
|
}
|
2018-07-30 14:45:44 +00:00
|
|
|
} );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-11-16 15:16:25 +00:00
|
|
|
}( mw.mobileFrontend ) );
|