version-picker.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const dropdown = document.querySelector('.version-picker .dropdown');
  2. const dropdownMenu = dropdown.querySelector('.dropdown-menu');
  3. fetchVersions(dropdown, dropdownMenu).then(() => {
  4. initializeVersionDropdown(dropdown, dropdownMenu);
  5. });
  6. /**
  7. * Initialize the dropdown functionality for version selection.
  8. *
  9. * @param {Element} dropdown - The dropdown element.
  10. * @param {Element} dropdownMenu - The dropdown menu element.
  11. */
  12. function initializeVersionDropdown(dropdown, dropdownMenu) {
  13. // Toggle the dropdown menu on click
  14. dropdown.addEventListener('click', function () {
  15. this.setAttribute('tabindex', 1);
  16. this.classList.toggle('active');
  17. dropdownMenu.style.display = (dropdownMenu.style.display === 'block') ? 'none' : 'block';
  18. });
  19. // Remove the 'active' class and hide the dropdown menu on focusout
  20. dropdown.addEventListener('focusout', function () {
  21. this.classList.remove('active');
  22. dropdownMenu.style.display = 'none';
  23. });
  24. // Handle item selection within the dropdown menu
  25. const dropdownMenuItems = dropdownMenu.querySelectorAll('li');
  26. dropdownMenuItems.forEach(function (item) {
  27. item.addEventListener('click', function () {
  28. dropdownMenuItems.forEach(function (item) {
  29. item.classList.remove('active');
  30. });
  31. this.classList.add('active');
  32. dropdown.querySelector('span').textContent = this.textContent;
  33. dropdown.querySelector('input').value = this.getAttribute('id');
  34. window.location.href = changeVersion(window.location.href, this.textContent);
  35. });
  36. });
  37. };
  38. /**
  39. * This function fetches the available versions from a GitHub repository
  40. * and inserts them into the version picker.
  41. *
  42. * @param {Element} dropdown - The dropdown element.
  43. * @param {Element} dropdownMenu - The dropdown menu element.
  44. * @returns {Promise<Array<string>>} A promise that resolves with an array of available versions.
  45. */
  46. function fetchVersions(dropdown, dropdownMenu) {
  47. return new Promise((resolve, reject) => {
  48. window.addEventListener("load", () => {
  49. fetch("https://api.github.com/repos/matrix-org/synapse/git/trees/gh-pages", {
  50. cache: "force-cache",
  51. }).then(res =>
  52. res.json()
  53. ).then(resObject => {
  54. const excluded = ['dev-docs', 'v1.91.0', 'v1.80.0', 'v1.69.0'];
  55. const tree = resObject.tree.filter(item => item.type === "tree" && !excluded.includes(item.path));
  56. const versions = tree.map(item => item.path).sort(sortVersions);
  57. // Create a list of <li> items for versions
  58. versions.forEach((version) => {
  59. const li = document.createElement("li");
  60. li.textContent = version;
  61. li.id = version;
  62. if (window.SYNAPSE_VERSION === version) {
  63. li.classList.add('active');
  64. dropdown.querySelector('span').textContent = version;
  65. dropdown.querySelector('input').value = version;
  66. }
  67. dropdownMenu.appendChild(li);
  68. });
  69. resolve(versions);
  70. }).catch(ex => {
  71. console.error("Failed to fetch version data", ex);
  72. reject(ex);
  73. })
  74. });
  75. });
  76. }
  77. /**
  78. * Custom sorting function to sort an array of version strings.
  79. *
  80. * @param {string} a - The first version string to compare.
  81. * @param {string} b - The second version string to compare.
  82. * @returns {number} - A negative number if a should come before b, a positive number if b should come before a, or 0 if they are equal.
  83. */
  84. function sortVersions(a, b) {
  85. // Put 'develop' and 'latest' at the top
  86. if (a === 'develop' || a === 'latest') return -1;
  87. if (b === 'develop' || b === 'latest') return 1;
  88. const versionA = (a.match(/v\d+(\.\d+)+/) || [])[0];
  89. const versionB = (b.match(/v\d+(\.\d+)+/) || [])[0];
  90. return versionB.localeCompare(versionA);
  91. }
  92. /**
  93. * Change the version in a URL path.
  94. *
  95. * @param {string} url - The original URL to be modified.
  96. * @param {string} newVersion - The new version to replace the existing version in the URL.
  97. * @returns {string} The updated URL with the new version.
  98. */
  99. function changeVersion(url, newVersion) {
  100. const parsedURL = new URL(url);
  101. const pathSegments = parsedURL.pathname.split('/');
  102. // Modify the version
  103. pathSegments[2] = newVersion;
  104. // Reconstruct the URL
  105. parsedURL.pathname = pathSegments.join('/');
  106. return parsedURL.href;
  107. }