From 56bd434a8f697c5ad78cdbf8219aa5a194b66a8b Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 7 Mar 2022 20:42:46 -0500 Subject: [PATCH] added some extensions, not all working --- BinaryDad.Extensions.sln | 19 +- .../BinaryDad.Web.Extensions.csproj | 16 + .../Extensions/ContextExtensions.cs | 19 ++ .../Extensions/RequestExtensions.cs | 35 +++ .../Extensions/RouteExtensions.cs | 23 ++ .../Extensions/ViewExtensions.cs | 278 ++++++++++++++++++ 6 files changed, 379 insertions(+), 11 deletions(-) create mode 100644 BinaryDad.Web.Extensions/BinaryDad.Web.Extensions.csproj create mode 100644 BinaryDad.Web.Extensions/Extensions/ContextExtensions.cs create mode 100644 BinaryDad.Web.Extensions/Extensions/RequestExtensions.cs create mode 100644 BinaryDad.Web.Extensions/Extensions/RouteExtensions.cs create mode 100644 BinaryDad.Web.Extensions/Extensions/ViewExtensions.cs diff --git a/BinaryDad.Extensions.sln b/BinaryDad.Extensions.sln index ad504bd..75050ff 100644 --- a/BinaryDad.Extensions.sln +++ b/BinaryDad.Extensions.sln @@ -1,10 +1,12 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29123.88 +# Visual Studio Version 17 +VisualStudioVersion = 17.1.32210.238 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BinaryDad.Extensions", "BinaryDad.Extensions\BinaryDad.Extensions.csproj", "{2676B147-0E5A-4161-88B6-6EAFE814B769}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BinaryDad.Web.Extensions", "BinaryDad.Web.Extensions\BinaryDad.Web.Extensions.csproj", "{4ABD9A10-95DA-4843-9EF0-7BF70658311D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {2676B147-0E5A-4161-88B6-6EAFE814B769}.Debug|Any CPU.Build.0 = Debug|Any CPU {2676B147-0E5A-4161-88B6-6EAFE814B769}.Release|Any CPU.ActiveCfg = Release|Any CPU {2676B147-0E5A-4161-88B6-6EAFE814B769}.Release|Any CPU.Build.0 = Release|Any CPU + {4ABD9A10-95DA-4843-9EF0-7BF70658311D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4ABD9A10-95DA-4843-9EF0-7BF70658311D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4ABD9A10-95DA-4843-9EF0-7BF70658311D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4ABD9A10-95DA-4843-9EF0-7BF70658311D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -22,13 +28,4 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8B0AB60B-3317-435D-9945-0EE5492D5141} EndGlobalSection - GlobalSection(TeamFoundationVersionControl) = preSolution - SccNumberOfProjects = 2 - SccEnterpriseProvider = {4CA58AB2-18FA-4F8D-95D4-32DDF27D184C} - SccTeamFoundationServer = http://tfs.binarydad.com:8080/tfs/defaultcollection - SccLocalPath0 = . - SccProjectUniqueName1 = BinaryDad.Extensions\\BinaryDad.Extensions.csproj - SccProjectName1 = BinaryDad.Extensions - SccLocalPath1 = BinaryDad.Extensions - EndGlobalSection EndGlobal diff --git a/BinaryDad.Web.Extensions/BinaryDad.Web.Extensions.csproj b/BinaryDad.Web.Extensions/BinaryDad.Web.Extensions.csproj new file mode 100644 index 0000000..4232f4e --- /dev/null +++ b/BinaryDad.Web.Extensions/BinaryDad.Web.Extensions.csproj @@ -0,0 +1,16 @@ + + + + netstandard2.0 + + + + + + + + + + + + diff --git a/BinaryDad.Web.Extensions/Extensions/ContextExtensions.cs b/BinaryDad.Web.Extensions/Extensions/ContextExtensions.cs new file mode 100644 index 0000000..ecbc816 --- /dev/null +++ b/BinaryDad.Web.Extensions/Extensions/ContextExtensions.cs @@ -0,0 +1,19 @@ +using System.Web.Mvc; + +namespace Nava.Web.UI +{ + public static class ContextExtensions + { + public static T GetViewModel(this ActionExecutedContext context) where T : class + { + var result = context.Result as ViewResultBase; + + if (result != null) + { + return result.Model as T; + } + + return default; + } + } +} diff --git a/BinaryDad.Web.Extensions/Extensions/RequestExtensions.cs b/BinaryDad.Web.Extensions/Extensions/RequestExtensions.cs new file mode 100644 index 0000000..7804c73 --- /dev/null +++ b/BinaryDad.Web.Extensions/Extensions/RequestExtensions.cs @@ -0,0 +1,35 @@ +using System.Collections.Generic; +using System.Collections.Specialized; + +namespace Nava.Web.UI +{ + public static class RequestExtensions + { + /// + /// Retrieves the submitted action (string value "submit-action") value from the request. Typically, this is the name of the + /// + /// Name of submitted action value + /// + public static bool HasSubmitAction(this NameValueCollection collection, string action) + { + var submittedAction = collection["submit-action"]; + + return submittedAction != null && submittedAction.Equals(action, System.StringComparison.OrdinalIgnoreCase); + } + + /// + /// Retrieves form data in a key/value pair collection + /// + /// + /// + public static IEnumerable> GetParameters(this NameValueCollection collection) + { + foreach (string k in collection.Keys) + { + yield return new KeyValuePair(k, collection[k]); + } + } + } +} \ No newline at end of file diff --git a/BinaryDad.Web.Extensions/Extensions/RouteExtensions.cs b/BinaryDad.Web.Extensions/Extensions/RouteExtensions.cs new file mode 100644 index 0000000..dd6a7b4 --- /dev/null +++ b/BinaryDad.Web.Extensions/Extensions/RouteExtensions.cs @@ -0,0 +1,23 @@ +using Microsoft.AspNetCore.Routing; + +namespace Nava.Web.UI +{ + public static class RouteExtensions + { + public static RouteValueDictionary AddCreate(this RouteValueDictionary routeValues, string key, object data) + { + // even if we have no routes, create a route value dictionary with the clientId as a value + if (routeValues == null) + { + routeValues = new RouteValueDictionary(); + } + + if (!routeValues.ContainsKey(key)) + { + routeValues.Add(key, data); + } + + return routeValues; + } + } +} diff --git a/BinaryDad.Web.Extensions/Extensions/ViewExtensions.cs b/BinaryDad.Web.Extensions/Extensions/ViewExtensions.cs new file mode 100644 index 0000000..cb0b889 --- /dev/null +++ b/BinaryDad.Web.Extensions/Extensions/ViewExtensions.cs @@ -0,0 +1,278 @@ +using BinaryDad.Extensions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Web.Mvc; +using System.Web.Mvc.Html; +using System.Web.WebPages.Html; + +namespace Nava.Web.UI +{ + public static class ViewExtensions + { + private const string DateFormat = "{0:M/d/yyyy}"; + + #region HtmlHelper + + //public static object GetRouteValue(this HtmlHelper html, string key) => html.ViewContext.RouteData.Values[key]; + + #region TextBoxFor<> + + public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes, bool enabled) => htmlHelper.TextBoxFor(expression, htmlAttributes.GetPropertyValues(), enabled); + + public static MvcHtmlString TextBoxFor(this HtmlHelper htmlHelper, Expression> expression, IDictionary htmlAttributes, bool enabled) + { + if (!enabled) + { + htmlAttributes.Add("disabled", "disabled"); + } + + return htmlHelper.TextBoxFor(expression, htmlAttributes); + } + + #endregion + + #region TextAreaFor<> + + public static MvcHtmlString TextAreaFor(this HtmlHelper htmlHelper, Expression> expression, object htmlAttributes, bool enabled) => htmlHelper.TextAreaFor(expression, htmlAttributes.GetPropertyValues(), enabled); + + public static MvcHtmlString TextAreaFor(this HtmlHelper htmlHelper, Expression> expression, IDictionary htmlAttributes, bool enabled) + { + if (!enabled) + { + htmlAttributes.Add("disabled", "disabled"); + } + + return htmlHelper.TextAreaFor(expression, htmlAttributes); + } + + #endregion + + #region DropDownListFor<> + + public static MvcHtmlString DropDownListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string optionLabel, object htmlAttributes, bool enabled) + { + var properties = htmlAttributes.GetPropertyValues(); + + if (!enabled) + { + properties.Add("disabled", "disabled"); + } + + return htmlHelper.DropDownListFor(expression, selectList, optionLabel, properties); + } + + #endregion + + #region DropDownList + + public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, object htmlAttributes, bool enabled) => htmlHelper.DropDownList(name, selectList, null, htmlAttributes, enabled); + + public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, string optionLabel, object htmlAttributes, bool enabled) + { + var properties = htmlAttributes.GetPropertyValues(); + + if (!enabled) + { + properties.Add("disabled", "disabled"); + } + + return htmlHelper.DropDownList(name, selectList, optionLabel, properties); + } + + #endregion + + #region CheckBox + + public static MvcHtmlString CheckBox(this HtmlHelper htmlHelper, string name, bool isChecked, object htmlAttributes, bool enabled) + { + var properties = htmlAttributes.GetPropertyValues(); + + if (!enabled) + { + properties.Add("disabled", "disabled"); + } + + return htmlHelper.CheckBox(name, isChecked, properties); + } + + #endregion + + #region DateBoxFor<> + + public static MvcHtmlString DateBoxFor(this HtmlHelper html, Expression> expression) => html.DateBoxFor(expression, null); + + public static MvcHtmlString DateBoxFor(this HtmlHelper html, Expression> expression, bool enabled) => html.DateBoxFor(expression, null, enabled); + + public static MvcHtmlString DateBoxFor(this HtmlHelper html, Expression> expression, object htmlAttributes) => html.DateBoxFor(expression, htmlAttributes, true); + + public static MvcHtmlString DateBoxFor(this HtmlHelper html, Expression> expression, object htmlAttributes, bool enabled) + { + var properties = htmlAttributes.GetPropertyValues() ?? new Dictionary(); + + UpdateDateBoxProperties(properties, enabled); + + return html.TextBoxFor(expression, DateFormat, properties); + } + + public static MvcHtmlString DateBox(this HtmlHelper html, string name, object value) => html.DateBox(name, value, null); + + public static MvcHtmlString DateBox(this HtmlHelper html, string name, object value, object htmlAttributes) => html.DateBox(name, value, htmlAttributes, true); + + public static MvcHtmlString DateBox(this HtmlHelper html, string name, object value, object htmlAttributes, bool enabled) + { + var properties = htmlAttributes.GetPropertyValues() ?? new Dictionary(); + + UpdateDateBoxProperties(properties, enabled); + + return html.TextBox(name, value, DateFormat, properties); + } + + #endregion + + #region EnumDropDownListFor<> + + public static MvcHtmlString EnumDropDownListFor(this HtmlHelper htmlHelper, Expression> expression, string optionLabel, object htmlAttributes, bool useDescription) + { + // shouldnt happen here, but just in case + if (!useDescription) + { + return htmlHelper.EnumDropDownListFor(expression, optionLabel, htmlAttributes); + } + + var type = typeof(TEnum); + + if (type.IsNullable()) + { + type = Nullable.GetUnderlyingType(type); + } + + var values = Enum.GetValues(type) + .Cast() + .Select(v => new + { + Value = Convert.ToInt32(v), + Text = (v as Enum).GetDescription() + }) + .ToList(); + + var list = new SelectList(values, "Value", "Text"); + + return htmlHelper.DropDownListFor(expression, list, optionLabel, htmlAttributes); + } + + #endregion + + #region ModalActionLink + + public static MvcHtmlString ModalActionLink(this HtmlHelper html, string linkText, string actionName, object routeValues, string dataTarget) => html.ModalActionLink(linkText, actionName, routeValues, dataTarget, null); + + public static MvcHtmlString ModalActionLink(this HtmlHelper html, string linkText, string actionName, object routeValues, string dataTarget, object htmlAttributes) => html.ModalActionLink(linkText, actionName, null, routeValues, dataTarget, htmlAttributes); + + public static MvcHtmlString ModalActionLink(this HtmlHelper html, string linkText, string actionName, string controllerName, object routeValues, string dataTarget) => html.ModalActionLink(linkText, actionName, controllerName, routeValues, dataTarget, null); + + public static MvcHtmlString ModalActionLink(this HtmlHelper html, string linkText, string actionName, string controllerName, object routeValues, string dataTarget, object htmlAttributes) => html.ModalActionLink(linkText, actionName, controllerName, routeValues, dataTarget, htmlAttributes, true); + + public static MvcHtmlString ModalActionLink(this HtmlHelper html, string linkText, string actionName, string controllerName, object routeValues, string dataTarget, object htmlAttributes, bool enabled) + { + var properties = htmlAttributes.GetPropertyValues() ?? new Dictionary(); + + MergeProperties(properties, GetModalProperties(dataTarget)); + + if (!enabled) + { + properties.Add("disabled", "disabled"); + } + + return html.ActionLink(linkText, actionName, controllerName, new RouteValueDictionary(routeValues), properties); + } + + #endregion + + #region ModalLink + + public static MvcHtmlString ModalLink(this HtmlHelper html, string linkText, string url, string dataTarget, object htmlAttributes) + { + var properties = htmlAttributes.GetPropertyValues() ?? new Dictionary(); + var builder = new TagBuilder("a"); + + builder.MergeAttributes(properties); + builder.MergeAttributes(GetModalProperties(dataTarget).ToDictionary()); + builder.MergeAttribute("href", url); + builder.SetInnerText(linkText); + + return MvcHtmlString.Create(builder.ToString()); + } + + #endregion + + #endregion + + #region UrlHelper + + public static string Action(this UrlHelper url, string actionName, string controllerName, object routeValues, string returnUrl, string returnTitle) + { + var values = new RouteValueDictionary(routeValues) + .AddCreate("returnUrl", returnUrl) + .AddCreate("returnTitle", returnTitle); + + return url.Action(actionName, controllerName, values); + } + + #endregion + + #region ViewData + + public static T Get(this ViewDataDictionary viewData, string key) where T : class => viewData[key] as T; + + #endregion + + #region Private methods + + private static IEnumerable> GetModalProperties(string dataTarget) + { + return new Dictionary + { + { "data-toggle", "modal" }, + { "data-target", dataTarget } + }; + } + + private static void MergeProperties(IDictionary source, IEnumerable> merge) + { + if (source != null && merge != null) + { + merge.ForEach(m => + { + if (source.ContainsKey(m.Key)) + { + source[m.Key] += String.Format(" {0}", m.Value.ToString()); + } + else + { + source.Add(m.Key, m.Value); + } + }); + } + } + + private static void UpdateDateBoxProperties(IDictionary properties, bool enabled) + { + MergeProperties(properties, new Dictionary + { + { "class", "datepicker form-control enrollmentdate edit-not-allowed" }, + { "data-provide", "datepicker" }, + { "data-date-format", "m/d/yyyy" }, + { "readonly", "readonly" } // this also hides the keyboard in iOS + }); + + if (!enabled) + { + properties.Add("disabled", "disabled"); + } + } + + #endregion + } +} \ No newline at end of file