MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 843: | Line 843: | ||
var apiBase = mw.util.wikiScript('api'); | var apiBase = mw.util.wikiScript('api'); | ||
var allRows = []; | var allRows = []; | ||
var tableBody = null; | |||
var statusSpan = null; | |||
$(document).ready(function() { | $(document).ready(function() { | ||
var wrapper = document.getElementById('article-overview-wrapper'); | var wrapper = document.getElementById('article-overview-wrapper'); | ||
if (!wrapper) return; | |||
if (!wrapper | |||
// | // Build controls row with button + status | ||
var | var controls = document.createElement('div'); | ||
controls.style.marginBottom = '14px'; | |||
var btn = document.createElement('button'); | var btn = document.createElement('button'); | ||
btn.textContent = 'Download CSV'; | btn.textContent = 'Download CSV'; | ||
btn.style.cssText = 'background-color:#e07b00;color:#fff;padding:9px 20px;border:none;border-radius:5px;font-size:14px;font-weight:bold;cursor:pointer;letter-spacing:0.3px;'; | btn.style.cssText = 'background-color:#e07b00;color:#fff;padding:9px 20px;border:none;border-radius:5px;font-size:14px;font-weight:bold;cursor:pointer;letter-spacing:0.3px;'; | ||
| Line 876: | Line 860: | ||
controls.appendChild(btn); | controls.appendChild(btn); | ||
statusSpan = document.createElement('span'); | |||
statusSpan.style.cssText = 'margin-left:14px;font-size:13px;color:#aaa;vertical-align:middle;'; | statusSpan.style.cssText = 'margin-left:14px;font-size:13px;color:#aaa;vertical-align:middle;'; | ||
statusSpan.textContent = 'Loading…'; | |||
controls.appendChild(statusSpan); | controls.appendChild(statusSpan); | ||
wrapper.appendChild(controls); | |||
// Build table | |||
var table = document.createElement('table'); | |||
table.style.cssText = 'width:100%;border-collapse:collapse;font-size:13px;'; | |||
var thead = document.createElement('thead'); | |||
var headerRow = document.createElement('tr'); | |||
headerRow.style.cssText = 'background-color:#2a2a2a;color:#f0a500;text-align:left;'; | |||
var cols = ['Category', 'Subcategory', 'Title', 'Words', 'Focus']; | |||
cols.forEach(function(colName, i) { | |||
var th = document.createElement('th'); | |||
th.textContent = colName; | |||
th.style.cssText = 'padding:9px 12px;border:1px solid #444;' + (colName === 'Words' ? 'text-align:right;' : ''); | |||
headerRow.appendChild(th); | |||
}); | |||
thead.appendChild(headerRow); | |||
table.appendChild(thead); | |||
tableBody = document.createElement('tbody'); | |||
tableBody.innerHTML = '<tr><td colspan="5" style="padding:12px;text-align:center;color:#aaa;font-style:italic;">Loading articles… please wait.</td></tr>'; | |||
table.appendChild(tableBody); | |||
wrapper.appendChild(table); | |||
buildTable(); | buildTable(); | ||
| Line 908: | Line 914: | ||
function renderTable(rows) { | function renderTable(rows) { | ||
if (!tableBody) return; | |||
if (! | |||
if (!rows || rows.length === 0) { | if (!rows || rows.length === 0) { | ||
tableBody.innerHTML = '<tr><td colspan="5" style="padding:10px;text-align:center;color:#aaa;">No articles found.</td></tr>'; | |||
return; | return; | ||
} | } | ||
| Line 925: | Line 930: | ||
html += '</tr>'; | html += '</tr>'; | ||
}); | }); | ||
tableBody.innerHTML = html; | |||
} | } | ||
function buildTable() { | function buildTable() { | ||
if (statusSpan) statusSpan.textContent = 'Fetching categories…'; | |||
if ( | |||
$.getJSON(apiBase, { action:'query', list:'allcategories', aclimit:500, format:'json' }) | $.getJSON(apiBase, { action:'query', list:'allcategories', aclimit:500, format:'json' }) | ||
| Line 940: | Line 944: | ||
if (totalCats === 0) { | if (totalCats === 0) { | ||
if (tableBody) tableBody.innerHTML = '<tr><td colspan="5" style="padding:10px;text-align:center;color:#aaa;">No categories found.</td></tr>'; | |||
if ( | if (statusSpan) statusSpan.textContent = ''; | ||
if ( | |||
return; | return; | ||
} | } | ||
| Line 948: | Line 951: | ||
function checkDone() { | function checkDone() { | ||
processed++; | processed++; | ||
if ( | if (statusSpan) statusSpan.textContent = 'Loading… (' + processed + '/' + totalCats + ' categories)'; | ||
if (processed === totalCats) { | if (processed === totalCats) { | ||
rows.sort(function(a,b){ | rows.sort(function(a,b){ | ||
| Line 959: | Line 962: | ||
allRows = rows; | allRows = rows; | ||
renderTable(rows); | renderTable(rows); | ||
if ( | if (statusSpan) statusSpan.textContent = rows.length + ' article entries loaded.'; | ||
} | } | ||
} | } | ||
| Line 998: | Line 1,001: | ||
}) | }) | ||
.fail(function() { | .fail(function() { | ||
if (tableBody) tableBody.innerHTML = '<tr><td colspan="5" style="padding:10px;text-align:center;color:#f55;">Error loading data. Check API access.</td></tr>'; | |||
if ( | |||
}); | }); | ||
} | } | ||