Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc / 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", String.Format(CultureInfo.InvariantCulture,\r
64                             MvcResources.Common_InvalidEnumValue, value, typeof(InsertionMode).FullName));\r
65                 }\r
66             }\r
67         }\r
68 \r
69         public string LoadingElementId {\r
70             get {\r
71                 return _loadingElementId ?? String.Empty;\r
72             }\r
73             set {\r
74                 _loadingElementId = value;\r
75             }\r
76         }\r
77 \r
78         public string OnBegin {\r
79             get {\r
80                 return _onBegin ?? String.Empty;\r
81             }\r
82             set {\r
83                 _onBegin = value;\r
84             }\r
85         }\r
86 \r
87         public string OnComplete {\r
88             get {\r
89                 return _onComplete ?? String.Empty;\r
90             }\r
91             set {\r
92                 _onComplete = value;\r
93             }\r
94         }\r
95 \r
96         public string OnFailure {\r
97             get {\r
98                 return _onFailure ?? String.Empty;\r
99             }\r
100             set {\r
101                 _onFailure = value;\r
102             }\r
103         }\r
104 \r
105         public string OnSuccess {\r
106             get {\r
107                 return _onSuccess ?? String.Empty;\r
108             }\r
109             set {\r
110                 _onSuccess = value;\r
111             }\r
112         }\r
113 \r
114         public string UpdateTargetId {\r
115             get {\r
116                 return _updateTargetId ?? String.Empty;\r
117             }\r
118             set {\r
119                 _updateTargetId = value;\r
120             }\r
121         }\r
122 \r
123         [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings",\r
124             Justification = "This property is used by the optionsBuilder which always accepts a string.")]\r
125         public string Url {\r
126             get {\r
127                 return _url ?? String.Empty;\r
128             }\r
129             set {\r
130                 _url = value;\r
131             }\r
132         }\r
133 \r
134         internal string ToJavascriptString() {\r
135             // creates a string of the form { key1: value1, key2 : value2, ... }\r
136             StringBuilder optionsBuilder = new StringBuilder("{");\r
137             optionsBuilder.Append(String.Format(CultureInfo.InvariantCulture, " insertionMode: {0},", AjaxExtensions.InsertionModeToString(InsertionMode)));\r
138             optionsBuilder.Append(PropertyStringIfSpecified("confirm", Confirm));\r
139             optionsBuilder.Append(PropertyStringIfSpecified("httpMethod", HttpMethod));\r
140             optionsBuilder.Append(PropertyStringIfSpecified("loadingElementId", LoadingElementId));\r
141             optionsBuilder.Append(PropertyStringIfSpecified("updateTargetId", UpdateTargetId));\r
142             optionsBuilder.Append(PropertyStringIfSpecified("url", Url));\r
143             optionsBuilder.Append(EventStringIfSpecified("onBegin", OnBegin));\r
144             optionsBuilder.Append(EventStringIfSpecified("onComplete", OnComplete));\r
145             optionsBuilder.Append(EventStringIfSpecified("onFailure", OnFailure));\r
146             optionsBuilder.Append(EventStringIfSpecified("onSuccess", OnSuccess));\r
147             optionsBuilder.Length--;\r
148             optionsBuilder.Append(" }");\r
149             return optionsBuilder.ToString();\r
150         }\r
151 \r
152         private static string EventStringIfSpecified(string propertyName, string handler) {\r
153             if (!String.IsNullOrEmpty(handler)) {\r
154                 return String.Format(CultureInfo.InvariantCulture, " {0}: Function.createDelegate(this, {1}),", propertyName, handler.ToString());\r
155             }\r
156             return String.Empty;\r
157         }\r
158 \r
159         private static string PropertyStringIfSpecified(string propertyName, string propertyValue) {\r
160             if (!String.IsNullOrEmpty(propertyValue)) {\r
161                 string escapedPropertyValue = propertyValue.Replace("'", @"\'");\r
162                 return String.Format(CultureInfo.InvariantCulture, " {0}: '{1}',", propertyName, escapedPropertyValue);\r
163             }\r
164             return String.Empty;\r
165         }\r
166     }\r
167 }\r