Use JavaScript to collapse long comments

Instead of using CSS to limit the height of package comments as
implemented in 7b13203 (Limit comment height to 15 lines, 2016-03-12),
use JavaScript to collapse long comments and add a link to expand them.
Clicking the same link twice results in the corresponding comment being
collapsed again.

If JavaScript is disabled, the full comments are shown (without any
possibility to collapse or expand).

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
This commit is contained in:
Lukas Fleischer 2017-04-19 17:21:15 +02:00
parent 44858e0618
commit 4abde895a5
3 changed files with 36 additions and 6 deletions

View file

@ -148,8 +148,3 @@ label.confirmation,
color: red; color: red;
font-weight: bold; font-weight: bold;
} }
#news div p {
max-height: 15em;
overflow: auto;
}

View file

@ -72,10 +72,45 @@ function collapseDependsList(list) {
}); });
} }
function collapseComment(div) {
var linkid = div.attr('id') + 'link',
par = div.find('p'),
height = par.height(),
maxheight = 200;
if (height <= maxheight)
return;
par.css({ 'overflow': 'hidden', 'height': maxheight + 'px' });
par.addClass('collapsed');
par.after('<p><a id="' + linkid + '" href="#">Show More…</a></p>');
$('#' + linkid).click(function(event) {
var newheight;
if (par.hasClass('collapsed')) {
par.css({ 'height': 'auto' });
newheight = par.height();
par.css({ 'height': maxheight });
$(this).text('Collapse');
} else {
newheight = maxheight;
$(this).text('Show More…');
}
par.animate({ 'height': newheight });
par.toggleClass('collapsed');
event.preventDefault();
});
}
$(document).ready(function() { $(document).ready(function() {
collapseDependsList("#pkgdepslist"); collapseDependsList("#pkgdepslist");
collapseDependsList("#pkgreqslist"); collapseDependsList("#pkgreqslist");
collapseDependsList("#pkgsrcslist"); collapseDependsList("#pkgsrcslist");
$(".article-content").each(function() {
collapseComment($(this));
});
}); });
</script> </script>

View file

@ -102,7 +102,7 @@ if (!isset($count)) {
</form> </form>
<?php endif; ?> <?php endif; ?>
</h4> </h4>
<div class="article-content<?php if ($is_deleted): ?> comment-deleted<?php endif; ?>"> <div id="<?= isset($pinned) ? "pinned-" : "comment-" ?><?= $row['ID'] ?>-content" class="article-content<?php if ($is_deleted): ?> comment-deleted<?php endif; ?>">
<p> <p>
<?= parse_comment($row['Comments']) ?> <?= parse_comment($row['Comments']) ?>
</p> </p>