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

Content deleted Content added
massive update
f
Line 1:
// Nominations Viewer
/*
//
Nominations Viewer
// Description: Compact nominations for [[WP:FAC]], [[WP:FAR]], [[WP:FLC]],
// [[WP:FLRC]], [[WP:FPC]], and [[WP:PR]].
// Documentation: [[Wikipedia:Nominations Viewer]]
//
// ===
//
// Begin settings
// ---
//
// Default:
//
// NominationsViewer =
// {
// 'enabledPages': ['Wikipedia:Featured article candidates', ...],
// 'nominationData': ['images', 'age', 'nominators', 'participants', 'votes'],
// }
const NominationsViewer = window.NominationsViewer
? window.NominationsViewer
: {};
 
if (NominationsViewer.enabledPages === null) {
Description: Compact nominations for [[WP:FAC]], [[WP:FAR]], [[WP:FLC]],
[[WP:FLRC]], [[WP:FPC]], and [[WP:PR]].
Documentation: [[Wikipedia:Nominations Viewer]]
 
===
 
Begin settings
---
 
Default:
 
NominationsViewer =
{
'enabledPages': ['Wikipedia:Featured article candidates', ...],
'nominationData': ['images', 'age', 'nominators', 'participants', 'votes'],
}
*/
const NominationsViewer =
window.NominationsViewer == null ? {} : window.NominationsViewer;
 
if (NominationsViewer.enabledPages == null) {
NominationsViewer.enabledPages = {
// 'User:Gary/Sandbox': 'nominations'
Line 38 ⟶ 37:
}
 
if (NominationsViewer.nominationData === null) {
NominationsViewer.nominationData = [
'images',
Line 47 ⟶ 46:
];
}
 
/*
/**
End settings
* End settings
*/
*/
 
class NominationsViewerClass {
/**
* Add empty nomination data holders for a nomination.
*
* @param {string} pageName Name of the nomination page.
* @param {jQuery} $parentNode Parent node containing the entire nomination.
* @param {Array} ids The ID names to create.
* @returns {jQuery} The new node we added.
*/
static addNominationData(pageName, $parentNode, ids) {
Line 71 ⟶ 73:
}
 
static addOverallNominationInformationaddAllNomInfo($headings) {
const data = { allH3Length: $headings.length };
 
Line 123 ⟶ 125:
 
// Age
return appendNominationData(
'NominationsViewerClass.firstRevisionCallback',
`action=query&prop=revisions&rvdir=newer&rvlimit=1&titles=${pageName}`,
Line 129 ⟶ 131:
}
 
static addNewNomData(pageName, data, id, abbrTitle, isFirstData) {
static appendGeneratedNominationData(
pageName,
data,
id,
abbrTitle,
isFirstData,
) {
if (!data) {
return false;
}
 
Line 150 ⟶ 146:
 
$newChild.append($abbr);
return $id.append($newChild);
}
 
/**
* Create the data that appears next to the nomination's listing.
*
* @param {string} pageName Page name of the nomination page.
* @returns {jQuery} The new node we added.
*/
static createData(pageName) {
Line 208 ⟶ 206:
/**
* Replace a nomination with a new and improved one.
*
* @param {jQuery} $h3 The h3 heading of the nomination.
* @param {numberObject} indexoptions The index of the nomination among the othersOptions.
* @param {jQuery} options.$h3 The h3 heading of the nomination.
* @param {number} options.index The index of the nomination among the others.
* @returns {undefined}
*/
static createNomination({ $h3, index }) {
Line 217 ⟶ 218:
// Skip it.
if (!$editLink.length) {
return true;
}
 
Line 251 ⟶ 252:
 
// Ask the API to add data to our placeholders
return this.appendAllNominationData(pageName);
}
 
Line 318 ⟶ 319:
// Check if enabled on this page
Object.keys(NominationsViewer.enabledPages).forEach((page) => {
// const type = NominationsViewer.enabledPages[page];
 
if (pageName === page.replace(/\s/g, '_')) {
currentPageIsEnabled = true;
} else if (pageName.indexOfstartsWith(page.replace(/\s/g, '_')) === 0) {
currentPageIsASubpage = true;
}
Line 328 ⟶ 327:
 
if (
currentPageIsEnabled === null ||
window.mw.config.get('wgAction') !== 'view' ||
window.location.href.indexOf('&oldid=') !== -1 ||
!(currentPageIsASubpage === null)
) {
return false;
Line 341 ⟶ 340:
const $parentNode = $('.mw-content-ltr');
const $h3s = $parentNode.find('h3');
this.addOverallNominationInformationaddAllNomInfo($h3s);
 
// loopLoop through each nomination
$h3s.each((index, element) =>
this.createNomination({
Line 370 ⟶ 369:
}
 
/*/ Helpers
Helpers
*/
static collapseTable(event) {
event.preventDefault();
Line 433 ⟶ 430:
let action = actionParam;
 
if (action === null) {
action = 'expand';
}
Line 449 ⟶ 446:
let action = actionParam;
 
if (action === null) {
action = '';
}
Line 468 ⟶ 465:
if (action === 'collapse') {
return toggleHideNom($node, $nomButton);
}

else if (action === 'expand') {
return toggleShowNom($node, $nomButton);
}
// These have to be separate from the above because they have a lower
 
// priority.
// These have to be separate from the above because they have a lower
} else if ($node.is(':visible')) {
// priority.
if ($node.is(':visible')) {
return toggleHideNom($node, $nomButton);
}

else if ($node.is(':hidden')) {
return toggleShowNom($node, $nomButton);
}
Line 487 ⟶ 490:
}
 
/*/ Callbacks
Callbacks
*/
static allRevisionsCallback(obj) {
const vars = this.formatJSON(obj);
Line 502 ⟶ 503:
 
vars.revisions.forEach((revision) => {
// revision = { 'revid', 'paretnid', 'user', 'timestamp', 'comment' }
if (!revision.user) {
return;
Line 525:
if (a[1] < b[1]) {
return 1;
}

else if (a[1] > b[1]) {
return -1;
}
 
return 0;
});
Line 533 ⟶ 536:
const usersArray2 = usersArray.map((user) => `${user[0]}: ${user[1]}`);
 
return this.appendGeneratedNominationDataaddNewNomData(
vars.pageName,
`${moreThan + userCount} ${this.pluralize('participant', userCount)}`,
Line 577 ⟶ 580:
});
 
this.appendGeneratedNominationDataaddNewNomData(
vars.pageName,
`${matches.length} ${this.pluralize('image', matches.length)}`,
Line 598 ⟶ 601:
);
 
// eslint-disable-next-line max-depth
if ($.isEmptyObject(listOfNominators)) {
listOfNominators = this.findNominators(content, /:<small>''.*/);
Line 623 ⟶ 627:
 
/* eslint-disable indent */
this.appendGeneratedNominationDataaddNewNomData(
vars.pageName,
allNominators.length > 0
Line 734 ⟶ 738:
 
const toolTip = supports + opposes;
return this.appendGeneratedNominationDataaddNewNomData(vars.pageName, dataText, 'votes', toolTip);
vars.pageName,
dataText,
'votes',
toolTip,
);
}
 
Line 747 ⟶ 746:
/**
* The callback after getting the first revision of a page.
*
* @param {Object} obj The response object from the Wikipedia API.
* @returns {undefined} Generated nomination data.
*/
static firstRevisionCallback(obj) {
Line 753 ⟶ 754:
 
if (!vars) {
return null;
}
 
Line 814 ⟶ 815:
}
 
return this.appendGeneratedNominationDataaddNewNomData(
vars.pageName,
timeAgo,
Line 821 ⟶ 822:
);
}
 
return null;
}
 
// Callback helpers
/*
Callback helpers
*/
static capitalize(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
Line 908 ⟶ 905:
const pageType = NominationsViewer.enabledPages[pageName];
 
if (type !== null) {
if (type === pageType) {
return true;
Line 937 ⟶ 934:
}
 
// Already ran?
if (window.NominationsViewerClass) {
return;
}
 
window.NominationsViewerClass = NominationsViewerClass;
NominationsViewerClass.init();
});