using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Ajax; using System.Web.Mvc.Html; using System.Web.Routing; //using T4MVC; using System.Web.WebPages; using System.Text.RegularExpressions; using System.IO; using System.Globalization; namespace Disco.Web { public static class HtmlExtensions { public static HtmlHelper GetPageHelper(this System.Web.WebPages.Html.HtmlHelper html) { return ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html; } #region T4MVC Disco Extensions public static MvcHtmlString ActionLinkClass(this HtmlHelper htmlHelper, string linkText, ActionResult result, string CssClasses) { return htmlHelper.ActionLink(linkText, result, new { @class = CssClasses }); } public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, string anchorId = null, string accesskey = null) { return htmlHelper.ActionLink(linkText, result, new { id = anchorId, accesskey = accesskey }); } public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, string anchorId) { return htmlHelper.ActionLink(linkText, result, new { id = anchorId }); } public static MvcHtmlString ActionLinkButton(this HtmlHelper htmlHelper, string linkText, ActionResult result, string id, string @class) { var classValue = string.IsNullOrEmpty(@class) ? "button" : string.Concat("button ", @class); if (string.IsNullOrWhiteSpace(id)) { return T4Extensions.ActionLink(htmlHelper, linkText, result, new { @class = classValue }); } else { return T4Extensions.ActionLink(htmlHelper, linkText, result, new { @class = classValue, id = id }); } } public static MvcHtmlString ActionLinkButton(this HtmlHelper htmlHelper, string linkText, ActionResult result, string id) { return ActionLinkButton(htmlHelper, linkText, result, id, null); } public static MvcHtmlString ActionLinkButton(this HtmlHelper htmlHelper, string linkText, ActionResult result) { return ActionLinkButton(htmlHelper, linkText, result, null); } public static MvcHtmlString ActionLinkSmallButton(this HtmlHelper htmlHelper, string linkText, ActionResult result, string id, string @class) { return ActionLinkButton(htmlHelper, linkText, result, id, string.IsNullOrEmpty(@class) ? "small" : string.Concat("small ", @class)); } public static MvcHtmlString ActionLinkSmallButton(this HtmlHelper htmlHelper, string linkText, ActionResult result, string id) { return ActionLinkSmallButton(htmlHelper, linkText, result, id, null); } public static MvcHtmlString ActionLinkSmallButton(this HtmlHelper htmlHelper, string linkText, ActionResult result) { return ActionLinkSmallButton(htmlHelper, linkText, result, null, null); } public static MvcHtmlString OrganisationLogoUrl(this UrlHelper urlHelper, int Width = 256, int Height = 256) { var config = new Disco.Data.Configuration.SystemConfiguration(null); return new MvcHtmlString(urlHelper.Action(MVC.API.System.OrganisationLogo(Width, Height, config.OrganisationLogoHash))); } #endregion public static MvcHtmlString ToMultilineString(this string s) { return new MvcHtmlString(HttpUtility.HtmlEncode(s).Replace(Environment.NewLine, "
").Replace("\n", "
").Replace("\r", "
")); } public static List> ToBreadcrumb(this HtmlHelper htmlHelper, string title1, ActionResult link1, string title2 = null, ActionResult link2 = null, string title3 = null, ActionResult link3 = null, string title4 = null, ActionResult link4 = null, string title5 = null, ActionResult link5 = null) { var breadCrumbs = new List>(); if (title1 != null) { breadCrumbs.Add(new Tuple(title1, link1)); } if (title2 != null) { breadCrumbs.Add(new Tuple(title2, link2)); } if (title3 != null) { breadCrumbs.Add(new Tuple(title3, link3)); } if (title4 != null) { breadCrumbs.Add(new Tuple(title4, link4)); } if (title5 != null) { breadCrumbs.Add(new Tuple(title5, link5)); } return breadCrumbs; } private static Lazy _ToMultilineJobRefString = new Lazy(() => { return new Regex("(?").Replace(Environment.NewLine, "
"); var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext); return new MvcHtmlString(_ToMultilineJobRefString.Value.Replace(multiLineString, string.Format("#$1", urlHelper.Action(MVC.Job.Show(null))))); } public static IEnumerable ToSelectListItems(this IEnumerable Items, string SelectedItem = null) { if (SelectedItem == null) return Items.Select(i => new SelectListItem() { Value = i, Text = i }); else return Items.Select(i => new SelectListItem() { Value = i, Text = i, Selected = i.Equals(SelectedItem) }); } } }