github.com/argoproj/argo-cd/v3@v3.2.1/docs/assets/versions.js (about)

     1  const targetNode = document.querySelector('.md-header__inner');
     2  const observerOptions = {
     3    childList: true,
     4    subtree: true
     5  };
     6  
     7  const VERSION_REGEX = /\/en\/(release-(?:v\d+|[\d\.]+|\w+)|latest|stable)\//;
     8  
     9  const observerCallback = function(mutationsList, observer) {
    10    for (let mutation of mutationsList) {
    11      if (mutation.type === 'childList') {
    12        const titleElement = document.querySelector('.md-header__inner > .md-header__title');
    13        if (titleElement) {
    14          initializeVersionDropdown();
    15          observer.disconnect();
    16        }
    17      }
    18    }
    19  };
    20  
    21  const observer = new MutationObserver(observerCallback);
    22  observer.observe(targetNode, observerOptions);
    23  
    24  function getCurrentVersion() {
    25    const currentVersion = window.location.href.match(VERSION_REGEX);
    26    if (currentVersion && currentVersion.length > 1) {
    27      return currentVersion[1];
    28    }
    29    return null;
    30  }
    31  
    32  function initializeVersionDropdown() {
    33    const callbackName = 'callback_' + new Date().getTime();
    34    window[callbackName] = function(response) {
    35      const div = document.createElement('div');
    36      div.innerHTML = response.html;
    37      const headerTitle = document.querySelector(".md-header__inner > .md-header__title");
    38      if (headerTitle) {
    39        headerTitle.appendChild(div);
    40      }
    41  
    42      const container = div.querySelector('.rst-versions');
    43      if (!container) return; // Exit if container not found
    44  
    45      // Add caret icon
    46      var caret = document.createElement('div');
    47      caret.innerHTML = "<i class='fa fa-caret-down dropdown-caret'></i>";
    48      caret.classList.add('dropdown-caret');
    49      const currentVersionElem = div.querySelector('.rst-current-version');
    50      if (currentVersionElem) {
    51        currentVersionElem.appendChild(caret);
    52      }
    53  
    54      // Add click listener to toggle dropdown
    55      if (currentVersionElem && container) {
    56        currentVersionElem.addEventListener('click', function() {
    57          container.classList.toggle('shift-up');
    58        });
    59      }
    60  
    61      // Sorting Logic
    62      sortVersionLinks(container);
    63    };
    64  
    65    // Load CSS
    66    var CSSLink = document.createElement('link');
    67    CSSLink.rel = 'stylesheet';
    68    CSSLink.href = '/assets/versions.css';
    69    document.getElementsByTagName('head')[0].appendChild(CSSLink);
    70  
    71    // Load JSONP Script
    72    var script = document.createElement('script');
    73    const currentVersion = getCurrentVersion();
    74    script.src = 'https://argo-cd.readthedocs.io/_/api/v2/footer_html/?' +
    75        'callback=' + callbackName + '&project=argo-cd&page=&theme=mkdocs&format=jsonp&docroot=docs&source_suffix=.md&version=' + (currentVersion || 'latest');
    76    document.getElementsByTagName('head')[0].appendChild(script);
    77  }
    78  
    79  // Function to sort version links
    80  function sortVersionLinks(container) {
    81    // Find all <dl> elements within the container
    82    const dlElements = container.querySelectorAll('dl');
    83  
    84    dlElements.forEach(dl => {
    85      const ddElements = Array.from(dl.querySelectorAll('dd'));
    86  
    87      // Check if ddElements contain version links
    88      const isVersionDl = ddElements.some(dd => {
    89        const link = dd.querySelector('a');
    90        return VERSION_REGEX.test(link?.getAttribute?.('href'));
    91      });
    92  
    93      // This dl contains version links, proceed to sort
    94      if (isVersionDl) {
    95        // Define sorting criteria
    96        ddElements.sort((a, b) => {
    97          const aText = a.textContent.trim().toLowerCase();
    98          const bText = b.textContent.trim().toLowerCase();
    99  
   100          // Prioritize 'latest' and 'stable'
   101          if (aText === 'latest') return -1;
   102          if (bText === 'latest') return 1;
   103          if (aText === 'stable') return -1;
   104          if (bText === 'stable') return 1;
   105  
   106          // Extract version numbers (e.g., release-2.9)
   107          const aVersionMatch = aText.match(/release-(\d+(\.\d+)*)/);
   108          const bVersionMatch = bText.match(/release-(\d+(\.\d+)*)/);
   109  
   110          if (aVersionMatch && bVersionMatch) {
   111            const aVersion = aVersionMatch[1].split('.').map(Number);
   112            const bVersion = bVersionMatch[1].split('.').map(Number);
   113  
   114            for (let i = 0; i < Math.max(aVersion.length, bVersion.length); i++) {
   115              const aNum = aVersion[i] || 0;
   116              const bNum = bVersion[i] || 0;
   117              if (aNum > bNum) return -1;
   118              if (aNum < bNum) return 1;
   119            }
   120            return 0;
   121          }
   122  
   123          // Fallback to alphabetical order
   124          return aText.localeCompare(bText);
   125        });
   126  
   127        // Remove existing <dd> elements
   128        ddElements.forEach(dd => dl.removeChild(dd));
   129  
   130        // Append sorted <dd> elements
   131        ddElements.forEach(dd => dl.appendChild(dd));
   132      }
   133    });
   134  }
   135  
   136  // VERSION WARNINGS
   137  window.addEventListener("DOMContentLoaded", function() {
   138    var margin = 30;
   139    var headerHeight = document.getElementsByClassName("md-header")[0].offsetHeight;
   140    const currentVersion = getCurrentVersion();
   141    if (currentVersion && currentVersion !== "stable") {
   142      if (currentVersion === "latest") {
   143        document.querySelector("div[data-md-component=announce]").innerHTML = "<div id='announce-msg'>You are viewing the docs for an unreleased version of Argo CD, <a href='https://argo-cd.readthedocs.io/en/stable/'>click here to go to the latest stable version.</a></div>";
   144      } else {
   145        document.querySelector("div[data-md-component=announce]").innerHTML = "<div id='announce-msg'>You are viewing the docs for a previous version of Argo CD, <a href='https://argo-cd.readthedocs.io/en/stable/'>click here to go to the latest stable version.</a></div>";
   146      }
   147      var bannerHeight = document.getElementById('announce-msg').offsetHeight + margin;
   148      document.querySelector("header.md-header").style.top = bannerHeight + "px";
   149      document.querySelector('style').textContent +=
   150          "@media screen and (min-width: 76.25em){ .md-sidebar { height: 0;  top:" + (bannerHeight + headerHeight) + "px !important; }}";
   151      document.querySelector('style').textContent +=
   152          "@media screen and (min-width: 60em){ .md-sidebar--secondary { height: 0;  top:" + (bannerHeight + headerHeight) + "px !important; }}";
   153    }
   154  });