188 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			188 lines
		
	
	
		
			8.9 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
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<string, object> 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<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes = null, bool renderLabelToRight = false, string customDisplayName = null)
 | 
						|
        {
 | 
						|
            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
 | 
						|
 | 
						|
            if (htmlAttributes == null)
 | 
						|
            {
 | 
						|
                htmlAttributes = new Dictionary<string, object>();
 | 
						|
            }
 | 
						|
 | 
						|
            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<Func<TModel, bool>>(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<Func<TModel, bool?>>(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<string, object> item in helper.ToComponent().HtmlAttributes)
 | 
						|
            {
 | 
						|
                if (item.Key == "data-labelmsg" && item.Value != null)
 | 
						|
                {
 | 
						|
                    labelMsg = item.Value.ToString();
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            StringBuilder stringBuilder = new StringBuilder();
 | 
						|
            stringBuilder.Append("<div class=\"searchInputRowHeight\"><div>");
 | 
						|
            stringBuilder.Append("<label class=\"searchPanelInputLabel\" for=\"").Append(helper.ToComponent().Name).Append("\">").Append(labelMsg).Append("</label>");
 | 
						|
            stringBuilder.Append("</div><div>");
 | 
						|
            stringBuilder.Append(helper.ToHtmlString());
 | 
						|
            stringBuilder.Append("</div></div>");
 | 
						|
 | 
						|
            return new MvcHtmlString(stringBuilder.ToString());
 | 
						|
        }
 | 
						|
 | 
						|
        public static MvcHtmlString RenderSearchPanelSideBar(this CheckBoxBuilder helper)
 | 
						|
        {
 | 
						|
            string labelMsg = "";
 | 
						|
            foreach (KeyValuePair<string, object> item in helper.ToComponent().HtmlAttributes)
 | 
						|
            {
 | 
						|
                if (item.Key == "data-labelmsg" && item.Value != null)
 | 
						|
                {
 | 
						|
                    labelMsg = item.Value.ToString();
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            StringBuilder stringBuilder = new StringBuilder();
 | 
						|
            stringBuilder.Append("<div class=\"searchPanelRow\">");
 | 
						|
            stringBuilder.Append("<div class=\"searchPanelRowTitle\">");
 | 
						|
            stringBuilder.Append("<label class=\"searchPanelLabel\" for=\"").Append(helper.ToComponent().Name).Append("\">").Append(labelMsg).Append("</label>");
 | 
						|
            stringBuilder.Append("</div>");
 | 
						|
            stringBuilder.Append("<div class=\"searchPanelRowValue\">");
 | 
						|
            stringBuilder.Append(helper.ToHtmlString());
 | 
						|
            stringBuilder.Append("</div>");
 | 
						|
            stringBuilder.Append("</div>");
 | 
						|
 | 
						|
            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<string, object> 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("<div class=\"{0} {1} \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
 | 
						|
            stringBuilder.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}</label>", controlName, labelMsg);
 | 
						|
            stringBuilder.AppendFormat("</div><div class=\"{0} {1}\">", 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("<div class=\"{0} {1} kretaLabelTooltip \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
 | 
						|
            stringBuilder.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}", controlName, labelMsg);
 | 
						|
            stringBuilder.Append(" <img class='kretaLabelTooltipImg' />");
 | 
						|
            stringBuilder.AppendFormat("<span class=\"kretaLabelTooltipText\">{0}</span>", tooltipResource);
 | 
						|
            stringBuilder.Append("</label>");
 | 
						|
            stringBuilder.AppendFormat("</div><div class=\"{0} {1}\">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass);
 | 
						|
        }
 | 
						|
 | 
						|
        private static void AddRenderWithNameCloseingFrame(StringBuilder stringBuilder)
 | 
						|
        {
 | 
						|
            stringBuilder.Append("</div>");
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |