How is the open/close status of table of contents saved?

The various table of contents macros must be saving their open/folded status somewhere. It’s lazy of me not to reverse engineer the code, but does anyone know off-hand how it’s saved/stored?

Thanks!

1 Like

The TableOfContents (“TOC”) open/folded status is stored in tiddlers with a prefix of $:/state/toc.

When you open a branch of the TOC, the corresponding $:/state/toc/... titles are constructed by appending the tag for each TOC level, separated by a “/”, except for the last (deepest) level, which is separated by a “-”, followed by two “-” and a <<qualify>> value generated by the TWCore. The contents of each $:/state/toc/... tiddler is “open” or “close”

To see this in action, go to http://TiddlyWiki.com and put the following into a tiddler:

<$list filter="[prefix[$:/state/toc]]">
<$link/> = {{!!text}}<br>
</$list>

Then, from the sidebar “Contents” tab, open the TOC branches for “Working with TiddlyWiki”, “Saving”, “TiddlyFox”.

The output from the code above should be something like this:

$:/state/toc/TableOfContents-Working with TiddlyWiki--118161433
$:/state/toc/TableOfContents/Working with TiddlyWiki-Saving--118161433
$:/state/toc/TableOfContents/Working with TiddlyWiki/Saving-Saving with TiddlyFox--118161433

enjoy,
-e

3 Likes

Thanks for the explainer, Eric. So if I wanted to control the open/close status outside of manually hitting the keys, I would need to somehow obtain the <<qualify>> code which makes everything unique. Other than hacking the macro code, is there any easy way to get that?

Thanks!

For some use cases, you won’t need to know the <<qualify>> code. Instead, you can just reference the desired state tiddlers by using a prefix filter.

For example, the following will close all previously opened branches for the TableOfContents:

<$button> close all
<$list filter="[prefix[$:/state/toc/TableOfContents]]">
<$action-setfield text="close"/>
</$list>
</$button>

Opening a specific branch is similar but only works for branches had been previously opened or closed (i.e., for which a state tiddler already exists).

<$button> open "saving"
<$list filter="[prefix[$:/state/toc/TableOfContents/Working with TiddlyWiki/Saving]]">
<$action-setfield text="open"/>
</$list>
</$button>

If you want to open a branch that wasn’t previously open, it gets a bit more complicated, because then you do need to know the <<qualify>> number. However, as long as at least one state tiddler already exists for that TOC, you can get the number from that state tiddler, like this:
<$vars qnum={{{ [prefix[$:/state/toc/TableOfContents]limit[1]split[--]last[]] }}}>
Then you can construct the desired state tiddler title and use addsuffix[--]addsuffix<qnum> to append the qualify number to the title.

Hope this makes sense…

-e