/// <summary> /// Render an items tree by specified item query result. /// </summary> /// <param name="items">The item query result.</param> /// <param name="currentPath">The current data item path.</param> /// <param name="htmlAttributes">The html attributes for treeview element.</param> /// <param name="dynamicLoad">Specified the tree node load on demand.</param> /// <returns></returns> public static HelperResult Tree(ContentQueryResult items, string currentPath = "", object htmlAttributes = null, bool dynamicLoad = true) { return(new HelperResult((w) => { var root = new XElement("ul", new XAttribute("data-role", "tree")); root.AddHtmlAttributes(htmlAttributes); AddChildren(root, items, items.View, currentPath); w.Write(root.OuterXml()); })); }
/// <summary> /// Add children nodes to specified parent element. /// </summary> /// <param name="parent">The parent xml element.</param> /// <param name="results">The item query result.</param> /// <param name="view">The view object</param> /// <param name="currentPath">The current data item path.</param> /// <param name="dynamicLoad">Specified the tree node load on demand.</param> public static void AddChildren(XElement parent, ContentQueryResult results, ContentViewDecorator view, string currentPath, bool dynamicLoad = true) { var Url = DNA.Utility.UrlUtility.CreateUrlHelper(); var fieldName = view.Parent.GetDefaultTitleField().Name; foreach (var item in results) { var hasChildren = item.HasChildren; var element = new XElement("li"); parent.Add(element); if (hasChildren) element.Add(new XAttribute("class", "d-node d-node-hasChildren")); else element.Add(new XAttribute("class", "d-node")); var isInPath = false; if (!string.IsNullOrEmpty(currentPath) && !string.IsNullOrEmpty(item.Path) && currentPath.StartsWith(item.Path)) { isInPath = true; if (hasChildren) element.Add(new XAttribute("data-expanded", "true")); if (item.Path==currentPath) element.Add(new XAttribute("data-selected", "true")); } element.Add(new XAttribute("data-id", item.ID.ToString())); if (item.ParentID != Guid.Empty) element.Add(new XAttribute("data-parentid", item.ParentID.ToString())); var linkElement = new XElement("a", new XAttribute("href", item.UrlComponent), item[fieldName]); element.Add(linkElement); if (hasChildren) { if (!isInPath && dynamicLoad) { var urlformat = "~/api/{0}/contents/items?name={1}&slug={2}&parentId={3}"; var popupUrl = Url.Content(string.Format(urlformat, view.Parent.Web.Name, view.Parent.Name, view.Name, item.ID.ToString())); element.Add(new XAttribute("data-popupurl", popupUrl)); } else { var childrenElement = new XElement("ul"); element.Add(childrenElement); AddChildren(childrenElement, view.Items(string.Format("parentId='{0}'", item.ID.ToString())), view, currentPath); } } } }
/// <summary> /// Renders the data items in ContentQueryResult with view template what defined in result's view. /// </summary> /// <param name="html">The html helper object.</param> /// <param name="results">The content query result object.</param> /// <returns></returns> public static HelperResult RenderViewTemplate(this HtmlHelper html, ContentQueryResult results) { return new HelperResult(w => { var view = results.View; var body = view.GetBodyTemplate(); if (!string.IsNullOrEmpty(body.Text)) { #region xslt if (body.ContentType == TemplateContentTypes.Xslt) { var doc = new XDocument(results.Element()); var xslt = new XslCompiledTransform(); if (!string.IsNullOrEmpty(body.Text)) { try { xslt.Load(XmlReader.Create(new StringReader(body.Text))); var args = new XsltArgumentList(); args.AddParam("appPath", "", App.Get().Context.AppUrl.ToString()); args.AddParam("web", "", view.Parent.Web.Name); args.AddParam("lang", "", view.Parent.Locale); args.AddParam("list", "", view.Parent.Name); args.AddParam("view", "", view.Name); xslt.Transform(doc.CreateReader(), args, w); } catch { } } return; } #endregion #region razor if (body.ContentType == TemplateContentTypes.Razor) { var viewFile = TemplateHelper.SaveAsView(view.Parent, body.Text, string.Format("_view_{0}_tmpl.cshtml", view.Name)); html.RenderPartial(viewFile); return; } #endregion #region xml if (body.ContentType == TemplateContentTypes.Xml) { var doc = XDocument.Parse("<dispPattern>" + body.Text + "</dispPattern>"); var elements = doc.Root.Elements(); foreach (var element in elements) { if (element.Name == "rows") { //var rows = model.Items; foreach (var row in results) { RenderRowPattern(element, w, row); } continue; } if (element.Name.Equals("col")) { RenderColPattern(element, w, view.Parent); continue; } if (element.Name == "html") { w.Write(element.Value); continue; } } return; } #endregion } }); }
/// <summary> /// Render the content query results to HTML. /// </summary> /// <param name="html">The html helper object.</param> /// <param name="results">The query results object.</param> public static void RenderQueryResults(this HtmlHelper html, ContentQueryResult results) { html.RenderPartial("~/Views/Contents/_ServerView.cshtml", results); if (results.View != null && results.View.AllowPaging) html.ViewContext.Writer.Write(html.Pager(results)); //html.ViewContext.HttpContext.Response.Write(); }
/// <summary> /// Render HTML pager element for specified content query result. /// </summary> /// <param name="html">The html helper object.</param> /// <param name="model"></param> /// <param name="htmlAttributes">The html attributes object for the output html element.</param> /// <returns></returns> public static HelperResult Pager(this HtmlHelper html, ContentQueryResult model, object htmlAttributes = null) { return new HelperResult(writer => { var Url = new UrlHelper(html.ViewContext.RequestContext); if (model.View.AllowPaging) { var maxPageCount = 10; var curIndex = model.Query.Index; var startIndex = 1; var endIndex = maxPageCount * startIndex; var curPage = (int)Math.Ceiling((decimal)((decimal)curIndex / (decimal)maxPageCount)); if (curPage > 1) startIndex = (curPage * maxPageCount) - maxPageCount + 1; endIndex = startIndex + maxPageCount; if (endIndex > model.Query.TotalPages) endIndex = model.Query.TotalPages; var _params = new List<string>(); var queryString = html.ViewContext.RequestContext.HttpContext.Request.QueryString; if (queryString != null) { foreach (var key in queryString.AllKeys) { if (key.Equals("index", StringComparison.OrdinalIgnoreCase) || key.Equals("size", StringComparison.OrdinalIgnoreCase)) continue; _params.Add(string.Format("{0}={1}", key, queryString[key])); } } if (model.Query.TotalPages <=1) return; var isLast = curPage * maxPageCount >= model.Query.TotalPages; var isFirst = startIndex == 1; var builder = new UriBuilder(); if (isLast) endIndex++; var pageElement = new XElement("div", new XAttribute("data-role", "pager"), new XAttribute("data-index", curIndex - 1)); pageElement.AddHtmlAttributes(htmlAttributes); if (!isFirst) { pageElement.Add( new XElement("a", new XAttribute("href", Url.BuildUrl(model.View.Url, _params)), new XAttribute("title", "Go to first page"), new XAttribute("class", "d-ui-widget d-button"), new XElement("span", new XAttribute("class", "d-icon d-icon-first"))), new XElement("a", new XAttribute("href", Url.Content(model.View.Url) + "?index=" + (curIndex - 1).ToString() + "&size=" + model.Query.Size + (_params.Count > 0 ? ("&" + string.Join("&", _params.ToArray())) : "")), new XAttribute("title", "Go to prev page"), new XAttribute("class", "d-ui-widget d-button"), new XElement("span", new XAttribute("class", "d-icon d-icon-prev"))) ); } for (int i = startIndex; i < endIndex; i++) { var pagerItem = new XElement("a", i != curIndex ? i : curIndex); //if (i == curIndex) // pagerItem.Add(new XAttribute("class", "d-ui-widget d-button d-state-active")); //else pagerItem.Add(new XAttribute("class", "d-ui-widget d-button")); if (i != curIndex) pagerItem.Add(new XAttribute("href", Url.Content(model.View.Url) + "?index=" + i.ToString() + "&size=" + model.Query.Size + (_params.Count > 0 ? ("&" + string.Join("&", _params.ToArray())) : ""))); pageElement.Add(pagerItem); } if (!isLast) { pageElement.Add( new XElement("a", new XAttribute("href", Url.Content(model.View.Url) + "?index=" + (curIndex + 1).ToString() + "&size=" + model.Query.Size + (_params.Count > 0 ? ("&" + string.Join("&", _params.ToArray())) : "")), new XAttribute("title", "Go to next page"), new XAttribute("class", "d-ui-widget d-button"), new XElement("span", new XAttribute("class", "d-icon d-icon-next"))), new XElement("a", new XAttribute("href", Url.Content(model.View.Url) + "?index=" + (model.Query.TotalPages).ToString() + "&size=" + model.Query.Size + (_params.Count > 0 ? ("&" + string.Join("&", _params.ToArray())) : "")), new XAttribute("title", "Go to last page"), new XAttribute("class", "d-ui-widget d-button"), new XElement("span", new XAttribute("class", "d-icon d-icon-last"))) ); } writer.Write(pageElement.OuterXml()); } }); }
/// <summary> /// Render an items tree by specified item query result. /// </summary> /// <param name="items">The item query result.</param> /// <param name="currentPath">The current data item path.</param> /// <param name="htmlAttributes">The html attributes for treeview element.</param> /// <param name="dynamicLoad">Specified the tree node load on demand.</param> /// <returns></returns> public static HelperResult Tree(ContentQueryResult items, string currentPath = "", object htmlAttributes = null, bool dynamicLoad = true) { return new HelperResult((w) => { var root = new XElement("ul", new XAttribute("data-role", "tree")); root.AddHtmlAttributes(htmlAttributes); AddChildren(root, items, items.View, currentPath); w.Write(root.OuterXml()); }); }
/// <summary> /// Add children nodes to specified parent element. /// </summary> /// <param name="parent">The parent xml element.</param> /// <param name="results">The item query result.</param> /// <param name="view">The view object</param> /// <param name="currentPath">The current data item path.</param> /// <param name="dynamicLoad">Specified the tree node load on demand.</param> public static void AddChildren(XElement parent, ContentQueryResult results, ContentViewDecorator view, string currentPath, bool dynamicLoad = true) { var Url = DNA.Utility.UrlUtility.CreateUrlHelper(); var fieldName = view.Parent.GetDefaultTitleField().Name; foreach (var item in results) { var hasChildren = item.HasChildren; var element = new XElement("li"); parent.Add(element); if (hasChildren) { element.Add(new XAttribute("class", "d-node d-node-hasChildren")); } else { element.Add(new XAttribute("class", "d-node")); } var isInPath = false; if (!string.IsNullOrEmpty(currentPath) && !string.IsNullOrEmpty(item.Path) && currentPath.StartsWith(item.Path)) { isInPath = true; if (hasChildren) { element.Add(new XAttribute("data-expanded", "true")); } if (item.Path == currentPath) { element.Add(new XAttribute("data-selected", "true")); } } element.Add(new XAttribute("data-id", item.ID.ToString())); if (item.ParentID != Guid.Empty) { element.Add(new XAttribute("data-parentid", item.ParentID.ToString())); } var linkElement = new XElement("a", new XAttribute("href", item.UrlComponent), item[fieldName]); element.Add(linkElement); if (hasChildren) { if (!isInPath && dynamicLoad) { var urlformat = "~/api/{0}/contents/items?name={1}&slug={2}&parentId={3}"; var popupUrl = Url.Content(string.Format(urlformat, view.Parent.Web.Name, view.Parent.Name, view.Name, item.ID.ToString())); element.Add(new XAttribute("data-popupurl", popupUrl)); } else { var childrenElement = new XElement("ul"); element.Add(childrenElement); AddChildren(childrenElement, view.Items(string.Format("parentId='{0}'", item.ID.ToString())), view, currentPath); } } } }