-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from Geta/feature/sortable-headers
Add sortable headers feature
- Loading branch information
Showing
14 changed files
with
283 additions
and
52 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
...FoundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Base/AbstractSortablePageModel.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,16 @@ | ||
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Models; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
|
||
namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Base; | ||
|
||
public abstract class AbstractSortablePageModel : PageModel | ||
{ | ||
public string SortColumn { get; set; } | ||
public SortDirection? SortDirection { get; set; } | ||
|
||
public void ApplySort(string sortColumn, SortDirection? sortDirection) | ||
{ | ||
SortColumn = sortColumn; | ||
SortDirection = sortDirection; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...r.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Components/SortableHeaderCell/Default.cshtml
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,13 @@ | ||
@using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Models | ||
@model Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeaderCell.SortableHeaderCellViewModel | ||
|
||
<a href="@Model.GetSortUrl()" | ||
style="white-space: nowrap"> | ||
@Model.DisplayName | ||
|
||
@if (Model.IsActive() && Model.GetSortDirection() != null) | ||
{ | ||
<span data-feather="@(Model.GetSortDirection() == SortDirection.Ascending ? "arrow-up" : "arrow-down")"> | ||
</span> | ||
} | ||
</a> |
26 changes: 26 additions & 0 deletions
26
...tFoundHandlerAdmin/Pages/Components/SortableHeaderCell/SortableHeaderCellViewComponent.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,26 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeaderCell; | ||
|
||
public class SortableHeaderCellViewComponent : ViewComponent | ||
{ | ||
private readonly IHttpContextAccessor _contextAccessor; | ||
|
||
public SortableHeaderCellViewComponent(IHttpContextAccessor contextAccessor) | ||
{ | ||
_contextAccessor = contextAccessor; | ||
} | ||
|
||
public IViewComponentResult Invoke(string key, string displayName) | ||
{ | ||
var context = _contextAccessor.HttpContext; | ||
|
||
return View(new SortableHeaderCellViewModel | ||
{ | ||
QueryString = context?.Request.QueryString.ToString(), | ||
Key = key, | ||
DisplayName = displayName | ||
}); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...taNotFoundHandlerAdmin/Pages/Components/SortableHeaderCell/SortableHeaderCellViewModel.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,63 @@ | ||
using System; | ||
using System.Web; | ||
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Base; | ||
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Models; | ||
|
||
namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Components.SortableHeaderCell; | ||
|
||
public class SortableHeaderCellViewModel | ||
{ | ||
public string DisplayName { get; set; } | ||
public string Key { get; set; } | ||
public string QueryString { get; set; } | ||
|
||
public SortDirection? GetNextSortDirection() | ||
{ | ||
return GetSortDirection() switch | ||
{ | ||
null => SortDirection.Ascending, | ||
SortDirection.Ascending => SortDirection.Descending, | ||
SortDirection.Descending => null, | ||
_ => throw new ArgumentOutOfRangeException() | ||
}; | ||
} | ||
|
||
public string GetSortColumn() | ||
{ | ||
if (GetNextSortDirection() == null) | ||
{ | ||
return null; | ||
} | ||
|
||
return Key; | ||
} | ||
|
||
public string GetSortUrl() | ||
{ | ||
var qs = HttpUtility | ||
.ParseQueryString(QueryString); | ||
|
||
qs[nameof(AbstractSortablePageModel.SortColumn)] = GetSortColumn(); | ||
qs[nameof(AbstractSortablePageModel.SortDirection)] = GetNextSortDirection().ToString(); | ||
|
||
return $"?{qs}"; | ||
} | ||
|
||
public bool IsActive() | ||
{ | ||
var sortColumn = HttpUtility | ||
.ParseQueryString(QueryString) | ||
.Get(nameof(AbstractSortablePageModel.SortColumn)); | ||
|
||
return !string.IsNullOrEmpty(sortColumn) && sortColumn == Key; | ||
} | ||
|
||
public SortDirection? GetSortDirection() | ||
{ | ||
var sortDirection = HttpUtility | ||
.ParseQueryString(QueryString) | ||
.Get(nameof(AbstractSortablePageModel.SortDirection)); | ||
|
||
return Enum.TryParse(sortDirection, out SortDirection sort) ? sort : null; | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
...oundHandler.Admin/Areas/GetaNotFoundHandlerAdmin/Pages/Extensions/EnumerableExtensions.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,45 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text.RegularExpressions; | ||
using Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Models; | ||
|
||
namespace Geta.NotFoundHandler.Admin.Areas.GetaNotFoundHandlerAdmin.Pages.Extensions; | ||
|
||
public static class EnumerableExtensions | ||
{ | ||
public static IEnumerable<T> Sort<T>(this IEnumerable<T> list, string propertyName, SortDirection? sortDirection) | ||
{ | ||
if (!string.IsNullOrEmpty(propertyName)) | ||
{ | ||
var prop = typeof(T).GetProperty(propertyName); | ||
|
||
if (prop != null && sortDirection != null) | ||
{ | ||
if (sortDirection == SortDirection.Ascending) | ||
{ | ||
list = list.OrderBy(x => GetValue(prop, x)); | ||
} | ||
else | ||
{ | ||
list = list.OrderByDescending(x => GetValue(prop, x)); | ||
} | ||
} | ||
} | ||
|
||
return list; | ||
} | ||
|
||
private static object GetValue<T>(PropertyInfo prop, T element) | ||
{ | ||
var value = prop.GetValue(element); | ||
|
||
// Value should be IComparable | ||
if (value is Regex regex) | ||
{ | ||
return regex.ToString(); | ||
} | ||
|
||
return value; | ||
} | ||
} |
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
Oops, something went wrong.