This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,113 @@
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<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> 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<TModel, TResult>(this HtmlHelper<TModel> html, Expression<Func<TModel, TResult>> 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<TModel, TResult>(this HtmlHelper<TModel> html, Expression<Func<TModel, TResult>> expression, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string id = "", bool formatted = false, IDictionary<string, object> htmlAttributes = null)
{
return KretaLabelForInner(html, expression, labelWidth, inputWidth, allSizeSame, false, id, formatted, htmlAttributes);
}
private static MvcHtmlString KretaLabelForInner<TModel, TResult>(this HtmlHelper<TModel> html, Expression<Func<TModel, TResult>> expression, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, bool withName = true, string id = "", bool formatted = false, IDictionary<string, object> 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("<div class=\"{0}\"><label class='windowInputLabel' for='{1}'>{2}</label></div>", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), fullHtmlFieldId, labelText);
}
string innerValue = value.ToString();
if (formatted)
{
innerValue = string.Format("<div>{0}</div>", 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("<div class=\"{0}\">{1}</div>", 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("<label");
helperAsString = helperAsString.Insert(labelOpenIndex, "<div class=\"kretaLabelTooltip kretaLabelTooltipForLabel\">");
int labelCloseIndex = helperAsString.IndexOf("</label>");
helperAsString = helperAsString.Insert(labelCloseIndex, $@" &nbsp;<img class=""kretaLabelTooltipImg""/> <span class=""kretaLabelTooltipText""> {tooltipResource} </span> ");
int labelCloseAfterIndex = helperAsString.IndexOf("</label>") + "</label>".Length;
helperAsString = helperAsString.Insert(labelCloseAfterIndex, "</div>");
return new MvcHtmlString(helperAsString);
}
}
}