This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,313 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using Kendo.Mvc.UI;
using Kendo.Mvc.UI.Fluent;
using Kreta.Framework;
namespace Kreta.Web.Helpers
{
public static class DropdownListExtensions
{
public static DropDownListBuilder KretaDropdownList(this HtmlHelper helper, string id, string url = null
, string dataTextField = "Text", string datavalueField = "Value", string groupName = null, Type groupType = null)
{
if (groupName != null)
{
var dropdownlist = helper.Kendo().DropDownList()
.Name(id)
.DataTextField(dataTextField)
.DataValueField(datavalueField)
.DataSource(source =>
{
source.Custom()
.ServerFiltering(true)
.Transport(transport =>
{
transport.Read(
read => read.Url(url));
})
.Group(g => g.Add(groupName, groupType));
});
return dropdownlist;
}
else
{
var dropdownlist = helper.Kendo().DropDownList()
.Name(id)
.DataTextField(dataTextField)
.DataValueField(datavalueField)
.DataSource(source =>
{
source.Custom()
.ServerFiltering(true)
.Transport(transport =>
{
transport.Read(
read => read.Url(url));
});
});
return dropdownlist;
}
}
public static DropDownListBuilder KretaDropdownList(this HtmlHelper helper, string id, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes = null)
{
if (htmlAttributes == null)
htmlAttributes = new Dictionary<string, object>();
htmlAttributes.Add("data-labelmsg", id);
var dropdown = helper.Kendo().DropDownList()
.Name(id)
.BindTo(selectList)
.HtmlAttributes(htmlAttributes);
return dropdown;
}
public static DropDownListBuilder KretaDropdownListFor<TModel, TProperty>(
this HtmlHelper<TModel> helper,
Expression<Func<TModel, TProperty>> expression,
IEnumerable<SelectListItem> selectList,
IDictionary<string, object> htmlAttributes = null,
bool addOptionLabel = true
)
{
var fieldName = ExpressionHelper.GetExpressionText(expression);
var fullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fieldName);
var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
if (htmlAttributes == null)
htmlAttributes = new Dictionary<string, object>();
var validationAttributes = helper.GetUnobtrusiveValidationAttributes(fullBindingName, metadata);
foreach (var key in validationAttributes.Keys)
{
if (key == "data-val-required")
{
htmlAttributes.Add("title", validationAttributes[key].ToString());
}
}
if (metadata.IsRequired)
{
htmlAttributes.Add("data-rule-required", "true");
htmlAttributes.Add("data-labelmsg", metadata.DisplayName + " *");
}
else
{
htmlAttributes.Add("data-labelmsg", metadata.DisplayName);
}
selectList = selectList.Where(t => !string.IsNullOrWhiteSpace(t.Value)).ToList();
var dropdown = helper.Kendo()
.DropDownListFor(expression)
.BindTo(selectList)
.HtmlAttributes(htmlAttributes);
if (addOptionLabel && selectList.Count() > 1)
{
dropdown.OptionLabel(StringResourcesUtil.GetString(364));
}
return dropdown;
}
public static DropDownListBuilder KretaDropdownListFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string url, string dataTextField = "Text", string datavalueField = "Value", IDictionary<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>();
var validationAttributes = helper.GetUnobtrusiveValidationAttributes(fullBindingName, metadata);
foreach (var key in validationAttributes.Keys)
{
if (key == "data-val-required")
{
htmlAttributes.Add("title", validationAttributes[key].ToString());
}
}
if (metadata.IsRequired)
{
htmlAttributes.Add("data-rule-required", "true");
//htmlAttributes.Add("title", string.Format(StringResourcesUtil.GetString(3477), metadata.DisplayName));
htmlAttributes.Add("data-labelmsg", metadata.DisplayName + " *");
}
else
{ htmlAttributes.Add("data-labelmsg", metadata.DisplayName); }
var dropdown = helper.Kendo()
.DropDownListFor(expression)
.HtmlAttributes(htmlAttributes)
.DataTextField(dataTextField)
.DataValueField(datavalueField)
.DataSource(source =>
{
source.Custom()
.ServerFiltering(true)
.Transport(transport =>
{
transport.Read(
read => read.Url(url));
}).Group(g => g.Add("Group", typeof(string)));
});
return dropdown;
}
public static MvcHtmlString RenderSearchPanel(this DropDownListBuilder helper)
{
List<SelectListItem> modSelectList = new List<SelectListItem>();
modSelectList.Add(new SelectListItem() { Text = StringResourcesUtil.GetString(364), Value = "" });
foreach (var item in helper.ToComponent().DataSource.Data)
{
var dli = (DropDownListItem)item;
modSelectList.Add(new SelectListItem() { Text = dli.Text, Value = dli.Value, Selected = dli.Selected });
}
if (modSelectList.Count == 2)
helper.BindTo(modSelectList);
string labelMsg = "";
foreach (var item in helper.ToComponent().HtmlAttributes)
{
if (item.Key == "data-labelmsg" && item.Value != null)
labelMsg = item.Value.ToString();
}
StringBuilder sb = new StringBuilder();
sb.Append("<div class=\"searchInputRowHeight\"><div>");
sb.Append("<label class=\"searchPanelInputLabel\" for=\"").Append(helper.ToComponent().Name).Append("\">").Append(labelMsg).Append("</label>");
sb.Append("</div><div>");
sb.Append(helper.ToHtmlString());
sb.Append("</div></div>");
return new MvcHtmlString(sb.ToString());
}
public static MvcHtmlString RenderSearchPanelSideBar(this DropDownListBuilder helper)
{
List<SelectListItem> modSelectList = new List<SelectListItem>();
modSelectList.Add(new SelectListItem() { Text = StringResourcesUtil.GetString(364), Value = "" });
foreach (var item in helper.ToComponent().DataSource.Data)
{
var dli = (DropDownListItem)item;
modSelectList.Add(new SelectListItem() { Text = dli.Text, Value = dli.Value, Selected = dli.Selected });
}
if (modSelectList.Count == 2)
helper.BindTo(modSelectList);
string labelMsg = "";
foreach (var item in helper.ToComponent().HtmlAttributes)
{
if (item.Key == "data-labelmsg" && item.Value != null)
labelMsg = item.Value.ToString();
}
StringBuilder sb = new StringBuilder();
sb.Append("<div class=\"searchPanelRow\">");
sb.Append("<div class=\"searchPanelRowTitle\">");
sb.Append("<label class=\"searchPanelLabel\" for=\"").Append(helper.ToComponent().Name).Append("\">").Append(labelMsg).Append("</label>");
sb.Append("</div>");
sb.Append("<div class=\"searchPanelRowValue\">");
sb.Append(helper.ToHtmlString());
sb.Append("</div>");
sb.Append("</div>");
return new MvcHtmlString(sb.ToString());
}
public static MvcHtmlString RenderWithName(this DropDownListBuilder 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);
else
AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, labelMsg, 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)
{
sb.AppendFormat("<div class=\"{0}\">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame));
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}</label>", controlName, labelMsg);
sb.AppendFormat("</div><div class=\"{0} \">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame));
}
private static void AddRenderWithNameTooltipBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string tooltipResource)
{
sb.AppendFormat("<div class=\"{0} kretaLabelTooltip \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame));
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} \">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame));
}
private static void AddRenderWithNameCloseingFrame(StringBuilder sb)
{
sb.Append("</div>");
}
public static MvcHtmlString RenderWithName(this DropDownListBuilder 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);
else
AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, label, tooltipResource);
sb.Append(helper.ToHtmlString());
AddRenderWithNameCloseingFrame(sb);
return new MvcHtmlString(sb.ToString());
}
public static MvcHtmlString RenderWithoutName(this DropDownListBuilder helper, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null)
{
var sb = new StringBuilder();
sb.AppendFormat($"<div class=\"{{0}} {(!string.IsNullOrWhiteSpace(tooltipResource) ? "kretaLabelTooltip" : "")}\">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame));
if (!string.IsNullOrWhiteSpace(tooltipResource))
{
helper.HtmlAttributes(new { style = "width: 94% !important;" });
}
sb.Append(helper.ToHtmlString());
if (!string.IsNullOrWhiteSpace(tooltipResource))
{
sb.Append("&nbsp;<img class='kretaLabelTooltipImg' />");
sb.AppendFormat("<span class=\"kretaLabelTooltipText\">{0}</span>", tooltipResource);
}
sb.Append("</div>");
return new MvcHtmlString(sb.ToString());
}
}
}