mailymaily/src/mailgo.ts

936 lines
26 KiB
TypeScript
Raw Normal View History

2020-07-03 13:28:01 +00:00
import {
MailgoConfig,
MailgoTranslations,
MailgoTranslation,
MailgoI18n,
} from "mailgo";
2020-04-25 15:24:28 +00:00
2020-06-10 17:19:38 +00:00
// i18n for mailgo
2020-07-03 17:06:16 +00:00
import * as i18n from "../i18n/i18n.json";
2020-06-10 17:19:38 +00:00
2020-07-01 20:53:13 +00:00
// mailgo scss
2020-07-03 13:28:01 +00:00
const mailgoCSS: string = require("./mailgo.scss").toString();
2020-06-11 07:27:38 +00:00
2020-06-10 17:19:38 +00:00
// default lang
2020-07-03 13:28:01 +00:00
const DEFAULT_LANG: string = "en";
2020-06-10 17:19:38 +00:00
2020-04-24 19:51:34 +00:00
// links
const MAILTO: string = "mailto:";
const TEL: string = "tel:";
const CALLTO: string = "callto:";
// mailgo types
const MAIL_TYPE: string = "mail";
const TEL_TYPE: string = "tel";
// default href for links
const DEFAULT_BTN_HREF: string = "javascript:void(0);";
2020-07-07 12:48:18 +00:00
// useful html tags
const spanHTMLTag: string = "span";
const aHTMLTag: string = "a";
const pHTMLTag: string = "p";
// default language
let lang: string = DEFAULT_LANG;
2020-04-24 19:51:34 +00:00
// mailgo variables
let url: URL,
mail: string = "",
encEmail: string = "",
cc: string = "",
bcc: string = "",
subject: string = "",
bodyMail: string = "";
// mailgo tel variables
let tel: string = "",
msg: string = "",
telegramUsername: string = "",
skypeUsername: string = "";
// the DOM elements
let title: HTMLElement,
titleTel: HTMLElement,
detailCc: HTMLElement,
detailBcc: HTMLElement,
detailSubject: HTMLElement,
detailBody: HTMLElement,
ccValue: HTMLElement,
bccValue: HTMLElement,
subjectValue: HTMLElement,
bodyValue: HTMLElement;
// mailgo buttons (actions)
let gmail: HTMLLinkElement,
outlook: HTMLLinkElement,
open: HTMLLinkElement,
telegram: HTMLLinkElement,
wa: HTMLLinkElement,
skype: HTMLLinkElement,
call: HTMLLinkElement,
copyMail: HTMLLinkElement,
copyTel: HTMLLinkElement;
/**
* mailgoInit
* the function that creates the mailgo elements in DOM
*/
2020-06-10 16:55:12 +00:00
const mailgoInit = (mailgoConfig?: MailgoConfig): void => {
2020-06-10 17:19:38 +00:00
// translations
2020-07-07 12:48:18 +00:00
let {
translations,
}: { translations: MailgoTranslations } = i18n as MailgoI18n;
2020-06-10 17:19:38 +00:00
// if a default language is defined use it
2020-07-01 20:52:23 +00:00
if (mailgoConfig?.lang && i18n.languages.includes(mailgoConfig.lang)) {
lang = mailgoConfig.lang;
2020-06-10 17:19:38 +00:00
}
2020-06-10 17:40:55 +00:00
// if is defined <html lang=""> use it!
2020-07-01 20:52:23 +00:00
if (!mailgoConfig?.forceLang) {
// keep the lang from html
2020-07-03 13:28:01 +00:00
let htmlLang: string = document.documentElement.lang;
2020-07-01 20:52:23 +00:00
// if there are translations...
if (i18n.languages.includes(htmlLang)) {
lang = document.documentElement.lang;
}
2020-06-10 17:40:55 +00:00
}
2020-06-10 17:19:38 +00:00
// strings
2020-07-03 13:28:01 +00:00
let defaultStrings: MailgoTranslation = translations[DEFAULT_LANG];
let strings: MailgoTranslation = translations[lang];
2020-06-10 17:19:38 +00:00
2020-04-24 19:51:34 +00:00
// mailgo mail
{
// modal
2020-07-03 21:34:26 +00:00
let modal: HTMLElement = createElement();
2020-04-24 19:51:34 +00:00
modal.style.display = "none";
modal.id = "mailgo";
modal.classList.add("m-modal");
2020-07-02 09:49:40 +00:00
// if dark is in config
if (mailgoConfig?.dark) {
modal.classList.add("m-dark");
}
2020-04-24 19:51:34 +00:00
// background
2020-07-03 21:34:26 +00:00
let modalBackground: HTMLElement = createElement();
2020-04-24 19:51:34 +00:00
modalBackground.className = "m-modal-back";
modal.appendChild(modalBackground);
// modal content
2020-07-03 21:34:26 +00:00
let modalContent: HTMLElement = createElement();
2020-04-24 19:51:34 +00:00
modalContent.className = "m-modal-content";
modal.appendChild(modalContent);
// title (email address)
title = createElement("strong");
title.id = "m-title";
title.className = "m-title";
modalContent.appendChild(title);
// details
2020-07-03 21:34:26 +00:00
let details: HTMLElement = createElement();
2020-04-24 19:51:34 +00:00
details.id = "m-details";
details.className = "m-details";
2020-07-07 12:48:18 +00:00
detailCc = createElement(pHTMLTag);
2020-04-24 19:51:34 +00:00
detailCc.id = "m-cc";
2020-07-07 12:48:18 +00:00
let ccSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
ccSpan.className = "w-500";
ccSpan.appendChild(createTextNode(strings.cc_ || defaultStrings.cc_));
2020-07-07 12:48:18 +00:00
ccValue = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
ccValue.id = "m-cc-value";
detailCc.appendChild(ccSpan);
detailCc.appendChild(ccValue);
details.appendChild(detailCc);
2020-07-07 12:48:18 +00:00
detailBcc = createElement(pHTMLTag);
2020-04-24 19:51:34 +00:00
detailBcc.id = "m-bcc";
2020-07-07 12:48:18 +00:00
let bccSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
bccSpan.className = "w-500";
bccSpan.appendChild(createTextNode(strings.bcc_ || defaultStrings.bcc_));
2020-07-07 12:48:18 +00:00
bccValue = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
bccValue.id = "m-bcc-value";
detailBcc.appendChild(bccSpan);
detailBcc.appendChild(bccValue);
details.appendChild(detailBcc);
2020-07-07 12:48:18 +00:00
detailSubject = createElement(pHTMLTag);
2020-04-24 19:51:34 +00:00
detailSubject.id = "m-subject";
2020-07-07 12:48:18 +00:00
let subjectSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
subjectSpan.className = "w-500";
2020-06-18 07:08:14 +00:00
subjectSpan.appendChild(
createTextNode(strings.subject_ || defaultStrings.subject_)
2020-06-18 07:08:14 +00:00
);
2020-07-07 12:48:18 +00:00
subjectValue = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
subjectValue.id = "m-subject-value";
detailSubject.appendChild(subjectSpan);
detailSubject.appendChild(subjectValue);
details.appendChild(detailSubject);
2020-07-07 12:48:18 +00:00
detailBody = createElement(pHTMLTag);
2020-04-24 19:51:34 +00:00
detailBody.id = "m-body";
2020-07-07 12:48:18 +00:00
let bodySpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
bodySpan.className = "w-500";
bodySpan.appendChild(createTextNode(strings.body_ || defaultStrings.body_));
2020-07-07 12:48:18 +00:00
bodyValue = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
bodyValue.id = "m-body-value";
detailBody.appendChild(bodySpan);
detailBody.appendChild(bodyValue);
details.appendChild(detailBody);
modalContent.appendChild(details);
// Gmail
2020-07-07 12:48:18 +00:00
gmail = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
gmail.id = "m-gmail";
gmail.href = DEFAULT_BTN_HREF;
gmail.classList.add("m-open");
gmail.classList.add("m-gmail");
2020-06-18 07:08:14 +00:00
gmail.appendChild(
createTextNode(strings.open_in || defaultStrings.open_in)
);
2020-07-07 12:48:18 +00:00
let gmailSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
gmailSpan.className = "w-500";
2020-06-30 09:26:07 +00:00
gmailSpan.appendChild(
createTextNode(strings.gmail || defaultStrings.gmail)
);
2020-04-24 19:51:34 +00:00
gmail.appendChild(gmailSpan);
modalContent.appendChild(gmail);
// Outlook
2020-07-07 12:48:18 +00:00
outlook = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
outlook.id = "m-outlook";
outlook.href = DEFAULT_BTN_HREF;
outlook.classList.add("m-open");
outlook.classList.add("m-outlook");
2020-06-18 07:08:14 +00:00
outlook.appendChild(
createTextNode(strings.open_in || defaultStrings.open_in)
);
2020-07-07 12:48:18 +00:00
let outlookSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
outlookSpan.className = "w-500";
2020-06-30 09:26:07 +00:00
outlookSpan.appendChild(
createTextNode(strings.outlook || defaultStrings.outlook)
);
2020-04-24 19:51:34 +00:00
outlook.appendChild(outlookSpan);
modalContent.appendChild(outlook);
// open default
2020-07-07 12:48:18 +00:00
open = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
open.id = "m-open";
open.href = DEFAULT_BTN_HREF;
open.classList.add("m-open");
open.classList.add("m-default");
2020-07-07 12:48:18 +00:00
let openSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
openSpan.className = "w-500";
2020-06-30 09:26:07 +00:00
openSpan.appendChild(createTextNode(strings.open || defaultStrings.open));
2020-04-24 19:51:34 +00:00
open.appendChild(openSpan);
2020-06-30 09:26:07 +00:00
open.appendChild(
createTextNode(strings._default || defaultStrings._default)
);
2020-04-24 19:51:34 +00:00
modalContent.appendChild(open);
// copy
2020-07-07 12:48:18 +00:00
copyMail = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
copyMail.id = "m-copy";
copyMail.href = DEFAULT_BTN_HREF;
copyMail.classList.add("m-copy");
copyMail.classList.add("w-500");
2020-06-30 09:26:07 +00:00
copyMail.appendChild(createTextNode(strings.copy || defaultStrings.copy));
2020-04-24 19:51:34 +00:00
modalContent.appendChild(copyMail);
modalContent.appendChild(byElement());
// add the modal at the end of the body
document.body.appendChild(modal);
// every click outside the modal will hide the modal
modalBackground.addEventListener("click", hideMailgo);
}
// mailgo tel
{
// modal
2020-07-03 21:34:26 +00:00
let modal: HTMLElement = createElement();
2020-04-24 19:51:34 +00:00
modal.style.display = "none";
modal.id = "mailgo-tel";
modal.classList.add("m-modal");
// if dark is in config
if (mailgoConfig?.dark) {
modal.classList.add("m-dark");
}
2020-04-24 19:51:34 +00:00
// background
2020-07-03 21:34:26 +00:00
let modalBackground: HTMLElement = createElement();
2020-04-24 19:51:34 +00:00
modalBackground.className = "m-modal-back";
modal.appendChild(modalBackground);
// modal content
2020-07-03 21:34:26 +00:00
let modalContent: HTMLElement = createElement();
2020-04-24 19:51:34 +00:00
modalContent.className = "m-modal-content";
modal.appendChild(modalContent);
// title (telephone number)
titleTel = createElement("strong");
titleTel.id = "m-tel-title";
titleTel.className = "m-title";
modalContent.appendChild(titleTel);
// Telegram
2020-07-07 12:48:18 +00:00
telegram = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
telegram.id = "m-tg";
telegram.href = DEFAULT_BTN_HREF;
telegram.classList.add("m-open");
telegram.classList.add("m-tg");
// by default not display
telegram.style.display = "none";
2020-06-18 07:08:14 +00:00
telegram.appendChild(
createTextNode(strings.open_in || defaultStrings.open_in)
);
2020-07-07 12:48:18 +00:00
let telegramSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
telegramSpan.className = "w-500";
2020-06-30 09:26:07 +00:00
telegramSpan.appendChild(
createTextNode(strings.telegram || defaultStrings.telegram)
);
2020-04-24 19:51:34 +00:00
telegram.appendChild(telegramSpan);
modalContent.appendChild(telegram);
// WhatsApp
2020-07-07 12:48:18 +00:00
wa = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
wa.id = "m-wa";
wa.href = DEFAULT_BTN_HREF;
wa.classList.add("m-open");
wa.classList.add("m-wa");
2020-06-18 07:08:14 +00:00
wa.appendChild(createTextNode(strings.open_in || defaultStrings.open_in));
2020-07-07 12:48:18 +00:00
let waSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
waSpan.className = "w-500";
2020-06-30 09:26:07 +00:00
waSpan.appendChild(
createTextNode(strings.whatsapp || defaultStrings.whatsapp)
);
2020-04-24 19:51:34 +00:00
wa.appendChild(waSpan);
modalContent.appendChild(wa);
// Skype
2020-07-07 12:48:18 +00:00
skype = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
skype.id = "m-skype";
skype.href = DEFAULT_BTN_HREF;
skype.classList.add("m-open");
skype.classList.add("m-skype");
2020-06-18 07:08:14 +00:00
skype.appendChild(
createTextNode(strings.open_in || defaultStrings.open_in)
);
2020-07-07 12:48:18 +00:00
let skypeSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
skypeSpan.className = "w-500";
2020-06-30 09:26:07 +00:00
skypeSpan.appendChild(
createTextNode(strings.skype || defaultStrings.skype)
);
2020-04-24 19:51:34 +00:00
skype.appendChild(skypeSpan);
modalContent.appendChild(skype);
// call default
2020-07-07 12:48:18 +00:00
call = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
call.id = "m-call";
call.href = DEFAULT_BTN_HREF;
call.classList.add("m-open");
call.classList.add("m-default");
2020-07-07 12:48:18 +00:00
let callSpan: HTMLElement = createElement(spanHTMLTag);
2020-04-24 19:51:34 +00:00
callSpan.className = "w-500";
2020-06-30 09:26:07 +00:00
callSpan.appendChild(createTextNode(strings.call || defaultStrings.call));
2020-04-24 19:51:34 +00:00
call.appendChild(callSpan);
2020-06-30 09:26:07 +00:00
call.appendChild(
createTextNode(strings._as_default || defaultStrings._as_default)
);
2020-04-24 19:51:34 +00:00
modalContent.appendChild(call);
// copy
2020-07-07 12:48:18 +00:00
copyTel = createElement(aHTMLTag) as HTMLLinkElement;
2020-04-24 19:51:34 +00:00
copyTel.id = "m-tel-copy";
copyTel.href = DEFAULT_BTN_HREF;
copyTel.classList.add("m-copy");
copyTel.classList.add("w-500");
2020-06-30 09:26:07 +00:00
copyTel.appendChild(createTextNode(strings.copy || defaultStrings.copy));
2020-04-24 19:51:34 +00:00
modalContent.appendChild(copyTel);
modalContent.appendChild(byElement());
// add the modal at the end of the body
document.body.appendChild(modal);
// every click outside the modal will hide the modal
modalBackground.addEventListener("click", hideMailgo);
}
2020-06-10 16:55:12 +00:00
// event listener on body, if the element is mailgo-compatible the mailgo modal will be rendered
document.addEventListener("click", mailgoCheckRender);
2020-04-24 19:51:34 +00:00
};
/**
* mailgoRender
* function to render a mailgo (mail or tel)
*/
2020-04-25 13:06:29 +00:00
const mailgoRender = (type = MAIL_TYPE, mailgo: HTMLLinkElement): void => {
2020-04-24 19:51:34 +00:00
// mailgo mail
if (type === MAIL_TYPE) {
// if the element href=^"mailto:"
if (mailgo.href && mailgo.href.toLowerCase().startsWith(MAILTO)) {
mail = decodeURIComponent(
mailgo.href.split("?")[0].split(MAILTO)[1].trim()
);
url = new URL(mailgo.href);
let urlParams: URLSearchParams = url.searchParams;
// optional parameters for the email
cc = urlParams.get("cc");
bcc = urlParams.get("bcc");
subject = urlParams.get("subject");
bodyMail = urlParams.get("body");
} else {
// if the element href="#mailgo" or class="mailgo"
// mail = data-address + @ + data-domain
mail =
mailgo.getAttribute("data-address") +
"@" +
mailgo.getAttribute("data-domain");
url = new URL(MAILTO + encodeURIComponent(mail));
// cc = data-cc-address + @ + data-cc-domain
cc =
mailgo.getAttribute("data-cc-address") +
"@" +
mailgo.getAttribute("data-cc-domain");
// bcc = data-bcc-address + @ + data-bcc-domain
bcc =
mailgo.getAttribute("data-bcc-address") +
"@" +
mailgo.getAttribute("data-bcc-domain");
// subject = data-subject
subject = mailgo.getAttribute("data-subject");
// body = data-body
bodyMail = mailgo.getAttribute("data-body");
2019-09-16 21:06:40 +00:00
}
2020-04-24 19:51:34 +00:00
// validate the email address
if (!validateEmails(mail.split(","))) return;
// if cc, bcc is not valid cc, bcc = ""
if (cc && !validateEmails(cc.split(","))) cc = "";
if (bcc && !validateEmails(bcc.split(","))) bcc = "";
// the title of the modal (email address)
title.innerHTML = mail.split(",").join("<br/>");
// add the details if provided
cc
? ((detailCc.style.display = "block"),
(ccValue.innerHTML = cc.split(",").join("<br/>")))
: (detailCc.style.display = "none");
bcc
? ((detailBcc.style.display = "block"),
(bccValue.innerHTML = bcc.split(",").join("<br/>")))
: (detailBcc.style.display = "none");
subject
? ((detailSubject.style.display = "block"),
(subjectValue.textContent = subject))
: (detailSubject.style.display = "none");
bodyMail
? ((detailBody.style.display = "block"),
(bodyValue.textContent = bodyMail))
: (detailBody.style.display = "none");
// add the actions
gmail.addEventListener("click", openGmail);
outlook.addEventListener("click", openOutlook);
encEmail = encodeEmail(mail);
open.addEventListener("click", openDefault);
copyMail.addEventListener("click", () => copy(mail));
}
// mailgo tel
if (type === TEL_TYPE) {
if (mailgo.href && mailgo.href.toLowerCase().startsWith(TEL)) {
tel = decodeURIComponent(mailgo.href.split("?")[0].split(TEL)[1].trim());
} else if (mailgo.href && mailgo.href.toLowerCase().startsWith(CALLTO)) {
tel = decodeURIComponent(
mailgo.href.split("?")[0].split(CALLTO)[1].trim()
);
} else if (mailgo.hasAttribute("data-tel")) {
tel = mailgo.getAttribute("data-tel");
msg = mailgo.getAttribute("data-msg");
2019-10-27 13:05:42 +00:00
}
2020-04-24 19:51:34 +00:00
// information
// let titleEl = getE("m-tel-title");
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// Telegram username
if (mailgo.hasAttribute("data-telegram")) {
telegramUsername = mailgo.getAttribute("data-telegram");
}
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// Telegram username
if (mailgo.hasAttribute("data-skype")) {
skypeUsername = mailgo.getAttribute("data-skype");
}
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// the title of the modal (tel)
titleTel.innerHTML = tel;
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// add the actions to buttons
wa.addEventListener("click", openWhatsApp);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
if (telegramUsername) {
setDisplay("m-tg", "block");
telegram.addEventListener("click", openTelegram);
}
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
skype.addEventListener("click", openSkype);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
call.addEventListener("click", callDefault);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
copyTel.addEventListener("click", () => copy(tel));
}
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// show the mailgo
showMailgo(type);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// add listener keyDown
document.addEventListener("keydown", mailgoKeydown);
};
2019-07-19 10:03:21 +00:00
2020-04-24 19:51:34 +00:00
// actions
2020-04-25 12:36:18 +00:00
const openGmail = (): void => {
2020-04-24 19:51:34 +00:00
// Gmail url
2020-07-03 21:34:26 +00:00
let gmailUrl: string =
2020-04-24 19:51:34 +00:00
"https://mail.google.com/mail/u/0/?view=cm&source=mailto&to=" +
encodeURIComponent(mail);
2019-07-19 10:03:21 +00:00
2020-04-24 19:51:34 +00:00
// the details if provided
if (cc) gmailUrl = gmailUrl.concat("&cc=" + encodeURIComponent(cc));
if (bcc) gmailUrl = gmailUrl.concat("&bcc=" + encodeURIComponent(bcc));
if (subject) gmailUrl = gmailUrl.concat("&subject=" + subject);
if (bodyMail) gmailUrl = gmailUrl.concat("&body=" + bodyMail);
2020-04-24 19:51:34 +00:00
// open the link
window.open(gmailUrl, "_blank");
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// hide the modal
hideMailgo();
};
2019-10-27 13:05:42 +00:00
2020-04-25 12:36:18 +00:00
const openOutlook = (): void => {
2020-04-24 19:51:34 +00:00
// Outlook url
2020-07-03 21:34:26 +00:00
let outlookUrl: string =
2020-04-24 19:51:34 +00:00
"https://outlook.live.com/owa/?path=/mail/action/compose&to=" +
encodeURIComponent(mail);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// the details if provided
if (subject) outlookUrl = outlookUrl.concat("&subject=" + subject);
if (bodyMail) outlookUrl = outlookUrl.concat("&body=" + bodyMail);
2019-07-19 10:03:21 +00:00
2020-04-24 19:51:34 +00:00
// open the link
window.open(outlookUrl, "_blank");
2019-07-19 10:03:21 +00:00
2020-04-24 19:51:34 +00:00
// hide the modal
hideMailgo();
};
2020-04-25 12:36:18 +00:00
const openDefault = (): void => {
2020-04-24 19:51:34 +00:00
mailToEncoded(encEmail);
hideMailgo();
};
2020-04-25 12:36:18 +00:00
const openTelegram = (): void => {
2020-04-24 19:51:34 +00:00
// Telegram url
2020-07-03 21:34:26 +00:00
let tgUrl: string = "https://t.me/" + telegramUsername;
2020-04-24 19:51:34 +00:00
// open the url
window.open(tgUrl, "_blank");
2019-09-16 22:23:41 +00:00
2020-04-24 19:51:34 +00:00
// hide the modal
hideMailgo();
};
2019-10-28 07:36:59 +00:00
2020-04-25 12:36:18 +00:00
const openSkype = (): void => {
2020-07-03 21:34:26 +00:00
let skype: string = skypeUsername !== "" ? skypeUsername : tel;
2019-10-28 07:36:59 +00:00
2020-04-24 19:51:34 +00:00
// Telegram url
2020-07-03 21:34:26 +00:00
let skypeUrl: string = "skype:" + skype;
2019-10-28 07:36:59 +00:00
2020-04-24 19:51:34 +00:00
// open the url
window.open(skypeUrl, "_blank");
// hide the modal
hideMailgo();
};
2019-05-26 11:43:17 +00:00
2020-04-25 12:36:18 +00:00
const openWhatsApp = (): void => {
2020-04-24 19:51:34 +00:00
// WhatsApp url
2020-07-03 21:34:26 +00:00
let waUrl: string = "https://wa.me/" + tel;
2019-09-13 20:29:20 +00:00
2020-04-24 19:51:34 +00:00
// the details if provided
if (msg) waUrl + "?text=" + msg;
2019-09-13 20:29:20 +00:00
2020-04-24 19:51:34 +00:00
// open the url
window.open(waUrl, "_blank");
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// hide the modal
hideMailgo();
};
2019-10-27 13:05:42 +00:00
2020-07-01 12:48:18 +00:00
const callDefault = () => {
2020-07-03 21:34:26 +00:00
let callUrl: string = "tel:" + tel;
2020-04-24 19:51:34 +00:00
window.open(callUrl);
hideMailgo();
};
2020-04-25 12:36:18 +00:00
const copy = (content: string): void => {
2020-04-24 19:51:34 +00:00
copyToClipboard(content);
let activeCopy: HTMLElement;
// the correct copyButton (mail or tel)
mailgoIsShowing(MAIL_TYPE) ? (activeCopy = copyMail) : (activeCopy = copyTel);
activeCopy.textContent = "copied";
setTimeout(() => {
activeCopy.textContent = "copy";
// hide after the timeout
2019-10-28 07:36:59 +00:00
hideMailgo();
2020-04-24 19:51:34 +00:00
}, 999);
};
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// function that returns if an element is a mailgo
2020-04-25 12:36:18 +00:00
const isMailgo = (element: HTMLElement, type: string = MAIL_TYPE): boolean => {
2020-04-24 19:51:34 +00:00
let href: string = (element as HTMLLinkElement).href;
// mailgo type mail
if (type === MAIL_TYPE) {
return (
// first case: it is an <a> element with "mailto:..." in href and no no-mailgo in classList
(href &&
href.toLowerCase().startsWith(MAILTO) &&
!element.classList.contains("no-mailgo")) ||
(element.hasAttribute("data-address") &&
// second case: the href=#mailgo
((href && element.getAttribute("href").toLowerCase() === "#mailgo") ||
// third case: the classList contains mailgo
(element.classList && element.classList.contains("mailgo"))))
);
}
// mailgo type tel
if (type === TEL_TYPE) {
return (
// first case: it is an <a> element with "tel:..." or "callto:..." in href and no no-mailgo in classList
(href &&
(href.toLowerCase().startsWith(TEL) ||
href.toLowerCase().startsWith(CALLTO)) &&
!element.classList.contains("no-mailgo")) ||
(element.hasAttribute("data-tel") &&
// second case: the href=#mailgo
href &&
element.getAttribute("href").toLowerCase() === "#mailgo") ||
// third case: the classList contains mailgo
(element.classList && element.classList.contains("mailgo"))
);
}
2019-10-27 13:13:52 +00:00
2020-04-24 19:51:34 +00:00
return false;
};
2019-10-27 13:13:52 +00:00
2020-04-24 19:51:34 +00:00
/**
* mailgoCheckRender
* function to check if an element is mailgo-enabled or not referencing to
* mail:
* document.querySelectorAll(
* 'a[href^="mailto:" i]:not(.no-mailgo), a[href="#mailgo"], a.mailgo'
* );
* tel:
* document.querySelectorAll(
* 'a[href^="tel:" i]:not(.no-mailgo), a[href="#mailgo"], a.mailgo'
* );
* or
* document.querySelectorAll(
* 'a[href^="callto:" i]:not(.no-mailgo), a[href="#mailgo"], a.mailgo'
* );
*/
2020-04-25 12:36:18 +00:00
const mailgoCheckRender = (event: Event): boolean => {
2020-04-24 19:51:34 +00:00
// check if the id=mailgo exists in the body
if (
!document.contains(getE("mailgo")) ||
!document.contains(getE("mailgo-tel"))
)
2020-07-03 21:34:26 +00:00
return false;
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// if a mailgo is already showing do nothing
2020-04-25 12:36:18 +00:00
if (mailgoIsShowing(MAIL_TYPE) || mailgoIsShowing(TEL_TYPE)) return false;
2019-10-27 13:13:52 +00:00
2020-04-24 19:51:34 +00:00
// the path of the event
let path =
(event.composedPath && event.composedPath()) ||
composedPath(event.target as HTMLElement);
2019-10-27 13:13:52 +00:00
2020-04-24 19:51:34 +00:00
if (path) {
path.forEach((element: HTMLElement) => {
2020-04-25 12:36:18 +00:00
if (element instanceof HTMLDocument || element instanceof Window)
return false;
2019-10-27 13:13:52 +00:00
2020-04-24 19:51:34 +00:00
// go in the event.path to find if the user has clicked on a mailgo element
if (isMailgo(element, MAIL_TYPE)) {
// stop the normal execution of the element click
event.preventDefault();
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// render mailgo
2020-04-25 13:06:29 +00:00
mailgoRender(MAIL_TYPE, element as HTMLLinkElement);
2019-10-27 13:13:52 +00:00
2020-04-25 12:36:18 +00:00
return true;
2020-04-24 19:51:34 +00:00
}
if (isMailgo(element, TEL_TYPE)) {
// stop the normal execution of the element click
event.preventDefault();
2019-10-27 13:13:52 +00:00
2020-04-24 19:51:34 +00:00
// render mailgo
2020-04-25 13:06:29 +00:00
mailgoRender(TEL_TYPE, element as HTMLLinkElement);
2019-10-27 13:13:52 +00:00
2020-04-25 12:36:18 +00:00
return true;
2020-04-24 19:51:34 +00:00
}
});
}
2019-10-28 07:36:59 +00:00
2020-04-25 12:36:18 +00:00
return false;
2020-04-24 19:51:34 +00:00
};
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
/**
* mailgoKeydown
* function to manage the keydown event when the modal is showing
*/
2020-04-25 12:36:18 +00:00
const mailgoKeydown = (event: KeyboardEvent): void => {
2020-04-24 19:51:34 +00:00
// if mailgo is showing
if (mailgoIsShowing(MAIL_TYPE)) {
switch (event.keyCode) {
case 27:
// Escape
hideMailgo();
break;
case 71:
// g -> open GMail
openGmail();
break;
case 79:
// o -> open Outlook
openOutlook();
break;
case 32:
case 13:
// spacebar or enter -> open default
openDefault();
break;
case 67:
// c -> copy
copy(mail);
break;
default:
return;
2019-10-27 13:05:42 +00:00
}
2020-04-24 19:51:34 +00:00
} else if (mailgoIsShowing(TEL_TYPE)) {
switch (event.keyCode) {
case 27:
// Escape
hideMailgo();
break;
case 84:
// t -> open Telegram
openTelegram();
break;
case 87:
// w -> open WhatsApp
openWhatsApp();
break;
case 32:
case 13:
// spacebar or enter -> call default
callDefault();
break;
case 67:
// c -> copy
copy(tel);
break;
default:
return;
2019-10-27 13:05:42 +00:00
}
2020-04-24 19:51:34 +00:00
}
return;
};
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// show the modal
2020-04-25 12:36:18 +00:00
const showMailgo = (type = MAIL_TYPE): boolean => {
2020-04-24 19:51:34 +00:00
// show mailgo type mail
if (type === MAIL_TYPE) {
setDisplay("mailgo", "flex");
2020-04-25 12:36:18 +00:00
return true;
2020-04-24 19:51:34 +00:00
}
// show mailgo type tel
if (type === TEL_TYPE) {
setDisplay("mailgo-tel", "flex");
2020-04-25 12:36:18 +00:00
return true;
2020-04-24 19:51:34 +00:00
}
2020-04-25 12:36:18 +00:00
return false;
2020-04-24 19:51:34 +00:00
};
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// hide the modal
2020-04-25 12:36:18 +00:00
const hideMailgo = (): void => {
2020-04-24 19:51:34 +00:00
setDisplay("mailgo", "none");
setDisplay("mailgo-tel", "none");
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// remove listener keyDown
document.removeEventListener("keydown", mailgoKeydown);
};
2019-10-28 20:03:22 +00:00
2020-04-24 19:51:34 +00:00
// is the mailgo modal hidden?
2020-04-25 12:36:18 +00:00
const mailgoIsShowing = (type = MAIL_TYPE): boolean => {
2020-04-24 19:51:34 +00:00
return type === MAIL_TYPE
? getDisplay("mailgo") === "flex"
: type === TEL_TYPE
? getDisplay("mailgo-tel") === "flex"
: false;
};
2019-10-28 20:03:22 +00:00
2020-04-25 12:36:18 +00:00
const byElement = (): HTMLLinkElement => {
2020-04-24 19:51:34 +00:00
// by
2020-07-07 12:48:37 +00:00
let by: HTMLLinkElement = createElement(aHTMLTag) as HTMLLinkElement;
2020-07-10 08:47:11 +00:00
by.href = "https://mailgo.dev?ref=mailgo-modal";
2020-04-24 19:51:34 +00:00
by.className = "m-by";
by.target = "_blank";
by.rel = "noopener noreferrer";
2020-07-10 08:47:11 +00:00
by.appendChild(createTextNode("mailgo.dev"));
2020-04-24 19:51:34 +00:00
return by;
};
2019-11-07 08:20:14 +00:00
2020-04-24 19:51:34 +00:00
// create element
2020-04-25 12:36:18 +00:00
const createElement = (element: string = "div"): HTMLElement =>
2020-04-24 19:51:34 +00:00
document.createElement(element);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// create text node
2020-04-25 12:36:18 +00:00
const createTextNode = (element: string): Text =>
document.createTextNode(element);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// decrypt email
2020-04-25 12:36:18 +00:00
const mailToEncoded = (encoded: string): string =>
2020-04-24 19:51:34 +00:00
(window.location.href = MAILTO + atob(encoded));
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// encode email
2020-04-25 12:36:18 +00:00
const encodeEmail = (email: string): string => btoa(email);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// getE shorthand
2020-04-25 12:36:18 +00:00
const getE = (id: string): HTMLElement => document.getElementById(id);
2019-05-26 11:43:17 +00:00
2020-04-24 19:51:34 +00:00
// get display value
2020-04-25 12:36:18 +00:00
const getDisplay = (id: string): string => getE(id).style.display;
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// get display value
2020-04-25 12:36:18 +00:00
const setDisplay = (id: string, value: string): string =>
2020-04-24 19:51:34 +00:00
(getE(id).style.display = value);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
// custom composedPath if path or event.composedPath() are not defined
2020-04-25 12:36:18 +00:00
const composedPath = (
el: HTMLElement
): (HTMLElement | Document | (Window & typeof globalThis))[] => {
2020-04-24 19:51:34 +00:00
let path = [];
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
while (el) {
path.push(el);
2019-10-27 13:05:42 +00:00
2020-04-24 19:51:34 +00:00
if (el.tagName === "HTML") {
path.push(document);
path.push(window);
return path;
2019-10-27 13:05:42 +00:00
}
2020-04-24 19:51:34 +00:00
el = el.parentElement;
}
};
2020-04-24 19:51:34 +00:00
// validate a single email with regex
2020-04-25 12:36:18 +00:00
const validateEmail = (email: string): boolean =>
2020-04-24 19:51:34 +00:00
/^(([^<>()[\]\\.,;:\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,}))$/.test(
email
);
// validate an array of emails
2020-04-25 12:36:18 +00:00
const validateEmails = (arr: string[]): boolean => arr.every(validateEmail);
2020-04-24 19:51:34 +00:00
// copy of a string
2020-04-25 12:36:18 +00:00
const copyToClipboard = (str: string): boolean => {
2020-04-25 13:06:29 +00:00
let el: HTMLInputElement = createElement("textarea") as HTMLInputElement;
2020-04-24 19:51:34 +00:00
el.value = str;
el.setAttribute("readonly", "");
el.style.position = "absolute";
el.style.left = "-9999px";
document.body.appendChild(el);
2020-07-03 21:34:26 +00:00
let selected: Range | boolean =
2020-04-24 19:51:34 +00:00
document.getSelection().rangeCount > 0
? document.getSelection().getRangeAt(0)
: false;
el.select();
document.execCommand("copy");
document.body.removeChild(el);
if (selected) {
document.getSelection().removeAllRanges();
document.getSelection().addRange(selected);
2020-04-25 12:36:18 +00:00
return true;
2020-04-24 19:51:34 +00:00
}
2020-04-25 12:36:18 +00:00
return false;
2020-04-24 10:00:23 +00:00
};
2019-10-27 13:05:42 +00:00
2020-04-25 12:36:18 +00:00
const mailgoStyle = (): void => {
2020-04-24 20:03:26 +00:00
// mailgo style
2020-06-16 15:29:04 +00:00
let mailgoCSSElement: HTMLStyleElement = createElement(
"style"
) as HTMLStyleElement;
mailgoCSSElement.id = "mailgo-style";
mailgoCSSElement.type = "text/css";
mailgoCSSElement.appendChild(createTextNode(mailgoCSS));
document.head.appendChild(mailgoCSSElement);
2020-04-24 20:03:26 +00:00
};
2020-04-25 12:15:03 +00:00
// mailgo
2020-07-03 08:06:53 +00:00
function mailgo(mailgoConfig?: MailgoConfig): void {
2020-04-24 19:51:34 +00:00
// if the window is defined...
if (window && typeof window !== "undefined") {
2020-04-24 20:03:26 +00:00
// add the style for mailgo
mailgoStyle();
2020-04-24 19:51:34 +00:00
// if is set an initEvent add the listener
2020-04-25 12:15:03 +00:00
if (mailgoConfig?.initEvent) {
2020-06-10 16:55:12 +00:00
document.addEventListener(mailgoConfig.initEvent, () => {
mailgoInit(mailgoConfig);
});
} else {
2020-06-10 16:55:12 +00:00
mailgoInit(mailgoConfig);
}
2020-04-24 19:51:34 +00:00
}
2020-07-03 08:06:53 +00:00
}
2020-04-25 12:15:03 +00:00
export default mailgo;