From 1fe7ef96bf8b6f538fca61d72b78833460935b9e Mon Sep 17 00:00:00 2001 From: yekalkan Date: Wed, 2 May 2018 14:35:47 +0300 Subject: [PATCH] Passed context and output variables to methods in some taghelpers --- .../Form/AbpDynamicformTagHelperService.cs | 26 +++++----- .../Form/AbpInputTagHelperService.cs | 28 +++++------ .../Form/AbpSelectTagHelperService.cs | 47 +++++++++++-------- 3 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpDynamicformTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpDynamicformTagHelperService.cs index 4c39dd335f..138632f1a2 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpDynamicformTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpDynamicformTagHelperService.cs @@ -22,7 +22,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form private readonly IServiceProvider _serviceProvider; public AbpDynamicFormTagHelperService( - HtmlEncoder htmlEncoder, + HtmlEncoder htmlEncoder, IHtmlGenerator htmlGenerator, IServiceProvider serviceProvider) { @@ -33,19 +33,19 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { - var list = InitilizeFormGroupContentsContext(context); + var list = InitilizeFormGroupContentsContext(context, output); NormalizeTagMode(context, output); - + await output.GetChildContentAsync(); await ConvertToMvcForm(context, output); ProcessFields(context, output); - SetContent(output,list); + SetContent(output, list); - SetFormAttributes(output); + SetFormAttributes(context, output); SetSubmitButton(context, output); } @@ -66,7 +66,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form RouteValues = TagHelper.RouteValues, ViewContext = TagHelper.ViewContext }; - + var formTagOutput = GetInnerTagHelper(output.Attributes, context, formTagHelper, "form", TagMode.StartTagAndEndTag); await formTagOutput.GetChildContentAsync(); @@ -81,7 +81,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form output.TagName = "form"; } - protected virtual void SetFormAttributes(TagHelperOutput output) + protected virtual void SetFormAttributes(TagHelperContext context, TagHelperOutput output) { output.Attributes.AddIfNotContains("method", "post"); } @@ -110,13 +110,13 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form output.PostContent.SetHtmlContent(output.PostContent.GetContent() + buttonHtml); } - protected virtual List InitilizeFormGroupContentsContext(TagHelperContext context) + protected virtual List InitilizeFormGroupContentsContext(TagHelperContext context, TagHelperOutput output) { var items = new List(); context.Items[FormGroupContents] = items; return items; } - + protected virtual void ProcessFields(TagHelperContext context, TagHelperOutput output) { var models = GetModels(context, output); @@ -125,15 +125,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form { if (IsSelectGroup(context, model)) { - ProcessSelectGroup(context, model); + ProcessSelectGroup(context, output, model); continue; } - ProcessInputGroup(context, model); + ProcessInputGroup(context, output, model); } } - protected virtual void ProcessSelectGroup(TagHelperContext context, ModelExpression model) + protected virtual void ProcessSelectGroup(TagHelperContext context, TagHelperOutput output, ModelExpression model) { var abpSelectTagHelper = _serviceProvider.GetRequiredService(); abpSelectTagHelper.AspFor = model; @@ -154,7 +154,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form return RenderTagHelper(attributes, context, abpButtonTagHelper, _htmlEncoder, "button", TagMode.StartTagAndEndTag); } - protected virtual void ProcessInputGroup(TagHelperContext context, ModelExpression model) + protected virtual void ProcessInputGroup(TagHelperContext context, TagHelperOutput output, ModelExpression model) { var abpInputTagHelper = _serviceProvider.GetRequiredService(); abpInputTagHelper.AspFor = model; diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpInputTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpInputTagHelperService.cs index 2a82d8c2b3..ccbca99a59 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpInputTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpInputTagHelperService.cs @@ -30,7 +30,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form var order = GetInputOrder(TagHelper.AspFor.ModelExplorer); - AddGroupToFormGroupContents(context, TagHelper.AspFor.Name, SurroundInnerHtmlAndGet(innerHtml, isCheckbox), order, out var surpress); + AddGroupToFormGroupContents(context, TagHelper.AspFor.Name, SurroundInnerHtmlAndGet(context, output, innerHtml, isCheckbox), order, out var surpress); if (surpress) { @@ -47,15 +47,15 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form protected virtual string GetFormInputGroupAsHtml(TagHelperContext context, TagHelperOutput output, out bool isCheckbox) { - var inputTag = GetInputTag(context, out isCheckbox); + var inputTag = GetInputTag(context, output, out isCheckbox); var inputHtml = RenderTagHelperOutput(inputTag, _encoder); - var label = GetLabelAsHtml(inputTag, isCheckbox); + var label = GetLabelAsHtml(context, output, inputTag, isCheckbox); - var validation = isCheckbox ? "" : GetValidationAsHtml(context); + var validation = isCheckbox ? "" : GetValidationAsHtml(context, output); - return GetContent(label, inputHtml, validation, isCheckbox); + return GetContent(context, output, label, inputHtml, validation, isCheckbox); } - protected virtual string GetValidationAsHtml(TagHelperContext context) + protected virtual string GetValidationAsHtml(TagHelperContext context, TagHelperOutput output) { var validationMessageTagHelper = new ValidationMessageTagHelper(_generator) { @@ -68,7 +68,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form return RenderTagHelper(attributeList, context, validationMessageTagHelper, _encoder, "span", TagMode.StartTagAndEndTag, true); } - protected virtual string GetContent(string label, string inputHtml, string validation, bool isCheckbox) + protected virtual string GetContent(TagHelperContext context, TagHelperOutput output, string label, string inputHtml, string validation, bool isCheckbox) { var innerContent = isCheckbox ? inputHtml + Environment.NewLine + label : @@ -78,14 +78,14 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form Environment.NewLine + validation + Environment.NewLine; } - protected virtual string SurroundInnerHtmlAndGet(string innerHtml, bool isCheckbox) + protected virtual string SurroundInnerHtmlAndGet(TagHelperContext context, TagHelperOutput output, string innerHtml, bool isCheckbox) { return "
" + Environment.NewLine + innerHtml + Environment.NewLine + "
"; } - protected virtual TagHelperOutput GetInputTag(TagHelperContext context, out bool isCheckbox) + protected virtual TagHelperOutput GetInputTag(TagHelperContext context, TagHelperOutput output, out bool isCheckbox) { var inputTagHelper = new InputTagHelper(_generator) { @@ -94,19 +94,19 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form }; var inputTagHelperOutput = GetInnerTagHelper(new TagHelperAttributeList(), context, inputTagHelper, "input"); - isCheckbox = IsInputCheckbox(inputTagHelperOutput.Attributes); + isCheckbox = IsInputCheckbox(context, output,inputTagHelperOutput.Attributes); inputTagHelperOutput.Attributes.Add("class", isCheckbox ? "form-check-input" : "form-control"); return inputTagHelperOutput; } - protected virtual bool IsInputCheckbox(TagHelperAttributeList attributes) + protected virtual bool IsInputCheckbox(TagHelperContext context, TagHelperOutput output, TagHelperAttributeList attributes) { return attributes.Any(a => a.Value != null && a.Name == "type" && a.Value.ToString() == "checkbox"); } - protected virtual string GetLabelAsHtml(TagHelperOutput inputTag, bool isCheckbox) + protected virtual string GetLabelAsHtml(TagHelperContext context, TagHelperOutput output, TagHelperOutput inputTag, bool isCheckbox) { if (string.IsNullOrEmpty(TagHelper.Label) && string.IsNullOrEmpty(TagHelper.AspFor.Metadata.DisplayName)) { @@ -116,11 +116,11 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form var checkboxClass = isCheckbox ? "class=\"form-check-label\" " : ""; return ""; } - protected virtual string GetLabelValue() + protected virtual string GetLabelValue(TagHelperContext context, TagHelperOutput output) { return string.IsNullOrEmpty(TagHelper.Label) ? TagHelper.AspFor.Metadata.DisplayName : diff --git a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs index 8782f66b3c..287ccdac02 100644 --- a/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs +++ b/src/Volo.Abp.AspNetCore.Mvc.UI.Bootstrap/TagHelpers/Form/AbpSelectTagHelperService.cs @@ -32,7 +32,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form var order = GetInputOrder(TagHelper.AspFor.ModelExplorer); - AddGroupToFormGroupContents(context, TagHelper.AspFor.Name, SurroundInnerHtmlAndGet(innerHtml), order, out var surpress); + AddGroupToFormGroupContents(context, TagHelper.AspFor.Name, SurroundInnerHtmlAndGet(context, output, innerHtml), order, out var surpress); if (surpress) { @@ -49,28 +49,21 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form protected virtual string GetFormInputGroupAsHtml(TagHelperContext context, TagHelperOutput output) { - var selectTag = GetSelectTag(context); + var selectTag = GetSelectTag(context, output); var selectAsHtml = RenderTagHelperOutput(selectTag, _encoder); - var label = GetLabelAsHtml(selectTag); + var label = GetLabelAsHtml(context, output, selectTag); return label + Environment.NewLine + selectAsHtml; } - protected virtual string SurroundInnerHtmlAndGet(string innerHtml) + protected virtual string SurroundInnerHtmlAndGet(TagHelperContext context, TagHelperOutput output, string innerHtml) { return "
" + Environment.NewLine + innerHtml + Environment.NewLine + "
"; } - protected virtual TagHelperOutput GetSelectTag(TagHelperContext context) + protected virtual TagHelperOutput GetSelectTag(TagHelperContext context, TagHelperOutput output) { - var selectItems = TagHelper.AspItems?.ToList(); - - if (TagHelper.AspItems == null && !GetSelectItemsIfProvidedByEnum(TagHelper.AspFor.ModelExplorer, out selectItems) && !GetSelectItemsIfProvidedFromAttribute(TagHelper.AspFor.ModelExplorer, out selectItems)) - { - throw new Exception("No items provided for select attribute."); - } - - SetSelectedValue(selectItems); + var selectItems = GetSelectItems(context, output); var selectTagHelper = new SelectTagHelper(_generator) { @@ -79,14 +72,28 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form ViewContext = TagHelper.ViewContext }; - var inputTagHelperOutput = GetInnerTagHelper(new TagHelperAttributeList(), context, selectTagHelper, "select", TagMode.StartTagAndEndTag); ; + var inputTagHelperOutput = GetInnerTagHelper(new TagHelperAttributeList(), context, selectTagHelper, "select", TagMode.StartTagAndEndTag); inputTagHelperOutput.Attributes.Add("class", "form-control"); return inputTagHelperOutput; } - protected virtual string GetLabelAsHtml(TagHelperOutput selectTag) + protected virtual List GetSelectItems(TagHelperContext context, TagHelperOutput output) + { + var selectItems = TagHelper.AspItems?.ToList(); + + if (TagHelper.AspItems == null && !GetSelectItemsIfProvidedByEnum(context, output, TagHelper.AspFor.ModelExplorer, out selectItems) && !GetSelectItemsIfProvidedFromAttribute(context, output, TagHelper.AspFor.ModelExplorer, out selectItems)) + { + throw new Exception("No items provided for select attribute."); + } + + SetSelectedValue(context, output, selectItems); + + return selectItems; + } + + protected virtual string GetLabelAsHtml(TagHelperContext context, TagHelperOutput output, TagHelperOutput selectTag) { if (string.IsNullOrEmpty(TagHelper.Label) && string.IsNullOrEmpty(TagHelper.AspFor.Metadata.DisplayName)) { @@ -103,7 +110,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form TagHelper.Label; } - protected virtual bool GetSelectItemsIfProvidedByEnum(ModelExplorer explorer, out List selectItems) + protected virtual bool GetSelectItemsIfProvidedByEnum(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List selectItems) { selectItems = explorer.Metadata.IsEnum ? explorer.ModelType.GetTypeInfo().GetMembers(BindingFlags.Public | BindingFlags.Static) .Select((t, i) => new SelectListItem { Value = i.ToString(), Text = t.Name }).ToList() : null; @@ -111,16 +118,16 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form return selectItems != null; } - protected virtual bool GetSelectItemsIfProvidedFromAttribute(ModelExplorer explorer, out List selectItems) + protected virtual bool GetSelectItemsIfProvidedFromAttribute(TagHelperContext context, TagHelperOutput output, ModelExplorer explorer, out List selectItems) { selectItems = GetAttribute(explorer)?.GetItems(explorer, explorer.Model.ToString())?.ToList(); return selectItems != null; } - protected virtual void SetSelectedValue(List selectItems) + protected virtual void SetSelectedValue(TagHelperContext context, TagHelperOutput output, List selectItems) { - var selectedValue = GetSelectedValue(); + var selectedValue = GetSelectedValue(context, output); if (!selectItems.Any(si => si.Selected)) { @@ -133,7 +140,7 @@ namespace Volo.Abp.AspNetCore.Mvc.UI.Bootstrap.TagHelpers.Form } } - protected virtual string GetSelectedValue() + protected virtual string GetSelectedValue(TagHelperContext context, TagHelperOutput output) { if (TagHelper.AspFor.ModelExplorer.Metadata.IsEnum) {