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

Нет описания правки
Нет описания правки
Метка: ручная отмена
Строка 335: Строка 335:
// Функция для подсветки ячеек в таблице при наведении
// Функция для подсветки ячеек в таблице при наведении
function applyHighlighting() {
function applyHighlighting() {
     if (/Mobi|Android/i.test(navigator.userAgent)) return;
     if (/Mobi|Android/i.test(navigator.userAgent)) {
        return;
    }


     var tables = document.querySelectorAll('.wikitable:not(.no-highlight-table)');
     var tables = document.querySelectorAll('.wikitable:not(.no-highlight-table)');


     for (var i = 0; i < tables.length; i++) {
     Array.prototype.forEach.call(tables, function(table) {
        var table = tables[i];
         var tbody = table.querySelector('tbody');
         var tbody = table.querySelector('tbody');
         if (!tbody) continue;
        var thead = table.querySelector('thead');
        var noHeader = table.classList.contains('no-header-table');
 
         if (tbody) {
            // Получаем все строки <tr> внутри первого уровня <tbody>
            var rows = Array.prototype.slice.call(tbody.querySelectorAll('tr')).filter(function(row) {
                // Проверяем, что <tr> находится на первом уровне, и исключаем строки с вложенными таблицами
                return row.parentElement === tbody && !row.querySelector('table');
            });
 
            // Пропускаем первую строку, если нет <thead> и нет класса 'no-header-table'
            var topLevelRows = (!thead && !noHeader) ? rows.slice(1) : rows;
 
            var hasInvalidRowspan = false;
            var hasTooManyRowspan = false;


        var rows = Array.prototype.slice.call(tbody.querySelectorAll('tr')).filter(function(row) {
            topLevelRows.forEach(function(row) {
            return !row.querySelector('table');
                var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')).filter(function(cell) {
        });
                    return !cell.classList.contains('mobile');
       
                });
        var rowspanCells = [];
                var cellCount = cells.length;


        for (var rowIndex = 0; rowIndex < rows.length; rowIndex++) {
                var rowspanCount = cells.filter(function(cell) {
            var row = rows[rowIndex];
                    return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1';
            var cells = Array.prototype.slice.call(row.querySelectorAll('td, th'));
                }).length;


            for (var cellIndex = 0; cellIndex < cells.length; cellIndex++) {
                if (rowspanCount > 2) {
                var cell = cells[cellIndex];
                    hasTooManyRowspan = true;
                 var rowspan = parseInt(cell.getAttribute('rowspan'), 10);
                    return;
                 }


                // Если у ячейки есть rowspan
                 if (cellCount <= 3 && rowspanCount > 1) {
                 if (!isNaN(rowspan) && rowspan > 1) {
                     hasTooManyRowspan = true;
                     rowspanCells.push({ cell: cell, rowIndex: rowIndex, rowspan: rowspan });
                    return;
                }


                    // Подсветка для ячейки с rowspan
                var hasValidRowspanEdge = cells.some(function(cell, index) {
                    cell.addEventListener('mouseover', (function(cell, rowIndex, rowspan) {
                    return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && (index === 0 || index === cells.length - 1);
                        return function() {
                });
                            for (var i = 0; i < rowspan; i++) {
                                if (rows[rowIndex + i]) {
                                    highlightRow(rows[rowIndex + i]);
                                }
                            }
                        };
                    })(cell, rowIndex, rowspan));


                    cell.addEventListener('mouseout', (function(cell, rowIndex, rowspan) {
                if (!hasValidRowspanEdge && cells.some(function(cell, index) {
                        return function() {
                    return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && index > 0 && index < cells.length - 1;
                            for (var i = 0; i < rowspan; i++) {
                })) {
                                if (rows[rowIndex + i]) {
                     hasInvalidRowspan = true;
                                    resetRow(rows[rowIndex + i]);
                                }
                            }
                        };
                     })(cell, rowIndex, rowspan));
                 }
                 }
            });


                // Подсветка для строки
            if (hasTooManyRowspan || hasInvalidRowspan) return;
                row.addEventListener('mouseover', (function(row) {
                    return function() {
                        highlightRow(row);
                    };
                })(row));


                 row.addEventListener('mouseout', (function(row) {
            topLevelRows.forEach(function(row) {
                     return function() {
                 var cells = Array.prototype.slice.call(row.querySelectorAll('td, th'));
                         resetRow(row);
                var originalStyles = cells.map(function(cell) {
                     return {
                        backgroundColor: getComputedStyle(cell).backgroundColor,
                         color: getComputedStyle(cell).color
                     };
                     };
                 })(row));
                 });
            }
        }


        function highlightRow(row) {
                cells.forEach(function(cell, index) {
            var rowCells = row.querySelectorAll('td, th');
                    if (cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1') return;
            for (var j = 0; j < rowCells.length; j++) {
                    cell.addEventListener('mouseover', function() {
                var cell = rowCells[j];
                        cells.forEach(function(innerCell, innerIndex) {
                cell.style.backgroundColor = brightenColor(getComputedStyle(cell).backgroundColor);
                            if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') {
                cell.style.color = brightenColor(getComputedStyle(cell).color);
                                innerCell.style.setProperty('background-color', brightenColor(originalStyles[innerIndex].backgroundColor), 'important');
             }
                                innerCell.style.setProperty('color', brightenColor(originalStyles[innerIndex].color), 'important');
                            }
                        });
                    });
                    cell.addEventListener('mouseout', function() {
                        cells.forEach(function(innerCell, innerIndex) {
                            if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') {
                                innerCell.style.setProperty('background-color', originalStyles[innerIndex].backgroundColor, 'important');
                                innerCell.style.setProperty('color', originalStyles[innerIndex].color, 'important');
                            }
                        });
                    });
                });
             });
         }
         }
 
    });
        function resetRow(row) {
            var rowCells = row.querySelectorAll('td, th');
            for (var j = 0; j < rowCells.length; j++) {
                var cell = rowCells[j];
                cell.style.backgroundColor = ''; // сбросить на стандартный фон
                cell.style.color = ''; // сбросить цвет текста
            }
        }
    }
}
}