Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / UrlHelper.cs
blob0f08ccf061a98f36bdf9ef3c0dc99d8e6bcb76fc
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.Helpers
17 using System.Collections;
18 using Castle.MonoRail.Framework;
19 using Castle.MonoRail.Framework.Services;
21 /// <summary>
22 /// Helper that allows the creation of urls using a dictionary.
23 ///
24 /// <para>
25 /// For more information see <see cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
26 /// </para>
27 ///
28 /// </summary>
29 ///
30 /// <remarks>
31 /// By default the urlhelper sets the encode to <c>true</c>, so the html generated is valid xhtml.
32 /// </remarks>
33 ///
34 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
35 public class UrlHelper : AbstractHelper
37 private IUrlBuilder urlBuilder;
38 private UrlInfo currentUrl;
40 /// <summary>
41 /// Gets or sets the URL builder.
42 /// </summary>
43 /// <value>The URL builder.</value>
44 public IUrlBuilder UrlBuilder
46 get { return urlBuilder; }
47 set { urlBuilder = value; }
50 /// <summary>
51 /// Gets or sets the current URL.
52 /// </summary>
53 /// <value>The current URL.</value>
54 public UrlInfo CurrentUrl
56 get { return currentUrl; }
57 set { currentUrl = value; }
60 /// <summary>
61 /// Sets the controller.
62 /// </summary>
63 /// <param name="controller">Current view's <see cref="AbstractHelper.Controller"/>.</param>
64 public override void SetController(Controller controller)
66 base.SetController(controller);
68 urlBuilder = controller.Context.GetService<IUrlBuilder>();
69 currentUrl = controller.Context.UrlInfo;
72 /// <summary>
73 /// Outputs a path constructed using the specified parameters.
74 /// </summary>
75 ///
76 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
77 ///
78 /// <example>
79 /// The following code uses nvelocity syntax:
80 ///
81 /// <code>
82 /// $url.for("%{action='Save'}")
83 /// </code>
84 ///
85 /// <para>outputs</para>
86 ///
87 /// <code>/ControllerNameFromContext/Save.extension_configured</code>
88 ///
89 /// <code>
90 /// $url.for("%{action='Edit',querystring='id=1'}")
91 /// </code>
92 ///
93 /// <para>outputs</para>
94 ///
95 /// <code>/ControllerNameFromContext/Edit.extension_configured?id=1</code>
96 /// </example>
97 ///
98 /// <param name="parameters">The parameters.</param>
99 /// <returns></returns>
100 public string For(IDictionary parameters)
102 SetEncodeDefault(parameters);
103 return urlBuilder.BuildUrl(currentUrl, parameters);
106 /// <summary>
107 /// Outputs an anchor element constructed using the specified parameters.
108 /// </summary>
109 ///
110 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
111 ///
112 /// <example>
113 /// The following code uses nvelocity syntax:
114 ///
115 /// <code>
116 /// $url.link('my link', "%{action='Save'}")
117 /// </code>
118 ///
119 /// <para>outputs</para>
120 ///
121 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Save.extension_configured">my link</a> ]]> </code>
122 ///
123 /// <code>
124 /// $url.link('my link', "%{action='Edit',querystring='id=1'}")
125 /// </code>
126 ///
127 /// <para>outputs</para>
128 ///
129 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Edit.extension_configured?id=1">my link</a> ]]> </code>
130 /// </example>
131 ///
132 /// <param name="innerContent">The anchor text.</param>
133 /// <param name="parameters">The parameters.</param>
134 /// <returns></returns>
135 public string Link(string innerContent, IDictionary parameters)
137 SetEncodeDefault(parameters);
138 return "<a href=\"" + For(parameters) + "\">" + innerContent + "</a>";
141 /// <summary>
142 /// Outputs an anchor element constructed using the specified parameters.
143 /// </summary>
144 ///
145 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
146 ///
147 /// <example>
148 /// The following code uses nvelocity syntax:
149 ///
150 /// <code>
151 /// $url.link('my link', "%{action='Save'}", "%{class='buttonlink'}")
152 /// </code>
153 ///
154 /// <para>outputs</para>
155 ///
156 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Save.extension_configured" class="buttonlink">my link</a> ]]> </code>
157 ///
158 /// </example>
159 ///
160 /// <param name="innerContent">The anchor text.</param>
161 /// <param name="parameters">The parameters.</param>
162 /// <param name="anchorAttributes">The anchor element attributes.</param>
163 /// <returns></returns>
164 public string Link(string innerContent, IDictionary parameters, IDictionary anchorAttributes)
166 SetEncodeDefault(parameters);
167 return "<a " + GetAttributes(anchorAttributes) + " href=\"" + For(parameters) + "\">" + innerContent + "</a>";
170 /// <summary>
171 /// Outputs a button element constructed using the specified parameters.
172 /// </summary>
173 ///
174 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
175 ///
176 /// <example>
177 /// The following code uses nvelocity syntax:
178 ///
179 /// <code>
180 /// $url.ButtonLink('my link', "%{action='Save'}")
181 /// </code>
182 ///
183 /// <para>outputs</para>
184 ///
185 /// <code><![CDATA[ <button type="button" onclick="javascript:window.location.href = '/ControllerNameFromContext/Save.extension_configured'">my link</a> ]]> </code>
186 ///
187 /// </example>
188 ///
189 /// <param name="innerContent">The button text.</param>
190 /// <param name="parameters">The parameters.</param>
191 /// <returns></returns>
192 public string ButtonLink(string innerContent, IDictionary parameters)
194 SetEncodeDefault(parameters);
195 return "<button type=\"button\" onclick=\"javascript:window.location.href = '" + For(parameters) + "'\">" + innerContent + "</button>";
198 /// <summary>
199 /// Outputs a button element constructed using the specified parameters.
200 /// </summary>
201 ///
202 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
203 ///
204 /// <example>
205 /// The following code uses nvelocity syntax:
206 ///
207 /// <code>
208 /// $url.ButtonLink('my link', "%{action='Save'}", "%{class='buttonlink'}")
209 /// </code>
210 ///
211 /// <para>outputs</para>
212 ///
213 /// <code><![CDATA[ <button type="button" onclick="javascript:window.location.href = '/ControllerNameFromContext/Save.extension_configured'" class="buttonlink">my link</a> ]]> </code>
214 ///
215 /// </example>
216 ///
217 /// <param name="innerContent">The button text.</param>
218 /// <param name="parameters">The parameters.</param>
219 /// <param name="buttonAttributes">The button element attributes.</param>
220 /// <returns></returns>
221 public string ButtonLink(string innerContent, IDictionary parameters, IDictionary buttonAttributes)
223 SetEncodeDefault(parameters);
224 return "<button type=\"button\"" + GetAttributes(buttonAttributes) + " onclick=\"javascript:window.location.href = '" + For(parameters) + "'\">" + innerContent + "</button>";
227 private static void SetEncodeDefault(IDictionary parameters)
229 if (!parameters.Contains("encode"))
231 parameters["encode"] = "true";