-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added the ability to customize entry point resolution (#31)
- Loading branch information
1 parent
7535c16
commit edf6ca5
Showing
7 changed files
with
174 additions
and
68 deletions.
There are no files selected for viewing
83 changes: 83 additions & 0 deletions
83
RequireJsNet/EntryPointResolver/DefaultEntryPointResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
RequireJsNet/EntryPointResolver/RequireEntryPointResolverCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters