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

91 lines
2.6 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`
if (!/Firefox[/\s](\d+\.\d+)/.test(navigator.userAgent))
document.querySelector(".component--glossary-toc-toggle").classList.add("noncompliant-fix");
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";
document.querySelector("#close-alert").onclick = function () {
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-02 00:11:20 +02:00
const element = event.target.href.split("#").pop();
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
document.querySelector("[data-action='subscribe to newsletter']").onclick = () => {
const email = document.getElementById("emailAddress").value;
if (!validateEmail(email)) return;
send(JSON.stringify({
"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
const element = window.location.href.split("#").pop();
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;
return emailRegex.test(String(email));
}