staticman.js 2.0 KB

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