/// <summary> /// Method used to create a top menu navigation button /// </summary> /// <param name="htmlHelper">html helper to extend</param> /// <param name="linkText">Text displayed on button</param> /// <param name="linkUrl">Default link url</param> /// <param name="linkClass">Css class</param> /// <param name="title">Mouse hover text</param> /// <param name="iconClass">Button icon class</param> /// <param name="linkId">Id if link button</param> /// <param name="buttonGroupModel">Drop down menu items</param> /// <returns></returns> public static MvcHtmlString TopMenuButton(this HtmlHelper htmlHelper, string linkText, string linkUrl, string linkClass, string title, string iconClass = null, string linkId = null, ButtonGroupModelBase buttonGroupModel = null) { var dropDownHtml = string.Empty; var buttonAttributes = InitButtonAttributes( linkClass, linkUrl, true); buttonAttributes.Add("id", linkId); buttonAttributes.Add("title", title); buttonAttributes.Add("data-placement", "top"); var iconHtml = string.Empty; if (!string.IsNullOrEmpty(iconClass)) { var icon = new TagBuilder("i"); icon.MergeAttribute("class", string.Format("icon-white {0}", iconClass)); if (!string.IsNullOrEmpty(linkText)) { icon.MergeAttribute("style", "margin-right: 10px;"); } // iconHtml = string.Format("{0}{1}", icon, string.IsNullOrEmpty(linkText) ? "" : " "); iconHtml = icon.ToString(); } var buttonTitle = iconHtml + linkText; if (buttonGroupModel != null) { var menuLinks = string.Empty; linkUrl = null; //Reset link url we use first menu item url //For each first level item foreach (var button in buttonGroupModel.Buttons) { if (!string.IsNullOrEmpty(button.Title)) { if (linkUrl == null) // Set button url to first link url { linkUrl = GetUrl(htmlHelper, button.DynamicPageInfo.Controller, button.DynamicPageInfo.Action); } //bool isButtonAncestorToCurrentPage = PageInfoManager.IsPageEqualToOrAncestorToOtherPage(button.DynamicPageInfo, SessionHandler.CurrentPage); //var cssClass = isButtonAncestorToCurrentPage ? "btn-active" : ""; var cssClass = button.IsCurrent ? "btn-active" : ""; var subMenuHtml = string.Empty; if (button.Children != null && button.Children.Count != 0) { var subMenuLinks = string.Empty; cssClass += " dropdown-submenu"; //For each second level item foreach (var subMenuButton in button.Children) { //Declare info icon attributes var subLinkInfoAttributes = new Dictionary <string, object>() { { "class", "icon-info" }, { "title", subMenuButton.Tooltip }, { "data-placement", "left" }, { "style", "padding: 0 5px 0 5px; margin-left: -15px;" } }; //Add menu item to submenu subMenuLinks += new TagBuilder("li") { Attributes = { new KeyValuePair <string, string>("class", subMenuButton.IsCurrent ? "btn-active" : "") }, InnerHtml = htmlHelper.IconActionLink( subLinkInfoAttributes, subMenuButton.Title, subMenuButton.DynamicPageInfo.Action, subMenuButton.DynamicPageInfo.Controller ).ToHtmlString() }.ToString(); } //Create the sub menu and add all links to it subMenuHtml = new TagBuilder("ul") { Attributes = { new KeyValuePair <string, string>("class", "dropdown-menu") }, InnerHtml = subMenuLinks }.ToString(); } //Declare info icon attributes var menuLinkInfoAttributes = new Dictionary <string, object>() { { "class", "icon-info" }, { "title", button.Tooltip }, { "data-placement", "left" }, { "style", "padding: 0 5px 0 5px; margin-left: -15px;" } }; //Add a menu link menuLinks += new TagBuilder("li") { Attributes = { new KeyValuePair <string, string>("class", cssClass) }, InnerHtml = htmlHelper.IconActionLink( menuLinkInfoAttributes, button.Title, button.DynamicPageInfo.Action, button.DynamicPageInfo.Controller ).ToHtmlString() + subMenuHtml }.ToString(); } } //Create the drop down menu var dropDownMenu = new TagBuilder("ul") { Attributes = { new KeyValuePair <string, string>("class", "dropdown-menu") }, InnerHtml = menuLinks }; //Create the down arrow buttonTitle += GetElementHtml( "i", new Dictionary <string, string>() { { "class", "icon-caret-down icon-white" }, { "style", "margin-left: 10px;" } } ); buttonAttributes["href"] = "#"; buttonAttributes.Add("data-toggle", "dropdown"); //Create the link that will trigger the drop down and add the icon to the link dropDownHtml = dropDownMenu.ToString(); } //Create a default link var defaultLinkHtml = GetBootstrapButtonHtml( buttonAttributes, buttonTitle, dropDownHtml ); //Create a div and add all controls to it var btnGroup = new TagBuilder("div") { InnerHtml = defaultLinkHtml }; btnGroup.MergeAttribute("class", "btn-group"); return(MvcHtmlString.Create(btnGroup.ToString())); }
public static MvcHtmlString TopMenuButton(this HtmlHelper htmlHelper, string linkText, string controller, string action, string linkClass, string title, string iconClass, ButtonGroupModelBase buttonGroupModel, string linkId) { return(TopMenuButton(htmlHelper, linkText, GetUrl(htmlHelper, controller, action), linkClass, title, iconClass, linkId, buttonGroupModel)); }