AccordionTree.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Web.Mvc;
  6. namespace Bowin.Web.Controls.Mvc
  7. {
  8. public class AccordionTree : BaseControl
  9. {
  10. public string OnClick { get; set; }
  11. //public string IconCls { get; set; }
  12. private string renderCssClass { get; set; }
  13. private string renderDataItemCss { get; set; }
  14. public string DateItemCss { get; set; }
  15. public string DataOptions { get; set; }
  16. public string AccordionItemOptions { get; set; }
  17. public List<AccordionTreeDataItem> DataItemList { get; set; }
  18. public UrlHelper Url { get; set; }
  19. public bool? IsNormalTree { get; set; }
  20. public bool? IsCheckTree { get; set; }
  21. public override string Render()
  22. {
  23. this.renderCssClass = CssClass;
  24. this.renderDataItemCss = DateItemCss;
  25. if (IsNormalTree.HasValue && IsNormalTree.Value)
  26. {
  27. return RenderNormalTree();
  28. }
  29. else
  30. {
  31. return RenderAccordionTree();
  32. }
  33. }
  34. private string RenderNormalTree()
  35. {
  36. this.renderDataItemCss = this.renderCssClass;
  37. List<AccordionTreeDataItem> treeItem = new List<AccordionTreeDataItem>();
  38. string mainMnuId = Guid.NewGuid().ToString();
  39. treeItem.AddRange(DataItemList);
  40. treeItem.ForEach(x => {
  41. if (string.IsNullOrEmpty(x.ParentMnuId))
  42. {
  43. x.ParentMnuId = mainMnuId;
  44. }
  45. });
  46. return treebind(new AccordionTreeDataItem { MnuId = mainMnuId, ParentMnuId = null });
  47. }
  48. private string RenderAccordionTree()
  49. {
  50. StringBuilder htmlBuilder = new StringBuilder();
  51. string attributes = "";
  52. attributes += string.Format(" class=\"easyui-accordion {0}\"", this.renderCssClass);
  53. attributes += string.Format(" data-options=\"{0}\"", DataOptions);
  54. htmlBuilder.AppendFormat("<div {0}>\r\n", attributes);
  55. var AccordionItems = DataItemList.Where(w => string.IsNullOrEmpty(w.ParentMnuId)).OrderBy(o => o.MnuOrder).ToList();
  56. AccordionItems.ForEach(it =>
  57. {
  58. if (it.MnuPic != null)
  59. {
  60. htmlBuilder.AppendFormat("\t<div title='{1}' data-imgurl=\"{2}\"{3}>{0}</div>\r\n", treebind(it), it.MnuName, Url.Content(it.MnuPic), AccordionItemOptions);
  61. }
  62. else
  63. {
  64. 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);
  65. }
  66. });
  67. htmlBuilder.Append("</div>");
  68. return htmlBuilder.ToString();
  69. }
  70. private string treebind(AccordionTreeDataItem cDataItem)
  71. {
  72. StringBuilder htmltreeBuilder = new StringBuilder();
  73. bool isFirstNode = string.IsNullOrWhiteSpace(cDataItem.ParentMnuId);
  74. var treeItems = DataItemList.Where(w => w.ParentMnuId == cDataItem.MnuId).OrderBy(o => o.MnuOrder).ToList();
  75. if (treeItems.Count > 0)
  76. {
  77. htmltreeBuilder.AppendFormat("<ul{0}>", isFirstNode ? string.Format(" class=\"easyui-tree {0}\" data-options=\"onClick: CMSFunction.Tree.OnClick\"", this.renderDataItemCss) : "");
  78. treeItems.ForEach(it =>
  79. {
  80. string childhtml = treebind(it);
  81. string treeContent = "";
  82. if (IsCheckTree.HasValue && IsCheckTree.Value)
  83. {
  84. 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);
  85. }
  86. else
  87. {
  88. if (!string.IsNullOrWhiteSpace(OnClick) && !string.IsNullOrWhiteSpace(it.MnuProgram))
  89. {
  90. if (it.MnuProgram.ToLower().StartsWith("http://"))
  91. {
  92. treeContent = string.Format("<a href=\"{0}\" style=\"text-decoration:none;\" target=\"_blank\">{1}</a>", it.MnuProgram, it.MnuName);
  93. }
  94. else
  95. {
  96. treeContent = OnClick.Replace("{MnuId}", it.MnuId).Replace("{MnuProgram}", Url.Content("~" + it.MnuProgram)).Replace("{MnuName}", it.MnuName);
  97. if (it.MnuPic != null)
  98. {
  99. treeContent = string.Format("<a href=\"javascript:{0}\" data-imgurl=\"{2}\" style=\"text-decoration:none;\">{1}</a>", treeContent, it.MnuName, Url.Content(it.MnuPic));
  100. }
  101. else
  102. {
  103. treeContent = string.Format("<a href=\"javascript:{0}\" style=\"text-decoration:none;\">{1}</a>", treeContent, it.MnuName);
  104. }
  105. }
  106. }
  107. else
  108. {
  109. treeContent = it.MnuName;
  110. }
  111. }
  112. htmltreeBuilder.AppendFormat("<li><span>{0}</span>{1}</li>", treeContent, childhtml);
  113. });
  114. htmltreeBuilder.Append("</ul>\r\n");
  115. }
  116. //else
  117. //{
  118. // if (isFirstNode)
  119. // {
  120. // htmltreeBuilder.Append("<p>{0}</p>\r\n");
  121. // }
  122. //}
  123. return htmltreeBuilder.ToString();
  124. }
  125. protected IDictionary<string, string> GetPropertyList()
  126. {
  127. var dictPropertyList = new Dictionary<string, string>();
  128. return dictPropertyList;
  129. }
  130. public static AccordionTree CreateControl(HtmlHelper htmlHelper, List<AccordionTreeDataItem> a_DataItemList, AccordionTreeOptions treeOptions, IDictionary<string, string> attributes = null)
  131. {
  132. AccordionTree tree = new AccordionTree();
  133. if (attributes != null)
  134. {
  135. tree.Attributes = attributes;
  136. }
  137. a_DataItemList = a_DataItemList ?? new List<AccordionTreeDataItem>();
  138. treeOptions = treeOptions ?? new AccordionTreeOptions();
  139. //tree.CssClass = string.IsNullOrWhiteSpace(treeOptions.CssClass) ? "easyui-accordion" : treeOptions.CssClass;
  140. tree.CssClass = treeOptions.CssClass;
  141. tree.OnClick = treeOptions.OnClick;
  142. //tree.IconCls = treeOptions.IconCls;
  143. //tree.DateItemCss = string.IsNullOrWhiteSpace(treeOptions.DateItemCss) ? "easyui-tree" : treeOptions.DateItemCss;
  144. tree.DateItemCss = treeOptions.DateItemCss;
  145. tree.DataOptions = string.IsNullOrWhiteSpace(treeOptions.DataOptions) ? "fit:true" : treeOptions.DataOptions;
  146. tree.AccordionItemOptions = string.IsNullOrWhiteSpace(treeOptions.AccordionItemOptions) ? "" : string.Format(" data-options='{0}'", treeOptions.AccordionItemOptions);
  147. tree.DataItemList = a_DataItemList;
  148. tree.Url = new UrlHelper(htmlHelper.ViewContext.RequestContext);
  149. tree.IsNormalTree = treeOptions.IsNormalTree;
  150. tree.IsCheckTree = treeOptions.IsCheckTree;
  151. return tree;
  152. }
  153. }
  154. }