82 lines
3.0 KiB
C#
82 lines
3.0 KiB
C#
using System;
|
|
using Microsoft.AspNetCore.Mvc.Razor;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
|
|
namespace AMWD.Common.AspNetCore.Extensions
|
|
{
|
|
/// <summary>
|
|
/// Extensions for the HTML (e.g. <see cref="IHtmlHelper"/>).
|
|
/// </summary>
|
|
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
|
|
public static class HtmlExtensions
|
|
{
|
|
/// <summary>
|
|
/// The prefix used to identify JavaScript parts.
|
|
/// </summary>
|
|
public static string JSPrefix { get; set; } = "_JS_";
|
|
|
|
/// <summary>
|
|
/// The prefix used to identify CascadingStyleSheet parts.
|
|
/// </summary>
|
|
public static string CSSPrefix { get; set; } = "_CSS_";
|
|
|
|
/// <summary>
|
|
/// Add a js snippet.
|
|
/// </summary>
|
|
/// <typeparam name="T">The dynamic type of the <see cref="IHtmlHelper"/>.</typeparam>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance.</param>
|
|
/// <param name="template">The template to use to add the snippet.</param>
|
|
/// <returns></returns>
|
|
public static T AddJS<T>(this IHtmlHelper<T> htmlHelper, Func<object, HelperResult> template)
|
|
{
|
|
htmlHelper.ViewContext.HttpContext.Items[$"{JSPrefix}{Guid.NewGuid()}"] = template;
|
|
return default;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Renders the js snippets into the view.
|
|
/// </summary>
|
|
/// <typeparam name="T">The dynamic type of the <see cref="IHtmlHelper"/>.</typeparam>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance.</param>
|
|
/// <returns></returns>
|
|
public static T RenderJS<T>(this IHtmlHelper<T> htmlHelper)
|
|
{
|
|
foreach (object key in htmlHelper.ViewContext.HttpContext.Items.Keys)
|
|
{
|
|
if (key.ToString().StartsWith(JSPrefix) && htmlHelper.ViewContext.HttpContext.Items[key] is Func<object, HelperResult> template)
|
|
htmlHelper.ViewContext.Writer.WriteLine(template(null));
|
|
}
|
|
return default;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a css snippet.
|
|
/// </summary>
|
|
/// <typeparam name="T">The dynamic type of the <see cref="IHtmlHelper"/>.</typeparam>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance.</param>
|
|
/// <param name="template">The template to use to add the snippet.</param>
|
|
/// <returns></returns>
|
|
public static T AddCSS<T>(this IHtmlHelper<T> htmlHelper, Func<object, HelperResult> template)
|
|
{
|
|
htmlHelper.ViewContext.HttpContext.Items[$"{CSSPrefix}{Guid.NewGuid()}"] = template;
|
|
return default;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Renders the css snippets into the view.
|
|
/// </summary>
|
|
/// <typeparam name="T">The dynamic type of the <see cref="IHtmlHelper"/>.</typeparam>
|
|
/// <param name="htmlHelper">The <see cref="IHtmlHelper"/> instance.</param>
|
|
/// <returns></returns>
|
|
public static T RenderCSS<T>(this IHtmlHelper<T> htmlHelper)
|
|
{
|
|
foreach (object key in htmlHelper.ViewContext.HttpContext.Items.Keys)
|
|
{
|
|
if (key.ToString().StartsWith(CSSPrefix) && htmlHelper.ViewContext.HttpContext.Items[key] is Func<object, HelperResult> template)
|
|
htmlHelper.ViewContext.Writer.WriteLine(template(null));
|
|
}
|
|
return default;
|
|
}
|
|
}
|
|
}
|