init
This commit is contained in:
@@ -0,0 +1,203 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Mvc.Html;
|
||||
|
||||
namespace Kreta.Web.Helpers
|
||||
{
|
||||
public static class TextareaExtensions
|
||||
{
|
||||
public static MvcHtmlString KretaTextArea(this HtmlHelper helper, string name, List<string> value, int rows = 3, Dictionary<string, object> htmlAttributes = null)
|
||||
{
|
||||
StringBuilder textareaSB = new StringBuilder();
|
||||
|
||||
textareaSB.Append("<textarea id='" + name + "' name='" + name + "' rows='" + rows + "' ");
|
||||
if (htmlAttributes != null)
|
||||
{
|
||||
if (htmlAttributes.ContainsKey("class"))
|
||||
{
|
||||
htmlAttributes["class"] += " k-textbox";
|
||||
}
|
||||
else
|
||||
{
|
||||
htmlAttributes.Add("class", "k-textbox");
|
||||
}
|
||||
|
||||
foreach (var item in htmlAttributes)
|
||||
{
|
||||
textareaSB.Append(item.Key.ToString() + "='" + item.Value.ToString() + "'");
|
||||
}
|
||||
}
|
||||
textareaSB.AppendFormat(">{0}</textarea>", HttpUtility.HtmlEncode(string.Join("\n", value)));
|
||||
|
||||
return new MvcHtmlString(textareaSB.ToString());
|
||||
}
|
||||
|
||||
public static MvcHtmlString KretaTextAreaFor<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression, int rows = 3, Dictionary<string, object> 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<string, object>();
|
||||
|
||||
var attributes = Mapper.GetUnobtrusiveValidationAttributes(helper, expression, htmlAttributes, metadata);
|
||||
|
||||
foreach (var key in attributes.Keys)
|
||||
{
|
||||
if (!htmlAttributes.ContainsKey(key))
|
||||
{
|
||||
htmlAttributes.Add(key, attributes[key].ToString());
|
||||
}
|
||||
}
|
||||
|
||||
var validationAttributes = helper.GetUnobtrusiveValidationAttributes(fullBindingName, metadata);
|
||||
|
||||
if (metadata.IsRequired)
|
||||
{ htmlAttributes.Add("data-labelmsg", metadata.DisplayName + " *"); }
|
||||
else
|
||||
{
|
||||
if (metadata.DisplayName == null)
|
||||
{
|
||||
htmlAttributes.Add("data-labelmsg", "");
|
||||
}
|
||||
else
|
||||
{
|
||||
htmlAttributes.Add("data-labelmsg", metadata.DisplayName);
|
||||
}
|
||||
}
|
||||
|
||||
if (htmlAttributes.ContainsKey("class"))
|
||||
{
|
||||
htmlAttributes["class"] += " k-textbox";
|
||||
}
|
||||
else
|
||||
{
|
||||
htmlAttributes.Add("class", "k-textbox");
|
||||
}
|
||||
|
||||
if (expression.Body is MemberExpression me && me.Member != null)
|
||||
{
|
||||
int maxLength = 0;
|
||||
|
||||
MaxLengthAttribute maxLengthAttribute = me.Member.GetCustomAttributes(typeof(MaxLengthAttribute), false).Cast<MaxLengthAttribute>().FirstOrDefault();
|
||||
|
||||
if (maxLengthAttribute != null)
|
||||
{
|
||||
maxLength = maxLengthAttribute.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
StringLengthAttribute stringLengthAttribute = me.Member.GetCustomAttributes(typeof(StringLengthAttribute), false).Cast<StringLengthAttribute>().FirstOrDefault();
|
||||
|
||||
if (stringLengthAttribute != null)
|
||||
{
|
||||
maxLength = stringLengthAttribute.MaximumLength;
|
||||
}
|
||||
}
|
||||
|
||||
if (maxLength > 0)
|
||||
{
|
||||
htmlAttributes["maxlength"] = maxLength.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder textareaSB = new StringBuilder();
|
||||
textareaSB.Append("<textarea id='" + fieldName.Replace(".", "_") + "' name='" + fieldName + "' rows='" + rows + "' ");
|
||||
|
||||
foreach (var item in htmlAttributes)
|
||||
{
|
||||
textareaSB.Append(item.Key.ToString() + "='" + item.Value.ToString() + "' ");
|
||||
}
|
||||
|
||||
textareaSB.Append(">").Append(HttpUtility.HtmlEncode(value)).Append("</textarea>");
|
||||
|
||||
return new MvcHtmlString(textareaSB.ToString());
|
||||
}
|
||||
|
||||
public static MvcHtmlString RenderWithName(this MvcHtmlString helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string customLabelDivClass = "", string customLabelClass = "", string tooltipResource = null)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
/*Hack hogy kiszedjük a stringből a labelMsg-t*/
|
||||
int startIndex = helper.ToString().IndexOf("data-labelmsg");
|
||||
string str = helper.ToString().Substring(startIndex + 15).Replace("</textarea>", "");
|
||||
int startIndex2 = str.IndexOf("'");
|
||||
string labelMsg = str.Substring(0, startIndex2);
|
||||
/*Hack hogy kiszedjük a stringből a labelMsg-t*/
|
||||
|
||||
/*Hack hogy kiszedjük a stringből az Id-t*/
|
||||
int startIndexId = helper.ToString().IndexOf("id");
|
||||
int startIndexName = helper.ToString().IndexOf("name");
|
||||
string str2 = helper.ToString().Substring(0, startIndexName).Replace("<textarea id='", "");
|
||||
int startIndex2Id = str2.IndexOf("'");
|
||||
string labelMsg2 = str2.Substring(0, startIndex2Id);
|
||||
/*Hack hogy kiszedjük a stringből a labelMsg-t*/
|
||||
|
||||
if (string.IsNullOrWhiteSpace(tooltipResource))
|
||||
AddRenderWithNameBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, labelMsg2, labelMsg, string.Empty);
|
||||
else
|
||||
AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, labelMsg2, labelMsg, string.Empty, tooltipResource);
|
||||
|
||||
sb.Append(helper.ToHtmlString());
|
||||
AddRenderWithNameCloseingFrame(sb);
|
||||
|
||||
return new MvcHtmlString(sb.ToString());
|
||||
}
|
||||
|
||||
public static MvcHtmlString RenderWithName(this MvcHtmlString helper, string label, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false, string tooltipResource = null)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(tooltipResource))
|
||||
AddRenderWithNameBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, string.Empty, label, string.Empty);
|
||||
else
|
||||
AddRenderWithNameTooltipBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, string.Empty, label, string.Empty, tooltipResource);
|
||||
|
||||
sb.Append(helper.ToHtmlString());
|
||||
AddRenderWithNameCloseingFrame(sb);
|
||||
|
||||
return new MvcHtmlString(sb.ToString());
|
||||
}
|
||||
|
||||
public static MvcHtmlString Render(this MvcHtmlString helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
AddRenderWithNameBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, string.Empty, string.Empty, string.Empty);
|
||||
sb.Append(helper.ToHtmlString());
|
||||
AddRenderWithNameCloseingFrame(sb);
|
||||
|
||||
return new MvcHtmlString(sb.ToString());
|
||||
}
|
||||
|
||||
private static void AddRenderWithNameBeginingFrame(StringBuilder sb, int labelWidth, int inputWidth, bool allSizeSame, string controlName, string labelMsg, string customClass)
|
||||
{
|
||||
sb.AppendFormat("<div class=\"{0} {1} \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
|
||||
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}</label>", controlName, labelMsg);
|
||||
sb.AppendFormat("</div><div class=\"{0} {1}\">", 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)
|
||||
{
|
||||
sb.AppendFormat("<div class=\"{0} {1} kretaLabelTooltip \">", BootsrapHelper.GetSizeClasses(labelWidth, allSizeSame), customClass);
|
||||
sb.AppendFormat("<label class=\"windowInputLabel\" for=\"{0}\">{1}", controlName, labelMsg);
|
||||
sb.Append(" <img class='kretaLabelTooltipImg' />");
|
||||
sb.AppendFormat("<span class=\"kretaLabelTooltipText\">{0}</span>", tooltipResource);
|
||||
sb.Append("</label>");
|
||||
sb.AppendFormat("</div><div class=\"{0} {1}\">", BootsrapHelper.GetSizeClasses(inputWidth, allSizeSame), customClass);
|
||||
}
|
||||
|
||||
private static void AddRenderWithNameCloseingFrame(StringBuilder sb)
|
||||
{
|
||||
sb.Append("</div>");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user