MediaWiki:Common.js: відмінності між версіями

нема опису редагування
Немає опису редагування
Немає опису редагування
Рядок 886: Рядок 886:


// ============================================================
// ============================================================
// SEASON RATING TABLE — medals, color extremes
// SEASON RATING TABLE — medals + column colors
// Detects tables with headers: № | name | Σ | І | %
// Adds .mcc-rating-table class, then processes columns
// ============================================================
// ============================================================
$(function () {
$(function () {
     // Find rating tables on season pages
     function processRatingTable($t) {
    // Rating table has columns: № | Пан/Пані | Σ | І | %
         // Collect header texts
    // We detect by: first th text = "№" or "Пан/Пані" nearby
    $('.content .mw-parser-output .wikitable').each(function () {
         var $t = $(this);
         var headers = [];
         var headers = [];
         $t.find('thead tr:first th, tr:first th').each(function () {
         $t.find('tr').first().find('th').each(function () {
             headers.push($(this).text().trim());
             headers.push($(this).text().trim());
         });
         });


         // Must have №, Σ, І, % columns — rating table signature
         // Must start with № and have Σ at index 2
         var hasNum  = headers[0] === '№';
         if (headers[0] !== '№') return;
         var hasSigma = headers.indexOf('Σ') !== -1 || headers[2] === 'Σ';
         if (headers.length < 4) return;
         var hasI    = headers.indexOf('І') !== -1 || headers[3] === 'І';
        // At least one of Σ, І, % must be present
         var hasPct = headers.indexOf('%') !== -1 || headers[4] === '%';
        var hasSigma = headers[2] === 'Σ';
        if (!hasSigma) return;
 
         var iGames = 3; // І
        var iPct  = 4; // %
        var hasI  = headers[iGames] === 'І';
         var hasPct = headers[iPct]   === '%';


         if (!hasNum || !hasSigma) return;
         // Mark table
        $t.addClass('mcc-rating-table');


         var $rows = $t.find('tbody tr').filter(function () {
         var $rows = $t.find('tbody tr').filter(function () {
             return $(this).find('td').length >= 4;
             return $(this).find('td').length >= 4;
         });
         });
         if (!$rows.length) return;
         if (!$rows.length) return;


         // Col indices
         // 1. Medal badges top 3
        var iNum  = 0; // №
        var iName  = 1; // Пан/Пані
        var iSigma = 2; // Σ
        var iGames = 3; // І
        var iPct  = 4; // %
 
        // ── 1. Medal badges for top 3 ──
         $rows.each(function () {
         $rows.each(function () {
             var $tds = $(this).find('td');
             var $tds = $(this).find('td');
             var numCell = $tds.eq(iNum).text().trim();
             var n = parseInt($tds.eq(0).text().trim());
            var n = parseInt(numCell);
             if (n >= 1 && n <= 3) {
            if (isNaN(n)) return;
                 $tds.eq(0).html('<span class="mcc-rank-' + n + '">' + n + '</span>');
             if (n === 1 || n === 2 || n === 3) {
                var cls = 'mcc-rank-' + n;
                 $tds.eq(iNum).html('<span class="' + cls + '">' + n + '</span>');
             }
             }
         });
         });


         // ── 2. Σ column white (CSS handles, but clear any conflicting color) ──
         // 2. Σ white done via CSS .mcc-rating-table td:nth-child(3)
        $rows.each(function () {
            var $td = $(this).find('td').eq(iSigma);
            $td.css({ color: '#ffffff', fontWeight: '700' });
        });


         // ── 3. Games (І) — highlight max/min ──
         // 3. Games (І) — max green, min red
         if (hasI) {
         if (hasI) {
             var gamesVals = [];
             var gVals = [];
             $rows.each(function () {
             $rows.each(function () {
                 var v = parseInt($(this).find('td').eq(iGames).text().trim());
                 var v = parseInt($(this).find('td').eq(iGames).text().trim());
                 if (!isNaN(v)) gamesVals.push(v);
                 if (!isNaN(v) && v > 0) gVals.push(v);
             });
             });
             if (gamesVals.length) {
             if (gVals.length > 1) {
                 var gMax = Math.max.apply(null, gamesVals);
                 var gMax = Math.max.apply(null, gVals);
                 var gMin = Math.min.apply(null, gamesVals);
                 var gMin = Math.min.apply(null, gVals);
                 $rows.each(function () {
                 $rows.each(function () {
                     var $td = $(this).find('td').eq(iGames);
                     var $td = $(this).find('td').eq(iGames);
Рядок 958: Рядок 949:
         }
         }


         // ── 4. WR (%) — highlight max/min ──
         // 4. WR (%) — max green, min red
         if (hasPct) {
         if (hasPct) {
             var pctVals = [];
             var pVals = [];
             $rows.each(function () {
             $rows.each(function () {
                 var raw = $(this).find('td').eq(iPct).text().trim().replace('%','');
                 var v = parseFloat($(this).find('td').eq(iPct).text().trim().replace('%',''));
                var v = parseFloat(raw);
                 if (!isNaN(v)) pVals.push(v);
                 if (!isNaN(v)) pctVals.push(v);
             });
             });
             if (pctVals.length) {
             if (pVals.length > 1) {
                 var pMax = Math.max.apply(null, pctVals);
                 var pMax = Math.max.apply(null, pVals);
                 var pMin = Math.min.apply(null, pctVals);
                 var pMin = Math.min.apply(null, pVals);
                 $rows.each(function () {
                 $rows.each(function () {
                     var $td = $(this).find('td').eq(iPct);
                     var $td = $(this).find('td').eq(iPct);
Рядок 978: Рядок 968:
             }
             }
         }
         }
    }
    // Run on all wikitables
    $('.content .mw-parser-output .wikitable').each(function () {
        processRatingTable($(this));
     });
     });
});
});