From 487773d5397c8a13c9374c5337064d12a49ba091 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Mon, 14 Oct 2013 12:35:55 +0300 Subject: [PATCH] Added optional param configPath to RenderRequireJsSetup method --- RequireJsNet.Docs/Views/Shared/_Layout.cshtml | 2 +- RequireJsNet/RequireJsHtmlHelpers.cs | 59 +++++++++---------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/RequireJsNet.Docs/Views/Shared/_Layout.cshtml b/RequireJsNet.Docs/Views/Shared/_Layout.cshtml index 1893695..cc3b063 100644 --- a/RequireJsNet.Docs/Views/Shared/_Layout.cshtml +++ b/RequireJsNet.Docs/Views/Shared/_Layout.cshtml @@ -48,6 +48,6 @@ - @Html.RenderRequireJsSetup(Url.Content("~/Scripts"), Url.Content("~/Scripts/require.js")) + @Html.RenderRequireJsSetup(Url.Content("~/Scripts"), Url.Content("~/Scripts/require.js"), Server.MapPath("~/RequireJS.release.config")) diff --git a/RequireJsNet/RequireJsHtmlHelpers.cs b/RequireJsNet/RequireJsHtmlHelpers.cs index b890a2c..a55ad42 100644 --- a/RequireJsNet/RequireJsHtmlHelpers.cs +++ b/RequireJsNet/RequireJsHtmlHelpers.cs @@ -18,7 +18,16 @@ namespace RequireJS { public static class RequireJsHtmlHelpers { - public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl) + /// + /// Setup RequireJS to be used in layouts + /// + /// + /// @Html.RenderRequireJsSetup(Url.Content("~/Scripts"), Url.Content("~/Scripts/require.js"), Server.MapPath("~/RequireJS.release.config")) + /// + /// Scrips folder + /// requirejs.js url + /// RequireJS.config server local path + public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl, string configPath = "") { var setupHtml = new StringBuilder(); @@ -39,9 +48,9 @@ public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string ba setupHtml.AppendLine(","); setupHtml.Append("baseUrl:'" + baseUrl + "'"); setupHtml.AppendLine(","); - setupHtml.Append("paths:" + html.GetRequireJsPaths()); + setupHtml.Append("paths:" + html.GetRequireJsPaths(configPath)); setupHtml.AppendLine(","); - setupHtml.AppendLine("shim:" + html.GetRequireJsShim()); + setupHtml.AppendLine("shim:" + html.GetRequireJsShim(configPath)); setupHtml.AppendLine("};"); setupHtml.AppendLine(""); @@ -67,26 +76,20 @@ public static MvcHtmlString RequireJsEntryPoint(this HtmlHelper html) return File.Exists(filePath) ? new MvcHtmlString(entryPoint) : null; } - public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html) + public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html, string configPath = "") { - var cfg = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.config"); - - if (!File.Exists(cfg)) + if (string.IsNullOrEmpty(configPath)) { - throw new FileNotFoundException("RequireJS config not found", cfg); + configPath = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.config"); } - var result = new StringBuilder(); - var paths = XDocument.Load(cfg).Descendants("paths").Descendants("path"); - -#if !DEBUG - - var cfgRelease = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.Release.config"); - if (File.Exists(cfgRelease)) + if (!File.Exists(configPath)) { - paths = XDocument.Load(cfgRelease).Descendants("paths").Descendants("path"); + throw new FileNotFoundException("RequireJS config not found", configPath); } -#endif + + var result = new StringBuilder(); + var paths = XDocument.Load(configPath).Descendants("paths").Descendants("path"); result.Append("{"); foreach (var item in paths) @@ -98,25 +101,21 @@ public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html) return new MvcHtmlString(result.ToString()); } - public static MvcHtmlString GetRequireJsShim(this HtmlHelper html) + public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, string configPath = "") { - var cfg = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.config"); - - if (!File.Exists(cfg)) + if (string.IsNullOrEmpty(configPath)) { - throw new FileNotFoundException("RequireJS config not found", cfg); + configPath = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.config"); } - var result = new StringBuilder(); - var shims = XDocument.Load(cfg).Descendants("shim").Descendants("dependencies"); -#if !DEBUG - - var cfgRelease = html.ViewContext.HttpContext.Server.MapPath("~/RequireJS.Release.config"); - if (File.Exists(cfgRelease)) + if (!File.Exists(configPath)) { - shims = XDocument.Load(cfg).Descendants("shim").Descendants("dependencies"); + throw new FileNotFoundException("RequireJS config not found", configPath); } -#endif + + var result = new StringBuilder(); + var shims = XDocument.Load(configPath).Descendants("shim").Descendants("dependencies"); + result.Append("{"); foreach (var item in shims) {