VERSION 1.0 CLASS
BEGIN
  MultiUse = -1    'True
END
Attribute VB_PredeclaredId = False
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Attribute VB_GlobalNameSpace = True
Attribute VB_Name = "DynamicOutlineBuilder"
'#Reference {A3B5C45B-F842-44fc-A5E5-50CED69BA86E}#2.0#0#C:\Program Files\Mindjet\MindManager 6\Mm5WebExport.dll#Mm5webexport 2.0 Type Library
'#Uses "GlobalParameterNames.mmbas"
'#Uses "GlobalUtilities.mmbas"
'#Uses "TOCHandler.cls"
Option Explicit

Dim addin As Mm5WebExportAddIn
Dim webex As WebExport

Dim CurrentLevel As Long
Dim LastLevel As Variant
Dim TreeItems As String
Dim oneTOCHandler As TOCHandler

Private Sub Class_Initialize()
    'Debug.Print "Class_Initialize"
End Sub

Private Sub Class_Terminate()
    'Debug.Print "Class_Terminate"
End Sub

Public Sub BuildDynamicOutline(oneTopic As Topic, NavigationDepth As Long )

   ' get these from localized parameters
   Dim HomeLabel As String
   Dim HomeLink As String

   Set addin = WebExportHelper.WebExportAddIn
   Set webex = addin.ActiveWebExport
   Set oneTOCHandler = New TOCHandler

   HomeLabel = webex.GetParameter("Strings.Navigation.Home")
   HomeLink = webex.GetParameter(WeRootExportFilenameResolved)
   HomeLink = "'" + HomeLink + "'"

   ' Initialize tree, traverse map to generate items and then close tree
   TreeItems = "var TREE_ITEMS = [" + vbCrLf + vbTab + "['" + HomeLabel + "'," + HomeLink + ",{'sb':' '},"
   Call TraverseMap(oneTopic, NavigationDepth)
   TreeItems = TreeItems + vbCrLf + "]" + vbCrLf + "];"

   ' Store our finished outline items string as a parameter
   webex.SetParameter("CustomParameter.DynamicOutlineString", TreeItems )

End Sub

Private Function TraverseMap(ByVal oneTopic As Topic, ByRef NavigationDepth As Long)

    Dim nextTopic As Topic
    Dim i, j, k  As Long
    Dim TopicCount As Long
    Dim Tabs As String
    Dim Tabs2 As String
    Dim theNonEmptyTopicText As String
    Dim TopicTextTruncated As String
    Dim TopicText As String
    Dim TopicLink As String
    Dim TopicTarget As String
    Dim s_Numbering As String ' topic numbering
    Dim fileName As String
    Dim fileBookmark As String
    Dim url As String

    TopicCount = oneTopic.SubTopics(False).Count

    For i = 1 To TopicCount

        Set nextTopic = oneTopic.SubTopics(False).Item(i)
        CurrentLevel = CurrentLevel + 1

        Tabs = ""
        For j = 1 To nextTopic.Level + 1
            Tabs = Tabs + vbTab
        Next j

        ' Check if a new line already has been inserted in tree string
        If Right(TreeItems,2) <> vbCrLf Then
           TreeItems = TreeItems + vbCrLf
        End If

        ' Make sure this is reset each time before we possibly set it below
        TopicTarget = ""

        ' If this is a hyperlink-only topic
        if webex.IsHyperlinkOnlyTopic(nextTopic) Then
            ' use the topic's hyperlink
            Dim text As String
            webex.UrlFromHyperlink(nextTopic.Hyperlink, url, text, TopicTarget)
        Else
            ' Link to the topic page (without bookmark to avoid "ugly" scrolling)
            webex.FilenameAndBookmarkFromTopic(nextTopic, fileName, fileBookmark)
            url = webex.PathToUrl(fileName)
        End If
            
        theNonEmptyTopicText = NonEmptyTopicText(webex, nextTopic)
        s_Numbering = oneTOCHandler.GetOutlineNumberForTopic(nextTopic, webex.GetParameter("Strings.Show.NumberToLevel"))
        TopicTextTruncated = CleanString(s_Numbering & theNonEmptyTopicText, webex, True)
        TopicTextTruncated = VerifyText(webex,TopicTextTruncated)
        TopicText = CleanString(s_Numbering & theNonEmptyTopicText, webex, False)
        TopicText = VerifyText(webex, TopicText)
        TopicLink = CleanFileName(url)

        ' Outline item entry
        ' (tt: = tooltip, sb: = status bar prompt, tw: = target window (frame)
        TreeItems =   TreeItems _
                    + Tabs _
                    + "['" + TopicTextTruncated + "', '" + TopicLink + "'," _
                    + "{" _
                    + "'tt':'" + TopicText + "'," _
                    + "'sb':'" + TopicLink + "'" 
        ' add our target window (frame) if we have one
        If Len(TopicTarget) > 0 Then
            TreeItems = TreeItems + ",'tw':'" + TopicTarget + "'"
        End If
        ' terminate the entry
        TreeItems = TreeItems + "},"

        ' Recursive call
        If nextTopic.Level <  NavigationDepth Then
            Call TraverseMap(nextTopic, NavigationDepth)
        End If

        CurrentLevel = CurrentLevel - 1

        ' Close opened sub level
        TreeItems = TreeItems + "]," + vbCrLf 

    Next i

End Function
