using learun.util;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Text;
namespace EC.Web
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.09.02
/// 描 述:对HtmlHelper类进行扩展
///
public static class HtmlHelperExtensions
{
///
/// 往页面中写入js文件
///
/// 需要扩展对象
/// 文件路径
///
public static HtmlString AppendJsFile(this IHtmlHelper htmlHelper, params string[] jsFiles)
{
if (htmlHelper is null)
{
throw new System.ArgumentNullException(nameof(htmlHelper));
}
string jsFile = "";
foreach (string file in jsFiles)
{
if (jsFile != "")
{
jsFile += ",";
}
jsFile += file;
}
string jsStr = "";
if (ConfigHelper.GetValue("env") != "dev")
{
jsStr = ConfigHelper.GetCache(jsFile);
}
if (string.IsNullOrEmpty(jsStr))
{
jsStr = JsCssHelper.ReadJSFile(jsFiles);
ConfigHelper.SetCache(jsFile, jsStr);
}
StringBuilder content = new StringBuilder();
string jsFormat = "";
content.AppendFormat(jsFormat, jsStr);
return new HtmlString(content.ToString());
}
///
/// 往页面中写入css样式
///
/// 需要扩展对象
/// 文件路径
///
public static HtmlString AppendCssFile(this IHtmlHelper htmlHelper, params string[] cssFiles)
{
if (htmlHelper is null)
{
throw new System.ArgumentNullException(nameof(htmlHelper));
}
string cssFile = "";
foreach (string file in cssFiles)
{
if (cssFile != "")
{
cssFile += ",";
}
cssFile += file;
}
string cssStr = "";
if (ConfigHelper.GetValue("env") != "dev")
{
cssStr = ConfigHelper.GetCache(cssFile);
}
if (string.IsNullOrEmpty(cssStr))
{
var url = "";
cssStr = JsCssHelper.ReadCssFile(cssFiles);
if (url != "/")
{
cssStr = cssStr.Replace("url(", "url(" + url);
}
ConfigHelper.SetCache(cssFile, cssStr);
}
StringBuilder content = new StringBuilder();
string cssFormat = "";
content.AppendFormat(cssFormat, cssStr);
return new HtmlString(content.ToString());
}
public static HtmlString Js(this IHtmlHelper htmlHelper, string strJs)
{
if (htmlHelper is null)
{
throw new System.ArgumentNullException(nameof(htmlHelper));
}
string jsStr = JsCssHelper.CompressJS(strJs);
StringBuilder content = new StringBuilder();
string jsFormat = "";
content.AppendFormat(jsFormat, jsStr);
return new HtmlString(content.ToString());
}
public static HtmlString Css(this IHtmlHelper htmlHelper, string strCss)
{
if (htmlHelper is null)
{
throw new System.ArgumentNullException(nameof(htmlHelper));
}
string cssStr = JsCssHelper.CompressCss(strCss);
StringBuilder content = new StringBuilder();
string cssFormat = "";
content.AppendFormat(cssFormat, cssStr);
return new HtmlString(content.ToString());
}
///
/// 获取虚拟目录
///
///
public static string GetVirtualPath(this IHtmlHelper htmlHelper)
{
if (htmlHelper is null)
{
throw new System.ArgumentNullException(nameof(htmlHelper));
}
return ConfigHelper.GetConfig().VirtualPath;
}
#region 权限模块
///
/// 设置当前页面地址
///
/// param >
/// < returns > returns >
public static HtmlString SetCurrentUrl(this IHtmlHelper htmlHelper)
{
if (htmlHelper is null)
{
throw new System.ArgumentNullException(nameof(htmlHelper));
}
string mouldeCode = ContextHelper.GetItem("mouldeCode") as string;
return new HtmlString("");
}
#endregion 权限模块
}
}