196 lines
9.5 KiB
C#
196 lines
9.5 KiB
C#
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<SelectListItem> 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("<li>");
|
|
stringBuilder.Append($"<input type=\"radio\" name=\"{id}\" value =\"{HttpUtility.HtmlEncode(item.Value)}\" id =\"{generatedId}\" class=\"k-radio\" {(item.Selected ? "checked=\"checked\"" : string.Empty)} />");
|
|
stringBuilder.Append($"<label for= \"{generatedId}\" class=\"k-radio-label\">{HttpUtility.HtmlEncode(item.Text)}</label>");
|
|
stringBuilder.Append("</li><br />");
|
|
|
|
ulTag.InnerHtml += stringBuilder.ToString();
|
|
}
|
|
|
|
return new MvcHtmlString(ulTag.ToString());
|
|
}
|
|
|
|
public static MvcHtmlString KretaRadioButtonListFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression, List<SelectListItem> 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<string, object>();
|
|
}
|
|
|
|
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<string, object>)htmlAttributes).Add("data-rule-required", "true");
|
|
((Dictionary<string, object>)htmlAttributes).Add("data-msg-required", attributeValue);
|
|
((Dictionary<string, object>)htmlAttributes).Add("title", attributeValue);
|
|
break;
|
|
}
|
|
}
|
|
|
|
validationAttributes = string.Join(" ", ((Dictionary<string, object>)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}>", $"</{enencodeHtmlTag}>");
|
|
}
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
stringBuilder.Append("<li>");
|
|
stringBuilder.Append($"<input type=\"radio\" name=\"{propertyName}\" value =\"{HttpUtility.HtmlEncode(item.Value)}\" id =\"{generatedId}\" class=\"k-radio\"{onchangeAttribute} {(item.Selected ? "checked=\"checked\"" : string.Empty)} {validationAttributes}/>");
|
|
stringBuilder.Append($"<label for= \"{generatedId}\" class=\"k-radio-label\">{encodedTextWithTags}</label>");
|
|
stringBuilder.Append("</li>");
|
|
if (!isHorizontal)
|
|
{
|
|
stringBuilder.Append("<br />");
|
|
}
|
|
|
|
ulTag.InnerHtml += stringBuilder.ToString();
|
|
}
|
|
|
|
return new MvcHtmlString(ulTag.ToString());
|
|
}
|
|
|
|
public static RadioButtonBuilder KretaRadioButtonNyomtatvany(this HtmlHelper helper, string id, IDictionary<string, object> 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("<div class=\"{0}\" style=\"padding:5px 0px;\">", BootsrapHelper.GetSizeClasses(12));
|
|
}
|
|
sb.AppendFormat("<div class=\"{0} {1} \">", BootsrapHelper.GetSizeClasses(labelWidth), customClass);
|
|
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}</label>", controlName, labelMsg);
|
|
sb.AppendFormat("</div><div class=\"{0} {1}\">", 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("<div class=\"{0}\" style=\"padding:5px 0px;\">", BootsrapHelper.GetSizeClasses(12));
|
|
}
|
|
sb.AppendFormat("<div class=\"{0} {1} kretaLabelTooltip \">", BootsrapHelper.GetSizeClasses(labelWidth), customClass);
|
|
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}", controlName, labelMsg);
|
|
sb.Append(" <img class='kretaLabelTooltipImg' />");
|
|
sb.AppendFormat("<span class=\"kretaLabelTooltipText\">{0}</span>", tooltipResource);
|
|
sb.Append("</label>");
|
|
sb.AppendFormat("</div><div class=\"{0} {1}\">", BootsrapHelper.GetSizeClasses(inputWidth), customClass);
|
|
}
|
|
|
|
private static void AddRenderWithNameCloseingFrame(StringBuilder sb, bool needsWrapperDiv)
|
|
{
|
|
sb.Append("</div>");
|
|
|
|
if (needsWrapperDiv)
|
|
{
|
|
sb.Append("</div>");
|
|
}
|
|
}
|
|
}
|
|
}
|