This commit is contained in:
Matteo Manzinello 2019-05-09 12:11:22 +02:00
parent 6123dabdf4
commit 7405765d9b

35
dist/mailgo.js vendored
View File

@ -8,12 +8,12 @@ mailgoInit = () => {
"https://unpkg.com/mailgo@" + version + "/dist/mailgo.min.css"; "https://unpkg.com/mailgo@" + version + "/dist/mailgo.min.css";
document.head.appendChild(styleSheet); document.head.appendChild(styleSheet);
// ottengo tutti i mailto contenuti nella pagina // all mailgos in the document
const mailgos = document.querySelectorAll( const mailgos = document.querySelectorAll(
'a[href^="mailto:"]:not(.no-mailgo), a[href="#mailgo"], a.mailgo' 'a[href^="mailto:"]:not(.no-mailgo), a[href="#mailgo"], a.mailgo'
); );
// attivo mailgo su tutti gli elementi // mailgo on every element
mailgos.forEach((mailgo, index) => { mailgos.forEach((mailgo, index) => {
let mail = "", let mail = "",
mailtoHref = "", mailtoHref = "",
@ -22,7 +22,7 @@ mailgoInit = () => {
subject = "", subject = "",
bodyMail = ""; bodyMail = "";
// caso in cui applico mailgo ad un <a> con mailto: // mailgo all the element with href=^"mailto:"
if (mailgo.href && mailgo.href.includes("mailto:")) { if (mailgo.href && mailgo.href.includes("mailto:")) {
mail = mailgo.href mail = mailgo.href
.split("?")[0] .split("?")[0]
@ -33,14 +33,14 @@ mailgoInit = () => {
url = new URL(mailtoHref); url = new URL(mailtoHref);
let urlParams = new URLSearchParams(url.search); let urlParams = new URLSearchParams(url.search);
// ottengo i parametri aggiuntivi // optional parameters for the email
cc = urlParams.get("cc"); cc = urlParams.get("cc");
bcc = urlParams.get("bcc"); bcc = urlParams.get("bcc");
subject = urlParams.get("subject"); subject = urlParams.get("subject");
bodyMail = urlParams.get("body"); bodyMail = urlParams.get("body");
} else { } else {
// caso in cui applico mailgo ad <a> con class="mailgo" o href=#mailgo // mailgo all the element with href="#mailgo" or class="mailgo"
// compongo l'email con data-address e data-domain passati nell'elemento // email = data-address + @ + data-domain
mail = mail =
mailgo.getAttribute("data-address") + mailgo.getAttribute("data-address") +
"@" + "@" +
@ -49,9 +49,10 @@ mailgoInit = () => {
url = new URL(mailtoHref); url = new URL(mailtoHref);
} }
// valido l'email, se non valida non abilito mailgo per il singolo elemento // validate the email address
if (!validateEmail(mail)) return; if (!validateEmail(mail)) return;
// modal
let modal = document.createElement("div"); let modal = document.createElement("div");
modal.classList.add("mailgo-modal"); modal.classList.add("mailgo-modal");
modal.setAttribute("data-index", index); modal.setAttribute("data-index", index);
@ -66,7 +67,7 @@ mailgoInit = () => {
modalContent.className = "mailgo-modal-content"; modalContent.className = "mailgo-modal-content";
modal.appendChild(modalContent); modal.appendChild(modalContent);
// titolo (l'email) // title (email address)
let strong = document.createElement("strong"); let strong = document.createElement("strong");
strong.className = "mailgo-title"; strong.className = "mailgo-title";
let strongContent = document.createTextNode(mail); let strongContent = document.createTextNode(mail);
@ -177,7 +178,7 @@ mailgoInit = () => {
open.appendChild(openContent); open.appendChild(openContent);
modalContent.appendChild(open); modalContent.appendChild(open);
// copia l'email // copy
let copy = document.createElement("a"); let copy = document.createElement("a");
copy.href = "#mailgo-copy"; copy.href = "#mailgo-copy";
copy.classList.add("mailgo-copy"); copy.classList.add("mailgo-copy");
@ -206,23 +207,23 @@ mailgoInit = () => {
by.appendChild(textBy); by.appendChild(textBy);
modalContent.appendChild(by); modalContent.appendChild(by);
// aggiungo il modal dopo l'elemento // add the modal after the element
mailgo.parentNode.insertBefore(modal, mailgo.nextSibling); mailgo.parentNode.insertBefore(modal, mailgo.nextSibling);
// se clicco sull'elemento appare il modal // show the modal on every element click
mailgo.addEventListener( mailgo.addEventListener(
"click", "click",
event => { event => {
// blocco l'esecuzione normale del mailto: // clock the mailto: classic behaviour
event.preventDefault(); event.preventDefault();
// setto il modal come attivo // modal is now active (showing)
mailgo.nextElementSibling.classList.add("is-active"); mailgo.nextElementSibling.classList.add("is-active");
}, },
false false
); );
// se clicco fuori scompare // every click outside the modal will hide the modal
modalBackground.addEventListener( modalBackground.addEventListener(
"click", "click",
event => { event => {
@ -233,16 +234,16 @@ mailgoInit = () => {
}); });
}; };
// aggiungo l'init di mailgo al DOMContentLoaded // DOMContentLoaded -> mailgoInit
document.addEventListener("DOMContentLoaded", mailgoInit, false); document.addEventListener("DOMContentLoaded", mailgoInit, false);
// funzionalità di validazione dell'email con regex // validate the email with refgex
validateEmail = email => { validateEmail = email => {
let re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; let re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email); return re.test(email);
}; };
// funzionalità di copia di una stringa // copy of a string
copyToClipboard = str => { copyToClipboard = str => {
const el = document.createElement("textarea"); const el = document.createElement("textarea");
el.value = str; el.value = str;