216 lines
13 KiB
C#
216 lines
13 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Linq.Expressions;
|
|
using System.Text;
|
|
using System.Web.Mvc;
|
|
using Kreta.Framework;
|
|
|
|
namespace Kreta.Web.Helpers
|
|
{
|
|
public static class RangeNumericExtensions
|
|
{
|
|
public static MvcHtmlString KretaRangeNumeric<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> fromexpression, Expression<Func<TModel, TValue>> toexpression, string title = "", int? precision = null, double? step = null)
|
|
{
|
|
var spinStep = (step ?? 1).ToString(CultureInfo.InvariantCulture.NumberFormat);
|
|
var fromFieldName = ExpressionHelper.GetExpressionText(fromexpression);
|
|
var fromFullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fromFieldName);
|
|
var fromMetadata = ModelMetadata.FromLambdaExpression(fromexpression, helper.ViewData);
|
|
var fromDisplayName = fromMetadata.DisplayName;
|
|
var fromValue = fromMetadata.Model;
|
|
var fromValidationAttributes = helper.GetUnobtrusiveValidationAttributes(fromFullBindingName, fromMetadata);
|
|
object globalMinValue = Constants.General.KretaRangeNumericSideBarMinValue;
|
|
if (fromValidationAttributes.Count > 0)
|
|
{ foreach (var item in fromValidationAttributes) { if (item.Key == "data-val-range-min") { globalMinValue = item.Value; } } }
|
|
int decimalPrecision = precision ?? (globalMinValue != null && globalMinValue is Double ? 2 : 0);
|
|
|
|
var toFieldName = ExpressionHelper.GetExpressionText(toexpression);
|
|
var toFullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(toFieldName);
|
|
var toMetadata = ModelMetadata.FromLambdaExpression(toexpression, helper.ViewData);
|
|
var toDisplayName = toMetadata.DisplayName;
|
|
var toValue = toMetadata.Model;
|
|
var toValidationAttributes = helper.GetUnobtrusiveValidationAttributes(toFullBindingName, toMetadata);
|
|
object globalMaxValue = Constants.General.KretaRangeNumericSideBarMaxValue;
|
|
if (toValidationAttributes.Count > 0)
|
|
{ foreach (var item in toValidationAttributes) { if (item.Key == "data-val-range-max") { globalMaxValue = item.Value; } } }
|
|
decimalPrecision = precision ?? (globalMaxValue != null && globalMaxValue is Double ? 2 : 0);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
string fromDate = "<input id='" + fromFieldName + "' name='" + fromFieldName + "' />";
|
|
string toDate = "<input id='" + toFieldName + "' name='" + toFieldName + "' />";
|
|
|
|
string scriptTag = @"<script>
|
|
$(document).ready(function () {
|
|
function set" + fromFieldName + @"Change() {
|
|
var maxValue = $('#" + toFieldName + @"').data('kendoNumericTextBox').value();
|
|
if(!(maxValue == null || typeof maxValue === 'undefined')) {
|
|
$('#" + fromFieldName + @"').data('kendoNumericTextBox').max(maxValue);
|
|
}
|
|
|
|
$('#" + fromFieldName + @"').focusin().focusout();
|
|
}
|
|
|
|
function set" + toFieldName + @"Change() {
|
|
var minValue = $('#" + fromFieldName + @"').data('kendoNumericTextBox').value();
|
|
if(!(minValue == null || typeof minValue === 'undefined'')) {
|
|
$('#" + toFieldName + @"').data('kendoNumericTextBox').min(minValue);
|
|
}
|
|
|
|
$('#" + toFieldName + @"').focusin().focusout();
|
|
}
|
|
|
|
$('#" + fromFieldName + @"').kendoNumericTextBox({
|
|
format: 'n" + decimalPrecision.ToString() + @"',
|
|
decimals: " + decimalPrecision.ToString() + @",
|
|
step: " + spinStep + @",
|
|
change: set" + fromFieldName + @"Change";
|
|
if (globalMinValue != null)
|
|
{ scriptTag += ",min: " + (globalMinValue is Double ? (double)globalMinValue : (int)globalMinValue).ToString(CultureInfo.InvariantCulture.NumberFormat) + ""; }
|
|
scriptTag += @"
|
|
});
|
|
|
|
$('#" + toFieldName + @"').kendoNumericTextBox({
|
|
format: 'n" + decimalPrecision.ToString() + @"',
|
|
decimals: " + decimalPrecision.ToString() + @",
|
|
step: " + spinStep + @",
|
|
change: set" + toFieldName + @"Change";
|
|
if (globalMaxValue != null)
|
|
{ scriptTag += ",max: " + (globalMaxValue is Double ? (double)globalMaxValue : (int)globalMaxValue).ToString(CultureInfo.InvariantCulture.NumberFormat) + ""; }
|
|
scriptTag += @"
|
|
});
|
|
});
|
|
</script>";
|
|
|
|
string displayName = "";
|
|
if (!string.IsNullOrWhiteSpace(title))
|
|
{ displayName = title; }
|
|
else
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(fromDisplayName))
|
|
{ displayName = fromDisplayName; }
|
|
else
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(toDisplayName))
|
|
{ displayName = toDisplayName; }
|
|
}
|
|
}
|
|
|
|
sb.Append("<div class=\"col-xs-6\">");
|
|
sb.Append("<label class=\"searchPanelLabel\" for=\"").Append("a").Append("\">").Append(displayName).Append("</label>");
|
|
sb.Append("</div>");
|
|
sb.Append("<div class=\"col-xs-6\">");
|
|
sb.Append(fromDate).Append(" ").Append(toDate).Append(scriptTag);
|
|
sb.Append("</div>");
|
|
|
|
return new MvcHtmlString(sb.ToString());
|
|
}
|
|
|
|
public static MvcHtmlString KretaRangeNumericSideBar<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> fromexpression, Expression<Func<TModel, TValue>> toexpression, string title = "", int? precision = null, bool needTolIgLabels = false, double? step = null)
|
|
{
|
|
var spinStep = (step ?? 1).ToString(CultureInfo.InvariantCulture.NumberFormat);
|
|
var fromFieldName = ExpressionHelper.GetExpressionText(fromexpression);
|
|
var fromFullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(fromFieldName);
|
|
var fromMetadata = ModelMetadata.FromLambdaExpression(fromexpression, helper.ViewData);
|
|
var fromDisplayName = fromMetadata.DisplayName;
|
|
var fromValue = fromMetadata.Model;
|
|
var fromValidationAttributes = helper.GetUnobtrusiveValidationAttributes(fromFullBindingName, fromMetadata);
|
|
object globalMinValue = Constants.General.KretaRangeNumericSideBarMinValue;
|
|
if (fromValidationAttributes.Count > 0)
|
|
{ foreach (var item in fromValidationAttributes) { if (item.Key == "data-val-range-min") { globalMinValue = item.Value; } } }
|
|
int decimalPrecision = precision ?? (globalMinValue != null && globalMinValue is Double ? 2 : 0);
|
|
|
|
var toFieldName = ExpressionHelper.GetExpressionText(toexpression);
|
|
var toFullBindingName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(toFieldName);
|
|
var toMetadata = ModelMetadata.FromLambdaExpression(toexpression, helper.ViewData);
|
|
var toDisplayName = toMetadata.DisplayName;
|
|
var toValue = toMetadata.Model;
|
|
var toValidationAttributes = helper.GetUnobtrusiveValidationAttributes(toFullBindingName, toMetadata);
|
|
object globalMaxValue = Constants.General.KretaRangeNumericSideBarMaxValue;
|
|
if (toValidationAttributes.Count > 0)
|
|
{ foreach (var item in toValidationAttributes) { if (item.Key == "data-val-range-max") { globalMaxValue = item.Value; } } }
|
|
decimalPrecision = precision ?? (globalMaxValue != null && globalMaxValue is Double ? 2 : 0);
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
string fromDate = "<input id='" + fromFieldName + "' name='" + fromFieldName + "' />";
|
|
string toDate = "<input id='" + toFieldName + "' name='" + toFieldName + "' />";
|
|
|
|
string scriptTag = @"<script>
|
|
$(document).ready(function () {
|
|
function set" + fromFieldName + @"Change() {
|
|
var maxValue = $('#" + toFieldName + @"').data('kendoNumericTextBox').value();
|
|
if(!(maxValue == null || typeof maxValue === 'undefined')) {
|
|
$('#" + fromFieldName + @"').data('kendoNumericTextBox').max(maxValue);
|
|
}
|
|
|
|
$('#" + fromFieldName + @"').focusin().focusout();
|
|
}
|
|
|
|
function set" + toFieldName + @"Change() {
|
|
var minValue = $('#" + fromFieldName + @"').data('kendoNumericTextBox').value();
|
|
if(!(minValue == null || typeof minValue === 'undefined')) {
|
|
$('#" + toFieldName + @"').data('kendoNumericTextBox').min(minValue);
|
|
}
|
|
|
|
$('#" + toFieldName + @"').focusin().focusout();
|
|
}
|
|
|
|
$('#" + fromFieldName + @"').kendoNumericTextBox({
|
|
format: 'n" + decimalPrecision.ToString() + @"',
|
|
decimals: " + decimalPrecision.ToString() + @",
|
|
spin: set" + fromFieldName + @"Change,
|
|
step: " + spinStep;
|
|
if (globalMinValue != null)
|
|
{ scriptTag += ",min: " + (globalMinValue is Double ? (double)globalMinValue : (int)globalMinValue).ToString(CultureInfo.InvariantCulture.NumberFormat) + ""; }
|
|
if (globalMaxValue != null)
|
|
{ scriptTag += ",max: " + (globalMaxValue is Double ? (double)globalMaxValue : (int)globalMaxValue).ToString(CultureInfo.InvariantCulture.NumberFormat) + ""; }
|
|
scriptTag += @"
|
|
});
|
|
|
|
$('#" + toFieldName + @"').kendoNumericTextBox({
|
|
format: 'n" + decimalPrecision.ToString() + @"',
|
|
decimals: " + decimalPrecision.ToString() + @",
|
|
spin: set" + toFieldName + @"Change,
|
|
step: " + spinStep;
|
|
if (globalMinValue != null)
|
|
{ scriptTag += ",min: " + (globalMinValue is Double ? (double)globalMinValue : (int)globalMinValue).ToString(CultureInfo.InvariantCulture.NumberFormat) + ""; }
|
|
if (globalMaxValue != null)
|
|
{ scriptTag += ",max: " + (globalMaxValue is Double ? (double)globalMaxValue : (int)globalMaxValue).ToString(CultureInfo.InvariantCulture.NumberFormat) + ""; }
|
|
scriptTag += @"
|
|
});
|
|
";
|
|
if (needTolIgLabels)
|
|
{
|
|
scriptTag += "$('#" + fromFieldName + @"').parent().parent().after('" + string.Format("<span class=\"searchPanelLabel\">{0}</span>", StringResourcesUtil.GetString(3080)) + @"');
|
|
$('#" + toFieldName + @"').parent().parent().after('" + string.Format("<span class=\"searchPanelLabel\">{0}</span>", StringResourcesUtil.GetString(3081)) + @"');"
|
|
;
|
|
}
|
|
scriptTag += @"
|
|
});
|
|
</script>";
|
|
|
|
string displayName = "";
|
|
if (!string.IsNullOrWhiteSpace(title))
|
|
{ displayName = title; }
|
|
else
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(fromDisplayName))
|
|
{ displayName = fromDisplayName; }
|
|
else
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(toDisplayName))
|
|
{ displayName = toDisplayName; }
|
|
}
|
|
}
|
|
|
|
sb.Append("<div class=\"searchPanelRow\">");
|
|
sb.Append("<div class=\"searchPanelRowTitle\">");
|
|
sb.Append("<label class=\"searchPanelLabel\" for=\"").Append("aaa").Append("\">").Append(displayName).Append("</label>");
|
|
sb.Append("</div>");
|
|
sb.Append("<div class=\"searchPanelRowValue\">");
|
|
sb.Append(fromDate).Append("<br />").Append(toDate).Append(scriptTag);
|
|
sb.Append("</div>");
|
|
sb.Append("</div>");
|
|
|
|
return new MvcHtmlString(sb.ToString());
|
|
}
|
|
}
|
|
}
|