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 selectList, IDictionary htmlAttributes = null) { if (htmlAttributes == null) htmlAttributes = new Dictionary(); htmlAttributes.Add("data-labelmsg", id); var dropdown = helper.Kendo().DropDownList() .Name(id) .BindTo(selectList) .HtmlAttributes(htmlAttributes); return dropdown; } public static DropDownListBuilder KretaDropdownListFor( this HtmlHelper helper, Expression> expression, IEnumerable selectList, IDictionary 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(); 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(this HtmlHelper helper, Expression> expression, string url, string dataTextField = "Text", string datavalueField = "Value", IDictionary 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(); 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 modSelectList = new List(); 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("
"); sb.Append(""); sb.Append("
"); sb.Append(helper.ToHtmlString()); sb.Append("
"); return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderSearchPanelSideBar(this DropDownListBuilder helper) { List modSelectList = new List(); 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("
"); sb.Append("
"); sb.Append(""); sb.Append("
"); sb.Append("
"); sb.Append(helper.ToHtmlString()); sb.Append("
"); sb.Append("
"); 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("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame)); sb.AppendFormat("", controlName, labelMsg); sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame)); } private static void AddRenderWithNameTooltipBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string tooltipResource) { sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame)); sb.AppendFormat(""); sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame)); } private static void AddRenderWithNameCloseingFrame(StringBuilder sb) { sb.Append("
"); } 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($"
", 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(" "); sb.AppendFormat("{0}", tooltipResource); } sb.Append("
"); return new MvcHtmlString(sb.ToString()); } } }