private MenuItem BuildItems(string areaName, MenuItemTemplate template, ControllerContext controllerContext)
        {
            MenuItem item = new MenuItem()
            {
                Action = template.Action,
                Area = string.IsNullOrEmpty(template.Area) ? areaName : template.Area,
                Controller = template.Controller,
                HtmlAttributes = new RouteValueDictionary(template.HtmlAttributes),
                Localizable = template.Localizable,
                Name = template.Name,
                ReadOnlyProperties = template.ReadOnlyProperties,
                RouteValues = new RouteValueDictionary(template.RouteValues),
                Text = template.Text,
                Visible = template.Visible,
                Initializer = template.Initializer
            };

            List<MenuItem> items = new List<MenuItem>();
            if (template.ItemContainers != null)
            {
                foreach (var itemContainer in template.ItemContainers)
                {
                    items.AddRange(itemContainer.GetItems(areaName, controllerContext));
                }
            }

            item.Items = items;

            return item;
        }
 public virtual bool IsActive(MenuItem item, ControllerContext controllerContext)
 {
     if (!string.IsNullOrEmpty(item.Area))
     {
         if (string.Compare(item.Area, AreaHelpers.GetAreaName(controllerContext.RouteData)) != 0)
         {
             return false;
         }
     }
     return string.Compare(controllerContext.RouteData.Values["controller"].ToString(), item.Controller, true) == 0;
 }
        public virtual MenuItem Initialize(MenuItem menuItem, ControllerContext controllerContext)
        {
            var areaName = AreaHelpers.GetAreaName(controllerContext.RouteData);
            if (!string.IsNullOrEmpty(areaName) && menuItem.RouteValues != null)
            {
                menuItem.RouteValues["area"] = areaName;
            }
            if (!string.IsNullOrEmpty(menuItem.Area) && menuItem.RouteValues != null)
            {
                menuItem.RouteValues["area"] = menuItem.Area;
            }

            var isActive =
               GetIsActive(menuItem, controllerContext);
            menuItem.IsCurrent = isActive;

            foreach (var sub in menuItem.Items)
            {
                sub.Initialize(controllerContext);
                if (sub.IsActive)
                {
                    menuItem.IsCurrent = false;
                }
                isActive = isActive || sub.IsActive;
            }

            menuItem.IsActive = isActive;

            //if (!this.IsActive)
            //{
            //    this.IsActive = DefaultActive(controllerContext);
            //}

            if (menuItem.Visible != false)
            {
                var isVisible =
               GetIsVisible(menuItem, controllerContext);

                if (string.IsNullOrEmpty(menuItem.Action) && menuItem.Items.Where(it => it.Visible == true).Count() == 0)
                {
                    isVisible = false;
                }

                menuItem.Visible = isVisible;
            }

            menuItem.Initialized = true;

            return menuItem;
        }
 protected virtual bool GetIsActive(MenuItem menuItem, ControllerContext controllerContext)
 {
     if (!string.IsNullOrEmpty(menuItem.Area))
     {
         if (string.Compare(menuItem.Area, AreaHelpers.GetAreaName(controllerContext.RouteData)) != 0)
         {
             return false;
         }
     }
     var active = string.Compare(controllerContext.RouteData.Values["controller"].ToString(), menuItem.Controller, true) == 0;
     if (active && menuItem.ReadOnlyProperties != null)
     {
         var activeByAction = menuItem.ReadOnlyProperties["activeByAction"];
         if (!string.IsNullOrEmpty(activeByAction) && activeByAction.ToLower() == "true")
         {
             active = string.Compare(controllerContext.RouteData.Values["action"].ToString(), menuItem.Action, true) == 0;
         }
     }
     return active;
 }
 public virtual bool IsVisible(MenuItem item, ControllerContext conrollerContext)
 {
     return true;
 }
 protected virtual bool GetIsVisible(MenuItem menuItem, ControllerContext controllerContext)
 {
     return true;
 }