コード例 #1
0
        private StringBuilder RenderedItems(List <BootStrapMenuItem> menuItems, string currentUrl, bool isRoot)
        {
            var html = new StringBuilder();

            foreach (BootStrapMenuItem item in menuItems)
            {
                BootStrapParentItem parentItem = item as BootStrapParentItem;

                // Is this the active menu item
                // TODO Make selecting current menu item more reliable
                if (currentUrl.Equals(item.NavigateToUrl, StringComparison.InvariantCultureIgnoreCase))
                {
                    item.IsActive = true;
                }

                if (item.HasPermission || parentItem != null)
                {
                    if (parentItem != null)
                    {
                        StringBuilder menuChildItems    = new StringBuilder();
                        bool          isActive          = false;
                        bool          isChildItemsAdded = false;

                        // Now add all the children
                        for (int index = 0; index < parentItem.Children.Count; index++)
                        {
                            BootStrapMenuItem itemChild = parentItem.Children[index];
                            if (!itemChild.IsDivider &&
                                currentUrl.Equals(itemChild.NavigateToUrl, StringComparison.InvariantCultureIgnoreCase))
                            {
                                itemChild.IsActive = true;
                                isActive           = true;
                            }
                            if (itemChild.HasPermission)
                            {
                                if (itemChild is BootStrapParentItem)
                                {
                                    var itemAsParent = ((BootStrapParentItem)itemChild);
                                    var subMenuItems = RenderedItems(itemAsParent.Children, currentUrl, false /*isRoot*/);
                                    if (subMenuItems.Length > 0)
                                    {
                                        menuChildItems.AppendFormat(SubMenu, itemAsParent.Class ?? String.Empty, itemAsParent.Text, subMenuItems);
                                        isChildItemsAdded = true;
                                    }
                                }
                                else if (itemChild.IsDivider)
                                {
                                    // Only add a divider if items above or below the divider are being shown
                                    if (index > 0 && (index - 1) < parentItem.Children.Count)
                                    {
                                        if (parentItem.Children[index - 1].HasPermission &&
                                            parentItem.Children[index + 1].HasPermission)
                                        {
                                            menuChildItems.AppendLine(Divider);
                                        }
                                    }
                                }
                                else if (string.IsNullOrEmpty(this.GetFullyQualifiedPath(itemChild.NavigateToUrl)))
                                {
                                    menuChildItems.AppendFormat(GroupLink, itemChild.Text);
                                    isChildItemsAdded = true;
                                }
                                else
                                {
                                    menuChildItems.AppendFormat(MenuLink, itemChild.IsActive ? "active" : string.Empty,
                                                                this.GetFullyQualifiedPath(itemChild.NavigateToUrl), itemChild.Text,
                                                                itemChild.Class != "" ? "class=\"" + itemChild.Class + "\"" : "",
                                                                !String.IsNullOrWhiteSpace(itemChild.id) ? "id=\"" + itemChild.id + "\" " : "");
                                    isChildItemsAdded = true;
                                }
                            }
                        }

                        if (item.HasPermission && isChildItemsAdded)
                        {
                            if (isRoot)
                            {
                                html.AppendLine(isActive ?
                                                "<li class=\"dropdown active\">"
                                    : "<li class=\"dropdown\">");
                                html.AppendFormat(DropDownLink, parentItem.Class ?? String.Empty, item.Text);
                                html.AppendLine("<ul class=\"dropdown-menu\">");
                                html.AppendLine(menuChildItems.ToString());
                                html.AppendLine("</ul></li>");
                            }
                            else
                            {
                                // TODO Do we need to inject the active class?
                                html.AppendFormat(SubMenu, parentItem.Class ?? String.Empty, item.Text, menuChildItems);
                            }
                        }
                    }
                    else
                    {
                        html.AppendFormat(MenuLink, item.IsDivider ? "divider" : item.IsActive ? "active" : string.Empty,
                                          this.GetFullyQualifiedPath(item.NavigateToUrl), item.Text,
                                          item.Class != "" ? "class=\"" + item.Class + "\"" : "",
                                          !String.IsNullOrWhiteSpace(item.id) ? "id=\"" + item.id + "\" " : "");
                    }
                }
            }
            return(html);
        }
コード例 #2
0
 /// <summary>
 /// Adds a boot strap menu item
 /// </summary>
 /// <param name="bootStrapMenuItem">
 /// The boot strap menu item.
 /// </param>
 /// <returns>
 /// Returns the index of the added item
 /// </returns>
 public int Add(BootStrapMenuItem bootStrapMenuItem)
 {
     this.bootStrapMenuItems.Add(bootStrapMenuItem);
     return(this.bootStrapMenuItems.Count - 1);
 }