MediaWiki:Common.js: различия между версиями

Нет описания правки
Нет описания правки
Строка 294: Строка 294:
// Функция для увеличения яркости цвета
// Функция для увеличения яркости цвета
function brightenColor(color, factor) {
function brightenColor(color, factor) {
    // Функция для вычисления яркости цвета (использует стандартное восприятие цвета)
     function getBrightness(r, g, b) {
     function getBrightness(r, g, b) {
         return 0.2126 * r + 0.7152 * g + 0.0722 * b;
         return 0.2126 * r + 0.7152 * g + 0.0722 * b;
Строка 338: Строка 337:
function applyHighlighting() {
function applyHighlighting() {
     var tables = document.querySelectorAll('.wikitable:not(.no-highlight-table)');
     var tables = document.querySelectorAll('.wikitable:not(.no-highlight-table)');
   
 
     Array.prototype.forEach.call(tables, function(table) {
     Array.prototype.forEach.call(tables, function(table) {
         var tbody = table.querySelector('tbody');
         var tbody = table.querySelector('tbody');
         var thead = table.querySelector('thead');
         var thead = table.querySelector('thead');
        var rows = Array.prototype.slice.call(tbody.querySelectorAll('tr')).filter(function(row) {
            return !row.querySelector('table');
        });
        var topLevelRows = thead ? rows : rows.slice(1);


         var hasInvalidRowspan = false;
         // Проверяем, что tbody существует
        var hasTooManyRowspan = false;
        if (tbody) {
            var rows = Array.prototype.slice.call(tbody.querySelectorAll('tr')).filter(function(row) {
                return !row.querySelector('table');
            });


        topLevelRows.forEach(function(row) {
            // Пропускаем первую строку, если thead нет
             var cells = Array.prototype.slice.call(row.querySelectorAll('td, th'));
             var topLevelRows = thead ? rows : rows.slice(1);


             var rowspanCount = cells.filter(function(cell) {
             var hasInvalidRowspan = false;
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1';
             var hasTooManyRowspan = false;
             }).length;


             if (rowspanCount > 2) {
             topLevelRows.forEach(function(row) {
                 hasTooManyRowspan = true;
                 var cells = Array.prototype.slice.call(row.querySelectorAll('td, th'));
                return;
            }


            var hasValidRowspanEdge = cells.some(function(cell, index) {
                var rowspanCount = cells.filter(function(cell) {
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && (index === 0 || index === cells.length - 1);
                    return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1';
            });
                }).length;


            if (!hasValidRowspanEdge && cells.some(function(cell, index) {
                if (rowspanCount > 2) {
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && index > 0 && index < cells.length - 1;
                    hasTooManyRowspan = true;
            })) {
                    return;
                hasInvalidRowspan = true;
                }
            }
        });


        if (hasTooManyRowspan || hasInvalidRowspan) return;
                var hasValidRowspanEdge = cells.some(function(cell, index) {
                    return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && (index === 0 || index === cells.length - 1);
                });


        topLevelRows.forEach(function(row) {
                if (!hasValidRowspanEdge && cells.some(function(cell, index) {
            var cells = Array.prototype.slice.call(row.querySelectorAll('td, th'));
                    return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && index > 0 && index < cells.length - 1;
            var originalStyles = cells.map(function(cell) {
                 })) {
                 return {
                     hasInvalidRowspan = true;
                     backgroundColor: getComputedStyle(cell).backgroundColor,
                 }
                    color: getComputedStyle(cell).color
                 };
             });
             });


             cells.forEach(function(cell, index) {
             if (hasTooManyRowspan || hasInvalidRowspan) return;
                if (cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1') return;


                 cell.addEventListener('mouseover', function() {
            topLevelRows.forEach(function(row) {
                    cells.forEach(function(innerCell, innerIndex) {
                var cells = Array.prototype.slice.call(row.querySelectorAll('td, th'));
                        if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') {
                var originalStyles = cells.map(function(cell) {
                            innerCell.style.backgroundColor = brightenColor(originalStyles[innerIndex].backgroundColor, 1.035);
                    return {
                            innerCell.style.color = brightenColor(originalStyles[innerIndex].color, 1.035);
                        backgroundColor: getComputedStyle(cell).backgroundColor,
                         }
                        color: getComputedStyle(cell).color
                    };
                });
 
                 cells.forEach(function(cell, index) {
                    if (cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1') return;
 
                    cell.addEventListener('mouseover', function() {
                        cells.forEach(function(innerCell, innerIndex) {
                            if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') {
                                innerCell.style.backgroundColor = brightenColor(originalStyles[innerIndex].backgroundColor, 1.035);
                                innerCell.style.color = brightenColor(originalStyles[innerIndex].color, 1.035);
                            }
                         });
                     });
                     });
                });


                cell.addEventListener('mouseout', function() {
                    cell.addEventListener('mouseout', function() {
                    cells.forEach(function(innerCell, innerIndex) {
                        cells.forEach(function(innerCell, innerIndex) {
                        if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') {
                            if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') {
                            innerCell.style.backgroundColor = originalStyles[innerIndex].backgroundColor;
                                innerCell.style.backgroundColor = originalStyles[innerIndex].backgroundColor;
                            innerCell.style.color = originalStyles[innerIndex].color;
                                innerCell.style.color = originalStyles[innerIndex].color;
                         }
                            }
                         });
                     });
                     });
                 });
                 });
             });
             });
         });
         }
     });
     });
}
}