using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Xml;
using System.Data;
namespace CustomWebMenu
{
/// <summary>
/// Custom CustomWebMenu class. Uses Tigra menu scripts
/// </summary>
[DefaultProperty("MenuXMLPath"),
ToolboxData("<{0}:CustomWebMenu runat=server></{0}:CustomWebMenu>")]
public class CustomWebMenu : WebControl
{
private string xmlDataSource = "";
private DataSet dsMenu = null;
private int padWidth = 15;
//root
private int rootheight = 24;
private int rootwidth = 100;
private int roottop = 0;
private int rootleft = 0;
private int rootoffsettop = 0;
private int rootoffsetleft = 100;
private int menuhidedelay = 200;
private int menuexpanddelay = 200;
private string rootouterstyleclass = "m0l0oout";
private string rootouteroverstyleclass = "m0l0oover";
private string rootinnerstyleclass = "m0l0iout";
private string rootinneroverstyleclass = "m0l0iover";
//level 1
private int childtheight = 24;
private int childwidth = 170;
private int childtop = 25;
private int childleft = 0;
private string childouterstyleclass = "m0l1oout";
private string childouteroverstyleclass = "m0l1oover";
private string childinnerstyleclass = "m0l1iout";
private string childinneroverstyleclass = "m0l1iover";
//level n
private int ntop = 5;
private int nleft = 160;
private string imgMenuPointerUrl = string.Empty;
public int RootTop
{
get { return roottop; }
set { roottop = value; }
}
public int RootLeft
{
get { return rootleft; }
set { rootleft = value; }
}
public int RootHeight
{
get { return rootheight; }
set { rootheight = value; }
}
public int RootWidth
{
get { return rootwidth; }
set { rootwidth = value; }
}
public int RootOffSetTop
{
get { return rootoffsettop; }
set { rootoffsettop = value; }
}
public int RootOffSetLeft
{
get { return rootoffsetleft; }
set { rootoffsetleft = value; }
}
public int MenuHideDelayTime
{
get { return menuhidedelay; }
set { menuhidedelay = value; }
}
public int MenuExpandDelayTime
{
get { return menuexpanddelay; }
set { menuexpanddelay = value; }
}
public string RootOuterStyle
{
get { return rootouterstyleclass; }
set { rootouterstyleclass = value; }
}
public string RootMouseOverStyle
{
get { return rootouteroverstyleclass; }
set { rootouteroverstyleclass = value; }
}
public string RootInnerStyle
{
get { return rootinnerstyleclass; }
set { rootinnerstyleclass = value; }
}
public string RootMouseStyle
{
get { return rootinneroverstyleclass; }
set { rootinneroverstyleclass = value; }
}
public int ChildHeight
{
get { return childtheight; }
set { childtheight = value; }
}
public int ChildWidth
{
get { return childwidth; }
set { childwidth = value; }
}
public int ChildTopMargin
{
get { return childtop ; }
set { childtop = value; }
}
public int ChildLeftMargin
{
get { return childleft; }
set { childleft = value; }
}
public string ChildOuterStyle
{
get { return childouterstyleclass; }
set { childouterstyleclass = value; }
}
public string ChildMouseOverStyle
{
get { return childouteroverstyleclass; }
set { childouteroverstyleclass = value; }
}
public string ChildInnerStyle
{
get { return childinnerstyleclass; }
set { childinnerstyleclass = value; }
}
public string ChildMouseStyle
{
get { return childinneroverstyleclass; }
set { childinneroverstyleclass = value; }
}
public int nChildTop
{
get { return ntop; }
set { ntop = value; }
}
public int nChildLeft
{
get { return nleft; }
set { nleft = value; }
}
/// <summary>
/// Distance of the menu arrow from the menu item caluculated using this Pad Width.
/// </summary>
public int PadWidth
{
get
{
return padWidth;
}
set
{
padWidth = value;
}
}
/// <summary>
/// Image for the arrow indicating the child menu items e.g.
/// <code><img src='image.gif'></code>
/// </summary>
public string MenuPointerUrl
{
set
{
imgMenuPointerUrl = value;
}
get
{
return imgMenuPointerUrl;
}
}
private string MenuPointer
{
get
{
return (string.IsNullOrEmpty(MenuPointerUrl)) ? "<img src=" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomWebMenu.Resources.Images.OneStep.gif") + " border=0>"
: "<img src=" + this.MenuPointerUrl + " border=0>";
}
}
/// <summary>
/// Path of the XML menu File
/// </summary>
[Category("Appearance"),
DefaultValue("")]
public string MenuXMLPath
{
get
{
return xmlDataSource;
}
set
{
xmlDataSource = value;
}
}
/// <summary>
/// DataSet from the xml menu as DataSource.
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public DataSet DataSource
{
get
{
return dsMenu;
}
set
{
dsMenu = value;
}
}
string GetMenuData()
{
if (dsMenu != null)
{
return GetScript(dsMenu.Tables[0]);
}
else if (xmlDataSource != "")
{
DataSet dsXmlMenu = new DataSet();
dsXmlMenu.ReadXml(xmlDataSource);
return GetScript(dsXmlMenu.Tables[0]);
}
else
{
return "";
}
}
string GetScript(DataTable dtbMenu)
{
StringBuilder oMenu = new StringBuilder();
oMenu.Append(@"<script language='javascript'>var MENU_ITEMS = [");
GetMenuItems(dtbMenu, "", "", "", ref oMenu);
//oMenu.Append(@"];new menu (MENU_ITEMS, MENU_POS);</script>");
oMenu.Append(@"];");
MenuLayout(ref oMenu);
oMenu.Append(" new menu (MENU_ITEMS, MENU_POS);</script>");
return oMenu.ToString();
}
private void ReadChildRows(DataTable dtbTable, string nodeId, string nodeName, string nodeParentName, ref StringBuilder oMenu)
{
DataRow[] drRows = dtbTable.Select("root='" + nodeId + "'");
if (drRows.Length > 0)
{
if (nodeId != "")
{
if (nodeParentName == "")
oMenu.Append("\r\n['" + nodeName + " " + MenuPointer + "', null, null,");
else
oMenu.Append("\r\n['" + nodeName.PadRight(padWidth, '^').Replace("^", " ") + MenuPointer + "', null, null,");
}
foreach (DataRow dr in drRows)
{
ReadChildRows(dtbTable, dr["menuid"].ToString(), dr["name"].ToString(), dr["root"].ToString(), ref oMenu);
if (dr["root"].ToString() != "")
{
drRows = dtbTable.Select("root='" + dr["menuid"].ToString() + "'");
if (drRows.Length == 0)
oMenu.Append("\r\n['" + dr["name"].ToString() + "','" + dr["url"].ToString() + "'],");
}
}
if (nodeId != "")
oMenu.Append("],\r\n\r\n");
}
else
{
if (nodeParentName == "")
oMenu.Append("\r\n['" + nodeName + " " + MenuPointer + "', null, null,null],");
}
}
private void GetMenuItems(DataTable dtbTable, string nodeId, string nodeName, string nodeParentName, ref StringBuilder oMenu)
{
DataRow[] drRows = dtbTable.Select("root='" + nodeId + "'");
if (drRows.Length > 0)
{
if (nodeId != "")
{
if (nodeParentName == "")
oMenu.Append("\r\n['" + nodeName + " " + MenuPointer + "', null, null,");
else
oMenu.Append("\r\n['" + nodeName.PadRight(padWidth, '^').Replace("^", " ") + MenuPointer + "', null, null,");
}
foreach (DataRow dr in drRows)
{
GetMenuItems(dtbTable, dr["menuid"].ToString(), dr["name"].ToString(), dr["root"].ToString(), ref oMenu);
if (dr["root"].ToString() != "")
{
drRows = dtbTable.Select("root='" + dr["menuid"].ToString() + "'");
if (drRows.Length == 0)
oMenu.Append("\r\n['" + dr["name"].ToString() + "','" + dr["url"].ToString() + "'],");
}
}
if (nodeId != "")
oMenu.Append("],\r\n\r\n");
}
else
{
if (nodeParentName == "")
oMenu.Append("\r\n['" + nodeName + " " + MenuPointer + "', null, null,null],");
}
}
private void MenuLayout(ref StringBuilder oMenu)
{
oMenu.Append(" var MENU_POS = [\r\n{");
oMenu.Append("\r\n 'height' : " + this.RootHeight + " ,\r\n");
oMenu.Append("'width' : " + this.RootWidth + " ,\r\n");
oMenu.Append("'block_top' : " + this.RootTop + " ,\r\n");
oMenu.Append("'block_left' : " + this.RootLeft + " ,\r\n");
oMenu.Append("'top' : " + this.RootOffSetTop + " ,\r\n");
oMenu.Append("'left' : " + this.RootOffSetLeft + " ,\r\n");
oMenu.Append("'hide_delay' : " + this.MenuHideDelayTime + " ,\r\n");
oMenu.Append("'expd_delay' : " + this.MenuExpandDelayTime + " ,\r\n");
oMenu.Append("'css' : { \r\n");
oMenu.Append("'outer' : [ '" + this.RootOuterStyle + "' ,'" + this.RootMouseOverStyle + "'],\r\n");
oMenu.Append("'inner' : [ '" + this.RootInnerStyle + "' ,'" + this.RootMouseStyle + "']\r\n}\r\n},\r\n{\r\n");
oMenu.Append("'height' : " + this.ChildHeight + " ,\r\n");
oMenu.Append("'width' : " + this.ChildWidth + " ,\r\n");
oMenu.Append("'block_top' : " + this.ChildTopMargin + " ,\r\n");
oMenu.Append("'block_left' : " + this.ChildLeftMargin + " ,\r\n");
oMenu.Append("'top' : " + 23 + " ,\r\n");
oMenu.Append("'left' : " + 0 + " ,\r\n");
oMenu.Append("'css' : { \r\n");
oMenu.Append("'outer' : [ '" + this.ChildOuterStyle + "' ,'" + this.ChildMouseOverStyle + "'],\r\n");
oMenu.Append("'inner' : [ '" + this.ChildInnerStyle + "' ,'" + this.ChildMouseStyle + "']\r\n}\r\n},\r\n{\r\n");
oMenu.Append("'block_top' : " + this.nChildTop + " ,\r\n");
oMenu.Append("'block_left' : " + this.nChildLeft );
oMenu.Append("\r\n } \r\n ];");
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ClientScriptManager cs = this.Page.ClientScript;
string CustomWebMenu = "CustomWebMenu.Resources.CustomWebMenu.js";
cs.RegisterClientScriptResource(this.GetType(), CustomWebMenu);
string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(), "CustomWebMenu.Resources.Css.CustomWebMenu.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "cssfile", css, false);
}
/// <summary>
/// Render this control to the output parameter specified.
/// </summary>
/// <param name="output"> The HTML writer to write out to </param>
protected override void Render(HtmlTextWriter output)
{
if (this.DataSource != null || !string.IsNullOrEmpty(this.xmlDataSource))
output.Write(GetMenuData());
}
}
}