MediaWiki:Common.js: відмінності між версіями

нема опису редагування
Немає опису редагування
Немає опису редагування
Рядок 531: Рядок 531:
             $icon.addClass('inactive');
             $icon.addClass('inactive');
         }
         }
    });
});
// ==================================================
// L-BOX NAVIGATION — замінює існуючий блок у Common.js
// ==================================================
$(function () {
    var $lbox = $('.l-box');
    if ($lbox.length === 0) return;
    var $items = $lbox.find('.l-box-item');
    // ── Будуємо карту секцій ──
    // Перераховуємо при кожному activate щоб врахувати
    // динамічний контент (таби, lazy load)
    function buildSectionMap() {
        var map = [];
        $items.each(function () {
            var target = $(this).data('target');
            if (target === 'top') {
                map.push({ $item: $(this), top: 0 });
            } else {
                // MediaWiki генерує id для h2 — пробіли замінює на _
                var $el = $('#' + target);
                if ($el.length) {
                    map.push({ $item: $(this), top: $el.offset().top });
                }
            }
        });
        // Сортуємо за позицією (на всяк випадок)
        map.sort(function (a, b) { return a.top - b.top; });
        return map;
    }
    // ── Оновлення активного пункту ──
    function updateActive(map) {
        var scrollY  = $(window).scrollTop() + 110; // offset для header
        var current  = null;
        for (var i = 0; i < map.length; i++) {
            if (map[i].top <= scrollY) {
                current = map[i].$item;
            }
        }
        $items.removeClass('active');
        if (current) current.addClass('active');
    }
    // ── Клік — скрол до секції ──
    $items.on('click', function () {
        var target = $(this).data('target');
        if (target === 'top') {
            $('html, body').animate({ scrollTop: 0 }, 280);
        } else {
            var $el = $('#' + target);
            if ($el.length) {
                var offset = $el.offset().top - 75;
                $('html, body').animate({ scrollTop: offset }, 280);
            }
        }
    });
    // ── Ініціалізація та підписка на scroll ──
    var sectionMap = buildSectionMap();
    // Перебудовуємо після можливого lazy-load (таби)
    $(document).on('mcc:content-loaded', function () {
        sectionMap = buildSectionMap();
        updateActive(sectionMap);
    });
    var ticking = false;
    $(window).on('scroll', function () {
        if (!ticking) {
            window.requestAnimationFrame(function () {
                updateActive(sectionMap);
                ticking = false;
            });
            ticking = true;
        }
    });
    // Перший виклик
    updateActive(sectionMap);
    // Перебудовуємо після resize (позиції можуть змінитись)
    $(window).on('resize', function () {
        sectionMap = buildSectionMap();
        updateActive(sectionMap);
     });
     });
});
});