Example #1
0
        public static List<NavigationItem> GetNavigationItems(Func<string, string> resolveUrl = null,
            Func<NavigationItemAttribute, bool> filter = null)
        {
            var result = new List<NavigationItem>();
            var menuItems = GetNavigationItemAttributes(filter);
            var remaining = new HashSet<string>();
            foreach (var item in menuItems)
                remaining.Add(item.Key);

            Action<List<NavigationItem>, NavigationItemAttribute> processMenu = null;
            processMenu = (parent, menu) =>
            {
                var path = (menu.Category.IsEmptyOrNull() ? "" : (menu.Category + "/"));
                path += (menu.Title.TrimToNull() ?? "");
                remaining.Remove(path);

                var section = new NavigationItem
                {
                    Title = menu.Title,
                    Url = (!string.IsNullOrEmpty(menu.Url) && resolveUrl != null) ? resolveUrl(menu.Url) : menu.Url,
                    IconClass = menu.IconClass.TrimToNull(),
                    Target = menu.Target.TrimToNull()
                };

                bool isAuthorizedSection = !menu.Url.IsEmptyOrNull() &&
                    (menu.Permission.IsEmptyOrNull() || Authorization.HasPermission(menu.Permission));

                var children = menuItems[path];
                foreach (var child in children)
                    processMenu(section.Children, child);

                if (section.Children.Count > 0 || isAuthorizedSection)
                    parent.Add(section);
            };

            remaining.Remove("");
            foreach (var menu in menuItems[""])
                processMenu(result, menu);

            while (remaining.Count > 0)
            {
                var first = remaining.First();
                remaining.Remove(first);
                var menu = new NavigationMenuAttribute(Int32.MaxValue, first);
                processMenu(result, menu);
            }

            return result;
        }
        private void SearchActivePath(NavigationItem link, string currentUrl, int[] currentPath, int depth,
            ref int[] bestMatch, ref int bestMatchLength)
        {
            currentPath[depth + 1] = 0;
            var url = link.Url ?? "";

            if (url != null && url.StartsWith("~/", StringComparison.Ordinal))
                url = VirtualPathUtility.ToAbsolute(url);

            if (currentUrl.IndexOf(url, StringComparison.OrdinalIgnoreCase) >= 0 &&
                (bestMatchLength == 0 || url.Length > bestMatchLength))
            {
                bestMatch = (int[])currentPath.Clone();
                bestMatchLength = url.Length;
            }

            if (depth <= 9)
            {
                foreach (var child in link.Children)
                    SearchActivePath(child, currentUrl, currentPath, depth + 1, ref bestMatch, ref bestMatchLength);
            }

            currentPath[depth]++;
        }
        public static List<NavigationItem> ConvertToNavigationItems(ILookup<string, NavigationItemAttribute> attrByCategory,
            Func<string, string> resolveUrl)
        {
            var result = new List<NavigationItem>();

            Action<List<NavigationItem>, NavigationItemAttribute> processAttr = null;
            processAttr = (parent, attr) =>
            {
                var item = new NavigationItem
                {
                    Title = (attr.Title ?? "").Replace("//", "/"),
                    FullPath = attr.FullPath,
                    Url = (!string.IsNullOrEmpty(attr.Url) && resolveUrl != null) ? resolveUrl(attr.Url) : attr.Url,
                    IconClass = attr.IconClass.TrimToNull(),
                    Target = attr.Target.TrimToNull()
                };

                bool isAuthorizedSection = !attr.Url.IsEmptyOrNull() &&
                    (attr.Permission.IsEmptyOrNull() || Authorization.HasPermission(attr.Permission));

                var path = (attr.Category.IsEmptyOrNull() ? "" : (attr.Category + "/"));
                path += (attr.Title ?? "");

                var children = attrByCategory[path];
                foreach (var child in children)
                    processAttr(item.Children, child);

                if (item.Children.Count > 0 || isAuthorizedSection)
                    parent.Add(item);
            };

            foreach (var menu in attrByCategory[""])
                processAttr(result, menu);

            return result;
        }
Example #4
0
        public static List<NavigationItem> GetNavigationItems(Func<string, string> resolveUrl = null,
            Func<NavigationItemAttribute, bool> filter = null)
        {
            var result = new List<NavigationItem>();
            var menuItems = GetNavigationItemAttributes(filter);
            var remaining = new HashSet<string>();
            foreach (var item in menuItems)
                remaining.Add(item.Key);

            Action<NavigationItemAttribute> processMenu = menu =>
            {
                remaining.Remove(menu.Title.TrimToNull());

                var section = new NavigationItem
                {
                    Title = menu.Title,
                    Url = (!string.IsNullOrEmpty(menu.Url) && resolveUrl != null) ? resolveUrl(menu.Url) : menu.Url,
                    IconClass = menu.IconClass.TrimToNull(),
                    Target = menu.Target.TrimToNull()
                };

                bool isAuthorizedSection = !menu.Url.IsEmptyOrNull() &&
                    (menu.Permission.IsEmptyOrNull() || Authorization.HasPermission(menu.Permission));

                var children = menuItems[menu.Title.TrimToNull() ?? "???"];
                foreach (var child in children)
                {
                    if (child.Url.IsEmptyOrNull())
                        continue;

                    if (child.Permission != null &&
                        !Authorization.HasPermission(child.Permission))
                        continue;

                    section.Children.Add(new NavigationItem
                    {
                        Title = child.Title,
                        Url = (!string.IsNullOrEmpty(child.Url) && resolveUrl != null) ? resolveUrl(child.Url) : child.Url,
                        IconClass = child.IconClass.TrimToNull(),
                        Target = child.Target.TrimToNull()
                    });
                }

                if (section.Children.Count > 0 || isAuthorizedSection)
                {
                    result.Add(section);
                }
            };

            foreach (var menu in menuItems[null])
                processMenu(menu);

            while (remaining.Count > 0)
            {
                var first = remaining.First();
                remaining.Remove(first);
                var menu = new NavigationMenuAttribute(Int32.MaxValue, first);
                processMenu(menu);
            }

            return result;
        }
Example #5
0
        public static List <NavigationItem> GetNavigationItems(Func <string, string> resolveUrl = null)
        {
            var result    = new List <NavigationItem>();
            var menuItems = GetNavigationItemAttributes();
            var remaining = new HashSet <string>();

            foreach (var item in menuItems)
            {
                remaining.Add(item.Key);
            }

            Action <NavigationItemAttribute> processMenu = menu =>
            {
                remaining.Remove(menu.Title.TrimToNull());

                var section = new NavigationItem
                {
                    Title     = menu.Title,
                    Url       = (!string.IsNullOrEmpty(menu.Url) && resolveUrl != null) ? resolveUrl(menu.Url) : menu.Url,
                    IconClass = menu.IconClass.TrimToNull(),
                    Target    = menu.Target.TrimToNull()
                };

                bool isAuthorizedSection = !menu.Url.IsEmptyOrNull() &&
                                           (menu.Permission.IsEmptyOrNull() || Authorization.HasPermission(menu.Permission));

                var children = menuItems[menu.Title.TrimToNull() ?? "???"];
                foreach (var child in children)
                {
                    if (child.Url.IsEmptyOrNull())
                    {
                        continue;
                    }

                    if (child.Permission != null &&
                        !Authorization.HasPermission(child.Permission))
                    {
                        continue;
                    }

                    section.Children.Add(new NavigationItem
                    {
                        Title     = child.Title,
                        Url       = (!string.IsNullOrEmpty(child.Url) && resolveUrl != null) ? resolveUrl(child.Url) : child.Url,
                        IconClass = child.IconClass.TrimToNull(),
                        Target    = child.Target.TrimToNull()
                    });
                }

                if (section.Children.Count > 0 || isAuthorizedSection)
                {
                    result.Add(section);
                }
            };

            foreach (var menu in menuItems[null])
            {
                processMenu(menu);
            }

            while (remaining.Count > 0)
            {
                var first = remaining.First();
                remaining.Remove(first);
                var menu = new NavigationMenuAttribute(Int32.MaxValue, first);
                processMenu(menu);
            }

            return(result);
        }