Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,300 @@
namespace System.Web.Mvc.Ajax {
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Mvc.Resources;
using System.Web.Routing;
public static class AjaxExtensions {
private const string LinkOnClickFormat = "Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), {0});";
private const string FormOnClickValue = "Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));";
private const string FormOnSubmitFormat = "Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), {0});";
private const string _globalizationScript = @"<script type=""text/javascript"" src=""{0}""></script>";
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, AjaxOptions ajaxOptions) {
return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, ajaxOptions);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, object routeValues, AjaxOptions ajaxOptions) {
return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
return ActionLink(ajaxHelper, linkText, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, AjaxOptions ajaxOptions) {
return ActionLink(ajaxHelper, linkText, actionName, controllerName, null /* values */, ajaxOptions, null /* htmlAttributes */);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions) {
return ActionLink(ajaxHelper, linkText, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
RouteValueDictionary newValues = new RouteValueDictionary(routeValues);
RouteValueDictionary newAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
return ActionLink(ajaxHelper, linkText, actionName, controllerName, newValues, ajaxOptions, newAttributes);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
return ActionLink(ajaxHelper, linkText, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
string targetUrl = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
return MvcHtmlString.Create(GenerateLink(ajaxHelper, linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes));
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
RouteValueDictionary newValues = new RouteValueDictionary(routeValues);
RouteValueDictionary newAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
return ActionLink(ajaxHelper, linkText, actionName, controllerName, protocol, hostName, fragment, newValues, ajaxOptions, newAttributes);
}
public static MvcHtmlString ActionLink(this AjaxHelper ajaxHelper, string linkText, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
string targetUrl = UrlHelper.GenerateUrl(null /* routeName */, actionName, controllerName, protocol, hostName, fragment, routeValues, ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
return MvcHtmlString.Create(GenerateLink(ajaxHelper, linkText, targetUrl, ajaxOptions, htmlAttributes));
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, AjaxOptions ajaxOptions) {
string formAction = ajaxHelper.ViewContext.HttpContext.Request.RawUrl;
return FormHelper(ajaxHelper, formAction, ajaxOptions, new RouteValueDictionary());
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, AjaxOptions ajaxOptions) {
return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, ajaxOptions);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions) {
return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
return BeginForm(ajaxHelper, actionName, (string)null /* controllerName */, routeValues, ajaxOptions, htmlAttributes);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, AjaxOptions ajaxOptions) {
return BeginForm(ajaxHelper, actionName, controllerName, null /* values */, ajaxOptions, null /* htmlAttributes */);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions) {
return BeginForm(ajaxHelper, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
RouteValueDictionary newValues = new RouteValueDictionary(routeValues);
RouteValueDictionary newAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
return BeginForm(ajaxHelper, actionName, controllerName, newValues, ajaxOptions, newAttributes);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
return BeginForm(ajaxHelper, actionName, controllerName, routeValues, ajaxOptions, null /* htmlAttributes */);
}
public static MvcForm BeginForm(this AjaxHelper ajaxHelper, string actionName, string controllerName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
// get target URL
string formAction = UrlHelper.GenerateUrl(null, actionName, controllerName, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, true /* includeImplicitMvcValues */);
return FormHelper(ajaxHelper, formAction, ajaxOptions, htmlAttributes);
}
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, AjaxOptions ajaxOptions) {
return BeginRouteForm(ajaxHelper, routeName, null /* routeValues */, ajaxOptions, null /* htmlAttributes */);
}
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, object routeValues, AjaxOptions ajaxOptions) {
return BeginRouteForm(ajaxHelper, routeName, (object)routeValues, ajaxOptions, null /* htmlAttributes */);
}
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
RouteValueDictionary newAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
return BeginRouteForm(ajaxHelper, routeName, new RouteValueDictionary(routeValues), ajaxOptions, newAttributes);
}
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
return BeginRouteForm(ajaxHelper, routeName, routeValues, ajaxOptions, null /* htmlAttributes */);
}
public static MvcForm BeginRouteForm(this AjaxHelper ajaxHelper, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
string formAction = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
return FormHelper(ajaxHelper, formAction, ajaxOptions, htmlAttributes);
}
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "You don't want to dispose of this object unless you intend to write to the response")]
private static MvcForm FormHelper(this AjaxHelper ajaxHelper, string formAction, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
TagBuilder builder = new TagBuilder("form");
builder.MergeAttributes(htmlAttributes);
builder.MergeAttribute("action", formAction);
builder.MergeAttribute("method", "post");
ajaxOptions = GetAjaxOptions(ajaxOptions);
if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) {
builder.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
}
else {
builder.MergeAttribute("onclick", FormOnClickValue);
builder.MergeAttribute("onsubmit", GenerateAjaxScript(ajaxOptions, FormOnSubmitFormat));
}
if (ajaxHelper.ViewContext.ClientValidationEnabled) {
// forms must have an ID for client validation
builder.GenerateId(ajaxHelper.ViewContext.FormIdGenerator());
}
ajaxHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
MvcForm theForm = new MvcForm(ajaxHelper.ViewContext);
if (ajaxHelper.ViewContext.ClientValidationEnabled) {
ajaxHelper.ViewContext.FormContext.FormId = builder.Attributes["id"];
}
return theForm;
}
public static MvcHtmlString GlobalizationScript(this AjaxHelper ajaxHelper) {
return GlobalizationScript(ajaxHelper, CultureInfo.CurrentCulture);
}
[SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "ajaxHelper", Justification = "This is an extension method")]
public static MvcHtmlString GlobalizationScript(this AjaxHelper ajaxHelper, CultureInfo cultureInfo) {
return GlobalizationScriptHelper(AjaxHelper.GlobalizationScriptPath, cultureInfo);
}
private static MvcHtmlString GlobalizationScriptHelper(string scriptPath, CultureInfo cultureInfo) {
if (cultureInfo == null) {
throw new ArgumentNullException("cultureInfo");
}
string src = VirtualPathUtility.AppendTrailingSlash(scriptPath) + cultureInfo.Name + ".js";
string scriptWithCorrectNewLines = _globalizationScript.Replace("\r\n", Environment.NewLine);
string formatted = String.Format(CultureInfo.InvariantCulture, scriptWithCorrectNewLines, src);
return MvcHtmlString.Create(formatted);
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, object routeValues, AjaxOptions ajaxOptions) {
return RouteLink(ajaxHelper, linkText, null /* routeName */, new RouteValueDictionary(routeValues), ajaxOptions,
new Dictionary<string, object>());
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
return RouteLink(ajaxHelper, linkText, null /* routeName */, new RouteValueDictionary(routeValues), ajaxOptions,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
return RouteLink(ajaxHelper, linkText, null /* routeName */, routeValues, ajaxOptions,
new Dictionary<string, object>());
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
return RouteLink(ajaxHelper, linkText, null /* routeName */, routeValues, ajaxOptions, htmlAttributes);
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions) {
return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions,
new Dictionary<string, object>());
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions, object htmlAttributes) {
return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions, HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(), ajaxOptions, htmlAttributes);
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, object routeValues, AjaxOptions ajaxOptions) {
return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(routeValues), ajaxOptions,
new Dictionary<string, object>());
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, object routeValues, AjaxOptions ajaxOptions, object htmlAttributes) {
return RouteLink(ajaxHelper, linkText, routeName, new RouteValueDictionary(routeValues), ajaxOptions,
HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions) {
return RouteLink(ajaxHelper, linkText, routeName, routeValues, ajaxOptions, new Dictionary<string, object>());
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
string targetUrl = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
return MvcHtmlString.Create(GenerateLink(ajaxHelper, linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes));
}
public static MvcHtmlString RouteLink(this AjaxHelper ajaxHelper, string linkText, string routeName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
if (String.IsNullOrEmpty(linkText)) {
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "linkText");
}
string targetUrl = UrlHelper.GenerateUrl(routeName, null /* actionName */, null /* controllerName */, protocol, hostName, fragment, routeValues ?? new RouteValueDictionary(), ajaxHelper.RouteCollection, ajaxHelper.ViewContext.RequestContext, false /* includeImplicitMvcValues */);
return MvcHtmlString.Create(GenerateLink(ajaxHelper, linkText, targetUrl, GetAjaxOptions(ajaxOptions), htmlAttributes));
}
private static string GenerateLink(AjaxHelper ajaxHelper, string linkText, string targetUrl, AjaxOptions ajaxOptions, IDictionary<string, object> htmlAttributes) {
TagBuilder tag = new TagBuilder("a") {
InnerHtml = HttpUtility.HtmlEncode(linkText)
};
tag.MergeAttributes(htmlAttributes);
tag.MergeAttribute("href", targetUrl);
if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) {
tag.MergeAttributes(ajaxOptions.ToUnobtrusiveHtmlAttributes());
}
else {
tag.MergeAttribute("onclick", GenerateAjaxScript(ajaxOptions, LinkOnClickFormat));
}
return tag.ToString(TagRenderMode.Normal);
}
private static string GenerateAjaxScript(AjaxOptions ajaxOptions, string scriptFormat) {
string optionsString = ajaxOptions.ToJavascriptString();
return String.Format(CultureInfo.InvariantCulture, scriptFormat, optionsString);
}
private static AjaxOptions GetAjaxOptions(AjaxOptions ajaxOptions) {
return (ajaxOptions != null) ? ajaxOptions : new AjaxOptions();
}
}
}

View File

@@ -0,0 +1,226 @@
namespace System.Web.Mvc.Ajax {
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
public class AjaxOptions {
private string _confirm;
private string _httpMethod;
private InsertionMode _insertionMode = InsertionMode.Replace;
private string _loadingElementId;
private string _onBegin;
private string _onComplete;
private string _onFailure;
private string _onSuccess;
private string _updateTargetId;
private string _url;
public string Confirm {
get {
return _confirm ?? String.Empty;
}
set {
_confirm = value;
}
}
public string HttpMethod {
get {
return _httpMethod ?? String.Empty;
}
set {
_httpMethod = value;
}
}
public InsertionMode InsertionMode {
get {
return _insertionMode;
}
set {
switch (value) {
case InsertionMode.Replace:
case InsertionMode.InsertAfter:
case InsertionMode.InsertBefore:
_insertionMode = value;
return;
default:
throw new ArgumentOutOfRangeException("value");
}
}
}
internal string InsertionModeString {
get {
switch (InsertionMode) {
case InsertionMode.Replace:
return "Sys.Mvc.InsertionMode.replace";
case InsertionMode.InsertBefore:
return "Sys.Mvc.InsertionMode.insertBefore";
case InsertionMode.InsertAfter:
return "Sys.Mvc.InsertionMode.insertAfter";
default:
return ((int)InsertionMode).ToString(CultureInfo.InvariantCulture);
}
}
}
internal string InsertionModeUnobtrusive {
get {
switch (InsertionMode) {
case InsertionMode.Replace:
return "replace";
case InsertionMode.InsertBefore:
return "before";
case InsertionMode.InsertAfter:
return "after";
default:
return ((int)InsertionMode).ToString(CultureInfo.InvariantCulture);
}
}
}
public int LoadingElementDuration {
get;
set;
}
public string LoadingElementId {
get {
return _loadingElementId ?? String.Empty;
}
set {
_loadingElementId = value;
}
}
public string OnBegin {
get {
return _onBegin ?? String.Empty;
}
set {
_onBegin = value;
}
}
public string OnComplete {
get {
return _onComplete ?? String.Empty;
}
set {
_onComplete = value;
}
}
public string OnFailure {
get {
return _onFailure ?? String.Empty;
}
set {
_onFailure = value;
}
}
public string OnSuccess {
get {
return _onSuccess ?? String.Empty;
}
set {
_onSuccess = value;
}
}
public string UpdateTargetId {
get {
return _updateTargetId ?? String.Empty;
}
set {
_updateTargetId = value;
}
}
[SuppressMessage("Microsoft.Design", "CA1056:UriPropertiesShouldNotBeStrings", Justification = "This property is used by the optionsBuilder which always accepts a string.")]
public string Url {
get {
return _url ?? String.Empty;
}
set {
_url = value;
}
}
internal string ToJavascriptString() {
// creates a string of the form { key1: value1, key2 : value2, ... }
StringBuilder optionsBuilder = new StringBuilder("{");
optionsBuilder.Append(String.Format(CultureInfo.InvariantCulture, " insertionMode: {0},", InsertionModeString));
optionsBuilder.Append(PropertyStringIfSpecified("confirm", Confirm));
optionsBuilder.Append(PropertyStringIfSpecified("httpMethod", HttpMethod));
optionsBuilder.Append(PropertyStringIfSpecified("loadingElementId", LoadingElementId));
optionsBuilder.Append(PropertyStringIfSpecified("updateTargetId", UpdateTargetId));
optionsBuilder.Append(PropertyStringIfSpecified("url", Url));
optionsBuilder.Append(EventStringIfSpecified("onBegin", OnBegin));
optionsBuilder.Append(EventStringIfSpecified("onComplete", OnComplete));
optionsBuilder.Append(EventStringIfSpecified("onFailure", OnFailure));
optionsBuilder.Append(EventStringIfSpecified("onSuccess", OnSuccess));
optionsBuilder.Length--;
optionsBuilder.Append(" }");
return optionsBuilder.ToString();
}
public IDictionary<string, object> ToUnobtrusiveHtmlAttributes() {
var result = new Dictionary<string, object> {
{ "data-ajax", "true" },
};
AddToDictionaryIfSpecified(result, "data-ajax-url", Url);
AddToDictionaryIfSpecified(result, "data-ajax-method", HttpMethod);
AddToDictionaryIfSpecified(result, "data-ajax-confirm", Confirm);
AddToDictionaryIfSpecified(result, "data-ajax-begin", OnBegin);
AddToDictionaryIfSpecified(result, "data-ajax-complete", OnComplete);
AddToDictionaryIfSpecified(result, "data-ajax-failure", OnFailure);
AddToDictionaryIfSpecified(result, "data-ajax-success", OnSuccess);
if (!String.IsNullOrWhiteSpace(LoadingElementId)) {
result.Add("data-ajax-loading", "#" + LoadingElementId);
if (LoadingElementDuration > 0) {
result.Add("data-ajax-loading-duration", LoadingElementDuration);
}
}
if (!String.IsNullOrWhiteSpace(UpdateTargetId)) {
result.Add("data-ajax-update", "#" + UpdateTargetId);
result.Add("data-ajax-mode", InsertionModeUnobtrusive);
}
return result;
}
// Helpers
private static void AddToDictionaryIfSpecified(IDictionary<string, object> dictionary, string name, string value) {
if (!String.IsNullOrWhiteSpace(value)) {
dictionary.Add(name, value);
}
}
private static string EventStringIfSpecified(string propertyName, string handler) {
if (!String.IsNullOrEmpty(handler)) {
return String.Format(CultureInfo.InvariantCulture, " {0}: Function.createDelegate(this, {1}),", propertyName, handler.ToString());
}
return String.Empty;
}
private static string PropertyStringIfSpecified(string propertyName, string propertyValue) {
if (!String.IsNullOrEmpty(propertyValue)) {
string escapedPropertyValue = propertyValue.Replace("'", @"\'");
return String.Format(CultureInfo.InvariantCulture, " {0}: '{1}',", propertyName, escapedPropertyValue);
}
return String.Empty;
}
}
}

View File

@@ -0,0 +1,7 @@
namespace System.Web.Mvc.Ajax {
public enum InsertionMode {
Replace = 0,
InsertBefore = 1,
InsertAfter = 2
}
}