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.Reflection;
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);
}
///
/// Return HTML markup for the , using a display template for a search selection field.
///
/// Model type.
/// Model value type.
/// HtmlHelper context.
/// Expression for the requested model value.
/// Expression for the requested model text.
/// POST action URL that returns a model enumeration as JSON result.
/// Additional HTML attributes for the value input.
/// Additional HTML attributes for the text input.
///
public static IHtmlContent SearchFor(
this IHtmlHelper htmlHelper,
Expression> valueExpression,
Expression> textExpression,
string searchActionUrl,
object htmlAttributesValue = null,
object htmlAttributesText = null)
{
var attributesText = htmlAttributesText == null
? new string[0]
: htmlAttributesText.GetType().GetProperties()
.Where(p => p.Name != "name")
.Select(p => String.Format("{0}='{1}'", p.Name.Replace("_", "-"), p.GetValue(htmlAttributesText)))
.ToArray();
var attributesValue = htmlAttributesValue == null
? new string[0]
: htmlAttributesValue.GetType().GetProperties()
.Where(p => p.Name != "name")
.Select(p => String.Format("{0}='{1}'", p.Name.Replace("_", "-"), p.GetValue(htmlAttributesValue)))
.ToArray();
var id = RandomId();
var valFunc = valueExpression.Compile();
var funcVal = htmlHelper.ViewData.Model == null
? default
: valFunc(htmlHelper.ViewData.Model);
var isDefaultValue = funcVal == null || (funcVal != null && funcVal.ToString() == default(TValue).ToString());
var textFunc = textExpression.Compile();
var funcText = htmlHelper.ViewData.Model == null
? default
: textFunc(htmlHelper.ViewData.Model);
var memberInfoText = textExpression.Body.NodeType == ExpressionType.MemberAccess
? ((MemberExpression)textExpression.Body).Member
: null;
var nameText = memberInfoText == null
? Guid.NewGuid().ToShortString()
: memberInfoText.Name;
if (htmlAttributesText != null && htmlAttributesText.GetType().GetProperty("name") != null)
nameText = htmlAttributesText.GetType().GetProperty("name").GetValue(htmlAttributesText).ToString();
var memberInfoValue = valueExpression.Body.NodeType == ExpressionType.MemberAccess
? ((MemberExpression)valueExpression.Body).Member
: null;
var nameValue = memberInfoValue == null
? Guid.NewGuid().ToShortString()
: memberInfoValue.Name;
if (htmlAttributesValue != null && htmlAttributesValue.GetType().GetProperty("name") != null)
nameValue = htmlAttributesValue.GetType().GetProperty("name").GetValue(htmlAttributesValue).ToString();
var scriptBuilder = new StringBuilder();
scriptBuilder.AppendFormat(
"",
id,
searchActionUrl
);
var contentBuilder = new StringBuilder();
contentBuilder.AppendFormat(
"
\n" +
"
\n" +
"\n" +
"
\n" +
"\n" +
"
\n" +
" \n" +
" \n" +
"
\n" +
"
\n" +
"\n" +
"\n" +
"\n" +
"
\n" +
"
\n" +
"
\n" +
"
\n" +
"
Suchergebnisse
\n" +
"
\n" +
"
\n" +
"
\n" +
"
\n" +
"
\n" +
"\n" +
"
\n" +
"
\n" +
"
",
id,
nameText,
funcText,
nameValue,
funcVal,
String.Join(" ", attributesText),
String.Join(" ", attributesValue),
(htmlHelper.ViewData.Model == null || isDefaultValue ? "style='display: none'" : String.Empty),
(htmlHelper.ViewData.Model == null || isDefaultValue ? String.Empty : "style='display: none'")
);
var htmlString = String.Format("{0}\n{1}", scriptBuilder.ToString(), contentBuilder.ToString());
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
}
}