using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Web; using System.Web.Mvc; using ChadSoft.UI; namespace ChadSoft.Web.Mvc { public static class UIExtensions { #region Base Paths private static readonly string STATIC_CONTENT_BASE_PATH = Parse.AppSetting("StaticContent.BasePath") ?? "/content"; private static readonly string IMAGES_BASE_PATH = string.Format("{0}/images", STATIC_CONTENT_BASE_PATH); private static readonly string CSS_STYLES_BASE_PATH = Parse.AppSetting("Stylesheets.BasePath") ?? "/styles"; #endregion /// /// Creates a formatted list of items based on the passed in format /// public static string ToFormattedList(this IEnumerable list, UIListType listType) { string outerListFormat = ""; string listFormat = ""; switch (listType) { case UIListType.Ordered: outerListFormat = "
    {0}
"; listFormat = "
  • {0}
  • "; break; case UIListType.Unordered: outerListFormat = ""; listFormat = "
  • {0}
  • "; break; case UIListType.TableCell: outerListFormat = "{0}"; listFormat = "{0}"; break; default: break; } return string.Format(outerListFormat, ForEach(list, listFormat)); } #region ForEach /// /// Iterates through an enumerable applying the format string to each line. /// /// The enumeration to apply the function to /// The format string to apply against each enumerated object /// The aggregated markup emitted by formatted output of all the enumerated objects. public static string ForEach(this IEnumerable enumerable, string format) { return ForEach(enumerable as IEnumerable, (o => string.Format(format, o))); } /// /// Iterates through an enumerable applying the format string to each line. /// /// The enumeration to apply the function to /// The format string to wrap the result of the inner items /// The format string to apply against each enumerated object /// The aggregated markup emitted by formatted output of all the enumerated objects. public static string ForEach(this IEnumerable enumerable, string outerFormat, string format) { return ForEach(enumerable as IEnumerable, outerFormat, (o => string.Format(format, o))); } #endregion #region ForEach /// /// Iterates through an enumerable adding the result of the function for each line. /// /// Type of object to be used in the function /// The enumeration to which the function will be applied /// The function to apply to each enumerated object /// The aggregated markup emitted by the function calls against all of the enumerated objects. public static string ForEach(this IEnumerable enumerable, Func function) { return ForEach(enumerable, "{0}", function); } /// /// Iterates through an enumerable adding the result of the function for each line. /// /// Type of object to be used in the function /// The enumeration to which the function will be applied /// The format string to wrap the result of the inner items /// The function to apply to each enumerated object /// The aggregated markup emitted by the function calls against all of the enumerated objects. public static string ForEach(this IEnumerable enumerable, string outerFormat, Func function) { var markup = new StringBuilder(); foreach (var val in enumerable) markup.AppendLine(function(val)); return string.Format(outerFormat, markup); } #endregion public static string Stylesheet(this UrlHelper urlHelper, string stylesheet) { var url = string.Format("{0}/{1}", CSS_STYLES_BASE_PATH, CleanRelativePath(stylesheet)); return string.Format("", url); } public static string ImageLink(this HtmlHelper html, string linkUrl, string imageUrl, string title, bool externalLink) { var cleanImageUrl = string.Format("{0}/{1}", IMAGES_BASE_PATH, CleanRelativePath(imageUrl)); var htmlAttibutes = externalLink ? "target='_blank'" : string.Empty; return string.Format("{1}", linkUrl, title, cleanImageUrl, htmlAttibutes); } /// /// Take a path and clean it up (if relative, make it absolute; remove trailing slash, etc.) /// /// the path to clean /// The cleaned-up path private static string CleanRelativePath(string path) { var cleanPath = path; // Clean up the URL if it's not empty or absolute if (!string.IsNullOrEmpty(cleanPath) && !cleanPath.StartsWith("http:", StringComparison.OrdinalIgnoreCase)) { // Clean up the path first if(cleanPath.StartsWith("~/", StringComparison.OrdinalIgnoreCase)) cleanPath = VirtualPathUtility.ToAbsolute(cleanPath); else if(cleanPath.StartsWith("/", StringComparison.OrdinalIgnoreCase)) cleanPath = cleanPath.Substring(1, cleanPath.Length - 1); } return cleanPath; } } }