using FluentValidation.Internal; using GreenTree.Strohrmann.ERP.Core.Extension; using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.EntityFrameworkCore.Infrastructure; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace GreenTree.Strohrmann.ERP.Web.Extension { public static class HtmlContentExtension { #region Inputs /// /// Return HTML markup for the , using a display template for a bootstrap extension token field. /// See bootstrap-tokenfield for more information of the /// input type. /// /// Model type. /// Model value type. /// HtmlHelper context. /// Expression for the requested model enumeration value. /// The delimiter string used to seperate token values. /// Additional HTML attributes. /// The available values for the tokenbox. /// True when every available value can only be selected once. public static IHtmlContent TokenBoxFor( this IHtmlHelper htmlHelper, Expression> expression, string delimiter = "|", object htmlAttributes = null, string[] availableValues = null, bool allowOnlyOnce = true) where TValue : IEnumerable { var attributes = htmlAttributes == null ? new string[0] : htmlAttributes.GetType().GetProperties() .Select(p => String.Format("{0}='{1}'", p.Name.Replace("_", "-"), p.GetValue(htmlAttributes))) .ToArray(); var func = expression.Compile(); var id = Guid.NewGuid().ToShortString(); var scriptBuilder = new StringBuilder(); var memberInfo = expression.Body.NodeType == ExpressionType.MemberAccess ? ((MemberExpression)expression.Body).Member : null; var type = expression.Body.GetType(); if (availableValues != null) { var autoComplete = JsonConvertWithoutQuotes(new { autocomplete = new { source = availableValues, delay = 200 }, showAutocompleteOnFocus = true }); scriptBuilder.AppendFormat( ""); } else { scriptBuilder.AppendFormat("", id); } if (htmlHelper.ViewData.Model == null) { return new HtmlString( String.Format( " {4}", id, String.Empty, delimiter, String.Join(" ", attributes), scriptBuilder)); } else { var values = func(htmlHelper.ViewData.Model); var valueInputs = values .Select(v => String.Format( "", memberInfo == null ? id : memberInfo.Name, v)); return new HtmlString( String.Format( " {4} {5}", id, values == null ? String.Empty : String.Join(delimiter, values), delimiter, String.Join(" ", attributes), scriptBuilder, String.Join(" ", valueInputs))); } } /// /// Return HTML markup for the , using a bootstrap badge containing 'Yes' or 'No'. /// See bootstrap badge for more information of the badge. /// /// Model type. /// HtmlHelper context. /// Expression for the requested model bool value. public static IHtmlContent YesNoBadgeFor( this IHtmlHelper htmlHelper, Expression> expression) { var func = expression.Compile(); if (htmlHelper.ViewData.Model == null) { return new HtmlString("N/A"); } else { return func(htmlHelper.ViewData.Model) ? new HtmlString("Ja") : new HtmlString("Nein"); } } /// /// Return HTML markup for the , using a display template for a country selection field. /// /// Model type. /// Model value type. /// HtmlHelper context. /// Expression for the requested model enumeration value. /// Additional HTML attributes. public static IHtmlContent CountrySelectionFor( this IHtmlHelper htmlHelper, Expression> expression, object htmlAttributes = null) { var attributes = htmlAttributes == null ? new string[0] : htmlAttributes.GetType().GetProperties() .Select(p => String.Format("{0}='{1}'", p.Name.Replace("_", "-"), p.GetValue(htmlAttributes))) .ToArray(); var func = expression.Compile(); var funcVal = htmlHelper.ViewData.Model == null ? String.Empty : func(htmlHelper.ViewData.Model).ToString(); var memberInfo = expression.Body.NodeType == ExpressionType.MemberAccess ? ((MemberExpression)expression.Body).Member : null; var name = memberInfo == null ? Guid.NewGuid().ToShortString() : memberInfo.Name; var selectBuilder = new StringBuilder(); selectBuilder.AppendFormat(""); return new HtmlString(selectBuilder.ToString()); } /// /// Generates a Span HTML element /// /// HtmlHelper context. /// The span content text. /// Additional HTML attributes. /// public static IHtmlContent Span(this IHtmlHelper htmlHelper, string content, object htmlAttributes = null) { var attributes = htmlAttributes == null ? new string[0] : htmlAttributes.GetType().GetProperties() .Select(p => String.Format("{0}='{1}'", p.Name.Replace("_", "-"), p.GetValue(htmlAttributes))) .ToArray(); var htmlString = String.Format("{1}", String.Join(" ", attributes), content); return new HtmlString(htmlString); } #endregion #region Helper /// /// Generates a random short ID string /// /// HtmlHelper context. public static string RandomId(this IHtmlHelper htmlHelper) { return Guid.NewGuid().ToShortString(); } /// /// Generates a random short ID string /// public static string RandomId() { return Guid.NewGuid().ToShortString(); } /// /// Converts an object in a JS literal object format /// /// The object to be converted. private static string JsonConvertWithoutQuotes(object value) { var serializer = new JsonSerializer(); var stringWriter = new StringWriter(); using (var writer = new JsonTextWriter(stringWriter)) { writer.QuoteName = false; serializer.Serialize(writer, value); } return stringWriter.ToString(); } #endregion } }