lbry.tech/app/dist/scripts/app.js

110 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-10-02 00:11:20 +02:00
/* global location, send, window */ "use strict";
2018-07-12 17:21:42 +02:00
2018-10-02 00:11:20 +02:00
document.addEventListener("DOMContentLoaded", () => {
2018-07-12 17:21:42 +02:00
scrollToElementOnLoad();
2018-10-02 00:11:20 +02:00
// Automatically open external links in new tabs
document.querySelectorAll("a[href^=http]").forEach(anchor => {
if (anchor.href.indexOf(location.hostname) === -1) {
anchor.rel = "noopener noreferrer";
anchor.target = "_blank";
2018-07-12 17:21:42 +02:00
}
});
});
2018-10-01 17:31:49 +02:00
// Browsers not Firefox do not yet support `text-orientation` and/or `writing-mode`
2018-10-03 18:53:46 +02:00
if (
!/Firefox[/\s](\d+\.\d+)/.test(navigator.userAgent) &&
document.querySelector(".component--glossary-toc-toggle")
) document.querySelector(".component--glossary-toc-toggle").classList.add("noncompliant-fix");
2018-10-01 17:31:49 +02:00
2018-07-12 17:21:42 +02:00
if ( // Toggle beta message
localStorage.getItem("hide lbry alert") &&
localStorage.getItem("hide lbry alert") === "true" // cannot set Booleans for some reason
) document.querySelector("#alert-beta").style.display = "none";
2018-10-06 22:53:01 +02:00
document.querySelector("#close-alert").onclick = function() {
2018-07-12 17:21:42 +02:00
localStorage.setItem("hide lbry alert", true);
document.querySelector("#alert-beta").style.display = "none";
};
2018-10-02 00:11:20 +02:00
// Smooth scroll
2018-07-12 17:21:42 +02:00
document.querySelectorAll("a[href^='#']").forEach(anchor => {
2018-10-02 00:11:20 +02:00
anchor.addEventListener("click", event => {
event.preventDefault();
2018-07-12 17:21:42 +02:00
2018-10-09 21:15:53 +02:00
const element = event.target.href.split("#").pop()
.toLowerCase();
2018-07-12 17:21:42 +02:00
let elementOffset;
if (document.getElementById(element)) {
elementOffset = document.getElementById(element).offsetTop - 74;
window.scroll({ top: elementOffset, behavior: "smooth" });
}
2018-10-02 00:11:20 +02:00
// Add hash to URL bar when sidebar links are clicked
2018-10-02 20:46:17 +02:00
if (
event.target.parentElement.className === "api__toc__item" ||
event.target.parentElement.parentElement.className === "component--glossary-toc"
) history.replaceState({}, "", `#${element}`);
2018-07-12 17:21:42 +02:00
});
});
2018-10-02 00:11:20 +02:00
// Newsletter
2018-10-10 21:04:16 +02:00
document.getElementById("emailAddress").addEventListener("keyup", event => {
const key = event.keyCode ? event.keyCode : event.which;
if (key === 13)
document.querySelector("[data-action='subscribe to newsletter']").click();
});
2018-10-02 00:11:20 +02:00
document.querySelector("[data-action='subscribe to newsletter']").onclick = () => {
2018-10-10 21:04:16 +02:00
const email = document.getElementById("emailAddress").value.trim();
if (!validateEmail(email)) {
document.getElementById("emailMessage").classList.add("error");
document.getElementById("emailMessage").innerHTML = "Your email address is invalid";
return;
}
2018-10-06 22:53:01 +02:00
2018-10-10 21:04:16 +02:00
document.getElementById("emailMessage").classList.remove("error");
send(JSON.stringify({
2018-10-06 22:53:01 +02:00
email: email,
message: "subscribe"
}));
2018-10-02 00:11:20 +02:00
};
// H E L P E R S
2018-07-12 17:21:42 +02:00
function scrollToElementOnLoad() {
if (window.location.href.includes("#")) {
setTimeout(() => { // give page time to breathe
2018-10-09 21:15:53 +02:00
const element = window.location.href.split("#").pop()
.toLowerCase();
2018-07-12 17:21:42 +02:00
let elementOffset;
if (document.getElementById(element)) {
elementOffset = document.getElementById(element).offsetTop - 74;
window.scroll({ top: elementOffset, behavior: "smooth" });
}
}, 150);
}
}
function validateEmail(email) {
2018-10-02 00:11:20 +02:00
const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\\.,;:\s@"]{2,})$/i;
2018-10-06 22:53:01 +02:00
2018-10-02 00:11:20 +02:00
return emailRegex.test(String(email));
}