kreta/KretaWeb/Helpers/SelectorExtensions.cs
2024-03-13 00:33:46 +01:00

257 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;
using Kreta.Core;
using Kreta.Framework;
using Kreta.Resources;
using Kreta.Web.Classes;
using Kreta.Web.Helpers.Error;
namespace Kreta.Web.Helpers
{
public static class SelectorExtensions
{
public static MvcHtmlString KretaSelectorFor<TModel, TResult>(this HtmlHelper<TModel> html, Expression<Func<TModel, TResult>> expression, List<SelectListItem> list, bool allowRemoveSelected = true, string clickFunctionName = "", bool preventClickEventOnDocumentReady = false)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string htmlFieldName = ExpressionHelper.GetExpressionText(expression);
var value = metadata.Model;
StringBuilder sb = new StringBuilder();
sb.Append("<div id=\"div").Append(htmlFieldName.Replace(".", "_")).Append("\">");
sb.Append("<div class=\"selectorwrapper\">");
sb.Append("<ul class=\"bars weeks\">");
foreach (var item in list)
{
sb.Append("<li val=\"").Append(item.Value).Append("\" class=\"\">");
sb.Append("<div>").Append(item.Text).Append("</div>");
sb.Append("</li>");
}
sb.Append("</ul>");
sb.Append("<div class=\"clearfix\"></div>");
sb.Append("</div>");
sb.Append("<input ");
var attributes = Mapper.GetUnobtrusiveValidationAttributes(html, expression, null, metadata);
if (metadata.IsRequired)
{
attributes.Add("data-labelmsg", metadata.DisplayName + " *");
}
else
{
attributes.Add("data-labelmsg", metadata.DisplayName);
}
foreach (var key in attributes.Keys)
{
sb.Append(key + "='" + attributes[key] + "' ");
}
sb.Append(" type =\"hidden\" value=\"\" name=\"").Append(htmlFieldName).Append("\" id=\"").Append("").Append(htmlFieldName.Replace(".", "_")).Append("\">");
sb.Append("</div>");
StringBuilder scriptSb = new StringBuilder();
scriptSb.Append("<script>");
scriptSb.Append("$(document).ready(function() {");
scriptSb.Append("$('#div").Append(htmlFieldName.Replace(".", "_")).Append("').find('.selectorwrapper > ul > li').click(function() {");
scriptSb.Append("ElemKivalasztasa(this, '").Append(htmlFieldName.Replace(".", "_")).Append("');");
if (!string.IsNullOrWhiteSpace(clickFunctionName))
scriptSb.Append(clickFunctionName).Append("(this);");
scriptSb.Append("});");
if (!preventClickEventOnDocumentReady && value != null)
scriptSb.Append("$('#div").Append(htmlFieldName.Replace(".", "_")).Append("').find('.selectorwrapper > ul > li[val=").Append(value).Append("]').click();");
scriptSb.Append("});");
scriptSb.Append("</script>");
StringBuilder scriptIISb = new StringBuilder();
if (allowRemoveSelected)
{
scriptIISb.Append(@"<script>
function ElemKivalasztasa(elem, hiddenclientid) {
var ugyanarra = $(elem).hasClass('activebar');
$(elem).siblings('li').removeClass('activebar');
if (ugyanarra) {
$(elem).removeClass('activebar');
$('#' + hiddenclientid).val('');
}
else {
$(elem).addClass('activebar');
$('#' + hiddenclientid).val($(elem).attr('val'));
}
}
</script>");
}
else
{
scriptIISb.Append(@"<script>
function ElemKivalasztasa(elem, hiddenclientid) {
var ugyanarra = $(elem).hasClass('activebar');
$(elem).siblings('li').removeClass('activebar');
if (!ugyanarra) {
$(elem).addClass('activebar');
$('#' + hiddenclientid).val($(elem).attr('val'));
}
}
</script>");
}
StringBuilder result = new StringBuilder();
result.Append(scriptIISb);
result.Append(sb);
result.Append(scriptSb);
return new MvcHtmlString(result.ToString());
}
public static MvcHtmlString RenderWithNameForSelector(this MvcHtmlString helper, int labelWidth = 6, int inputWidth = 6, bool allSizeSame = false)
{
StringBuilder sb = new StringBuilder();
string labelMsg = null;
var str = helper.ToString();
MatchCollection matches = Regex.Matches(str, @"data-labelmsg='(.*)' ");
foreach (Match match in matches)
{
labelMsg = match.Groups[1].Value;
}
string labelName = null;
MatchCollection matches2 = Regex.Matches(str.Replace('"', '@'), @"name=@(.*)@ ");
foreach (Match match in matches2)
{
labelName = match.Groups[1].Value;
}
if (labelMsg == null || labelName == null)
{
throw new StatusError(500, ErrorResource.HibaTortentAzOldalonAzAdatokBetoltesekor)
{
UnHandledException = new Exception(ErrorResource.DOMHiba)
};
}
AddRenderWithNameBeginingFrame(sb, labelWidth, inputWidth, allSizeSame, labelName, labelMsg, 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 AddRenderWithNameCloseingFrame(StringBuilder sb)
{
sb.Append("</div>");
}
public static MvcHtmlString KretaSelectorForGrid<TModel, TResult>(this HtmlHelper<TModel> html, string htmlFieldName, string name, string value, Expression<Func<TModel, TResult>> expression, List<ExtendedSelectListItem> list, bool hasTooltip = false, string customClickFunctionName = null)
{
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var controlStringBuilder = new StringBuilder();
controlStringBuilder.Append($"<div id=\"div{htmlFieldName.Replace(".", "_")}\">");
controlStringBuilder.Append("<div class=\"selectorwrapper\">");
controlStringBuilder.Append("<ul class=\"bars weeks\">");
foreach (ExtendedSelectListItem item in list)
{
string tooltip = null;
if (hasTooltip)
{
if (!string.IsNullOrWhiteSpace(item.Tooltip))
{
tooltip = item.Tooltip;
}
else if (int.TryParse(item.Value, out var itemValue) && itemValue.IsEntityId())
{
tooltip = StringResourcesUtil.GetString(itemValue);
}
}
controlStringBuilder.Append(!string.IsNullOrWhiteSpace(tooltip) ?
$"<li val=\"{item.Value}\" class=\"kretaSelectorButton\" title=\"{tooltip}\">" :
$"<li val=\"{item.Value}\" class=\"kretaSelectorButton\">");
controlStringBuilder.Append($"<div>{item.Text}</div>");
controlStringBuilder.Append("</li>");
}
controlStringBuilder.Append("</ul>");
controlStringBuilder.Append("<div class=\"clearfix\"></div>");
controlStringBuilder.Append("<input ");
RouteValueDictionary attributes = Mapper.GetUnobtrusiveValidationAttributes(html, expression, null, metadata);
foreach (string key in attributes.Keys)
{
controlStringBuilder.Append($"{key}='{attributes[key]}' ");
}
controlStringBuilder.AppendFormat(@"type =""hidden"" value="""" name=""{0}"" id=""{0}"" data-rowInputName=""{1}"" />", htmlFieldName, name);
controlStringBuilder.Append("</div>");
controlStringBuilder.Append("</div>");
var scriptStringBuilder = new StringBuilder();
scriptStringBuilder.Append("<script>");
scriptStringBuilder.Append("$(document).ready(function() {");
scriptStringBuilder.Append($"$('\\#div{htmlFieldName.Replace(".", "_")}').find('.selectorwrapper > ul > li').click(function() {{");
scriptStringBuilder.Append(@"
var elem = $(this);
var ugyanarra = elem.hasClass('activebar');
var hidden = elem.closest('.selectorwrapper').find('input[type=""hidden""]');
elem.siblings('li').removeClass('activebar');
if (ugyanarra)
{
elem.removeClass('activebar');
hidden.val('');
hidden.trigger('change');
}
else {
elem.addClass('activebar');
hidden.val($(elem).attr('val'));
hidden.trigger('change');
}
");
if (!string.IsNullOrWhiteSpace(customClickFunctionName))
{
scriptStringBuilder.Append(customClickFunctionName + "(this);");
}
scriptStringBuilder.Append("});");
scriptStringBuilder.Append($"$('\\#div{htmlFieldName.Replace(".", "_")}').find('.selectorwrapper > ul > li[val=\"{value}\"]').click();");
scriptStringBuilder.Append("});");
scriptStringBuilder.Append("</script>");
var result = new StringBuilder();
result.Append(controlStringBuilder);
result.Append(scriptStringBuilder);
return new MvcHtmlString(result.ToString());
}
}
}