2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2018-05-26 16:32:20 +02:00
|
|
|
using System.Globalization;
|
|
|
|
using Microsoft.AspNetCore.Html;
|
2018-04-19 22:42:12 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc.ViewFeatures;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Views
|
|
|
|
{
|
|
|
|
public static class ViewsRazor
|
|
|
|
{
|
2021-03-19 12:05:46 +01:00
|
|
|
private const string ACTIVE_CATEGORY_KEY = "ActiveCategory";
|
|
|
|
private const string ACTIVE_PAGE_KEY = "ActivePage";
|
|
|
|
|
2021-04-08 15:32:42 +02:00
|
|
|
public static void SetActivePageAndTitle<T>(this ViewDataDictionary viewData, T activePage, string title = null, string mainTitle = null)
|
2018-04-19 22:42:12 +02:00
|
|
|
where T : IConvertible
|
|
|
|
{
|
2021-04-08 15:32:42 +02:00
|
|
|
// Browser Title
|
2018-04-19 22:57:23 +02:00
|
|
|
viewData["Title"] = title ?? activePage.ToString();
|
2021-04-08 15:32:42 +02:00
|
|
|
// Breadcrumb
|
|
|
|
viewData["MainTitle"] = mainTitle;
|
|
|
|
viewData["PageTitle"] = title;
|
|
|
|
// Navigation
|
2018-04-19 22:42:12 +02:00
|
|
|
viewData[ACTIVE_PAGE_KEY] = activePage;
|
2021-03-19 12:05:46 +01:00
|
|
|
SetActiveCategory(viewData, activePage.GetType());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void SetActiveCategory<T>(this ViewDataDictionary viewData, T activeCategory)
|
|
|
|
{
|
|
|
|
viewData[ACTIVE_CATEGORY_KEY] = activeCategory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string IsActiveCategory<T>(this ViewDataDictionary viewData, T category)
|
|
|
|
{
|
|
|
|
if (!viewData.ContainsKey(ACTIVE_CATEGORY_KEY))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var activeCategory = (T)viewData[ACTIVE_CATEGORY_KEY];
|
|
|
|
return category.Equals(activeCategory) ? "active" : null;
|
2018-04-19 22:42:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static string IsActivePage<T>(this ViewDataDictionary viewData, T page)
|
|
|
|
where T : IConvertible
|
|
|
|
{
|
2020-10-21 14:02:20 +02:00
|
|
|
if (!viewData.ContainsKey(ACTIVE_PAGE_KEY))
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2018-04-19 22:42:12 +02:00
|
|
|
var activePage = (T)viewData[ACTIVE_PAGE_KEY];
|
|
|
|
return page.Equals(activePage) ? "active" : null;
|
|
|
|
}
|
2018-05-26 16:32:20 +02:00
|
|
|
|
2018-05-26 16:35:42 +02:00
|
|
|
public static HtmlString ToBrowserDate(this DateTimeOffset date)
|
2018-05-26 16:32:20 +02:00
|
|
|
{
|
2021-04-01 04:55:43 +02:00
|
|
|
var displayDate = date.ToString("o", CultureInfo.InvariantCulture);
|
2021-03-07 23:51:50 +01:00
|
|
|
return new HtmlString($"<span class='localizeDate'>{displayDate}</span>");
|
2018-05-26 16:32:20 +02:00
|
|
|
}
|
2018-05-26 16:42:55 +02:00
|
|
|
|
2021-03-07 23:51:50 +01:00
|
|
|
public static HtmlString ToBrowserDate(this DateTime date)
|
2019-05-15 15:02:03 +02:00
|
|
|
{
|
2021-04-01 04:55:43 +02:00
|
|
|
var displayDate = date.ToString("o", CultureInfo.InvariantCulture);
|
2021-03-07 23:51:50 +01:00
|
|
|
return new HtmlString($"<span class='localizeDate'>{displayDate}</span>");
|
2019-05-15 15:02:03 +02:00
|
|
|
}
|
2021-03-07 23:51:50 +01:00
|
|
|
|
2018-05-28 16:36:49 +02:00
|
|
|
public static string ToTimeAgo(this DateTimeOffset date)
|
2018-05-26 16:42:55 +02:00
|
|
|
{
|
|
|
|
var formatted = (DateTimeOffset.UtcNow - date).TimeString() + " ago";
|
|
|
|
return formatted;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static string TimeString(this TimeSpan timeSpan)
|
|
|
|
{
|
|
|
|
if (timeSpan.TotalMinutes < 1)
|
|
|
|
{
|
|
|
|
return $"{(int)timeSpan.TotalSeconds} second{Plural((int)timeSpan.TotalSeconds)}";
|
|
|
|
}
|
|
|
|
if (timeSpan.TotalHours < 1)
|
|
|
|
{
|
|
|
|
return $"{(int)timeSpan.TotalMinutes} minute{Plural((int)timeSpan.TotalMinutes)}";
|
|
|
|
}
|
|
|
|
if (timeSpan.Days < 1)
|
|
|
|
{
|
|
|
|
return $"{(int)timeSpan.TotalHours} hour{Plural((int)timeSpan.TotalHours)}";
|
|
|
|
}
|
|
|
|
return $"{(int)timeSpan.TotalDays} day{Plural((int)timeSpan.TotalDays)}";
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string Plural(int totalDays)
|
|
|
|
{
|
|
|
|
return totalDays > 1 ? "s" : string.Empty;
|
|
|
|
}
|
2018-04-19 22:42:12 +02:00
|
|
|
}
|
|
|
|
}
|