fix a lot of bugs

This commit is contained in:
Matteo Manzinello 2019-09-17 00:21:09 +02:00
parent acc4d1d960
commit 12db39dad0
3 changed files with 79 additions and 59 deletions

2
dist/mailgo.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,24 +1,6 @@
// @flow // @flow
const V = "MAILGO_VERSION"; const V = "MAILGO_VERSION";
const MAILTO = "mailto:";
const TEL = "tel:";
const CALLTO = "callto:";
const MAIL_TYPE = "mail";
const TEL_TYPE = "tel";
let url = "",
mail = "",
encEmail = "",
cc = "",
bcc = "",
subject = "",
bodyMail = "";
let tel = "",
msg = "";
// mailgo style (gulp) // mailgo style (gulp)
const mailgoCSS = document.createElement("style"); const mailgoCSS = document.createElement("style");
mailgoCSS.id = "mailgo-style"; mailgoCSS.id = "mailgo-style";
@ -27,6 +9,37 @@ const mailgoCSSContent = document.createTextNode(`MAILGO_STYLE`);
mailgoCSS.appendChild(mailgoCSSContent); mailgoCSS.appendChild(mailgoCSSContent);
document.head.appendChild(mailgoCSS); document.head.appendChild(mailgoCSS);
// links
const MAILTO = "mailto:";
const TEL = "tel:";
const CALLTO = "callto:";
// mailgo types
const MAIL_TYPE = "mail";
const TEL_TYPE = "tel";
// mailgo variables
let url = "",
mail = "",
encEmail = "",
cc = "",
bcc = "",
subject = "",
bodyMail = "";
// mailgo tel variables
let tel = "",
msg = "";
// mailgo buttons
let gmailButton,
outlookButton,
openButton,
waButton,
telegramButton,
callButton,
copyButton;
/** /**
* mailgoInit * mailgoInit
* the function that creates the mailgo elements in DOM * the function that creates the mailgo elements in DOM
@ -362,10 +375,10 @@ const mailgoRender = (type = MAIL_TYPE, mailgo) => {
let bodyValueEl = getE("mailgo-body-value"); let bodyValueEl = getE("mailgo-body-value");
// actions // actions
let gmailButton = getE("mailgo-gmail"); gmailButton = getE("mailgo-gmail");
let outlookButton = getE("mailgo-outlook"); outlookButton = getE("mailgo-outlook");
let openButton = getE("mailgo-open"); openButton = getE("mailgo-open");
let copyButton = getE("mailgo-copy"); copyButton = getE("mailgo-copy");
// the title of the modal (email address) // the title of the modal (email address)
titleEl.innerHTML = mail.split(",").join("<br/>"); titleEl.innerHTML = mail.split(",").join("<br/>");
@ -391,18 +404,14 @@ const mailgoRender = (type = MAIL_TYPE, mailgo) => {
: (bodyEl.style.display = "none"); : (bodyEl.style.display = "none");
// add the actions // add the actions
gmailButton.addEventListener("click", () => gmailButton.addEventListener("click", () => actions.openGmail());
actions.openGmail(mail, cc, bcc, subject, bodyMail)
);
outlookButton.addEventListener("click", () => outlookButton.addEventListener("click", () => actions.openOutlook());
actions.openOutlook(mail, subject, bodyMail)
);
encEmail = encodeEmail(mail); encEmail = encodeEmail(mail);
openButton.addEventListener("click", () => actions.openDefault(encEmail)); openButton.addEventListener("click", () => actions.openDefault());
copyButton.addEventListener("click", () => actions.copy(mail, copyButton)); copyButton.addEventListener("click", () => actions.copy());
} }
// mailgo tel // mailgo tel
if (type === TEL_TYPE) { if (type === TEL_TYPE) {
@ -429,22 +438,22 @@ const mailgoRender = (type = MAIL_TYPE, mailgo) => {
let titleEl = getE("mailgo-tel-title"); let titleEl = getE("mailgo-tel-title");
// actions // actions
let waButton = getE("mailgo-wa"); waButton = getE("mailgo-wa");
let telegramButton = getE("mailgo-telegram"); telegramButton = getE("mailgo-telegram");
let callButton = getE("mailgo-call"); callButton = getE("mailgo-call");
let copyButton = getE("mailgo-tel-copy"); copyButton = getE("mailgo-tel-copy");
// the title of the modal (tel) // the title of the modal (tel)
titleEl.innerHTML = tel; titleEl.innerHTML = tel;
// add the actions // add the actions
waButton.addEventListener("click", () => actions.openWhatsApp(tel)); waButton.addEventListener("click", () => actions.openWhatsApp());
telegramButton.addEventListener("click", () => actions.openTelegram(tel)); telegramButton.addEventListener("click", () => actions.openTelegram());
callButton.addEventListener("click", () => actions.callDefault(tel)); callButton.addEventListener("click", () => actions.callDefault());
copyButton.addEventListener("click", () => actions.copy(tel, copyButton)); copyButton.addEventListener("click", () => actions.copy(tel));
} }
// show the mailgo // show the mailgo
@ -456,7 +465,7 @@ const mailgoRender = (type = MAIL_TYPE, mailgo) => {
// actions // actions
const actions = { const actions = {
openGmail: (mail, cc, bcc, subject, bodyMail) => { openGmail: () => {
let gmailUrl = let gmailUrl =
"https://mail.google.com/mail/u/0/?view=cm&source=mailto&to=" + "https://mail.google.com/mail/u/0/?view=cm&source=mailto&to=" +
encodeURIComponent(mail); encodeURIComponent(mail);
@ -469,7 +478,7 @@ const actions = {
window.open(gmailUrl, "_blank"); window.open(gmailUrl, "_blank");
}, },
openOutlook: (mail, subject, bodyMail) => { openOutlook: () => {
let outlookUrl = let outlookUrl =
"https://outlook.live.com/owa/?path=/mail/action/compose&to=" + "https://outlook.live.com/owa/?path=/mail/action/compose&to=" +
encodeURIComponent(mail); encodeURIComponent(mail);
@ -479,27 +488,30 @@ const actions = {
window.open(outlookUrl, "_blank"); window.open(outlookUrl, "_blank");
}, },
openDefault: encEmail => { openDefault: () => {
mailToEncoded(encEmail); mailToEncoded(encEmail);
}, },
openTelegram: (tel, msg = "") => { openTelegram: () => {
let tgUrl = "tg://msg?text=" + msg + "&to=" + tel; let tgUrl = "tg://msg?text=" + msg + "&to=" + tel;
window.open(tgUrl, "_blank"); window.open(tgUrl, "_blank");
}, },
openWhatsApp: (tel, msg = "") => { openWhatsApp: () => {
let waUrl = "https://wa.me/" + tel; let waUrl = "https://wa.me/" + tel;
window.open(waUrl, "_blank"); window.open(waUrl, "_blank");
}, },
callDefault: tel => { callDefault: () => {
let callUrl = "tel:" + tel; let callUrl = "tel:" + tel;
window.open(callUrl); window.open(callUrl);
}, },
copy: (content, copyButton) => { copy: content => {
copyToClipboard(content); copyToClipboard(content);
mailgoIsShowing(MAIL_TYPE)
? (copyButton = getE("mailgo-copy"))
: (copyButton = getE("mailgo-tel-copy"));
copyButton.textContent = "copied"; copyButton.textContent = "copied";
setTimeout(() => (copyButton.textContent = "copy"), 999); setTimeout(() => (copyButton.textContent = "copy"), 999);
} }
@ -558,6 +570,9 @@ const mailgoCheckRender = event => {
if (path) { if (path) {
path.forEach(element => { path.forEach(element => {
console.log(element);
if (element instanceof HTMLDocument || element instanceof Window) return;
// go in the event.path to find if the user has clicked on a mailgo element // go in the event.path to find if the user has clicked on a mailgo element
if (isMailgo(element)) { if (isMailgo(element)) {
// stop the normal execution of the element click // stop the normal execution of the element click
@ -587,9 +602,11 @@ const mailgoCheckRender = event => {
* mailgoKeydown * mailgoKeydown
* function to manage the keydown event when the modal is showing * function to manage the keydown event when the modal is showing
*/ */
const mailgoKeydown = () => { const mailgoKeydown = event => {
console.log(event);
// if mailgo is showing // if mailgo is showing
if (mailgoIsShowing(MAIL_TYPE)) { if (mailgoIsShowing(MAIL_TYPE)) {
console.log("son qui");
switch (event.keyCode) { switch (event.keyCode) {
case 27: case 27:
// Escape // Escape
@ -597,20 +614,20 @@ const mailgoKeydown = () => {
break; break;
case 71: case 71:
// g -> open GMail // g -> open GMail
actions.openGmail(mail, cc, bcc, subject, bodyMail); actions.openGmail();
break; break;
case 79: case 79:
// o -> open Outlook // o -> open Outlook
actions.openOutlook(mail, subject, bodyMail); actions.openOutlook();
break; break;
case 32: case 32:
case 13: case 13:
// spacebar or enter -> open default // spacebar or enter -> open default
actions.openDefault(encEmail); actions.openDefault();
break; break;
case 67: case 67:
// c -> copy // c -> copy
actions.copy(mail, copyButton); actions.copy(mail);
break; break;
default: default:
return; return;
@ -623,20 +640,20 @@ const mailgoKeydown = () => {
break; break;
case 71: case 71:
// g -> open GMail // g -> open GMail
actions.openGmail(mail, cc, bcc, subject, bodyMail); actions.openGmail();
break; break;
case 79: case 79:
// o -> open Outlook // o -> open Outlook
actions.openOutlook(mail, subject, bodyMail); actions.openOutlook();
break; break;
case 32: case 32:
case 13: case 13:
// spacebar or enter -> open default // spacebar or enter -> open default
actions.openDefault(encEmail); actions.openDefault();
break; break;
case 67: case 67:
// c -> copy // c -> copy
actions.copy(tel, copyButton); actions.copy(tel);
break; break;
default: default:
return; return;
@ -669,9 +686,12 @@ const hideMailgo = () => {
// is the mailgo modal hidden? // is the mailgo modal hidden?
const mailgoIsShowing = (type = MAIL_TYPE) => { const mailgoIsShowing = (type = MAIL_TYPE) => {
type === TEL_TYPE if (type === MAIL_TYPE) {
? getDisplay("mailgo-tel") === "flex" return getDisplay("mailgo") === "flex";
: getDisplay("mailgo") === "flex"; } else if (type === TEL_TYPE) {
return getDisplay("mailgo-tel") === "flex";
}
return false;
}; };
// decrypt email // decrypt email