Esempio n. 1
0
        private MenuItem CreateMenuItemByTemplate(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);
        }
Esempio n. 2
0
        static IEnumerable <IMenuItemContainer> CreateItems(Configuration.MenuItemElementCollection itemElementCollection, List <IMenuItemContainer> itemContainers)
        {
            if (itemElementCollection != null)
            {
                if (!string.IsNullOrEmpty(itemElementCollection.Type))
                {
                    itemContainers.Add((IMenuItemContainer)Activator.CreateInstance(Type.GetType(itemElementCollection.Type)));
                }
                else
                {
                    foreach (Configuration.MenuItemElement element in itemElementCollection)
                    {
                        IMenuItemContainer itemContainer = null;
                        if (!string.IsNullOrEmpty(element.Type))
                        {
                            itemContainer = (IMenuItemContainer)Activator.CreateInstance(Type.GetType(element.Type));
                        }
                        else
                        {
                            itemContainer = new MenuItemTemplate();
                        }
                        itemContainers.Add(itemContainer);
                        if (itemContainer is MenuItemTemplate)
                        {
                            var itemTemplate = (MenuItemTemplate)itemContainer;

                            itemTemplate.Name               = element.Name;
                            itemTemplate.Text               = element.Text;
                            itemTemplate.Action             = element.Action;
                            itemTemplate.Controller         = element.Controller;
                            itemTemplate.Visible            = element.Visible;
                            itemTemplate.Area               = element.Area;
                            itemTemplate.RouteValues        = new System.Web.Routing.RouteValueDictionary(element.RouteValues.Attributes);
                            itemTemplate.HtmlAttributes     = new System.Web.Routing.RouteValueDictionary(element.HtmlAttributes.Attributes);
                            itemTemplate.ReadOnlyProperties = element.UnrecognizedProperties;

                            if (!string.IsNullOrEmpty(element.Initializer))
                            {
                                Type type = Type.GetType(element.Initializer);
                                itemTemplate.Initializer = (IMenuItemInitializer)Activator.CreateInstance(type);
                            }

                            List <IMenuItemContainer> subItems = new List <IMenuItemContainer>();
                            if (element.Items != null)
                            {
                                itemTemplate.ItemContainers = CreateItems(element.Items, subItems);
                            }
                        }
                    }
                }
            }
            return(itemContainers);
        }