This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Action Bot
committed
Dec 11, 2023
1 parent
9f7c038
commit fd5cad8
Showing
6 changed files
with
234 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
.version-picker { | ||
display: flex; | ||
align-items: center; | ||
} | ||
|
||
.version-picker .dropdown { | ||
width: 130px; | ||
max-height: 29px; | ||
margin-left: 10px; | ||
display: inline-block; | ||
border-radius: 4px; | ||
border: 1px solid var(--theme-popup-border); | ||
position: relative; | ||
font-size: 13px; | ||
color: var(--fg); | ||
height: 100%; | ||
text-align: left; | ||
} | ||
.version-picker .dropdown .select { | ||
cursor: pointer; | ||
display: block; | ||
padding: 5px 2px 5px 15px; | ||
} | ||
.version-picker .dropdown .select > i { | ||
font-size: 10px; | ||
color: var(--fg); | ||
cursor: pointer; | ||
float: right; | ||
line-height: 20px !important; | ||
} | ||
.version-picker .dropdown:hover { | ||
border: 1px solid var(--theme-popup-border); | ||
} | ||
.version-picker .dropdown:active { | ||
background-color: var(--theme-popup-bg); | ||
} | ||
.version-picker .dropdown.active:hover, | ||
.version-picker .dropdown.active { | ||
border: 1px solid var(--theme-popup-border); | ||
border-radius: 2px 2px 0 0; | ||
background-color: var(--theme-popup-bg); | ||
} | ||
.version-picker .dropdown.active .select > i { | ||
transform: rotate(-180deg); | ||
} | ||
.version-picker .dropdown .dropdown-menu { | ||
position: absolute; | ||
background-color: var(--theme-popup-bg); | ||
width: 100%; | ||
left: -1px; | ||
right: 1px; | ||
margin-top: 1px; | ||
border: 1px solid var(--theme-popup-border); | ||
border-radius: 0 0 4px 4px; | ||
overflow: hidden; | ||
display: none; | ||
max-height: 300px; | ||
overflow-y: auto; | ||
z-index: 9; | ||
} | ||
.version-picker .dropdown .dropdown-menu li { | ||
font-size: 12px; | ||
padding: 6px 20px; | ||
cursor: pointer; | ||
} | ||
.version-picker .dropdown .dropdown-menu { | ||
padding: 0; | ||
list-style: none; | ||
} | ||
.version-picker .dropdown .dropdown-menu li:hover { | ||
background-color: var(--theme-hover); | ||
} | ||
.version-picker .dropdown .dropdown-menu li.active::before { | ||
display: inline-block; | ||
content: "✓"; | ||
margin-inline-start: -14px; | ||
width: 14px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
|
||
const dropdown = document.querySelector('.version-picker .dropdown'); | ||
const dropdownMenu = dropdown.querySelector('.dropdown-menu'); | ||
|
||
fetchVersions(dropdown, dropdownMenu).then(() => { | ||
initializeVersionDropdown(dropdown, dropdownMenu); | ||
}); | ||
|
||
/** | ||
* Initialize the dropdown functionality for version selection. | ||
* | ||
* @param {Element} dropdown - The dropdown element. | ||
* @param {Element} dropdownMenu - The dropdown menu element. | ||
*/ | ||
function initializeVersionDropdown(dropdown, dropdownMenu) { | ||
// Toggle the dropdown menu on click | ||
dropdown.addEventListener('click', function () { | ||
this.setAttribute('tabindex', 1); | ||
this.classList.toggle('active'); | ||
dropdownMenu.style.display = (dropdownMenu.style.display === 'block') ? 'none' : 'block'; | ||
}); | ||
|
||
// Remove the 'active' class and hide the dropdown menu on focusout | ||
dropdown.addEventListener('focusout', function () { | ||
this.classList.remove('active'); | ||
dropdownMenu.style.display = 'none'; | ||
}); | ||
|
||
// Handle item selection within the dropdown menu | ||
const dropdownMenuItems = dropdownMenu.querySelectorAll('li'); | ||
dropdownMenuItems.forEach(function (item) { | ||
item.addEventListener('click', function () { | ||
dropdownMenuItems.forEach(function (item) { | ||
item.classList.remove('active'); | ||
}); | ||
this.classList.add('active'); | ||
dropdown.querySelector('span').textContent = this.textContent; | ||
dropdown.querySelector('input').value = this.getAttribute('id'); | ||
|
||
window.location.href = changeVersion(window.location.href, this.textContent); | ||
}); | ||
}); | ||
}; | ||
|
||
/** | ||
* This function fetches the available versions from a GitHub repository | ||
* and inserts them into the version picker. | ||
* | ||
* @param {Element} dropdown - The dropdown element. | ||
* @param {Element} dropdownMenu - The dropdown menu element. | ||
* @returns {Promise<Array<string>>} A promise that resolves with an array of available versions. | ||
*/ | ||
function fetchVersions(dropdown, dropdownMenu) { | ||
return new Promise((resolve, reject) => { | ||
window.addEventListener("load", () => { | ||
|
||
fetch("https://api.github.com/repos/matrix-org/synapse/git/trees/gh-pages", { | ||
cache: "force-cache", | ||
}).then(res => | ||
res.json() | ||
).then(resObject => { | ||
const excluded = ['dev-docs', 'v1.91.0', 'v1.80.0', 'v1.69.0']; | ||
const tree = resObject.tree.filter(item => item.type === "tree" && !excluded.includes(item.path)); | ||
const versions = tree.map(item => item.path).sort(sortVersions); | ||
|
||
// Create a list of <li> items for versions | ||
versions.forEach((version) => { | ||
const li = document.createElement("li"); | ||
li.textContent = version; | ||
li.id = version; | ||
|
||
if (window.SYNAPSE_VERSION === version) { | ||
li.classList.add('active'); | ||
dropdown.querySelector('span').textContent = version; | ||
dropdown.querySelector('input').value = version; | ||
} | ||
|
||
dropdownMenu.appendChild(li); | ||
}); | ||
|
||
resolve(versions); | ||
|
||
}).catch(ex => { | ||
console.error("Failed to fetch version data", ex); | ||
reject(ex); | ||
}) | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* Custom sorting function to sort an array of version strings. | ||
* | ||
* @param {string} a - The first version string to compare. | ||
* @param {string} b - The second version string to compare. | ||
* @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. | ||
*/ | ||
function sortVersions(a, b) { | ||
// Put 'develop' and 'latest' at the top | ||
if (a === 'develop' || a === 'latest') return -1; | ||
if (b === 'develop' || b === 'latest') return 1; | ||
|
||
const versionA = (a.match(/v\d+(\.\d+)+/) || [])[0]; | ||
const versionB = (b.match(/v\d+(\.\d+)+/) || [])[0]; | ||
|
||
return versionB.localeCompare(versionA); | ||
} | ||
|
||
/** | ||
* Change the version in a URL path. | ||
* | ||
* @param {string} url - The original URL to be modified. | ||
* @param {string} newVersion - The new version to replace the existing version in the URL. | ||
* @returns {string} The updated URL with the new version. | ||
*/ | ||
function changeVersion(url, newVersion) { | ||
const parsedURL = new URL(url); | ||
const pathSegments = parsedURL.pathname.split('/'); | ||
|
||
// Modify the version | ||
pathSegments[2] = newVersion; | ||
|
||
// Reconstruct the URL | ||
parsedURL.pathname = pathSegments.join('/'); | ||
|
||
return parsedURL.href; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
window.SYNAPSE_VERSION = 'v1.40'; |