staticman.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. ---
  2. layout: null
  3. ---
  4. (function ($) {
  5. $('#new_comment').submit(function () {
  6. const form = this;
  7. $(form).addClass('disabled');
  8. {% assign sm = site.staticman -%}
  9. const endpoint = '{{ sm.endpoint }}';
  10. const repository = '{{ sm.repository }}';
  11. const branch = '{{ sm.branch }}';
  12. const url = endpoint + repository + '/' + branch + '/comments';
  13. const data = $(this).serialize();
  14. const xhr = new XMLHttpRequest();
  15. xhr.open("POST", url);
  16. xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  17. xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  18. xhr.onreadystatechange = function () {
  19. if(xhr.readyState === XMLHttpRequest.DONE) {
  20. const status = xhr.status;
  21. if (status >= 200 && status < 400) {
  22. formSubmitted();
  23. } else {
  24. formError();
  25. }
  26. }
  27. };
  28. function formSubmitted() {
  29. $('#comment-form-submit').addClass('d-none');
  30. $('#comment-form-submitted').removeClass('d-none');
  31. $('.page__comments-form .js-notice').removeClass('alert-danger');
  32. $('.page__comments-form .js-notice').addClass('alert-success');
  33. showAlert('success');
  34. }
  35. function formError() {
  36. $('#comment-form-submitted').addClass('d-none');
  37. $('#comment-form-submit').removeClass('d-none');
  38. $('.page__comments-form .js-notice').removeClass('alert-success');
  39. $('.page__comments-form .js-notice').addClass('alert-danger');
  40. showAlert('failure');
  41. $(form).removeClass('disabled');
  42. }
  43. xhr.send(data);
  44. return false;
  45. });
  46. function showAlert(message) {
  47. $('.page__comments-form .js-notice').removeClass('d-none');
  48. if (message == 'success') {
  49. $('.page__comments-form .js-notice-text-success').removeClass('d-none');
  50. $('.page__comments-form .js-notice-text-failure').addClass('d-none');
  51. } else {
  52. $('.page__comments-form .js-notice-text-success').addClass('d-none');
  53. $('.page__comments-form .js-notice-text-failure').removeClass('d-none');
  54. }
  55. }
  56. })(jQuery);