1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc.Ajax {
\r
15 using System.Diagnostics.CodeAnalysis;
\r
16 using System.Globalization;
\r
18 using System.Web.Mvc.Resources;
\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
32 public string Confirm {
\r
34 return _confirm ?? String.Empty;
\r
41 public string HttpMethod {
\r
43 return _httpMethod ?? String.Empty;
\r
46 _httpMethod = value;
\r
50 public InsertionMode InsertionMode {
\r
52 return _insertionMode;
\r
56 case InsertionMode.Replace:
\r
57 case InsertionMode.InsertAfter:
\r
58 case InsertionMode.InsertBefore:
\r
59 _insertionMode = value;
\r
63 throw new ArgumentOutOfRangeException("value", String.Format(CultureInfo.InvariantCulture,
\r
64 MvcResources.Common_InvalidEnumValue, value, typeof(InsertionMode).FullName));
\r
69 public string LoadingElementId {
\r
71 return _loadingElementId ?? String.Empty;
\r
74 _loadingElementId = value;
\r
78 public string OnBegin {
\r
80 return _onBegin ?? String.Empty;
\r
87 public string OnComplete {
\r
89 return _onComplete ?? String.Empty;
\r
92 _onComplete = value;
\r
96 public string OnFailure {
\r
98 return _onFailure ?? String.Empty;
\r
101 _onFailure = value;
\r
105 public string OnSuccess {
\r
107 return _onSuccess ?? String.Empty;
\r
110 _onSuccess = value;
\r
114 public string UpdateTargetId {
\r
116 return _updateTargetId ?? String.Empty;
\r
119 _updateTargetId = value;
\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
127 return _url ?? String.Empty;
\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
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
156 return String.Empty;
\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
164 return String.Empty;
\r