Skip to content

Commit

Permalink
Overload for RenderRequireJsSetup to accept a list of config files, w…
Browse files Browse the repository at this point in the history
…ill merge all paths and shims, will throw error if duplicates are found
  • Loading branch information
stefanprodan committed Oct 16, 2013
1 parent bc5cbfc commit a90a292
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 31 deletions.
21 changes: 21 additions & 0 deletions RequireJsNet.Docs/RequireJS.Shared.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<paths>
<path key="jquery" value="lib/vendor/jquery/jquery" />
<path key="jquery-migrate-core" value="lib/vendor/jquery/plugins/jquery-migrate-core" />
<path key="jquery-ui" value="lib/vendor/jquery/ui/jquery-ui" />
<path key="jquery-ui-i18n" value="lib/vendor/jquery/ui/jquery-ui-i18n" />
</paths>
<shim>
<dependencies for="jquery-migrate-core" exports="">
<add dependency="jquery"/>
</dependencies>
<dependencies for="jquery-ui" exports="">
<add dependency="jquery"/>
</dependencies>
<dependencies for="jquery-ui-i18n" exports="">
<add dependency="jquery"/>
<add dependency="jquery-ui"/>
</dependencies>
</shim>
</configuration>
14 changes: 0 additions & 14 deletions RequireJsNet.Docs/RequireJS.config
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<paths>
<path key="jquery" value="lib/vendor/jquery/jquery" />
<path key="jquery-migrate-core" value="lib/vendor/jquery/plugins/jquery-migrate-core" />
<path key="jquery-ui" value="lib/vendor/jquery/ui/jquery-ui" />
<path key="jquery-ui-i18n" value="lib/vendor/jquery/ui/jquery-ui-i18n" />
<path key="jquery-mobile" value="lib/vendor/jquery/mobile/jquery.mobile" />
<path key="jquery-validate" value="lib/vendor/jquery/plugins/validate/jquery.validate" />
<path key="jquery-validate-unobtrusive" value="lib/vendor/jquery/plugins/validate/jquery.validate.unobtrusive" />
<path key="amplify" value="lib/vendor/amplify/amplify" />
<path key="text" value="lib/vendor/requirejs/text" />
</paths>
<shim>
<dependencies for="jquery-migrate-core" exports="">
<add dependency="jquery"/>
</dependencies>
<dependencies for="jquery-ui" exports="">
<add dependency="jquery"/>
</dependencies>
<dependencies for="jquery-ui-i18n" exports="">
<add dependency="jquery"/>
<add dependency="jquery-ui"/>
</dependencies>
<dependencies for="jquery-mobile" exports="">
<add dependency="jquery"/>
</dependencies>
Expand Down
1 change: 1 addition & 0 deletions RequireJsNet.Docs/RequireJsNet.Docs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
<Content Include="Scripts\Controllers\Root\Home\home-index.js" />
<Content Include="RequireJS.config" />
<Content Include="RequireJS.Release.config" />
<Content Include="RequireJS.Shared.config" />
<None Include="Scripts\Lib\Vendor\Amplify\amplify-vsdoc.js" />
<Content Include="Scripts\keepalive-service.js" />
<Content Include="Scripts\Lib\Vendor\Amplify\amplify.js" />
Expand Down
8 changes: 4 additions & 4 deletions RequireJsNet.Docs/Views/Shared/_Layout.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@
@if (HttpContext.Current.IsDebuggingEnabled)
{
@Html.RenderRequireJsSetup(Url.Content("~/Scripts"),
Url.Content("~/Scripts/require.js"),
Server.MapPath("~/RequireJS.config"))
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"))
Url.Content("~/Scripts/require.js"),
Server.MapPath("~/RequireJS.release.config"))
}

</body>
Expand Down
153 changes: 140 additions & 13 deletions RequireJsNet/RequireJsHtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*/

using System.Data;
using System.IO;
using System.Xml.Linq;
using System.Collections.Generic;
Expand All @@ -27,16 +29,17 @@ public static class RequireJsHtmlHelpers
/// <param name="baseUrl">Scrips folder</param>
/// <param name="requireUrl">requirejs.js url</param>
/// <param name="configPath">RequireJS.config server local path</param>
public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl, string configPath = "")
public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl,
string configPath = "")
{
var setupHtml = new StringBuilder();

var entryPointPath = html.RequireJsEntryPoint();

if(entryPointPath != null)
if (entryPointPath != null)
{
setupHtml.AppendLine("<script type=\"text/javascript\">");

setupHtml.AppendLine("var requireConfig = {");
setupHtml.Append("pageOptions:" + RequireJsOptions.ConvertToJsObject(html.ViewBag.PageOptions));
setupHtml.AppendLine(",");
Expand All @@ -62,17 +65,59 @@ public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string ba
return new MvcHtmlString(setupHtml.ToString());
}

/// <summary>
/// Setup RequireJS to be used in layouts
/// </summary>
/// <param name="baseUrl">Scrips folder</param>
/// <param name="requireUrl">requirejs.js url</param>
/// <param name="configsList">RequireJS.config files server local paths</param>
public static MvcHtmlString RenderRequireJsSetup(this HtmlHelper html, string baseUrl, string requireUrl,
IList<string> configsList)
{
var setupHtml = new StringBuilder();

var entryPointPath = html.RequireJsEntryPoint();

if (entryPointPath != null)
{
setupHtml.AppendLine("<script type=\"text/javascript\">");

setupHtml.AppendLine("var requireConfig = {");
setupHtml.Append("pageOptions:" + RequireJsOptions.ConvertToJsObject(html.ViewBag.PageOptions));
setupHtml.AppendLine(",");
setupHtml.AppendLine("websiteOptions:" + RequireJsOptions.ConvertToJsObject(html.ViewBag.GlobalOptions));
setupHtml.AppendLine("};");

setupHtml.AppendLine("var require = {");
setupHtml.Append("locale:'" + html.CurrentCulture() + "'");
setupHtml.AppendLine(",");
setupHtml.Append("baseUrl:'" + baseUrl + "'");
setupHtml.AppendLine(",");
setupHtml.Append("paths:" + html.GetRequireJsPaths(configsList));
setupHtml.AppendLine(",");
setupHtml.AppendLine("shim:" + html.GetRequireJsShim(configsList));
setupHtml.AppendLine("};");

setupHtml.AppendLine("</script>");

setupHtml.AppendLine("<script data-main=\"" + entryPointPath + "\" src=\"" + requireUrl + "\">");
setupHtml.AppendLine("</script>");
}

return new MvcHtmlString(setupHtml.ToString());
}

public static MvcHtmlString RequireJsEntryPoint(this HtmlHelper html)
{
var area = html.ViewContext.RouteData.DataTokens["area"] != null
? html.ViewContext.RouteData.DataTokens["area"].ToString()
: "Root";
? html.ViewContext.RouteData.DataTokens["area"].ToString()
: "Root";
var controller = html.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue as string;
var action = html.ViewContext.Controller.ValueProvider.GetValue("action").RawValue as string;
var entryPointTmpl = "Controllers/{0}/" + controller + "/" + controller + "-" + action;
var entryPointTmpl = "Controllers/{0}/" + controller + "/" + controller + "-" + action;
var entryPoint = string.Format(entryPointTmpl, area);
var filePath = html.ViewContext.HttpContext.Server.MapPath("~/Scripts/" + entryPoint + ".js");
if (File.Exists(filePath))
if (File.Exists(filePath))
{
return new MvcHtmlString(entryPoint);
}
Expand All @@ -99,13 +144,48 @@ public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html, string confi
result.Append("{");
foreach (var item in paths)
{
result.AppendFormat("\"{0}\":\"{1}\"{2}", item.Attribute("key").Value.Trim(), item.Attribute("value").Value.Trim(), paths.Last() == item ? "" : ",");
result.AppendFormat("\"{0}\":\"{1}\"{2}", item.Attribute("key").Value.Trim(),
item.Attribute("value").Value.Trim(), paths.Last() == item ? "" : ",");
}
result.Append("}");

return new MvcHtmlString(result.ToString());
}

public static MvcHtmlString GetRequireJsPaths(this HtmlHelper html, IList<string> configsList)
{
var pathList = new List<string>();

var result = new StringBuilder();
result.Append("{");
foreach (var configPath in configsList)
{
if (!File.Exists(configPath))
{
throw new FileNotFoundException("RequireJS config not found", configPath);
}

var paths = XDocument.Load(configPath).Descendants("paths").Descendants("path");
foreach (var item in paths)
{
//check unique name
var name = item.Attribute("key").Value.Trim();
if (pathList.Contains(name))
{
throw new DuplicateNameException(name + " duplicate path found in " + configPath);
}
pathList.Add(name);

result.AppendFormat("\"{0}\":\"{1}\"{2}", item.Attribute("key").Value.Trim(),
item.Attribute("value").Value.Trim(),
(paths.Last() == item && configsList.Last() == configPath) ? "" : ",");
}

}
result.Append("}");
return new MvcHtmlString(result.ToString());
}

public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, string configPath = "")
{
if (string.IsNullOrEmpty(configPath))
Expand All @@ -128,12 +208,14 @@ public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, string config
var deps = item.Descendants("add");
foreach (var dep in deps)
{
result.AppendFormat("\"{0}\"{1}", dep.Attribute("dependency").Value.Trim(), deps.Last() == dep ? "" : ",");
result.AppendFormat("\"{0}\"{1}", dep.Attribute("dependency").Value.Trim(),
deps.Last() == dep ? "" : ",");
}

var exports = item.Attribute("exports") != null && !string.IsNullOrEmpty(item.Attribute("exports").Value)
? ", exports: '" + item.Attribute("exports").Value.Trim() + "'"
: string.Empty;
var exports = item.Attribute("exports") != null &&
!string.IsNullOrEmpty(item.Attribute("exports").Value)
? ", exports: '" + item.Attribute("exports").Value.Trim() + "'"
: string.Empty;

result.AppendFormat("]{0} {1}{2} ", exports, "}", shims.Last() == item ? "" : ",");
}
Expand All @@ -142,6 +224,51 @@ public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, string config
return new MvcHtmlString(result.ToString());
}

public static MvcHtmlString GetRequireJsShim(this HtmlHelper html, IList<string> configsList)
{
var shimList = new List<string>();
var result = new StringBuilder();
result.Append("{");
foreach (var configPath in configsList)
{
if (!File.Exists(configPath))
{
throw new FileNotFoundException("RequireJS config not found", configPath);
}

var shims = XDocument.Load(configPath).Descendants("shim").Descendants("dependencies");
foreach (var item in shims)
{
//check unique name
var name = item.Attribute("for").Value.Trim();
if (shimList.Contains(name))
{
throw new DuplicateNameException(name + " duplicate shim found in " + configPath);
}
shimList.Add(name);

result.AppendFormat(" \"{0}\": {1} deps: [", item.Attribute("for").Value.Trim(), "{");
var deps = item.Descendants("add");
foreach (var dep in deps)
{
result.AppendFormat("\"{0}\"{1}", dep.Attribute("dependency").Value.Trim(),
deps.Last() == dep ? "" : ",");
}

var exports = item.Attribute("exports") != null &&
!string.IsNullOrEmpty(item.Attribute("exports").Value)
? ", exports: '" + item.Attribute("exports").Value.Trim() + "'"
: string.Empty;

result.AppendFormat("]{0} {1}{2} ", exports, "}",
(shims.Last() == item && configsList.Last() == configPath) ? "" : ",");
}
}
result.Append("}");

return new MvcHtmlString(result.ToString());
}

public static string CurrentCulture(this HtmlHelper html)
{
// split the ro-Ro string by '-' so it returns eg. ro / en
Expand All @@ -150,7 +277,7 @@ public static string CurrentCulture(this HtmlHelper html)

public static Dictionary<string, int> ToJsonDictionary<TEnum>()
{
var enumType = typeof(TEnum);
var enumType = typeof (TEnum);
var names = Enum.GetNames(enumType);
return Enum.GetNames(enumType).ToDictionary(r => r, r => Convert.ToInt32(Enum.Parse(enumType, r)));
}
Expand Down

0 comments on commit a90a292

Please sign in to comment.