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; using Kreta.Framework; using Kreta.Resources; namespace Kreta.Web.Helpers { public static class NumericExtensions { public static NumericTextBoxBuilder KretaNumeric(this HtmlHelper helper, string name, Dictionary htmlAttributes = null) { if (htmlAttributes == null) htmlAttributes = new Dictionary(); var numeric = helper.Kendo().NumericTextBox() .Name(name) .Min(0) .Max(Constants.General.DoubleNumericTextboxDefaultMaxValue) .HtmlAttributes(htmlAttributes); numeric.DecreaseButtonTitle(CommonResource.ErtekCsokkentese).IncreaseButtonTitle(CommonResource.ErtekNovelese); return numeric; } public static NumericTextBoxBuilder KretaNumericFor(this HtmlHelper helper, Expression> expression, 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(); //if (metadata.IsRequired) { htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } //else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } if (metadata.IsRequired) { htmlAttributes.Add("data-rule-required", "true"); htmlAttributes.Add("title", string.Format(StringResourcesUtil.GetString(3477), metadata.DisplayName)); htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } var numeric = helper.Kendo() .NumericTextBoxFor(expression) .Min(0) .Max(Constants.General.DecimalNumericTextboxDefaultMaxValue) .HtmlAttributes(htmlAttributes); if (metadata.IsRequired) numeric.Events(e => e.Change("KretaNumericHelper.setTitle")); numeric.DecreaseButtonTitle(CommonResource.ErtekCsokkentese).IncreaseButtonTitle(CommonResource.ErtekNovelese); return numeric; } public static NumericTextBoxBuilder KretaNumericFor(this HtmlHelper helper, Expression> expression, 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(); //if (metadata.IsRequired) { htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } //else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } if (metadata.IsRequired) { htmlAttributes.Add("data-rule-required", "true"); htmlAttributes.Add("title", string.Format(StringResourcesUtil.GetString(3477), metadata.DisplayName)); htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } var numeric = helper.Kendo() .NumericTextBoxFor(expression) .Min(0) .Max(Constants.General.DecimalNumericTextboxDefaultMaxValue) .HtmlAttributes(htmlAttributes); if (metadata.IsRequired) numeric.Events(e => e.Change("KretaNumericHelper.setTitle")); numeric.DecreaseButtonTitle(CommonResource.ErtekCsokkentese).IncreaseButtonTitle(CommonResource.ErtekNovelese); return numeric; } public static NumericTextBoxBuilder KretaNumericFor(this HtmlHelper helper, Expression> expression, 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(); //if (metadata.IsRequired) { htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } //else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } if (metadata.IsRequired) { htmlAttributes.Add("data-rule-required", "true"); htmlAttributes.Add("title", string.Format(StringResourcesUtil.GetString(3477), metadata.DisplayName)); htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } var numeric = helper.Kendo() .NumericTextBoxFor(expression) .Min(0) .Max(int.MaxValue) .Decimals(0) .Format("#") .HtmlAttributes(htmlAttributes); if (metadata.IsRequired) numeric.Events(e => e.Change("KretaNumericHelper.setTitle")); numeric.DecreaseButtonTitle(CommonResource.ErtekCsokkentese).IncreaseButtonTitle(CommonResource.ErtekNovelese); return numeric; } public static NumericTextBoxBuilder KretaNumericFor(this HtmlHelper helper, Expression> expression, 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(); //if (metadata.IsRequired) { htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } //else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } if (metadata.IsRequired) { htmlAttributes.Add("data-rule-required", "true"); htmlAttributes.Add("title", string.Format(StringResourcesUtil.GetString(3477), metadata.DisplayName)); htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } var numeric = helper.Kendo() .NumericTextBoxFor(expression) .Min(0) .Max(int.MaxValue) .Decimals(0) .Format("#") .HtmlAttributes(htmlAttributes); if (metadata.IsRequired) numeric.Events(e => e.Change("KretaNumericHelper.setTitle")); numeric.DecreaseButtonTitle(CommonResource.ErtekCsokkentese).IncreaseButtonTitle(CommonResource.ErtekNovelese); return numeric; } public static NumericTextBoxBuilder KretaNumericFor(this HtmlHelper helper, Expression> expression, 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(); //if (metadata.IsRequired) { htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } //else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } if (metadata.IsRequired) { htmlAttributes.Add("data-rule-required", "true"); htmlAttributes.Add("title", string.Format(StringResourcesUtil.GetString(3477), metadata.DisplayName)); htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } //if (value != null) // htmlAttributes.Add("value", value.ToString().Replace(",", ".")); var numeric = helper.Kendo() .NumericTextBoxFor(expression) .Min(0) .Max(Constants.General.DoubleNumericTextboxDefaultMaxValue) .HtmlAttributes(htmlAttributes); if (metadata.IsRequired) numeric.Events(e => e.Change("KretaNumericHelper.setTitle")); numeric.DecreaseButtonTitle(CommonResource.ErtekCsokkentese).IncreaseButtonTitle(CommonResource.ErtekNovelese); return numeric; } public static NumericTextBoxBuilder KretaNumericFor(this HtmlHelper helper, Expression> expression, IDictionary htmlAttributes = null, string label = 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(); //if (metadata.IsRequired) { htmlAttributes.Add("labelmsg", metadata.DisplayName + " *"); } //else { htmlAttributes.Add("labelmsg", metadata.DisplayName); } var displayName = string.IsNullOrWhiteSpace(label) ? metadata.DisplayName : label; if (metadata.IsRequired) { htmlAttributes.Add("data-rule-required", "true"); htmlAttributes.Add("title", string.Format(StringResourcesUtil.GetString(3477), displayName)); htmlAttributes.Add("labelmsg", displayName + " *"); } else { htmlAttributes.Add("labelmsg", displayName); } //if (value != null) // htmlAttributes.Add("value", value.ToString().Replace(",", ".")); var numeric = helper.Kendo() .NumericTextBoxFor(expression) .Min(0) .Max(Constants.General.DoubleNumericTextboxDefaultMaxValue) .HtmlAttributes(htmlAttributes); if (metadata.IsRequired) numeric.Events(e => e.Change("KretaNumericHelper.setTitle")); numeric.DecreaseButtonTitle(CommonResource.ErtekCsokkentese).IncreaseButtonTitle(CommonResource.ErtekNovelese); return numeric; } public static MvcHtmlString RenderSearchPanel(this NumericTextBoxBuilder helper) { string labelMsg = ""; foreach (var item in helper.ToComponent().HtmlAttributes) { if (item.Key == "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 RenderSearchPanel(this NumericTextBoxBuilder helper) { string labelMsg = ""; foreach (var item in helper.ToComponent().HtmlAttributes) { if (item.Key == "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 RenderSearchPanel(this NumericTextBoxBuilder helper) { string labelMsg = ""; foreach (var item in helper.ToComponent().HtmlAttributes) { if (item.Key == "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 NumericTextBoxBuilder helper) { string labelMsg = ""; foreach (var item in helper.ToComponent().HtmlAttributes) { if (item.Key == "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 RenderSearchPanelSideBar(this NumericTextBoxBuilder helper) { string labelMsg = ""; foreach (var item in helper.ToComponent().HtmlAttributes) { if (item.Key == "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 RenderSearchPanelSideBar(this NumericTextBoxBuilder helper) { string labelMsg = ""; foreach (var item in helper.ToComponent().HtmlAttributes) { if (item.Key == "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 NumericTextBoxBuilder 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 == "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); if (helper.ToComponent().Max.HasValue) { sb.Append(@""); } if (helper.ToComponent().Min.HasValue) { sb.Append(@""); } return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderWithName(this NumericTextBoxBuilder 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 == "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 NumericTextBoxBuilder helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null, bool needLabel = true) { string labelMsg = ""; foreach (var item in helper.ToComponent().HtmlAttributes) { if (item.Key == "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, needLabel); else AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, helper.ToComponent().Name, labelMsg, string.Empty, tooltipResource, needLabel); sb.Append(helper.ToHtmlString()); AddRenderWithNameCloseingFrame(sb); return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderWithName(this NumericTextBoxBuilder 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); if (helper.ToComponent().Max.HasValue) { sb.Append(@""); } if (helper.ToComponent().Min.HasValue) { sb.Append(@""); } return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString RenderWithName(this NumericTextBoxBuilder 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()); } public static MvcHtmlString RenderWithName(this NumericTextBoxBuilder 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()); } public static MvcHtmlString RenderWithoutName(this NumericTextBoxBuilder helper, int inputWidth = 6, string tooltipResource = null) { return RenderWithName(helper, 1, inputWidth, tooltipResource: tooltipResource, needLabel: false); } private static void AddRenderWithNameBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string customClass, bool needLabel = true) { if (needLabel) { sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); sb.AppendFormat("", controlName, labelMsg); sb.AppendFormat("
"); } 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, bool needLabel = true) { sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass); if (needLabel) { sb.AppendFormat(""); sb.Append("
"); } sb.AppendFormat("
", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass); } private static void AddRenderWithNameCloseingFrame(StringBuilder sb) { sb.Append("
"); } } }