11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci{
41cb0ef41Sopenharmony_ci  function setupTheme() {
51cb0ef41Sopenharmony_ci    const kCustomPreference = 'customDarkTheme';
61cb0ef41Sopenharmony_ci    const userSettings = sessionStorage.getItem(kCustomPreference);
71cb0ef41Sopenharmony_ci    const themeToggleButton = document.getElementById('theme-toggle-btn');
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci    if (userSettings === null && window.matchMedia) {
101cb0ef41Sopenharmony_ci      const mq = window.matchMedia('(prefers-color-scheme: dark)');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci      if ('onchange' in mq) {
131cb0ef41Sopenharmony_ci        function mqChangeListener(e) {
141cb0ef41Sopenharmony_ci          document.documentElement.classList.toggle('dark-mode', e.matches);
151cb0ef41Sopenharmony_ci        }
161cb0ef41Sopenharmony_ci        mq.addEventListener('change', mqChangeListener);
171cb0ef41Sopenharmony_ci        if (themeToggleButton) {
181cb0ef41Sopenharmony_ci          themeToggleButton.addEventListener(
191cb0ef41Sopenharmony_ci            'click',
201cb0ef41Sopenharmony_ci            function() {
211cb0ef41Sopenharmony_ci              mq.removeEventListener('change', mqChangeListener);
221cb0ef41Sopenharmony_ci            },
231cb0ef41Sopenharmony_ci            { once: true },
241cb0ef41Sopenharmony_ci          );
251cb0ef41Sopenharmony_ci        }
261cb0ef41Sopenharmony_ci      }
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci      if (mq.matches) {
291cb0ef41Sopenharmony_ci        document.documentElement.classList.add('dark-mode');
301cb0ef41Sopenharmony_ci      }
311cb0ef41Sopenharmony_ci    } else if (userSettings === 'true') {
321cb0ef41Sopenharmony_ci      document.documentElement.classList.add('dark-mode');
331cb0ef41Sopenharmony_ci    }
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci    if (themeToggleButton) {
361cb0ef41Sopenharmony_ci      themeToggleButton.hidden = false;
371cb0ef41Sopenharmony_ci      themeToggleButton.addEventListener('click', function() {
381cb0ef41Sopenharmony_ci        sessionStorage.setItem(
391cb0ef41Sopenharmony_ci          kCustomPreference,
401cb0ef41Sopenharmony_ci          document.documentElement.classList.toggle('dark-mode'),
411cb0ef41Sopenharmony_ci        );
421cb0ef41Sopenharmony_ci      });
431cb0ef41Sopenharmony_ci    }
441cb0ef41Sopenharmony_ci  }
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  function setupPickers() {
471cb0ef41Sopenharmony_ci    function closeAllPickers() {
481cb0ef41Sopenharmony_ci      for (const picker of pickers) {
491cb0ef41Sopenharmony_ci        picker.parentNode.classList.remove('expanded');
501cb0ef41Sopenharmony_ci      }
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci      window.removeEventListener('click', closeAllPickers);
531cb0ef41Sopenharmony_ci      window.removeEventListener('keydown', onKeyDown);
541cb0ef41Sopenharmony_ci    }
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci    function onKeyDown(e) {
571cb0ef41Sopenharmony_ci      if (e.key === 'Escape') {
581cb0ef41Sopenharmony_ci        closeAllPickers();
591cb0ef41Sopenharmony_ci      }
601cb0ef41Sopenharmony_ci    }
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci    const pickers = document.querySelectorAll('.picker-header > a');
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ci    for (const picker of pickers) {
651cb0ef41Sopenharmony_ci      const parentNode = picker.parentNode;
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci      picker.addEventListener('click', function(e) {
681cb0ef41Sopenharmony_ci        e.preventDefault();
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci        /*
711cb0ef41Sopenharmony_ci          closeAllPickers as window event trigger already closed all the pickers,
721cb0ef41Sopenharmony_ci          if it already closed there is nothing else to do here
731cb0ef41Sopenharmony_ci        */
741cb0ef41Sopenharmony_ci        if (parentNode.classList.contains('expanded')) {
751cb0ef41Sopenharmony_ci          return;
761cb0ef41Sopenharmony_ci        }
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci        /*
791cb0ef41Sopenharmony_ci          In the next frame reopen the picker if needed and also setup events
801cb0ef41Sopenharmony_ci          to close pickers if needed.
811cb0ef41Sopenharmony_ci        */
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci        requestAnimationFrame(function() {
841cb0ef41Sopenharmony_ci          parentNode.classList.add('expanded');
851cb0ef41Sopenharmony_ci          window.addEventListener('click', closeAllPickers);
861cb0ef41Sopenharmony_ci          window.addEventListener('keydown', onKeyDown);
871cb0ef41Sopenharmony_ci        });
881cb0ef41Sopenharmony_ci      });
891cb0ef41Sopenharmony_ci    }
901cb0ef41Sopenharmony_ci  }
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci  function setupStickyHeaders() {
931cb0ef41Sopenharmony_ci    const header = document.querySelector('.header');
941cb0ef41Sopenharmony_ci    let ignoreNextIntersection = false;
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci    new IntersectionObserver(
971cb0ef41Sopenharmony_ci      function(e) {
981cb0ef41Sopenharmony_ci        const currentStatus = header.classList.contains('is-pinned');
991cb0ef41Sopenharmony_ci        const newStatus = e[0].intersectionRatio < 1;
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci        // Same status, do nothing
1021cb0ef41Sopenharmony_ci        if (currentStatus === newStatus) {
1031cb0ef41Sopenharmony_ci          return;
1041cb0ef41Sopenharmony_ci        } else if (ignoreNextIntersection) {
1051cb0ef41Sopenharmony_ci          ignoreNextIntersection = false;
1061cb0ef41Sopenharmony_ci          return;
1071cb0ef41Sopenharmony_ci        }
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci        /*
1101cb0ef41Sopenharmony_ci          To avoid flickering, ignore the next changes event that is triggered
1111cb0ef41Sopenharmony_ci          as the visible elements in the header change once we pin it.
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci          The timer is reset anyway after few milliseconds.
1141cb0ef41Sopenharmony_ci        */
1151cb0ef41Sopenharmony_ci        ignoreNextIntersection = true;
1161cb0ef41Sopenharmony_ci        setTimeout(function() {
1171cb0ef41Sopenharmony_ci          ignoreNextIntersection = false;
1181cb0ef41Sopenharmony_ci        }, 50);
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci        header.classList.toggle('is-pinned', newStatus);
1211cb0ef41Sopenharmony_ci      },
1221cb0ef41Sopenharmony_ci      { threshold: [1] },
1231cb0ef41Sopenharmony_ci    ).observe(header);
1241cb0ef41Sopenharmony_ci  }
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  function setupAltDocsLink() {
1271cb0ef41Sopenharmony_ci    const linkWrapper = document.getElementById('alt-docs');
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci    function updateHashes() {
1301cb0ef41Sopenharmony_ci      for (const link of linkWrapper.querySelectorAll('a')) {
1311cb0ef41Sopenharmony_ci        link.hash = location.hash;
1321cb0ef41Sopenharmony_ci      }
1331cb0ef41Sopenharmony_ci    }
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci    addEventListener('hashchange', updateHashes);
1361cb0ef41Sopenharmony_ci    updateHashes();
1371cb0ef41Sopenharmony_ci  }
1381cb0ef41Sopenharmony_ci
1391cb0ef41Sopenharmony_ci  function setupCopyButton() {
1401cb0ef41Sopenharmony_ci    const buttons = document.querySelectorAll('.copy-button');
1411cb0ef41Sopenharmony_ci    buttons.forEach((button) => {
1421cb0ef41Sopenharmony_ci      button.addEventListener('click', (el) => {
1431cb0ef41Sopenharmony_ci        const parentNode = el.target.parentNode;
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci        const flavorSelector = parentNode.querySelector('.js-flavor-selector');
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci        let code = '';
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci        if (flavorSelector) {
1501cb0ef41Sopenharmony_ci          if (flavorSelector.checked) {
1511cb0ef41Sopenharmony_ci            code = parentNode.querySelector('.mjs').textContent;
1521cb0ef41Sopenharmony_ci          } else {
1531cb0ef41Sopenharmony_ci            code = parentNode.querySelector('.cjs').textContent;
1541cb0ef41Sopenharmony_ci          }
1551cb0ef41Sopenharmony_ci        } else {
1561cb0ef41Sopenharmony_ci          code = parentNode.querySelector('code').textContent;
1571cb0ef41Sopenharmony_ci        }
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci        button.textContent = 'Copied';
1601cb0ef41Sopenharmony_ci        navigator.clipboard.writeText(code);
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci        setTimeout(() => {
1631cb0ef41Sopenharmony_ci          button.textContent = 'Copy';
1641cb0ef41Sopenharmony_ci        }, 2500);
1651cb0ef41Sopenharmony_ci      });
1661cb0ef41Sopenharmony_ci    });
1671cb0ef41Sopenharmony_ci  }
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci  function bootstrap() {
1701cb0ef41Sopenharmony_ci    // Check if we have JavaScript support.
1711cb0ef41Sopenharmony_ci    document.documentElement.classList.add('has-js');
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci    // Restore user mode preferences.
1741cb0ef41Sopenharmony_ci    setupTheme();
1751cb0ef41Sopenharmony_ci
1761cb0ef41Sopenharmony_ci    // Handle pickers with click/taps rather than hovers.
1771cb0ef41Sopenharmony_ci    setupPickers();
1781cb0ef41Sopenharmony_ci
1791cb0ef41Sopenharmony_ci    // Track when the header is in sticky position.
1801cb0ef41Sopenharmony_ci    setupStickyHeaders();
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci    // Make link to other versions of the doc open to the same hash target (if it exists).
1831cb0ef41Sopenharmony_ci    setupAltDocsLink();
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ci    setupCopyButton();
1861cb0ef41Sopenharmony_ci  }
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci  if (document.readyState === 'loading') {
1891cb0ef41Sopenharmony_ci    document.addEventListener('DOMContentLoaded', bootstrap, { once: true });
1901cb0ef41Sopenharmony_ci  } else {
1911cb0ef41Sopenharmony_ci    bootstrap();
1921cb0ef41Sopenharmony_ci  }
1931cb0ef41Sopenharmony_ci}
194