'#Reference {A3B5C45B-F842-44fc-A5E5-50CED69BA86E}#2.0#0#C:\Program Files\Mindjet\MindManager 6\Mm5WebExport.dll#Mm5webexport 2.0 Type Library
' Imgmap.mmbas
' Generate a client side image map and image file from the current map
' The map is rendered at 100%, with all the objects as currently in view
' To hide objects from the output, either hide them in the view menu, filter them or set up the Level of Detail on individual topics

' TODO: handle linked documents correctly

'#Uses "ParameterNames.mmbas"
'#Uses "GlobalUtilities.mmbas"

Option Explicit

Sub Main

    Dim m_Addin As Mm5WebExportAddIn
    Dim m_Webex As WebExport
    Dim m_Page As Page
    Dim m_Doc As Document
    Dim m_Topic As Topic
    Dim m_GraphicExport As GraphicExport
    Dim s_ImageFile As String ' image filename
    Dim s_FullImageFile As String
    Dim s_DataFile As String ' filename for raw data generated separately
    Dim s_CSIMname As String ' Client side image map name and data block
    Dim s_CSIMblock As String
    Dim s_ImageMapData As String
    Dim s_Extra As String
    Dim theURL As String
    Dim theText As String
    Dim theTarget As String
    Dim b_MapBackground As Boolean  ' include map background in the exported image, transparent otherwise
    Dim i_ZoomFactor As Double ' display zoom factor

    Set m_Addin = WebExportHelper.WebExportAddIn
    Set m_Webex = m_Addin.ActiveWebExport

    On Error GoTo ReportError

    m_Webex.Trace("Entering Imgmap.mmbas", 2 )
    Set m_Page = m_Webex.ActivePage
    Set m_Doc = m_Webex.Document

    ' Create the image file
    ' Generate custom image file
    b_MapBackground = m_Webex.GetParameter("Strings.Miscellaneous.MapBackground") ' read user setting
    ' choose jpg to include the map background, gif for transparent
    s_ImageFile = f_cropExtension(m_Webex.ActivePage.Filename) & IIf(b_MapBackground, ".jpg", ".gif")
    s_FullImageFile = m_Webex.FullExportFilename(s_ImageFile) ' generate full path version for writing out
    m_Webex.SetParameter("CustomParameter.ImageFile", s_ImageFile) ' set placeholder

    ' Create image file
    Set m_GraphicExport = m_Doc.GraphicExport ' get graphic export object
    ' Add all regular topics as currently viewed
    m_GraphicExport.AddTopic(m_Doc.CentralTopic) ' will bring in Callout Topics, Floating Topics and Relationships

    ' Add original hyperlinks to the imagefile
    For Each m_Topic In m_Doc.Range(mmRangeAllTopics, False) ' includes callout and floating topics
        If m_Topic.HasHyperlink Then
        
     	    ' Reset values before call to UrlFromHyperlink
            theText = ""
            theTarget = ""
                    
            ' get the resolved hyperlink
            m_Webex.UrlFromHyperlink(m_Topic.Hyperlink, theURL, theText, theTarget)
            
            ' if it's a valid hyperlink
            If Len(theURL) > 0 Then

                ' use the "extra" parameter for our hyperlink target (if any)
                If Len(theTarget) > 0 Then 
                    s_Extra = " target=""" & theTarget & """ "
                Else
                    s_Extra = ""
                End If
                
                ' Add hyperlink to both the Text and the Hyperlink icon
                m_GraphicExport.AddLink(m_Topic, mmLinkTypeHyperlink, theUrl, theText, s_Extra)
                m_GraphicExport.AddLink(m_Topic, mmLinkTypeTopic, theUrl, theText, s_Extra)
            End If
            
        End If
    Next

    i_ZoomFactor = m_Webex.GetParameter("Strings.Show.ZoomFactor") / 100 ' read zoom factor, convert from %
    m_GraphicExport.ExportZoomed(s_FullImageFile, IIf(b_MapBackground, mmGraphicTypeJpeg, mmGraphicTypeGif), i_ZoomFactor, True, 0, 0)

    ' Now create Client Side Imagemap
    s_CSIMname = f_GuidToName(m_Doc.Guid) ' create a unique CSIM name in case more than one map is used in a page
    s_CSIMblock = m_GraphicExport.GetImageMap(s_CSIMname)

    ' Set up placeholders
    m_Webex.SetParameter("CustomParameter.Imagemap.Name", s_CSIMname)
    m_Webex.SetParameter("CustomParameter.Imagemap.Block", s_CSIMblock)
    Set m_GraphicExport = Nothing ' Release object

    ' Process the imagemap raw file
    s_ImageMapData = m_Webex.ReadFile("Imgmap.htm")

    ' Process all commands and parameters
    ProcessString m_Webex, s_ImageMapData

    ' Write the imagemap code to its own .txt file
    s_DataFile = m_Webex.ActivePage.Filename & ".txt" ' retain the .html as it helps identify content
    m_Webex.WriteFile(s_DataFile, s_ImageMapData) ' write page back to the map file for this topic

    ' Write out the result to the main page
    m_Webex.PushString(s_ImageMapData)

    m_Webex.Trace("Exiting Imgmap.mmbas",2)

    Exit Sub

ReportError:
    ProcessError(m_Webex)

End Sub

Function f_GuidToName(ByVal s_Guid) As String
    ' Convert a Base-64 GUID to an object name usable in a web page
    Dim s_1 As String

    s_1 = Replace(s_Guid, "=", "") ' strip "="
    s_1 = Replace(s_1, "/", "") ' strip "/"
    f_GuidToName = Replace(s_1, "+", "") ' strip "+" and return result
End Function

Function f_cropExtension(ByVal s_Filepath As String) As String
    ' Crop the dot and filename extension off a path + filename
    ' It can then be used as a foldername or can have another extension added
    Dim i_1 As Long

    f_cropExtension = s_Filepath ' default unchanged
    i_1 = InStrRev(s_Filepath, ".") ' look for last . character
    If IsNull(i_1) Then Exit Function ' not found
    If i_1 < 1 Then Exit Function ' not found
    f_cropExtension = Left(s_Filepath, i_1 - 1)

End Function
