MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 294: | Строка 294: | ||
// Функция для увеличения яркости цвета | // Функция для увеличения яркости цвета | ||
function brightenColor(color, factor) { | function brightenColor(color, factor) { | ||
function getBrightness(r, g, b) { | function getBrightness(r, g, b) { | ||
return 0.2126 * r + 0.7152 * g + 0.0722 * b; | return 0.2126 * r + 0.7152 * g + 0.0722 * b; | ||
| Строка 338: | Строка 337: | ||
function applyHighlighting() { | function applyHighlighting() { | ||
var tables = document.querySelectorAll('.wikitable:not(.no-highlight-table)'); | var tables = document.querySelectorAll('.wikitable:not(.no-highlight-table)'); | ||
Array.prototype.forEach.call(tables, function(table) { | Array.prototype.forEach.call(tables, function(table) { | ||
var tbody = table.querySelector('tbody'); | var tbody = table.querySelector('tbody'); | ||
var thead = table.querySelector('thead'); | var thead = table.querySelector('thead'); | ||
var | // Проверяем, что tbody существует | ||
if (tbody) { | |||
var rows = Array.prototype.slice.call(tbody.querySelectorAll('tr')).filter(function(row) { | |||
return !row.querySelector('table'); | |||
}); | |||
// Пропускаем первую строку, если thead нет | |||
var | var topLevelRows = thead ? rows : rows.slice(1); | ||
var | var hasInvalidRowspan = false; | ||
var hasTooManyRowspan = false; | |||
topLevelRows.forEach(function(row) { | |||
var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')); | |||
var rowspanCount = cells.filter(function(cell) { | |||
return cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1'; | |||
}).length; | |||
if (rowspanCount > 2) { | |||
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; | |||
cell.addEventListener('mouseover', function() { | topLevelRows.forEach(function(row) { | ||
var cells = Array.prototype.slice.call(row.querySelectorAll('td, th')); | |||
var originalStyles = cells.map(function(cell) { | |||
return { | |||
backgroundColor: getComputedStyle(cell).backgroundColor, | |||
} | color: getComputedStyle(cell).color | ||
}; | |||
}); | |||
cells.forEach(function(cell, index) { | |||
if (cell.hasAttribute('rowspan') && cell.getAttribute('rowspan') !== '1') return; | |||
cell.addEventListener('mouseover', function() { | |||
cells.forEach(function(innerCell, innerIndex) { | |||
if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') { | |||
innerCell.style.backgroundColor = brightenColor(originalStyles[innerIndex].backgroundColor, 1.035); | |||
innerCell.style.color = brightenColor(originalStyles[innerIndex].color, 1.035); | |||
} | |||
}); | |||
}); | }); | ||
cell.addEventListener('mouseout', function() { | |||
cells.forEach(function(innerCell, innerIndex) { | |||
if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') { | |||
innerCell.style.backgroundColor = originalStyles[innerIndex].backgroundColor; | |||
innerCell.style.color = originalStyles[innerIndex].color; | |||
} | } | ||
}); | |||
}); | }); | ||
}); | }); | ||
}); | }); | ||
} | } | ||
}); | }); | ||
} | } | ||