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 KretaTextBox(this HtmlHelper helper, string name, bool enabled = true, Dictionary htmlAttributes = null) { var tb = helper.Kendo().TextBox() //.Name(Guid.NewGuid().ToString()) .Name(name) .Enable(enabled); if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } htmlAttributes.Add("type", "text"); htmlAttributes.Add("name", name); tb.HtmlAttributes(htmlAttributes); return tb; } public static TextBoxBuilder KretaTextBoxFor(this HtmlHelper helper, Expression> expression, Dictionary 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(); 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().FirstOrDefault(); if (maxLengthAttribute != null) { maxLength = maxLengthAttribute.Length; } else { StringLengthAttribute stringLengthAttribute = me.Member.GetCustomAttributes(typeof(StringLengthAttribute), false).Cast().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 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("
"); sb.Append(""); sb.Append("
"); sb.Append(helper.ToHtmlString()); sb.Append("
"); return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderSearchPanelSideBar(this TextBoxBuilder 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("
"); sb.Append("
"); sb.Append(""); sb.Append("
"); sb.Append("
"); sb.Append(helper.ToHtmlString()); sb.Append("
"); sb.Append("
"); return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderWithName(this TextBoxBuilder 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 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 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 helper, int inputWidth = 6, string customClass = null) { if (customClass == null) customClass = string.Empty; var sb = new StringBuilder(); sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, false), customClass); sb.Append(helper.ToHtmlString()); sb.Append("
"); return new MvcHtmlString(sb.ToString()); } private static string RenderWithName(string label, TextBoxBuilder 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("
{0}
", bottomInfo); } private static void AddRenderWithNameBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string customClass) { sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); sb.AppendFormat("", controlName, labelMsg); sb.AppendFormat("
", 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("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); sb.AppendFormat(""); sb.AppendFormat("
", 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("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); sb.AppendFormat(""); sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass); sb.AppendFormat("{0}", tooltipResource); } private static void AddRenderWithNameCloseingFrame(StringBuilder sb) { sb.Append("
"); } } }