using System; using System.Data; using System.Configuration; using System.Collections; using System.Collections.Generic; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Text; namespace ScrewTurn.Wiki { public partial class ClientImageBrowser : System.Web.UI.UserControl { private const string ClientBrowserItems = "ClientBrowserItems"; private string leafCssClass = ""; private string nodeCssClass = ""; private string nodeContent = ""; private string upCssClass = ""; private string upLevelContent = ""; protected void Page_Load(object sender, EventArgs e) { Render(); } /// /// Removes all the items in the browser and re-populates it. /// public void PopulateBrowser() { if(Populate == null) ViewState[ClientBrowserItems] = new List(); else ViewState[ClientBrowserItems] = Populate(this, new PopulateEventArgs()); Render(); } private void Render() { lblStrings.Text = string.Format("", BuildSubTreeContainerID(0)); List items = (List)ViewState[ClientBrowserItems]; if(items == null) return; StringBuilder sb = new StringBuilder(); sb.Append(@"
"); int iteration = 0; RenderSubTree(items, sb, ref iteration, BuildSubTreeContainerID(0)); sb.Append("
"); lblContent.Text = sb.ToString(); } private void RenderSubTree(List items, StringBuilder sb, ref int iteration, string parentId) { // This method generates the client markup and JavaScript that contains a tree of images // The sub-trees are NOT rendered as nested elements StringBuilder temp = new StringBuilder(); string id = BuildSubTreeContainerID(iteration); sb.AppendFormat(@"
", id, (iteration != 0 ? @" style=""display: none;""" : "")); if(iteration != 0) { sb.AppendFormat(@"", upCssClass, parentId, Properties.Messages.UpLevel, upLevelContent + Properties.Messages.UpLevel); } foreach(TreeElement item in items) { iteration++; // Before invoking RenderSubTree recursively! // Render item RenderItem(item, sb, iteration); if(item.SubItems.Count > 0) { RenderSubTree(item.SubItems, temp, ref iteration, id); } } sb.Append("
"); sb.Append(temp.ToString()); } private void RenderItem(TreeElement item, StringBuilder sb, int iteration) { if(item.SubItems.Count > 0) { // Expanding link string containerId = BuildSubTreeContainerID(iteration); sb.AppendFormat(@"
{3}
", nodeCssClass, containerId, item.Name, nodeContent + item.Text); } else { // Action link sb.AppendFormat(@"
{3}
", leafCssClass, item.OnClientClick, item.Name, item.Text); } } private string BuildSubTreeContainerID(int iteration) { return string.Format("sub_{0}_{1}", ID, iteration); } /// /// Gets or sets the CSS Class for leaf items. /// public string LeafCssClass { get { return leafCssClass; } set { leafCssClass = value; } } /// /// Gets or sets the CSS Class for folder items. /// public string NodeCssClass { get { return nodeCssClass; } set { nodeCssClass = value; } } /// /// Gets or sets the content for folder items. /// public string NodeContent { get { return nodeContent; } set { nodeContent = value; } } /// /// Gets or sets the CSS Class for "Up one level" nodes. /// public string UpCssClass { get { return upCssClass; } set { upCssClass = value; } } /// /// Gets or sets the content for "Up one level" nodes. /// public string UpLevelContent { get { return upLevelContent; } set { upLevelContent = value; } } /// /// Delegate used for handling the Populate event. /// /// The object that fired the event. /// The event arguments. /// A list of items contained in the expanded sub-tree. public delegate List PopulateEventHandler(object sender, PopulateEventArgs e); /// /// Occurs when a sub-tree is populated. /// public event PopulateEventHandler Populate; } }