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 = "TOCHandler"
'#Reference {A3B5C45B-F842-44fc-A5E5-50CED69BA86E}#2.0#0#C:\Program Files\Mindjet\MindManager 6\Mm5WebExport.dll#Mm5webexport 2.0 Type Library
Option Explicit

Dim addin As Mm5WebExportAddIn
Dim webex As WebExport

Dim TOCList() As Variant
Dim LastLevel As Long
Dim NumberOfTopicsInMap As Long
Dim GlobalTopicCount As Long
Dim CurrentOutlineNumber As String

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

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

' Builds outline numbers for the whole map
Public Sub BuildOutlineNumberList(oneTopic As Topic)
Dim i_ptr As Integer

   Dim oneDoc As Document
   Dim oneRange As Range
   Dim ToLocate As String
   Dim SearchItem As String

   Set addin = WebExportHelper.WebExportAddIn
   Set webex = addin.ActiveWebExport

   ' Get the central topic object
   Set oneDoc = oneTopic.Document

   ' Outline level counter initialization
   'LastLevel = 0
   'GlobalTopicCount = 0

   ' Dimension the array according to number of topics in map
   NumberOfTopicsInMap = oneDoc.Range(mmRangeAllTopics,False).Count
   ReDim TOCList(NumberOfTopicsInMap, 2)

   ' Recursive map traversal
   'Call TraverseMap(oneTopic)
   i_ptr = 1 ' start at beginning of array
   Call p_TraverseMap(oneTopic, "", 0, i_ptr)

   ' Persist array with outline numbers
   webex.SetParameter("CustomParameter.TOCOutlineNumbers", TOCList() )
   webex.SetParameter("CustomParameter.TOCArrayDimension", NumberOfTopicsInMap )

End Sub

Public Function GetOutlineNumberForTopic(ByVal oneTopic As Topic, ByVal i_MaxLevel As Integer) As String
' Takes a topic and looks up its outline number. BuildOutline Number must have been
' called prior to this to ensure there is an array with outline numbers persisted
' as "CustomParameter.TOCOutlineNumbers"
Dim TMPTOCList As Variant
Dim TMPTOCArrayDimension As Variant
Dim s_1 As String
Dim i_1 As Long
Dim IDToLocate As String

	' Assume we won't find the GUID ID we are looking for
	GetOutlineNumberForTopic = ""

	If oneTopic.Level > i_MaxLevel Then Exit Function ' deeper than max level, quit

	' Connect to the current web export
    Set addin = WebExportHelper.WebExportAddIn
    Set webex = addin.ActiveWebExport

   ' Retrieve array
    TMPTOCArrayDimension = webex.GetParameter("CustomParameter.TOCArrayDimension")
    ReDim TMPTOCList(TMPTOCArrayDimension,2)
    TMPTOCList = webex.GetParameter("CustomParameter.TOCOutlineNumbers")

	' Look for the GUID...
    IDToLocate = oneTopic.Guid

	' Iterate over elements in array
    For i_1 = 1 To TMPTOCArrayDimension
        If TMPTOCList(i_1, 1) = IDToLocate Then
           ' Return TOC number for this guid topic ID
           s_1 = TMPTOCList(i_1, 2)
           If Len(s_1) > 0 Then s_1 = s_1 & " " ' append space if not blank
           GetOutlineNumberForTopic = s_1
        End If
	Next i_1

End Function

Private Sub p_TraverseMap(ByVal m_wkgTopic As Topic, ByVal s_Prefix As String, ByVal i_Level As Integer, ByRef i_Ptr As Integer)
' Traverse the map from the central topic downwards and write entries in the TOCList
' We only use unfiltered topics for this
Dim i_1 As Integer
Dim s_1 As String

	' Write number and GUID for this topic
	TOCList(i_Ptr, 1) = CStr(m_wkgTopic.Guid)
	s_1 = s_Prefix ' special handling for Main Topics
	If i_Level = 1 Then s_1 = s_1 & "." ' extra dot
	TOCList(i_Ptr, 2) = s_1
	i_Ptr = i_Ptr + 1 ' next one

	' Go down a level
	If Len(s_Prefix) > 0 Then s_Prefix = s_Prefix & "."
	For i_1 = 1 To m_wkgTopic.SubTopics(False).Count
		Call p_TraverseMap(m_wkgTopic.SubTopics(False).Item(i_1), s_Prefix & CStr(i_1), i_Level + 1, i_Ptr)
	Next i_1

End Sub

