User:Gary/nominations viewer.js: Difference between revisions

Content deleted Content added
f
Improve vote-matching
Line 102:
}
 
/**
* Call the Wikipedia API with params then run a function on the return data.
*
* @param {Object} params The params to pass to the Wikipedia API.
* @param {Function} callback The function to run with the return data.
* @returns {undefined}
*/
function addNomData(params, callback) {
$.getJSON(mw.util.wikiScript('api'), {
Line 111 ⟶ 118:
}
 
/**
* Add all data to a nomination.
*
* @param {string} pageName The page name.
* @returns {undefined}
*/
function addAllNomData(pageName) {
// Participants, age. Get all the edits for this nomination.
Line 139 ⟶ 152:
}
 
/**
// Add data to a nomination.
* Add data to a nomination.
*
* @param {Object} options Options
* @param {string} options.pageName The page name to which to add this data.
* @param {string} options.data The data to add.
* @param {string} options.id The ID of the field to add to.
* @param {string} options.hoverText Data that appears on hover.
* @returns {undefined}
*/
function addNewNomData({ pageName, data, id, hoverText }) {
if (!data) {
Line 164 ⟶ 186:
'<span>(<span>'
);
const matchArchiveNumber = pageName.match(/([0-9]\d+)$/);
 
const conditions = matchArchiveNumber && matchArchiveNumber[1] > 1;
Line 212 ⟶ 234:
* Replace a nomination with a new and improved one.
*
* @param {Object} options Options.
* @param {jQuery} options.$h3 The h3 heading of the nomination.
* @param {number} options.index The index of the nomination among the
Line 224 ⟶ 246:
 
// There are no edit links.
if (!$editLinks.length === 0) {
return;
}
Line 241 ⟶ 263:
// to the review page directly. So, skip this nomination.
if (
!$filteredEditLinks.length === 0 ||
!$filteredEditLinks.eq(0).attr('href') ||
!$filteredEditLinks
Line 338 ⟶ 360:
// Move the node, if it isn't a text node
if (nomNextSibling.nodeType !== 3) {
// eslint-disable-next-line unicorn/prefer-node-append
hiddenNode.appendChild(nomNextSibling);
}
Line 412 ⟶ 435:
function collapseTable(event) {
event.preventDefault();
 
const tableIndex = event.data.index;
const collapseCaption = 'hide';
Line 418 ⟶ 442:
const $button = $(`#collapseButton${tableIndex}`);
const $table = $(`#collapsibleTable${tableIndex}`);
 
if (!$table.length || !$button.length) {
if ($table.length === 0 || $button.length === 0) {
return false;
}
 
const $rows = $table.find('> tbody > tr');
 
Line 476 ⟶ 502:
const { allH3Length } = event.data;
 
new Array(allH3Length).fill().forEach((value, index) => {
.filltoggleNom(index, action);
});
.forEach((value, index) => {
toggleNom(index, action);
});
}
 
Line 704 ⟶ 728:
}
 
/**
* Generate the patterns used to find vote text.
*
* @returns {Object} The patterns.
*/
function getVoteTextAndPatterns() {
// Look for text that is enclosed within bold text, or level-4 headings.
let supportText;
const wrapPattern = "('''|====)";
let supportPattern;
let opposeText;
let opposePattern;
 
// The amount of characters allowed between the vote text, and the wrapping
// patterns.
const voteBuffer = 25;
letconst basicPatternOpentextPattern = `'''(.{0,${voteBuffer}})?`;
let basicPatternClose = `(.{0,${voteBuffer}})?'''`;
 
let openPattern = `${wrapPattern}${textPattern}`;
if (nomType('nominations')) {
let closePattern = `${textPattern}${wrapPattern}`;
supportText = 'support';
opposeText = 'oppose';
 
let supportPatternsupportText = new RegExp('support';
let opposeText = 'oppose';
`${basicPatternOpen}support${basicPatternClose}`,
 
'gi'
if (nomType('reviews');) {
opposePattern = new RegExp(
`${basicPatternOpen}oppose${basicPatternClose}`,
'gi'
);
} else if (nomType('reviews')) {
supportText = 'keep';
opposeText = 'delist';
 
supportPattern = new RegExp(
`${basicPatternOpen}keep${basicPatternClose}`,
'gi'
);
opposePattern = new RegExp(
`${basicPatternOpen}delist${basicPatternClose}`,
'gi'
);
} else if (nomType('pictures')) {
supportTextopenPattern = "\\*(\\s)?'support''.*?";
opposeTextclosePattern = ".*?'oppose''";
}
 
return {
basicPatternOpen = "\\*(\\s)?'''.*?";
supportText,
basicPatternClose = ".*?'''";
supportPattern: new RegExp(
 
`${openPattern}${supportText}${closePattern}`,
supportPattern = new RegExp(
`${basicPatternOpen}Support${basicPatternClose}`,
'gi'
);,
opposeText,
opposePattern = new RegExp(
opposePattern: new RegExp(
`${basicPatternOpen}Oppose${basicPatternClose}`,
`${openPattern}${opposeText}${closePattern}`,
'gi'
);,
};
 
return { supportText, supportPattern, opposeText, opposePattern };
}
 
Line 769 ⟶ 781:
}
 
/**
* Add votes data to a nomination.
*
* @param {string} content The nomination's content.
* @param {string} pageName The page name.
* @returns {undefined}
*/
function addVotes(content, pageName) {
if (!dataIsEnabled('votes') || nomType('peer reviews')) {
Line 844 ⟶ 863:
}
 
/**
* Check if the data field is enabled.
*
* @param {string} dataName The name of the data field to look up.
* @returns {boolean} The data field is enabled, so we want to use it.
*/
function dataIsEnabled(dataName) {
return NominationsViewer.nominationData.some((data) => dataName === data);
Line 925 ⟶ 950:
}
 
/**
function nomType(type) {
* Check if the nomination type of the current nomination is the type
* specified. If no type is specified, then return the type of the current
* nomination. Possible types are: `nominations`, `peer reviews`, `pictures`,
* and `reviews`, as specified in `NominationsViewer.enabledPages`.
*
* @param {string} [type] The type to compare the current nomination with.
* @returns {boolean|string} The current nomination matches the type
* specified, or the type of the current nomination.
*/
function nomType(type = null) {
const pageName = mw.config.get('wgPageName').replace(/_/g, ' ');
const pageType = NominationsViewer.enabledPages[pageName];