Участник:SpaceAndrey/common.js: различия между версиями

Материал из Space Station 14 Вики
Новая страница: «(function () { 'use strict'; function initLoreSwitchers() { document.querySelectorAll('.lore-switcher').forEach(function (sw) { var group = sw.getAttribute('data-lore-group') || ''; var btns = sw.querySelectorAll('.lore-switcher-btn'); var contents = document.querySelectorAll( '.lore-content[data-lore-group="' + group + '"]' ); if (!btns.length || !contents.length) r...»
 
Нет описания правки
 
Строка 1: Строка 1:
console.log('=== common.js SpaceAndrey загружен ===');
(function () {
(function () {
     'use strict';
     'use strict';

Текущая версия от 21:51, 29 сентября 2026

console.log('=== common.js SpaceAndrey загружен ===');
(function () {
    'use strict';

    function initLoreSwitchers() {
        document.querySelectorAll('.lore-switcher').forEach(function (sw) {
            var group = sw.getAttribute('data-lore-group') || '';
            var btns = sw.querySelectorAll('.lore-switcher-btn');
            var contents = document.querySelectorAll(
                '.lore-content[data-lore-group="' + group + '"]'
            );
            if (!btns.length || !contents.length) return;

            function activate(tab) {
                btns.forEach(function (b) {
                    b.classList.toggle('active', b.getAttribute('data-lore-tab') === tab);
                });
                contents.forEach(function (c) {
                    c.style.display =
                        (c.getAttribute('data-lore-tab') === tab) ? '' : 'none';
                });
            }

            btns.forEach(function (b) {
                b.addEventListener('click', function () {
                    var tab = b.getAttribute('data-lore-tab');
                    activate(tab);
                    try {
                        localStorage.setItem('lore-tab-' + group, tab);
                    } catch (e) {}
                });
            });

            var saved = null;
            try { saved = localStorage.getItem('lore-tab-' + group); } catch (e) {}
            var hasSaved = saved && Array.prototype.some.call(btns, function (b) {
                return b.getAttribute('data-lore-tab') === saved;
            });
            activate(hasSaved ? saved : btns[0].getAttribute('data-lore-tab'));
        });
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initLoreSwitchers);
    } else {
        initLoreSwitchers();
    }
})();