1 namespace System.Web.Mvc.Ajax {
2     using System;
3     using System.Collections.Generic;
4     using System.Diagnostics.CodeAnalysis;
5     using System.Globalization;
6     using System.Text;
7 
8     public class AjaxOptions {
9         private string _confirm;
10         private string _httpMethod;
11         private InsertionMode _insertionMode = InsertionMode.Replace;
12         private string _loadingElementId;
13         private string _onBegin;
14         private string _onComplete;
15         private string _onFailure;
16         private string _onSuccess;
17         private string _updateTargetId;
18         private string _url;
19 
20         public string Confirm {
21             get {
22                 return _confirm ?? String.Empty;
23             }
24             set {
25                 _confirm = value;
26             }
27         }
28 
29         public string HttpMethod {
30             get {
31                 return _httpMethod ?? String.Empty;
32             }
33             set {
34                 _httpMethod = value;
35             }
36         }
37 
38         public InsertionMode InsertionMode {
39             get {
40                 return _insertionMode;
41             }
42             set {
43                 switch (value) {
44                     case InsertionMode.Replace:
45                     case InsertionMode.InsertAfter:
46                     case InsertionMode.InsertBefore:
47                         _insertionMode = value;
48                         return;
49 
50                     default:
51                         throw new ArgumentOutOfRangeException("value");
52                 }
53             }
54         }
55 
56         internal string InsertionModeString {
57             get {
58                 switch (InsertionMode) {
59                     case InsertionMode.Replace:
60                         return "Sys.Mvc.InsertionMode.replace";
61                     case InsertionMode.InsertBefore:
62                         return "Sys.Mvc.InsertionMode.insertBefore";
63                     case InsertionMode.InsertAfter:
64                         return "Sys.Mvc.InsertionMode.insertAfter";
65                     default:
66                         return ((int)InsertionMode).ToString(CultureInfo.InvariantCulture);
67                 }
68             }
69         }
70 
71         internal string InsertionModeUnobtrusive {
72             get {
73                 switch (InsertionMode) {
74                     case InsertionMode.Replace:
75                         return "replace";
76                     case InsertionMode.InsertBefore:
77                         return "before";
78                     case InsertionMode.InsertAfter:
79                         return "after";
80                     default:
81                         return ((int)InsertionMode).ToString(CultureInfo.InvariantCulture);
82                 }
83             }
84         }
85 
86         public int LoadingElementDuration {
87             get;
88             set;
89         }
90 
91         public string LoadingElementId {
92             get {
93                 return _loadingElementId ?? String.Empty;
94             }
95             set {
96                 _loadingElementId = value;
97             }
98         }
99 
100         public string OnBegin {
101             get {
102                 return _onBegin ?? String.Empty;
103             }
104             set {
105                 _onBegin = value;
106             }
107         }
108 
109         public string OnComplete {
110             get {
111                 return _onComplete ?? String.Empty;
112             }
113             set {
114                 _onComplete = value;
115             }
116         }
117 
118         public string OnFailure {
119             get {
120                 return _onFailure ?? String.Empty;
121             }
122             set {
123                 _onFailure = value;
124             }
125         }
126 
127         public string OnSuccess {
128             get {
129                 return _onSuccess ?? String.Empty;
130             }
131             set {
132                 _onSuccess = value;
133             }
134         }
135 
136         public string UpdateTargetId {
137             get {
138                 return _updateTargetId ?? String.Empty;
139             }
140             set {
141                 _updateTargetId = value;
142             }
143         }
144 
145         [SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "This property is used by the optionsBuilder which always accepts a string.")]
146         public string Url {
147             get {
148                 return _url ?? String.Empty;
149             }
150             set {
151                 _url = value;
152             }
153         }
154 
ToJavascriptString()155         internal string ToJavascriptString() {
156             // creates a string of the form { key1: value1, key2 : value2, ... }
157             StringBuilder optionsBuilder = new StringBuilder("{");
158             optionsBuilder.Append(String.Format(CultureInfo.InvariantCulture, " insertionMode: {0},", InsertionModeString));
159             optionsBuilder.Append(PropertyStringIfSpecified("confirm", Confirm));
160             optionsBuilder.Append(PropertyStringIfSpecified("httpMethod", HttpMethod));
161             optionsBuilder.Append(PropertyStringIfSpecified("loadingElementId", LoadingElementId));
162             optionsBuilder.Append(PropertyStringIfSpecified("updateTargetId", UpdateTargetId));
163             optionsBuilder.Append(PropertyStringIfSpecified("url", Url));
164             optionsBuilder.Append(EventStringIfSpecified("onBegin", OnBegin));
165             optionsBuilder.Append(EventStringIfSpecified("onComplete", OnComplete));
166             optionsBuilder.Append(EventStringIfSpecified("onFailure", OnFailure));
167             optionsBuilder.Append(EventStringIfSpecified("onSuccess", OnSuccess));
168             optionsBuilder.Length--;
169             optionsBuilder.Append(" }");
170             return optionsBuilder.ToString();
171         }
172 
ToUnobtrusiveHtmlAttributes()173         public IDictionary<string, object> ToUnobtrusiveHtmlAttributes() {
174             var result = new Dictionary<string, object> {
175                 { "data-ajax", "true" },
176             };
177 
178             AddToDictionaryIfSpecified(result, "data-ajax-url", Url);
179             AddToDictionaryIfSpecified(result, "data-ajax-method", HttpMethod);
180             AddToDictionaryIfSpecified(result, "data-ajax-confirm", Confirm);
181 
182             AddToDictionaryIfSpecified(result, "data-ajax-begin", OnBegin);
183             AddToDictionaryIfSpecified(result, "data-ajax-complete", OnComplete);
184             AddToDictionaryIfSpecified(result, "data-ajax-failure", OnFailure);
185             AddToDictionaryIfSpecified(result, "data-ajax-success", OnSuccess);
186 
187             if (!String.IsNullOrWhiteSpace(LoadingElementId)) {
188                 result.Add("data-ajax-loading", "#" + LoadingElementId);
189 
190                 if (LoadingElementDuration > 0) {
191                     result.Add("data-ajax-loading-duration", LoadingElementDuration);
192                 }
193             }
194 
195             if (!String.IsNullOrWhiteSpace(UpdateTargetId)) {
196                 result.Add("data-ajax-update", "#" + UpdateTargetId);
197                 result.Add("data-ajax-mode", InsertionModeUnobtrusive);
198             }
199 
200             return result;
201         }
202 
203         // Helpers
204 
AddToDictionaryIfSpecified(IDictionary<string, object> dictionary, string name, string value)205         private static void AddToDictionaryIfSpecified(IDictionary<string, object> dictionary, string name, string value) {
206             if (!String.IsNullOrWhiteSpace(value)) {
207                 dictionary.Add(name, value);
208             }
209         }
210 
EventStringIfSpecified(string propertyName, string handler)211         private static string EventStringIfSpecified(string propertyName, string handler) {
212             if (!String.IsNullOrEmpty(handler)) {
213                 return String.Format(CultureInfo.InvariantCulture, " {0}: Function.createDelegate(this, {1}),", propertyName, handler.ToString());
214             }
215             return String.Empty;
216         }
217 
PropertyStringIfSpecified(string propertyName, string propertyValue)218         private static string PropertyStringIfSpecified(string propertyName, string propertyValue) {
219             if (!String.IsNullOrEmpty(propertyValue)) {
220                 string escapedPropertyValue = propertyValue.Replace("'", @"\'");
221                 return String.Format(CultureInfo.InvariantCulture, " {0}: '{1}',", propertyName, escapedPropertyValue);
222             }
223             return String.Empty;
224         }
225     }
226 }
227