Convert public.js to es6 syntax
[MAILPOET-2741]
This commit is contained in:
@@ -2,50 +2,47 @@ import MailPoet from 'mailpoet';
|
|||||||
import jQuery from 'jquery';
|
import jQuery from 'jquery';
|
||||||
import 'parsleyjs';
|
import 'parsleyjs';
|
||||||
|
|
||||||
jQuery(function ($) { // eslint-disable-line func-names
|
jQuery(($) => {
|
||||||
window.reCaptchaCallback = function reCaptchaCallback() {
|
window.reCaptchaCallback = function reCaptchaCallback() {
|
||||||
$('.mailpoet_recaptcha').each(function () { // eslint-disable-line func-names
|
$('.mailpoet_recaptcha').each((index, element) => {
|
||||||
var sitekey = $(this).attr('data-sitekey');
|
const recaptcha = $(element);
|
||||||
var container = $(this).find('> .mailpoet_recaptcha_container').get(0);
|
const sitekey = recaptcha.attr('data-sitekey');
|
||||||
var field = $(this).find('> .mailpoet_recaptcha_field');
|
const container = recaptcha.find('> .mailpoet_recaptcha_container').get(0);
|
||||||
var widgetId;
|
const field = recaptcha.find('> .mailpoet_recaptcha_field');
|
||||||
|
let widgetId;
|
||||||
if (sitekey) {
|
if (sitekey) {
|
||||||
widgetId = window.grecaptcha.render(container, { sitekey: sitekey, size: 'compact' });
|
widgetId = window.grecaptcha.render(container, { sitekey, size: 'compact' });
|
||||||
field.val(widgetId);
|
field.val(widgetId);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function isSameDomain(url) {
|
function isSameDomain(url) {
|
||||||
var link = document.createElement('a');
|
const link = document.createElement('a');
|
||||||
link.href = url;
|
link.href = url;
|
||||||
return (window.location.hostname === link.hostname);
|
return (window.location.hostname === link.hostname);
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateCaptcha(e) {
|
function updateCaptcha(e) {
|
||||||
var captcha;
|
const captcha = $('img.mailpoet_captcha');
|
||||||
var captchaSrc;
|
|
||||||
var hashPos;
|
|
||||||
var newSrc;
|
|
||||||
captcha = $('img.mailpoet_captcha');
|
|
||||||
if (!captcha.length) return false;
|
if (!captcha.length) return false;
|
||||||
captchaSrc = captcha.attr('src');
|
const captchaSrc = captcha.attr('src');
|
||||||
hashPos = captchaSrc.indexOf('#');
|
const hashPos = captchaSrc.indexOf('#');
|
||||||
newSrc = hashPos > 0 ? captchaSrc.substring(0, hashPos) : captchaSrc;
|
const newSrc = hashPos > 0 ? captchaSrc.substring(0, hashPos) : captchaSrc;
|
||||||
captcha.attr('src', newSrc + '#' + new Date().getTime());
|
captcha.attr('src', `${newSrc}#${new Date().getTime()}`);
|
||||||
if (e) e.preventDefault();
|
if (e) e.preventDefault();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$(function () { // eslint-disable-line func-names
|
$(() => {
|
||||||
// setup form validation
|
// setup form validation
|
||||||
$('form.mailpoet_form').each(function () { // eslint-disable-line func-names
|
$('form.mailpoet_form').each((index, element) => {
|
||||||
var form = $(this);
|
const form = $(element);
|
||||||
// Detect form is placed in tight container
|
// Detect form is placed in tight container
|
||||||
if (form.width() < 500) {
|
if (form.width() < 500) {
|
||||||
form.addClass('mailpoet_form_tight_container');
|
form.addClass('mailpoet_form_tight_container');
|
||||||
}
|
}
|
||||||
form.parsley().on('form:validated', function () { // eslint-disable-line func-names
|
form.parsley().on('form:validated', () => {
|
||||||
// clear messages
|
// clear messages
|
||||||
form.find('.mailpoet_message > p').hide();
|
form.find('.mailpoet_message > p').hide();
|
||||||
|
|
||||||
@@ -55,8 +52,8 @@ jQuery(function ($) { // eslint-disable-line func-names
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
form.parsley().on('form:submit', function (parsley) { // eslint-disable-line func-names
|
form.parsley().on('form:submit', (parsley) => {
|
||||||
var formData = form.mailpoetSerializeObject() || {};
|
const formData = form.mailpoetSerializeObject() || {};
|
||||||
// check if we're on the same domain
|
// check if we're on the same domain
|
||||||
if (isSameDomain(window.MailPoetForm.ajax_url) === false) {
|
if (isSameDomain(window.MailPoetForm.ajax_url) === false) {
|
||||||
// non ajax post request
|
// non ajax post request
|
||||||
@@ -77,10 +74,10 @@ jQuery(function ($) { // eslint-disable-line func-names
|
|||||||
action: 'subscribe',
|
action: 'subscribe',
|
||||||
data: formData.data,
|
data: formData.data,
|
||||||
})
|
})
|
||||||
.fail(function handleFailedPost(response) {
|
.fail((response) => {
|
||||||
if (
|
if (
|
||||||
response.meta !== undefined
|
response.meta !== undefined
|
||||||
&& response.meta.redirect_url !== undefined
|
&& response.meta.redirect_url !== undefined
|
||||||
) {
|
) {
|
||||||
// go to page
|
// go to page
|
||||||
window.top.location.href = response.meta.redirect_url;
|
window.top.location.href = response.meta.redirect_url;
|
||||||
@@ -89,23 +86,21 @@ jQuery(function ($) { // eslint-disable-line func-names
|
|||||||
updateCaptcha();
|
updateCaptcha();
|
||||||
}
|
}
|
||||||
form.find('.mailpoet_validate_error').html(
|
form.find('.mailpoet_validate_error').html(
|
||||||
response.errors.map(function buildErrorMessage(error) {
|
response.errors.map((error) => error.message).join('<br />')
|
||||||
return error.message;
|
|
||||||
}).join('<br />')
|
|
||||||
).show();
|
).show();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.done(function handleRecaptcha(response) {
|
.done((response) => {
|
||||||
if (window.grecaptcha && formData.recaptcha) {
|
if (window.grecaptcha && formData.recaptcha) {
|
||||||
window.grecaptcha.reset(formData.recaptcha);
|
window.grecaptcha.reset(formData.recaptcha);
|
||||||
}
|
}
|
||||||
return response;
|
return response;
|
||||||
})
|
})
|
||||||
.done(function handleSuccess(response) {
|
.done((response) => {
|
||||||
// successfully subscribed
|
// successfully subscribed
|
||||||
if (
|
if (
|
||||||
response.meta !== undefined
|
response.meta !== undefined
|
||||||
&& response.meta.redirect_url !== undefined
|
&& response.meta.redirect_url !== undefined
|
||||||
) {
|
) {
|
||||||
// go to page
|
// go to page
|
||||||
window.location.href = response.meta.redirect_url;
|
window.location.href = response.meta.redirect_url;
|
||||||
@@ -130,13 +125,13 @@ jQuery(function ($) { // eslint-disable-line func-names
|
|||||||
// resize iframe
|
// resize iframe
|
||||||
if (
|
if (
|
||||||
window.frameElement !== null
|
window.frameElement !== null
|
||||||
&& MailPoet !== undefined
|
&& MailPoet !== undefined
|
||||||
&& MailPoet.Iframe
|
&& MailPoet.Iframe
|
||||||
) {
|
) {
|
||||||
MailPoet.Iframe.autoSize(window.frameElement);
|
MailPoet.Iframe.autoSize(window.frameElement);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.always(function subscribeFormAlways() {
|
.always(() => {
|
||||||
form.removeClass('mailpoet_form_sending');
|
form.removeClass('mailpoet_form_sending');
|
||||||
});
|
});
|
||||||
return false;
|
return false;
|
@@ -6,4 +6,4 @@
|
|||||||
|
|
||||||
import 'mailpoet'; // side effect - assigns MailPoet to window
|
import 'mailpoet'; // side effect - assigns MailPoet to window
|
||||||
import 'jquery.serialize_object'; // side effect - extends jQuery
|
import 'jquery.serialize_object'; // side effect - extends jQuery
|
||||||
import 'public.js'; // side effect - assigns to window, sets up form validation, etc.
|
import 'public.jsx'; // side effect - assigns to window, sets up form validation, etc.
|
||||||
|
Reference in New Issue
Block a user