title: $:/core/macros/tree
tags: $:/tags/Macro

\define leaf-node(prefix)
<li>
<$list filter="""[[$prefix$$(chunk)$]is[shadow]] [[$prefix$$(chunk)$]is[tiddler]] +[removeprefix[$prefix$]] +[limit[1]]""" 
emptyMessage="""<$text text="$prefix$$(chunk)$"/>""">
<span>{{$:/core/images/file}}</span> <$link to="""$prefix$$(chunk)$""">
<$view field="title"/>
</$link> 
</$list>
</li>
\end

\define branch-node(prefix)
<$reveal type="nomatch" text="" default="""$(chunk)$""">
<li>      
<$list filter="""[all[shadows+tiddlers]prefix[$prefix$$(chunk)$]] -[[$prefix$$(chunk)$]] +[limit[1]]""">
<$reveal type="nomatch" state="""$:/state/tree/$prefix$$(chunk)$""" text="show">
<$button set="""$:/state/tree/$prefix$$(chunk)$""" setTo="show" class="tc-btn-invisible">{{$:/core/images/folder}} <$text text="""$(chunk)$"""/></$button>
</$reveal>
<$reveal type="match" state="""$:/state/tree/$prefix$$(chunk)$""" text="show">
<$button set="""$:/state/tree/$prefix$$(chunk)$""" setTo="hide" class="tc-btn-invisible">{{$:/core/images/folder}} <$text text="""$(chunk)$"""/></$button>
</$reveal>
</$list>
<$list filter="""[all[shadows+tiddlers]prefix[$prefix$$(chunk)$]] -[[$prefix$$(chunk)$]] +[limit[1]]"""><span>(<$count filter="""[all[shadows+tiddlers]prefix[$prefix$$(chunk)$]] -[[$prefix$$(chunk)$]]"""/>)</span>
<$reveal type="match" state="""$:/state/tree/$prefix$$(chunk)$""" text="show">
<$macrocall $name="tree-node" prefix="""$prefix$$(chunk)$"""/>
</$reveal>
</$list>
</li>
</$reveal>
\end

\define tree-node(prefix)
<ol>
<$list filter="[all[shadows+tiddlers]removeprefix[$prefix$]splitbefore[/]sort[title]] +[!suffix[/]]" variable="chunk">
<<leaf-node """$prefix$""">>
</$list>
<$list filter="[all[shadows+tiddlers]removeprefix[$prefix$]splitbefore[/]sort[title]] +[suffix[/]]" variable="chunk">
<<branch-node """$prefix$""">>
</$list>
</ol>
\end

\define tree(prefix)
<div class="tc-tree">
<span><$text text="""$prefix$"""/></span>
<div>
<$macrocall $name="tree-node" prefix="""$prefix$"""/>
</div>
</div>
\end
