Home Forums General XMetaL Discussion Structure view: how to set the initial collapse level? Reply To: Structure view: how to set the initial collapse level?

Derek Read

Reply to: Structure view: how to set the initial collapse level?

Anyone that wants to mess around with a scripting solution could start with the following as a base to work from.

It works at least as far as I understand the original request but I haven't done that much testing.

Also, how useful it is I don't really know. I think the best solution for most people would be to create CSS for the Structure View that gives you exactly what you want to see, including hiding anything you don't want to see. That way you don't need to mess around with collapsing anything. I think that assumes the primary purpose of the Structure View for you is navigation. See my previous response for info on that.

To use this, create an MCR file (choose whatever name you like) containing the following and place it in the Startup subfolder. As written, it should run for all document types. Remove line 3 in an end user environment. That's just there for testing.

[code=structureViewDefaultCollapse.mcr]

//change the following line to the “collapse level” you want
var level = 3;
Application.Alert(“collapsing structure view at level ” + level); //hopefully you don't need to change anything from here on down
var origSelection = ActiveDocument.Range;
var ancestors = level – 1;
var xpath = “//*[count(ancestor::*) = ” + ancestors + “]”;
var nodes = ActiveDocument.documentElement.getNodesByXPath(xpath);
ActiveDocument.FormattingUpdating = false;
for(i=0;i Selection.SelectNodeContents(nodes.item(i));
Selection.SVCollapsedContainer = true;
}
ActiveDocument.FormattingUpdating = true;
origSelection.Select();
]]>

[/code]

The SVCollapsedContainer API is kind of finicky and (unlike almost all other APIs) doesn't work the same with Selection and Range objects. That's why I ended up using Selection here and not Range. Since moving the Selection around is visible I have also toggled formatting off and on.

Storing the Range in origSelection and restoring it is probably not necessary in this event but it probably would be for other events or in a user-initiated macro.

Reply

Products
Downloads
Support