Advertisement
Guest User

ResourceAggregatorHelper

a guest
Jul 9th, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.58 KB | None | 0 0
  1. public static class ResourceAggregatorHelper
  2.     {
  3.         public static string CSSKey = "pageCSSList";
  4.         public static string JSKey = "pageJSList";
  5.         public static string TmplKey = "pageTmplList";        
  6.  
  7.         private static void AddToDictionary(HtmlHelper helper, string[] files, string key)
  8.         {
  9.             if (files == null || files.Length == 0)
  10.                 return;
  11.  
  12.             TempDataDictionary dict = helper.ViewContext.TempData;
  13.             if (!dict.ContainsKey(key))
  14.                 dict.Add(key, new List<string>());
  15.  
  16.             (dict[key] as List<string>).AddRange(files);
  17.         }
  18.  
  19.         private static void InsertToDictionary(HtmlHelper helper, string[] files, string key)
  20.         {
  21.             if (files == null || files.Length == 0)
  22.                 return;
  23.  
  24.             TempDataDictionary dict = helper.ViewContext.TempData;
  25.             if (!dict.ContainsKey(key))
  26.                 dict.Add(key, new List<string>());
  27.  
  28.             (dict[key] as List<string>).InsertRange(0, files);
  29.         }
  30.  
  31.         private static string GetCSVFromDictionary(HtmlHelper helper, string key)
  32.         {
  33.             if (!helper.ViewContext.TempData.ContainsKey(key))
  34.                 return String.Empty;
  35.             else
  36.                 return String.Join(",", (helper.ViewContext.TempData[key] as List<string>));
  37.         }
  38.  
  39.         public static void AddCSS(this HtmlHelper helper, params string[] files)
  40.         {
  41.             AddToDictionary(helper, files, CSSKey);
  42.         }
  43.  
  44.         public static void AddCSSToTop(this HtmlHelper helper, params string[] files)
  45.         {
  46.             InsertToDictionary(helper, files, CSSKey);
  47.         }
  48.  
  49.         public static string GetCSS(this HtmlHelper helper)
  50.         {
  51.             return GetCSVFromDictionary(helper, CSSKey);
  52.         }
  53.  
  54.         public static MvcHtmlString GetCSSLink(this HtmlHelper helper, bool ignoreCache = false)
  55.         {
  56.             if (!helper.ViewContext.TempData.ContainsKey(CSSKey))
  57.                 return MvcHtmlString.Empty;
  58.             else
  59.             {
  60.                 UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
  61.                 var url = urlHelper.RouteUrl("resCSS", new { area = "", fileCsv = helper.Encode(GetCSVFromDictionary(helper, CSSKey)) });
  62.                 var link = "<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />".FormatWith(url);
  63.                 return MvcHtmlString.Create(link);
  64.             }
  65.         }
  66.  
  67.         public static void AddJS(this HtmlHelper helper, params string[] files)
  68.         {
  69.             AddToDictionary(helper, files, JSKey);
  70.         }
  71.  
  72.         public static void AddJSToTop(this HtmlHelper helper, params string[] files)
  73.         {
  74.             InsertToDictionary(helper, files, JSKey);
  75.         }
  76.  
  77.         public static string GetJS(this HtmlHelper helper)
  78.         {
  79.             return GetCSVFromDictionary(helper, JSKey);
  80.         }
  81.  
  82.         public static MvcHtmlString GetJSLink(this HtmlHelper helper, bool ignoreCache = false)
  83.         {
  84.             if (!helper.ViewContext.TempData.ContainsKey(JSKey))
  85.                 return MvcHtmlString.Empty;
  86.             else
  87.             {
  88.                 UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
  89.                 var url = urlHelper.RouteUrl("resJS", new { area = "", fileCsv = helper.Encode(GetCSVFromDictionary(helper, JSKey)) });
  90.                 var link = "<script src=\"{0}\" type=\"text/javascript\"></script>".FormatWith(url);
  91.                 return MvcHtmlString.Create(link);
  92.             }
  93.         }
  94.  
  95.         public static void AddTmpl(this HtmlHelper helper, params string[] files)
  96.         {
  97.             AddToDictionary(helper, files, TmplKey);
  98.         }
  99.  
  100.         public static void AddTmplToTop(this HtmlHelper helper, params string[] files)
  101.         {
  102.             InsertToDictionary(helper, files, TmplKey);
  103.         }
  104.  
  105.         public static string GetTmpl(this HtmlHelper helper)
  106.         {
  107.             return GetCSVFromDictionary(helper, TmplKey);
  108.         }
  109.  
  110.         public static MvcHtmlString GetTmplLink(this HtmlHelper helper, bool ignoreCache = false)
  111.         {
  112.             if (!helper.ViewContext.TempData.ContainsKey(TmplKey))
  113.                 return MvcHtmlString.Empty;
  114.             else
  115.             {
  116.                 UrlHelper urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
  117.                 var url = urlHelper.RouteUrl("resTmpl", new { area = "", fileCsv = helper.Encode(GetCSVFromDictionary(helper, TmplKey)) });
  118.                 return MvcHtmlString.Create(url);
  119.             }
  120.         }
  121.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement