Skip to content

Commit

Permalink
Merge pull request #14549 from jdalsem/5.1bugfix
Browse files Browse the repository at this point in the history
fix(comments): only load comment form when needed
  • Loading branch information
jeabakker committed Jan 11, 2024
2 parents 8ce4327 + 1eaa55b commit 2848c0a
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
1 change: 1 addition & 0 deletions engine/classes/Elgg/Application/SystemEventHandlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static function init() {
elgg_register_ajax_view('admin/users/listing/details');
elgg_register_ajax_view('core/ajax/edit_comment');
elgg_register_ajax_view('forms/admin/user/change_email');
elgg_register_ajax_view('forms/comment/save');
elgg_register_ajax_view('navigation/menu/user_hover/contents');
elgg_register_ajax_view('notifications/subscriptions/details');
elgg_register_ajax_view('object/plugin/details');
Expand Down
23 changes: 23 additions & 0 deletions views/default/elgg/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@ define(['jquery', 'elgg'], function ($, elgg) {
}
});

$(document).on('click', '.elgg-toggle-comment', function (event) {
var $anchor = $(this);
var comment_guid = $anchor.data().loadComment;

require(['elgg/Ajax', 'elgg/toggle'], function(Ajax) {
var ajax = new Ajax();

ajax.form('comment/save', {
data: {
guid: comment_guid
},
success: function(result) {
$('div[data-comments-placeholder=' + comment_guid + ']')
.html(result)
.slideToggle('medium')
.find('textarea, [contenteditable]').filter(':visible').first().focus();

$anchor.toggleClass('elgg-toggle elgg-toggle-comment');
}
});
});
});

$(document).on('submit', '.elgg-form-comment-save', function (event) {
var $form = $(this);

Expand Down
5 changes: 4 additions & 1 deletion views/default/object/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,13 @@
]);

if (elgg_extract('show_add_form', $vars, true) && $comment->canComment()) {
elgg_require_js('elgg/comments');

$body .= elgg_view('output/url', [
'text' => elgg_echo('generic_comments:add'),
'href' => "#elgg-form-comment-save-{$comment->guid}",
'class' => ['elgg-toggle', 'elgg-subtext'],
'data-load-comment' => $comment->guid,
'class' => ['elgg-subtext', 'elgg-toggle-comment'],
]);
}
}
Expand Down
47 changes: 26 additions & 21 deletions views/default/page/elements/comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,36 @@ function(QueryBuilder $qb) use ($show_guid, $latest_first) {
$show_login_form = elgg_extract('show_login_form', $vars, $show_login_form);

if ($show_add_form && $entity->canComment()) {
$form_vars = [
'id' => "elgg-form-comment-save-{$entity->guid}",
'prevent_double_submit' => false,
];
if ($entity instanceof \ElggComment) {
$form_vars['class'] = 'hidden';
}

if (!$entity instanceof \ElggComment && $latest_first && $comments_list && elgg_get_config('comment_box_collapses')) {
$form_vars['class'] = 'hidden';
$form = elgg_format_element('div', [
'id' => "elgg-form-comment-save-{$entity->guid}",
'data-comments-placeholder' => $entity->guid,
'class' => 'hidden',
]);
} else {
$form_vars = [
'id' => "elgg-form-comment-save-{$entity->guid}",
'prevent_double_submit' => false,
];

$module_vars['menu'] = elgg_view_menu('comments', [
'items' => [
[
'name' => 'add',
'text' => elgg_echo('generic_comments:add'),
'href' => '#' . $form_vars['id'],
'icon' => 'plus',
'class' => ['elgg-button', 'elgg-button-action', 'elgg-toggle'],
if (!$entity instanceof \ElggComment && $latest_first && $comments_list && elgg_get_config('comment_box_collapses')) {
$form_vars['class'] = 'hidden';

$module_vars['menu'] = elgg_view_menu('comments', [
'items' => [
[
'name' => 'add',
'text' => elgg_echo('generic_comments:add'),
'href' => '#' . $form_vars['id'],
'icon' => 'plus',
'class' => ['elgg-button', 'elgg-button-action', 'elgg-toggle'],
],
],
],
]);
]);
}

$form = elgg_view_form('comment/save', $form_vars, $vars);
}

$form = elgg_view_form('comment/save', $form_vars, $vars);
} elseif (!elgg_is_logged_in() && $show_login_form) {
$login_form_contents = elgg_view_form('login', [], ['returntoreferer' => true]);

Expand Down

0 comments on commit 2848c0a

Please sign in to comment.