/// <summary> /// Render an items tree for specified list /// </summary> /// <param name="list">The content list object</param> /// <param name="current">The content data item object.</param> /// <param name="htmlAttributes">The html attributes object.</param> /// <returns></returns> public static HelperResult Tree(ContentListDecorator list, ContentDataItemDecorator current = null, object htmlAttributes = null) { return(new HelperResult((w) => { using (var writer = new Html32TextWriter(w)) { writer.WriteBeginTag("ul"); writer.WriteAttribute("data-role", "tree"); if (htmlAttributes != null) { var dict = htmlAttributes.ToDictionary(); foreach (var key in dict.Keys) { if (key.StartsWith("data_")) { writer.WriteAttribute(key.Replace("_", "-"), (string)dict[key]); } else { writer.WriteAttribute(key, (string)dict[key]); } } } writer.Write(Html32TextWriter.TagRightChar); var items = list.GetItems(i => i.ParentItemID == Guid.Empty && i.IsPublished && i.IsCurrentVersion).OrderBy(i => i.Pos); RenderChildren(writer, list, items, current); writer.WriteEndTag("ul"); } })); }
public static void WriteValue(this Html32TextWriter w, string value, string classname) { w.WriteBeginTag("td"); w.WriteAttribute("class", classname); w.Write(">"); if (!string.IsNullOrEmpty(value)) { w.WriteEncodedText(value); } w.WriteEndTag("td"); }
private static void RenderChildren(Html32TextWriter writer, ContentListDecorator list, IEnumerable <ContentDataItem> items, ContentDataItem current) { var app = App.Get(); var fieldName = list.GetDefaultTitleField().Name; var Url = DNA.Utility.UrlUtility.CreateUrlHelper(); foreach (var item in items) { var itemWrapper = app.Wrap(item); writer.WriteBeginTag("li"); var _class = "d-node"; var children = itemWrapper.Children(); var childrenCount = children.Count(); if (childrenCount > 0) { _class += " d-node-hasChildren"; } //writer.WriteAttribute("class", "d-node d-node-hasChildren"); var isInPath = false; if (current != null && !string.IsNullOrEmpty(current.Path) && !string.IsNullOrEmpty(item.Path) && current.Path.StartsWith(item.Path)) { isInPath = true; if (childrenCount > 0) { writer.WriteAttribute("data-expanded", "true"); } } writer.WriteAttribute("data-id", itemWrapper.ID.ToString()); if (current != null && itemWrapper.ID.Equals(current.ID)) { writer.WriteAttribute("data-selected", "true"); } if (itemWrapper.ParentItemID != Guid.Empty) { writer.WriteAttribute("data-parentid", itemWrapper.ParentItemID.ToString()); } if (childrenCount > 0) { if (!isInPath) { var urlformat = "~/api/contents/items?name={0}&slug={1}&parentId={2}"; var popupUrl = Url.Content(string.Format(urlformat, list.Name, list.Views.Default.Name, itemWrapper.ID.ToString())); writer.WriteAttribute("data-popupurl", popupUrl); } } writer.WriteAttribute("class", _class); writer.Write(Html32TextWriter.TagRightChar); writer.WriteBeginTag("a"); writer.WriteAttribute("href", itemWrapper.UrlComponent); writer.Write(Html32TextWriter.TagRightChar); writer.Write(itemWrapper.Value(fieldName).Raw); writer.WriteEndTag("a"); if (childrenCount > 0) { if (isInPath) { writer.WriteFullBeginTag("ul"); RenderChildren(writer, list, children, current); writer.WriteEndTag("ul"); } } writer.WriteEndTag("li"); } }