using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; namespace Bowin.Web.Controls.Mvc { public class AccordionTree : BaseControl { public string OnClick { get; set; } //public string IconCls { get; set; } private string renderCssClass { get; set; } private string renderDataItemCss { get; set; } public string DateItemCss { get; set; } public string DataOptions { get; set; } public string AccordionItemOptions { get; set; } public List DataItemList { get; set; } public UrlHelper Url { get; set; } public bool? IsNormalTree { get; set; } public bool? IsCheckTree { get; set; } public override string Render() { this.renderCssClass = CssClass; this.renderDataItemCss = DateItemCss; if (IsNormalTree.HasValue && IsNormalTree.Value) { return RenderNormalTree(); } else { return RenderAccordionTree(); } } private string RenderNormalTree() { this.renderDataItemCss = this.renderCssClass; List treeItem = new List(); string mainMnuId = Guid.NewGuid().ToString(); treeItem.AddRange(DataItemList); treeItem.ForEach(x => { if (string.IsNullOrEmpty(x.ParentMnuId)) { x.ParentMnuId = mainMnuId; } }); return treebind(new AccordionTreeDataItem { MnuId = mainMnuId, ParentMnuId = null }); } private string RenderAccordionTree() { StringBuilder htmlBuilder = new StringBuilder(); string attributes = ""; attributes += string.Format(" class=\"easyui-accordion {0}\"", this.renderCssClass); attributes += string.Format(" data-options=\"{0}\"", DataOptions); htmlBuilder.AppendFormat("
\r\n", attributes); var AccordionItems = DataItemList.Where(w => string.IsNullOrEmpty(w.ParentMnuId)).OrderBy(o => o.MnuOrder).ToList(); AccordionItems.ForEach(it => { if (it.MnuPic != null) { htmlBuilder.AppendFormat("\t
{0}
\r\n", treebind(it), it.MnuName, Url.Content(it.MnuPic), AccordionItemOptions); } else { htmlBuilder.AppendFormat("\t
{0}
\r\n", treebind(it), it.MnuName, AccordionItemOptions); } }); htmlBuilder.Append("
"); return htmlBuilder.ToString(); } private string treebind(AccordionTreeDataItem cDataItem) { StringBuilder htmltreeBuilder = new StringBuilder(); bool isFirstNode = string.IsNullOrWhiteSpace(cDataItem.ParentMnuId); var treeItems = DataItemList.Where(w => w.ParentMnuId == cDataItem.MnuId).OrderBy(o => o.MnuOrder).ToList(); if (treeItems.Count > 0) { htmltreeBuilder.AppendFormat("", isFirstNode ? string.Format(" class=\"easyui-tree {0}\" data-options=\"onClick: CMSFunction.Tree.OnClick\"", this.renderDataItemCss) : ""); treeItems.ForEach(it => { string childhtml = treebind(it); string treeContent = ""; if (IsCheckTree.HasValue && IsCheckTree.Value) { treeContent = string.Format("
{1}
", it.MnuId, it.MnuName, this.Name, this.ID + "_" + it.MnuId); } else { if (!string.IsNullOrWhiteSpace(OnClick) && !string.IsNullOrWhiteSpace(it.MnuProgram)) { if (it.MnuProgram.ToLower().StartsWith("http://")) { treeContent = string.Format("{1}", it.MnuProgram, it.MnuName); } else { treeContent = OnClick.Replace("{MnuId}", it.MnuId).Replace("{MnuProgram}", Url.Content("~" + it.MnuProgram)).Replace("{MnuName}", it.MnuName); if (it.MnuPic != null) { treeContent = string.Format("{1}", treeContent, it.MnuName, Url.Content(it.MnuPic)); } else { treeContent = string.Format("{1}", treeContent, it.MnuName); } } } else { treeContent = it.MnuName; } } htmltreeBuilder.AppendFormat("
  • {0}{1}
  • ", treeContent, childhtml); }); htmltreeBuilder.Append("\r\n"); } //else //{ // if (isFirstNode) // { // htmltreeBuilder.Append("

    {0}

    \r\n"); // } //} return htmltreeBuilder.ToString(); } protected IDictionary GetPropertyList() { var dictPropertyList = new Dictionary(); return dictPropertyList; } public static AccordionTree CreateControl(HtmlHelper htmlHelper, List a_DataItemList, AccordionTreeOptions treeOptions, IDictionary attributes = null) { AccordionTree tree = new AccordionTree(); if (attributes != null) { tree.Attributes = attributes; } a_DataItemList = a_DataItemList ?? new List(); treeOptions = treeOptions ?? new AccordionTreeOptions(); //tree.CssClass = string.IsNullOrWhiteSpace(treeOptions.CssClass) ? "easyui-accordion" : treeOptions.CssClass; tree.CssClass = treeOptions.CssClass; tree.OnClick = treeOptions.OnClick; //tree.IconCls = treeOptions.IconCls; //tree.DateItemCss = string.IsNullOrWhiteSpace(treeOptions.DateItemCss) ? "easyui-tree" : treeOptions.DateItemCss; tree.DateItemCss = treeOptions.DateItemCss; tree.DataOptions = string.IsNullOrWhiteSpace(treeOptions.DataOptions) ? "fit:true" : treeOptions.DataOptions; tree.AccordionItemOptions = string.IsNullOrWhiteSpace(treeOptions.AccordionItemOptions) ? "" : string.Format(" data-options='{0}'", treeOptions.AccordionItemOptions); tree.DataItemList = a_DataItemList; tree.Url = new UrlHelper(htmlHelper.ViewContext.RequestContext); tree.IsNormalTree = treeOptions.IsNormalTree; tree.IsCheckTree = treeOptions.IsCheckTree; return tree; } } }