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

95 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-01-17 21:41:26 +01:00
"use strict"; /* global document, history, location, navigator, send, window */
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-10-02 00:11:20 +02:00
// Smooth scroll
2018-07-12 17:21:42 +02:00
document.querySelectorAll("a[href^='#']").forEach(anchor => {
2019-02-05 00:42:52 +01:00
if (anchor.classList.contains("no-smooth")) // Ignore smooth scroll functionality
return;
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" });
history.pushState({}, "", `#${element}`); // Add hash to URL bar
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");
2019-02-08 00:06:03 +01:00
send({
2018-10-06 22:53:01 +02:00
email: email,
message: "subscribe"
2019-02-08 00:06:03 +01:00
});
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;
2019-02-12 00:47:01 +01:00
return emailRegex.test(String(email)); // eslint-disable-line padding-line-between-statements
}