public static class ResourceAggregatorHelper
{
public static string CSSKey = "pageCSSList";
public static string JSKey = "pageJSList";
public static string TmplKey = "pageTmplList";
private static void AddToDictionary(HtmlHelper helper, string[] files, string key)
{
if (files == null || files.Length == 0)
return;
TempDataDictionary dict = helper.ViewContext.TempData;
if (!dict.ContainsKey(key))
dict.Add(key, new List<string>());
(dict[key] as List<string>).AddRange(files);
}
private static void InsertToDictionary(HtmlHelper helper, string[] files, string key)
{
if (files == null || files.Length == 0)
return;
TempDataDictionary dict = helper.ViewContext.TempData;
if (!dict.ContainsKey(key))
dict.Add(key, new List<string>());
(dict[key] as List<string>).InsertRange(0, files);
}
private static string GetCSVFromDictionary(HtmlHelper helper, string key)
{
if (!helper.ViewContext.TempData.ContainsKey(key))
return String.Empty;
else
return String.Join(",", (helper.ViewContext.TempData[key] as List<string>));
}
public static void AddCSS(this HtmlHelper helper, params string[] files)
{
AddToDictionary(helper, files, CSSKey);
}
public static void AddCSSToTop(this HtmlHelper helper, params string[] files)
{
InsertToDictionary(helper, files, CSSKey);
}
public static string GetCSS(this HtmlHelper helper)
{
return GetCSVFromDictionary(helper, CSSKey);
}
public static MvcHtmlString GetCSSLink(this HtmlHelper helper, bool ignoreCache = false)
{
if (!helper.ViewContext.TempData.ContainsKey(CSSKey))
return MvcHtmlString.Empty;
else
{
UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
var url = urlHelper.RouteUrl("resCSS", new { area = "", fileCsv = helper.Encode(GetCSVFromDictionary(helper, CSSKey)) });
var link = "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />".FormatWith(url);
return MvcHtmlString.Create(link);
}
}
public static void AddJS(this HtmlHelper helper, params string[] files)
{
AddToDictionary(helper, files, JSKey);
}
public static void AddJSToTop(this HtmlHelper helper, params string[] files)
{
InsertToDictionary(helper, files, JSKey);
}
public static string GetJS(this HtmlHelper helper)
{
return GetCSVFromDictionary(helper, JSKey);
}
public static MvcHtmlString GetJSLink(this HtmlHelper helper, bool ignoreCache = false)
{
if (!helper.ViewContext.TempData.ContainsKey(JSKey))
return MvcHtmlString.Empty;
else
{
UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
var url = urlHelper.RouteUrl("resJS", new { area = "", fileCsv = helper.Encode(GetCSVFromDictionary(helper, JSKey)) });
var link = "<script src=\"{0}\" type=\"text/javascript\"></script>".FormatWith(url);
return MvcHtmlString.Create(link);
}
}
public static void AddTmpl(this HtmlHelper helper, params string[] files)
{
AddToDictionary(helper, files, TmplKey);
}
public static void AddTmplToTop(this HtmlHelper helper, params string[] files)
{
InsertToDictionary(helper, files, TmplKey);
}
public static string GetTmpl(this HtmlHelper helper)
{
return GetCSVFromDictionary(helper, TmplKey);
}
public static MvcHtmlString GetTmplLink(this HtmlHelper helper, bool ignoreCache = false)
{
if (!helper.ViewContext.TempData.ContainsKey(TmplKey))
return MvcHtmlString.Empty;
else
{
UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
var url = urlHelper.RouteUrl("resTmpl", new { area = "", fileCsv = helper.Encode(GetCSVFromDictionary(helper, TmplKey)) });
return MvcHtmlString.Create(url);
}
}
}