Browse Source

Added active page checks to nav bar

tags/3.0.0^2
Teknikode 10 months ago
parent
commit
d8f3379625

+ 5
- 5
Teknik/Areas/User/Views/User/_LoginPartial.cshtml View File

@@ -8,22 +8,22 @@
<ul class="dropdown-menu dropdown-left-manual" role="menu" aria-labelledby="user_menu">
<li class="text-center text-uppercase dropdown__username">Signed in as @User.Identity.Name</li>
<li role="separator" class="divider"></li>
<li>
<li is-active-route asp-controller="User" asp-action="ViewProfile">
<a href="@Url.SubRouteUrl("user", "User.ViewProfile", new { username = User.Identity.Name })"><i class="fa fa-user fa-fw"></i>&nbsp;&nbsp;Profile</a>
</li>
<li>
<li is-active-route asp-controller="Blog" asp-action="Blog" asp-route-username="@User.Identity.Name">
<a href="@Url.SubRouteUrl("blog", "Blog.Blog", new { username = User.Identity.Name })"><i class="fa fa-book fa-fw"></i>&nbsp;&nbsp;View Blog</a>
</li>
<li>
<li is-active-route asp-controller="User" asp-action="ViewServiceData">
<a href="@Url.SubRouteUrl("user", "User.ViewServiceData")"><i class="fa fa-database fa-fw"></i>&nbsp;&nbsp;Service Data</a>
</li>
<li>
<li is-active-route asp-controller="User" asp-action="Settings">
<a href="@Url.SubRouteUrl("user", "User.Settings")"><i class="fa fa-sliders fa-fw"></i>&nbsp;&nbsp;Settings</a>
</li>
@if (User.IsInRole("Admin"))
{
<li role="separator" class="divider"></li>
<li>
<li is-active-route asp-controller="Admin" asp-action="Dashboard">
<a href="@Url.SubRouteUrl("admin", "Admin.Dashboard")"><i class="fa fa-cogs fa-fw"></i>&nbsp;&nbsp;Administration</a>
</li>
}

+ 10
- 10
Teknik/Views/Shared/_Navbar.cshtml View File

@@ -25,13 +25,13 @@
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="@Url.GetActive("www", string.Empty)">
<li is-active-route asp-controller="Home">
<a href="@Url.SubRouteUrl("www", "Home.Index")">Home</a>
</li>
<li class="@Url.GetActive("about")">
<li is-active-route asp-controller="About">
<a href="@Url.SubRouteUrl("about", "About.Index")">About</a>
</li>
<li class="@Url.GetActive("blog")">
<li is-active-route asp-controller="Blog" asp-action="Blog" asp-route-username="">
<a href="@Url.SubRouteUrl("blog", "Blog.Blog", new { username = string.Empty })">News</a>
</li>
<li class="divider-vertical"></li>
@@ -39,19 +39,19 @@
<li class="dropdown">
<a href="#" id="services_menu" class="dropdown-toggle" data-toggle="dropdown">Services <strong class="caret"></strong></a>
<ul class="dropdown-menu dropdown-left-manual" role="menu" aria-labelledby="services_menu">
<li class="@Url.GetActive("podcast")">
<li is-active-route asp-controller="Podcast">
<a href="@Url.SubRouteUrl("podcast", "Podcast.Index")"><i class="fa fa-microphone fa-fw"></i>&nbsp;&nbsp;Teknikast</a>
</li>
<li class="@Url.GetActive("upload", "u")">
<li is-active-route asp-controller="Upload" asp-action="Index">
<a href="@Url.SubRouteUrl("upload", "Upload.Index")"><i class="fa fa-lock fa-fw"></i>&nbsp;&nbsp;Upload Files</a>
</li>
<li class="@Url.GetActive("paste", "p")">
<li is-active-route asp-controller="Paste" asp-action="Index">
<a href="@Url.SubRouteUrl("paste", "Paste.Index")"><i class="fa fa-code fa-fw"></i>&nbsp;&nbsp;Pastebin</a>
</li>
<li class="@Url.GetActive("shorten", "s")">
<li is-active-route asp-controller="Shortener">
<a href="@Url.SubRouteUrl("shorten", "Shortener.Index")"><i class="fa fa-link fa-fw"></i>&nbsp;&nbsp;Url Shortener</a>
</li>
<li class="@Url.GetActive("vault", "v")">
<li is-active-route asp-controller="Vault" asp-action="NewVault">
<a href="@Url.SubRouteUrl("vault", "Vault.NewVault")"><i class="fa fa-folder fa-fw"></i>&nbsp;&nbsp;Create Vault</a>
</li>
<li role="separator" class="divider"></li>
@@ -66,10 +66,10 @@
</li>
</ul>
</li>
<li class="@Url.GetActive("contact")">
<li is-active-route asp-controller="Contact">
<a href="@Url.SubRouteUrl("contact", "Contact.Index")">Contact</a>
</li>
<li class="@Url.GetActive("help")">
<li is-active-route asp-controller="Help">
<a href="@Url.SubRouteUrl("help", "Help.Index")">Help</a>
</li>
</ul>

+ 136
- 0
Utilities/TagHelpers/ActiveRouteTagHelper.cs View File

@@ -0,0 +1,136 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Teknik.Utilities.TagHelpers
{
[HtmlTargetElement(Attributes = "is-active-route")]
public class ActiveRouteTagHelper : TagHelper
{
private readonly IHttpContextAccessor _contextAccessor;

public ActiveRouteTagHelper(IHttpContextAccessor contextAccessor)
{
_contextAccessor = contextAccessor;
}

private IDictionary<string, string> _routeValues;

/// <summary>The name of the action method.</summary>
/// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
[HtmlAttributeName("asp-action")]
public string Action { get; set; }

/// <summary>The name of the controller.</summary>
/// <remarks>Must be <c>null</c> if <see cref="P:Microsoft.AspNetCore.Mvc.TagHelpers.AnchorTagHelper.Route" /> is non-<c>null</c>.</remarks>
[HtmlAttributeName("asp-controller")]
public string Controller { get; set; }

[HtmlAttributeName("asp-page")]
public string Page { get; set; }

/// <summary>Additional parameters for the route.</summary>
[HtmlAttributeName("asp-all-route-data", DictionaryAttributePrefix = "asp-route-")]
public IDictionary<string, string> RouteValues
{
get
{
if (this._routeValues == null)
this._routeValues = (IDictionary<string, string>)new Dictionary<string, string>((IEqualityComparer<string>)StringComparer.OrdinalIgnoreCase);
return this._routeValues;
}
set
{
this._routeValues = value;
}
}

/// <summary>
/// Gets or sets the <see cref="T:Microsoft.AspNetCore.Mvc.Rendering.ViewContext" /> for the current request.
/// </summary>
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }

public override void Process(TagHelperContext context, TagHelperOutput output)
{
base.Process(context, output);

if (ShouldBeActive())
{
MakeActive(output);
}

output.Attributes.RemoveAll("is-active-route");
}

private bool ShouldBeActive()
{
string currentController = string.Empty;
string currentAction = string.Empty;

if (ViewContext.RouteData.Values["Controller"] != null)
{
currentController = ViewContext.RouteData.Values["Controller"].ToString();
}

if (ViewContext.RouteData.Values["Action"] != null)
{
currentAction = ViewContext.RouteData.Values["Action"].ToString();
}

if (Controller != null)
{
if (!string.IsNullOrWhiteSpace(Controller) && Controller.ToLower() != currentController.ToLower())
{
return false;
}

if (!string.IsNullOrWhiteSpace(Action) && Action.ToLower() != currentAction.ToLower())
{
return false;
}
}

if (Page != null)
{
if (!string.IsNullOrWhiteSpace(Page) && Page.ToLower() != _contextAccessor.HttpContext.Request.Path.Value.ToLower())
{
return false;
}
}

foreach (KeyValuePair<string, string> routeValue in RouteValues)
{
if (!ViewContext.RouteData.Values.ContainsKey(routeValue.Key) ||
ViewContext.RouteData.Values[routeValue.Key].ToString() != routeValue.Value)
{
return false;
}
}

return true;
}

private void MakeActive(TagHelperOutput output)
{
var classAttr = output.Attributes.FirstOrDefault(a => a.Name == "class");
if (classAttr == null)
{
classAttr = new TagHelperAttribute("class", "active");
output.Attributes.Add(classAttr);
}
else if (classAttr.Value == null || classAttr.Value.ToString().IndexOf("active") < 0)
{
output.Attributes.SetAttribute("class", classAttr.Value == null
? "active"
: classAttr.Value.ToString() + " active");
}
}
}
}

+ 24
- 3
Utilities/UrlExtensions.cs View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using System;
using System.Linq;
@@ -176,14 +177,34 @@ namespace Teknik.Utilities

public static string GetActive(this IUrlHelper url, params string[] subs)
{
string curSub = url.GetSubdomain();
return url.GetActive(null, null, subs);
}

public static string GetActive(this IUrlHelper url, string controller)
{
return url.GetActive(controller, null);
}

public static string GetActive(this IUrlHelper url, string controller, string action, params string[] subs)
{
var curSub = url.GetSubdomain();
var curController = url.ActionContext.RouteData.Values["Controller"]?.ToString();
var curAction = url.ActionContext.RouteData.Values["Action"]?.ToString();
foreach (string sub in subs)
{
if (curSub == sub)
{
return "active";
if ((string.IsNullOrEmpty(controller) || curController == controller) &&
(string.IsNullOrEmpty(action) || curAction == action))
return "active";
}
}
if (!subs.Any() &&
(string.IsNullOrEmpty(controller) || curController == controller) &&
(string.IsNullOrEmpty(action) || curAction == action))
{
return "active";
}
return string.Empty;
}


Loading…
Cancel
Save