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

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


     var tables = document.querySelectorAll('.wikitable:not(.no-highlight-table)');
     var tables = document.querySelectorAll('.wikitable:not(.no-highlight-table)');
Строка 343: Строка 341:
     tables.forEach(function(table) {
     tables.forEach(function(table) {
         var tbody = table.querySelector('tbody');
         var tbody = table.querySelector('tbody');
         var thead = table.querySelector('thead');
         if (!tbody) return;
        var noHeader = table.classList.contains('no-header-table');


         if (tbody) {
         var rows = Array.from(tbody.querySelectorAll('tr')).filter(row => !row.querySelector('table'));
            var rows = Array.from(tbody.querySelectorAll('tr')).filter(function(row) {
        var rowspanCells = [];
                return row.parentElement === tbody && !row.querySelector('table');
            });


             var topLevelRows = (!thead && !noHeader) ? rows.slice(1) : rows;
        rows.forEach(function(row, rowIndex) {
             var cells = Array.from(row.querySelectorAll('td, th'));


             // Для отслеживания всех ячеек с rowspan
             cells.forEach(function(cell) {
            var rowspanCells = [];
                var rowspan = parseInt(cell.getAttribute('rowspan'), 10);


            topLevelRows.forEach(function(row, rowIndex) {
                 // Если у ячейки есть rowspan
                 var cells = Array.from(row.querySelectorAll('td, th'));
                 if (!isNaN(rowspan) && rowspan > 1) {
                var originalStyles = cells.map(function(cell) {
                    rowspanCells.push({ cell: cell, rowIndex: rowIndex, rowspan: rowspan });
                    return {
                        backgroundColor: getComputedStyle(cell).backgroundColor,
                        color: getComputedStyle(cell).color
                    };
                });
 
                 cells.forEach(function(cell, index) {
                    var rowspan = parseInt(cell.getAttribute('rowspan'), 10);
 
                    if (!isNaN(rowspan) && rowspan > 1) {
                        rowspanCells.push({ cell: cell, rowIndex: rowIndex, rowspan: rowspan });
 
                        // Подсвечиваем строки, которые затрагивает ячейка с rowspan
                        var isRowspanHighlighted = false;
 
                        cell.addEventListener('mouseover', function() {
                            if (!isRowspanHighlighted) {
                                for (var i = 0; i < rowspan; i++) {
                                    var targetRow = topLevelRows[rowIndex + i];
                                    if (targetRow) {
                                        Array.from(targetRow.children).forEach(function(targetCell) {
                                            targetCell.style.setProperty('background-color', brightenColor(getComputedStyle(targetCell).backgroundColor), 'important');
                                            targetCell.style.setProperty('color', brightenColor(getComputedStyle(targetCell).color), 'important');
                                        });
                                    }
                                }
                                isRowspanHighlighted = true;
                            }
                        });
 
                        cell.addEventListener('mouseout', function() {
                            for (var i = 0; i < rowspan; i++) {
                                var targetRow = topLevelRows[rowIndex + i];
                                if (targetRow) {
                                    Array.from(targetRow.children).forEach(function(targetCell) {
                                        targetCell.style.removeProperty('background-color');
                                        targetCell.style.removeProperty('color');
                                    });
                                }
                            }
                            isRowspanHighlighted = false;
                        });
                    }


                     // Подсвечиваем все ячейки в строке при наведении на ряд
                     // Подсветка для ячейки с rowspan
                     cell.addEventListener('mouseover', function() {
                     cell.addEventListener('mouseover', function() {
                         cells.forEach(function(innerCell, innerIndex) {
                         for (var i = 0; i < rowspan; i++) {
                            innerCell.style.setProperty('background-color', brightenColor(originalStyles[innerIndex].backgroundColor), 'important');
                             if (rows[rowIndex + i]) {
                            innerCell.style.setProperty('color', brightenColor(originalStyles[innerIndex].color), 'important');
                                 highlightRow(rows[rowIndex + i]);
                        });
 
                        // Подсвечиваем ячейки rowspan, которые затрагивают текущую строку
                        rowspanCells.forEach(function(rowspanCell) {
                             if (rowIndex >= rowspanCell.rowIndex && rowIndex < rowspanCell.rowIndex + rowspanCell.rowspan) {
                                 rowspanCell.cell.style.setProperty('background-color', brightenColor(getComputedStyle(rowspanCell.cell).backgroundColor), 'important');
                                rowspanCell.cell.style.setProperty('color', brightenColor(getComputedStyle(rowspanCell.cell).color), 'important');
                             }
                             }
                         });
                         }
                     });
                     });


                    // Сброс подсветки при уходе с ячейки
                     cell.addEventListener('mouseout', function() {
                     cell.addEventListener('mouseout', function() {
                         cells.forEach(function(innerCell, innerIndex) {
                         for (var i = 0; i < rowspan; i++) {
                            innerCell.style.removeProperty('background-color');
                             if (rows[rowIndex + i]) {
                            innerCell.style.removeProperty('color');
                                 resetRow(rows[rowIndex + i]);
                        });
 
                        // Сбрасываем подсветку для ячеек rowspan
                        rowspanCells.forEach(function(rowspanCell) {
                             if (rowIndex >= rowspanCell.rowIndex && rowIndex < rowspanCell.rowIndex + rowspanCell.rowspan) {
                                 rowspanCell.cell.style.removeProperty('background-color');
                                rowspanCell.cell.style.removeProperty('color');
                             }
                             }
                         });
                         }
                     });
                     });
                }
                // Подсветка для строки
                row.addEventListener('mouseover', function() {
                    highlightRow(row);
                 });
                 });
                row.addEventListener('mouseout', function() {
                    resetRow(row);
                });
            });
        });
        function highlightRow(row) {
            row.querySelectorAll('td, th').forEach(function(cell) {
                cell.style.backgroundColor = brightenColor(getComputedStyle(cell).backgroundColor);
                cell.style.color = brightenColor(getComputedStyle(cell).color);
            });
        }
        function resetRow(row) {
            row.querySelectorAll('td, th').forEach(function(cell) {
                cell.style.backgroundColor = ''; // сбросить на стандартный фон
                cell.style.color = ''; // сбросить цвет текста
             });
             });
         }
         }