MediaWiki:Common.js: Difference between revisions
Appearance
Add auto-sorting leaderboard code |
Fixed code |
||
| Line 7: | Line 7: | ||
// Check if the table exists | // Check if the table exists | ||
if (!table) { | if (!table) { | ||
return; // Exit if the table doesn't exist | return; // Exit if the table doesn't exist | ||
} | } | ||
| Line 16: | Line 15: | ||
return row !== headerRow; // Filter out the header row | return row !== headerRow; // Filter out the header row | ||
}); | }); | ||
// Calculate and display total edits in the last column for each row | // Calculate and display total edits in the last column for each row | ||
| Line 32: | Line 30: | ||
return editB - editA; // Sort descending by total edit count | return editB - editA; // Sort descending by total edit count | ||
}); | }); | ||
// Update ranks after sorting, handling ties | // Update ranks after sorting, handling ties | ||
| Line 50: | Line 47: | ||
} | } | ||
}); | }); | ||
// Clear the table and re-add sorted rows | // Clear the table and re-add sorted rows | ||
while (table.rows.length > 1) { | while (table.rows.length > 1) { | ||
table.deleteRow(1); // Remove existing rows except for the header | table.deleteRow(1); // Remove existing rows except for the header | ||
} | } | ||
| Line 66: | Line 60: | ||
table.appendChild(row); // Re-add the sorted row to the table | table.appendChild(row); // Re-add the sorted row to the table | ||
}); | }); | ||
} | } | ||
| Line 76: | Line 67: | ||
// Function to calculate total edits for a row | // Function to calculate total edits for a row | ||
function calculateTotalEdits(row) { | function calculateTotalEdits(row) { | ||
var username = row.cells[1] ? row.cells[1].innerText : ""; // Get the username safely | var username = row.cells[1] ? row.cells[1].innerText.trim() : ""; // Get the username safely | ||
// Initialize total edits | // Initialize total edits | ||
var totalEdits = 0; | var totalEdits = 0; | ||
// | // Get the edit count from the 3rd column (index 2) | ||
var editCountsText = row.cells[2] ? row.cells[2].innerText.replace(/,/g, '').trim() : ""; // Remove commas first | |||
var editCounts = editCountsText.split(/\s+/).map(function(edit) { // Split only on whitespace | |||
return parseInt(edit, 10) || 0; // Convert to integer | |||
}); | |||
// Get fandom edits from the "Fandom edits" cell (index 3) | |||
var fandomEdits = row.cells[3] ? parseInt(row.cells[3].innerText.trim().replace(/[^0-9]/g, ''), 10) || 0 : 0; | |||
// Sum up all extracted edit counts and fandom edits | |||
totalEdits = editCounts.reduce((acc, count) => acc + count, 0) + fandomEdits; | |||
return totalEdits; | |||
} | } | ||
Revision as of 11:45, 30 January 2025
/* Any JavaScript here will be loaded for all users on every page load. */
// Function to sort leaderboard
function sortLeaderboard() {
var table = document.querySelector('.edit-leaderboard'); // Target the specific class
// Check if the table exists
if (!table) {
return; // Exit if the table doesn't exist
}
// Select all rows excluding the header row
var headerRow = table.rows[0]; // Get the header row
var rows = Array.prototype.slice.call(table.querySelectorAll('tr')).filter(function(row) {
return row !== headerRow; // Filter out the header row
});
// Calculate and display total edits in the last column for each row
rows.forEach(function(row) {
var totalEdits = calculateTotalEdits(row); // Calculate total edits
if (row.cells[4]) {
row.cells[4].innerHTML = '<strong>' + formatNumber(totalEdits) + '</strong>'; // Update the Total Edits column with bold formatting and comma separation
}
});
// Sort rows by the edit count (column index 4 for Total Edits)
rows.sort(function(rowA, rowB) {
var editA = calculateTotalEdits(rowA); // Use the new function to get total edits
var editB = calculateTotalEdits(rowB); // Use the new function to get total edits
return editB - editA; // Sort descending by total edit count
});
// Update ranks after sorting, handling ties
var currentRank = 1; // Start from rank 1
var lastCount = -1; // Initialize lastCount to a value that won't match any edit count
rows.forEach(function(row) {
var editCount = calculateTotalEdits(row); // Get current total edit count
// Assign rank: if current edit count is the same as the last, keep the same rank
if (editCount !== lastCount) {
row.cells[0].innerHTML = '<strong>' + currentRank + '</strong>'; // Assign current rank with bold formatting
lastCount = editCount; // Update lastCount to the current edit count
currentRank++; // Increment rank for the next unique entry
} else {
row.cells[0].innerHTML = '<strong>' + (currentRank - 1) + '</strong>'; // Keep the same rank for ties
}
});
// Clear the table and re-add sorted rows
while (table.rows.length > 1) {
table.deleteRow(1); // Remove existing rows except for the header
}
// Append the header row back to the table first
table.appendChild(headerRow);
// Append sorted rows back to the table
rows.forEach(function(row) {
table.appendChild(row); // Re-add the sorted row to the table
});
}
// Execute the sorting function
sortLeaderboard();
// Function to calculate total edits for a row
function calculateTotalEdits(row) {
var username = row.cells[1] ? row.cells[1].innerText.trim() : ""; // Get the username safely
// Initialize total edits
var totalEdits = 0;
// Get the edit count from the 3rd column (index 2)
var editCountsText = row.cells[2] ? row.cells[2].innerText.replace(/,/g, '').trim() : ""; // Remove commas first
var editCounts = editCountsText.split(/\s+/).map(function(edit) { // Split only on whitespace
return parseInt(edit, 10) || 0; // Convert to integer
});
// Get fandom edits from the "Fandom edits" cell (index 3)
var fandomEdits = row.cells[3] ? parseInt(row.cells[3].innerText.trim().replace(/[^0-9]/g, ''), 10) || 0 : 0;
// Sum up all extracted edit counts and fandom edits
totalEdits = editCounts.reduce((acc, count) => acc + count, 0) + fandomEdits;
return totalEdits;
}
// Function to format numbers with commas
function formatNumber(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); // Format number with commas
}