Commit 3f5296ec authored by sajolida's avatar sajolida
Browse files

Reorganize code

parent 430122b3
document.addEventListener("DOMContentLoaded", function() {
// Display floating-toggleable-links to prevent people without JS to
// either always see the toggles or have broken toggle links.
function showFloatingToggleableLinks() {
var links = document.getElementsByClassName("floating-toggleable-link");
for (let i = 0; i < links.length; i++) {
show(links[i]);
}
}
showFloatingToggleableLinks();
/* Generic functions */
function hide(elm) {
elm.style.display = "none";
......@@ -99,13 +91,6 @@ document.addEventListener("DOMContentLoaded", function() {
}
}
function toggleContinueLink(state) {
hide(document.getElementById("skip-download"));
hide(document.getElementById("skip-verification"));
hide(document.getElementById("next"));
show(document.getElementById(state));
}
function hitCounter(status) {
try {
var counter_url, url, scenario, version, cachebust;
......@@ -122,21 +107,13 @@ document.addEventListener("DOMContentLoaded", function() {
} catch (e) { } // Ignore if we fail to hit the download counter
}
/* Display logic functions */
function showAnotherMirror() {
hide(document.getElementById("bittorrent"));
show(document.getElementById("try-another-mirror"));
}
function resetVerificationResult(result) {
hide(document.getElementById("verifying-download"));
hide(document.getElementById("verification-successful"));
hide(document.getElementById("verification-failed"));
hide(document.getElementById("verification-failed-again"));
hide(document.getElementById("verification-error"));
show(document.getElementById("verification"));
toggleContinueLink("skip-verification");
}
function showVerifyButton() {
hide(document.getElementById("verifying-download"));
show(document.getElementById("verify-button"));
......@@ -184,15 +161,26 @@ document.addEventListener("DOMContentLoaded", function() {
}
}
// Reset the page to its initial state:
// - Detect the browser version and display the relevant variant
detectBrowser();
// - Show "Verify" button
showVerifyButton();
// - Display 'Skip download' as continue link
toggleContinueLink("skip-download");
function resetVerificationResult(result) {
hide(document.getElementById("verifying-download"));
hide(document.getElementById("verification-successful"));
hide(document.getElementById("verification-failed"));
hide(document.getElementById("verification-failed-again"));
hide(document.getElementById("verification-error"));
show(document.getElementById("verification"));
toggleContinueLink("skip-verification");
}
function toggleContinueLink(state) {
hide(document.getElementById("skip-download"));
hide(document.getElementById("skip-verification"));
hide(document.getElementById("next"));
show(document.getElementById(state));
}
/* Initialize event handlers */
// Display "Verify with your browser" when image is clicked
// Direct download
document.getElementById("download-img").onclick = function(e) { download(e, this); }
document.getElementById("download-iso").onclick = function(e) { download(e, this); }
......@@ -209,23 +197,17 @@ document.addEventListener("DOMContentLoaded", function() {
}
}
// Display BitTorrent verification tip when BitTorrent download is clicked
document.getElementById("bittorrent-download").onclick = function(e) {
hide(document.getElementById("javascript-verification-tip"));
show(document.getElementById("bittorrent-verification-tip"));
toggleContinueLink("next");
}
// Reset verification when downloading again after failure
document.getElementById("download-img-again").onclick = function(e) { downloadAgain(e, this); }
document.getElementById("download-iso-again").onclick = function(e) { downloadAgain(e, this); }
// BitTorrent download
document.getElementById("download-img-torrent").onclick = function(e) { downloadTorrent(e, this); }
document.getElementById("download-iso-torrent").onclick = function(e) { downloadTorrent(e, this); }
function downloadAgain(e, elm) {
function downloadTorrent(e, elm) {
try {
e.preventDefault();
hitCounter("download-image-again");
resetVerificationResult();
showVerifyButton();
hitCounter("download-torrent");
hide(document.getElementById("javascript-verification-tip"));
show(document.getElementById("bittorrent-verification-tip"));
toggleContinueLink("next");
} finally {
// Setting window.location.href will abort AJAX requests resulting
// in a NetworkError depending on the timing and browser.
......@@ -233,14 +215,16 @@ document.addEventListener("DOMContentLoaded", function() {
}
}
// Display "Verify with BitTorrent" when Torrent file is clicked
document.getElementById("download-img-torrent").onclick = function(e) { downloadTorrent(e, this); }
document.getElementById("download-iso-torrent").onclick = function(e) { downloadTorrent(e, this); }
// Download again after failure
document.getElementById("download-img-again").onclick = function(e) { downloadAgain(e, this); }
document.getElementById("download-iso-again").onclick = function(e) { downloadAgain(e, this); }
function downloadTorrent(e, elm) {
function downloadAgain(e, elm) {
try {
e.preventDefault();
hitCounter("download-torrent");
hitCounter("download-image-again");
resetVerificationResult();
showVerifyButton();
} finally {
// Setting window.location.href will abort AJAX requests resulting
// in a NetworkError depending on the timing and browser.
......@@ -261,6 +245,19 @@ document.addEventListener("DOMContentLoaded", function() {
setTimeout(function(){ showVerificationResult("successful"); }, 10000);
}
/* Final initialization */
// Display floating-toggleable-links to prevent people without JS to
// either always see the toggles or have broken toggle links.
var links = document.getElementsByClassName("floating-toggleable-link");
for (let i = 0; i < links.length; i++) {
show(links[i]);
}
detectBrowser();
showVerifyButton();
toggleContinueLink("skip-download");
// To debug the display of the different states:
// showVerifyingDownload('test.img');
// showVerificationProgress('50');
......
......@@ -28,10 +28,8 @@ were solved, and known issues that have already been identified.</p>
<div id="download-minor" class="indent">
<p id="bittorrent">
or download using
<span id="bittorrent-download">
<a href="[[!inline pages="inc/stable_amd64_img_torrent_url" raw="yes" sort="age"]]" id="download-img-torrent" class="usb upgrade download-only-img">BitTorrent</a>
<a href="[[!inline pages="inc/stable_amd64_iso_torrent_url" raw="yes" sort="age"]]" id="download-iso-torrent" class="dvd vm download-only-iso">BitTorrent</a>
</span>
<a href="[[!inline pages="inc/stable_amd64_img_torrent_url" raw="yes" sort="age"]]" id="download-img-torrent" class="usb upgrade download-only-img">BitTorrent</a>
<a href="[[!inline pages="inc/stable_amd64_iso_torrent_url" raw="yes" sort="age"]]" id="download-iso-torrent" class="dvd vm download-only-iso">BitTorrent</a>
</p>
<p id="try-another-mirror">If the download fails, try to
<a href="[[!inline pages="inc/stable_amd64_img_url" raw="yes" sort="age"]]" class="usb upgrade download-only-img">download from another mirror.</a>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment