<%
' ********************************************************************
' Copyright (C) 2001 Microsoft Corporation. All rights reserved.
'
'	IMPORTANT.  Please read the legal.txt file, located in the
'	"<CMS Install Directory>\Server\httpexec\WBC\Internals"
'	directory, governing the use of this internal file."
' ********************************************************************

' ********************************************************************
' File:		/NR/System/WBC/Internals/ServerControls/CNCTreeControl.inc
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	For displaying expandable/collapsable tree hierarchy in
'			HTML 3.2 browser
'
' Public Interface of CNCTreeControl
'	- Sub InitTreeControl( strTreeID, pNCTreeDom, pNCTreeRenderer, bFreshTree )
'	- Sub PropagateParameter( strName, strValue )
'	- Sub DisplayTree()
'	- Property DisplayLeafs [read/write]
'	- Property IndentWidth [read/write]
'	- Property NodeSpacing [read/write]
'	- Property NodePadding [read/write]
'	- Property TreeControlPath [read/write]
'	- Property EnableJavaScriptHooks [read/write]
'
' INCTreeDom Callback Interface of CNCTreeControl
'	- Function GetRootObject()
'	- Function GetObjectByID( strID )
'	- Function GetObjectID( pObject )
'	- Function GetObjectDisplayName( pObject )
'	- Function ShouldRecurseContainer( pChild )
'	- Function GetChildrenContainerCol( pObject )
'	- Function GetChildrenLeafCol( pObject )
'
' INCTreeRenderer Callback Interface of CNCTreeControl
'	- Sub RenderContainerNode( pObject, pNCTreeDom, bExpanded )
'	- Sub RenderLeafNode( pObject, pNCTreeDom )
' ********************************************************************

' -- Error Strings
Const IDS_NCTREE_ERR_NCTREEDOM = "<div><b>NCTreeControl Error:</b> NCTreeDom Object not properly initialized.</div>"
Const IDS_NCTREE_ERR_RENDERER = "<div><b>NCTreeControl Error:</b> NCTreeRenderer Object not properly initialized.</div>"
Const IDS_NCTREE_ERR_ROUNDTRIP = "<div><b>NCTreeControl Error:</b> Round Trip URL not properly initialized.</div>"
Const IDS_NCTREE_ERR_NOINIT = "<div><b>NCTreeControl Error:</b> InitTreeControl() has not been called before calling DisplayTree().</div>"

' -- QueryString parameter (managed internally by this module)
Const IDS_NCTREE_QSTR_EXPAND = "NCTreeExpand_"
Const IDS_NCTREE_QSTR_COLLAPSE = "NCTreeCollapse_"
Const IDS_NCTREE_COOKIE_EXPANDED = "NCTreenodeExpanded_"

Const IDS_NCTREE_DEFAULT_PATH = "/NR/System/WBC/Internals/ServerControls/TreeControl"

Class CNCTreeControl
	' -- Property to configure UI layout and display
	Public DisplayLeafs
	Public IndentWidth
	Public NodeSpacing
	Public NodePadding
	Public TreeControlPath
	' -- affects whether Javascript hook functions are called when there
	'	 is a click event on the plus or minus sign on the tree
	Public EnableJavaScriptHooks

	' -- Module internal state management
	Private m_bInitCalled
	' -- Mandatory parameters passed in by cleint for controlling the Tree Display
	Private m_strTreeID, m_pNCTreeDom, m_pNCTreeRenderer, m_bFreshTree

	' -- Variables to identify the QParam names for the Expand/Collapse command
	Private m_strQParamNameExpand, m_strQParamNameCollapse
	' -- For keeping track of node to be Expanded/Collapsed
	Private m_strExpandNode, m_strCollapseNode
	' -- Variables to identify the Cookie name for the nodes that are expanded
	Private m_strCookieNameExpanded


	' -- Dictionary to keep track of set of QParams caller wants to enforce
	Private m_dictCallerEnforcedQParams

	' ---- [Private] Initialize the TreeControl module
	Private Sub Class_Initialize()
		m_bInitCalled = False

		' -- Mandatory settings configured through InitTreeControl()
		m_strTreeID = ""
		Set m_pNCTreeDom = Nothing
		Set m_pNCTreeRenderer = Nothing
		m_bFreshTree = False

		' -- Optional settings configured through properties
		DisplayLeafs = True
		IndentWidth = 20
		NodeSpacing = 0
		NodePadding = 0
		TreeControlPath = IDS_NCTREE_DEFAULT_PATH
		EnableJavaScriptHooks = False

		' -- Internal variables in the module
		Set m_dictCallerEnforcedQParams = CreateObject("Scripting.Dictionary")
	End Sub


	' -------------------------------------------------------------------------
	'
	'	This subroutine initializes the CNCTreeControl with all mandatory
	'	parameters.
	'
	'	@param strTreeID [in] - a unique ID to identify this instance so that
	'				state persistence can be done in an instance-specifc manner.
	'				The result is that multiple CNCTreeControl's can be used in
	'				different, or even the same, ASP page(s) without conflict.
	'
	'	@param pNCTreeDom [in] - an object which supports the
	'				INCTreeDOM defined at the top of this file; for
	'				climbing the object model of the hierarchy to be displayed.
	'
	'	@param pNCTreeRenderer [in] - an object which supports the
	'				INCTreeRenderer defined at the top of this file; for
	'				rendering the tree nodes in HTML.
	'
	'	@param bFreshTree [in] - for specifying whetehr the Tree should be
	'				rendered in a fresh state or use the previous expanded
	'				state.
	'
	' -------------------------------------------------------------------------
	Public Sub InitTreeControl( strTreeID, pNCTreeDom, pNCTreeRenderer, bFreshTree )
		' -- validate parameters first
		If (pNCTreeDom Is Nothing) Then
			Call Response.Write(IDS_NCTREE_ERR_NCTREEDOM)
			Exit Sub
		End If
		If (pNCTreeRenderer Is Nothing) Then
			Call Response.Write(IDS_NCTREE_ERR_RENDERER)
			Exit Sub
		End If

		' -- flag that module has been initialized
		m_bInitCalled = True

		' -- Cache passed in values
		m_strTreeID = strTreeID
		Set m_pNCTreeDom = pNCTreeDom
		Set m_pNCTreeRenderer = pNCTreeRenderer
		m_bFreshTree = bFreshTree

		' -- Given the TreeID we can figure out the QParam and Cookie names for this Tree
		m_strQParamNameExpand = IDS_NCTREE_QSTR_EXPAND & strTreeID
		m_strQParamNameCollapse = IDS_NCTREE_QSTR_COLLAPSE & strTreeID
		m_strCookieNameExpanded = IDS_NCTREE_COOKIE_EXPANDED & strTreeID

		' -- From the QParam names we can figure out the state of this Tree
		m_strExpandNode = CStr(Request.QueryString(m_strQParamNameExpand) )
		m_strCollapseNode = CStr(Request.QueryString(m_strQParamNameCollapse) )

		' -- set current Tree state based on all these known information
		Call SetCurrentTreeState()
	End Sub

	' ---- [Public] Allows caller to enforce some QueryString parameters
	Public Sub PropagateParameter( strName, strValue )
		' Remove old one if it exists already
		If (m_dictCallerEnforcedQParams.Exists( strName ) = True) Then
			Call m_dictCallerEnforcedQParams.Remove( strName )
		End If
		' Add the new one to the internal dictionary
		Call m_dictCallerEnforcedQParams.Add( strName, strValue )
	End Sub

	' ---- [Public] Display a Tree for a Content Management Server hierarchy
	Public Sub DisplayTree()
		' -- check if InitTreeControl() has been called
		If (m_bInitCalled = False) Then
			Call Response.Write(IDS_NCTREE_ERR_NOINIT)
			Exit Sub
		End If

		' -- set up already, display table that represents the tree
		Call Response.Write( vbCrLf & "<table width=100% border=0 cellspacing=" & NodeSpacing & _
											" cellpadding=" & NodePadding & ">" & vbCrLf )

		' -- recursively iterate down through the hierarchy
		Dim pRootObject
		Set pRootObject = m_pNCTreeDom.GetRootObject()
		If Not (pRootObject Is Nothing) Then
			Call RecurseContainers( pRootObject, 0 )
		End If

		' -- end the table
		Call Response.Write( "</table>" & vbCrLf )
	End Sub


	' ---- [Private] Set the state of the Tree
	Private Sub SetCurrentTreeState()
		If (m_bFreshTree = True) Then
			' Tree is told to start fresh
			Call ResetExpandedSet()
			' Set Root node as the only one in the expanded set
			Dim pRootObject
			Set pRootObject = m_pNCTreeDom.GetRootObject()
			If Not (pRootObject Is Nothing) Then
				Call AddToExpandedSet( m_pNCTreeDom.GetObjectID( pRootObject ) )
			End If
		Else
			' Tree is told to Expand or Collapse
			If (m_strExpandNode <> "") Then
				Call AddToExpandedSet( m_strExpandNode )
			ElseIf (m_strCollapseNode <> "") Then
				Call RemoveFromExpandedSet( m_strCollapseNode )
			End If
		End If
	End Sub

	' ---- [Private] Determines the selected object has any children underneath it
	Private Function SelectedObjHasChildren( pSelObj )
		If (m_pNCTreeDom.GetChildrenContainerCol(pSelObj).Count > 0) Then
			SelectedObjHasChildren = True
			Exit Function
		End If
		If (DisplayLeafs = True) Then
			If (m_pNCTreeDom.GetChildrenLeafCol(pSelObj).Count > 0) Then
				SelectedObjHasChildren = True
				Exit Function
			End If
		End If

		SelectedObjHasChildren = False
	End Function

	' ---- [Private] Recursively iterate through the Container Nodes collection of a given Object
	Private Sub RecurseContainers( pContainer, nIndentLevel )
		' -- guard against pContainer being Nothing
		If (pContainer Is Nothing) Then
			Exit Sub
		End If

		' -- render HTML for a new row
		Call GenerateRowBeginHTML()

		' -- generate indentation based on nIndentLevel
		Call GenerateIndentationHTML( nIndentLevel )

		' -- cache if current Container is expanded or not
		Dim bExpanded
		bExpanded = IsInExpandedSet( m_pNCTreeDom.GetObjectID(pContainer) )

		' -- generate 'Plus' or 'Minus' icon for Node depending on its expanded state
		If ( bExpanded = True ) Then
			Call GenerateMinusIconHTML( pContainer )
		Else
			Call GeneratePlusIconHTML( pContainer )
		End If
		Call m_pNCTreeRenderer.RenderContainerNode( pContainer, m_pNCTreeDom, bExpanded )

		' -- render HTML to end current level
		Call GenerateRowEndHTML()

		' -- if pContainer is on the expanded path, display its children too
		If ( bExpanded = True ) Then
			' -- Display Containers children collection
			Dim colChildren, pChild
			Set colChildren = m_pNCTreeDom.GetChildrenContainerCol( pContainer )
			For Each pChild In colChildren
				' -- give every type a chance to do filtering
				If m_pNCTreeDom.ShouldRecurseIntoContainer( pContainer, pChild ) Then
					Call RecurseContainers( pChild, nIndentLevel+1 )
				End If
			Next
			' -- Display Leaf children collection, if configured to do so
			If (DisplayLeafs) Then
				Set colChildren = m_pNCTreeDom.GetChildrenLeafCol( pContainer )
				If (Not colChildren Is Nothing) Then
					For Each pChild In colChildren
						Call IterateLeafs( pChild, nIndentLevel+1 )
					Next
				End If
			End If
		End If
	End Sub

	' ---- [Private] Iterate through the Leaf Nodes collection of a given Object
	Private Sub IterateLeafs( pLeaf, nIndentLevel )
		' -- render HTML for a new row
		Call GenerateRowBeginHTML()

		' -- generate indentation based on nIndentLevel
		Call GenerateIndentationHTML( nIndentLevel )

		' -- generate HTML for Leaf Node
		Call m_pNCTreeRenderer.RenderLeafNode( pLeaf, m_pNCTreeDom )

		' -- render HTML to end current level
		Call GenerateRowEndHTML()
	End Sub

	Private Sub GenerateRowBeginHTML()
		Call Response.Write( vbTab & "<tr><td><nobr>" & vbCrLf )
	End Sub

	Private Sub GenerateRowEndHTML()
		Call Response.Write( vbTab & "</nobr></td></tr>" & vbCrLf )
	End Sub

	' ---- [Private] Generate the HTML for indentation
	Private Sub GenerateIndentationHTML( nIndentLevel )
		' -- generate indentation based on nIndentLevel
		Dim nIndex
		For nIndex = 1 To nIndentLevel
			Call Response.Write( vbTab & vbTab & "<span><img height=1 width='" & IndentWidth & _
									"' border=0 src='" & TreeControlPath & "/Images/Transparent.gif'></span>" & vbCrLf )
		Next
	End Sub

	' ---- [Private] Generate the HTML for a 'Plus' icon before a container
	Private Sub GeneratePlusIconHTML( pContainer )
		Dim strID, strDisplayName

		strID = m_pNCTreeDom.GetObjectID(pContainer)
		strDisplayName = Server.URLEncode( m_pNCTreeDom.GetObjectDisplayName(pContainer) )

		'If EnableJavaScriptHooks Then
		If False Then	
			Call Response.Write( vbTab & vbTab & "<a href='" & GetRoundTripURL( strID, True ) & _
								"' onclick='return WBC_onExpandTreeNode( """ & strID & """, """ & strDisplayName & """);'>" )
		Else
			Call Response.Write( vbTab & vbTab & "<a href='" & GetRoundTripURL( strID, True ) & "'>" )
		End If
		Call Response.Write( "<img src='" & TreeControlPath & "/Images/Plus.gif' border=0></a>" )
	End Sub

	' ---- [Private] Generate the HTML for the 'Minus' icon before a container
	Private Sub GenerateMinusIconHTML( pContainer )
		Dim strID, strDisplayName

		strID = m_pNCTreeDom.GetObjectID(pContainer)
		strDisplayName = Server.URLEncode( m_pNCTreeDom.GetObjectDisplayName(pContainer) )

		'If EnableJavaScriptHooks Then
		If False Then	
			Call Response.Write( vbTab & vbTab & "<a href='" & GetRoundTripURL( strID, False ) & _
								"' onclick='return WBC_onExpandTreeNode( """ & strID & """, """ & strDisplayName & """);'>" )
		Else
			Call Response.Write( vbTab & vbTab & "<a href='" & GetRoundTripURL( strID, False ) & "'>" )
		End If
		Call Response.Write( "<img src='" & TreeControlPath & "/Images/Minus.gif' border=0></a>" )
	End Sub

	' ---- [Private] Checks if the object is in the expanded set
	Private Function IsInExpandedSet( strID )
		' If it does not exist in the incoming Cookies, need to check if it is
		' the latest one to be added according to QueryString;
		' If it exists in the incoming Cookies, need to also verify if it is
		' the latest one to be removed according to QueryString.

		IsInExpandedSet = (CBool( Request.Cookies(m_strCookieNameExpanded)(strID) = strID ) And _
													CBool( strID <> m_strCollapseNode )) Or _
											(CBool( Request.Cookies(m_strCookieNameExpanded)(strID) <> strID ) And _
													CBool( strID = m_strExpandNode ))
	End Function

	' ---- [Private] Add an object ID to the set of expanded objects
	Private Sub AddToExpandedSet( strID )
		Response.Cookies(m_strCookieNameExpanded)(strID) = strID
	End Sub

	' ---- [Private] Remove an object ID to the set of expanded objects
	Private Sub RemoveFromExpandedSet( strID )
		Response.Cookies(m_strCookieNameExpanded)(strID) = ""
	End Sub

	' ---- [Private] Reset the expanded set by clearing cookies
	Private Sub ResetExpandedSet()
		Response.Cookies(m_strCookieNameExpanded) = ""
	End Sub

	' ---- [Private] Generate a base URL for round-tripping.
	Private Function GetRoundTripURL( strObjID, bPlus  )
		GetRoundTripURL = Request.ServerVariables("URL") & "?"

		' -- generate querystring for expand/collapse command for the node
		Dim strCommand
		If (bPlus = True) Then
			strCommand = m_strQParamNameExpand & "=" & Server.URLEncode(strObjID) & "&"
		Else
			strCommand = m_strQParamNameCollapse & "=" & Server.URLEncode(strObjID) & "&"
		End If
		GetRoundTripURL = GetRoundTripURL & strCommand

		' -- Deal with the caller enforced QParams
		Dim aKeys, aItems, nIndex
		aItems = m_dictCallerEnforcedQParams.Items
		aKeys = m_dictCallerEnforcedQParams.Keys
		For nIndex = 0 To UBound(aKeys)
			GetRoundTripURL = GetRoundTripURL & aKeys(nIndex) & "=" & Server.URLEncode(aItems(nIndex)) & "&"
		Next

		' -- Regenerate other QParams that are not managed by this Tree
		Dim pQParam, strParamName, pSubQParam
		For Each pQParam In Request.QueryString
			strParamName = CStr(pQParam)
			' Mask out all QParam's that are from this tree
			If ( (strParamName <> m_strQParamNameExpand) And (strParamName <> m_strQParamNameCollapse) And _
																								Not m_dictCallerEnforcedQParams.Exists(strParamName) ) Then
				For Each pSubQParam In Request.QueryString(pQParam)
					GetRoundTripURL = GetRoundTripURL & Server.URLEncode(strParamName) & "=" & Server.URLEncode(CStr(pSubQParam)) & "&"
				Next
			End If
		Next
	End Function

End Class


%>