using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Web; using System.Web.Mvc; using Kendo.Mvc.UI; using Kendo.Mvc.UI.Fluent; namespace Kreta.Web.Helpers { public static class RadioButtonListExtensions { public static MvcHtmlString KretaRadioButtonList(this HtmlHelper helper, string id, List items, object htmlAttributes = null) { TagBuilder ulTag = new TagBuilder("ul"); ulTag.AddCssClass("noUlLiButton"); int index = 0; foreach (SelectListItem item in items) { index++; string generatedId = id + index; StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("
  • "); stringBuilder.Append($""); stringBuilder.Append($""); stringBuilder.Append("

  • "); ulTag.InnerHtml += stringBuilder.ToString(); } return new MvcHtmlString(ulTag.ToString()); } public static MvcHtmlString KretaRadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, List items, bool isHorizontal = false, object htmlAttributes = null, string onChangeFunction = null, bool isNeedValidation = false) { var validationAttributes = ""; if (isNeedValidation) { var fieldName = ExpressionHelper.GetExpressionText(expression); var fullBindingName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName); var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); var value = metadata.Model; if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } fullBindingName = fullBindingName.Replace('.', '_'); var validationAttributesDict = htmlHelper.GetUnobtrusiveValidationAttributes(fullBindingName, metadata); foreach (var attribute in validationAttributesDict) { var attributeValue = attribute.Value.ToString(); switch (attribute.Key) { case "data-val-required": ((Dictionary)htmlAttributes).Add("data-rule-required", "true"); ((Dictionary)htmlAttributes).Add("data-msg-required", attributeValue); ((Dictionary)htmlAttributes).Add("title", attributeValue); break; } } validationAttributes = string.Join(" ", ((Dictionary)htmlAttributes).Select(x => x.Key + "=\"" + x.Value + "\"")); } TagBuilder ulTag = new TagBuilder("ul"); ulTag.AddCssClass("noUlLiButton k-widget"); if (isHorizontal) { ulTag.AddCssClass("list-inline"); } ulTag.Attributes.Add("style", "border-style: none"); int index = 0; MemberExpression body = expression.Body as MemberExpression; string propertyName = body.Member.Name; string onchangeAttribute = string.Empty; if (!string.IsNullOrWhiteSpace(onChangeFunction)) { onchangeAttribute = " onchange=\"" + onChangeFunction + ";\""; } foreach (SelectListItem item in items) { index++; string generatedId = propertyName + index; string encodedTextWithTags = HttpUtility.HtmlEncode(item.Text); foreach (string enencodeHtmlTag in Constants.UnencodeHtmlTagList) { encodedTextWithTags = encodedTextWithTags?.Replace($"<{enencodeHtmlTag}>", $"<{enencodeHtmlTag}>").Replace($"</{enencodeHtmlTag}>", $""); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("
  • "); stringBuilder.Append($""); stringBuilder.Append($""); stringBuilder.Append("
  • "); if (!isHorizontal) { stringBuilder.Append("
    "); } ulTag.InnerHtml += stringBuilder.ToString(); } return new MvcHtmlString(ulTag.ToString()); } public static RadioButtonBuilder KretaRadioButtonNyomtatvany(this HtmlHelper helper, string id, IDictionary htmlAttributes = null, object value = null, bool check = false) { var radioButton = helper.Kendo().RadioButton() .Name(id) .Value(value) .Checked(check); if (htmlAttributes != null) { radioButton.HtmlAttributes(htmlAttributes); } return radioButton; } public static MvcHtmlString RenderWithName(this RadioButtonBuilder helper, int labelWidth = 6, int inputWidth = 6, string customClass = "", string tooltipResource = null, bool includeWrapperDiv = true) { string labelMsg = ""; foreach (var item in helper.ToComponent().HtmlAttributes) { if (item.Key == "data-labelmsg" && item.Value != null) labelMsg = item.Value.ToString(); } var sb = new StringBuilder(); if (string.IsNullOrWhiteSpace(tooltipResource)) AddRenderWithNameBeginingFrame(sb, labelWidth, inputWidth, helper.ToComponent().Name, labelMsg, customClass, includeWrapperDiv); else AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, helper.ToComponent().Name, labelMsg, customClass, tooltipResource, includeWrapperDiv); sb.Append(helper.ToHtmlString()); AddRenderWithNameCloseingFrame(sb, includeWrapperDiv); return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderWithName(this RadioButtonBuilder helper, string label, int labelWidth = 6, int inputWidth = 6, string tooltipResource = null, bool includeWrapperDiv = true) { var sb = new StringBuilder(); if (string.IsNullOrWhiteSpace(tooltipResource)) AddRenderWithNameBeginingFrame(sb, labelWidth, inputWidth, helper.ToComponent().Name, label, string.Empty, includeWrapperDiv); else AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, helper.ToComponent().Name, label, string.Empty, tooltipResource, includeWrapperDiv); sb.Append(helper.ToHtmlString()); AddRenderWithNameCloseingFrame(sb, includeWrapperDiv); return new MvcHtmlString(sb.ToString()); } private static void AddRenderWithNameBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, string controlName, string labelMsg, string customClass, bool needsWrapperDiv) { if (needsWrapperDiv) { sb.AppendFormat("
    ", BootsrapHelper.GetSizeClasses(12)); } sb.AppendFormat("
    ", BootsrapHelper.GetSizeClasses(labelWidth), customClass); sb.AppendFormat("", controlName, labelMsg); sb.AppendFormat("
    ", BootsrapHelper.GetSizeClasses(inputWidth), customClass); } private static void AddRenderWithNameTooltipBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, string controlName, string labelMsg, string customClass, string tooltipResource, bool needsWrapperDiv) { if (needsWrapperDiv) { sb.AppendFormat("
    ", BootsrapHelper.GetSizeClasses(12)); } sb.AppendFormat("
    ", BootsrapHelper.GetSizeClasses(labelWidth), customClass); sb.AppendFormat(""); sb.AppendFormat("
    ", BootsrapHelper.GetSizeClasses(inputWidth), customClass); } private static void AddRenderWithNameCloseingFrame(StringBuilder sb, bool needsWrapperDiv) { sb.Append("
    "); if (needsWrapperDiv) { sb.Append("
    "); } } } }