Debian Codegraph @

skins/MinervaNeue/resources/skins.minerva.scripts/page-issues/index.js

raw symbols SCIP
1 /**
2 * @typedef {Object.<string, IssueSummary[]>} IssueSummaryMap
3 * @ignore
4 */
5
6 const PageHTMLParser = require( 'mobile.startup' ).PageHTMLParser;
7 const KEYWORD_ALL_SECTIONS = 'all';
8 const namespaceIds = mw.config.get( 'wgNamespaceIds' );
9 const NS_MAIN = namespaceIds[ '' ];
10 const NS_CATEGORY = namespaceIds.category;
11 const CURRENT_NS = mw.config.get( 'wgNamespaceNumber' );
12 const features = mw.config.get( 'wgMinervaFeatures', {} );
13 const pageIssuesParser = require( './parser.js' );
14 const pageIssuesOverlay = require( './overlay/pageIssuesOverlay.js' );
15 const pageIssueFormatter = require( './page/pageIssueFormatter.js' );
16 // When the query string flag is set force on new treatment.
17 // When wgMinervaPageIssuesNewTreatment is the default this line can be removed.
18 const QUERY_STRING_FLAG = mw.util.getParamValue( 'minerva-issues' );
19 const newTreatmentEnabled = features.pageIssues || QUERY_STRING_FLAG;
20
21 /**
22 * Render a banner in a containing element.
23 * if in group B, a learn more link will be append to any amboxes inside $container
24 * if in group A or control, any amboxes in container will be removed and a link "page issues"
25 * will be rendered above the heading.
26 * This function comes with side effects. It will populate a global "allIssues" object which
27 * will link section numbers to issues.
28 *
29 * @param {PageHTMLParser} pageHTMLParser parser to search for page issues
30 * @param {string} labelText what the label of the page issues banner should say
31 * @param {string} section that the banner and its issues belong to.
32 * If string KEYWORD_ALL_SECTIONS banner should apply to entire page.
33 * @param {boolean} inline - if true the first ambox in the section will become the entry point
34 * for the issues overlay
35 * and if false, a link will be rendered under the heading.
36 * @param {OverlayManager} overlayManager
37 * @ignore
38 *
39 * @return {{ambox: jQuery, issueSummaries: IssueSummary[]}}
40 */
41 function insertBannersOrNotice( pageHTMLParser, labelText, section, inline, overlayManager ) {
42 const issueUrl = section === KEYWORD_ALL_SECTIONS ? '#/issues/' + KEYWORD_ALL_SECTIONS : '#/issues/' + section;
43 const selector = [ '.ambox', '.tmbox', '.cmbox', '.fmbox' ].join( ',' );
44 const issueSummaries = [];
45
46 const $metadata = section === KEYWORD_ALL_SECTIONS ?
47 pageHTMLParser.$el.find( selector ) :
48 // find heading associated with the section
49 pageHTMLParser.findChildInSectionLead( parseInt( section, 10 ), selector );
50 // clean it up a little
51 $metadata.find( '.NavFrame' ).remove();
52 $metadata.each( ( _i, el ) => {
53 const $el = $( el );
54
55 if ( $el.find( selector ).length === 0 ) {
56 const issueSummary = pageIssuesParser.extract( $el );
57 // Some issues after "extract" has been run will have no text.
58 // For example in Template:Talk header the table will be removed and no issue found.
59 // These should not be rendered.
60 if ( issueSummary.text ) {
61 issueSummaries.push( issueSummary );
62 }
63 }
64 } );
65
66 if ( inline ) {
67 issueSummaries.forEach( ( issueSummary, i ) => {
68 const isGrouped = issueSummary.issue.grouped;
69 const lastIssueIsGrouped = issueSummaries[ i - 1 ] &&
70 issueSummaries[ i - 1 ].issue.grouped;
71 const multiple = isGrouped && !lastIssueIsGrouped;
72 // only render the first grouped issue of each group
73 pageIssueFormatter.insertPageIssueBanner(
74 issueSummary,
75 mw.msg( 'skin-minerva-issue-learn-more' ),
76 issueUrl,
77 overlayManager,
78 multiple
79 );
80 } );
81 } else if ( issueSummaries.length ) {
82 pageIssueFormatter.insertPageIssueNotice( labelText, section );
83 }
84
85 return {
86 ambox: $metadata,
87 issueSummaries: issueSummaries
88 };
89 }
90
91 /**
92 * Obtains the list of issues for the current page and provided section
93 *
94 * @ignore
95 * @param {IssueSummaryMap} allIssues Mapping section {number}
96 * to {IssueSummary}
97 * @param {number|string} section either KEYWORD_ALL_SECTIONS or a number relating to the
98 * section the issues belong to
99 * @return {jQuery[]} array of all issues.
100 */
101 function getIssues( allIssues, section ) {
102 if ( section !== KEYWORD_ALL_SECTIONS ) {
103 return allIssues[ section ] || [];
104 }
105 // Note section.all may not exist, depending on the structure of the HTML page.
106 // It will only exist when Minerva has been run in desktop mode.
107 // If it's absent, we'll reduce all the other lists into one.
108 return allIssues[ KEYWORD_ALL_SECTIONS ] || Object.keys( allIssues ).reduce(
109 ( all, key ) => all.concat( allIssues[ key ] ),
110 []
111 );
112 }
113
114 /**
115 * Scan an element for any known cleanup templates and replace them with a button
116 * that opens them in a mobile friendly overlay.
117 *
118 * @ignore
119 * @param {OverlayManager} overlayManager
120 * @param {PageHTMLParser} pageHTMLParser
121 */
122 function initPageIssues( overlayManager, pageHTMLParser ) {
123 let section;
124 let issueSummaries = [];
125 const allIssues = {};
126 const $lead = pageHTMLParser.getLeadSectionElement();
127 const issueOverlayShowAll = CURRENT_NS === NS_CATEGORY || !$lead;
128 const inline = newTreatmentEnabled && CURRENT_NS === NS_MAIN;
129
130 // set A-B test class.
131 // When wgMinervaPageIssuesNewTreatment is the default this can be removed.
132 if ( newTreatmentEnabled ) {
133 $( document.documentElement ).addClass( 'issues-group-B' );
134 }
135
136 if ( CURRENT_NS === NS_CATEGORY ) {
137 section = KEYWORD_ALL_SECTIONS;
138 // e.g. Template:English variant category; Template:WikiProject
139 issueSummaries = insertBannersOrNotice( pageHTMLParser, mw.msg( 'mobile-frontend-meta-data-issues-header' ),
140 section, inline, overlayManager ).issueSummaries;
141 allIssues[ section ] = issueSummaries;
142 } else if ( CURRENT_NS === NS_MAIN ) {
143 const label = mw.msg( 'mobile-frontend-meta-data-issues-header' );
144 if ( issueOverlayShowAll ) {
145 section = KEYWORD_ALL_SECTIONS;
146 issueSummaries = insertBannersOrNotice(
147 pageHTMLParser, label, section, inline, overlayManager
148 ).issueSummaries;
149 allIssues[ section ] = issueSummaries;
150 } else {
151 // parse lead
152 section = '0';
153 issueSummaries = insertBannersOrNotice(
154 pageHTMLParser, label, section, inline, overlayManager
155 ).issueSummaries;
156 allIssues[ section ] = issueSummaries;
157 if ( newTreatmentEnabled ) {
158 // parse other sections but only in group B. In treatment A no issues are shown
159 // for sections.
160 pageHTMLParser.$el.find( PageHTMLParser.HEADING_SELECTOR ).each(
161 ( i, headingEl ) => {
162 const $headingEl = $( headingEl );
163 // section number is absent on protected pages, when this is the case
164 // use i, otherwise icon will not show (T340910)
165 const sectionNum = $headingEl.find( '.edit-page' ).data( 'section' ) || i;
166
167 // Note certain headings matched using
168 // PageHTMLParser.HEADING_SELECTOR may not be headings and will
169 // not have a edit link. E.g. table of contents.
170 if ( sectionNum ) {
171 // Render banner for sectionNum associated with headingEl inside
172 // Page
173 section = sectionNum.toString();
174 issueSummaries = insertBannersOrNotice(
175 pageHTMLParser, label, section, inline, overlayManager
176 ).issueSummaries;
177 allIssues[ section ] = issueSummaries;
178 }
179 }
180 );
181 }
182 }
183 }
184
185 // Setup the overlay route.
186 overlayManager.add( new RegExp( '^/issues/(\\d+|' + KEYWORD_ALL_SECTIONS + ')$' ), ( s ) => pageIssuesOverlay(
187 getIssues( allIssues, s ), s, CURRENT_NS
188 ) );
189 }
190
191 module.exports = {
192 init: initPageIssues,
193 test: {
194 insertBannersOrNotice: insertBannersOrNotice
195 }
196 };