-
Notifications
You must be signed in to change notification settings - Fork 632
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 highlightning to admin menu #2320
Open
mk0sojo
wants to merge
7
commits into
HTBox:master
Choose a base branch
from
mk0sojo:issue-branch-2178
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
23cbe9e
Added highlightning in admin menu
mk0sojo 651f653
Hoover highlight color added
mk0sojo b3d35a1
Just a sample scenario for a campaign
MisterJames a793eb5
Added highlightning in admin menu
mk0sojo c7c26ff
Hoover highlight color added
mk0sojo cd5b2c9
Fixed mistake commit in appsettings.json
mk0sojo 878d694
Merged with latest
mk0sojo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
189 changes: 189 additions & 0 deletions
189
AllReadyApp/Web-App/AllReady.UnitTest/TagHelpers/ActiveRouteTagHelperShould.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
using AllReady.TagHelpers; | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AllReady.UnitTest.TagHelpers | ||
{ | ||
public class ActiveRouteTagHelperShould | ||
{ | ||
private const string controllerName = "Home"; | ||
private const string actionName = "List"; | ||
private Dictionary<string, string> routeValue = new Dictionary<string, string> { { "Id", "2" } }; | ||
private const string page = "/index"; | ||
|
||
private const string notMatchingControllerName = "HomeX"; | ||
private const string notMatchingActionName = "Index"; | ||
private Dictionary<string, string> notMathingRouteValue = new Dictionary<string, string> { { "Id", "3" } }; | ||
private const string notMatchingPage = "/index2"; | ||
|
||
private Func<bool, HtmlEncoder, Task<TagHelperContent>> GetEmptyChildContent() | ||
{ | ||
TagHelperContent content = new DefaultTagHelperContent(); | ||
return (b, encoder) => Task.FromResult(content); | ||
} | ||
|
||
private TagHelperContext GetContext() | ||
{ | ||
return new TagHelperContext(new TagHelperAttributeList(), new Dictionary<object, object>(), Guid.NewGuid().ToString()); | ||
} | ||
|
||
private TagHelperOutput GetOutput() | ||
{ | ||
return new TagHelperOutput("time", new TagHelperAttributeList(), GetEmptyChildContent()); | ||
} | ||
|
||
private ViewContext GetViewContextMVC() | ||
{ | ||
var viewContext = new ViewContext(); | ||
viewContext.RouteData = new Microsoft.AspNetCore.Routing.RouteData(); | ||
viewContext.RouteData.Values.Add("Controller", controllerName); | ||
viewContext.RouteData.Values.Add("Action", actionName); | ||
viewContext.RouteData.Values.Add("Id", "2"); | ||
return viewContext; | ||
} | ||
|
||
private ViewContext GetViewContextPage() | ||
{ | ||
var viewContext = new ViewContext(); | ||
viewContext.RouteData = new Microsoft.AspNetCore.Routing.RouteData(); | ||
viewContext.RouteData.Values.Add("page", page); | ||
return viewContext; | ||
} | ||
|
||
[Fact] | ||
public void NotHaveClassAttributeIfNoClassPassedInAndIfNoParametersPassedIn() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextMVC() | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(0, output.Attributes.Count); | ||
} | ||
|
||
[Fact] | ||
public void HaveActiveClassAttributeWhenControllerMatch() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextMVC(), | ||
Controller = controllerName | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(1, output.Attributes.Count); | ||
Assert.Equal("class", output.Attributes[0].Name); | ||
Assert.Equal("active", output.Attributes[0].Value); | ||
} | ||
|
||
[Fact] | ||
public void NotHaveClassAttributeWhenControllerNotMatching() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextMVC(), | ||
Controller = notMatchingControllerName | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(0, output.Attributes.Count); | ||
} | ||
|
||
[Fact] | ||
public void HaveActiveClassAttributeWhenActionMatch() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextMVC(), | ||
Controller = controllerName, | ||
Action = actionName | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(1, output.Attributes.Count); | ||
Assert.Equal("class", output.Attributes[0].Name); | ||
Assert.Equal("active", output.Attributes[0].Value); | ||
} | ||
|
||
[Fact] | ||
public void NotHaveClassAttributeWhenActionNotMatching() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextMVC(), | ||
Controller = controllerName, | ||
Action = notMatchingActionName | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(0, output.Attributes.Count); | ||
} | ||
|
||
[Fact] | ||
public void HaveActiveClassAttributeWhenRouteDataMatch() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextMVC(), | ||
Controller = controllerName, | ||
Action = actionName, | ||
RouteValues = routeValue | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(1, output.Attributes.Count); | ||
Assert.Equal("class", output.Attributes[0].Name); | ||
Assert.Equal("active", output.Attributes[0].Value); | ||
} | ||
|
||
[Fact] | ||
public void NotHaveClassAttributeWhenRouteDataNotMatching() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextMVC(), | ||
Controller = controllerName, | ||
Action = actionName, | ||
RouteValues = notMathingRouteValue | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(0, output.Attributes.Count); | ||
} | ||
|
||
[Fact] | ||
public void HaveActiveClassAttributePageMatch() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextPage(), | ||
Page = page | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(1, output.Attributes.Count); | ||
Assert.Equal("class", output.Attributes[0].Name); | ||
Assert.Equal("active", output.Attributes[0].Value); | ||
} | ||
|
||
[Fact] | ||
public void NotHaveClassAttributeWhenPageNotMatching() | ||
{ | ||
ActiveRouteTagHelper tagHelper = new ActiveRouteTagHelper | ||
{ | ||
ViewContext = GetViewContextPage(), | ||
Page = notMatchingPage | ||
}; | ||
var output = GetOutput(); | ||
tagHelper.Process(GetContext(), output); | ||
Assert.Equal(0, output.Attributes.Count); | ||
} | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
AllReadyApp/Web-App/AllReady/TagHelpers/ActiveRouteTagHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
using Microsoft.AspNetCore.Mvc.Rendering; | ||
using Microsoft.AspNetCore.Mvc.ViewFeatures; | ||
using Microsoft.AspNetCore.Razor.TagHelpers; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace AllReady.TagHelpers | ||
{ | ||
[HtmlTargetElement(Attributes = "is-active-route")] | ||
public class ActiveRouteTagHelper : TagHelper | ||
{ | ||
private IDictionary<string, string> _routeValues; | ||
|
||
/// <summary> | ||
/// Name of the action method | ||
/// </summary> | ||
[HtmlAttributeName("asp-action")] | ||
public string Action { get; set; } | ||
|
||
/// <summary> | ||
/// Name of the controller | ||
/// </summary> | ||
[HtmlAttributeName("asp-controller")] | ||
public string Controller { get; set; } | ||
|
||
/// <summary> | ||
/// Name of the razor page (e.g. /index) | ||
/// </summary> | ||
[HtmlAttributeName("asp-page")] | ||
public string Page { get; set; } | ||
|
||
/// <summary> | ||
/// Additional route parameters | ||
/// </summary> | ||
[HtmlAttributeName("asp-all-route-data", DictionaryAttributePrefix = "asp-route-")] | ||
public IDictionary<string, string> RouteValues | ||
{ | ||
get | ||
{ | ||
if (_routeValues == null) | ||
_routeValues = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); | ||
return _routeValues; | ||
} | ||
set | ||
{ | ||
_routeValues = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The ViewContext | ||
/// </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() | ||
{ | ||
if (ViewContext.RouteData.Values.ContainsKey("Controller")) | ||
{ | ||
return ShouldBeActieMvc(); | ||
} | ||
else if (ViewContext.RouteData.Values.ContainsKey("page")) | ||
{ | ||
return ShouldBeActiveRazorPage(); | ||
} | ||
return true; | ||
} | ||
|
||
private bool ShouldBeActieMvc() | ||
{ | ||
if (string.IsNullOrWhiteSpace(Controller) && | ||
string.IsNullOrWhiteSpace(Action) && | ||
!RouteValues.Any(r => !ViewContext.RouteData.Values.ContainsKey(r.Key))) | ||
{ | ||
return false; | ||
} | ||
|
||
string currentController = ViewContext.RouteData.Values["Controller"].ToString(); | ||
string currentAction = ViewContext.RouteData.Values["Action"].ToString(); | ||
|
||
if (!string.IsNullOrWhiteSpace(Controller) && Controller.ToLower() != currentController.ToLower()) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(Action) && Action.ToLower() != currentAction.ToLower()) | ||
{ | ||
return false; | ||
} | ||
|
||
foreach (var routeValue in RouteValues) | ||
{ | ||
if (!ViewContext.RouteData.Values.ContainsKey(routeValue.Key) || | ||
ViewContext.RouteData.Values[routeValue.Key].ToString() != routeValue.Value) | ||
{ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private bool ShouldBeActiveRazorPage() | ||
{ | ||
string currentPage = ViewContext.RouteData.Values["Page"].ToString(); | ||
|
||
if (string.IsNullOrWhiteSpace(Page) || Page.ToLower() != currentPage.ToLower()) | ||
{ | ||
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"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think this is not supposed to be here :)