Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added FluentBreadcrumbNodeBuilder and ViewDataDictionaryExtensions #100

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions examples/RazorPagesAndMvc/Controllers/Java/EasyJavaController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using SmartBreadcrumbs.Attributes;
using SmartBreadcrumbs.Extensions;
using SmartBreadcrumbs.Nodes;

namespace RazorPagesAndMvc.Controllers.Java
{
Expand All @@ -19,5 +21,19 @@ public IActionResult Post1()
return View();
}

public IActionResult Post2()
{
var builder = new FluentBreadcrumbNodeBuilder()
.AddMvcNode("Index", "Java", "Java")
.AddMvcNode(nameof(Index), "EasyJava", "Easy")
.AddMvcNode(nameof(Post2), "EasyJava", "Post 2")
.EndNode()
.EndNode()
.EndNode();

ViewData.SetBreadcrumbs(builder.Build());

return View();
}
}
}
3 changes: 3 additions & 0 deletions examples/RazorPagesAndMvc/Pages/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<li>
<a asp-action="Post1" asp-controller="EasyJava">Post 1</a>
</li>
<li>
<a asp-action="Post2" asp-controller="EasyJava">Post 2</a>
</li>
</ul>
</li>
<li>
Expand Down
3 changes: 3 additions & 0 deletions examples/RazorPagesAndMvc/Views/EasyJava/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@
<li>
<a asp-action="Post1" asp-controller="EasyJava">Post 1</a>
</li>
<li>
<a asp-action="Post2" asp-controller="EasyJava">Post 2</a>
</li>
</ul>
8 changes: 8 additions & 0 deletions examples/RazorPagesAndMvc/Views/EasyJava/Post2.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

@{
ViewData["Title"] = "Post2";
Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h1>Post 2</h1>

3 changes: 3 additions & 0 deletions examples/RazorPagesAndMvc/Views/Java/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<li>
<a asp-action="Post1" asp-controller="EasyJava">Post 1</a>
</li>
<li>
<a asp-action="Post2" asp-controller="EasyJava">Post 2</a>
</li>
</ul>
</li>
<li>
Expand Down
13 changes: 13 additions & 0 deletions src/Extensions/ViewDataDictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using SmartBreadcrumbs.Nodes;

namespace SmartBreadcrumbs.Extensions
{
public static class ViewDataDictionaryExtensions
{
public static void SetBreadcrumbs(this ViewDataDictionary viewData, BreadcrumbNode breadcrumb)
{
viewData["BreadcrumbNode"] = breadcrumb;
}
}
}
137 changes: 137 additions & 0 deletions src/Nodes/FluentBreadcrumbNodeBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
namespace SmartBreadcrumbs.Nodes
{
public class FluentBreadcrumbNodeBuilder
{
private readonly FluentBreadcrumbNodeBuilder parent;
private BreadcrumbNode node;
private bool isFirstNodeInitializing;

private BreadcrumbNode lastNode;

public FluentBreadcrumbNodeBuilder()
{
isFirstNodeInitializing = true;
}

public FluentBreadcrumbNodeBuilder(BreadcrumbNode node, FluentBreadcrumbNodeBuilder parent = null)
{
this.node = node;
this.parent = parent;
}

public FluentBreadcrumbNodeBuilder AddNode(BreadcrumbNode node)
{
if (isFirstNodeInitializing)
{
this.node = node;
isFirstNodeInitializing = false;
return this;
}
else
{
return new FluentBreadcrumbNodeBuilder(node, this);
}
}

public FluentBreadcrumbNodeBuilder AddMvcNode(
string action, string controller, string title, bool overwriteTitleOnExactMatch = false, string iconClasses = null, string areaName = null)
{
var node = new MvcBreadcrumbNode(action, controller, title, overwriteTitleOnExactMatch, iconClasses, areaName);

if (isFirstNodeInitializing)
{
this.node = node;
isFirstNodeInitializing = false;
return this;
}
else
{
return new FluentBreadcrumbNodeBuilder(node, this);
}
}

public FluentBreadcrumbNodeBuilder AddMvcControllerNode(
string controller, string title, bool overwriteTitleOnExactMatch = false, string iconClasses = null, string areaName = null)
{
var node = new MvcControllerBreadcrumbNode(controller, title, overwriteTitleOnExactMatch, iconClasses, areaName);

if (isFirstNodeInitializing)
{
this.node = node;
isFirstNodeInitializing = false;
return this;
}
else
{
return new FluentBreadcrumbNodeBuilder(node, this);
}
}

public FluentBreadcrumbNodeBuilder AddRazorPageNode(
string path, string title, bool overwriteTitleOnExactMatch = false, string iconClasses = null, string areaName = null)
{
var node = new RazorPageBreadcrumbNode(path, title, overwriteTitleOnExactMatch, iconClasses, areaName);

if (isFirstNodeInitializing)
{
this.node = node;
isFirstNodeInitializing = false;
return this;
}
else
{
return new FluentBreadcrumbNodeBuilder(node, this);
}
}

public FluentBreadcrumbNodeBuilder EndNode()
{
if (parent != null)
{
node.Parent = parent.node;

if (lastNode == null) // Should only happen one time
{
parent.lastNode = node;
}
else // Bubble the last node up the chain.. (to use inside Build())
{
parent.lastNode = lastNode;
}

return parent;
}

return this;
}

public FluentBreadcrumbNodeBuilder SetIconClasses(string iconClasses)
{
node.IconClasses = iconClasses;
return this;
}

public FluentBreadcrumbNodeBuilder SetOverwriteTitleOnExactMatch(bool overwriteTitleOnExactMatch)
{
node.OverwriteTitleOnExactMatch = overwriteTitleOnExactMatch; ;
return this;
}

public FluentBreadcrumbNodeBuilder SetRouteValues(object routeValues)
{
node.RouteValues = routeValues;
return this;
}

public FluentBreadcrumbNodeBuilder SetTitle(string title)
{
node.Title = title;
return this;
}

public BreadcrumbNode Build()
{
return lastNode;
}
}
}