MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) мНет описания правки |
||
| Строка 339: | Строка 339: | ||
// Функция для подсветки ячеек в таблице при наведении | // Функция для подсветки ячеек в таблице при наведении | ||
function applyHighlighting() { | function applyHighlighting() { | ||
// Проверка, является ли устройство мобильным | // Проверка, является ли устройство мобильным | ||
if (/Mobi|Android/i.test(navigator.userAgent)) { | if (/Mobi|Android/i.test(navigator.userAgent)) { | ||
return; | return; | ||
| Строка 353: | Строка 353: | ||
var noHeader = table.classList.contains('no-header-table'); | var noHeader = table.classList.contains('no-header-table'); | ||
if (!tbody) return; | |||
if (tbody) | |||
// Получаем все строки <tr> внутри tbody, исключая строки с вложенными таблицами | |||
var rows = Array.prototype.slice.call(tbody.querySelectorAll('tr')).filter(function(row) { | |||
return !row.querySelector('table'); | |||
}); | |||
// Пропускаем первую строку, если нет thead и нет класса 'no-header' | |||
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; | |||
// Проверка на наличие ячеек с rowspan | |||
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; | |||
} | |||
// Проверка корректности rowspan на краю строки | |||
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; | |||
} | |||
}); | |||
// Если некорректные строки с rowspan или слишком много rowspan — игнорируем таблицу | |||
if (hasTooManyRowspan || hasInvalidRowspan) return; | |||
// Объект для хранения оригинальных стилей | |||
var originalStyles = {}; | |||
// Проходим по каждой строке для добавления событий подсветки | |||
topLevelRows.forEach(function(row, rowIndex) { | |||
var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')); | |||
// Сохраняем исходные стили каждой ячейки | |||
originalStyles[rowIndex] = {}; | |||
cells.forEach(function(cell, cellIndex) { | |||
originalStyles[rowIndex][cellIndex] = { | |||
backgroundColor: getComputedStyle(cell).backgroundColor, | |||
color: getComputedStyle(cell).color | |||
}; | |||
}); | |||
cells.forEach(function(cell, cellIndex) { | |||
var rowspan = parseInt(cell.getAttribute('rowspan'), 10) || 1; | |||
// Добавляем обработчик события при наведении мыши | |||
cell.addEventListener('mouseover', function() { | |||
// Подсвечиваем все строки, охватываемые rowspan | |||
for (var i = rowIndex; i < rowIndex + rowspan; i++) { | |||
var targetRow = topLevelRows[i]; | |||
if (!targetRow) continue; | |||
var targetCells = Array.prototype.slice.call(targetRow.querySelectorAll('td, th')); | |||
targetCells.forEach(function(targetCell, targetIndex) { | |||
targetCell.style.backgroundColor = brightenColor(getComputedStyle(targetCell).backgroundColor); | |||
targetCell.style.color = brightenColor(getComputedStyle(targetCell).color); | |||
}); | }); | ||
} | |||
// Подсвечиваем ячейки в текущем ряду | |||
cells.forEach(function(innerCell, innerIndex) { | |||
innerCell.style.backgroundColor = brightenColor(getComputedStyle(innerCell).backgroundColor); | |||
innerCell.style.color = brightenColor(getComputedStyle(innerCell).color); | |||
}); | }); | ||
// | // Подсвечиваем ячейки, прилегающие к текущей ячейке с rowspan | ||
for (var i = rowIndex + 1; i < rowIndex + rowspan; i++) { | |||
if (!topLevelRows[i]) continue; | |||
// Восстанавливаем стили | var affectedRow = topLevelRows[i]; | ||
var affectedCells = Array.prototype.slice.call(affectedRow.querySelectorAll('td, th')); | |||
affectedCells.forEach(function(affectedCell) { | |||
affectedCell.style.backgroundColor = brightenColor(getComputedStyle(affectedCell).backgroundColor); | |||
affectedCell.style.color = brightenColor(getComputedStyle(affectedCell).color); | |||
}); | |||
} | |||
}); | |||
// Добавляем обработчик события, когда мышь уходит с ячейки | |||
cell.addEventListener('mouseout', function() { | |||
// Восстанавливаем стили для всех строк, затронутых rowspan | |||
for (var i = rowIndex; i < rowIndex + rowspan; i++) { | |||
var targetRow = topLevelRows[i]; | |||
if (!targetRow) continue; | |||
var targetCells = Array.prototype.slice.call(targetRow.querySelectorAll('td, th')); | |||
targetCells.forEach(function(targetCell, targetIndex) { | |||
targetCell.style.backgroundColor = originalStyles[i][targetIndex].backgroundColor; | |||
targetCell.style.color = originalStyles[i][targetIndex].color; | |||
}); | }); | ||
} | |||
// Восстанавливаем стили в текущем ряду | |||
cells.forEach(function(innerCell, innerIndex) { | |||
innerCell.style.backgroundColor = originalStyles[rowIndex][innerIndex].backgroundColor; | |||
innerCell.style.color = originalStyles[rowIndex][innerIndex].color; | |||
}); | }); | ||
}); | }); | ||
}); | }); | ||
} | }); | ||
}); | }); | ||
} | } | ||