MediaWiki:Common.js: различия между версиями
Pok (обсуждение | вклад) Нет описания правки |
Pok (обсуждение | вклад) Нет описания правки Метка: ручная отмена |
||
| Строка 335: | Строка 335: | ||
// Функция для подсветки ячеек в таблице при наведении | // Функция для подсветки ячеек в таблице при наведении | ||
function applyHighlighting() { | function applyHighlighting() { | ||
if (/Mobi|Android/i.test(navigator.userAgent)) return; | if (/Mobi|Android/i.test(navigator.userAgent)) { | ||
return; | |||
} | |||
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) { | |||
var tbody = table.querySelector('tbody'); | var tbody = table.querySelector('tbody'); | ||
if (! | var thead = table.querySelector('thead'); | ||
var noHeader = table.classList.contains('no-header-table'); | |||
if (tbody) { | |||
// Получаем все строки <tr> внутри первого уровня <tbody> | |||
var rows = Array.prototype.slice.call(tbody.querySelectorAll('tr')).filter(function(row) { | |||
// Проверяем, что <tr> находится на первом уровне, и исключаем строки с вложенными таблицами | |||
return row.parentElement === tbody && !row.querySelector('table'); | |||
}); | |||
// Пропускаем первую строку, если нет <thead> и нет класса 'no-header-table' | |||
var topLevelRows = (!thead && !noHeader) ? rows.slice(1) : rows; | |||
var hasInvalidRowspan = false; | |||
var hasTooManyRowspan = false; | |||
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) { | |||
if ( | 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; | |||
row. | topLevelRows.forEach(function(row) { | ||
return | 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.setProperty('background-color', brightenColor(originalStyles[innerIndex].backgroundColor), 'important'); | |||
} | innerCell.style.setProperty('color', brightenColor(originalStyles[innerIndex].color), 'important'); | ||
} | |||
}); | |||
}); | |||
cell.addEventListener('mouseout', function() { | |||
cells.forEach(function(innerCell, innerIndex) { | |||
if (!innerCell.hasAttribute('rowspan') || innerCell.getAttribute('rowspan') === '1') { | |||
innerCell.style.setProperty('background-color', originalStyles[innerIndex].backgroundColor, 'important'); | |||
innerCell.style.setProperty('color', originalStyles[innerIndex].color, 'important'); | |||
} | |||
}); | |||
}); | |||
}); | |||
}); | |||
} | } | ||
}); | |||
} | } | ||