public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { FormGroupBuilder formGroupBuilder = new FormGroupBuilder(_htmlGenerator, ViewContext, _encoder, _htmlHelper); TweakingConfiguration tweakingConfig = new TweakingConfiguration(); context.Items.Add("TweakingConfig", tweakingConfig); await output.GetChildContentAsync(); StringBuilder builder = new StringBuilder(); foreach (ModelExplorer prop in Model.ModelExplorer.Properties) { builder.Append(await formGroupBuilder.GetFormGroup(prop, tweakingConfig)); } if (string.IsNullOrEmpty(SubmitButtonText)) { SubmitButtonText = "Create"; } builder.Append($@"<div class=""form-group""> <button type=""submit"" class=""{mergeClasses(SubmitButtonClasses)}"">{SubmitButtonText}</button> </div>"); output.TagName = "form"; output.Attributes.Add(new TagHelperAttribute("method", "post")); output.Attributes.Add(new TagHelperAttribute("action", TargetAction)); output.Attributes.Add(new TagHelperAttribute("autocomplete", "off")); output.Content.SetHtmlContent(builder.ToString()); }
private async Task <string> buildRadioInputsHtml(ModelExplorer property, IEnumerable <SelectListItem> items, TweakingConfiguration tweakingConfig) { StringBuilder inputs = new StringBuilder(); foreach (var item in items) { inputs.Append($"<br>{await buildInputHtml(property, tweakingConfig, "radio", item.Value, item.Selected)} <span>{item.Text}</span>"); } return(inputs.ToString()); }
private async Task <string> buildInputHtml(ModelExplorer property, TweakingConfiguration tweakingConfig, string inputType = "", string inputValue = "", bool radioChecked = false) { PropertyTweakingConfiguration propertyConfig = tweakingConfig.GetByPropertyFullName(property.GetFullName()); if (propertyConfig == null || string.IsNullOrEmpty(propertyConfig.InputTemplatePath)) { InputTagHelper input = new InputTagHelper(_htmlGenerator) { For = new ModelExpression(property.GetFullName(), property), ViewContext = _viewContext }; var attrs = new TagHelperAttributeList { new TagHelperAttribute("class", $"form-control {propertyConfig?.InputClasses}") }; if (!string.IsNullOrEmpty(inputType)) { input.InputTypeName = inputType; input.Value = inputValue; // Setting the Type attributes requires providing an initialized // AttributeList with the type attribute attrs = new TagHelperAttributeList() { new TagHelperAttribute("class", $"{propertyConfig?.InputClasses}"), new TagHelperAttribute("type", inputType), new TagHelperAttribute("value", inputValue) }; if (radioChecked) { attrs.Add(new TagHelperAttribute("checked", "checked")); } } AutoCompleteAttribute autoComplete; if (property.HasAutoComplete(out autoComplete)) { attrs.AddClass("autocomplete"); attrs.Add(getAutoCompleteDataAttribute(autoComplete, property.Container)); } return(await GetGeneratedContentFromTagHelper("input", TagMode.SelfClosing, input, attributes : attrs )); } else { return(renderInputTemplate(property, propertyConfig.InputTemplatePath)); } }
private async Task <string> _getFormGroupForSimpleProperty(ModelExplorer property, TweakingConfiguration tweakingConfig) { string label = await buildLabelHtml(property, tweakingConfig); string input = await buildInputHtml(property, tweakingConfig); string validation = await buildValidationMessageHtml(property, tweakingConfig); return($@"<div class='form-group'> {label} {input} {validation} </div>"); }
public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { TweakingConfiguration overridingConfig = context.Items["TweakingConfig"] as TweakingConfiguration; overridingConfig.Add(new PropertyTweakingConfiguration { Property = TweakedProperty, InputTemplatePath = InputTemplatePath, LabelClasses = LabelClasses, InputClasses = InputClasses, ValidationClasses = ValidationClasses }); output.SuppressOutput(); return(Task.CompletedTask); }
private async Task <string> buildLabelHtml(ModelExplorer property, TweakingConfiguration tweakingConfig) { TagHelper label = new LabelTagHelper(_htmlGenerator) { For = new ModelExpression(property.GetFullName(), property), ViewContext = _viewContext }; PropertyTweakingConfiguration propertyConfig = tweakingConfig .GetByPropertyFullName(property.GetFullName()); return(await GetGeneratedContentFromTagHelper( "label", TagMode.StartTagAndEndTag, label, new TagHelperAttributeList() { new TagHelperAttribute("class", propertyConfig?.LabelClasses) })); }
private async Task <string> _getFormGroupsForComplexProperty(ModelExplorer property, TweakingConfiguration config) { StringBuilder builder = new StringBuilder(); string label = await buildLabelHtml(property, config); foreach (var prop in property.Properties) { builder.Append(await GetFormGroup(prop, config)); } return($@"<div class='form-group'> {label} <div class=""sub-form-group""> {builder.ToString()} </div> </div>"); }
public Task <string> GetFormGroup(ModelExplorer property, TweakingConfiguration tweakingConfig) { if (!property.IsReadOnly()) { ItemsSourceAttribute itemSource; if (property.HasValueFromSelectList(out itemSource)) { return(_getFormGroupForSelectList(property, itemSource.GetItems(property.Container), itemSource.ChoicesType, tweakingConfig)); } else if (property.ModelType.IsSimpleType()) { return(_getFormGroupForSimpleProperty(property, tweakingConfig)); } else { return(_getFormGroupsForComplexProperty(property, tweakingConfig)); } } else { return(Task.Run(() => String.Empty)); } }
private async Task <string> _getFormGroupForSelectList(ModelExplorer property, IEnumerable <SelectListItem> items, ChoicesTypes choicesType, TweakingConfiguration tweakingConfig) { string label = await buildLabelHtml(property, tweakingConfig); string select = ""; if (choicesType == ChoicesTypes.RADIO) { select = await buildRadioInputsHtml(property, items, tweakingConfig); } else { select = await buildSelectHtml(property, items, tweakingConfig); } string validation = await buildValidationMessageHtml(property, tweakingConfig); return($@"<div class='form-group'> {label} {select} {validation} </div>"); }
private async Task <string> buildValidationMessageHtml(ModelExplorer property, TweakingConfiguration tweakingConfig) { PropertyTweakingConfiguration propertyConfig = tweakingConfig.GetByPropertyFullName(property.GetFullName()); TagHelper validationMessage = new ValidationMessageTagHelper(_htmlGenerator) { For = new ModelExpression(property.GetFullName(), property), ViewContext = _viewContext }; return(await GetGeneratedContentFromTagHelper("span", TagMode.StartTagAndEndTag, validationMessage, new TagHelperAttributeList() { new TagHelperAttribute("class", propertyConfig?.ValidationClasses) })); }
private async Task <string> buildSelectHtml(ModelExplorer property, IEnumerable <SelectListItem> items, TweakingConfiguration tweakingConfig) { TagHelper select = new SelectTagHelper(_htmlGenerator) { For = new ModelExpression(property.GetFullName(), property), ViewContext = _viewContext, Items = items }; return(await GetGeneratedContentFromTagHelper("select", TagMode.StartTagAndEndTag, select, new TagHelperAttributeList { new TagHelperAttribute("class", "form-control dropdown") })); }