This commit is contained in:
Flavio Copes 2015-10-07 23:53:40 +02:00
parent a178946aba
commit 5790ac019e
1 changed files with 65 additions and 61 deletions

View File

@ -3,59 +3,61 @@
<h3>Add a Comment</h3> <h3>Add a Comment</h3>
<script> <script>
function validateEmail(email) { $(function() {
var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; function validateEmail(email) {
return re.test(email); var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
} return re.test(email);
jQuery(document).on('click tap', '.js__add-new-comment', function(event) {
event.preventDefault();
var text = $('.js__new-comment-text').val();
var name = $('.js__new-comment-name').val();
var email = $('.js__new-comment-email').val();
var captcha = $('#g-recaptcha-response').val();
if (text.length == 0 || email.length == 0 || name.length == 0) {
$('.alert').html('Please fill all the fields');
$('.alert-container').show();
return;
} }
if (!validateEmail(email)) { $(document).on('click tap', '.js__add-new-comment', function(event) {
$('.alert').html('Please enter a valid email'); event.preventDefault();
$('.alert-container').show();
return;
}
{% if use_captcha %} var text = $('.js__new-comment-text').val();
if (!captcha) { var name = $('.js__new-comment-name').val();
$('.alert').html("Error validating the security code"); var email = $('.js__new-comment-email').val();
$('.alert-container').show(); var captcha = $('#g-recaptcha-response').val();
return;
}
{% endif %}
jQuery.ajax({ if (text.length == 0 || email.length == 0 || name.length == 0) {
url: "{{ grav.uri.rootUrl }}/add-comment", $('.alert').html('Please fill all the fields');
data: { $('.alert-container').show();
text: $('.js__new-comment-text').val(), return;
name: $('.js__new-comment-name').val(), }
email: $('.js__new-comment-email').val(),
title: "{{ grav.page.header.title }}", if (!validateEmail(email)) {
lang: "{{ grav.language.getActive }}", $('.alert').html('Please enter a valid email');
path: "{{ grav.uri.path }}", $('.alert-container').show();
{% if use_captcha %}recaptchaResponse: captcha{% endif %} return;
}, }
type: 'POST'
}) {% if use_captcha %}
.success(function() { if (!captcha) {
$('.alert-container').hide(); $('.alert').html("Error validating the security code");
window.location.reload(); $('.alert-container').show();
}) return;
.error(function() { }
$('.alert').html("Error while posting the comment"); {% endif %}
$('.alert-container').show();
$.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(),
title: "{{ grav.page.header.title }}",
lang: "{{ grav.language.getActive }}",
path: "{{ grav.uri.path }}",
{% if use_captcha %}recaptchaResponse: captcha{% endif %}
},
type: 'POST'
})
.success(function() {
$('.alert-container').hide();
window.location.reload();
})
.error(function() {
$('.alert').html("Error while posting the comment");
$('.alert-container').show();
});
}); });
}); });
</script> </script>
@ -64,20 +66,22 @@ jQuery(document).on('click tap', '.js__add-new-comment', function(event) {
<script src="https://www.google.com/recaptcha/api.js?onload=captchaOnloadCallback&render=explicit" async defer></script> <script src="https://www.google.com/recaptcha/api.js?onload=captchaOnloadCallback&render=explicit" async defer></script>
<script> <script>
var captchaOnloadCallback = function captchaOnloadCallback() { $(function() {
grecaptcha.render('g-recaptcha', { var captchaOnloadCallback = function captchaOnloadCallback() {
'sitekey': "{{grav.config.plugins.comments.recatpcha_site_key}}", grecaptcha.render('g-recaptcha', {
'callback': captchaValidatedCallback, 'sitekey': "{{grav.config.plugins.comments.recatpcha_site_key}}",
'expired-callback': captchaExpiredCallback 'callback': captchaValidatedCallback,
}); 'expired-callback': captchaExpiredCallback
} });
}
var captchaValidatedCallback = function captchaValidatedCallback() { var captchaValidatedCallback = function captchaValidatedCallback() {
}; };
var captchaExpiredCallback = function captchaExpiredCallback() { var captchaExpiredCallback = function captchaExpiredCallback() {
grecaptcha.reset(); grecaptcha.reset();
}; };
});
</script> </script>
{% endif %} {% endif %}