/// <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);
                    }
                }
            }
        }
Beispiel #2
0
 public ActionResult ValidateFilter(int id, string filter)
 {
     if (!string.IsNullOrEmpty(filter))
     {
         var view = App.Get().DataContext.Find<ContentView>(id);
         var wrapper = new ContentViewDecorator(view, App.Get().DataContext);
         try
         {
             wrapper.Items(filter);
         }
         catch (Exception e)
         {
             return Json(false, JsonRequestBehavior.AllowGet);
         }
     }
     return Json(true, JsonRequestBehavior.AllowGet);
 }
 /// <summary>
 /// Render an items tree for specified view 
 /// </summary>
 /// <param name="view">The data view object to get data items .</param>
 /// <param name="parentID">The root data item id.</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(ContentViewDecorator view, string parentID = "", string currentPath = "", object htmlAttributes = null, bool dynamicLoad = true)
 {
     var items = view.Items(string.Format("parentId='{0}'", string.IsNullOrEmpty(parentID) ? Guid.Empty.ToString() : parentID));
      return Tree(items, currentPath, htmlAttributes, dynamicLoad);
 }