2015-10-07 18:18:44 +00:00
|
|
|
{% set use_captcha = grav.config.plugins.comments.use_captcha %}
|
|
|
|
|
2015-10-06 17:23:02 +00:00
|
|
|
<h3>Add a Comment</h3>
|
|
|
|
|
|
|
|
<script>
|
2015-10-07 14:23:32 +00:00
|
|
|
function validateEmail(email) {
|
|
|
|
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
|
|
|
|
return re.test(email);
|
|
|
|
}
|
|
|
|
|
2015-10-06 17:23:02 +00:00
|
|
|
jQuery(document).on('click tap', '.js__add-new-comment', function(event) {
|
|
|
|
event.preventDefault();
|
2015-10-07 14:23:32 +00:00
|
|
|
|
|
|
|
var text = $('.js__new-comment-text').val();
|
|
|
|
var name = $('.js__new-comment-name').val();
|
|
|
|
var email = $('.js__new-comment-email').val();
|
2015-10-07 18:18:44 +00:00
|
|
|
var captcha = $('#g-recaptcha-response').val();
|
2015-10-07 14:23:32 +00:00
|
|
|
|
|
|
|
if (text.length == 0 || email.length == 0 || name.length == 0) {
|
|
|
|
alert('Please fill all the fields');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!validateEmail(email)) {
|
|
|
|
alert('Please enter a valid email');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-07 18:18:44 +00:00
|
|
|
{% if use_captcha %}
|
|
|
|
if (!captcha) {
|
|
|
|
alert("Error validating the security code");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
{% endif %}
|
|
|
|
|
2015-10-06 17:23:02 +00:00
|
|
|
jQuery.ajax({
|
|
|
|
url: "{{ grav.uri.rootUrl }}/add-comment",
|
|
|
|
data: {
|
|
|
|
text: $('.js__new-comment-text').val(),
|
|
|
|
name: $('.js__new-comment-name').val(),
|
|
|
|
email: $('.js__new-comment-email').val(),
|
2015-10-07 14:23:32 +00:00
|
|
|
title: "{{ grav.page.header.title }}",
|
|
|
|
lang: "{{ grav.language.getActive }}",
|
2015-10-07 18:18:44 +00:00
|
|
|
path: "{{ grav.uri.path }}",
|
|
|
|
{% if use_captcha %}recaptchaResponse: captcha{% endif %}
|
2015-10-06 17:23:02 +00:00
|
|
|
},
|
|
|
|
type: 'POST'
|
|
|
|
})
|
|
|
|
.success(function() {
|
|
|
|
window.location.reload();
|
|
|
|
})
|
|
|
|
.error(function() {
|
2015-10-07 18:18:44 +00:00
|
|
|
alert("Error while posting the comment");
|
2015-10-06 17:23:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2015-10-07 18:18:44 +00:00
|
|
|
{% if use_captcha %}
|
|
|
|
<script src="https://www.google.com/recaptcha/api.js?onload=captchaOnloadCallback&render=explicit" async defer></script>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
var captchaOnloadCallback = function captchaOnloadCallback() {
|
|
|
|
grecaptcha.render('g-recaptcha', {
|
|
|
|
'sitekey': "{{grav.config.plugins.comments.recatpcha_site_key}}",
|
|
|
|
'callback': captchaValidatedCallback,
|
|
|
|
'expired-callback': captchaExpiredCallback
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var captchaValidatedCallback = function captchaValidatedCallback() {
|
|
|
|
};
|
|
|
|
|
|
|
|
var captchaExpiredCallback = function captchaExpiredCallback() {
|
|
|
|
grecaptcha.reset();
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
{% endif %}
|
|
|
|
|
2015-10-06 17:23:02 +00:00
|
|
|
<form>
|
|
|
|
<textarea class="js__new-comment-text"></textarea>
|
|
|
|
Name: <input type="text" class="js__new-comment-name" />
|
2015-10-07 14:23:32 +00:00
|
|
|
Email: <input type="email" class="js__new-comment-email" />
|
2015-10-07 18:18:44 +00:00
|
|
|
{% if use_captcha %}
|
|
|
|
<div class="g-recaptcha" id="g-recaptcha"></div>
|
|
|
|
{% endif %}
|
2015-10-06 17:23:02 +00:00
|
|
|
|
|
|
|
<input type="submit" class="js__add-new-comment" />
|
|
|
|
</form>
|
|
|
|
|
2015-10-07 14:23:32 +00:00
|
|
|
{% if grav.twig.comments|length %}
|
|
|
|
|
|
|
|
<h3>Comments</h3>
|
|
|
|
|
|
|
|
<table>
|
|
|
|
{% for comment in grav.twig.comments|array_reverse %}
|
|
|
|
<tr>
|
|
|
|
<td>
|
|
|
|
{{comment.text|e}}
|
|
|
|
<br />
|
|
|
|
Written on {{comment.date|e}} by {{comment.author|e}}
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
{% endfor %}
|
|
|
|
</table>
|
|
|
|
{% endif %}
|