using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; using System.Web.Mvc; using Kendo.Mvc.UI; using Kendo.Mvc.UI.Fluent; namespace Kreta.Web.Helpers { public static class CheckBoxExtensions { public static CheckBoxBuilder KretaCheckBox(this HtmlHelper helper, string name, string label, bool check, bool enabled = true, IDictionary htmlAttributes = null) { CheckBoxBuilder checkBox = helper.Kendo().CheckBox() .Name(name) .Label(label) .Checked(check) .Enable(enabled); if (htmlAttributes != null) { checkBox.HtmlAttributes(htmlAttributes); } return checkBox; } public static CheckBoxBuilder KretaCheckBoxFor(this HtmlHelper helper, Expression> expression, IDictionary htmlAttributes = null, bool renderLabelToRight = false, string customDisplayName = null) { ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } var displayName = !string.IsNullOrWhiteSpace(customDisplayName) ? customDisplayName : metadata.DisplayName; if (!renderLabelToRight) { htmlAttributes.Add("data-labelmsg", displayName); } else { //NOTE: Erre azért van szükség, mert a körülötte lévő div nem veszi fel a label méretét, mert fix 5px-re van állítva és emiatt belecsúszik az alatta lévő! if (htmlAttributes.ContainsKey("class")) { htmlAttributes["class"] += " disableCheckBoxFixHeight"; } else { htmlAttributes.Add("class", "disableCheckBoxFixHeight"); } } switch (expression.ReturnType.Name) { case "Boolean": CheckBoxBuilder checkbox = helper.Kendo() .CheckBoxFor(Expression.Lambda>(expression.Body, expression.Parameters)) .Label(renderLabelToRight ? displayName : string.Empty) /*Ne generáljon ki label-t az input mögé, ha balra akarjuk a szöveget, azt a RenderWithName-el kell*/ .HtmlAttributes(htmlAttributes); return checkbox; default: CheckBoxBuilder nullableCheckbox = helper.Kendo() .CheckBoxFor(Expression.Lambda>(expression.Body, expression.Parameters)) .Label(renderLabelToRight ? displayName : string.Empty) /*Ne generáljon ki label-t az input mögé, ha balra akarjuk a szöveget, azt a RenderWithName-el kell*/ .HtmlAttributes(htmlAttributes); return nullableCheckbox; } } public static MvcHtmlString RenderSearchPanel(this CheckBoxBuilder helper) { string labelMsg = ""; foreach (KeyValuePair item in helper.ToComponent().HtmlAttributes) { if (item.Key == "data-labelmsg" && item.Value != null) { labelMsg = item.Value.ToString(); } } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("
"); stringBuilder.Append(""); stringBuilder.Append("
"); stringBuilder.Append(helper.ToHtmlString()); stringBuilder.Append("
"); return new MvcHtmlString(stringBuilder.ToString()); } public static MvcHtmlString RenderSearchPanelSideBar(this CheckBoxBuilder helper) { string labelMsg = ""; foreach (KeyValuePair item in helper.ToComponent().HtmlAttributes) { if (item.Key == "data-labelmsg" && item.Value != null) { labelMsg = item.Value.ToString(); } } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("
"); stringBuilder.Append("
"); stringBuilder.Append(""); stringBuilder.Append("
"); stringBuilder.Append("
"); stringBuilder.Append(helper.ToHtmlString()); stringBuilder.Append("
"); stringBuilder.Append("
"); return new MvcHtmlString(stringBuilder.ToString()); } public static MvcHtmlString RenderWithName(this CheckBoxBuilder helper, int labelWidth = 6, int inputWidth = 6, string customClass = "", string tooltipResource = null, string labelMsg = null, bool allSizeSame = false) { if (string.IsNullOrWhiteSpace(labelMsg)) { foreach (KeyValuePair item in helper.ToComponent().HtmlAttributes) { if (item.Key == "data-labelmsg" && item.Value != null) { labelMsg = item.Value.ToString(); } } } var stringBuilder = new StringBuilder(); if (string.IsNullOrWhiteSpace(tooltipResource)) { AddRenderWithNameBeginingFrame(stringBuilder, labelWidth, inputWidth, helper.ToComponent().Name, labelMsg, customClass, allSizeSame); } else { AddRenderWithNameTooltipBeginingFrame(stringBuilder, labelWidth, inputWidth, helper.ToComponent().Name, labelMsg, customClass, tooltipResource, allSizeSame); } stringBuilder.Append(helper.ToHtmlString()); AddRenderWithNameCloseingFrame(stringBuilder); return new MvcHtmlString(stringBuilder.ToString()); } public static MvcHtmlString RenderWithName(this CheckBoxBuilder helper, string label, int labelWidth = 6, int inputWidth = 6, string tooltipResource = null) { var stringBuilder = new StringBuilder(); if (string.IsNullOrWhiteSpace(tooltipResource)) { AddRenderWithNameBeginingFrame(stringBuilder, labelWidth, inputWidth, helper.ToComponent().Name, label, string.Empty, false); } else { AddRenderWithNameTooltipBeginingFrame(stringBuilder, labelWidth, inputWidth, helper.ToComponent().Name, label, string.Empty, tooltipResource, false); } stringBuilder.Append(helper.ToHtmlString()); AddRenderWithNameCloseingFrame(stringBuilder); return new MvcHtmlString(stringBuilder.ToString()); } private static void AddRenderWithNameBeginingFrame(StringBuilder stringBuilder, int labelWidth, int inputWidth, string controlName, string labelMsg, string customClass, bool allSizeSame) { stringBuilder.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); stringBuilder.AppendFormat("", controlName, labelMsg); stringBuilder.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass); } private static void AddRenderWithNameTooltipBeginingFrame(StringBuilder stringBuilder, int labelWidth, int inputWidth, string controlName, string labelMsg, string customClass, string tooltipResource, bool allSizeSame) { stringBuilder.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); stringBuilder.AppendFormat(""); stringBuilder.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass); } private static void AddRenderWithNameCloseingFrame(StringBuilder stringBuilder) { stringBuilder.Append("
"); } } }