staticman.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Static comments
  2. // from: https://github.com/eduardoboucas/popcorn/blob/gh-pages/js/main.js
  3. (function ($) {
  4. var $comments = $('.js-comments');
  5. $('.js-form').submit(function () {
  6. var form = this;
  7. let url = $(this).attr('action');
  8. let data = $(this).serialize();
  9. $(form).addClass('form--loading');
  10. var xhr = new XMLHttpRequest();
  11. xhr.open("POST", url);
  12. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  13. xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  14. xhr.onreadystatechange = function () {
  15. if(xhr.readyState === XMLHttpRequest.DONE) {
  16. var status = xhr.status;
  17. if (status >= 200 && status < 400) {
  18. showModal('Perfect !', 'Thanks for your comment! It will show on the site once it has been approved. .');
  19. $(form).removeClass('form--loading');
  20. } else {
  21. console.error(xhr.statusText);
  22. showModal('Error', 'Sorry, there was an error with the submission!');
  23. $(form).removeClass('form--loading');
  24. }
  25. }
  26. };
  27. xhr.send(data);
  28. return false;
  29. });
  30. $('.js-close-modal').click(function () {
  31. $('body').removeClass('show-modal');
  32. });
  33. function showModal(title, message) {
  34. $('.js-modal-title').text(title);
  35. $('.js-modal-text').html(message);
  36. $('body').addClass('show-modal');
  37. }
  38. })(jQuery);