Mediawiki:common.js
| Строка 106: | Строка 106: | ||
}; | }; | ||
var LawTooltipsModule = { | |||
init: function() { | |||
var tableCells = document.querySelectorAll('.law-tooltips td, .law-tooltips th'); | |||
if (tableCells.length === 0) return; | |||
function run() { | |||
var lawData = LawTooltipsModule.extractLawData(); | |||
if (Object.keys(lawData).length > 0) { | |||
LawTooltipsModule.initCellTooltips(tableCells, lawData); | |||
return true; | |||
} | |||
return false; | |||
} | |||
MediaWikiTooltips.load(function() { | |||
if (run()) return; | |||
setTimeout(function() { | |||
if (run()) return; | |||
setTimeout(run, 500); | |||
}, 300); | |||
}); | |||
}, | |||
extractLawData: function() { | |||
var lawData = {}; | |||
var containers = document.querySelectorAll('.mw-collapsible'); | |||
containers.forEach(function(block) { | |||
var content = block.querySelector('.mw-collapsible-content'); | |||
if (content && block.classList.contains('mw-collapsed')) { | |||
block.classList.remove('mw-collapsed'); | |||
var toggle = content.querySelector('.mw-collapsible-toggle'); | |||
if (toggle) toggle.setAttribute('aria-expanded', 'true'); | |||
} | |||
}); | |||
var detailTables = document.querySelectorAll('.mw-collapsible-content table'); | |||
detailTables.forEach(function(table) { | |||
if (table.classList.contains('law-tooltips')) return; | |||
var rows = table.querySelectorAll('tr'); | |||
rows.forEach(function(row) { | |||
var cells = row.querySelectorAll('td'); | |||
if (cells.length < 3) return; | |||
var crimeCell = cells[0]; | |||
var descriptionCell = cells[1]; | |||
var exampleCell = cells[2]; | |||
var anchor = crimeCell.querySelector('[id]'); | |||
if (!anchor) return; | |||
var lawCode = anchor.id; | |||
var titleElement = crimeCell.querySelector('b'); | |||
if (!titleElement || !lawCode.match(/^\d{3}$/)) return; | |||
var titleText = titleElement.textContent.trim(); | |||
var lawTitle = titleText.replace(/^\d{3}\s*/, '').replace(/^\d{3}/, '').trim(); | |||
var description = descriptionCell.innerHTML.trim(); | |||
var examples = []; | |||
var exampleItems = exampleCell.querySelectorAll('li'); | |||
exampleItems.forEach(function(item) { | |||
var html = item.innerHTML.trim().replace(/<br\s*\/?>/gi, ''); | |||
if (html) examples.push(html); | |||
}); | |||
lawData[lawCode] = { | |||
title: lawTitle, | |||
description: description, | |||
examples: examples | |||
}; | |||
}); | |||
}); | |||
return lawData; | |||
}, | |||
createTooltipContent: function(lawCode, lawData) { | |||
var data = lawData[lawCode]; | |||
if (!data) return null; | |||
var html = '<div class="law-tooltip">'; | |||
html += '<h4 class="law-tooltip-title">' + lawCode + ' - ' + data.title + '</h4>'; | |||
html += '<p class="law-tooltip-description">' + data.description + '</p>'; | |||
if (data.examples && data.examples.length > 0) { | |||
html += '<div class="law-tooltip-examples">'; | |||
html += '<strong>Примеры:</strong><ul>'; | |||
data.examples.slice(0, 5).forEach(function(example) { | |||
html += '<li>' + example + '</li>'; | |||
}); | |||
html += '</ul></div>'; | |||
} | |||
html += '</div>'; | |||
return html; | |||
}, | |||
initCellTooltips: function(tableCells, lawData) { | |||
tableCells.forEach(function(cell) { | |||
var link = cell.querySelector('a[href*="#"]'); | |||
if (!link) return; | |||
var href = link.getAttribute('href'); | |||
var match = href.match(/#(\d{3})/); | |||
if (!match) return; | |||
var lawCode = match[1]; | |||
var tooltipContent = LawTooltipsModule.createTooltipContent(lawCode, lawData); | |||
if (!tooltipContent) return; | |||
cell.classList.add('law-cell-with-tooltip'); | |||
cell.addEventListener('click', function(e) { | |||
e.preventDefault(); | |||
window.location.hash = lawCode; | |||
}); | |||
tippy(cell, { | |||
content: tooltipContent, | |||
allowHTML: true, | |||
interactive: true, | |||
placement: 'top', | |||
maxWidth: 400, | |||
theme: 'law-theme', | |||
arrow: true, | |||
duration: [200, 150], | |||
delay: [0, 50], | |||
appendTo: document.body, | |||
boundary: 'viewport', | |||
popperOptions: { | |||
strategy: 'fixed', | |||
modifiers: [ | |||
{ | |||
name: 'preventOverflow', | |||
options: { | |||
boundary: 'viewport', | |||
padding: 20, | |||
altAxis: true, | |||
altBoundary: true | |||
} | |||
}, | |||
{ | |||
name: 'flip', | |||
options: { | |||
fallbackPlacements: ['bottom', 'left', 'right'] | |||
} | |||
}, | |||
{ | |||
name: 'computeStyles', | |||
options: { adaptive: false } | |||
} | |||
] | |||
}, | |||
onShow: function(instance) { | |||
document.querySelectorAll('[data-tippy-root]').forEach(function(tooltip) { | |||
if (tooltip._tippy && tooltip._tippy !== instance) { | |||
tooltip._tippy.hide(); | |||
} | |||
}); | |||
} | |||
}); | |||
}); | }); | ||
} | } | ||
}; | |||
} | |||
var DataTooltipsModule = { | var DataTooltipsModule = { | ||