Skip to content

Commit ee47b8e

Browse files
committed
Initial loading of the router
1 parent a716db5 commit ee47b8e

88 files changed

Lines changed: 21177 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Core/ActionNameInterceptor.cs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Linq;
3+
using System.Web.Mvc;
4+
5+
namespace JsRouting.Core
6+
{
7+
/// <summary>
8+
/// Interceptor that replaces the name of the controller action when a ActionName attribute is applied
9+
/// </summary>
10+
public class ActionNameInterceptor : IControllerActionInterceptor
11+
{
12+
/// <summary>
13+
/// Action name attribute type
14+
/// </summary>
15+
private static readonly Type actionNameAttribute = typeof(ActionNameAttribute);
16+
17+
/// <summary>
18+
/// Controller action interceptor that changes the action name when specified by attribute
19+
/// </summary>
20+
/// <param name="definition">Controller action definition</param>
21+
/// <returns>
22+
/// Value indicating whether to accept the controller action
23+
/// </returns>
24+
public bool Intercept(ControllerActionDefinition definition)
25+
{
26+
var attribute = definition.Method
27+
.GetCustomAttributes(actionNameAttribute, true)
28+
.OfType<ActionNameAttribute>()
29+
.FirstOrDefault();
30+
31+
if (attribute != null)
32+
definition.ActionName = attribute.Name;
33+
34+
return true;
35+
}
36+
}
37+
}

‎Core/AssemblyLoader.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Generic;
2+
using System.Reflection;
3+
using System.Web.Mvc;
4+
using StructureMap;
5+
6+
namespace JsRouting.Core
7+
{
8+
/// <summary>
9+
/// Assembly loader
10+
/// </summary>
11+
public static class AssemblyLoader
12+
{
13+
/// <summary>
14+
/// Gets a container with the loaded dependencies
15+
/// </summary>
16+
/// <param name="assemblies">Assemblies to scan</param>
17+
/// <returns>Container containing various dependencies to form the JavaScript routing definitions</returns>
18+
public static IContainer Load(IEnumerable<string> assemblies)
19+
{
20+
return new Container(config =>
21+
{
22+
config.Scan(a =>
23+
{
24+
// add the main JsRouting assembly to the scanner
25+
a.Assembly(typeof(Output).Assembly);
26+
27+
// add the assemblies to scan
28+
foreach (string assembly in assemblies)
29+
a.Assembly(Assembly.LoadFrom(assembly));
30+
31+
// types to scan..
32+
a.AddAllTypesOf<IRouteSource>();
33+
a.AddAllTypesOf<IRouteInterceptor>();
34+
a.AddAllTypesOf<ControllerBase>();
35+
a.AddAllTypesOf<IControllerActionInterceptor>();
36+
a.AddAllTypesOf<IJavaScriptAddition>();
37+
});
38+
});
39+
}
40+
}
41+
}

‎Core/ConstraintDefinition.cs‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace JsRouting.Core
5+
{
6+
/// <summary>
7+
/// JavaScript constraint definition
8+
/// </summary>
9+
public class RouteConstraintDefinition : IJavaScriptSource
10+
{
11+
/// <summary>
12+
/// Constraint JavaScript representation format
13+
/// </summary>
14+
private static readonly string ConstraintJs = "$.routeManager.constraintTypeDefs.{0}('{1}', {2})";
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="RouteConstraintDefinition"/> class.
18+
/// </summary>
19+
/// <param name="parameterName">Constraining Parameter</param>
20+
/// <param name="constraintName">Name of the constraint.</param>
21+
/// <param name="data">JavaScript representation of the data passed to the constraint</param>
22+
public RouteConstraintDefinition(string parameterName, string constraintName, string data)
23+
{
24+
if (data == null) throw new ArgumentNullException("data");
25+
26+
this.Data = data;
27+
this.ConstraintName = constraintName;
28+
this.ParameterName = parameterName;
29+
}
30+
31+
/// <summary>
32+
/// Gets or sets the data passed to the constraint
33+
/// </summary>
34+
public string Data { get; private set; }
35+
36+
/// <summary>
37+
/// Gets or sets the name of the constraint
38+
/// </summary>
39+
public string ConstraintName { get; private set; }
40+
41+
/// <summary>
42+
/// Gets or sets the parameter name for the constraint
43+
/// </summary>
44+
public string ParameterName { get; set; }
45+
46+
/// <summary>
47+
/// Creates the initialization JavaScript for the constraint
48+
/// </summary>
49+
/// <returns>Constraint JavaScript</returns>
50+
public string ToJavaScript()
51+
{
52+
return string.Format(ConstraintJs, this.ConstraintName, this.ParameterName, this.Data);
53+
}
54+
}
55+
}

‎Core/ControllerActionDefinition.cs‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
7+
namespace JsRouting.Core
8+
{
9+
/// <summary>
10+
/// Controller action definition
11+
/// </summary>
12+
public class ControllerActionDefinition : IJavaScriptSource
13+
{
14+
/// <summary>
15+
/// JavaScript registration
16+
/// </summary>
17+
private static readonly string jsPattern = @"$.routeManager.mapControllerAction('{0}', '{1}', {2});";
18+
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ControllerActionDefinition"/> class.
21+
/// </summary>
22+
/// <param name="method">The method</param>
23+
public ControllerActionDefinition(MethodInfo method)
24+
{
25+
if (method == null) throw new ArgumentNullException("method");
26+
this.Method = method;
27+
this.ActionName = method.Name;
28+
this.ControllerName = method.DeclaringType.Name.Replace("Controller", string.Empty);
29+
this.Parameters = new LinkedList<ParameterResult>();
30+
}
31+
32+
/// <summary>
33+
/// Gets the name of the controller action.
34+
/// </summary>
35+
public string ActionName { get; set; }
36+
37+
/// <summary>
38+
/// Gets the name of the controller for the action.
39+
/// </summary>
40+
public string ControllerName { get; set; }
41+
42+
/// <summary>
43+
/// Gets the method for the action.
44+
/// </summary>
45+
public MethodInfo Method { get; private set; }
46+
47+
/// <summary>
48+
/// Gets a list of parameters
49+
/// </summary>
50+
public LinkedList<ParameterResult> Parameters { get; private set; }
51+
52+
/// <summary>
53+
/// Gets or sets the method description
54+
/// </summary>
55+
public string Description { get; set; }
56+
57+
/// <summary>
58+
/// Gets the JavaScritpt registration for the controller action
59+
/// </summary>
60+
/// <returns>Controller action JavaScript</returns>
61+
public string ToJavaScript()
62+
{
63+
var builder = new StringBuilder().AppendFormat("function({0}additionalData)", this.Parameters.Select(p => p.Name).ConcatAll(", ", true))
64+
.Append("{\r\n")
65+
.AppendFormat("/// <summary>{0}</summary>", this.Description.Trim().Replace("\n", "\n/// "));
66+
67+
var defaults = this.Parameters.Where(p => !string.IsNullOrEmpty(p.DefaultValue) && !p.DefaultValue.Equals("null", StringComparison.OrdinalIgnoreCase))
68+
.ToDictionary(p => p.Name, p => p.DefaultValue);
69+
70+
defaults["controller"] = "\"" + this.ControllerName + "\"";
71+
defaults["action"] = "\"" + this.ActionName + "\"";
72+
73+
// add the parameter comments for IntelliSense
74+
this.Parameters.Aggregate(builder, AppendComment);
75+
76+
// create the call to the action method
77+
builder.Append("\r\n return $.routeManager.action($.extend({");
78+
defaults.Select(pair => string.Format("{0}:{1}", pair.Key, pair.Value))
79+
.ConcatAll(", ")
80+
.AppendTo(builder)
81+
.Append("}, additionalData, {");
82+
83+
this.Parameters
84+
.Select(param => string.Format("{0}:{0}", param.Name))
85+
.ConcatAll(", ")
86+
.AppendTo(builder)
87+
.Append("})); }");
88+
89+
return string.Format(jsPattern, this.ControllerName, this.ActionName, builder.ToString());
90+
}
91+
92+
/// <summary>
93+
/// Appends a parameter comment to the string builder
94+
/// </summary>
95+
/// <param name="sb">String Builder</param>
96+
/// <param name="param">Parameter to append</param>
97+
/// <returns>String builder with appended comment</returns>
98+
private static StringBuilder AppendComment(StringBuilder sb, ParameterResult param)
99+
{
100+
return sb.AppendFormat("\r\n/// <param name=\"{0}\" mayBeNull=\"{1}\" optional=\"{1}\" type=\"{2}\">{3}</param>",
101+
param.Name,
102+
param.Optional.ToString().ToLowerInvariant(),
103+
param.ParameterInfo.ParameterType.Name,
104+
param.Description.Trim().Replace("\n", "\n/// "));
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)