using System;
using System.Collections;
using System.Data;
using System.IO;
using System.Reflection;
using System.Resources;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace UDDI.Web
{
public delegate void MenuEventHandler( object sender, MenuEventArgs e );
[ParseChildren()]
public class MenuControl : UddiControl, IPostBackEventHandler
{
public MenuControl()
{
this.SelectedIndexChanged += new MenuEventHandler( this.MenuControl_SelectedIndexChanged );
MenuItems = new MenuItemControlCollection();
}
public event MenuEventHandler SelectedIndexChanged;
protected string Roots
{
get
{
return HyperLinkManager.GetSecureHyperLink( "" );
}
}
protected MenuItemControlCollection MenuItems;
public string CssClass
{
get{ return this.Name; }
}
private string name;
public string Name
{
get{ return name; }
set{ name=value; }
}
private MenuType type;
public MenuType Type
{
get{ return type; }
set{ type=(MenuType)value; }
}
private string width = "100%";
public string Width
{
get{ return width; }
set{ width=value; }
}
private string borderwidth = "0";
public string BorderWidth
{
get{ return borderwidth; }
set{ borderwidth=value; }
}
//private int selectedindex;
public int SelectedIndex
{
get
{
if( null==ViewState[ this.Name + "_Index" ] )
ViewState[ this.Name + "_Index" ] = 0;
return (int)ViewState[ this.Name + "_Index" ];
}
set{ ViewState[ this.Name + "_Index" ]=value; }
}
public MenuItemControl SelectedItem
{
get
{
return (MenuItemControl)this.Controls[ this.SelectedIndex ];
}
}
private string height = "100%";
public string Height
{
get{ return height; }
set{ height=value; }
}
private string topoffset;
public string TopOffset
{
get{ return topoffset; }
set{ topoffset=value; }
}
private string leftoffset;
public string LeftOffset
{
get{ return leftoffset; }
set{ leftoffset=value; }
}
private int staticindex=-1;
public int StaticIndex
{
get{ return staticindex; }
set{ staticindex=(int)value; }
}
private ArrayList items = new ArrayList();
protected override void OnLoad( EventArgs e )
{
//
// This script code is now in client.js
//
//if( !Page.IsClientScriptBlockRegistered( "SideNav_ClientScript" ) )
//{
//Page.RegisterClientScriptBlock( "SideNav_ClientScript",MenuControl.ClientScript );
//}
if( !Page.IsPostBack )
{
if( null!=Request[ this.Name + "_Index" ] )
this.SelectedIndex = Convert.ToInt32( Request[ this.Name + "_Index" ] );
}
base.OnLoad( e );
}
protected override void Render( HtmlTextWriter output )
{
output.Write(
"
\r\n"
);
if( MenuType.Horizontal==this.Type )
{
output.Write(
"\r\n\t" );
this.RenderChildren( output );
output.Write(
"
\r\n"
);
}
else//vertical
{
output.Write(
" \r\n"+
" | \r\n" +
"
\r\n"
);
this.RenderChildren( output );
//render the tail of the menu
output.Write(
"
|
"
);
}
output.Write(
"
"
);
}
protected override void RenderChildren( HtmlTextWriter output )
{
this.EnsureChildControls();
for( int i=0;i=0 )
{
if( i==this.StaticIndex )
item.Selected=true;
else
item.Selected=false;
}
else
{
if( i==this.SelectedIndex )
item.Selected=true;
else
item.Selected=false;
}
if( null==item.NavigateUrl && MenuItemType.Item==item.Type )
{
item.NavigateUrl="javascript:"+Page.GetPostBackEventReference( this, i.ToString() );
}
}
base.RenderChildren( output );
}
protected override void AddParsedSubObject( object obj )
{
if( obj is MenuItemControl )
this.AddItem( (MenuItemControl)obj );
base.AddParsedSubObject( obj );
}
public int AddItem( MenuItemControl item )
{
if( null==item.Name )
item.Name = this.Name;
item.MenuType = this.Type;
item.LeftOffSet = this.LeftOffset;
return this.MenuItems.Add( item );
}
public void RaisePostBackEvent( string eventArgument )
{
int newIndex = Convert.ToInt32( eventArgument );
this.EnsureChildControls();
this.OnSelectedIndexChanged( new MenuEventArgs( newIndex ) );
}
protected void OnSelectedIndexChanged( MenuEventArgs e )
{
if( null!=SelectedIndexChanged )
this.SelectedIndexChanged( this,e );
}
private void MenuControl_SelectedIndexChanged( object sender, MenuEventArgs e )
{
this.SelectedIndex = e.Index;
}
public const string ClientScript = @"
";
}
public class MenuEventArgs : EventArgs
{
public MenuEventArgs( int newindex )
{
this.index = newindex;
}
private int index;
public int Index
{
get{ return index; }
}
}
public class MenuItemControlCollection : CollectionBase
{
public MenuItemControl this[ int index ]
{
get{ return (MenuItemControl)this.List[ index ]; }
set{ this.List[ index ] = value; }
}
protected internal int Add( MenuItemControl item )
{
return this.List.Add( item );
}
protected internal void Remove( MenuItemControl item )
{
this.List.Remove( item );
}
protected internal void Remove( int index )
{
this.List.RemoveAt( index );
}
}
[ParseChildren()]
public class MenuItemControl : UserControl
{
private ArrayList items = new ArrayList();
protected string Root
{
get{ return ( "/" == Request.ApplicationPath ) ? "" : Request.ApplicationPath; }
}
protected string Roots
{
get
{
return HyperLinkManager.GetSecureHyperLink( "" );
}
}
private bool requirehttps;
public bool RequireHttps
{
get{ return requirehttps; }
set{ requirehttps=value; }
}
private bool requirehttp;
public bool RequireHttp
{
get{ return requirehttp; }
set{ requirehttp=value; }
}
private string cssclass;
public string CssClass
{
get{ return cssclass; }
set{ cssclass=value; }
}
protected string root
{
get{ return HyperLinkManager.GetHyperLink( "" ); }
}
private bool selected;
public bool Selected
{
get{ return selected; }
set{ selected=value; }
}
private string text;
public string Text
{
get
{
if( null!=text )
{
if( text.Length>65 )
text=text.Substring( 0,65 ) + "...";
}
return text;
}
set{ text=value; }
}
private string navigateurl;
public string NavigateUrl
{
get{ return navigateurl; }
set{ navigateurl=value; }
}
private string navigatepage;
public string NavigatePage
{
get{ return navigatepage; }
set{ navigatepage=value; }
}
private string navigatetarget;
public string NavigateTarget
{
get{ return navigatetarget; }
set{ navigatetarget=value; }
}
private string name;
public string Name
{
get{ return name; }
set{ name=value; }
}
private MenuType menutype;
public MenuType MenuType
{
get{ return menutype; }
set{ menutype=(MenuType)value; }
}
private MenuItemType type;
public MenuItemType Type
{
get{ return type; }
set{ type=(MenuItemType)value;}
}
protected internal string LeftOffSet="0";
protected override void Render( HtmlTextWriter output )
{
if( MenuType.Vertical==this.MenuType )
{
RenderVertical( output );
}
else
{
RenderHorizontal( output );
}
}
protected void RenderVertical( HtmlTextWriter output )
{
output.Write(
"\r\n\t"
);
RenderHorizontal( output );
output.Write(
"
\r\n"
);
}
protected void RenderHorizontal( HtmlTextWriter output )
{
output.Write(
" | "
);
output.Write(
""
);
RenderText( output );
output.Write(
" | \r\n" );
}
protected void RenderText( HtmlTextWriter output )
{
switch( this.Type )
{
case MenuItemType.Item:
HyperLink l = new HyperLink();
l.Text = this.Text;
if( null!=this.NavigateTarget && ""!=this.NavigateTarget.Trim() )
l.Target = this.NavigateTarget;
string classname = this.Name + "_Text";
if( this.Selected )
{
classname += "Selected";
}
else
{
if( null!=this.NavigatePage )
{
if( RequireHttps )
{
l.NavigateUrl = HyperLinkManager.GetSecureHyperLink( NavigatePage );
}
else if( RequireHttp )
{
l.NavigateUrl = HyperLinkManager.GetNonSecureHyperLink( NavigatePage );
}
else
{
l.NavigateUrl = HyperLinkManager.GetHyperLink( NavigatePage );
}
}
else if( null!=this.NavigateUrl )
{
l.NavigateUrl = this.NavigateUrl;
}
}
l.CssClass=classname;
l.Target = this.NavigateTarget;
output.Write( "" );
l.RenderControl( output );
output.Write( "" );
break;
case MenuItemType.Separator:
Image i = new Image();
i.ImageUrl = Root + "/images/pixel.gif";
i.Height = new Unit( 1 );
i.Width = new Unit( 1 );
i.BorderWidth = new Unit( 0 );
i.RenderControl( output );
break;
case MenuItemType.Title:
Label label = new Label();
label.Text = this.Text;
output.Write( "" );
label.RenderControl( output );
output.Write( "" );
break;
}
}
public void RenderChildrenExt( HtmlTextWriter output )
{
EnsureChildControls();
RenderChildren( output );
}
}
public enum MenuType
{
Horizontal,
Vertical
}
public enum MenuItemType
{
Title,
Separator,
Item
}
}