Skip to content

Commit 98188bb

Browse files
feat!: remove jquery from staticman comments
1 parent ba84b36 commit 98188bb

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

_includes/staticman-comments.html

-6
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,6 @@ <h3 class="page__comments-title">{{ site.data.ui-text[site.locale].comments_labe
7272
</div>
7373

7474
<!-- Load script to handle comment form submission -->
75-
<!-- doing something a bit funky here because I want to be careful not to include JQuery twice! -->
76-
<script>
77-
if (typeof jQuery == 'undefined') {
78-
document.write('<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-5AkRS45j4ukf+JbWAfHL8P4onPA9p0KwwP7pUdjSQA3ss9edbJUJc/XcYAiheSSz" crossorigin="anonymous"></scr' + 'ipt>');
79-
}
80-
</script>
8175
<script src="{{ "/assets/js/staticman.js" | relative_url }}"></script>
8276
</div>
8377
{% endif %}

assets/js/staticman.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
layout: null
33
---
44

5-
(function ($) {
6-
$('#new_comment').submit(function () {
5+
document.addEventListener('DOMContentLoaded', function() {
6+
document.getElementById('new_comment').addEventListener('submit', function(event) {
7+
event.preventDefault();
78
const form = this;
89

9-
$(form).addClass('disabled');
10+
form.classList.add('disabled');
1011

1112
{% assign sm = site.staticman -%}
1213
const endpoint = '{{ sm.endpoint }}';
1314
const repository = '{{ sm.repository }}';
1415
const branch = '{{ sm.branch }}';
1516
const url = endpoint + repository + '/' + branch + '/comments';
16-
const data = $(this).serialize();
17+
const data = new URLSearchParams(new FormData(form)).toString();
1718

1819
const xhr = new XMLHttpRequest();
1920
xhr.open("POST", url);
@@ -31,35 +32,36 @@ layout: null
3132
};
3233

3334
function formSubmitted() {
34-
$('#comment-form-submit').addClass('d-none');
35-
$('#comment-form-submitted').removeClass('d-none');
36-
$('.page__comments-form .js-notice').removeClass('alert-danger');
37-
$('.page__comments-form .js-notice').addClass('alert-success');
35+
document.getElementById('comment-form-submit').classList.add('d-none');
36+
document.getElementById('comment-form-submitted').classList.remove('d-none');
37+
const notice = document.querySelector('.page__comments-form .js-notice');
38+
notice.classList.remove('alert-danger');
39+
notice.classList.add('alert-success');
3840
showAlert('success');
3941
}
4042

4143
function formError() {
42-
$('#comment-form-submitted').addClass('d-none');
43-
$('#comment-form-submit').removeClass('d-none');
44-
$('.page__comments-form .js-notice').removeClass('alert-success');
45-
$('.page__comments-form .js-notice').addClass('alert-danger');
44+
document.getElementById('comment-form-submitted').classList.add('d-none');
45+
document.getElementById('comment-form-submit').classList.remove('d-none');
46+
const notice = document.querySelector('.page__comments-form .js-notice');
47+
notice.classList.remove('alert-success');
48+
notice.classList.add('alert-danger');
4649
showAlert('failure');
47-
$(form).removeClass('disabled');
50+
form.classList.remove('disabled');
4851
}
4952

5053
xhr.send(data);
51-
52-
return false;
5354
});
5455

5556
function showAlert(message) {
56-
$('.page__comments-form .js-notice').removeClass('d-none');
57-
if (message == 'success') {
58-
$('.page__comments-form .js-notice-text-success').removeClass('d-none');
59-
$('.page__comments-form .js-notice-text-failure').addClass('d-none');
57+
const notice = document.querySelector('.page__comments-form .js-notice');
58+
notice.classList.remove('d-none');
59+
if (message === 'success') {
60+
document.querySelector('.page__comments-form .js-notice-text-success').classList.remove('d-none');
61+
document.querySelector('.page__comments-form .js-notice-text-failure').classList.add('d-none');
6062
} else {
61-
$('.page__comments-form .js-notice-text-success').addClass('d-none');
62-
$('.page__comments-form .js-notice-text-failure').removeClass('d-none');
63+
document.querySelector('.page__comments-form .js-notice-text-success').classList.add('d-none');
64+
document.querySelector('.page__comments-form .js-notice-text-failure').classList.remove('d-none');
6365
}
6466
}
65-
})(jQuery);
67+
});

0 commit comments

Comments
 (0)