1
function showErrors(errorsArray, defaultField) {
2
errorsArray.forEach(function (err) {
3
var id_field = err.path;
4
if (id_field === 'unknown') {
5
id_field = defaultField;
7
var input = $('#' + id_field);
8
input.addClass('error');
9
input.parent().append('<small class="error">' + err.message + '</small>');
13
function clearErrors(fields) {
14
fields.forEach(function (id) {
15
var input = $('#' + id);
16
if (input.hasClass('error')) {
17
input.parent().find('small.error').remove();
18
input.removeClass('error');
24
var form = $('#comment_form');
26
var storageLocal = window.localStorage;
27
var defaultField = 'comment_text';
28
var allFields = ['name', 'email', 'website', 'comment_text'];
32
var comment_url = form.attr('data-url');
33
var comments_section = $('section.comments');
35
comments_section.on('submit', '#comment_form', function() {
36
var formData = $('#comment_form').serialize();
37
$('.ajax-loader').css('display', 'flex');
43
success: function(data) {
45
clearErrors(allFields);
47
$('#comments-wrapper').append($('#comment_add'));
48
$('#comment_text').val('');
49
$('#parentId').val(0);
50
$('#comments_thread').load(window.location.pathname + ' #comments_thread > *', function () {
51
$('.ajax-loader').hide();
54
showErrors(data.errors, defaultField);
55
$('.ajax-loader').hide();
59
clearErrors(allFields);
60
showErrors([{path: defaultField, message: 'Непонятная ошибка 🤔'}], defaultField);
61
$('.ajax-loader').hide();
68
comments_section.on('click', '.comment-reply span', function() {
69
var parent_id = $(this).attr('data-comment-id');
71
$('#form_bottom_' + parent_id).append($('#comment_add'));
72
$('#parentId').val(parent_id);
75
comments_section.on('click', '#topic-reply span', function() {
76
$('#comments-wrapper').append($('#comment_add'));
77
$('#parentId').val(0);
80
function initCommentator() {
82
var nameField = $('#name');
83
if (nameField.length) {
84
if (storageLocal.getItem('nickname')) {
85
nameField.val(storageLocal.getItem('nickname'));
87
if (storageLocal.getItem('email')) {
88
$('#email').val(storageLocal.getItem('email'));
90
if (storageLocal.getItem('website')) {
91
$('#website').val(storageLocal.getItem('website'));
97
function saveCommentator() {
99
var nameField = $('#name');
100
if (nameField.length) {
101
storageLocal.setItem('nickname', nameField.val());
102
storageLocal.setItem('email', $('#email').val());
103
storageLocal.setItem('website', $('#website').val());