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 ColorPickerExtensions { public static ColorPickerBuilder KretaColorPicker(this HtmlHelper helper, string name, bool enabled = true, Dictionary htmlAttributes = null) { var cp = helper.Kendo().ColorPicker() .Name(name) .Enable(enabled); if (htmlAttributes == null) { htmlAttributes = new Dictionary(); } cp.HtmlAttributes(htmlAttributes); return cp; } public static ColorPickerBuilder KretaColorPickerFor(this HtmlHelper helper, Expression> expression, Dictionary htmlAttributes = null) { var fieldName = ExpressionHelper.GetExpressionText(expression); var fullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName); var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData); var value = metadata.Model; if (htmlAttributes == null) htmlAttributes = new Dictionary(); htmlAttributes.Add("data-labelmsg", metadata.DisplayName); var cp = helper.Kendo().ColorPicker() .Name(fullBindingName) .HtmlAttributes(htmlAttributes); if (value != null) { cp.Value(value.ToString()); } return cp; } public static MvcHtmlString RenderWithName(this ColorPickerBuilder helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null) { 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, allSizeSame, helper.ToComponent().Name, labelMsg, string.Empty); else AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, labelMsg, string.Empty, tooltipResource); sb.Append(helper.ToHtmlString()); AddRenderWithNameCloseingFrame(sb); return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderWithName(this ColorPickerBuilder helper, string label, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null) { var sb = new StringBuilder(); if (string.IsNullOrWhiteSpace(tooltipResource)) AddRenderWithNameBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, label, string.Empty); else AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, label, string.Empty, tooltipResource); sb.Append(helper.ToHtmlString()); AddRenderWithNameCloseingFrame(sb); return new MvcHtmlString(sb.ToString()); } private static void AddRenderWithNameBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string customClass) { sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); sb.AppendFormat("", controlName, labelMsg); sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass); } private static void AddRenderWithNameTooltipBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string customClass, string tooltipResource) { sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); sb.AppendFormat(""); sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass); } private static void AddRenderWithNameCloseingFrame(StringBuilder sb) { sb.Append("
"); } } }