/* ==================================================================== ОБЕРТКА ДЛЯ ОЖИДАНИЯ JQUERY ==================================================================== */ (function waitForJQuery() { if (typeof jQuery === 'undefined') { setTimeout(waitForJQuery, 10); } else { (function($) { if (typeof aveabspath === 'undefined') { window.aveabspath = '/'; } /* --- ТАЙМЕРЫ --- */ function initCommentTimers() { $('.timer-count:not(.timer-running)').each(function() { var $timer = $(this); $timer.addClass('timer-running'); var timeLeft = parseInt($timer.attr('data-left')); var cid = $timer.attr('id').replace('timer_', ''); var countdown = setInterval(function() { timeLeft--; if (timeLeft <= 0) { clearInterval(countdown); $('#controls_' + cid).fadeOut(300); $('#timer_container_' + cid).html('Время на правку истекло'); return; } var minutes = Math.floor(timeLeft / 60); var seconds = timeLeft % 60; $timer.text(minutes + ':' + (seconds < 10 ? '0' : '') + seconds); $timer.attr('data-left', timeLeft); }, 1000); $timer.data('interval-id', countdown); }); } /* Limit Plugin (Исправленный с возвратом контекста) */ $.fn.extend({ limit: function(limit, element) { return this.each(function() { var self = $(this); function substring() { var val = self.val(); if (val.length > limit) self.val(val.substring(0, limit)); if (typeof element !== 'undefined') { var remaining = limit - self.val().length; $(element).html(remaining < 0 ? '0' : remaining); } } self.on('focus keyup paste', substring); substring(); }); } }); function getCaptha() { var now = new Date(); $('#captcha img').attr('src', aveabspath + 'inc/captcha.php?cd=' + now.getTime()); } function validate(form) { var checks = [ { field: form.comment_author_name, msg: "Введите имя" }, { field: form.comment_author_email, msg: "Введите Email" }, { field: form.comment_text, msg: "Введите текст" } ]; for (var i = 0; i < checks.length; i++) { if (checks[i].field && !checks[i].field.value.trim()) { alert(checks[i].msg); $(checks[i].field).focus(); return false; } } return true; } /* --- ДЕЙСТВИЯ (DELETE, ANSWER, LOCK) --- */ function cAction(obj, action) { var $btn = $(obj); var cid = $btn.data('id'); if (!cid) return; if (action === 'answer') { $('#parent_id').val(cid); $('#mod_comment_new').insertBefore('#end' + cid).show(); $('html, body').animate({ scrollTop: $('#mod_comment_new').offset().top - 150 }, 500); return; } $.get(aveabspath + 'index.php', { module: 'comment', action: action, docid: typeof DOC_ID !== 'undefined' ? DOC_ID : '', Id: cid }, function() { if (action === 'delete') { var $commentBlock = $btn.closest('.mod_comment_comment'); if ($commentBlock.find('#mod_comment_new').length > 0) { $('#mod_comment_new').insertAfter($('.mod_comment_comment').last()); } $commentBlock.fadeOut(300, function() { $(this).remove(); }); } if (action === 'unlock' || action === 'lock') { var isLock = (action === 'lock'); $btn.toggleClass('mod_comment_lock mod_comment_unlock text-dark text-success') .attr('title', isLock ? 'Разблокировать' : 'Заблокировать') .find('i').toggleClass('bi-lock-fill bi-unlock-fill'); } }); } /* --- ИНИЦИАЛИЗАЦИЯ --- */ $(document).ready(function() { initCommentTimers(); var $doc = $(document); // Делегирование с защитой от двойного клика через .off() $doc.off('click', '.mod_comment_answer').on('click', '.mod_comment_answer', function(e) { e.preventDefault(); cAction(this, 'answer'); }); $doc.off('click', '.mod_comment_delete').on('click', '.mod_comment_delete', function(e) { e.preventDefault(); if (confirm('Удалить этот комментарий?')) cAction(this, 'delete'); }); $doc.off('click', '.mod_comment_lock, .mod_comment_unlock').on('click', '.mod_comment_lock, .mod_comment_unlock', function(e) { e.preventDefault(); cAction(this, $(this).hasClass('mod_comment_lock') ? 'lock' : 'unlock'); }); // Редактирование $doc.off('click', '.mod_comment_edit').on('click', '.mod_comment_edit', function(e) { e.preventDefault(); var cid = $(this).data('id'); var $textBlock = $('#comment_wrapper_' + cid).find('.mod_comment_text'); $('.edit-form-container').remove(); $('.mod_comment_text').show(); var cleanText = $textBlock.html().replace(//mg, "\n").trim(); var currentImg = $('#comment_wrapper_' + cid).find('.mod_comment_attached_image img').attr('src'); var editHtml = `
${currentImg ? `
` : ''}
Осталось:
`; $textBlock.hide().after(editHtml); $('#ta_' + cid).limit(1000, '#charsLeft_' + cid).focus(); }); $doc.off('click', '.cancelButton').on('click', '.cancelButton', function() { $(this).closest('.edit-form-container').prev('.mod_comment_text').show(); $(this).closest('.edit-form-container').remove(); }); $doc.off('click', '.saveButton').on('click', '.saveButton', function() { var $btn = $(this); var cid = $btn.data('id'); var fd = new FormData(); fd.append('module', 'comment'); fd.append('action', 'edit'); fd.append('Id', cid); fd.append('text', $('#ta_' + cid).val()); fd.append('delete_image', $('#del_img_' + cid).is(':checked') ? 1 : 0); var file = $('#new_file_' + cid)[0].files[0]; if (file) fd.append('comment_image', file); $.ajax({ url: aveabspath + 'index.php?ajax=1', type: 'POST', data: fd, processData: false, contentType: false, beforeSend: function() { $btn.prop('disabled', true).text('...'); }, success: function() { location.reload(); } }); }); // ОТПРАВКА НОВОГО КОММЕНТАРИЯ $doc.off('submit', '#mod_comment_new form').on('submit', '#mod_comment_new form', function(e) { e.preventDefault(); if (!validate(this)) return false; var $form = $(this); var $btn = $form.find('[type="submit"]'); var originalBtnText = $btn.text(); $.ajax({ url: aveabspath + 'index.php?ajax=1', type: 'POST', data: new FormData(this), processData: false, contentType: false, beforeSend: function() { $btn.prop('disabled', true).text('Отправка...'); }, success: function(data) { if (data.includes('wrong_securecode')) { alert("Код капчи введен неверно!"); getCaptha(); $btn.prop('disabled', false).text(originalBtnText); } else { location.reload(); } }, error: function() { alert("Ошибка связи с сервером"); $btn.prop('disabled', false).text(originalBtnText); } }); }); $doc.off('click', '#captcha img, #reload_captcha').on('click', '#captcha img, #reload_captcha', function(e) { e.preventDefault(); getCaptha(); }); }); })(jQuery); } })();