Static class which contains javascript helper functions
Esempio n. 1
0
        public static IHtmlString Script(this HtmlHelper helper, string includeJS)
        {
            if (helper == null)
            {
                throw new ArgumentNullException("helper");
            }
            if (String.IsNullOrEmpty(includeJS))
            {
                throw new ArgumentNullException("includeJS");
            }

            var context = helper.ViewContext.HttpContext;

            var script  = ScriptBundleManager.GetScriptBundle(includeJS);
            var scripts = GetIncludedScripts(context);

            if (!scripts.Contains(script))
            {
                scripts.Add(script);

                return(new HtmlString(String.Format("    <script src=\"{0}\" type=\"text/javascript\"></script>\n",
                                                    HttpUtility.HtmlAttributeEncode(ContentHashCache.ResolveWithHash(script)))));
            }
            else
            {
                return(new HtmlString(""));
            }
        }
        public static HtmlString Stylesheet(this HtmlHelper helper, string cssUrl)
        {
            if (helper == null)
            {
                throw new ArgumentNullException("helper");
            }

            if (String.IsNullOrEmpty(cssUrl))
            {
                throw new ArgumentNullException("cssUrl");
            }

            var context = helper.ViewContext.HttpContext;

            var css      = CssBundleManager.GetCssBundle(cssUrl);
            var included = GetIncludedCssList(context);

            if (!included.Contains(css))
            {
                included.Add(css);

                return(new HtmlString(String.Format("    <link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\"/>\n",
#if !ASPNETMVC
                                                    WebUtility.HtmlEncode(ContentHashCache.ResolveWithHash(css)))));
#else
                                                    HttpUtility.HtmlAttributeEncode(ContentHashCache.ResolveWithHash(css))));
#endif
            }
            else
            {
                return(new HtmlString(""));
            }
        }
        public static IHtmlString StyleBundle(this HtmlHelper helper, string bundleKey)
        {
            if (helper == null)
            {
                throw new ArgumentNullException("helper");
            }

            if (String.IsNullOrEmpty(bundleKey))
            {
                throw new ArgumentNullException("bundleKey");
            }

            var context = helper.ViewContext.HttpContext;

            if (!CssBundleManager.IsEnabled)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var include in CssBundleManager.GetBundleIncludes(bundleKey))
                {
                    var cssUrl = include;
                    if (string.IsNullOrEmpty(cssUrl))
                    {
                        continue;
                    }

                    if (cssUrl != null && cssUrl.StartsWith("dynamic://", StringComparison.OrdinalIgnoreCase))
                    {
                        var scriptName = cssUrl.Substring(10);
                        cssUrl = DynamicScriptManager.GetScriptInclude(scriptName, ".css");
                        cssUrl = VirtualPathUtility.ToAbsolute("~/DynJS.axd/" + cssUrl);
                    }
                    else
                    {
                        cssUrl = BundleUtils.ExpandVersionVariable(cssUrl);
                        cssUrl = VirtualPathUtility.ToAbsolute(cssUrl);
                    }

                    var cssList = GetIncludedCssList(context);

                    if (!cssList.Contains(cssUrl))
                    {
                        cssList.Add(cssUrl);
                        sb.AppendLine(String.Format("    <link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\"/>\n",
#if !ASPNETMVC
                                                    WebUtility.HtmlEncode(ContentHashCache.ResolveWithHash(cssUrl))));
#else
                                                    HttpUtility.HtmlAttributeEncode(ContentHashCache.ResolveWithHash(cssUrl))));
#endif
                    }
                }

                return(new HtmlString(sb.ToString()));
            }

            return(Stylesheet(helper, "dynamic://CssBundle." + bundleKey));
        }
        public static IHtmlString ScriptBundle(this HtmlHelper helper, string bundleKey)
        {
            if (helper == null)
            {
                throw new ArgumentNullException("helper");
            }

            if (String.IsNullOrEmpty(bundleKey))
            {
                throw new ArgumentNullException("bundleKey");
            }

            var context = helper.ViewContext.HttpContext;

            if (!ScriptBundleManager.IsEnabled)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var include in ScriptBundleManager.GetBundleIncludes(bundleKey))
                {
                    var scriptUrl = include;
                    if (string.IsNullOrEmpty(scriptUrl))
                    {
                        continue;
                    }

                    if (scriptUrl != null && scriptUrl.StartsWith("dynamic://", StringComparison.OrdinalIgnoreCase))
                    {
                        var scriptName = scriptUrl.Substring(10);
                        scriptUrl = DynamicScriptManager.GetScriptInclude(scriptName);
                        scriptUrl = VirtualPathUtility.ToAbsolute("~/DynJS.axd/" + scriptUrl);
                    }
                    else
                    {
                        scriptUrl = BundleUtils.ExpandVersionVariable(scriptUrl);
                        scriptUrl = VirtualPathUtility.ToAbsolute(scriptUrl);
                    }

                    var scripts = GetIncludedScripts(context);

                    if (!scripts.Contains(scriptUrl))
                    {
                        scripts.Add(scriptUrl);
                        sb.AppendLine(String.Format("    <script src=\"{0}\" type=\"text/javascript\"></script>\n",
#if !ASPNETMVC
                                                    WebUtility.HtmlEncode(ContentHashCache.ResolveWithHash(scriptUrl))));
#else
                                                    HttpUtility.HtmlAttributeEncode(ContentHashCache.ResolveWithHash(scriptUrl))));
#endif
                    }
                }

                return(new HtmlString(sb.ToString()));
            }

            return(Script(helper, "dynamic://Bundle." + bundleKey));
        }
Esempio n. 5
0
        private static void Changed(string name)
        {
            var extension = Path.GetExtension(name);

            if (extension == null || string.Compare(extension, ".css", StringComparison.OrdinalIgnoreCase) != 0)
            {
                return;
            }

            ContentHashCache.ScriptsChanged();
            CssBundleManager.CssChanged();
        }
Esempio n. 6
0
 public static IHtmlString Stylesheet(this HtmlHelper helper, string cssUrl)
 {
     return(new HtmlString(string.Format("    <link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\"/>\n",
                                         ContentHashCache.ResolveWithHash(cssUrl))));
 }