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

мНет описания правки
мНет описания правки
Строка 362: Строка 362:
         // Пропускаем первую строку, если нет thead и нет класса 'no-header'
         // Пропускаем первую строку, если нет thead и нет класса 'no-header'
         var topLevelRows = (!thead && !noHeader) ? rows.slice(1) : rows;
         var topLevelRows = (!thead && !noHeader) ? rows.slice(1) : rows;
        var hasInvalidRowspan = false;
        var hasTooManyRowspan = false;
        // Проверка на корректность rowspan
        topLevelRows.forEach(function(row) {
            var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')).filter(function(cell) {
                return !cell.classList.contains('mobile');
            });
            var cellCount = cells.length;
            var rowspanCount = cells.filter(function(cell) {
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1';
            }).length;
            if (rowspanCount > 2) {
                hasTooManyRowspan = true;
                return;
            }
            if (cellCount <= 3 && rowspanCount > 1) {
                hasTooManyRowspan = true;
                return;
            }
            var hasValidRowspanEdge = cells.some(function(cell, index) {
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && (index === 0 || index === cells.length - 1);
            });
            if (!hasValidRowspanEdge && cells.some(function(cell, index) {
                return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1' && index > 0 && index < cells.length - 1;
            })) {
                hasInvalidRowspan = true;
            }
        });
        if (hasTooManyRowspan || hasInvalidRowspan) return;


         // Проходим по каждой строке для добавления событий подсветки
         // Проходим по каждой строке для добавления событий подсветки
Строка 411: Строка 374:


             cells.forEach(function(cell, cellIndex) {
             cells.forEach(function(cell, cellIndex) {
                 if (cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1') {
                 var rowspan = cell.hasAttribute('rowspan') ? parseInt(cell.getAttribute('rowspan'), 10) : 1;
                     var rowspan = parseInt(cell.getAttribute('rowspan'), 10);
 
                // Наведение на ячейку
                cell.addEventListener('mouseover', function() {
                     highlightRow(rowIndex, rowspan);
                });


                    // Наведение на ячейку с rowspan
                // Убираем подсветку при выходе мыши
                    cell.addEventListener('mouseover', function() {
                cell.addEventListener('mouseout', function() {
                        for (var i = rowIndex; i < rowIndex + rowspan; i++) {
                    resetHighlight(rowIndex, rowspan);
                            var targetRow = rows[i];
                });
                            if (!targetRow) continue;


                            var targetCells = Array.prototype.slice.call(targetRow.querySelectorAll('td, th'));
                // Функция для подсветки строки и ячеек с rowspan
                            targetCells.forEach(function(targetCell) {
                function highlightRow(startRowIndex, rowspan) {
                                targetCell.style.backgroundColor = brightenColor(getComputedStyle(targetCell).backgroundColor);
                    for (var i = startRowIndex; i < startRowIndex + rowspan; i++) {
                                targetCell.style.color = brightenColor(getComputedStyle(targetCell).color);
                        var targetRow = rows[i];
                            });
                         if (!targetRow) continue;
                         }
                    });


                    // Убираем подсветку при выходе мыши
                        var targetCells = Array.prototype.slice.call(targetRow.querySelectorAll('td, th'));
                    cell.addEventListener('mouseout', function() {
                         targetCells.forEach(function(targetCell) {
                         for (var i = rowIndex; i < rowIndex + rowspan; i++) {
                             targetCell.style.backgroundColor = brightenColor(getComputedStyle(targetCell).backgroundColor);
                             var targetRow = rows[i];
                             targetCell.style.color = brightenColor(getComputedStyle(targetCell).color);
                             if (!targetRow) continue;
                        });
                    }


                            var targetCells = Array.prototype.slice.call(targetRow.querySelectorAll('td, th'));
                    // Подсветить все ячейки в текущем ряду
                            targetCells.forEach(function(targetCell, targetIndex) {
                    cells.forEach(function(targetCell) {
                                targetCell.style.backgroundColor = originalStyles[targetIndex].backgroundColor;
                        targetCell.style.backgroundColor = brightenColor(getComputedStyle(targetCell).backgroundColor);
                                targetCell.style.color = originalStyles[targetIndex].color;
                        targetCell.style.color = brightenColor(getComputedStyle(targetCell).color);
                            });
                        }
                     });
                     });
                }


                    // Наведение на ячейки, которые охвачены rowspan
                // Функция для сброса подсветки
                     for (var i = rowIndex + 1; i < rowIndex + rowspan; i++) {
                function resetHighlight(startRowIndex, rowspan) {
                        if (!rows[i]) continue;
                     for (var i = startRowIndex; i < startRowIndex + rowspan; i++) {
                         var affectedRow = rows[i];
                         var targetRow = rows[i];
                         var affectedCells = Array.prototype.slice.call(affectedRow.querySelectorAll('td, th'));
                         if (!targetRow) continue;
 
                        affectedCells.forEach(function(affectedCell) {
                            affectedCell.addEventListener('mouseover', function() {
                                cell.style.backgroundColor = brightenColor(getComputedStyle(cell).backgroundColor);
                                cell.style.color = brightenColor(getComputedStyle(cell).color);
                            });


                            affectedCell.addEventListener('mouseout', function() {
                        var targetCells = Array.prototype.slice.call(targetRow.querySelectorAll('td, th'));
                                cell.style.backgroundColor = originalStyles[cellIndex].backgroundColor;
                        targetCells.forEach(function(targetCell, targetIndex) {
                                cell.style.color = originalStyles[cellIndex].color;
                            targetCell.style.backgroundColor = originalStyles[targetIndex].backgroundColor;
                            });
                            targetCell.style.color = originalStyles[targetIndex].color;
                         });
                         });
                     }
                     }
                    // Сбросить подсветку всех ячеек в текущем ряду
                    cells.forEach(function(targetCell, targetIndex) {
                        targetCell.style.backgroundColor = originalStyles[targetIndex].backgroundColor;
                        targetCell.style.color = originalStyles[targetIndex].color;
                    });
                 }
                 }
             });
             });