Refactoring
This commit is contained in:
80
AMWD.Common.AspNetCore/Extensions/HtmlExtensions.cs
Normal file
80
AMWD.Common.AspNetCore/Extensions/HtmlExtensions.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
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>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user