Skip to content

Commit

Permalink
RenderRequireJsSetup resolves server path for config files
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprodan committed Oct 24, 2013
1 parent 1b369d2 commit b9e8dcb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
28 changes: 16 additions & 12 deletions RequireJsNet.Docs/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,22 @@
</div>
</footer>

@if (HttpContext.Current.IsDebuggingEnabled)
{
@Html.RenderRequireJsSetup(Url.Content("~/Scripts"),
Url.Content("~/Scripts/require.js"),
new List<string> {Server.MapPath("~/RequireJS.shared.config"), Server.MapPath("~/RequireJS.config")})
}
else
{
@Html.RenderRequireJsSetup(Url.Content("~/Scripts"),
Url.Content("~/Scripts/require.js"),
Server.MapPath("~/RequireJS.release.config"))
}
@if (HttpContext.Current.IsDebuggingEnabled)
{
@Html.RenderRequireJsSetup(Url.Content("~/Scripts"),
Url.Content("~/Scripts/require.js"),
new []
{
"~/RequireJS.shared.config",
"~/RequireJS.config"
})
}
else
{
@Html.RenderRequireJsSetup(Url.Content("~/Scripts"),
Url.Content("~/Scripts/require.js"),
Server.MapPath("~/RequireJS.release.config"))
}

</body>
</html>
36 changes: 31 additions & 5 deletions RequireJsNet/RequireJsHtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class RequireJsHtmlHelpers
/// Setup RequireJS to be used in layouts
/// </summary>
/// <example>
/// @Html.RenderRequireJsSetup(Url.Content("~/Scripts"), Url.Content("~/Scripts/require.js"), Server.MapPath("~/RequireJS.release.config"))
/// @Html.RenderRequireJsSetup(Url.Content("~/Scripts"), Url.Content("~/Scripts/require.js"), "~/RequireJS.release.config")
/// </example>
/// <param name="baseUrl">Scrips folder</param>
/// <param name="requireUrl">requirejs.js url</param>
Expand All @@ -33,9 +33,15 @@ public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string ba
string configPath = "")
{
var setupHtml = new StringBuilder();

var entryPointPath = html.RequireJsEntryPoint();

//resolve config path
if (!string.IsNullOrEmpty(configPath) && configPath.StartsWith("~"))
{
configPath = html.ViewContext.HttpContext.Server.MapPath(configPath);
}

if (entryPointPath != null)
{
setupHtml.AppendLine("<script type=\"text/javascript\">");
Expand Down Expand Up @@ -70,14 +76,16 @@ public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string ba
/// </summary>
/// <param name="baseUrl">Scrips folder</param>
/// <param name="requireUrl">requirejs.js url</param>
/// <param name="configsList">RequireJS.config files server local paths</param>
/// <param name="configsList">RequireJS.config files path</param>
public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl,
IList<string> configsList)
{
var setupHtml = new StringBuilder();

var entryPointPath = html.RequireJsEntryPoint();

var configs = MapPath(html, configsList);

if (entryPointPath != null)
{
setupHtml.AppendLine("<script type=\"text/javascript\">");
Expand All @@ -93,9 +101,9 @@ public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string ba
setupHtml.AppendLine(",");
setupHtml.Append("baseUrl:'" + baseUrl + "'");
setupHtml.AppendLine(",");
setupHtml.Append("paths:" + html.GetRequireJsPaths(configsList));
setupHtml.Append("paths:" + html.GetRequireJsPaths(configs));
setupHtml.AppendLine(",");
setupHtml.AppendLine("shim:" + html.GetRequireJsShim(configsList));
setupHtml.AppendLine("shim:" + html.GetRequireJsShim(configs));
setupHtml.AppendLine("};");

setupHtml.AppendLine("</script>");
Expand Down Expand Up @@ -281,5 +289,23 @@ public static Dictionary<string, int> ToJsonDictionary<TEnum>()
var names = Enum.GetNames(enumType);
return Enum.GetNames(enumType).ToDictionary(r => r, r => Convert.ToInt32(Enum.Parse(enumType, r)));
}

private static List<string> MapPath(HtmlHelper html, IList<string> configsList)
{
var list = new List<string>();
foreach (var item in configsList)
{
if(item.StartsWith("~"))
{
list.Add(html.ViewContext.HttpContext.Server.MapPath(item));
}
else
{
list.Add(item);
}
}

return list;
}
}
}

0 comments on commit b9e8dcb

Please sign in to comment.