<%
' ********************************************************************
' 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/CNCGridControl.inc
' Version: 	Microsoft Content Management Server
' Author:	Microsoft Corporation.
' Purpose:	Responsible for displaying a grid that can be sorted on each column
'
' Public Interface of CNCGridControl
'	- Sub InitGridControl( strGridID, pNCGridDecorator, colObjects, bPersistedAcrossSessions )
'	- Sub SetDisplayRange( nColBegin, nColEnd )
'	- Sub AddColumn( strColumnIndex, bSortable )
'	- Sub PropagateParameter( strName, strValue )
'	- Sub DisplayGrid()
'	- Property ColumnHeadAnchorCSSClass [read/write]
'	- Property AscendingIndicatorHTML [read/write]
'	- Property DescendingIndicatorHTML [read/write]
'	- Property Width [read/write]
'	- Property ItemSpacing [read/write]
'	- Property ItemPadding [read/write]
'	- Property ContentItemsVisible [read/write]
'
' INCGridDecorator Callback Interface of CNCGridControl
'	- Sub RenderColumnHeadTRAttrib()
'	- Sub RenderColumnHeadTDAttrib( strColumnIndex )
'	- Sub RenderColumnHeadTDContent( strColumnIndex )
'	- Sub RenderItemTRAttrib( pObject, nCollectionIndex, nDisplayIndex )
'	- Sub RenderItemTDAttrib( pObject, nCollectionIndex, nDisplayIndex, strColumnIndex )
'	- Sub RenderItemTDContent( pObject, nCollectionIndex, nDisplayIndex, strColumnIndex )
'	- Sub RenderEmptyGrid()
'	- Sub SortCollection( strSortByColumn, colObjects, bAscending )
' ********************************************************************

' -- Error Strings
Const IDS_NCGRID_ERR_DECORATOR = "<div><b>CNCGridControl Error:</b> Nothing is passed in for the CNCGridDecorator.</div>"
Const IDS_NCGRID_ERR_COLLECTION = "<div><b>CNCGridControl Error:</b> Collection not properly initialized.</div>"
Const IDS_NCGRID_ERR_RANGE = "<div><b>CNCGridControl Error:</b> Invalid range passed in.</div>"
Const IDS_NCGRID_ERR_DUPCOLUMN = "<div><b>CNCGridControl Error:</b> Duplicate column names.</div>"
Const IDS_NCGRID_ERR_NOINIT = "<div><b>CNCGridControl Error:</b> InitGridControl() has not been called before calling DisplayGrid().</div>"

' ---- Top level cookie/queyrstring name for CNCGridControl
Const IDS_NCGRID_SORTBY_ID = "NCGridSortBy"
Const IDS_NCGRID_SORTASCENDING_ID = "NCGridSortAscending"

' ---- Default persistence duration in days
Const C_INT_GRID_PERSISTENCE_DURATION = 365

Class CNCGridControl

	' -- Property to configure UI layout and display
	Public ColumnHeadAnchorCSSClass
	Public AscendingIndicatorHTML
	Public DescendingIndicatorHTML
	Public Width
	Public ItemSpacing
	Public ItemPadding
	Public ContentItemsVisible

	' -- Mandatory parameters passed in by client for controlling the module
	Private m_strGridID, m_pNCGridDecorator, m_colObjects
	' -- Rnage of items to be displayed
	Private m_nColBegin, m_nColEnd

	' -- Module internal state management
	Private m_bInitCalled
	Private m_dictColumns, m_dictCallerEnforcedQParams, m_nMaxColumn
	Private m_strQParamNameSortBy, m_strQParamNameSortAscending
	Private m_strCurrentSortColumnIndex, m_bSortAscending


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

		' -- Default values for mandatory settings configured through InitGridControl()
		m_strGridID = ""
		Set m_pNCGridDecorator = Nothing
		Set m_colObjects = Nothing

		' -- Default values for optional settings configured through properties
		ColumnHeadAnchorCSSClass = ""
		AscendingIndicatorHTML = "<font color=#000000 face='webdings'>5</font>"
		DescendingIndicatorHTML = "<font color=#000000 face='webdings'>6</font>"
		Width = "100%"
		ItemSpacing = 0
		ItemPadding = 0
		ContentItemsVisible = True

		' -- class internal variables
		Set m_dictColumns = CreateObject("Scripting.Dictionary")
		Set m_dictCallerEnforcedQParams = CreateObject("Scripting.Dictionary")
		m_nColBegin = -1
		m_nColEnd = -1
		m_nMaxColumn = 0
	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine initializes the CNCGridControl with all mandatory
	'	parameters.
	'
	'	@param strGridID [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 CNCGridControl's can be used in
	'				different, or even the same, ASP page(s) without conflict.
	'
	'	@param pNCGridDecorator [in] - a renderer object which supports the
	'				INCGridDecorator defined at the top of this file; for
	'				painting the Grid in HTML.
	'
	'	@param colObjects [in] - collection of objects to be displayed in the
	'				Grid. Must support accessing items by index: colObjects(i)
	'
	'	@param bPersistedAcrossSessions [in] - for specifying whetehr the
	'				state should be persisted across browser sessions. If True,
	'				state will be persisted for a year. If False, the
	'				state will be persisted for the current browser session
	'				only.
	'
	' -------------------------------------------------------------------------
	Public Sub InitGridControl( strGridID, pNCGridDecorator, colObjects, bPersistedAcrossSessions )
		' -- validate parameters first
		If (pNCGridDecorator Is Nothing) Then
			Call Response.Write(IDS_NCGRID_ERR_DECORATOR)
			Exit Sub
		End If
		If (colObjects Is Nothing) Then
			Call Response.Write(IDS_NCGRID_ERR_COLLECTION)
			Exit Sub
		End If

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

		' -- Cache passed in values
		m_strGridID = strGridID
		Set m_pNCGridDecorator = pNCGridDecorator
		Set m_colObjects = colObjects

		' -- set up main querystring param names
		m_strQParamNameSortBy = IDS_NCGRID_SORTBY_ID & "_" & m_strGridID
		m_strQParamNameSortAscending = IDS_NCGRID_SORTASCENDING_ID & "_" & m_strGridID

		' -- If we are told of a new sort index from QueryString, store it into Cookies
		If ( Request.QueryString(m_strQParamNameSortBy) <> "" ) Then
			Response.Cookies(IDS_NCGRID_SORTBY_ID)(m_strGridID) = Request.QueryString(m_strQParamNameSortBy)
			Response.Cookies(IDS_NCGRID_SORTASCENDING_ID)(m_strGridID) = Request.QueryString(m_strQParamNameSortAscending)
			If (bPersistedAcrossSessions = True) Then
				Response.Cookies(IDS_NCGRID_SORTBY_ID).Expires = Now + C_INT_GRID_PERSISTENCE_DURATION
				Response.Cookies(IDS_NCGRID_SORTASCENDING_ID).Expires = Now + C_INT_GRID_PERSISTENCE_DURATION
			End If

			m_strCurrentSortColumnIndex = CStr( Request.QueryString(m_strQParamNameSortBy) )
			m_bSortAscending = CBool( Request.QueryString(m_strQParamNameSortAscending) )

		ElseIf (Request.Cookies(IDS_NCGRID_SORTBY_ID)(m_strGridID) <> "")  Then

		' -- If cookies exist already, use it and extend its life
			m_strCurrentSortColumnIndex = CStr( Request.Cookies(IDS_NCGRID_SORTBY_ID)(m_strGridID) )
			m_bSortAscending = CBool( Request.Cookies(IDS_NCGRID_SORTASCENDING_ID)(m_strGridID) )
			If (bPersistedAcrossSessions = True ) Then
				Response.Cookies(IDS_NCGRID_SORTBY_ID).Expires = Now + C_INT_GRID_PERSISTENCE_DURATION
				Response.Cookies(IDS_NCGRID_SORTASCENDING_ID).Expires = Now + C_INT_GRID_PERSISTENCE_DURATION
			End if
		End If

		' -- Sort the list using m_pNCGridSorter
		Call m_pNCGridDecorator.SortCollection( m_strCurrentSortColumnIndex, m_colObjects, m_bSortAscending )

	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine sets the range of items in collection to be displayed
	'
	'	@param nColBegin [in] - the beginning index for the range of items to
	'				be displayed in the Grid
	'
	'	@param nColEnd [in] - the ending index for the range of items to be
	'				displayed in the Grid
	'
	' -------------------------------------------------------------------------
	Public Sub SetDisplayRange( nColBegin, nColEnd )
		m_nColBegin = nColBegin
		m_nColEnd = nColEnd
		' -- any meaningless range should be treated as -1, -1
		If (m_nColBegin > m_nColEnd) Then
			m_nColBegin = -1
			m_nColEnd = -1
		End If
		' -- enforce maximum on m_nColEnd
		If (m_nColEnd > (m_colObjects.Count-1)) Then
			m_nColEnd = m_colObjects.Count-1
		End If
	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine adds a column to the grid. The order in which the
	'	columns are added will determine the column display order
	'
	'	@param strColumnIndex [in] - the index for the column which will be
	'				used to uniquely identify the column among all the columns.
	'
	'	@param bSortable [in] - define whether the column should be sortable
	'				or not.
	'
	' -------------------------------------------------------------------------
	Public Sub AddColumn( strColumnIndex, bSortable )
		' Avoid putting duplicate columns into dictionary
		If (m_dictColumns.Exists(strColumnIndex)) Then
			Call Response.Write(IDS_NCGRID_ERR_DUPCOLUMN)
			Exit Sub
		Else
			Call m_dictColumns.Add( strColumnIndex, bSortable )
		End If
	End Sub

	' -------------------------------------------------------------------------
	'
	'	This subroutine allows client to enforce some QueryString parameters
	'	to over-ride default behavior of retaining all querysting parameters
	'	when round-tripping.
	'
	'	@param strName [in] - name of the querystring parameter
	'
	'	@param strValue [in] - value of the querystring parameter to be
	'							enforced.
	'
	' -------------------------------------------------------------------------
	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

	' -------------------------------------------------------------------------
	'
	'	This subroutine displays a Grid of sortable objects, using all the
	'	properties and options set up earlier.
	'
	' -------------------------------------------------------------------------
	Public Sub DisplayGrid()
		' -- check if InitGridControl() has been called
		If (m_bInitCalled = False) Then
			Call Response.Write(IDS_NCGRID_ERR_NOINIT)
			Exit Sub
		End If

		' -- figure out number of columns first
		Dim aColumnIndex, aColumnSortable
		aColumnIndex = m_dictColumns.Keys
		aColumnSortable = m_dictColumns.Items
		m_nMaxColumn = UBound( aColumnIndex )

		' -- figure how many rows we need to render
		Dim nItems, nRowsMax
		If (m_nColBegin < 0) And (m_nColEnd < 0) Then
			nItems = m_colObjects.Count
			m_nColBegin = 0
			m_nColEnd = m_colObjects.Count-1
		Else
			nItems = m_nColEnd - m_nColBegin + 1
		End If
		nRowsMax = nItems - 1

		' -- Start the <table>
		Call Response.Write( vbCrLf & "<table width='" & Width & "' border='0' cellspacing=" & ItemSpacing & _
											" cellpadding=" & ItemPadding & ">" & vbCrLf )

		' -- generate top row table cells for column headings
		Dim nColumnIndex, strColumnIndex, bAscending
		Response.Write( vbTab & "<tr " )
		Call m_pNCGridDecorator.RenderColumnHeadTRAttrib()
		Response.Write( ">" & vbCrLf )
		For nColumnIndex = 0 To m_dictColumns.Count -1
			Response.Write( "<td " )
			Call m_pNCGridDecorator.RenderColumnHeadTDAttrib( aColumnIndex(nColumnIndex) )
			Response.Write( ">" )
			' generate <a> tag if column is sort-able
			strColumnIndex = aColumnIndex(nColumnIndex)
			If (aColumnSortable(nColumnIndex) = True) And (nItems > 0) Then
				' by default, sort Ascending first
				bAscending = True
				' sort by what we are told if this column is told to be sorted
				If (m_strCurrentSortColumnIndex = strColumnIndex) Then
					bAscending = Not m_bSortAscending
				End If
				Response.Write( "<a class='" & ColumnHeadAnchorCSSClass & "' href='" & GetRoundTripSortURL( strColumnIndex, bAscending ) & "'>" )
				Call m_pNCGridDecorator.RenderColumnHeadTDContent( strColumnIndex )
				Response.Write( "</a>" )
				' put in sort order indicator if the column is the one being sorted
				If (m_strCurrentSortColumnIndex = strColumnIndex) Then
					If (m_bSortAscending = True) Then
						Response.Write( " " & AscendingIndicatorHTML )
					Else
						Response.Write( " " & DescendingIndicatorHTML )
					End If
				End If
			Else
				Call m_pNCGridDecorator.RenderColumnHeadTDContent( strColumnIndex )
			End If
			Response.Write( "</td>" )
		Next
		Response.Write( vbTab & "</tr>" & vbCrLf )

		' display content depending on the ContentItemsVisible flag
		If (ContentItemsVisible = True) Then
			If (nItems <= 0) Then
				' -- Render empty Grid
				Response.Write( vbTab & "<tr>" & vbCrLf )
				Response.Write( vbTab & vbTab & "<td align='center' colspan='" & CStr(m_nMaxColumn+1) & "'>" )
				Call m_pNCGridDecorator.RenderEmptyGrid()
				Response.Write( "</td>" & vbCrLf )
				Response.Write( vbTab & "</tr>" & vbCrLf )
			Else
				' -- render the rows and columns of the data
				Dim nRowIndex, nRowOffset, nCollectionIndex
				For nRowIndex = 0 To nRowsMax
					nCollectionIndex = m_nColBegin + nRowIndex
					Response.Write( vbTab & "<tr " )
					Call m_pNCGridDecorator.RenderItemTRAttrib( m_colObjects(nCollectionIndex), nCollectionIndex, nRowIndex )
					Response.Write( ">" & vbCrLf )
					If ((m_nColEnd < 0) Or (nCollectionIndex <= m_nColEnd)) Then
						Response.Write( vbTab & vbTab )
						' -- repeat callback for each item m_nMaxColumn+1 number of times,
						'		each time generating a block of <td>...</td>
						For nColumnIndex = 0 To m_nMaxColumn
							Response.Write( "<td " )
							Call m_pNCGridDecorator.RenderItemTDAttrib( m_colObjects(nCollectionIndex), nCollectionIndex, _
																			nRowIndex, aColumnIndex(nColumnIndex) )
							Response.Write( ">" )
							Call m_pNCGridDecorator.RenderItemTDContent( m_colObjects(nCollectionIndex), nCollectionIndex, _
																			nRowIndex, aColumnIndex(nColumnIndex) )
							Response.Write( "</td>" )
						Next
						Response.Write( vbCrLf )
					Else
						Response.Write( vbTab & vbTab & "<td>&nbsp;</td>" & vbCrLf )
					End If
					Response.Write( vbTab & "</tr>" & vbCrLf )
				Next
			End If
		End If

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

	' ---- [Private] Return the roundtrip sort URL for a certain column
	Private Function GetRoundTripSortURL( strColumnIndex, bAscending )
		GetRoundTripSortURL = Request.ServerVariables("URL") & "?"

		' -- Deal with the main query string params managed by this control
		Dim strGridQString
		strGridQString = m_strQParamNameSortBy & "=" & strColumnIndex & "&" & _
								m_strQParamNameSortAscending & "=" & CStr(bAscending) & "&"
		GetRoundTripSortURL = GetRoundTripSortURL & strGridQString

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

		' -- Regenerate other QParams that are not managed by this Grid
		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_strQParamNameSortBy) And (strParamName <> m_strQParamNameSortAscending) And _
					Not m_dictCallerEnforcedQParams.Exists(strParamName) ) Then
				For Each pSubQParam In Request.QueryString(pQParam)
					GetRoundTripSortURL = GetRoundTripSortURL & Server.URLEncode(CStr(pQParam)) & "=" & Server.URLEncode(CStr(pSubQParam)) & "&"
				Next
			End If
		Next
	End Function

End Class

%>