Skip to content

Commit

Permalink
Update installer2.html
Browse files Browse the repository at this point in the history
  • Loading branch information
alf45tar authored Aug 17, 2024
1 parent 6ab1093 commit 999a569
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions installer2.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
</select>

<script>
// GitHub repository and API URL for fetching tags
const GITHUB_REPO = "alf45tar/PedalinoMini";
const GITHUB_API_URL = `https://api.github.com/repos/${GITHUB_REPO}/git/refs/tags`;

// Mapping of board keys to their display names
const boards = {
"esp32doit-devkit-v1": "Esp32 DOIT DevKit v1",
"ttgo-t-eight": "TTGO T-Eight",
Expand All @@ -26,70 +28,92 @@
"lilygo-t-display-s3": "LilyGO T-Display-S3"
};

// Fetches tags from the GitHub repository
async function fetchTags() {
try {
const response = await fetch(GITHUB_API_URL);
const tags = await response.json();
return tags;
} catch (error) {
console.error("Errore durante il recupero dei tag da GitHub:", error);
console.error("Error fetching tags from GitHub:", error);
}
}

// Compares two version numbers to determine order
function compareVersions(v1, v2) {
const parts1 = v1.split('.').map(Number);
const parts2 = v2.split('.').map(Number);

for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
const part1 = parts1[i] || 0;
const part2 = parts2[i] || 0;
if (part1 > part2) return -1;
if (part1 < part2) return 1;
if (part1 > part2) return -1; // Newer version first
if (part1 < part2) return 1; // Older version later
}
return 0;
return 0; // Versions are equal
}

// Checks if a JSON file exists at the given URL
async function checkFileExists(url) {
try {
const response = await fetch(url, { method: 'HEAD' });
return response.ok;
} catch (error) {
console.error("Errore durante la verifica del file:", error);
console.error("Error checking file existence:", error);
return false;
}
}

// Generates the dropdown menu by fetching and filtering versions
async function generateDropdown() {
const tags = await fetchTags();
const tags = await fetchTags(); // Get all tags from GitHub
const selectElement = document.getElementById('firmware-select');

// Estrarre le versioni dai tag e ordinarle
const sortedTags = tags
.map(tag => tag.ref.split('/').pop().replace('v', ''))
.sort(compareVersions);
const versionMap = {};

for (const [boardKey, boardName] of Object.entries(boards)) {
const optgroup = document.createElement('optgroup');
optgroup.label = boardName;
// Iterate over all tags
for (const tag of tags) {
const versionNumber = tag.ref.split('/').pop().replace('v', ''); // Extract version number

for (const versionNumber of sortedTags) {
// Iterate over all boards
for (const [boardKey, boardName] of Object.entries(boards)) {
// Construct the URL for the JSON file
const url = `https://raw.githubusercontent.com/${GITHUB_REPO}/v${versionNumber}/firmware/${boardKey}/${boardKey}-${versionNumber}.json`;
const exists = await checkFileExists(url);

const exists = await checkFileExists(url); // Check if JSON file exists
if (exists) {
const option = document.createElement('option');
option.value = url;
option.textContent = `${boardName} ${versionNumber}`;
optgroup.appendChild(option);
if (!versionMap[boardName]) {
versionMap[boardName] = [];
}
versionMap[boardName].push({ versionNumber, url }); // Add to version map
}
}
}

// Generate dropdown options after filtering and sorting
for (const [boardName, versions] of Object.entries(versionMap)) {
const optgroup = document.createElement('optgroup');
optgroup.label = boardName;

// Sort versions in descending order (newest first)
versions.sort((a, b) => compareVersions(a.versionNumber, b.versionNumber));

// Create option elements for each valid version
for (const { versionNumber, url } of versions) {
const option = document.createElement('option');
option.value = url;
option.textContent = `${boardName} ${versionNumber}`;
optgroup.appendChild(option);
}

// Add the optgroup to the select element if it contains options
if (optgroup.children.length > 0) {
selectElement.appendChild(optgroup);
}
}
}

// Initiate dropdown generation on page load
generateDropdown();
</script>

Expand Down

0 comments on commit 999a569

Please sign in to comment.