123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using CB.Data;
- using CB.Entity;
- using CB.Common;
- using System.Text.RegularExpressions;
- namespace CB.TrendTool
- {
- public class TrendToolHtml
- {
- static string name = "#@@paraname", id = "#@@paraid", value = "#@@paravalue";
- static string content = "#@@paracontent";
- //未使用注释 static string tempTitle = "<span>#@@paravalue</span>";
- //未使用注释 static string tempRadio = "<input name='#@@paraname' id='#@@paraid' type='radio' value='#@@paravalue' /><label for='#@@paraid'>#@@paravalue</label>";
- //static string tempCheckbox = "<input name='#@@paraname' id='#@@paraid' type='checkbox' value='#@@paravalue'/><label for='#@@paraid'>#@@paravalue</label>";
- //未使用注释 static string tempspan = "<span for='#@@paraid' class='frame white' onclick='spanOnclick(this);'>#@@paravalue</span><input type='hidden' name='#@@paraname' id='#@@paraid' hiddenvalue='#@@paravalue' />";
- protected static Dictionary<int, string> TemplateHtml = new Dictionary<int, string>();
- TrendToolHtml()
- {
- }
- public static string GetToolsHtml(int PageID)
- {
- StringBuilder ToolsHtml = new StringBuilder();
- //List<TrendToolInfo> lists = TrendToolService.ToList(new TrendToolInfo() { PageID = PageID, Status = FilterStatus.Default }).ToList();
- List<TrendToolInfo> lists = Caches.GetTrendToolList(new TrendToolInfo() { PageID = PageID, Status = FilterStatus.Default }).ToList();
- List<TrendToolInfo> ParentLists = lists.FindAll(x => x.ParentID == -1).OrderBy(o => o.ToolOrder).ToList();
- foreach (TrendToolInfo parent in ParentLists)
- {
- List<TrendToolInfo> Items = lists.FindAll(x => x.PageID == parent.PageID).OrderBy(o => o.ParentID).OrderBy(o => o.ToolOrder).ToList();
- ToolsHtml.Append(GetToolHtml(parent.Id, Items));
- }
- return ToolsHtml.ToString();
- }
- public static string GetToolHtml(int ParentID, List<TrendToolInfo> ChildList)
- {
- StringBuilder ToolHtml = new StringBuilder();
- //生成父节点html
- TrendToolInfo parent = ChildList.Find(x => x.Id == ParentID);
- List<TrendToolInfo> Items = ChildList.FindAll(x => x.ParentID == ParentID).OrderBy(o => o.ToolOrder).ToList();
- ToolHtml.Append(GetHTML(parent, Items.Count > 0 ? true : false));
- //ChildList.Remove(parent);
- for (int i = 0; i < Items.Count; i++)
- {
- TrendToolInfo Child = Items[i];
- List<TrendToolInfo> ChildItems = ChildList.FindAll(x => x.ParentID == Child.Id);
- string ChildHtml = "";
- if (ChildItems.Count > 0)
- { ChildHtml = GetToolHtml(Child.Id, ChildList); }
- else
- { ChildHtml = GetHTML(Child, ChildItems.Count > 0 ? true : false); }
- ToolHtml.Replace(content, ChildHtml + (i + 1 == Items.Count ? "" : content));
- }
- return ToolHtml.ToString();
- }
- private static string GetHTML(TrendToolInfo entity, bool isCreateChildItem)
- {
- //如果实体默认有html,直接返回原有的html
- if (!string.IsNullOrEmpty(entity.HTML))
- { return entity.HTML.Trim(); }
- string hHeader = "#@@Title";
- string hContent = "#@@Content";
- string hFooter = "#@@Remark";
- string templateHtml = entity.TemplateHTML;
- //主体html
- List<string> items = new List<string>();
- if (!String.IsNullOrEmpty(entity.ItemValue.Trim()))
- { items = entity.ItemValue.Split(',').ToList(); }
- //if (items.Count != entity.ItemCount)
- //{ throw new Exception("元素总数和和配置的总数不匹配"); }
- //头部html
- if (!string.IsNullOrEmpty(entity.Title))
- { templateHtml = templateHtml.Replace(hHeader, entity.Title); }
- else
- { templateHtml = templateHtml.Replace(hHeader, ""); }
- //匹配{}中的字符串.该字符串会循环生成
- var match = Regex.Match(templateHtml, @"\{(.*)\}", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
- string cycleHtml = match.Groups[1].Value;
- templateHtml = templateHtml.Replace("{" + cycleHtml + "}", "");
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < items.Count; i++)
- {
- string MainHtml = "";
- if (string.IsNullOrEmpty(entity.HTML))
- {
- MainHtml = cycleHtml.Replace(value, items[i]);
- }
- else
- { MainHtml = entity.HTML; }
- MainHtml = MainHtml.Replace(id, ToolUtility.FilterNamePrefix + entity.Id + "_" + i.ToString());
- MainHtml = MainHtml.Replace(name, ToolUtility.FilterNamePrefix + entity.Id);
- sb.Append(MainHtml);
- }
- if (isCreateChildItem)
- { sb.Append(content); }
- templateHtml = templateHtml.Replace(hContent, sb.ToString());
- //尾部html
- if (!string.IsNullOrEmpty(entity.Remark))
- { templateHtml = templateHtml.Replace(hFooter, entity.Remark); }
- else
- { templateHtml = templateHtml.Replace(hFooter, ""); }
- templateHtml = templateHtml.Replace(name, ToolUtility.FilterNamePrefix + entity.Id);
-
- return templateHtml;
- }
- }
- }
|