kreta/KretaWeb/Helpers/TextBoxExtensions.cs
2024-03-13 00:33:46 +01:00

274 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Kendo.Mvc.UI;
using Kendo.Mvc.UI.Fluent;
namespace Kreta.Web.Helpers
{
public static class TextBoxExtensions
{
public static TextBoxBuilder<string> KretaTextBox(this HtmlHelper helper, string name, bool enabled = true, Dictionary<string, object> htmlAttributes = null)
{
var tb = helper.Kendo().TextBox()
//.Name(Guid.NewGuid().ToString())
.Name(name)
.Enable(enabled);
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
htmlAttributes.Add("type", "text");
htmlAttributes.Add("name", name);
tb.HtmlAttributes(htmlAttributes);
return tb;
}
public static TextBoxBuilder<string> KretaTextBoxFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, Dictionary<string, object> htmlAttributes = null)
{
var fieldName = ExpressionHelper.GetExpressionText(expression);
var fullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName);
var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
// var value = metadata.Model;
if (htmlAttributes == null)
htmlAttributes = new Dictionary<string, object>();
var attributes = Mapper.GetUnobtrusiveValidationAttributes(helper, expression, htmlAttributes, metadata);
foreach (var key in attributes.Keys)
{
try
{
if (htmlAttributes.ContainsKey(key) == false)
{
htmlAttributes.Add(key, attributes[key].ToString());
}
}
catch
{
}
}
var validationAttributes = helper.GetUnobtrusiveValidationAttributes(fullBindingName, metadata);
bool isRequiredIf = RequiredIfReflector.RequiredIf(helper, validationAttributes);
if (metadata.IsRequired || isRequiredIf)
{
htmlAttributes.Add("data-labelmsg", metadata.DisplayName + " *");
}
else
{
htmlAttributes.Add("data-labelmsg", metadata.DisplayName);
}
htmlAttributes.Add("type", "text");
htmlAttributes.Add("value", metadata.Model);
if (expression.Body is MemberExpression me && me.Member != null)
{
int maxLength = 0;
MaxLengthAttribute maxLengthAttribute = me.Member.GetCustomAttributes(typeof(MaxLengthAttribute), false).Cast<MaxLengthAttribute>().FirstOrDefault();
if (maxLengthAttribute != null)
{
maxLength = maxLengthAttribute.Length;
}
else
{
StringLengthAttribute stringLengthAttribute = me.Member.GetCustomAttributes(typeof(StringLengthAttribute), false).Cast<StringLengthAttribute>().FirstOrDefault();
if (stringLengthAttribute != null)
{
maxLength = stringLengthAttribute.MaximumLength;
}
}
if (maxLength > 0)
{
htmlAttributes["maxlength"] = maxLength.ToString();
}
}
var tb = helper.Kendo().TextBox()
.Name(fullBindingName)
.HtmlAttributes(htmlAttributes);
return tb;
}
public static MvcHtmlString RenderSearchPanel(this TextBoxBuilder<string> helper)
{
string labelMsg = "";
foreach (var item in helper.ToComponent().HtmlAttributes)
{
if (item.Key == "data-labelmsg" && item.Value != null)
labelMsg = item.Value.ToString();
}
StringBuilder sb = new StringBuilder();
sb.Append("<div class=\"searchInputRowHeight\"><div>");
sb.Append("<label class=\"searchPanelInputLabel\" for=\"").Append(helper.ToComponent().Name).Append("\">").Append(labelMsg).Append("</label>");
sb.Append("</div><div>");
sb.Append(helper.ToHtmlString());
sb.Append("</div></div>");
return new MvcHtmlString(sb.ToString());
}
public static MvcHtmlString RenderSearchPanelSideBar(this TextBoxBuilder<string> helper)
{
string labelMsg = "";
foreach (var item in helper.ToComponent().HtmlAttributes)
{
if (item.Key == "data-labelmsg" && item.Value != null)
labelMsg = item.Value.ToString();
}
StringBuilder sb = new StringBuilder();
sb.Append("<div class=\"searchPanelRow\">");
sb.Append("<div class=\"searchPanelRowTitle\">");
sb.Append("<label class=\"searchPanelLabel\" for=\"").Append(helper.ToComponent().Name).Append("\">").Append(labelMsg).Append("</label>");
sb.Append("</div>");
sb.Append("<div class=\"searchPanelRowValue\">");
sb.Append(helper.ToHtmlString());
sb.Append("</div>");
sb.Append("</div>");
return new MvcHtmlString(sb.ToString());
}
public static MvcHtmlString RenderWithName(this TextBoxBuilder<string> helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null, string customClass = null, bool tooltipOnControl = false, string customLabelMessage = null)
{
string labelMsg = "";
if (!string.IsNullOrWhiteSpace(customLabelMessage))
{
labelMsg = customLabelMessage;
}
else
{
foreach (var item in helper.ToComponent().HtmlAttributes)
{
if (item.Key == "data-labelmsg" && item.Value != null)
labelMsg = item.Value.ToString();
}
}
return new MvcHtmlString(RenderWithName(labelMsg, helper, labelWidth, inputWidth, allSizeSame, tooltipResource, customClass: customClass, tooltipOnControl: tooltipOnControl));
}
public static MvcHtmlString RenderWithName(this TextBoxBuilder<string> helper, string label, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null, bool tooltipOnControl = false)
{
return new MvcHtmlString(RenderWithName(label, helper, labelWidth, inputWidth, allSizeSame, tooltipResource, tooltipOnControl: tooltipOnControl));
}
public static MvcHtmlString RenderWithBottomInfoAndName(this TextBoxBuilder<string> helper, string label, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null, string bottomInfo = null, bool tooltipOnControl = false)
{
string labelMsg = label;
if (labelMsg == null)
{
foreach (var item in helper.ToComponent().HtmlAttributes)
{
if (item.Key == "data-labelmsg" && item.Value != null)
labelMsg = item.Value.ToString();
}
}
return new MvcHtmlString(RenderWithName(labelMsg, helper, labelWidth, inputWidth, allSizeSame, tooltipResource, bottomInfo, tooltipOnControl: tooltipOnControl));
}
public static MvcHtmlString RenderWithoutName(this TextBoxBuilder<string> helper, int inputWidth = 6, string customClass = null)
{
if (customClass == null)
customClass = string.Empty;
var sb = new StringBuilder();
sb.AppendFormat("<div class=\"{0} {1}\">", BootsrapHelper.GetSizeClasses(inputWidth, false), customClass);
sb.Append(helper.ToHtmlString());
sb.Append("</div>");
return new MvcHtmlString(sb.ToString());
}
private static string RenderWithName(string label, TextBoxBuilder<string> helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null, string bottomInfo = null, string customClass = null, bool tooltipOnControl = false)
{
var sb = new StringBuilder();
if (customClass == null)
customClass = string.Empty;
if (string.IsNullOrWhiteSpace(tooltipResource))
AddRenderWithNameBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, label, customClass);
else
{
if (!tooltipOnControl)
{
AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, label, customClass, tooltipResource);
}
else
{
AddRenderWithNameTooltipOnControlBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, label, customClass, tooltipResource);
}
}
sb.Append(helper.ToHtmlString());
if (!string.IsNullOrWhiteSpace(bottomInfo))
{
AddRenderWithNameBottomInfo(sb, bottomInfo);
}
AddRenderWithNameCloseingFrame(sb);
return sb.ToString();
}
private static void AddRenderWithNameBottomInfo(StringBuilder sb, string bottomInfo)
{
sb.AppendFormat("<div style=\"text-align: right; margin: -4px 0 4px 0; font-size: 70%;\"><span>{0}</span></div>", bottomInfo);
}
private static void AddRenderWithNameBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string customClass)
{
sb.AppendFormat("<div class=\"{0} {1} \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}</label>", controlName, labelMsg);
sb.AppendFormat("</div><div class=\"{0} {1}\">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass);
}
private static void AddRenderWithNameTooltipBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string customClass, string tooltipResource)
{
sb.AppendFormat("<div class=\"{0} {1} kretaLabelTooltip \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}", controlName, labelMsg);
sb.Append("&nbsp;<img class='kretaLabelTooltipImg' />");
sb.AppendFormat("<span class=\"kretaLabelTooltipText\">{0}</span>", tooltipResource);
sb.Append("</label>");
sb.AppendFormat("</div><div class=\"{0} {1}\">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass);
}
private static void AddRenderWithNameTooltipOnControlBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string customClass, string tooltipResource)
{
sb.AppendFormat("<div class=\"{0} {1} kretaLabelTooltip \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}", controlName, labelMsg);
sb.Append("</label>");
sb.AppendFormat("</div><div class=\"{0} {1} kretaLabelTooltip\">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass);
sb.AppendFormat("<span class=\"kretaLabelTooltipText\">{0}</span>", tooltipResource);
}
private static void AddRenderWithNameCloseingFrame(StringBuilder sb)
{
sb.Append("</div>");
}
}
}