From ab3b3c6e402d35de08f80eb60c22e2e3cd5c1f3e Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 21 Mar 2018 21:58:14 -0400 Subject: [PATCH] active page status, org customer select filter --- .../Controllers/OrganizationsController.cs | 5 +- src/Admin/Models/OrganizationsModel.cs | 2 +- src/Admin/TagHelpers/ActivePageTagHelper.cs | 76 +++++++++++++++++++ .../TagHelpers/OptionsSelectedTagHelper.cs | 44 +++++++++++ src/Admin/Views/Organizations/Index.cshtml | 12 +-- src/Admin/Views/Shared/_Layout.cshtml | 8 +- src/Admin/Views/Users/Index.cshtml | 2 +- src/Admin/Views/_ViewImports.cshtml | 1 + 8 files changed, 136 insertions(+), 14 deletions(-) create mode 100644 src/Admin/TagHelpers/ActivePageTagHelper.cs create mode 100644 src/Admin/TagHelpers/OptionsSelectedTagHelper.cs diff --git a/src/Admin/Controllers/OrganizationsController.cs b/src/Admin/Controllers/OrganizationsController.cs index ea0b38f133..68e923724d 100644 --- a/src/Admin/Controllers/OrganizationsController.cs +++ b/src/Admin/Controllers/OrganizationsController.cs @@ -19,7 +19,7 @@ namespace Bit.Admin.Controllers _organizationRepository = organizationRepository; } - public async Task Index(string name = null, string userEmail = null, bool paid = false, + public async Task Index(string name = null, string userEmail = null, bool? paid = null, int page = 1, int count = 25) { if(page < 1) @@ -33,8 +33,7 @@ namespace Bit.Admin.Controllers } var skip = (page - 1) * count; - var organizations = await _organizationRepository.SearchAsync(name, userEmail, paid ? (bool?)true : null, - skip, count); + var organizations = await _organizationRepository.SearchAsync(name, userEmail, paid, skip, count); return View(new OrganizationsModel { Items = organizations as List, diff --git a/src/Admin/Models/OrganizationsModel.cs b/src/Admin/Models/OrganizationsModel.cs index f03a065b2c..a5d9dc2410 100644 --- a/src/Admin/Models/OrganizationsModel.cs +++ b/src/Admin/Models/OrganizationsModel.cs @@ -6,6 +6,6 @@ namespace Bit.Admin.Models { public string Name { get; set; } public string UserEmail { get; set; } - public bool Paid { get; set; } + public bool? Paid { get; set; } } } diff --git a/src/Admin/TagHelpers/ActivePageTagHelper.cs b/src/Admin/TagHelpers/ActivePageTagHelper.cs new file mode 100644 index 0000000000..9ba30ab1f1 --- /dev/null +++ b/src/Admin/TagHelpers/ActivePageTagHelper.cs @@ -0,0 +1,76 @@ +using System; +using System.Linq; +using Microsoft.AspNetCore.Mvc.Controllers; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Microsoft.AspNetCore.Razor.TagHelpers; + +namespace Bit.Billing.TagHelpers +{ + [HtmlTargetElement("li", Attributes = ActiveControllerName)] + [HtmlTargetElement("li", Attributes = ActiveActionName)] + public class ActivePageTagHelper : TagHelper + { + private const string ActiveControllerName = "active-controller"; + private const string ActiveActionName = "active-action"; + + private readonly IHtmlGenerator _generator; + + public ActivePageTagHelper(IHtmlGenerator generator) + { + _generator = generator; + } + + [HtmlAttributeNotBound] + [ViewContext] + public ViewContext ViewContext { get; set; } + [HtmlAttributeName(ActiveControllerName)] + public string ActiveController { get; set; } + [HtmlAttributeName(ActiveActionName)] + public string ActiveAction { get; set; } + + public override void Process(TagHelperContext context, TagHelperOutput output) + { + if(context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if(output == null) + { + throw new ArgumentNullException(nameof(output)); + } + + if(ActiveAction == null && ActiveController == null) + { + return; + } + + var descriptor = ViewContext.ActionDescriptor as ControllerActionDescriptor; + if(descriptor == null) + { + return; + } + + var controllerMatch = ActiveMatch(ActiveController, descriptor.ControllerName); + var actionMatch = ActiveMatch(ActiveAction, descriptor.ActionName); + if(controllerMatch && actionMatch) + { + var classValue = "active"; + if(output.Attributes["class"] != null) + { + classValue += " " + output.Attributes["class"].Value; + output.Attributes.Remove(output.Attributes["class"]); + } + + output.Attributes.Add("class", classValue); + } + } + + private bool ActiveMatch(string route, string descriptor) + { + return route == null || route == "*" || + route.Split(',').Any(c => c.Trim().ToLower() == descriptor.ToLower()); + } + } +} diff --git a/src/Admin/TagHelpers/OptionsSelectedTagHelper.cs b/src/Admin/TagHelpers/OptionsSelectedTagHelper.cs new file mode 100644 index 0000000000..ca28a942cd --- /dev/null +++ b/src/Admin/TagHelpers/OptionsSelectedTagHelper.cs @@ -0,0 +1,44 @@ +using System; +using Microsoft.AspNetCore.Mvc.ViewFeatures; +using Microsoft.AspNetCore.Razor.TagHelpers; + +namespace Bit.Billing.TagHelpers +{ + [HtmlTargetElement("option", Attributes = SelectedName)] + public class OptionSelectedTagHelper : TagHelper + { + private const string SelectedName = "asp-selected"; + + private readonly IHtmlGenerator _generator; + + public OptionSelectedTagHelper(IHtmlGenerator generator) + { + _generator = generator; + } + + [HtmlAttributeName(SelectedName)] + public bool Selected { get; set; } + + public override void Process(TagHelperContext context, TagHelperOutput output) + { + if(context == null) + { + throw new ArgumentNullException(nameof(context)); + } + + if(output == null) + { + throw new ArgumentNullException(nameof(output)); + } + + if(Selected) + { + output.Attributes.Add("selected", "selected"); + } + else + { + output.Attributes.RemoveAll("selected"); + } + } + } +} diff --git a/src/Admin/Views/Organizations/Index.cshtml b/src/Admin/Views/Organizations/Index.cshtml index 56b03beced..b10c84d52f 100644 --- a/src/Admin/Views/Organizations/Index.cshtml +++ b/src/Admin/Views/Organizations/Index.cshtml @@ -5,15 +5,17 @@

Organizations

-
+ -
- - -
+ +
diff --git a/src/Admin/Views/Shared/_Layout.cshtml b/src/Admin/Views/Shared/_Layout.cshtml index a113e3726a..7d668d9e43 100644 --- a/src/Admin/Views/Shared/_Layout.cshtml +++ b/src/Admin/Views/Shared/_Layout.cshtml @@ -27,13 +27,13 @@