88 lines
3.0 KiB
JavaScript
88 lines
3.0 KiB
JavaScript
define([
|
|
'jquery',
|
|
'Magento_Ui/js/modal/modal',
|
|
'mage/validation'
|
|
], function ($, modal) {
|
|
'use strict';
|
|
|
|
return function (config, element) {
|
|
var $popup = $(element);
|
|
var $form = $popup.find('form');
|
|
var $status = $popup.find('.esun-contact-popup__status');
|
|
var $submit = $form.find('[type="submit"]');
|
|
|
|
modal({
|
|
type: 'popup',
|
|
responsive: true,
|
|
innerScroll: true,
|
|
modalClass: 'esun-contact-popup-modal',
|
|
title: $.mage.__('Leave Message'),
|
|
buttons: []
|
|
}, $popup);
|
|
|
|
$(document).on('click.esunContactPopup', config.triggerSelector, function (event) {
|
|
event.preventDefault();
|
|
$status.removeClass('success error').empty();
|
|
$popup.modal('openModal');
|
|
});
|
|
|
|
// Keep product-page delegated button handlers from receiving this click.
|
|
$submit.on('click.esunContactPopup', function (event) {
|
|
event.stopPropagation();
|
|
});
|
|
|
|
$form.on('submit.esunContactPopup', function (event) {
|
|
var scrollPosition = window.pageYOffset || document.documentElement.scrollTop;
|
|
|
|
event.preventDefault();
|
|
event.stopImmediatePropagation();
|
|
|
|
if (!$form.validation('isValid')) {
|
|
window.scrollTo(0, scrollPosition);
|
|
window.requestAnimationFrame(function () {
|
|
window.scrollTo(0, scrollPosition);
|
|
});
|
|
return;
|
|
}
|
|
|
|
$submit.prop('disabled', true).addClass('disabled');
|
|
$status.removeClass('success error').empty();
|
|
|
|
$.ajax({
|
|
url: $form.attr('action'),
|
|
type: 'POST',
|
|
dataType: 'json',
|
|
data: $form.serialize()
|
|
}).done(function (response) {
|
|
if (response.success) {
|
|
$(document).trigger('esun:generateLead', {leadType: 'contact_popup'});
|
|
$form[0].reset();
|
|
$status.addClass('success').text(response.message);
|
|
} else {
|
|
$status.addClass('error').text(response.message);
|
|
}
|
|
}).fail(function (xhr) {
|
|
var response = xhr.responseJSON || {};
|
|
|
|
$status.addClass('error').text(
|
|
response.message || $.mage.__('Unable to send your message. Please try again.')
|
|
);
|
|
}).always(function () {
|
|
$submit.prop('disabled', false).removeClass('disabled');
|
|
window.scrollTo(0, scrollPosition);
|
|
window.requestAnimationFrame(function () {
|
|
window.scrollTo(0, scrollPosition);
|
|
});
|
|
});
|
|
});
|
|
|
|
// Register Magento validation after our isolated submit handler.
|
|
$form.validation();
|
|
$form.off('invalid-form.validate');
|
|
|
|
if ($form.data('validator')) {
|
|
$form.data('validator').settings.focusInvalid = false;
|
|
}
|
|
};
|
|
});
|