MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) Нет описания правки Метка: ручная отмена |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 366: | Строка 366: | ||
var hasTooManyRowspan = false; | var hasTooManyRowspan = false; | ||
// Проходим по строкам первого уровня | |||
topLevelRows.forEach(function(row) { | topLevelRows.forEach(function(row) { | ||
var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')).filter(function(cell) { | var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')).filter(function(cell) { | ||
return !cell.classList.contains('mobile'); | |||
}); | }); | ||
var cellCount = cells.length; | var cellCount = cells.length; | ||
| Строка 406: | Строка 405: | ||
// Если есть некорректные строки с rowspan или слишком много rowspan, выходим | // Если есть некорректные строки с rowspan или слишком много rowspan, выходим | ||
if (hasTooManyRowspan || hasInvalidRowspan) return; | if (hasTooManyRowspan || hasInvalidRowspan) return; | ||
var originalStyles = {}; | |||
// Проходим по каждой строке для добавления событий подсветки | // Проходим по каждой строке для добавления событий подсветки | ||
topLevelRows.forEach(function(row) { | topLevelRows.forEach(function(row, rowIndex) { | ||
var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')); | var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')); | ||
// Сохраняем исходные стили каждой ячейки | |||
cells.forEach(function(cell, index) { | |||
if (!originalStyles[rowIndex]) originalStyles[rowIndex] = []; | |||
originalStyles[rowIndex][index] = { | |||
backgroundColor: getComputedStyle(cell).backgroundColor, | backgroundColor: getComputedStyle(cell).backgroundColor, | ||
color: getComputedStyle(cell).color | color: getComputedStyle(cell).color | ||
| Строка 418: | Строка 421: | ||
}); | }); | ||
cells.forEach(function(cell, index) { | cells.forEach(function(cell, index) { | ||
var rowspan = parseInt(cell.getAttribute('rowspan'), 10) || 1; | |||
// Добавляем обработчик события при наведении мыши | // Добавляем обработчик события при наведении мыши | ||
cell.addEventListener('mouseover', function() { | cell.addEventListener('mouseover', function() { | ||
// Подсвечиваем все строки, охватываемые rowspan | |||
if (rowspan > 1) { | |||
for (var i = 0; i < rowspan; i++) { | |||
var targetRow = topLevelRows[rowIndex + i]; | |||
if (targetRow) { | |||
var targetCells = targetRow.querySelectorAll('td, th'); | |||
Array.prototype.forEach.call(targetCells, function(innerCell, innerIndex) { | |||
innerCell.style.backgroundColor = brightenColor(getComputedStyle(innerCell).backgroundColor); | |||
innerCell.style.color = brightenColor(getComputedStyle(innerCell).color); | |||
}); | |||
} | |||
} | |||
} | |||
// Подсвечиваем ячейки в текущем ряду | |||
cells.forEach(function(innerCell, innerIndex) { | cells.forEach(function(innerCell, innerIndex) { | ||
innerCell.style.backgroundColor = brightenColor(getComputedStyle(innerCell).backgroundColor); | |||
innerCell.style.color = brightenColor(getComputedStyle(innerCell).color); | |||
}); | |||
// Подсвечиваем ячейки, прилегающие к rowspan | |||
if (rowspan > 1) { | |||
var nextRow = topLevelRows[rowIndex + rowspan]; | |||
if (nextRow) { | |||
var nextRowCells = Array.prototype.slice.call(nextRow.querySelectorAll('td, th')); | |||
nextRowCells.forEach(function(nextCell) { | |||
if (nextCell.cellIndex >= cell.cellIndex && nextCell.cellIndex < cell.cellIndex + rowspan) { | |||
nextCell.style.backgroundColor = brightenColor(getComputedStyle(nextCell).backgroundColor); | |||
nextCell.style.color = brightenColor(getComputedStyle(nextCell).color); | |||
} | |||
}); | |||
} | } | ||
} | } | ||
}); | }); | ||
// Добавляем обработчик события, когда мышь уходит с ячейки | // Добавляем обработчик события, когда мышь уходит с ячейки | ||
cell.addEventListener('mouseout', function() { | cell.addEventListener('mouseout', function() { | ||
// Восстанавливаем стили для всех строк, затронутых rowspan | |||
if (rowspan > 1) { | |||
for (var i = 0; i < rowspan; i++) { | |||
var targetRow = topLevelRows[rowIndex + i]; | |||
if (targetRow) { | |||
var targetCells = targetRow.querySelectorAll('td, th'); | |||
Array.prototype.forEach.call(targetCells, function(innerCell, innerIndex) { | |||
innerCell.style.backgroundColor = originalStyles[rowIndex + i][innerIndex].backgroundColor; | |||
innerCell.style.color = originalStyles[rowIndex + i][innerIndex].color; | |||
}); | |||
} | |||
} | |||
} | |||
// Восстанавливаем стили в текущем ряду | |||
cells.forEach(function(innerCell, innerIndex) { | cells.forEach(function(innerCell, innerIndex) { | ||
innerCell.style.backgroundColor = originalStyles[rowIndex][innerIndex].backgroundColor; | |||
innerCell.style.color = originalStyles[rowIndex][innerIndex].color; | |||
}); | }); | ||
}); | }); | ||