сделано управление количеством ответов - теперь есть AJAX подгрузка ответов которые остались после выведенных ответов из числа заданного в админке

This commit is contained in:
2026-01-01 18:33:12 +05:00
parent dbe7b454af
commit cdf1251f24
3 changed files with 85 additions and 11 deletions

View File

@@ -289,6 +289,7 @@ function commentListShow($tpl_dir)
$assign['comment_allowed_extensions'] = $settings['comment_allowed_extensions'] ?? 'jpg,jpeg,png,gif';
$assign['comment_max_file_size'] = $settings['comment_max_file_size'] ?? 2048;
$assign['comment_max_files'] = (int)($settings['comment_max_files'] ?? 5);
$assign['ajax_replies_limit'] = (int)($settings['comment_ajax_replies_limit'] ?? 0);
$comments = array();
@@ -447,6 +448,26 @@ if ($row['comment_changed'] > 0) {
$page_nav = '';
}
// --- НАЧАЛО ВСТАВКИ ДЛЯ AJAX ---
$assign['more_counts'] = [];
// Проверяем, не запросил ли пользователь раскрыть конкретную ветку
$requested_branch = (int)($_REQUEST['ajax_load_branch'] ?? 0);
if ($assign['ajax_replies_limit'] > 0 && !empty($comments)) {
foreach ($comments as $parentId => $subList) {
if ($parentId > 0 && count($subList) > $assign['ajax_replies_limit']) {
// Если это ТА САМАЯ ветка, которую мы подгружаем по AJAX — НЕ обрезаем её
if ($requested_branch == $parentId) {
continue;
}
$assign['more_counts'][$parentId] = count($subList) - $assign['ajax_replies_limit'];
$comments[$parentId] = array_slice($subList, 0, $assign['ajax_replies_limit']);
}
}
}
$assign['closed'] = @$comments[0][0]['comments_close'];
$assign['comments'] = $comments;
$assign['theme'] = defined('THEME_FOLDER') ? THEME_FOLDER : DEFAULT_THEME_FOLDER;

View File

@@ -36,11 +36,12 @@
{/if}
</div>
{if isset($comments[0])}
<div class="comments-list" id="comments_list">
{include file="$subtpl" subcomments=$comments[0]}
</div>
{/if}
{if isset($comments[0])}
<div class="comments-list" id="comments_list">
{* Добавляем parentId=0, чтобы кнопка знала, к какой группе относятся ответы *}
{include file="$subtpl" subcomments=$comments[0] parentId=0}
</div>
{/if}
<a id="end"></a>
@@ -286,5 +287,46 @@
</script>
<script src="{$ABS_PATH}modules/comment/js/comment.js" type="text/javascript"></script>
{literal}
<script type="text/javascript">
function loadMoreReplies(parentId) {
const loader = document.getElementById('ajax_loader_' + parentId);
if (!loader) return;
const originalContent = loader.innerHTML;
loader.innerHTML = '<div class="spinner-border spinner-border-sm text-primary" role="status"></div> Загрузка...';
const separator = window.location.href.indexOf('?') !== -1 ? '&' : '?';
const url = window.location.href + separator + 'ajax_load_branch=' + parentId;
fetch(url)
.then(response => response.text())
.then(html => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const newBranch = doc.getElementById('comment_wrapper_' + parentId);
const currentBranch = document.getElementById('comment_wrapper_' + parentId);
if (newBranch && currentBranch) {
currentBranch.innerHTML = newBranch.innerHTML;
if (loader) loader.remove();
// Инициализация тултипов Bootstrap после обновления DOM
if (typeof bootstrap !== 'undefined' && bootstrap.Tooltip) {
const tooltipTriggerList = [].slice.call(currentBranch.querySelectorAll('[data-bs-toggle="tooltip"]'));
tooltipTriggerList.map(t => new bootstrap.Tooltip(t));
}
}
})
.catch(err => {
console.error('AJAX Error:', err);
loader.innerHTML = originalContent;
alert('Не удалось подгрузить ответы.');
});
}
</script>
{/literal}
{/if}
{/if}

View File

@@ -1,4 +1,4 @@
{foreach from=$subcomments item=c}
{foreach from=$subcomments item=c name=sub_loop}
<div class="card mb-3 mod_comment_comment{if $c.parent_id} ms-2 ms-md-4{/if}"
id="comment_wrapper_{$c.Id}"
data-parent="{$c.parent_id|default:0}"
@@ -174,11 +174,22 @@
<span id="end{$c.Id}"></span>
{if isset($comments) && isset($comments[$c.Id])}
<div class="mt-2">
{include file="$subtpl" subcomments=$comments[$c.Id] sub=1}
</div>
{/if}
{if isset($comments) && isset($comments[$c.Id])}
<div class="mt-2">
{* ВАЖНО: передаем parentId=$c.Id для следующего уровня вложенности *}
{include file="$subtpl" subcomments=$comments[$c.Id] sub=1 parentId=$c.Id}
</div>
{/if}
</div>
{* --- ВОТ СЮДА ВСТАВЛЯЕМ КНОПКУ --- *}
{if $smarty.foreach.sub_loop.last && isset($more_counts[$parentId])}
<div id="ajax_loader_{$parentId}" class="ms-4 mb-3">
<button class="btn btn-sm btn-outline-primary" onclick="loadMoreReplies('{$parentId}')">
<i class="bi bi-chevron-double-down"></i> Показать еще {$more_counts[$parentId]} ответов
</button>
</div>
{/if}
{/foreach}