Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.Framework / Helpers / UrlHelper.cs
blobd77966c3d74110acf5e2525ca34961134b38300c
1 // Copyright 2004-2008 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 #region Constructors
41 /// <summary>
42 /// Initializes a new instance of the <see cref="UrlHelper"/> class.
43 /// </summary>
44 public UrlHelper() { }
45 /// <summary>
46 /// Initializes a new instance of the <see cref="UrlHelper"/> class.
47 /// setting the Controller, Context and ControllerContext.
48 /// </summary>
49 /// <param name="engineContext">The engine context.</param>
50 public UrlHelper(IEngineContext engineContext) : base(engineContext) { }
51 #endregion
53 /// <summary>
54 /// Gets or sets the URL builder.
55 /// </summary>
56 /// <value>The URL builder.</value>
57 public IUrlBuilder UrlBuilder
59 get { return urlBuilder; }
60 set { urlBuilder = value; }
63 /// <summary>
64 /// Gets or sets the current URL.
65 /// </summary>
66 /// <value>The current URL.</value>
67 public UrlInfo CurrentUrl
69 get { return currentUrl; }
70 set { currentUrl = value; }
73 /// <summary>
74 /// Sets the context.
75 /// </summary>
76 /// <param name="context">The context.</param>
77 public override void SetContext(IEngineContext context)
79 base.SetContext(context);
81 urlBuilder = (IUrlBuilder) context.GetService(typeof(IUrlBuilder));
82 currentUrl = context.UrlInfo;
85 /// <summary>
86 /// Outputs a path constructed using the specified parameters.
87 /// </summary>
88 ///
89 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
90 ///
91 /// <example>
92 /// The following code uses nvelocity syntax:
93 ///
94 /// <code>
95 /// $url.for("%{action='Save'}")
96 /// </code>
97 ///
98 /// <para>outputs</para>
99 ///
100 /// <code>/ControllerNameFromContext/Save.extension_configured</code>
101 ///
102 /// <code>
103 /// $url.for("%{action='Edit',querystring='id=1'}")
104 /// </code>
105 ///
106 /// <para>outputs</para>
107 ///
108 /// <code>/ControllerNameFromContext/Edit.extension_configured?id=1</code>
109 /// </example>
110 ///
111 /// <param name="parameters">The parameters.</param>
112 /// <returns></returns>
113 public string For(IDictionary parameters)
115 SetEncodeDefault(parameters);
117 UrlBuilderParameters urlParams = UrlBuilderParameters.From(parameters).
118 SetRouteMatch(ControllerContext.RouteMatch);
120 return urlBuilder.BuildUrl(currentUrl, urlParams);
123 /// <summary>
124 /// Outputs an anchor element constructed using the specified parameters.
125 /// </summary>
126 ///
127 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
128 ///
129 /// <example>
130 /// The following code uses nvelocity syntax:
131 ///
132 /// <code>
133 /// $url.link('my link', "%{action='Save'}")
134 /// </code>
135 ///
136 /// <para>outputs</para>
137 ///
138 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Save.extension_configured">my link</a> ]]> </code>
139 ///
140 /// <code>
141 /// $url.link('my link', "%{action='Edit',querystring='id=1'}")
142 /// </code>
143 ///
144 /// <para>outputs</para>
145 ///
146 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Edit.extension_configured?id=1">my link</a> ]]> </code>
147 /// </example>
148 ///
149 /// <param name="innerContent">The anchor text.</param>
150 /// <param name="parameters">The parameters.</param>
151 /// <returns></returns>
152 public string Link(string innerContent, IDictionary parameters)
154 return "<a href=\"" + For(parameters) + "\">" + innerContent + "</a>";
157 /// <summary>
158 /// Outputs an anchor element constructed using the specified parameters.
159 /// </summary>
160 ///
161 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
162 ///
163 /// <example>
164 /// The following code uses nvelocity syntax:
165 ///
166 /// <code>
167 /// $url.link('my link', "%{action='Save'}", "%{class='buttonlink'}")
168 /// </code>
169 ///
170 /// <para>outputs</para>
171 ///
172 /// <code><![CDATA[ <a href="/ControllerNameFromContext/Save.extension_configured" class="buttonlink">my link</a> ]]> </code>
173 ///
174 /// </example>
175 ///
176 /// <param name="innerContent">The anchor text.</param>
177 /// <param name="parameters">The parameters.</param>
178 /// <param name="anchorAttributes">The anchor element attributes.</param>
179 /// <returns></returns>
180 public string Link(string innerContent, IDictionary parameters, IDictionary anchorAttributes)
182 return "<a " + GetAttributes(anchorAttributes) + " href=\"" + For(parameters) + "\">" + innerContent + "</a>";
185 /// <summary>
186 /// Outputs a button element constructed using the specified parameters.
187 /// </summary>
188 ///
189 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
190 ///
191 /// <example>
192 /// The following code uses nvelocity syntax:
193 ///
194 /// <code>
195 /// $url.ButtonLink('my link', "%{action='Save'}")
196 /// </code>
197 ///
198 /// <para>outputs</para>
199 ///
200 /// <code><![CDATA[ <button type="button" onclick="javascript:window.location.href = '/ControllerNameFromContext/Save.extension_configured'">my link</a> ]]> </code>
201 ///
202 /// </example>
203 ///
204 /// <param name="innerContent">The button text.</param>
205 /// <param name="parameters">The parameters.</param>
206 /// <returns></returns>
207 public string ButtonLink(string innerContent, IDictionary parameters)
209 return "<button type=\"button\" onclick=\"javascript:window.location.href = '" + For(parameters) + "'\">" + innerContent + "</button>";
212 /// <summary>
213 /// Outputs a button element constructed using the specified parameters.
214 /// </summary>
215 ///
216 /// <seealso cref="DefaultUrlBuilder.BuildUrl(UrlInfo,IDictionary)"/>
217 ///
218 /// <example>
219 /// The following code uses nvelocity syntax:
220 ///
221 /// <code>
222 /// $url.ButtonLink('my link', "%{action='Save'}", "%{class='buttonlink'}")
223 /// </code>
224 ///
225 /// <para>outputs</para>
226 ///
227 /// <code><![CDATA[ <button type="button" onclick="javascript:window.location.href = '/ControllerNameFromContext/Save.extension_configured'" class="buttonlink">my link</a> ]]> </code>
228 ///
229 /// </example>
230 ///
231 /// <param name="innerContent">The button text.</param>
232 /// <param name="parameters">The parameters.</param>
233 /// <param name="buttonAttributes">The button element attributes.</param>
234 /// <returns></returns>
235 public string ButtonLink(string innerContent, IDictionary parameters, IDictionary buttonAttributes)
237 return "<button type=\"button\"" + GetAttributes(buttonAttributes) + " onclick=\"javascript:window.location.href = '" + For(parameters) + "'\">" + innerContent + "</button>";
240 private static void SetEncodeDefault(IDictionary parameters)
242 if (!parameters.Contains("encode"))
244 parameters["encode"] = "true";