<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;
using Signum.Utilities;
using System.Web.Mvc.Html;

namespace Signum.Web
{
    public static class ValueLineHelper
    {
        public static ValueLineConfigurator Configurator = new ValueLineConfigurator(); 

        private static string ManualValueLine<T>(this HtmlHelper helper, string idValueField, T value, string labelText, Dictionary<string, object> valueFieldHtmlProps, ValueLineType? valueLineType)
        {
            StringBuilder sb = new StringBuilder();

            idValueField = helper.GlobalName(idValueField);

            if (StyleContext.Current.LabelVisible)
            {
                sb.Append("<div>");
                sb.Append(helper.Label(idValueField + "lbl", labelText, idValueField, TypeContext.CssLineLabel));
            }
            string valueStr = (value != null) ? value.ToString() : "";
            if (StyleContext.Current.ReadOnly)
                sb.Append(helper.Span(idValueField, valueStr, "valueLine"));
            else
            {
                ValueLineType vltype = (valueLineType.HasValue) ? 
                    valueLineType.Value : 
                    Configurator.GetDefaultValueLineType(typeof(T));

                if (valueFieldHtmlProps == null)
                    valueFieldHtmlProps = new Dictionary<string, object>();

                if (StyleContext.Current.ShowValidationMessage)
                {
                    valueFieldHtmlProps.Add("class", "valueLine inlineVal"); //inlineVal class tells Javascript code to show Inline Error
                    sb.Append(Configurator.constructor[vltype](helper, new ValueLineData(idValueField, value, valueFieldHtmlProps, typeof(T)))); 
                    sb.Append("\n");
                    sb.Append("&nbsp;");
                    sb.Append(helper.ValidationMessage(idValueField));
                    sb.Append("\n");
                }
                else
                {
                    valueFieldHtmlProps.Add("class", "valueLine");
                    sb.Append(Configurator.constructor[vltype](helper, new ValueLineData(idValueField, value, valueFieldHtmlProps, typeof(T)))); 
                    sb.Append("\r\n");
                }
            }
            if (StyleContext.Current.LabelVisible)
                sb.Append("</div>");
            if (StyleContext.Current.BreakLine)
                sb.Append("<div class=\"clearall\"></div>\n");

            helper.ViewContext.HttpContext.Response.Write(sb.ToString());

            return idValueField;
        }

        public static string EnumComboBox(this HtmlHelper helper, string idValueField, Type enumType, object value, Dictionary<string, object> htmlProperties) 
        {
            StringBuilder sb = new StringBuilder();
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = "-", Value = "", Selected = true });
            items.AddRange(
                Enum.GetValues(enumType.UnNullify())
                    .Cast<object>()
                    .Select(v => new SelectListItem()
                        {
                            Text = EnumExtensions.NiceToString(v),
                            Value = v.ToString(), 
                            Selected = object.Equals(value,v),
                        })
                );
            
            return helper.DropDownList(idValueField, items, htmlProperties);
        }

        public static string DateTimePickerTextbox(this HtmlHelper helper, string idValueField, object value, Dictionary<string, object> htmlProperties)
        {
            return helper.TextBox(idValueField, value != null ? value.ToString() : "", htmlProperties) + 
                   "\n" + 
                   helper.Calendar(idValueField);
        }

        public static string TextboxInLine(this HtmlHelper helper, string idValueField, string valueStr, Dictionary<string, object> htmlProperties)
        {
            htmlProperties.Add("autocomplete", "off");
            htmlProperties.Add("onblur", "this.setAttribute('value', this.value);");
            return helper.TextBox(idValueField, valueStr, htmlProperties);
        }

        public static string CheckBox(this HtmlHelper helper, string idValueField, bool? value, Dictionary<string, object> htmlProperties)
        { 
            return System.Web.Mvc.Html.InputExtensions.CheckBox(helper, idValueField, value.HasValue ? value.Value : false, htmlProperties) + "\n";
        }

        public static string ValueLine<T>(this HtmlHelper helper, T value, string idValueField, ValueLine options)
        {
            if (options == null || options.LabelText == null)
                throw new ArgumentException("LabelText property of ValueLineOptions must be specified for Manual Value Lines");

            if (options.StyleContext != null)
            {
                using (options.StyleContext)
                    return helper.ManualValueLine(idValueField, value, options.LabelText, options.ValueFieldHtmlProps, options.ValueLineType);
            }<commit_msg>(TFS CS 2847)<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Linq.Expressions;
using Signum.Utilities;
using System.Web.Mvc.Html;

namespace Signum.Web
{
    public static class ValueLineHelper
    {
        public static ValueLineConfigurator Configurator = new ValueLineConfigurator(); 

        private static string ManualValueLine<T>(this HtmlHelper helper, string idValueField, T value, string labelText, Dictionary<string, object> valueFieldHtmlProps, ValueLineType? valueLineType)
        {
            StringBuilder sb = new StringBuilder();

            idValueField = helper.GlobalName(idValueField);

            if (StyleContext.Current.LabelVisible)
            {
                sb.Append("<div>");
                sb.Append(helper.Label(idValueField + "lbl", labelText, idValueField, TypeContext.CssLineLabel));
            }
            string valueStr = (value != null) ? value.ToString() : "";
            if (StyleContext.Current.ReadOnly)
                sb.Append(helper.Span(idValueField, valueStr, "valueLine"));
            else
            {
                ValueLineType vltype = (valueLineType.HasValue) ? 
                    valueLineType.Value : 
                    Configurator.GetDefaultValueLineType(typeof(T));

                if (valueFieldHtmlProps == null)
                    valueFieldHtmlProps = new Dictionary<string, object>();

                if (StyleContext.Current.ShowValidationMessage)
                {
                    valueFieldHtmlProps.Add("class", "valueLine inlineVal"); //inlineVal class tells Javascript code to show Inline Error
                    sb.Append(Configurator.constructor[vltype](helper, new ValueLineData(idValueField, value, valueFieldHtmlProps, typeof(T)))); 
                    sb.Append("\n");
                    sb.Append("&nbsp;");
                    sb.Append(helper.ValidationMessage(idValueField));
                    sb.Append("\n");
                }
                else
                {
                    valueFieldHtmlProps.Add("class", "valueLine");
                    sb.Append(Configurator.constructor[vltype](helper, new ValueLineData(idValueField, value, valueFieldHtmlProps, typeof(T)))); 
                    sb.Append("\r\n");
                }
            }
            if (StyleContext.Current.LabelVisible)
                sb.Append("</div>");
            if (StyleContext.Current.BreakLine)
                sb.Append("<div class=\"clearall\"></div>\n");

            helper.ViewContext.HttpContext.Response.Write(sb.ToString());

            return idValueField;
        }

        public static string EnumComboBox(this HtmlHelper helper, string idValueField, Type enumType, object value, Dictionary<string, object> htmlProperties) 
        {
            StringBuilder sb = new StringBuilder();
            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = "-", Value = "", Selected = true });
            items.AddRange(
                Enum.GetValues(enumType.UnNullify())
                    .Cast<object>()
                    .Select(v => new SelectListItem()
                        {
                            Text = EnumExtensions.NiceToString(v),
                            Value = v.ToString(), 
                            Selected = object.Equals(value,v),
                        })
                );
            
            return helper.DropDownList(idValueField, items, htmlProperties);
        }

        public static string DateTimePickerTextbox(this HtmlHelper helper, string idValueField, object value, Dictionary<string, object> htmlProperties)
        {
            return helper.TextBox(idValueField, value != null ? value.ToString() : "", htmlProperties) + 
                   "\n" + 
                   helper.Calendar(idValueField);
        }

        public static string TextboxInLine(this HtmlHelper helper, string idValueField, string valueStr, Dictionary<string, object> htmlProperties)
        {
            htmlProperties.Add("autocomplete", "off");
            if (htmlProperties.ContainsKey("onblur"))
                htmlProperties["onblur"] = "this.setAttribute('value', this.value); " + htmlProperties["onblur"];
            else
                htmlProperties.Add("onblur", "this.setAttribute('value', this.value);");
            return helper.TextBox(idValueField, valueStr, htmlProperties);
        }

        public static string CheckBox(this HtmlHelper helper, string idValueField, bool? value, Dictionary<string, object> htmlProperties)
        { 
            return System.Web.Mvc.Html.InputExtensions.CheckBox(helper, idValueField, value.HasValue ? value.Value : false, htmlProperties) + "\n";
        }

        public static string ValueLine<T>(this HtmlHelper helper, T value, string idValueField, ValueLine options)
        {
            if (options == null || options.LabelText == null)
                throw new ArgumentException("LabelText property of ValueLineOptions must be specified for Manual Value Lines");

            if (options.StyleContext != null)
            {
                using (options.StyleContext)
                    return helper.ManualValueLine(idValueField, value, options.LabelText, options.ValueFieldHtmlProps, options.ValueLineType);
            }
{'fraction_non_alphanumeric': 0.10017922837468163, 'fraction_numerical': 0.0003773228940665975, 'mean_word_length': 2.051813471502591, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3840192', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2502, 'n_words': 561}