using System;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace CSSFriendly
{
public class SiteMapPathAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
{
public SiteMapPathAdapter()
{
//
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
RegisterScripts();
}
private void RegisterScripts()
{
Page.ClientScript.RegisterClientScriptInclude(GetType(), GetType().ToString(), Page.ResolveUrl("~/JavaScript/MenuAdapter.js"));
}
protected override void RenderBeginTag(HtmlTextWriter writer)
{
if ((Control != null) && (Control.Attributes["CssSelectorClass"] != null) && (Control.Attributes["CssSelectorClass"].Length > 0))
{
writer.WriteLine();
writer.WriteBeginTag("div");
writer.WriteAttribute("class", Control.Attributes["CssSelectorClass"]);
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
}
writer.WriteLine();
writer.WriteBeginTag("div");
writer.WriteAttribute("class", "AspNet-siteMap");
writer.Write(HtmlTextWriter.TagRightChar);
}
protected override void RenderEndTag(HtmlTextWriter writer)
{
writer.WriteEndTag("div");
if ((Control != null) && (Control.Attributes["CssSelectorClass"] != null) && (Control.Attributes["CssSelectorClass"].Length > 0))
{
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("div");
}
writer.WriteLine();
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Indent++;
SiteMapPath item = (SiteMapPath)Control;
SiteMapProvider Provider = ((System.Web.UI.WebControls.SiteMapPath)(Control)).Provider;
SiteMapNodeCollection collection = new SiteMapNodeCollection();
SiteMapNode node = Provider.CurrentNode;
if (node != null)
{
collection.Add(node);
while (node != Provider.CurrentNode.RootNode)
{
node = node.ParentNode;
collection.Add(node);
}
}
BuildItems(collection, true, writer);
writer.Indent--;
writer.WriteLine();
}
private void BuildItems(SiteMapNodeCollection items, bool isRoot, HtmlTextWriter writer)
{
if (items.Count > 0)
{
writer.WriteLine();
writer.WriteBeginTag("p");
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
for (int i = items.Count - 1; i > -1; i--)
{
// Write href links for item, if item is not currentNode
if (i != 0)
{
BuildItem(items[i], writer);
}
else
{
// if node is currentNode just print the title
writer.WriteLine();
writer.Indent++;
writer.Write(items[i].Title);
writer.Indent--;
}
}
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("p");
}
}
private void BuildItem(SiteMapNode item, HtmlTextWriter writer)
{
if ((item != null) && (writer != null))
{
if (item.Url.Length > 0)
{
writer.WriteLine();
writer.WriteBeginTag("a");
writer.WriteAttribute("href", Page.ResolveUrl(item.Url));
writer.WriteAttribute("class", "AspNet-SiteMap-Link");
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
writer.WriteLine();
writer.Write(item.Title);
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("a");
writer.WriteLine();
writer.WriteBeginTag("span");
writer.WriteAttribute("class", "Hide");
writer.Write(HtmlTextWriter.TagRightChar);
writer.Indent++;
writer.Write(",");
writer.Indent--;
writer.WriteLine();
writer.WriteEndTag("span");
}
}
}
}
}