MyBB Community Forums

Full Version: How to set default-state of collapse-buttons?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How to set default state of collapse buttons (on homepage categories) to collapsed? (instead of expanded)

[attachment=39856]

i see general.jw contains expander.click function. One could trigger that on page load, for each category, but that seems inefficient. 

i see the saveCollapsed function stores the collapsed state in browser. Can we create that cookie on first visit to force collapsed view? Where's best place for that? Does that function make a separate cookie for every category?

i see global.php contains the setup code, which checks the collapse cookie:
should be a way to mod or just remove this code. 
But, if i'm making a plugin, i should not alter the global.php.
Update: i deleted this code from global.php-- the page did not load in collapsed mode.

More analysis: the comment says "to automatically show them us expanded", but i believe that comment might not be accurate. Rather, what happens here is the code loops through the array of category collapse-states, and applies the user's state from previous visit.

I don't see where a default state is applied, for categories not found, or if the cookie isn't found.

// set up collapsable items (to automatically show them us expanded)
$collapsed = array('boardstats' => '', 'boardstats_e' => '', 'quickreply' => '', 'quickreply_e' => '');
$collapsedimg = $collapsed;
if($colcookie)
{
	$col = explode("|", $colcookie);
	if(!is_array($col))
	{
		$col[0] = $colcookie; // only one item
	}
	unset($collapsed);
	foreach($col as $key => $val)
	{
		$ex = $val."_e";
		$co = $val."_c";
		$collapsed[$co] = "display: show;";
		$collapsed[$ex] = "display: none;";
		$collapsedimg[$val] = "_collapsed";
		$collapsedthead[$val] = " thead_collapsed";
	}
}
I've had to deal with this before. It's kind of surprising that something like this isn't standard. I wrote a plugin to deal with it, but it isn't finished yet. Basically you can use the following code:


    $plugins->add_hook('global_start', 'setCollapsedCookie');   
    $mybb->cookies['collapsed'] = trim(implode('|', $userCollapsed));
    my_setcookie("collapsed", $mybb->cookies['collapsed'];


Where $userCollapsed is an array of the categories you want to collapse. It is all done through the "collapsed" cookie. Hope that helps. If I get around to finishing my plugin that lets you set default states in the AdminCP I will post on Github for all to use.
here is a jQuery based method - add in index template before </body>
<script type="text/javascript">
 jQuery(document).ready(function(){
var x = document.getElementsByClassName("expander");
 for (var i = 0; i < x.length - 1 ; i++) {
 x[i].click();
}
});
</script>
Note: categories expand/collapse status toggles on refreshing the page
Thx everyone for these awesome solutions!

I solved it by editing the Index page Template-- i just removed the forums completely, showing only the categories. Not possible to expand. For now, that's a fine solution for us.
I would mby be looking at this in the future so just replying back to say I am very interested in this.
I use it together with FontAwesome icons for these collapse buttons, great!
This is how I made the auto expander auto-collape by default and stay collapsed on refresh, open and it stays open on refresh.

This is the best method to me, simply because no other method worked for me.

<td class="thead tcat_menu tcat_collapse{$collapsedimg['usercpprofile']}">
<div class="expcolimage"><img src="{$theme['imgdir']}/collapse{$collapsedimg['usercpprofile']}.png" id="usercpprofile_img" class="expander" alt="[-]" title="[-]" /></div>
<div><span class="smalltext"><strong>{$lang->ucp_nav_profile}</strong></span></div>
</td>
Interesting method.

I've only tried setting individual custom tables to a collapsed state manually in templates like this:
<thead>
<tr>
<td class="thead" colspan="5">
<div class="expcolimage"><img src=".../collapse_collapsed.png" id="cat_105_img" class="expander" alt="[+]" title="[+]" data-original-title="[-]" style="cursor: pointer;"></div>
<div><h4 align="center"><a href="forumdisplay.php?fid=5">Test</a></h4><p class="smalltext" align="center">Test</p></div>
</td>
</tr>
</thead>
(2018-01-13, 03:50 AM)primesoftware Wrote: [ -> ]I've had to deal with this before. It's kind of surprising that something like this isn't standard. I wrote a plugin to deal with it, but it isn't finished yet. Basically you can use the following code:


    $plugins->add_hook('global_start', 'setCollapsedCookie');   
    $mybb->cookies['collapsed'] = trim(implode('|', $userCollapsed));
    my_setcookie("collapsed", $mybb->cookies['collapsed'];


Where $userCollapsed is an array of the categories you want to collapse. It is all done through the "collapsed" cookie. Hope that helps. If I get around to finishing my plugin that lets you set default states in the AdminCP I will post on Github for all to use.

Ran into this issue on my forum and this post inspired me to go ahead and make a plugin for this. Sharing it here incase someone else falls down this rabbit hole. 

https://github.com/Rhababo/myBB_auto_col...ategories/