2017-07-12 15:12:40 +00:00
|
|
|
( function ( M, $ ) {
|
2018-05-08 22:03:52 +00:00
|
|
|
var AB = M.require( 'skins.minerva.scripts/AB' ),
|
2018-07-02 22:12:37 +00:00
|
|
|
allIssues = {},
|
|
|
|
KEYWORD_ALL_SECTIONS = 'all',
|
|
|
|
ACTION_EDIT = mw.config.get( 'wgAction' ) === 'edit',
|
|
|
|
NS_MAIN = 0,
|
|
|
|
NS_TALK = 1,
|
|
|
|
NS_CATEGORY = 14,
|
2018-05-08 22:03:52 +00:00
|
|
|
isInGroupB = new AB(
|
|
|
|
'WME.PageIssuesAB',
|
|
|
|
mw.config.get( 'wgMinervaABSamplingRate', 0 ),
|
|
|
|
mw.user.sessionId()
|
|
|
|
).getBucket() === 'B',
|
|
|
|
Icon = M.require( 'mobile.startup/Icon' );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
|
|
|
( function () {
|
2018-07-02 22:12:37 +00:00
|
|
|
var page = M.getCurrentPage(),
|
2018-03-15 00:23:18 +00:00
|
|
|
getIconFromAmbox = M.require( 'skins.minerva.scripts/utils' )
|
|
|
|
.getIconFromAmbox,
|
2017-08-25 15:17:03 +00:00
|
|
|
overlayManager = M.require( 'skins.minerva.scripts/overlayManager' ),
|
2017-07-12 15:12:40 +00:00
|
|
|
CleanupOverlay = M.require( 'mobile.issues/CleanupOverlay' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract a summary message from a cleanup template generated element that is
|
|
|
|
* friendly for mobile display.
|
2018-07-03 14:50:09 +00:00
|
|
|
* @param {object} $box element to extract the message from
|
2017-07-12 15:12:40 +00:00
|
|
|
* @ignore
|
2018-03-15 00:23:18 +00:00
|
|
|
* @typedef {object} IssueSummary
|
|
|
|
* @prop {string} icon HTML string.
|
|
|
|
* @prop {string} text HTML string.
|
|
|
|
* @return {IssueSummary}
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
function extractMessage( $box ) {
|
|
|
|
var selector = '.mbox-text, .ambox-text',
|
|
|
|
$container = $( '<div>' );
|
|
|
|
|
|
|
|
$box.find( selector ).each( function () {
|
|
|
|
var contents,
|
|
|
|
$this = $( this );
|
|
|
|
// Clean up talk page boxes
|
|
|
|
$this.find( 'table, .noprint' ).remove();
|
|
|
|
contents = $this.html();
|
|
|
|
|
|
|
|
if ( contents ) {
|
|
|
|
$( '<p>' ).html( contents ).appendTo( $container );
|
|
|
|
}
|
|
|
|
} );
|
2018-03-15 00:23:18 +00:00
|
|
|
return {
|
|
|
|
icon: getIconFromAmbox( $box ).toHtmlString(),
|
|
|
|
text: $container.html()
|
|
|
|
};
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a link element that opens the issues overlay.
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
*
|
|
|
|
* @param {string} labelText The text value of the element
|
2018-07-03 14:50:09 +00:00
|
|
|
* @return {JQuery}
|
2017-07-12 15:12:40 +00:00
|
|
|
*/
|
|
|
|
function createLinkElement( labelText ) {
|
|
|
|
return $( '<a class="cleanup mw-mf-cleanup"></a>' )
|
|
|
|
.text( labelText );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render a banner in a containing element.
|
2018-07-02 22:12:37 +00:00
|
|
|
* 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-03 14:50:09 +00:00
|
|
|
* @param {JQuery.Object} $container to render the page issues banner inside.
|
2017-07-12 15:12:40 +00:00
|
|
|
* @param {string} labelText what the label of the page issues banner should say
|
2018-07-02 22:12:37 +00:00
|
|
|
* @param {number|string} section that the banner and its issues belong to.
|
|
|
|
* If string KEYWORD_ALL_SECTIONS banner should apply to entire page.
|
2018-07-09 21:24:29 +00:00
|
|
|
* @param {boolean} inline - if true the first ambox in the section will become the entry point for the issues overlay
|
|
|
|
* and if false, a link will be rendered under the heading.
|
2017-07-12 15:12:40 +00:00
|
|
|
* @ignore
|
|
|
|
*/
|
2018-07-02 22:12:37 +00:00
|
|
|
function createBanner( $container, labelText, section, inline ) {
|
2018-05-08 22:03:52 +00:00
|
|
|
var $learnMore,
|
2018-07-02 22:12:37 +00:00
|
|
|
issueUrl = section === KEYWORD_ALL_SECTIONS ? '#/issues/' + KEYWORD_ALL_SECTIONS : '#/issues/' + section,
|
2018-05-08 22:03:52 +00:00
|
|
|
selector = 'table.ambox, table.tmbox, table.cmbox, table.fmbox',
|
2017-07-12 15:12:40 +00:00
|
|
|
$metadata = $container.find( selector ),
|
|
|
|
issues = [],
|
|
|
|
$link;
|
|
|
|
|
|
|
|
// clean it up a little
|
|
|
|
$metadata.find( '.NavFrame' ).remove();
|
|
|
|
$metadata.each( function () {
|
2018-03-15 00:23:18 +00:00
|
|
|
var issue,
|
2017-07-12 15:12:40 +00:00
|
|
|
$this = $( this );
|
|
|
|
|
|
|
|
if ( $this.find( selector ).length === 0 ) {
|
2018-03-15 00:23:18 +00:00
|
|
|
issue = extractMessage( $this );
|
2018-07-09 21:31:46 +00:00
|
|
|
// Some issues after "extractMessage" has been run will have no text.
|
|
|
|
// For example in Template:Talk header the table will be removed and no issue found.
|
|
|
|
// These should not be rendered.
|
|
|
|
if ( issue.text ) {
|
|
|
|
issues.push( issue );
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
} );
|
2018-07-02 22:12:37 +00:00
|
|
|
// store it for late
|
|
|
|
allIssues[section] = issues;
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-09 21:24:29 +00:00
|
|
|
if ( inline ) {
|
2018-05-08 22:03:52 +00:00
|
|
|
new Icon( {
|
|
|
|
glyphPrefix: 'minerva',
|
|
|
|
name: 'warning',
|
|
|
|
isSmall: true
|
2018-06-19 22:52:28 +00:00
|
|
|
} ).prependTo( $metadata.find( '.mbox-text' ) );
|
2018-05-08 22:03:52 +00:00
|
|
|
$learnMore = $( '<span>' )
|
|
|
|
.addClass( 'ambox-learn-more' )
|
|
|
|
.text( mw.msg( 'skin-minerva-issue-learn-more' ) );
|
|
|
|
if ( $( '.mw-collapsible-content' ).length ) {
|
|
|
|
// e.g. Template:Multiple issues
|
2018-06-19 22:52:28 +00:00
|
|
|
$learnMore.insertBefore( $metadata.find( '.mw-collapsible-content' ) );
|
2018-05-08 22:03:52 +00:00
|
|
|
} else {
|
|
|
|
// e.g. Template:merge from
|
2018-06-19 22:52:28 +00:00
|
|
|
$learnMore.appendTo( $metadata.find( '.mbox-text-span' ) );
|
2018-05-08 22:03:52 +00:00
|
|
|
}
|
2018-06-28 22:43:19 +00:00
|
|
|
$metadata.click( function () {
|
2018-07-02 22:12:37 +00:00
|
|
|
overlayManager.router.navigate( issueUrl );
|
2018-06-28 22:43:19 +00:00
|
|
|
return false;
|
2018-05-08 22:03:52 +00:00
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
$link = createLinkElement( labelText );
|
2018-07-02 22:12:37 +00:00
|
|
|
// In group B, we link to all issues no matter where the banner is.
|
|
|
|
$link.attr( 'href', '#/issues/' + KEYWORD_ALL_SECTIONS );
|
2018-05-08 22:03:52 +00:00
|
|
|
if ( $metadata.length ) {
|
|
|
|
$link.insertAfter( $( 'h1#section_0' ) );
|
|
|
|
$metadata.remove();
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 22:12:37 +00:00
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-07-02 22:12:37 +00:00
|
|
|
/**
|
|
|
|
* Obtains the list of issues for the current page and provided section
|
|
|
|
* @param {number|string} section either KEYWORD_ALL_SECTIONS or a number relating to the section
|
|
|
|
* the issues belong to
|
|
|
|
* @return {jQuery.Object[]} array of all issues.
|
|
|
|
*/
|
|
|
|
function getIssues( section ) {
|
|
|
|
if ( section === KEYWORD_ALL_SECTIONS ) {
|
|
|
|
// 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.
|
|
|
|
return section.all || Object.keys( allIssues ).reduce(
|
|
|
|
function ( all, key ) {
|
|
|
|
return all.concat( allIssues[key] );
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return allIssues[section] || [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain a suitable heading for the issues overlay based on the namespace
|
|
|
|
* @param {number} ns is the namespace to generate heading for
|
|
|
|
* @return {string} heading for overlay
|
|
|
|
*/
|
|
|
|
function getNamespaceHeadingText( ns ) {
|
|
|
|
switch ( ns ) {
|
|
|
|
case NS_CATEGORY:
|
|
|
|
return mw.msg( 'mobile-frontend-meta-data-issues-categories' );
|
|
|
|
case NS_TALK:
|
|
|
|
return mw.msg( 'mobile-frontend-meta-data-issues-talk' );
|
|
|
|
case NS_MAIN:
|
|
|
|
return mw.msg( 'mobile-frontend-meta-data-issues' );
|
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
2017-07-12 15:12:40 +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
|
|
|
|
*/
|
|
|
|
function initPageIssues() {
|
|
|
|
var ns = mw.config.get( 'wgNamespaceNumber' ),
|
2018-07-02 22:12:37 +00:00
|
|
|
label,
|
|
|
|
headingText = ACTION_EDIT ? mw.msg( 'edithelp' ) : getNamespaceHeadingText( ns ),
|
|
|
|
$lead = page.getLeadSectionElement(),
|
|
|
|
issueOverlayShowAll = ns === NS_CATEGORY || ns === NS_TALK || ACTION_EDIT || !$lead,
|
2018-07-09 21:24:29 +00:00
|
|
|
inline = isInGroupB && ns === 0,
|
2018-07-02 22:12:37 +00:00
|
|
|
$container = $( '#bodyContent' );
|
2017-07-12 15:12:40 +00:00
|
|
|
|
2018-05-08 22:03:52 +00:00
|
|
|
// set A-B test class.
|
|
|
|
$( 'html' ).addClass( isInGroupB ? 'issues-group-B' : 'issues-group-A' );
|
2018-07-02 22:12:37 +00:00
|
|
|
|
|
|
|
if ( ACTION_EDIT ) {
|
|
|
|
// Editor uses different parent element
|
2017-08-25 15:17:03 +00:00
|
|
|
$container = $( '#mw-content-text' );
|
2018-07-02 22:12:37 +00:00
|
|
|
createBanner( $container, mw.msg( 'edithelp' ), KEYWORD_ALL_SECTIONS, inline );
|
|
|
|
} else if ( ns === NS_TALK || ns === NS_CATEGORY ) {
|
|
|
|
// e.g. Template:English variant category; Template:WikiProject
|
|
|
|
createBanner( $container, mw.msg( 'mobile-frontend-meta-data-issues-header-talk' ), KEYWORD_ALL_SECTIONS, inline );
|
|
|
|
} else if ( ns === NS_MAIN ) {
|
|
|
|
label = mw.msg( 'mobile-frontend-meta-data-issues-header' );
|
|
|
|
if ( issueOverlayShowAll ) {
|
|
|
|
createBanner( $container, label, KEYWORD_ALL_SECTIONS, inline );
|
|
|
|
} else {
|
|
|
|
// parse lead
|
|
|
|
createBanner( $lead, label, 0, inline );
|
|
|
|
if ( isInGroupB ) {
|
|
|
|
// parse other sections but only in group B. In treatment A no issues are shown for sections.
|
|
|
|
$lead.nextAll( '[class^="mf-section"]' ).each( function ( i, sectionEl ) {
|
|
|
|
createBanner( $( sectionEl ), label, i + 1, inline );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 22:12:37 +00:00
|
|
|
// Setup the overlay route.
|
|
|
|
overlayManager.add( new RegExp( '^/issues/(\\d+|' + KEYWORD_ALL_SECTIONS + ')$' ), function ( section ) {
|
|
|
|
return new CleanupOverlay( {
|
|
|
|
issues: getIssues( section ),
|
2018-07-11 21:13:19 +00:00
|
|
|
// Note only the main namespace is expected to make use of section issues, so the heading will always be
|
|
|
|
// minerva-meta-data-issues-section-header regardless of namespace
|
|
|
|
headingText: section === '0' || section === KEYWORD_ALL_SECTIONS ? headingText :
|
|
|
|
mw.msg( 'minerva-meta-data-issues-section-header' )
|
2018-07-02 22:12:37 +00:00
|
|
|
} );
|
|
|
|
} );
|
2017-07-12 15:12:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the issues banner on the page
|
2018-02-07 22:33:19 +00:00
|
|
|
// Pages which dont exist (id 0) cannot have issues
|
|
|
|
if ( !page.isMissing ) {
|
|
|
|
initPageIssues();
|
|
|
|
}
|
2017-07-12 15:12:40 +00:00
|
|
|
}() );
|
|
|
|
|
|
|
|
}( mw.mobileFrontend, jQuery ) );
|