Fix bugs in sizing TableLayoutPanel (Xamarin bug 18638)
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / Ajax / AjaxExtensions.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc.Ajax {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.ComponentModel;\r
17     using System.Globalization;\r
18     using System.Web;\r
19     using System.Web.Mvc;\r
20     using System.Web.Mvc.Html;\r
21     using System.Web.Mvc.Resources;\r
22     using System.Web.Routing;\r
23 \r
24     public static class AjaxExtensions {\r
25         private const string LinkOnClickFormat = "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});";\r
26         private const string FormOnSubmitFormat = "Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), {0});";\r
27 \r
28         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, AjaxOptions ajaxOptions) {\r
29             return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, ajaxOptions);\r
30         }\r
31 \r
32         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, object routeValues, AjaxOptions ajaxOptions) {\r
33             return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);\r
34         }\r
35 \r
36         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {\r
37             return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);\r
38         }\r
39 \r
40         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {\r
41             return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);\r
42         }\r
43 \r
44         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
45             return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);\r
46         }\r
47 \r
48         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, AjaxOptions ajaxOptions) {\r
49             return ActionLink(ajaxHelper, linkText, actionName, controllerName, null /* values */, ajaxOptions, null /* htmlAttributes */);\r
50         }\r
51 \r
52         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions) {\r
53             return ActionLink(ajaxHelper, linkText, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);\r
54         }\r
55 \r
56         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {\r
57             RouteValueDictionary newValues = new RouteValueDictionary(routeValues);\r
58             Dictionary<string, object> newAttributes = ObjectToCaseSensitiveDictionary(htmlAttributes);\r
59             return ActionLink(ajaxHelper, linkText, actionName, controllerName, newValues, ajaxOptions, newAttributes);\r
60         }\r
61 \r
62         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {\r
63             return ActionLink(ajaxHelper, linkText, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);\r
64         }\r
65 \r
66         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
67             if (String.IsNullOrEmpty(linkText)) {\r
68                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");\r
69             }\r
70 \r
71             string targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);\r
72 \r
73             return GenerateLink(linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes);\r
74         }\r
75 \r
76         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {\r
77             RouteValueDictionary newValues = new RouteValueDictionary(routeValues);\r
78             Dictionary<string, object> newAttributes = ObjectToCaseSensitiveDictionary(htmlAttributes);\r
79             return ActionLink(ajaxHelper, linkText, actionName, controllerName, protocol, hostName, fragment, newValues, ajaxOptions, newAttributes);\r
80         }\r
81 \r
82         public static string ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
83             if (String.IsNullOrEmpty(linkText)) {\r
84                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");\r
85             }\r
86 \r
87             string targetUrl = UrlHelper.GenerateUrl(null /* routeName */, actionName, controllerName, protocol, hostName, fragment, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);\r
88 \r
89             return GenerateLink(linkText, targetUrl, ajaxOptions, htmlAttributes);\r
90         }\r
91 \r
92         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, AjaxOptions ajaxOptions) {\r
93             string formAction = ajaxHelper.ViewContext.HttpContext.Request.RawUrl;\r
94 \r
95             TagBuilder builder = new TagBuilder("form");\r
96 \r
97             builder.MergeAttribute("action", formAction);\r
98             builder.MergeAttribute("method", "post");\r
99             builder.MergeAttribute("onsubmit", GenerateAjaxScript(GetAjaxOptions(ajaxOptions), FormOnSubmitFormat));\r
100 \r
101             HttpResponseBase response = ajaxHelper.ViewContext.HttpContext.Response;\r
102             response.Write(builder.ToString(TagRenderMode.StartTag));\r
103             return new MvcForm(response);\r
104         }\r
105 \r
106         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, AjaxOptions ajaxOptions) {\r
107             return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, ajaxOptions);\r
108         }\r
109 \r
110         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions) {\r
111             return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);\r
112         }\r
113 \r
114         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {\r
115             return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);\r
116         }\r
117 \r
118         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {\r
119             return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);\r
120         }\r
121 \r
122         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
123             return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);\r
124         }\r
125 \r
126         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, AjaxOptions ajaxOptions) {\r
127             return BeginForm(ajaxHelper, actionName, controllerName, null /* values */, ajaxOptions, null /* htmlAttributes */);\r
128         }\r
129 \r
130         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions) {\r
131             return BeginForm(ajaxHelper, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);\r
132         }\r
133 \r
134         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {\r
135             RouteValueDictionary newValues = new RouteValueDictionary(routeValues);\r
136             Dictionary<string, object> newAttributes = ObjectToCaseSensitiveDictionary(htmlAttributes);\r
137             return BeginForm(ajaxHelper, actionName, controllerName, newValues, ajaxOptions, newAttributes);\r
138         }\r
139 \r
140         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {\r
141             return BeginForm(ajaxHelper, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);\r
142         }\r
143 \r
144         public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
145             // get target URL\r
146             string formAction = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);\r
147             return FormHelper(ajaxHelper, formAction, ajaxOptions, htmlAttributes);\r
148         }\r
149 \r
150         public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, AjaxOptions ajaxOptions) {\r
151             return BeginRouteForm(ajaxHelper, routeName, null /* routeValues */, ajaxOptions, null /* htmlAttributes */);\r
152         }\r
153 \r
154         public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, object routeValues, AjaxOptions ajaxOptions) {\r
155             return BeginRouteForm(ajaxHelper, routeName, (object)routeValues, ajaxOptions, null /* htmlAttributes */);\r
156         }\r
157 \r
158         public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {\r
159             Dictionary<string, object> newAttributes = ObjectToCaseSensitiveDictionary(htmlAttributes);\r
160             return BeginRouteForm(ajaxHelper, routeName, new RouteValueDictionary(routeValues), ajaxOptions, newAttributes);\r
161         }\r
162 \r
163         public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {\r
164             return BeginRouteForm(ajaxHelper, routeName, routeValues, ajaxOptions, null /* htmlAttributes */);\r
165         }\r
166 \r
167         public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
168             string formAction = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);\r
169             return FormHelper(ajaxHelper, formAction, ajaxOptions, htmlAttributes);\r
170         }\r
171 \r
172         private static MvcForm FormHelper(this AjaxHelper ajaxHelper, string formAction, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
173             TagBuilder builder = new TagBuilder("form");\r
174             builder.MergeAttributes(htmlAttributes);\r
175             builder.MergeAttribute("action", formAction);\r
176             builder.MergeAttribute("method", "post");\r
177             builder.MergeAttribute("onsubmit", GenerateAjaxScript(GetAjaxOptions(ajaxOptions), FormOnSubmitFormat));\r
178 \r
179             HttpResponseBase response = ajaxHelper.ViewContext.HttpContext.Response;\r
180             response.Write(builder.ToString(TagRenderMode.StartTag));\r
181             return new MvcForm(response);\r
182         }\r
183 \r
184         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, object routeValues, AjaxOptions ajaxOptions) {\r
185             return RouteLink(ajaxHelper, linkText, null /* routeName */, new RouteValueDictionary(routeValues), ajaxOptions,\r
186                              new Dictionary<string, object>());\r
187         }\r
188 \r
189         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {\r
190             return RouteLink(ajaxHelper, linkText, null /* routeName */, new RouteValueDictionary(routeValues), ajaxOptions,\r
191                              ObjectToCaseSensitiveDictionary(htmlAttributes));\r
192         }\r
193 \r
194         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {\r
195             return RouteLink(ajaxHelper, linkText, null /* routeName */, routeValues, ajaxOptions,\r
196                              new Dictionary<string, object>());\r
197         }\r
198 \r
199         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
200             return RouteLink(ajaxHelper, linkText, null /* routeName */, routeValues, ajaxOptions, htmlAttributes);\r
201         }\r
202 \r
203         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions) {\r
204             return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions,\r
205                              new Dictionary<string, object>());\r
206         }\r
207 \r
208         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions, object htmlAttributes) {\r
209             return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions, ObjectToCaseSensitiveDictionary(htmlAttributes));\r
210         }\r
211 \r
212         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
213             return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions, htmlAttributes);\r
214         }\r
215 \r
216         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, object routeValues, AjaxOptions ajaxOptions) {\r
217             return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(routeValues), ajaxOptions,\r
218                              new Dictionary<string, object>());\r
219         }\r
220 \r
221         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {\r
222             return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(routeValues), ajaxOptions,\r
223                              ObjectToCaseSensitiveDictionary(htmlAttributes));\r
224         }\r
225 \r
226         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {\r
227             return RouteLink(ajaxHelper, linkText, routeName, routeValues, ajaxOptions, new Dictionary<string, object>());\r
228         }\r
229 \r
230         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
231             if (String.IsNullOrEmpty(linkText)) {\r
232                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");\r
233             }\r
234 \r
235             string targetUrl = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);\r
236 \r
237             return GenerateLink(linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes);\r
238         }\r
239 \r
240         public static string RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
241             if (String.IsNullOrEmpty(linkText)) {\r
242                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");\r
243             }\r
244 \r
245             string targetUrl = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, protocol, hostName, fragment, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);\r
246 \r
247             return GenerateLink(linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes);\r
248         }\r
249 \r
250         internal static string InsertionModeToString(InsertionMode insertionMode) {\r
251             switch (insertionMode) {\r
252                 case InsertionMode.Replace:\r
253                     return "Sys.Mvc.InsertionMode.replace";\r
254                 case InsertionMode.InsertBefore:\r
255                     return "Sys.Mvc.InsertionMode.insertBefore";\r
256                 case InsertionMode.InsertAfter:\r
257                     return "Sys.Mvc.InsertionMode.insertAfter";\r
258                 default:\r
259                     return ((int)insertionMode).ToString(CultureInfo.InvariantCulture);\r
260             }\r
261         }\r
262 \r
263         private static Dictionary<string, object> ObjectToCaseSensitiveDictionary(object values) {\r
264             Dictionary<string, object> dict = new Dictionary<string, object>(StringComparer.Ordinal);\r
265             if (values != null) {\r
266                 foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(values)) {\r
267                     object val = prop.GetValue(values);\r
268                     dict[prop.Name] = val;\r
269                 }\r
270             }\r
271             return dict;\r
272         }\r
273 \r
274         private static string GenerateLink(string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {\r
275             TagBuilder tag = new TagBuilder("a") {\r
276                 InnerHtml = HttpUtility.HtmlEncode(linkText)\r
277             };\r
278 \r
279             tag.MergeAttributes(htmlAttributes);\r
280             tag.MergeAttribute("href", targetUrl);\r
281             tag.MergeAttribute("onclick", GenerateAjaxScript(ajaxOptions, LinkOnClickFormat));\r
282 \r
283             return tag.ToString(TagRenderMode.Normal);\r
284         }\r
285 \r
286         private static string GenerateAjaxScript(AjaxOptions ajaxOptions, string scriptFormat) {\r
287             string optionsString = ajaxOptions.ToJavascriptString();\r
288             return String.Format(CultureInfo.InvariantCulture, scriptFormat, optionsString);\r
289         }\r
290 \r
291         private static AjaxOptions GetAjaxOptions(AjaxOptions ajaxOptions) {\r
292             return (ajaxOptions != null) ? ajaxOptions : new AjaxOptions();\r
293         }\r
294     }\r
295 }\r