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;


  function getTableBody() {
    var table = document.getElementById('article-overview-table');
    if (!table) return null;
    var tb = table.querySelector('tbody');
    return tb;
  }
  // Inject the Download button and status span above the table
   $(document).ready(function() {
   $(document).ready(function() {
     var wrapper = document.getElementById('article-overview-wrapper');
     var wrapper = document.getElementById('article-overview-wrapper');
    var table = document.getElementById('article-overview-table');
     if (!wrapper) return;
     if (!wrapper || !table) return;


     // Create or find controls div
     // Build controls row with button + status
     var controls = document.getElementById('article-overview-controls');
     var controls = document.createElement('div');
    if (!controls) {
     controls.style.marginBottom = '14px';
      controls = document.createElement('div');
      controls.id = 'article-overview-controls';
      controls.style.marginBottom = '14px';
      wrapper.insertBefore(controls, table);
     } else {
      controls.style.marginBottom = '14px';
    }


    // Create Download button
     var btn = document.createElement('button');
     var btn = document.createElement('button');
    btn.id = 'download-csv-btn';
     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);


     // Create status span
     statusSpan = document.createElement('span');
    var statusSpan = document.createElement('span');
    statusSpan.id = 'overview-status';
     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) {
    var tbody = getTableBody();
     if (!tableBody) return;
     if (!tbody) return;
     if (!rows || rows.length === 0) {
     if (!rows || rows.length === 0) {
       tbody.innerHTML = '<tr><td colspan="5" style="padding:10px;text-align:center;color:#aaa;">No articles found.</td></tr>';
       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>';
     });
     });
     tbody.innerHTML = html;
     tableBody.innerHTML = html;
   }
   }


   function buildTable() {
   function buildTable() {
    var status = document.getElementById('overview-status');
     if (statusSpan) statusSpan.textContent = 'Fetching categories…';
     if (status) status.textContent = 'Fetching categories…';


     $.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) {
          var tbody = getTableBody();
           if (tableBody) tableBody.innerHTML = '<tr><td colspan="5" style="padding:10px;text-align:center;color:#aaa;">No categories found.</td></tr>';
           if (tbody) tbody.innerHTML = '<tr><td colspan="5" style="padding:10px;text-align:center;color:#aaa;">No categories found.</td></tr>';
           if (statusSpan) statusSpan.textContent = '';
           if (status) status.textContent = '';
           return;
           return;
         }
         }
Line 948: Line 951:
         function checkDone() {
         function checkDone() {
           processed++;
           processed++;
           if (status) status.textContent = 'Loading… (' + processed + '/' + totalCats + ' categories processed)';
           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 (status) status.textContent = rows.length + ' article entries loaded.';
             if (statusSpan) statusSpan.textContent = rows.length + ' article entries loaded.';
           }
           }
         }
         }
Line 998: Line 1,001:
       })
       })
       .fail(function() {
       .fail(function() {
        var tbody = getTableBody();
         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 (tbody) tbody.innerHTML = '<tr><td colspan="5" style="padding:10px;text-align:center;color:#f55;">Error loading data. Check API access.</td></tr>';
       });
       });
   }
   }