cogDisplays a table of content spanning a page and its children
TypeSnippet
Category
Developed by

Ludovic Dubost

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

This code written in XWiki Syntax 1.0 allows to display a Table of Content allowing to display a tree of pages in your wiki. It will display the tree from a top page a certain number of levels down (3 by default) and will assign the currently selected page in your tree (if it is present in the tree) with a different CSS class. This code is best used in a panel. Some part of this code will be later moved to a plugin. 

## the page on which we are currently
#if($request.selectedpage)
#set($selectedpage = $request.selectedpage)
#else
#set($selectedpage = $doc.fullName)
#end
## the page at the top of the tree, provided externally
## in can be for instance provided by the breadcrumb code
#if($request.toppage)
#set($toppage = $request.toppage)
#elseif(!$toppage)
#set($toppage = $doc.fullName)
#end
## showtocelement macro
#macro(showtocelement $pagedoc $selectedpage $withchilds $breadcrumb)
#if(!$pagedoc.isNew())
#set($counter = $counter + 1)
<li id="toc_text_wrapper_${counter}" #if($pagedoc.fullName==$selectedpage) class="selected #if(!$withchilds) nochilds #end"#end>
<span id="toc_showhide_${counter}"  class="viewhide" onclick="$('toc_folder_${counter}').toggleClassName('hidden'); $('toc_showhide_${counter}').toggleClassName('expanded')">+/-</span><span id="toc_text_${counter}" #if($pagedoc.fullName==$selectedpage) class="selected" #end>
#if($pagedoc.isNew())$pagedoc.fullName#else[$pagedoc.displayTitle>${pagedoc.fullName}?bc=${breadcrumb}]#end</span>
#end
## showtoc macro
#macro(showtoc $page $selectedpage $toppage $level $maxlevel $breadcrumb)
#set($pagedoc = $xwiki.getDocument($page))
#set($links = $pagedoc.getLinkedPages())
#if($links && $links.size()>0)
#set($withchilds = true)
#else
#set($withchilds = false)
#end
#showtocelement($pagedoc $selectedpage $withchilds $breadcrumb)
#if(($level < $maxlevel || $bcDocumentList.contains($page) || $bcDocumentList.contains("xwiki:${page}"))&& $withchilds)
<ul id="toc_folder_${counter}"  >
 #set($ok = $bclist.add($page))
 #foreach($link in $links)
 #if(($level > 0 || $page == $selectedpage || (!$bcDocumentList.contains($page) && !$bcDocumentList.contains("xwiki:${page}"))) && $page != $toppage)
   #set($level = $level + 1)
  #end
  #set($child = $link)
  #if($child.indexOf(".")==-1)
   #set($child = "${pagedoc.web}.${child}")
  #end
  #set($breadcrumb = "")
  #foreach($bcpage in $bclist)
   #if($breadcrumb=="")
    #set($breadcrumb = $bcpage)
   #else
    #set($breadcrumb = "${breadcrumb};${bcpage}")
   #end
  #end
  #showtoc($child $selectedpage $toppage $level $maxlevel $breadcrumb)
  #if($level != 0)
   #set($level = $level - 1)
  #end
 #end ## end foreach
 #set($i = $bclist.size())
 #set($i = $i - 1)
 #if($i >= 0)
  #set($ok = $bclist.remove($i))
 #end
</ul>
#end
</li>
 #end
#end ## end macro
## displaying the toc
#set($counter = 0)
#set($bclist = $xwiki.arrayList)
<ul id="toc">
#showtoc($toppage $selectedpage $toppage 0 1 $toppage)
</ul>
     

Get Connected