📄 tabs.cs
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
// Copyright (c) Telligent Systems Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Xml;
using System.Xml.Serialization;
using CommunityServer.Components;
using System.Text;
//NOTE: This should be refactored into a "real" control. For now, it simply renders the markup for our admin tabs.
namespace CommunityServer.Controls
{
/// <summary>
/// Renders a Tab + Submenus based on the tab configuration file.
/// </summary>
public class TabControl : Control
{
#region Private Members
private string _selectedTab;
private string _fileLocation;
private bool _showChildren = true;
#endregion
#region Public Properties
/// <summary>
/// Returns the currently selected tab
/// </summary>
public string SelectedTab
{
get { return this._selectedTab; }
set { this._selectedTab = value; }
}
/// <summary>
/// Controls if Child tabs are displayed below the selected parent.
/// </summary>
public bool ShowChildren
{
get { return this._showChildren; }
set { this._showChildren = value; }
}
/// <summary>
/// returns the location of the current tab configuration file
/// </summary>
public string FileLocation
{
get
{
if(_fileLocation == null)
_fileLocation = "~/admin/tab.config";
return this._fileLocation;
}
set { this._fileLocation = value; }
}
#endregion
#region GetTabs()
/// <summary>
/// Returns the current instance of the TabCollection
/// </summary>
/// <returns></returns>
protected TabCollection GetTabs()
{
string path = Context.Server.MapPath(FileLocation);
TabCollection tc = CSCache.Get(path) as TabCollection;
if(tc == null)
{
tc = (TabCollection)Serializer.ConvertFileToObject(path,typeof(TabCollection));
CSCache.Max(path,tc,new System.Web.Caching.CacheDependency(path));
}
return tc;
}
#endregion
#region Tab Helpers
//NOTE: clean up the filtering
/// <summary>
/// Allows tabs to removed/not included on other tabs
/// </summary>
protected bool IsNotFiltered(Tab child)
{
if(child.Filter == null)
return true;
string[] filters = child.Filter.Split(',');
foreach(string filter in filters)
{
if(string.Compare(filter,this.SelectedTab,true)==0)
return false;
}
return true;
}
/// <summary>
/// Checks to see if the current tab has an Roles and if the current user is a member any
/// of the roles
/// </summary>
protected bool IncludeLink(Tab t)
{
if(t.HasRoles)
{
string[] roles = t.Roles.Split(';');
foreach(string role in roles)
{
if(Context.User.IsInRole(role))
return true;
}
return false;
}
return true;
}
/// <summary>
/// Resolves the current url and attempts to append the specified querystring
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
protected string FormatLink(Tab t)
{
if(t.Href == null)
return Context.Request.Url.ToString();
string url = null;
if(t.HasQueryString)
url = string.Format(t.Href,Context.Request.QueryString[t.QueryString]);
else
url = t.Href;
return ResolveUrl(url);
}
#endregion
#region GetState
/// <summary>
/// Walks the tab and it's children to see if any of them are currently selected
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
private SelectedState GetState(Tab t)
{
//Check the parent
if(string.Compare(t.Name,SelectedTab,true,System.Globalization.CultureInfo.InvariantCulture) == 0)
return SelectedState.Parent;
//Walk each of the child tabs
if(t.HasChildren)
{
foreach(Tab child in t.Children)
{
if(string.Compare(child.Name,this.SelectedTab,true,System.Globalization.CultureInfo.InvariantCulture) == 0)
return SelectedState.Child;
else if(child.HasChildren)
{
foreach(Tab cc in child.Children)
if(string.Compare(cc.Name,this.SelectedTab,true,System.Globalization.CultureInfo.InvariantCulture) == 0)
return SelectedState.ChildChild;
}
}
}
//Nothing here is selected
return SelectedState.Not;
}
#endregion
#region CreateTabMarkUp
/// <summary>
/// Creates the markup for the current TabCollection
/// </summary>
/// <returns></returns>
protected string CreateTabMarkUp()
{
//Get the tab data
TabCollection tc = GetTabs();
StringBuilder sb = new StringBuilder();
//Open the primary UL
sb.Append("<ul id=\"primary\">");
//Walk the parents
foreach(Tab parent in tc.Tabs)
{
//Is this parent enabled? Check Permissions
if(parent.Enabled && IncludeLink(parent))
{
//Check to see if/how this is tab is selected
SelectedState state = GetState(parent);
if(state == SelectedState.Not)
{
//Not selected, just render a normal link
sb.AppendFormat("<li><a href=\"{0}\">{1}</a></li>",FormatLink(parent),parent.Text);
}
else
{
//Something here is selected
//Parent is rendered in normal span tags.
sb.AppendFormat("<li><a class=\"selectedtab\" href=\"{0}\">{1}</a>",FormatLink(parent),parent.Text);
//Is there any child tabs? Do we want to display them?
if(this.ShowChildren && parent.HasChildren)
{
//Start child UL
sb.Append("<ul id=\"secondary\">");
foreach(Tab child in parent.Children)
{
//Make sure this tab is OK to display
if(child.Enabled && IncludeLink(child) && IsNotFiltered(child))
{
//open li
sb.Append("<li>");
//if the parent is selected, we dispaly links to all child tabs
// if(state == SelectedState.Parent || string.Compare(child.Name,this.SelectedTab,true,CultureInfo.InvariantCulture) != 0)
// {
sb.AppendFormat("<a href=\"{0}\">{1}</a>",FormatLink(child),child.Text);
// }
// else
// {
// //This child tab is selected, do not display a link
// sb.AppendFormat("<span>{0}</span>",child.Text);
// }
//close li
sb.Append("</li>");
}
}
//close secondary UL
sb.Append("</ul>");
}
}
}
}
sb.Append("</ul>");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -