Skip to content

Commit

Permalink
added the ability to customize entry point resolution (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
CezarCretu committed Jan 5, 2015
1 parent 7535c16 commit edf6ca5
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 68 deletions.
83 changes: 83 additions & 0 deletions RequireJsNet/EntryPointResolver/DefaultEntryPointResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using RequireJsNet.Helpers;

namespace RequireJsNet.EntryPointResolver
{
public class DefaultEntryPointResolver : IEntryPointResolver
{
private const string DefaultEntryPointRoot = "~/Scripts/";
private const string DefaultArea = "Common";

public string Resolve(ViewContext viewContext, string entryPointRoot)
{
var routingInfo = viewContext.GetRoutingInfo();
var rootUrl = string.Empty;
var withBaseUrl = true;
var server = viewContext.HttpContext.Server;

if (entryPointRoot != DefaultEntryPointRoot)
{
withBaseUrl = false;
rootUrl = UrlHelper.GenerateContentUrl(entryPointRoot, viewContext.HttpContext);
}

// search for controller/action.js in current area
var entryPointTmpl = "Controllers/{0}/" + routingInfo.Controller + "/" + routingInfo.Action;
var entryPoint = string.Format(entryPointTmpl, routingInfo.Area).ToModuleName();
var filePath = server.MapPath(entryPointRoot + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, entryPointRoot);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
}

// search for controller/action.js in common area
entryPoint = string.Format(entryPointTmpl, DefaultArea).ToModuleName();
filePath = server.MapPath(entryPointRoot + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, entryPointRoot);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
}

// search for controller/controller-action.js in current area
entryPointTmpl = "Controllers/{0}/" + routingInfo.Controller + "/" + routingInfo.Controller + "-" + routingInfo.Action;
entryPoint = string.Format(entryPointTmpl, routingInfo.Area).ToModuleName();
filePath = server.MapPath(entryPointRoot + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, entryPointRoot);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
}

// search for controller/controller-action.js in common area
entryPoint = string.Format(entryPointTmpl, DefaultArea).ToModuleName();
filePath = server.MapPath(entryPointRoot + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, entryPointRoot);
return withBaseUrl ? computedEntry : rootUrl + computedEntry;
}

return null;
}

private static string GetEntryPoint(HttpServerUtilityBase server, string filePath, string root)
{

var fileName = PathHelpers.GetExactFilePath(filePath);
var folder = server.MapPath(root);
return PathHelpers.GetRequireRelativePath(folder, fileName);
}
}
}
13 changes: 13 additions & 0 deletions RequireJsNet/EntryPointResolver/IEntryPointResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace RequireJsNet.EntryPointResolver
{
public interface IEntryPointResolver
{
string Resolve(ViewContext viewContext, string entryPointRoot);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using RequireJsNet.EntryPointResolver;

namespace RequireJsNet
{
public class RequireEntryPointResolverCollection
{
private List<IEntryPointResolver> resolvers = new List<IEntryPointResolver>();

public void Clear()
{
lock (resolvers)
{
resolvers.Clear();
}

}

public void Prepend(IEntryPointResolver resolver)
{
lock (resolvers)
{
resolvers.Insert(0, resolver);
}
}

public void Add(IEntryPointResolver resolver)
{
lock (resolvers)
{
resolvers.Add(resolver);
}
}

internal string Resolve(ViewContext viewContext, string entryPointRoot)
{
string result = null;

lock (resolvers)
{
foreach (var resolver in resolvers)
{
result = resolver.Resolve(viewContext, entryPointRoot);
if (result != null)
{
break;
}
}
}

return result;
}
}
}
11 changes: 6 additions & 5 deletions RequireJsNet/Helpers/HtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// http://www.opensource.org/licenses/mit-license.php
// http://www.gnu.org/licenses/gpl.html

using System.Web;
using System.Web.Mvc;

using RequireJsNet.Models;
Expand All @@ -13,14 +14,14 @@ namespace RequireJsNet.Helpers
{
internal static class HtmlHelpers
{
public static RoutingInfo GetRoutingInfo(this HtmlHelper html)
public static RoutingInfo GetRoutingInfo(this ViewContext viewContext)
{
var area = html.ViewContext.RouteData.DataTokens["area"] != null
? html.ViewContext.RouteData.DataTokens["area"].ToString()
var area = viewContext.RouteData.DataTokens["area"] != null
? 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 controller = viewContext.Controller.ValueProvider.GetValue("controller").RawValue as string;
var action = viewContext.Controller.ValueProvider.GetValue("action").RawValue as string;
return new RoutingInfo
{
Area = area,
Expand Down
66 changes: 3 additions & 63 deletions RequireJsNet/RequireJsHtmlHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ namespace RequireJsNet

public static class RequireJsHtmlHelpers
{
private const string DefaultEntryPointRoot = "~/Scripts/";
private const string DefaultArea = "Common";

/// <summary>
/// Setup RequireJS to be used in layouts
/// </summary>
Expand Down Expand Up @@ -136,60 +133,9 @@ public static MvcHtmlString RenderRequireJsSetup(
/// </returns>
public static MvcHtmlString RequireJsEntryPoint(this HtmlHelper html, string root)
{
var routingInfo = html.GetRoutingInfo();
var rootUrl = string.Empty;
var withBaseUrl = true;
var server = html.ViewContext.HttpContext.Server;

if (root != DefaultEntryPointRoot)
{
withBaseUrl = false;
rootUrl = UrlHelper.GenerateContentUrl(root, html.ViewContext.HttpContext);
}

// search for controller/action.js in current area
var entryPointTmpl = "Controllers/{0}/" + routingInfo.Controller + "/" + routingInfo.Action;
var entryPoint = string.Format(entryPointTmpl, routingInfo.Area).ToModuleName();
var filePath = server.MapPath(root + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, root);
return new MvcHtmlString(withBaseUrl ? computedEntry : rootUrl + computedEntry + ".js");
}

// search for controller/action.js in common area
entryPoint = string.Format(entryPointTmpl, DefaultArea).ToModuleName();
filePath = server.MapPath(root + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, root);
return new MvcHtmlString(withBaseUrl ? computedEntry : rootUrl + computedEntry + ".js");
}
var result = RequireJsOptions.ResolverCollection.Resolve(html.ViewContext, root);

// search for controller/controller-action.js in current area
entryPointTmpl = "Controllers/{0}/" + routingInfo.Controller + "/" + routingInfo.Controller + "-" + routingInfo.Action;
entryPoint = string.Format(entryPointTmpl, routingInfo.Area).ToModuleName();
filePath = server.MapPath(root + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, root);
return new MvcHtmlString(withBaseUrl ? computedEntry : rootUrl + computedEntry + ".js");
}

// search for controller/controller-action.js in common area
entryPoint = string.Format(entryPointTmpl, DefaultArea).ToModuleName();
filePath = server.MapPath(root + entryPoint + ".js");

if (File.Exists(filePath))
{
var computedEntry = GetEntryPoint(server, filePath, root);
return new MvcHtmlString(withBaseUrl ? computedEntry : rootUrl + computedEntry + ".js");
}

return null;
return result != null ? new MvcHtmlString(result) : null;
}

public static Dictionary<string, int> ToJsonDictionary<TEnum>()
Expand All @@ -199,12 +145,6 @@ public static Dictionary<string, int> ToJsonDictionary<TEnum>()
}


private static string GetEntryPoint(HttpServerUtilityBase server, string filePath, string root)
{

var fileName = PathHelpers.GetExactFilePath(filePath);
var folder = server.MapPath(root);
return PathHelpers.GetRequireRelativePath(folder, fileName);
}

}
}
3 changes: 3 additions & 0 deletions RequireJsNet/RequireJsNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
<Compile Include="Configuration\JsonWriter.cs" />
<Compile Include="Configuration\WriterFactory.cs" />
<Compile Include="Configuration\XmlWriter.cs" />
<Compile Include="EntryPointResolver\DefaultEntryPointResolver.cs" />
<Compile Include="EntryPointResolver\IEntryPointResolver.cs" />
<Compile Include="ExceptionThrowingLogger.cs" />
<Compile Include="Helpers\HtmlHelpers.cs" />
<Compile Include="Helpers\JavaScriptHelpers.cs" />
Expand All @@ -107,6 +109,7 @@
<Compile Include="Models\RequireMapElement.cs" />
<Compile Include="Models\RequireReplacement.cs" />
<Compile Include="Models\RoutingInfo.cs" />
<Compile Include="EntryPointResolver\RequireEntryPointResolverCollection.cs" />
<Compile Include="RequireRendererConfiguration.cs" />
<Compile Include="Validation\ConfigValidator.cs" />
<Compile Include="Configuration\IConfigReader.cs" />
Expand Down
8 changes: 8 additions & 0 deletions RequireJsNet/RequireJsOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RequireJsNet.EntryPointResolver;

namespace RequireJsNet
{
Expand All @@ -24,6 +25,13 @@ public static class RequireJsOptions

private const string PageOptionsKey = "pageOptions";

public static readonly RequireEntryPointResolverCollection ResolverCollection = new RequireEntryPointResolverCollection();

static RequireJsOptions()
{
ResolverCollection.Add(new DefaultEntryPointResolver());
}

public static Dictionary<string, object> GetGlobalOptions(HttpContextBase context)
{
var page = context.Items[GlobalOptionsKey] as Dictionary<string, object>;
Expand Down

0 comments on commit edf6ca5

Please sign in to comment.