From d6962993895ed897661ee730c95db900597105db Mon Sep 17 00:00:00 2001 From: Matteo Manzinello Date: Fri, 24 Apr 2020 22:21:06 +0200 Subject: [PATCH] go on with typescript, support for ssr and configuration --- index.d.ts | 2 +- src/mailgo.script.js | 3 ++- src/mailgo.ts | 24 +++++++----------------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/index.d.ts b/index.d.ts index 0e86d15..1a426c4 100644 --- a/index.d.ts +++ b/index.d.ts @@ -4,4 +4,4 @@ export as namespace Mailgo; -export function mailgo(mailgoConfig: any | undefined): void; +export function mailgo(mailgoConfig?: any): void; diff --git a/src/mailgo.script.js b/src/mailgo.script.js index 647a33b..164b3fd 100644 --- a/src/mailgo.script.js +++ b/src/mailgo.script.js @@ -1,2 +1,3 @@ import { mailgoDOMContentLoaded } from "../mailgo"; -mailgoDOMContentLoaded(); + +mailgoDOMContentLoaded({ initEvent: "DOMContentLoaded" }); diff --git a/src/mailgo.ts b/src/mailgo.ts index f678016..48d170d 100644 --- a/src/mailgo.ts +++ b/src/mailgo.ts @@ -823,28 +823,18 @@ const mailgoStyle = () => { }; // start default mailgo -export const mailgoDOMContentLoaded = () => { +export const mailgo = (mailgoConfig?: any) => { // if the window is defined... if (window && typeof window !== "undefined") { // add the style for mailgo mailgoStyle(); - // DOMContentLoaded -> mailgoInit (creates the modals) - document.addEventListener("DOMContentLoaded", mailgoInit); - - // event listener on body, if the element is mailgo-compatible the mailgo modal will be rendered - document.addEventListener("click", mailgoCheckRender); - } -}; - -export const mailgo = () => { - // if the window is defined... - if (window && typeof window !== "undefined") { - // add the style for mailgo - mailgoStyle(); - - // mailgo init - mailgoInit(); + // if is set an initEvent add the listener + if (mailgoConfig.initEvent) { + document.addEventListener(mailgoConfig.initEvent, mailgoInit); + } else { + mailgoInit(); + } // event listener on body, if the element is mailgo-compatible the mailgo modal will be rendered document.addEventListener("click", mailgoCheckRender);