lbry.tech/public/scripts/app.js

79 lines
2 KiB
JavaScript
Raw Normal View History

/* global $, location, send, window */ "use strict";
2018-07-12 17:21:42 +02:00
$(function () {
scrollToElementOnLoad();
$("a[href^=http]").each(function () { // Automatically open external links in new tabs
if (this.href.indexOf(location.hostname) === -1) {
$(this).attr("target", "_blank");
}
});
});
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";
};
// Smooth scroll
2018-07-12 17:21:42 +02:00
document.querySelectorAll("a[href^='#']").forEach(anchor => {
anchor.addEventListener("click", function (e) {
e.preventDefault();
const element = this.href.split("#").pop();
let elementOffset;
if (document.getElementById(element)) {
elementOffset = document.getElementById(element).offsetTop - 74;
window.scroll({ top: elementOffset, behavior: "smooth" });
}
});
});
// Newsletter
$("[data-action='subscribe to newsletter']").on("click", () => {
const email = $("#emailAddress").val();
if (!validateEmail(email)) return;
send(JSON.stringify({
"email": email,
"message": "subscribe"
}));
});
// 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) {
const re = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\\.,;:\s@"]{2,})$/i;
return re.test(String(email));
}