New test.
[mono.git] / mcs / class / System.Web.Mvc2 / System.Web.Mvc / Ajax / AjaxOptions.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.Diagnostics.CodeAnalysis;\r
16     using System.Globalization;\r
17     using System.Text;\r
18     using System.Web.Mvc.Resources;\r
19 \r
20     public class AjaxOptions {\r
21         private string _confirm;\r
22         private string _httpMethod;\r
23         private InsertionMode _insertionMode = InsertionMode.Replace;\r
24         private string _loadingElementId;\r
25         private string _onBegin;\r
26         private string _onComplete;\r
27         private string _onFailure;\r
28         private string _onSuccess;\r
29         private string _updateTargetId;\r
30         private string _url;\r
31 \r
32         public string Confirm {\r
33             get {\r
34                 return _confirm ?? String.Empty;\r
35             }\r
36             set {\r
37                 _confirm = value;\r
38             }\r
39         }\r
40 \r
41         public string HttpMethod {\r
42             get {\r
43                 return _httpMethod ?? String.Empty;\r
44             }\r
45             set {\r
46                 _httpMethod = value;\r
47             }\r
48         }\r
49 \r
50         public InsertionMode InsertionMode {\r
51             get {\r
52                 return _insertionMode;\r
53             }\r
54             set {\r
55                 switch (value) {\r
56                     case InsertionMode.Replace:\r
57                     case InsertionMode.InsertAfter:\r
58                     case InsertionMode.InsertBefore:\r
59                         _insertionMode = value;\r
60                         return;\r
61 \r
62                     default:\r
63                         throw new ArgumentOutOfRangeException("value");\r
64                 }\r
65             }\r
66         }\r
67 \r
68         public string LoadingElementId {\r
69             get {\r
70                 return _loadingElementId ?? String.Empty;\r
71             }\r
72             set {\r
73                 _loadingElementId = value;\r
74             }\r
75         }\r
76 \r
77         public string OnBegin {\r
78             get {\r
79                 return _onBegin ?? String.Empty;\r
80             }\r
81             set {\r
82                 _onBegin = value;\r
83             }\r
84         }\r
85 \r
86         public string OnComplete {\r
87             get {\r
88                 return _onComplete ?? String.Empty;\r
89             }\r
90             set {\r
91                 _onComplete = value;\r
92             }\r
93         }\r
94 \r
95         public string OnFailure {\r
96             get {\r
97                 return _onFailure ?? String.Empty;\r
98             }\r
99             set {\r
100                 _onFailure = value;\r
101             }\r
102         }\r
103 \r
104         public string OnSuccess {\r
105             get {\r
106                 return _onSuccess ?? String.Empty;\r
107             }\r
108             set {\r
109                 _onSuccess = value;\r
110             }\r
111         }\r
112 \r
113         public string UpdateTargetId {\r
114             get {\r
115                 return _updateTargetId ?? String.Empty;\r
116             }\r
117             set {\r
118                 _updateTargetId = value;\r
119             }\r
120         }\r
121 \r
122         [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings",\r
123             Justification = "This property is used by the optionsBuilder which always accepts a string.")]\r
124         public string Url {\r
125             get {\r
126                 return _url ?? String.Empty;\r
127             }\r
128             set {\r
129                 _url = value;\r
130             }\r
131         }\r
132 \r
133         internal string ToJavascriptString() {\r
134             // creates a string of the form { key1: value1, key2 : value2, ... }\r
135             StringBuilder optionsBuilder = new StringBuilder("{");\r
136             optionsBuilder.Append(String.Format(CultureInfo.InvariantCulture, " insertionMode: {0},", AjaxExtensions.InsertionModeToString(InsertionMode)));\r
137             optionsBuilder.Append(PropertyStringIfSpecified("confirm", Confirm));\r
138             optionsBuilder.Append(PropertyStringIfSpecified("httpMethod", HttpMethod));\r
139             optionsBuilder.Append(PropertyStringIfSpecified("loadingElementId", LoadingElementId));\r
140             optionsBuilder.Append(PropertyStringIfSpecified("updateTargetId", UpdateTargetId));\r
141             optionsBuilder.Append(PropertyStringIfSpecified("url", Url));\r
142             optionsBuilder.Append(EventStringIfSpecified("onBegin", OnBegin));\r
143             optionsBuilder.Append(EventStringIfSpecified("onComplete", OnComplete));\r
144             optionsBuilder.Append(EventStringIfSpecified("onFailure", OnFailure));\r
145             optionsBuilder.Append(EventStringIfSpecified("onSuccess", OnSuccess));\r
146             optionsBuilder.Length--;\r
147             optionsBuilder.Append(" }");\r
148             return optionsBuilder.ToString();\r
149         }\r
150 \r
151         private static string EventStringIfSpecified(string propertyName, string handler) {\r
152             if (!String.IsNullOrEmpty(handler)) {\r
153                 return String.Format(CultureInfo.InvariantCulture, " {0}: Function.createDelegate(this, {1}),", propertyName, handler.ToString());\r
154             }\r
155             return String.Empty;\r
156         }\r
157 \r
158         private static string PropertyStringIfSpecified(string propertyName, string propertyValue) {\r
159             if (!String.IsNullOrEmpty(propertyValue)) {\r
160                 string escapedPropertyValue = propertyValue.Replace("'", @"\'");\r
161                 return String.Format(CultureInfo.InvariantCulture, " {0}: '{1}',", propertyName, escapedPropertyValue);\r
162             }\r
163             return String.Empty;\r
164         }\r
165     }\r
166 }\r