Участник:Predestinato/common.js

Материал из МК14 | Space Station 14 Wiki
Версия от 11:20, 7 апреля 2026; Predestinato (обсуждение | вклад) (Новая страница: «(function () { function getTargetAnchorFromHref(href) { if (!href || href.charAt(0) !== '#') return null; var id = href.slice(1); if (!/^a[0-9x]+$/i.test(id)) return null; return document.getElementById(id); } function ensureSectionOpen(anchor) { var section = anchor.closest('.law-section.mw-collapsible'); if (!section) return; if (!section.classList.contains('mw-collapsed')) return; var toggle = section.querySelector('...»)
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)

Замечание: Возможно, после публикации вам придётся очистить кэш своего браузера, чтобы увидеть изменения.

  • Firefox / Safari: Удерживая клавишу Shift, нажмите на панели инструментов Обновить либо нажмите Ctrl+F5 или Ctrl+R (⌘+R на Mac)
  • Google Chrome: Нажмите Ctrl+Shift+R (⌘+Shift+R на Mac)
  • Internet Explorer / Edge: Удерживая Ctrl, нажмите Обновить либо нажмите Ctrl+F5
  • Opera: Нажмите Ctrl+F5.
(function () {
  function getTargetAnchorFromHref(href) {
    if (!href || href.charAt(0) !== '#') return null;
    var id = href.slice(1);
    if (!/^a[0-9x]+$/i.test(id)) return null;
    return document.getElementById(id);
  }

  function ensureSectionOpen(anchor) {
    var section = anchor.closest('.law-section.mw-collapsible');
    if (!section) return;
    if (!section.classList.contains('mw-collapsed')) return;
    var toggle = section.querySelector('.mw-collapsible-toggle a, .mw-collapsible-toggle');
    if (toggle) toggle.click();
  }

  function headingForAnchor(anchor) {
    var node = anchor.nextElementSibling;
    while (node) {
      if (/^H[1-6]$/.test(node.tagName)) return node;
      node = node.nextElementSibling;
    }
    return null;
  }

  function clearHighlights() {
    document.querySelectorAll('.law-target-active').forEach(function (el) {
      el.classList.remove('law-target-active');
    });
    document.querySelectorAll('.law-section-active').forEach(function (el) {
      el.classList.remove('law-section-active');
    });
  }

  function smoothFocus(anchor) {
    ensureSectionOpen(anchor);
    var heading = headingForAnchor(anchor);
    var section = anchor.closest('.law-section');

    clearHighlights();
    if (heading) {
      heading.classList.add('law-target-heading', 'law-target-active');
    }
    if (section) {
      section.classList.add('law-section-active');
    }

    var targetNode = heading || anchor;
    targetNode.scrollIntoView({ behavior: 'smooth', block: 'center' });

    window.setTimeout(function () {
      if (heading) heading.classList.remove('law-target-active');
      if (section) section.classList.remove('law-section-active');
    }, 2600);
  }

  function handleHashNavigation() {
    var anchor = getTargetAnchorFromHref(window.location.hash);
    if (!anchor) return;
    smoothFocus(anchor);
  }

  function bindTableLinks() {
    document.addEventListener('click', function (event) {
      var link = event.target.closest('a[href^="#a"]');
      if (!link) return;
      var anchor = getTargetAnchorFromHref(link.getAttribute('href'));
      if (!anchor) return;

      event.preventDefault();
      if (window.location.hash !== link.getAttribute('href')) {
        history.pushState(null, '', link.getAttribute('href'));
      }
      smoothFocus(anchor);
    });
  }

  function init() {
    bindTableLinks();
    handleHashNavigation();
    window.addEventListener('hashchange', handleHashNavigation);
  }

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