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