From 81a37be18f12f2eba968ad5df4070f93a91eb6aa Mon Sep 17 00:00:00 2001 From: Pavel Dohnal Date: Thu, 3 Sep 2020 09:31:00 +0200 Subject: [PATCH] Display form on mouse exit [MAILPOET-3057] --- assets/js/src/public.jsx | 24 ++++++++++++++++++------ lib/Form/DisplayFormInWPContent.php | 7 +++++++ views/form/front_end_form.html | 1 + 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/assets/js/src/public.jsx b/assets/js/src/public.jsx index c1bc591e95..897eb10df7 100644 --- a/assets/js/src/public.jsx +++ b/assets/js/src/public.jsx @@ -70,6 +70,15 @@ jQuery(($) => { }); } + function doDisplayForm(formDiv, showOverlay) { + formDiv.addClass('active'); + checkFormContainer(formDiv); + + if (showOverlay) { + formDiv.prev('.mailpoet_form_popup_overlay').addClass('active'); + } + } + function showForm(formDiv, showOverlay = false) { const form = formDiv.find('form'); const position = form.data('position'); @@ -82,13 +91,16 @@ jQuery(($) => { delay = 0; } setTimeout(() => { - formDiv.addClass('active'); - checkFormContainer(formDiv); - - if (showOverlay) { - formDiv.prev('.mailpoet_form_popup_overlay').addClass('active'); - } + doDisplayForm(formDiv, showOverlay); }, delay * 1000); + + const exitIntentEnabled = form.data('exit-intent-enabled'); + if (exitIntentEnabled) { + $(document).on('mouseleave', () => { + doDisplayForm(formDiv, showOverlay); + $(document).off('mouseleave'); + }); + } } const closeForm = (formDiv) => { diff --git a/lib/Form/DisplayFormInWPContent.php b/lib/Form/DisplayFormInWPContent.php index 7c72ba61a3..a4d667e82a 100644 --- a/lib/Form/DisplayFormInWPContent.php +++ b/lib/Form/DisplayFormInWPContent.php @@ -157,6 +157,13 @@ class DisplayFormInWPContent { $templateData['position'] = $formSettings['form_placement'][$displayType]['position'] ?? ''; $templateData['backgroundColor'] = $formSettings['backgroundColor'] ?? ''; $templateData['fontFamily'] = $formSettings['font_family'] ?? ''; + $templateData['enableExitIntent'] = false; + if ( + isset($formSettings['form_placement'][$displayType]['exit_intent_enabled']) + && ($formSettings['form_placement'][$displayType]['exit_intent_enabled'] === '1') + ) { + $templateData['enableExitIntent'] = true; + } // generate security token $templateData['token'] = Security::generateToken(); diff --git a/views/form/front_end_form.html b/views/form/front_end_form.html index 1d49938a15..11222b6922 100644 --- a/views/form/front_end_form.html +++ b/views/form/front_end_form.html @@ -34,6 +34,7 @@ novalidate data-delay="<%= delay %>" data-position="<%= position %>" + data-exit-intent-enabled="<%= enableExitIntent %>" data-background-color="<%= backgroundColor %>" data-font-family="<%= fontFamily %>" >