123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- 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<AccordionTreeDataItem> 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<AccordionTreeDataItem> treeItem = new List<AccordionTreeDataItem>();
- 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("<div {0}>\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<div title='{1}' data-imgurl=\"{2}\"{3}>{0}</div>\r\n", treebind(it), it.MnuName, Url.Content(it.MnuPic), AccordionItemOptions);
- }
- else
- {
- htmlBuilder.AppendFormat("\t<div title='{1}' data-imgurl=\"" + Url.Content("~/Content/Bowin.Control.Core/themes/metro-blue/sysImages/dot.png") + "\" {2}>{0}</div>\r\n", treebind(it), it.MnuName, AccordionItemOptions);
- }
- });
- htmlBuilder.Append("</div>");
- 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("<ul{0}>", 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("<div><input id='{3}' onclick='CMSFunction.Tree.CheckBranch();' type='checkbox' name='{2}' value='{0}'/>{1}</div>", 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("<a href=\"{0}\" style=\"text-decoration:none;\" target=\"_blank\">{1}</a>", 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("<a href=\"javascript:{0}\" data-imgurl=\"{2}\" style=\"text-decoration:none;\">{1}</a>", treeContent, it.MnuName, Url.Content(it.MnuPic));
- }
- else
- {
- treeContent = string.Format("<a href=\"javascript:{0}\" style=\"text-decoration:none;\">{1}</a>", treeContent, it.MnuName);
- }
- }
- }
- else
- {
- treeContent = it.MnuName;
- }
- }
- htmltreeBuilder.AppendFormat("<li><span>{0}</span>{1}</li>", treeContent, childhtml);
- });
- htmltreeBuilder.Append("</ul>\r\n");
- }
- //else
- //{
- // if (isFirstNode)
- // {
- // htmltreeBuilder.Append("<p>{0}</p>\r\n");
- // }
- //}
- return htmltreeBuilder.ToString();
- }
- protected IDictionary<string, string> GetPropertyList()
- {
- var dictPropertyList = new Dictionary<string, string>();
- return dictPropertyList;
- }
- public static AccordionTree CreateControl(HtmlHelper htmlHelper, List<AccordionTreeDataItem> a_DataItemList, AccordionTreeOptions treeOptions, IDictionary<string, string> attributes = null)
- {
- AccordionTree tree = new AccordionTree();
- if (attributes != null)
- {
- tree.Attributes = attributes;
- }
- a_DataItemList = a_DataItemList ?? new List<AccordionTreeDataItem>();
- 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;
- }
- }
- }
|