Abstracting navigation so it can use any enums

This commit is contained in:
lepipele 2018-04-19 15:42:12 -05:00
parent 0934bebf7b
commit 76cd9a7b25
11 changed files with 46 additions and 47 deletions

View File

@ -1,6 +1,6 @@
@model EmailsViewModel
@{
ViewData.SetActivePageAndTitle(ServerNavPages.Pages.Emails);
ViewData.SetActivePageAndTitle(ServerNavPages.Emails);
}

View File

@ -1,6 +1,6 @@
@model UsersViewModel
@{
ViewData.SetActivePageAndTitle(ServerNavPages.Pages.Users);
ViewData.SetActivePageAndTitle(ServerNavPages.Users);
}

View File

@ -1,6 +1,6 @@
@model BTCPayServer.Services.PoliciesSettings
@{
ViewData.SetActivePageAndTitle(ServerNavPages.Pages.Policies);
ViewData.SetActivePageAndTitle(ServerNavPages.Policies);
}

View File

@ -1,6 +1,6 @@
@model RatesViewModel
@{
ViewData.SetActivePageAndTitle(ServerNavPages.Pages.Rates);
ViewData.SetActivePageAndTitle(ServerNavPages.Rates);
}

View File

@ -1,30 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace BTCPayServer.Views.Server
{
public static class ServerNavPages
{
public enum Pages
{
Index, Users, Rates, Emails, Policies, Theme, Hangfire
}
public const string ACTIVE_PAGE_KEY = "ActivePage";
public static void SetActivePageAndTitle(this ViewDataDictionary viewData, Pages activePage)
{
viewData["Title"] = activePage.ToString();
viewData[ACTIVE_PAGE_KEY] = activePage;
}
public static string IsActivePage(this ViewDataDictionary viewData, Pages page)
{
var activePage = viewData[ACTIVE_PAGE_KEY] as Pages?;
return activePage == page ? "active" : null;
}
}
}

View File

@ -1,6 +1,6 @@
@model BTCPayServer.Services.ThemeSettings
@{
ViewData.SetActivePageAndTitle(ServerNavPages.Pages.Theme);
ViewData.SetActivePageAndTitle(ServerNavPages.Theme);
}

View File

@ -1,6 +1,6 @@
@model UserViewModel
@{
ViewData.SetActivePageAndTitle(ServerNavPages.Pages.Users);
ViewData.SetActivePageAndTitle(ServerNavPages.Users);
}

View File

@ -1,11 +1,9 @@
@using BTCPayServer.Views.Server
<div class="nav flex-column nav-pills">
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Pages.Users)" asp-action="Users">Users</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Pages.Rates)" asp-action="Rates">Rates</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Pages.Emails)" asp-action="Emails">Email server</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Pages.Policies)" asp-action="Policies">Policies</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Pages.Theme)" asp-action="Theme">Theme</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Pages.Hangfire)" href="~/hangfire" target="_blank">Hangfire</a>
<div class="nav flex-column nav-pills">
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Users)" asp-action="Users">Users</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Rates)" asp-action="Rates">Rates</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Emails)" asp-action="Emails">Email server</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Policies)" asp-action="Policies">Policies</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Theme)" asp-action="Theme">Theme</a>
<a class="nav-link @ViewData.IsActivePage(ServerNavPages.Hangfire)" href="~/hangfire" target="_blank">Hangfire</a>
</div>

View File

@ -1,2 +1 @@
@using BTCPayServer.Views.Server
@using BTCPayServer.Models.ServerViewModels
@using BTCPayServer.Models.ServerViewModels

View File

@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
namespace BTCPayServer.Views
{
public static class ViewsRazor
{
public const string ACTIVE_PAGE_KEY = "ActivePage";
public static void SetActivePageAndTitle<T>(this ViewDataDictionary viewData, T activePage)
where T : IConvertible
{
viewData["Title"] = activePage.ToString();
viewData[ACTIVE_PAGE_KEY] = activePage;
}
public static string IsActivePage<T>(this ViewDataDictionary viewData, T page)
where T : IConvertible
{
var activePage = (T)viewData[ACTIVE_PAGE_KEY];
return page.Equals(activePage) ? "active" : null;
}
}
public enum ServerNavPages
{
Index, Users, Rates, Emails, Policies, Theme, Hangfire
}
}

View File

@ -1,5 +1,6 @@
@using Microsoft.AspNetCore.Identity
@using BTCPayServer
@using BTCPayServer.Views
@using BTCPayServer.Models
@using BTCPayServer.Models.AccountViewModels
@using BTCPayServer.Models.InvoicingModels