using System.Web.Mvc; namespace FHT.Agency.Web.Utility { public abstract class SuccessAction { public static SuccessAction Refresh() { return RefreshSuccessAction.Instance; } public static SuccessAction Redirect(string url) { return RedirectSuccessAction.Redirect(url); } public sealed class RefreshSuccessAction : SuccessAction { public static readonly RefreshSuccessAction Instance = new RefreshSuccessAction(); private RefreshSuccessAction() { } } public sealed class RedirectSuccessAction : SuccessAction { public string Url { get; private set; } private RedirectSuccessAction(string url) { Url = url; } public static RedirectSuccessAction Redirect(string url) { return new RedirectSuccessAction(url); } } public sealed class SubmitSuccessAction : SuccessAction { public static readonly SubmitSuccessAction Instance = new SubmitSuccessAction(); private SubmitSuccessAction() { } } } public class ActionableOptions { public string[] AdditionalClasses { get; set; } public string ConfirmTip { get; set; } public string SuccessTip { get; set; } /// /// null means no actions are taken /// public SuccessAction SuccessAction { get; set; } } public static partial class AgencyHtmlHelperExtensions { private const string SuccessActionKey = "data-success-action"; public static MvcHtmlString ActionableAction(this HtmlHelper html, string name, string actionName, string controllerName, object routeValues = null, ActionableOptions options = null) { return html.ActionableAction2(name, actionName, controllerName, routeValues, options); } public static MvcHtmlString ActionableAction(this HtmlHelper html, string name, string actionName, string controllerName, long id, ActionableOptions options = null) { return html.ActionableAction2(name, actionName, controllerName, new { Id = id }, options); } private static void AddOptions(TagBuilder anchor, ActionableOptions options) { anchor.Attributes.Add("href", "javascript:void(0);"); if (options != null) { if (options.AdditionalClasses != null) { foreach (string k in options.AdditionalClasses) { anchor.AddCssClass(k); } } if (options.ConfirmTip != null) { anchor.Attributes.Add("data-actionable-confirm-tip", options.ConfirmTip); } if (options.SuccessTip != null) { anchor.Attributes.Add("data-actionable-success-tip", options.SuccessTip); } if (options.SuccessAction != null) { if (options.SuccessAction is SuccessAction.RefreshSuccessAction) { anchor.Attributes.Add(SuccessActionKey, "__refresh__"); } else if (options.SuccessAction is SuccessAction.RedirectSuccessAction) { var url = (SuccessAction.RedirectSuccessAction)options.SuccessAction; anchor.Attributes.Add(SuccessActionKey, url.Url); } else if (options.SuccessAction is SuccessAction.SubmitSuccessAction) { anchor.Attributes.Add(SuccessActionKey, "__submit__"); } } } } public static MvcHtmlString ActionableAction2(this HtmlHelper html, string name, string actionName, string controllerName, object routeValues = null, ActionableOptions options = null) { var urlHelper = new UrlHelper(html.ViewContext.RequestContext); var anchor = new TagBuilder("a"); AddOptions(anchor, options); anchor.AddCssClass("js-actionable-action"); System.Web.Routing.RouteValueDictionary attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(new { }); anchor.MergeAttributes(attrs); anchor.InnerHtml = name; string url = urlHelper.Action(actionName, controllerName, routeValues); anchor.Attributes.Add("data-actionable-url", url); return new MvcHtmlString(anchor.ToString()); } public static MvcHtmlString ActionableBatch(this HtmlHelper html, string name, string actionName, string controllerName, ActionableOptions options = null) { return html.ActionableBatch2(name, actionName, controllerName, new { }, options); } public static MvcHtmlString ActionableBatch2(this HtmlHelper html, string name, string actionName, string controllerName, object routeValues = null, ActionableOptions options = null) { var urlHelper = new UrlHelper(html.ViewContext.RequestContext); var anchor = new TagBuilder("a"); AddOptions(anchor, options); anchor.AddCssClass("js-actionable-batch"); anchor.InnerHtml = name; System.Web.Routing.RouteValueDictionary attrs = HtmlHelper.AnonymousObjectToHtmlAttributes(new { }); anchor.MergeAttributes(attrs); string url = urlHelper.Action(actionName, controllerName, routeValues); anchor.Attributes.Add("data-actionable-url", url); return new MvcHtmlString(anchor.ToString()); } } }