blog/themes/beautifulhugo/static/js/staticman.js

47 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

2022-06-25 08:56:31 +01:00
// Static comments
// from: https://github.com/eduardoboucas/popcorn/blob/gh-pages/js/main.js
(function ($) {
var $comments = $('.js-comments');
$('.js-form').submit(function () {
var form = this;
2022-08-21 10:48:00 +01:00
let url = $(this).attr('action');
let data = $(this).serialize();
2022-06-25 08:56:31 +01:00
$(form).addClass('form--loading');
2022-08-21 10:48:00 +01:00
var xhr = new XMLHttpRequest();
xhr.open("POST", url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onreadystatechange = function () {
if(xhr.readyState === XMLHttpRequest.DONE) {
var status = xhr.status;
if (status >= 200 && status < 400) {
showModal('Perfect !', 'Thanks for your comment! It will show on the site once it has been approved. .');
$(form).removeClass('form--loading');
} else {
console.error(xhr.statusText);
showModal('Error', 'Sorry, there was an error with the submission!');
$(form).removeClass('form--loading');
}
2022-06-25 08:56:31 +01:00
}
2022-08-21 10:48:00 +01:00
};
xhr.send(data);
2022-06-25 08:56:31 +01:00
return false;
});
$('.js-close-modal').click(function () {
$('body').removeClass('show-modal');
});
function showModal(title, message) {
$('.js-modal-title').text(title);
$('.js-modal-text').html(message);
$('body').addClass('show-modal');
}
})(jQuery);