kreta/KretaWeb/Helpers/ColorPickerExtensions.cs
2024-03-13 00:33:46 +01:00

106 lines
5 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 ColorPickerExtensions
{
public static ColorPickerBuilder KretaColorPicker(this HtmlHelper helper, string name, bool enabled = true, Dictionary<string, object> htmlAttributes = null)
{
var cp = helper.Kendo().ColorPicker()
.Name(name)
.Enable(enabled);
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
cp.HtmlAttributes(htmlAttributes);
return cp;
}
public static ColorPickerBuilder KretaColorPickerFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, Dictionary<string, object> 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<string, object>();
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("<div class=\"{0} {1} \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}</label>", controlName, labelMsg);
sb.AppendFormat("</div><div class=\"{0} {1}\">", 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("<div class=\"{0} {1} kretaLabelTooltip \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}", controlName, labelMsg);
sb.Append("&nbsp;<img class='kretaLabelTooltipImg' />");
sb.AppendFormat("<span class=\"kretaLabelTooltipText\">{0}</span>", tooltipResource);
sb.Append("</label>");
sb.AppendFormat("</div><div class=\"{0} {1}\">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass);
}
private static void AddRenderWithNameCloseingFrame(StringBuilder sb)
{
sb.Append("</div>");
}
}
}