using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Mvc.Html; namespace Kreta.Web.Helpers { public static class LabelExtensions { public static MvcHtmlString KretaInputLabelFor(this HtmlHelper html, Expression> expression, IDictionary htmlAttributes = null) { var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName); string fullHtmlFieldId = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName); string text = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last(); if (string.IsNullOrWhiteSpace(text)) { return MvcHtmlString.Empty; } var validationAttributes = html.GetUnobtrusiveValidationAttributes(fullHtmlFieldName, metadata); bool isRequiredIf = RequiredIfReflector.RequiredIf(html, validationAttributes); if (metadata.IsRequired || isRequiredIf) { text += " *"; } var tag = new TagBuilder("label"); tag.MergeAttributes(htmlAttributes); tag.MergeAttribute("for", fullHtmlFieldId); tag.AddCssClass("windowInputLabel"); tag.SetInnerText(text); return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal)); } public static MvcHtmlString KretaLabelFor(this HtmlHelper html, Expression> expression, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string id = "", bool formatted = false, string labelText = null) { return KretaLabelForInner(html, expression, labelWidth, inputWidth, allSizeSame, true, id, formatted, labelText: labelText); } public static MvcHtmlString KretaLabelForWithoutName(this HtmlHelper html, Expression> expression, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string id = "", bool formatted = false, IDictionary htmlAttributes = null) { return KretaLabelForInner(html, expression, labelWidth, inputWidth, allSizeSame, false, id, formatted, htmlAttributes); } private static MvcHtmlString KretaLabelForInner(this HtmlHelper html, Expression> expression, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, bool withName = true, string id = "", bool formatted = false, IDictionary htmlAttributes = null, string labelText = null) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); string htmlFieldName = ExpressionHelper.GetExpressionText(expression); string fullHtmlFieldId = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(htmlFieldName); if (string.IsNullOrWhiteSpace(labelText)) { labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.')[htmlFieldName.Split('.').Length - 1]; } MvcHtmlString value = html.DisplayTextFor(expression); StringBuilder sb = new StringBuilder(); if (withName) { sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), fullHtmlFieldId, labelText); } string innerValue = value.ToString(); if (formatted) { innerValue = string.Format("
{0}
", HttpUtility.HtmlDecode(value.ToString())); } var tag = new TagBuilder("label"); if (htmlAttributes != null) { tag.MergeAttributes(htmlAttributes); } tag.MergeAttribute("displayfor", fullHtmlFieldId); tag.InnerHtml = innerValue; if (string.IsNullOrWhiteSpace(id)) { tag.AddCssClass("windowInputLabel"); } else { tag.AddCssClass("windowInputValue"); tag.Attributes.Add("id", id); } sb.AppendFormat("
{1}
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), tag.ToString(TagRenderMode.Normal)); return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderWithTooltip(this MvcHtmlString helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string customLabelDivClass = "", string customLabelClass = "", string tooltipResource = null) { string helperAsString = helper.ToString(); int labelOpenIndex = helperAsString.IndexOf(""); int labelCloseIndex = helperAsString.IndexOf(""); helperAsString = helperAsString.Insert(labelCloseIndex, $@"   {tooltipResource} "); int labelCloseAfterIndex = helperAsString.IndexOf("") + "".Length; helperAsString = helperAsString.Insert(labelCloseAfterIndex, ""); return new MvcHtmlString(helperAsString); } } }