How to put all the comments in a collapsible fieldset ?
bumathan - November 10, 2006 - 13:25
Hi !
I wonder how to put all the comments and the "add a comment" link in a collapsible fieldset ?
Could you tell me in wich file do I have to specify that please ?
Thank you .
Matt

i think it's only possible
i think it's only possible with javascript. try adding this code into your theme's page file just before the closing
</body>tag<script type="text/javascript">if (cTop = document.getElementById("comment")) {
var cFieldset = document.createElement("FIELDSET");
var cLegend = document.createElement("LEGEND");
cFieldset.appendChild(cLegend);
var cParent = cTop.parentNode;
var cNext = cTop.nextSibling;
cFieldset.appendChild(cTop);
var cCount = 0;
while (cNext) {
if (cNext.tagName=="DIV") cCount++;
cCopy = cNext;
cNext = cNext.nextSibling;
cFieldset.appendChild(cCopy);
}
cFieldset.className = "collapsible" + ((location.href.indexOf('#comment')==-1) ? " collapsed" : "");
cLegend.innerHTML = "<?php print t('Comments') ?> ("+cCount+")";
cParent.appendChild(cFieldset);
if ("function" != typeof(window.isJsEnabled)) {
document.write("<script type=\"text/javascript\" src=\"<?php print $base_path ?>misc/drupal.js\"><\/script>");
}
if ("function" != typeof(window.collapseAutoAttach)) {
document.write("<script type=\"text/javascript\" src=\"<?php print $base_path ?>misc/collapse.js\"><\/script>");
}
}
</script>
--
Geneticists from METU
Excellent!
thanks ufku!
this should be part of the core... its a very worthwhile option.
i was actually looking for a variation of this. trying to figure out how to add an admin button to every comment, so a moderator could set troll comments to collapsed- so they're not completely deleted, but just minimized so as not to clutter up the discussion. then users could click to a javascript link to unfold the comments as needed.
Interesting idea
Similar to the Digg approach of hiding "dugg down" comments below a chosen threshold minus the democracy. But it might be more appropriate for a smaller site...
Tried this in 5.x site and doesn't work. Any suggestions?
I tried putting this code into the page.tpl.php of a 5.1 site but it doesn't work. Any suggestions?
I'm looking for something similar to work with D5 ...
... but rather than but the whole thread into one collapsible fieldset, I would like to make each comment collapsible ... so initially the comments appear like threaded list - collapsed, but each one can be 'opened' individually in context (not on a new page).
Any pointers welcome.
it works on D5 plus
It works perfectly.
You just have to change the second line from
if (cTop = document.getElementById("comment")) {to
if (cTop = document.getElementById("comments")) {As the CSS id of the comments DIV is now "comments". And it works just fine :)
Count Comments?
By changing comment to comments (as above) this works for me, however the number in brackets does not count correctly, it just shows (0).
I tried using
$count = comment_num_all($node->nid);print $count(from http://drupal.org/node/347726) which works on the single story page, but not for a page with multiple stories. I presume it doesn't know what nid it should be counting??Any ideas?
Many Thanks
Graham
for 6.x
Add the below code to your theme's script.js(create if not exists or somehow insert this code into your pages)
$(document).ready(function() {var $comments = $('#comments');
if (!$comments.size()) return;
var count = $comments.find('.comment').size();
var label = Drupal.formatPlural(count, '1 comment', '@count comments');
var $legend = $(document.createElement('legend')).html(label);
var fsetclass = 'comments collapsible' + (location.href.search(/#(comment|new)/) == -1 ? ' collapsed' : '');
var $fset = $(document.createElement('fieldset')).addClass(fsetclass);
$fset.insertBefore($comments).append($legend).append($comments);
Drupal.behaviors.collapse($fset.parent());
});
--
ufku.com - IMCE - BUEditor
Thanks for your suggested
Thanks for your suggested script, this time it counts the comments correctly but doesn't show the comments when clicked, just appears as text with the lines either side as though it should be a collaspible fieldset.
IE is reporting Webpage error details
Message: Object doesn't support this property or methodLine: 670
Char: 3
Code: 0
Which refers to
Drupal.behaviors.collapse($fset.parent());Maybe the answer lies somewhere inbetween the two. Unfortunately I can't work out exactly what each one is doing to figure it out :(
I've suprised myself and
I've suprised myself and managed to get a combination of the 2 to work. Here's what I ended up with
<script type="text/javascript">if (cTop = document.getElementById("comments")) {
var $comments = $('#comments');
var count = $comments.find('.comment').size();
var cFieldset = document.createElement("FIELDSET");
var cLegend = document.createElement("LEGEND");
cFieldset.appendChild(cLegend);
var cParent = cTop.parentNode;
var cNext = cTop.nextSibling;
cFieldset.appendChild(cTop);
cFieldset.className = "collapsible" + ((location.href.indexOf('#comment')==-1) ? " collapsed" : "");
cLegend.innerHTML = "<?php print t('Comments') ?> ("+count+")";
cParent.appendChild(cFieldset);
if ("function" != typeof(window.isJsEnabled)) {
document.write("<script type=\"text/javascript\" src=\"<?php print $base_path ?>misc/drupal.js\"><\/script>");
}
if ("function" != typeof(window.collapseAutoAttach)) {
document.write("<script type=\"text/javascript\" src=\"<?php print $base_path ?>misc/collapse.js\"><\/script>");
}
}
</script>
This is probably not the most efficient way of doing this but it works on my install!
collapse.js
It requires misc/collapse.js to be included in the page.
You may add it using
<?phpdrupal_add_js('misc/collapse.js');
?>
--
ufku.com - IMCE - BUEditor
Ahh that did it! Much
Ahh that did it! Much appreciated!