关于生成目录树结构的类(2)

发表于:2007-06-30来源:作者:点击数: 标签:
Class Tree Public Top Public Left Public ExpandImage Public CollapseImage Public LeafImage Private mszPosition Private mcolChildren Public Property Let Absolute(bData) If bData Then mszPosition = "absolute" Else mszPosition = "relative" En
     Class Tree
  
  Public Top
  Public Left
  Public ExpandImage
  Public CollapseImage
  Public LeafImage
  Private mszPosition
  Private mcolChildren
  
  Public Property Let Absolute(bData)
  If bData Then mszPosition = "absolute" Else mszPosition = "relative"
  End Property
  
  Public Property Get Absolute()
  Absolute = CBool(mszPosition = "absolute")
  End Property
  
  Private Sub Class_Initialize()
  Set mcolChildren = Server.CreateObject("Scripting.Dictionary")
  mnTop = 0
  mnLeft = 0
  mszPosition = "absolute"
  End Sub
  
  Private Sub Class_Terminate()
  mcolChildren.RemoveAll()
  Set mcolChildren = Nothing
  End Sub
  
  Public Function CreateChild(szValue)
  
  Set CreateChild = New TreeNode
  
  CreateChild.Value = szValue
  CreateChild.ExpandImage = ExpandImage
  CreateChild.CollapseImage = CollapseImage
  CreateChild.LeafImage = LeafImage
  
  mcolChildren.Add mcolChildren.Count + 1, CreateChild
  
  End Function
  
  Public Sub LoadTemplate(szFileName)
  Dim objWorkingNode
  Dim colNodeStack
  Dim fsObj, tsObj
  Dim szLine
  Dim nCurrDepth, nNextDepth
  
  Set colNodeStack = Server.CreateObject("Scripting.Dictionary")
  Set fsObj = CreateObject("Scripting.FileSystemObject")
  Set tsObj = fsObj.OpenTextFile(szFileName, 1)
  
  nCurrDepth = 0
  While Not tsObj.AtEndOfLine
  nNextDepth = 1
  szLine = tsObj.ReadLine()
  
  If nCurrDepth = 0 Then
  Set objWorkingNode = CreateChild(Trim(szLine))
  nCurrDepth = 1
  Else
  While Mid(szLine,nNextDepth,1) = vbTab Or Mid(szLine,nNextDepth,1) = " "
  nNextDepth = nNextDepth + 1
  WEnd
  
  If nNextDepth > 1 Then szLine = Trim(Mid(szLine,nNextDepth))
  
  If szLine <> "" Then
  If nNextDepth > nCurrDepth Then
  If colNodeStack.Exists(nCurrDepth) Then
  Set colNodeStack.Item(nCurrDepth) = objWorkingNode
  Else
  colNodeStack.Add nCurrDepth, objWorkingNode
  End If
  
  Set objWorkingNode = objWorkingNode.CreateChild(szLine)
  
  nCurrDepth = nCurrDepth + 1
  ElseIf nNextDepth <= nCurrDepth Then
  
  If nNextDepth > 1 Then
  
  nNextDepth = nNextDepth - 1
  While Not colNodeStack.Exists(nNextDepth) And nNextDepth > 1
  nNextDepth = nNextDepth - 1
  WEnd
  
  Set objWorkingNode = colNodeStack.Item(nNextDepth)
  Set objWorkingNode = objWorkingNode.CreateChild(szLine)
  
  nNextDepth = nNextDepth + 1
  Else
  Set objWorkingNode = CreateChild(szLine)
  End If
  
  nCurrDepth = nNextDepth
  End If
  End If
  
  End If
  WEnd
  
  tsObj.Close()
  Set tsObj = Nothing
  Set fsObj = Nothing
  
  colNodeStack.RemoveAll()
  Set colNodeStack = Nothing
  
  End Sub
  
  
  Public Sub Draw()
  
  AddClientScript()
  
  Response.Write "<div id=""treectrl"" style=""left: " & Left & "px; top: " & Top & "px; position: " & mszPosition & ";"">" & vbCrLf
  Response.Write "<form name=""treectrlfrm"" action=""" & Request.ServerVariables("SCRIPT_NAME") & """ method=""get"">" & vbCrLf
  Response.Write "<table border=""0"">" & vbCrLf
  Response.Write "<tr><td>" & vbCrLf
  
  For Each ChildNode In mcolChildren.Items
  ChildNode.Draw()
  Next
  
  Response.Write "</td></tr>" & vbCrLf
  Response.Write "</table>" & vbCrLf
  
  Response.Write "<input type=""hidden"" name=""togglenode"" value="""">" & vbCrLf
  Response.Write "</form>" & vbCrLf
  Response.Write "</div>" & vbCrLf
  
  End Sub
  
  Private Sub AddClientScript()
  %>
  <script language="JavaScript">
  
  function expandNode(szNodeName)
  {
  if(document.layers != null) {
  document.treectrl.document.treectrlfrm.togglenode.value = szNodeName;
  document.treectrl.document.treectrlfrm.submit();
  }
  else {
  document.all["treectrlfrm"].togglenode.value = szNodeName;
  document.all["treectrlfrm"].submit();
  }
  }
  
  function collapseNode(szNodeName)
  {
  if(document.layers != null) {
  document.treectrl.document.treectrlfrm.elements[szNodeName].value = -1;
  document.treectrl.document.treectrlfrm.submit();
  }
  else {
  document.treectrlfrm.elements[szNodeName].value = -1;
  document.treectrlfrm.submit();
  }
  }
  
  </script>
  <%
  End Sub
  
  End Class
  
  %>
  演示地址:http://www.cnaspol.com/templatetree/test.asp
  
  
  
  

原文转自:http://www.ltesting.net