zHaytam / SmartBreadcrumbs

A utility library for ASP.NET Core (both MVC and Razor Pages) websites to easily add and customize breadcrumbs.
https://blog.zhaytam.com/2018/06/24/asp-net-core-using-smartbreadcrumbs/
MIT License
161 stars 77 forks source link

Query for Tags #71

Open pollend opened 4 years ago

pollend commented 4 years ago

so I have these utility methods that I use to tie the navigation bar with the breadcrumb list. should allow for some custom breadcrumbs by iterating over the entries.

public class BreadcrumbHelper : IViewContextAware
{
       private readonly ILogger _logger;
        private BreadcrumbManager _breadcrumbManager;
        private ViewContext _viewContext;

        public BreadcrumbHelper(ILogger<BreadcrumbHelper> logger, BreadcrumbManager breadcrumbManager)
        {
            _logger = logger;
            _breadcrumbManager = breadcrumbManager;

        }

        public void Contextualize(ViewContext viewContext)
        {
            _viewContext = viewContext;
        }

        private String _extractTag(String tag)
        {
            if (tag.Contains("."))
            {
                return tag.Split(".")[1];
            }
            return tag;
        }

        public List<String> GetTags()
        {
            List<String> tags = new List<string>();
            var nodeKey = new NodeKey(_viewContext.ActionDescriptor.RouteValues,
                _viewContext.HttpContext.Request.Method);
            var node = _viewContext.ViewData["BreadcrumbNode"] as BreadcrumbNode ??
                       _breadcrumbManager.GetNode(nodeKey.Value);
            if (node != null)
            {
                tags.Add(_extractTag(node.Title));

                while (node.Parent != null)
                {
                    node = node.Parent;
                    tags.Add(_extractTag(node.Title));
                }
            }
            return tags;
        }

        public bool IsOnRoute(String tag)
        {
            var nodeKey = new NodeKey(_viewContext.ActionDescriptor.RouteValues,
                _viewContext.HttpContext.Request.Method);
            var node = _viewContext.ViewData["BreadcrumbNode"] as BreadcrumbNode ??
                       _breadcrumbManager.GetNode(nodeKey.Value);
            if (node != null)
            {
                if (node.Title.EndsWith(tag))
                {
                    return true;
                }

                while (node.Parent != null)
                {
                    node = node.Parent;
                    if (node.Title.EndsWith(tag))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
zHaytam commented 4 years ago

Hello,
I'm not sure I understand what the issue (if there is any)?

pollend commented 4 years ago

Would be nice to have some utility methods to build up the list of tags from cshtml.

zHaytam commented 4 years ago

If I understand correctly, you want something like a Tag property on breadcrumb nodes so that you do something with it?

pollend commented 4 years ago

So my understanding is the BreadcrumbManager is a graph of nodes for the different page routes. My basic request would be some kind of utility class with enough functionality to generate a custom breadcrumb. There are some other additional functionality such as what I'm currently using it for which is marking out the location in the page navigation.

I guess these are more of my rough thoughts which could improve the library. there isn't anything particularly actionable. Some kind of functionality like this I think would help a lot.

image

zHaytam commented 4 years ago

My basic request would be some kind of utility class with enough functionality to generate a custom breadcrumb.

I am very sorry but I still don't get it, especially this part. Can you tell me exactly your use case and how you would like SmartBreadcrumbs to do it? (A sample usage code)

pollend commented 4 years ago

I was thinking of a class like this so the breadcrumb doesn't need to be defined by the library. I'm not sure how useful this would be. I could also use this to mark out the location in the navigation based off the current breadcrumb path. I was thinking about a more front facing class for general usage. I really like the library, just a couple things that I would like to see that are not necessary. if this is not necessary, then I guess this issue can just be closed out.

example, this is kind of one simple usecase but the api can be up for debate.

<div>
@for(var node : @smartbreadcrumb.path(reverse=true)){
{
<a class="{node.defaultName}" href="{node.path}">{node.name}</p>
}
</div>
zHaytam commented 4 years ago

If the BreadcrumbManager class made the list of nodes visible, you could do that by injecting it no? (e.g. BreadcrumbManager.Nodes, and then you foreach them)

pollend commented 4 years ago

you can add a class that is aware of the viewcontext. I'm not sure if a class with IViewContextAware works with a singleton so it might have to be scope. so you can return a list of nodes for the current route. The functionality is there to generate a breadcrumb for the current path. Something like BreadcrumbManager.Nodes could work as well or maybe something that describes the current route.

BreadcrumbManager.currentPath <-- the list of nodes for the current path BreadcrumbManager.isOnPath <-- if node is on the current path for the breadcrumb etc ...