Mediawiki:common.js

Вернуться

Строка 106: Строка 106:
     };
     };
      
      
    var LawTooltipsModule = {
var LawTooltipsModule = {
        init: function() {
    init: function() {
            var tableCells = document.querySelectorAll('.law-tooltips td, .law-tooltips th');
        var tableCells = document.querySelectorAll('.law-tooltips td, .law-tooltips th');
            if (tableCells.length === 0) return;
        if (tableCells.length === 0) return;
           
            MediaWikiTooltips.load(function() {
        function run() {
                var lawData = LawTooltipsModule.extractLawData();
            var lawData = LawTooltipsModule.extractLawData();
                LawTooltipsModule.initCellTooltips(tableCells, lawData);
            if (Object.keys(lawData).length > 0) {
            });
                LawTooltipsModule.initCellTooltips(tableCells, lawData);
        },
                return true;
       
            }
        extractLawData: function() {
            return false;
    var lawData = {};
        }
    var detailTables = document.querySelectorAll('.mw-collapsible-content table');
   
        MediaWikiTooltips.load(function() {
    detailTables.forEach(function(table) {
            if (run()) return;
        var rows = table.querySelectorAll('tr');
            setTimeout(function() {
       
                if (run()) return;
        rows.forEach(function(row) {
                setTimeout(run, 500);
            var cells = row.querySelectorAll('td');
            }, 300);
            if (cells.length < 3) return;
        });
           
    },
            var crimeCell = cells[0];
            var descriptionCell = cells[1];
    extractLawData: function() {
            var exampleCell = cells[2];
        var lawData = {};
           
        var containers = document.querySelectorAll('.mw-collapsible');
            var anchor = crimeCell.querySelector('[id]');
        containers.forEach(function(block) {
            if (!anchor) return;
            var content = block.querySelector('.mw-collapsible-content');
           
            if (content && block.classList.contains('mw-collapsed')) {
            var lawCode = anchor.id;
                block.classList.remove('mw-collapsed');
            var titleElement = crimeCell.querySelector('b');
                var toggle = content.querySelector('.mw-collapsible-toggle');
           
                if (toggle) toggle.setAttribute('aria-expanded', 'true');
            if (titleElement && lawCode.match(/^\d{3}$/)) {
            }
                var titleText = titleElement.textContent.trim();
        });
                var lawTitle = titleText.replace(/^\d{3}\s*/, '').replace(/^\d{3}/, '').trim();
                var description = descriptionCell.innerHTML.trim();
        var detailTables = document.querySelectorAll('.mw-collapsible-content table');
               
        detailTables.forEach(function(table) {
                var examples = [];
            if (table.classList.contains('law-tooltips')) return;
                var exampleItems = exampleCell.querySelectorAll('li');
            var rows = table.querySelectorAll('tr');
exampleItems.forEach(function(item) {
    var html = item.innerHTML.trim();
            rows.forEach(function(row) {
    // Удаляем все <br> теги
                var cells = row.querySelectorAll('td');
    html = html.replace(/<br\s*\/?>/gi, '');
                if (cells.length < 3) return;
    if (html) examples.push(html);
});
                var crimeCell = cells[0];
               
                var descriptionCell = cells[1];
                lawData[lawCode] = {
                var exampleCell = cells[2];
                    title: lawTitle,
                    description: description,
                var anchor = crimeCell.querySelector('[id]');
                    examples: examples
                if (!anchor) return;
                };
            }
                var lawCode = anchor.id;
        });
                var titleElement = crimeCell.querySelector('b');
    });
                if (!titleElement || !lawCode.match(/^\d{3}$/)) return;
   
    return lawData;
                var titleText = titleElement.textContent.trim();
},
                var lawTitle = titleText.replace(/^\d{3}\s*/, '').replace(/^\d{3}/, '').trim();
       
                var description = descriptionCell.innerHTML.trim();
    createTooltipContent: function(lawCode, lawData) {
    var data = lawData[lawCode];
                var examples = [];
    if (!data) return null;
                var exampleItems = exampleCell.querySelectorAll('li');
   
                exampleItems.forEach(function(item) {
    var html = '<div class="law-tooltip">';
                    var html = item.innerHTML.trim().replace(/<br\s*\/?>/gi, '');
    html += '<h4 class="law-tooltip-title">' + lawCode + ' - ' + data.title + '</h4>';
                    if (html) examples.push(html);
    html += '<p class="law-tooltip-description">' + data.description + '</p>';
                });
   
    if (data.examples && data.examples.length > 0) {
                lawData[lawCode] = {
        html += '<div class="law-tooltip-examples">';
                    title: lawTitle,
        html += '<strong>Примеры:</strong><ul>';
                    description: description,
        data.examples.slice(0, 5).forEach(function(example) {
                    examples: examples
            html += '<li>' + example + '</li>'; // Теперь example содержит HTML
                };
            });
        });
        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();
                        }
                    });
                }
            });
        });
        });
        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 = {