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");
\r
68 public string LoadingElementId {
\r
70 return _loadingElementId ?? String.Empty;
\r
73 _loadingElementId = value;
\r
77 public string OnBegin {
\r
79 return _onBegin ?? String.Empty;
\r
86 public string OnComplete {
\r
88 return _onComplete ?? String.Empty;
\r
91 _onComplete = value;
\r
95 public string OnFailure {
\r
97 return _onFailure ?? String.Empty;
\r
100 _onFailure = value;
\r
104 public string OnSuccess {
\r
106 return _onSuccess ?? String.Empty;
\r
109 _onSuccess = value;
\r
113 public string UpdateTargetId {
\r
115 return _updateTargetId ?? String.Empty;
\r
118 _updateTargetId = value;
\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
126 return _url ?? String.Empty;
\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
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
155 return String.Empty;
\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
163 return String.Empty;
\r