global config

This commit is contained in:
Matteo Manzinello 2020-07-10 13:02:17 +02:00
parent c0d13a39c7
commit 241817cfd4
8 changed files with 55 additions and 29 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

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

@ -259,7 +259,9 @@ var DEFAULT_BTN_HREF = "javascript:void(0);"; // useful html tags
var spanHTMLTag = "span";
var aHTMLTag = "a";
var pHTMLTag = "p"; // default language
var pHTMLTag = "p"; // global mailgo config object
var config; // default language
var lang = DEFAULT_LANG; // mailgo variables
@ -285,16 +287,20 @@ var gmail, outlook, mailgo_open, telegram, wa, skype, call, copyMail, copyTel;
*/
var mailgo_mailgoInit = function mailgoInit(mailgoConfig) {
// translations
var _config, _config2;
// set the global config
config = mailgoConfig; // translations
var _ref = i18n_namespaceObject,
translations = _ref.translations; // if a default language is defined use it
if ((mailgoConfig === null || mailgoConfig === void 0 ? void 0 : mailgoConfig.lang) && i18n["languages"].includes(mailgoConfig.lang)) {
lang = mailgoConfig.lang;
if (((_config = config) === null || _config === void 0 ? void 0 : _config.lang) && i18n["languages"].includes(config.lang)) {
lang = config.lang;
} // if is defined <html lang=""> use it!
if (!(mailgoConfig === null || mailgoConfig === void 0 ? void 0 : mailgoConfig.forceLang)) {
if (!((_config2 = config) === null || _config2 === void 0 ? void 0 : _config2.forceLang)) {
// keep the lang from html
var htmlLang = document.documentElement.lang; // if there are translations...
@ -308,13 +314,15 @@ var mailgo_mailgoInit = function mailgoInit(mailgoConfig) {
var strings = translations[lang]; // mailgo mail
{
var _config3;
// modal
var modal = createElement();
modal.style.display = "none";
modal.id = "mailgo";
modal.classList.add("m-modal"); // if dark is in config
if (mailgoConfig === null || mailgoConfig === void 0 ? void 0 : mailgoConfig.dark) {
if ((_config3 = config) === null || _config3 === void 0 ? void 0 : _config3.dark) {
modal.classList.add("m-dark");
} // background
@ -428,6 +436,8 @@ var mailgo_mailgoInit = function mailgoInit(mailgoConfig) {
} // mailgo tel
{
var _config4;
// modal
var _modal = createElement();
@ -437,7 +447,7 @@ var mailgo_mailgoInit = function mailgoInit(mailgoConfig) {
_modal.classList.add("m-modal"); // if dark is in config
if (mailgoConfig === null || mailgoConfig === void 0 ? void 0 : mailgoConfig.dark) {
if ((_config4 = config) === null || _config4 === void 0 ? void 0 : _config4.dark) {
_modal.classList.add("m-dark");
} // background
@ -608,11 +618,13 @@ var mailgoRender = function mailgoRender() {
} else if (mailgo.hasAttribute("data-tel")) {
tel = mailgo.getAttribute("data-tel");
msg = mailgo.getAttribute("data-msg");
} // information
} // validate the phone number
if (!validateTel(tel)) return; // information
// let titleEl = getE("m-tel-title");
// Telegram username
if (mailgo.hasAttribute("data-telegram")) {
telegramUsername = mailgo.getAttribute("data-telegram");
} // Telegram username
@ -980,6 +992,12 @@ var validateEmail = function validateEmail(email) {
var validateEmails = function validateEmails(arr) {
return arr.every(validateEmail);
}; // validate a single tel with regex
var validateTel = function validateTel(tel) {
// TODO: find a good regex for telephone numbers
return true;
}; // copy of a string
@ -1014,18 +1032,18 @@ var mailgoStyle = function mailgoStyle() {
}; // mailgo
function mailgo(mailgoConfig) {
function mailgo(config) {
// if the window is defined...
if (window && typeof window !== "undefined") {
// add the style for mailgo
mailgoStyle(); // if is set an initEvent add the listener
if (mailgoConfig === null || mailgoConfig === void 0 ? void 0 : mailgoConfig.initEvent) {
document.addEventListener(mailgoConfig.initEvent, function () {
mailgo_mailgoInit(mailgoConfig);
if (config === null || config === void 0 ? void 0 : config.initEvent) {
document.addEventListener(config.initEvent, function () {
mailgo_mailgoInit(config);
});
} else {
mailgo_mailgoInit(mailgoConfig);
mailgo_mailgoInit(config);
}
}
}

File diff suppressed because one or more lines are too long

2
mailgo.d.ts vendored
View File

@ -8,6 +8,8 @@ declare module "mailgo" {
dark?: boolean;
lang?: string;
forceLang?: boolean;
validateEmail?: boolean;
validateTel?: boolean;
};
export type MailgoTranslation = {

View File

@ -31,6 +31,9 @@ const spanHTMLTag: string = "span";
const aHTMLTag: string = "a";
const pHTMLTag: string = "p";
// global mailgo config object
let config: MailgoConfig;
// default language
let lang: string = DEFAULT_LANG;
@ -77,18 +80,21 @@ let gmail: HTMLLinkElement,
* the function that creates the mailgo elements in DOM
*/
const mailgoInit = (mailgoConfig?: MailgoConfig): void => {
// set the global config
config = mailgoConfig;
// translations
let {
translations,
}: { translations: MailgoTranslations } = i18n as MailgoI18n;
// if a default language is defined use it
if (mailgoConfig?.lang && i18n.languages.includes(mailgoConfig.lang)) {
lang = mailgoConfig.lang;
if (config?.lang && i18n.languages.includes(config.lang)) {
lang = config.lang;
}
// if is defined <html lang=""> use it!
if (!mailgoConfig?.forceLang) {
if (!config?.forceLang) {
// keep the lang from html
let htmlLang: string = document.documentElement.lang;
@ -111,7 +117,7 @@ const mailgoInit = (mailgoConfig?: MailgoConfig): void => {
modal.classList.add("m-modal");
// if dark is in config
if (mailgoConfig?.dark) {
if (config?.dark) {
modal.classList.add("m-dark");
}
@ -263,7 +269,7 @@ const mailgoInit = (mailgoConfig?: MailgoConfig): void => {
modal.classList.add("m-modal");
// if dark is in config
if (mailgoConfig?.dark) {
if (config?.dark) {
modal.classList.add("m-dark");
}
@ -924,19 +930,19 @@ const mailgoStyle = (): void => {
};
// mailgo
function mailgo(mailgoConfig?: MailgoConfig): void {
function mailgo(config?: MailgoConfig): void {
// if the window is defined...
if (window && typeof window !== "undefined") {
// add the style for mailgo
mailgoStyle();
// if is set an initEvent add the listener
if (mailgoConfig?.initEvent) {
document.addEventListener(mailgoConfig.initEvent, () => {
mailgoInit(mailgoConfig);
if (config?.initEvent) {
document.addEventListener(config.initEvent, () => {
mailgoInit(config);
});
} else {
mailgoInit(mailgoConfig);
mailgoInit(config);
}
}
}