/// <summary> /// Wraps the template for a single property in a div. The label and validation message for the /// property are also added /// </summary> /// <param name="htmlHelper">The HTML helper.</param> /// <param name="propertyMetadata">The property metadata.</param> /// <param name="template">The template.</param> protected override IHtmlContent WrapProperty(IHtmlHelper htmlHelper, ModelMetadata propertyMetadata, IHtmlContent template) { Guard.NotNull(htmlHelper, nameof(htmlHelper)); Guard.NotNull(propertyMetadata, nameof(propertyMetadata)); var builder = new HtmlContentBuilder(); var label = htmlHelper.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: null); if (!IsEmpty(label)) { var labelTag = HtmlTag.Division; labelTag.AddCssClass("editor-label"); labelTag.InnerHtml.SetHtmlContent(label); builder.AppendLine(labelTag); } var valueDivTag = HtmlTag.Division; valueDivTag.AddCssClass("editor-field"); valueDivTag.InnerHtml.AppendHtml(template); valueDivTag.InnerHtml.AppendHtml(" "); valueDivTag.InnerHtml.AppendHtml(htmlHelper.ValidationMessage( propertyMetadata.PropertyName, message: null, htmlAttributes: null, tag: null)); builder.AppendLine(valueDivTag); return(builder); }
public static IHtmlContent ReCaptchaV2(string siteKey, string size, string theme, string language, string callback, string errorCallback, string expiredCallback) { var content = new HtmlContentBuilder(); content.AppendFormat(@"<div class=""g-recaptcha"" data-sitekey=""{0}""", siteKey); if (!string.IsNullOrEmpty(size)) { content.AppendFormat(@" data-size=""{0}""", size); } if (!string.IsNullOrEmpty(theme)) { content.AppendFormat(@" data-theme=""{0}""", theme); } if (!string.IsNullOrEmpty(callback)) { content.AppendFormat(@" data-callback=""{0}""", callback); } if (!string.IsNullOrEmpty(errorCallback)) { content.AppendFormat(@" data-error-callback=""{0}""", errorCallback); } if (!string.IsNullOrEmpty(expiredCallback)) { content.AppendFormat(@" data-expired-callback=""{0}""", expiredCallback); } content.AppendFormat("></div>"); content.AppendLine(); content.AppendFormat(@"<script src=""https://www.google.com/recaptcha/api.js?hl={0}"" defer></script>", language); return(content); }
private IHtmlContent CreateContestJson(IDictionary <int, string> contestYears) { var script = new HtmlContentBuilder(); script.AppendHtmlLine("<script> if (contest_years == undefined) { var contest_years = {"); foreach (var pair in contestYears) { script.AppendLine($"{pair.Key}: {pair.Value},"); } script.AppendHtmlLine("}; } </script>"); return(script); }
public static IHtmlContent ReCaptchaV2Invisible(string siteKey, string text, string className, string language, string callback, string badge) { var content = new HtmlContentBuilder(); content.AppendFormat(@"<button class=""g-recaptcha {0}""", className); content.AppendFormat(@" data-sitekey=""{0}""", siteKey); content.AppendFormat(@" data-callback=""{0}""", callback); content.AppendFormat(@" data-badge=""{0}""", badge); content.AppendFormat(@">{0}</button>", text); content.AppendLine(); content.AppendFormat(@"<script src=""https://www.google.com/recaptcha/api.js?hl={0}"" defer></script>", language); return(content); }
public IHtmlContent CachedShapeWrapper(IShape Shape) { // No need to optimize this code as it will be used for debugging purpose var contentBuilder = new HtmlContentBuilder(); var metadata = Shape.Metadata; var cache = metadata.Cache(); contentBuilder.AppendLine(); contentBuilder.AppendHtmlLine($"<!-- CACHED SHAPE: {cache.CacheId} ({Guid.NewGuid()})"); contentBuilder.AppendHtmlLine($" VARY BY: {String.Join(", ", cache.Contexts)}"); contentBuilder.AppendHtmlLine($" DEPENDENCIES: {String.Join(", ", cache.Tags)}"); contentBuilder.AppendHtmlLine($" EXPIRES ON: {cache.ExpiresOn}"); contentBuilder.AppendHtmlLine($" EXPIRES AFTER: {cache.ExpiresAfter}"); contentBuilder.AppendHtmlLine($" EXPIRES SLIDING: {cache.ExpiresSliding}"); contentBuilder.AppendHtmlLine("-->"); contentBuilder.AppendHtml(metadata.ChildContent); contentBuilder.AppendLine(); contentBuilder.AppendHtmlLine($"<!-- END CACHED SHAPE: {cache.CacheId} -->"); return(contentBuilder); }
/// <summary> /// Wraps the template for a single property in a div and adds a label /// </summary> /// <param name="htmlHelper">The HTML helper.</param> /// <param name="propertyMetadata">The property metadata.</param> /// <param name="template">The template.</param> protected override IHtmlContent WrapProperty(IHtmlHelper htmlHelper, ModelMetadata propertyMetadata, IHtmlContent template) { Guard.NotNull(propertyMetadata, nameof(propertyMetadata)); var builder = new HtmlContentBuilder(); var label = propertyMetadata.GetDisplayName(); if (!string.IsNullOrEmpty(label)) { var labelTag = HtmlTag.Division; labelTag.InnerHtml.SetContent(label); labelTag.AddCssClass("display-label"); builder.AppendLine(labelTag); } var valueDivTag = HtmlTag.Division; valueDivTag.AddCssClass("display-field"); valueDivTag.InnerHtml.SetHtmlContent(template); builder.AppendLine(valueDivTag); return(builder); }
public static IHtmlContent ReCaptchaV3(string siteKey, string action, string language, string callBack) { var content = new HtmlContentBuilder(); content.AppendHtml(@"<input id=""g-recaptcha-response"" name=""g-recaptcha-response"" type=""hidden"" value="""" />"); content.AppendFormat(@"<script src=""https://www.google.com/recaptcha/api.js?render={0}&hl={1}""></script>", siteKey, language); content.AppendHtml("<script>"); content.AppendHtml("function updateReCaptcha() {"); content.AppendFormat("grecaptcha.execute('{0}', {{action: '{1}'}}).then(function(token){{", siteKey, action); content.AppendHtml("document.getElementById('g-recaptcha-response').value = token;"); content.AppendHtml("});"); content.AppendHtml("}"); content.AppendHtml("grecaptcha.ready(function() {setInterval(updateReCaptcha, 100000); updateReCaptcha()});"); content.AppendHtml("</script>"); content.AppendLine(); return(content); }
public static async Task <IHtmlContent> BlockContainerAsync(this IViewComponentHelper component, Dictionary <string, List <BlockModel> > containers, string containerId) { var builder = new HtmlContentBuilder(); if (containers == null || !containers.TryGetValue(containerId, out var blocks) || blocks.Count == 0) { return(builder); } foreach (var blockModel in blocks) { var fieldTemplate = _fieldTemplateService.Value.Get <BlockFieldTemplate>(blockModel.FieldTemplateSystemId); if (string.IsNullOrWhiteSpace(fieldTemplate?.TemplatePath) || fieldTemplate.TemplatePath.IndexOf("MVC:", StringComparison.OrdinalIgnoreCase) == -1) { throw new InvalidOperationException("Could not find template for block."); } var templateDefaults = fieldTemplate.TemplatePath.Split(':').Skip(1).ToArray(); if (templateDefaults.Length < 1) { throw new InvalidOperationException("Could not find template for block."); } var controllerType = Type.GetType(templateDefaults[0], true, true); if (controllerType == null) { throw new InvalidOperationException("Could not find template for block."); } var tagBuilder = new TagBuilder("section"); tagBuilder.Attributes["data-litium-block-id"] = blockModel.SystemId.ToString(); tagBuilder.InnerHtml.AppendHtml(await component.InvokeAsync(controllerType, blockModel)); builder.AppendHtml(tagBuilder); builder.AppendLine(); } return(builder); }
public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var templateInfo = viewData.TemplateInfo; var modelExplorer = viewData.ModelExplorer; if (templateInfo.TemplateDepth > 1) { if (modelExplorer.Model == null) { return new HtmlString(modelExplorer.Metadata.NullDisplayText); } var text = modelExplorer.GetSimpleDisplayText(); if (modelExplorer.Metadata.HtmlEncode) { return new StringHtmlContent(text); } return new HtmlString(text); } var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices; var viewEngine = serviceProvider.GetRequiredService<ICompositeViewEngine>(); var viewBufferScope = serviceProvider.GetRequiredService<IViewBufferScope>(); var content = new HtmlContentBuilder(); foreach (var propertyExplorer in modelExplorer.Properties) { var propertyMetadata = propertyExplorer.Metadata; if (!ShouldShow(propertyExplorer, templateInfo)) { continue; } var templateBuilder = new TemplateBuilder( viewEngine, viewBufferScope, htmlHelper.ViewContext, htmlHelper.ViewData, propertyExplorer, htmlFieldName: propertyMetadata.PropertyName, templateName: null, readOnly: false, additionalViewData: null); var templateBuilderResult = templateBuilder.Build(); if (!propertyMetadata.HideSurroundingHtml) { var label = htmlHelper.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: null); if (!string.IsNullOrEmpty(label.ToString())) { var labelTag = new TagBuilder("div"); labelTag.AddCssClass("editor-label"); labelTag.InnerHtml.SetContent(label); content.AppendLine(labelTag); } var valueDivTag = new TagBuilder("div"); valueDivTag.AddCssClass("editor-field"); valueDivTag.InnerHtml.AppendHtml(templateBuilderResult); valueDivTag.InnerHtml.AppendHtml(" "); valueDivTag.InnerHtml.AppendHtml(htmlHelper.ValidationMessage( propertyMetadata.PropertyName, message: null, htmlAttributes: null, tag: null)); content.AppendLine(valueDivTag); } else { content.AppendHtml(templateBuilderResult); } } return content; }
public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var templateInfo = viewData.TemplateInfo; var modelExplorer = viewData.ModelExplorer; if (modelExplorer.Model == null) { return(new HtmlString(modelExplorer.Metadata.NullDisplayText)); } if (templateInfo.TemplateDepth > 1) { var text = modelExplorer.GetSimpleDisplayText(); if (modelExplorer.Metadata.HtmlEncode) { text = htmlHelper.Encode(text); } return(new HtmlString(text)); } var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices; var viewEngine = serviceProvider.GetRequiredService <ICompositeViewEngine>(); var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>(); var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count); foreach (var propertyExplorer in modelExplorer.Properties) { var propertyMetadata = propertyExplorer.Metadata; if (!ShouldShow(propertyExplorer, templateInfo)) { continue; } var templateBuilder = new TemplateBuilder( viewEngine, viewBufferScope, htmlHelper.ViewContext, htmlHelper.ViewData, propertyExplorer, htmlFieldName: propertyMetadata.PropertyName, templateName: null, readOnly: true, additionalViewData: null); var templateBuilderResult = templateBuilder.Build(); if (!propertyMetadata.HideSurroundingHtml) { var label = propertyMetadata.GetDisplayName(); if (!string.IsNullOrEmpty(label)) { var labelTag = new TagBuilder("div"); labelTag.InnerHtml.SetContent(label); labelTag.AddCssClass("display-label"); content.AppendLine(labelTag); } var valueDivTag = new TagBuilder("div"); valueDivTag.AddCssClass("display-field"); valueDivTag.InnerHtml.SetHtmlContent(templateBuilderResult); content.AppendLine(valueDivTag); } else { content.AppendHtml(templateBuilderResult); } } return(content); }
public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper) { var viewData = htmlHelper.ViewData; var templateInfo = viewData.TemplateInfo; var modelExplorer = viewData.ModelExplorer; if (templateInfo.TemplateDepth > 1) { if (modelExplorer.Model == null) { return(new HtmlString(modelExplorer.Metadata.NullDisplayText)); } var text = modelExplorer.GetSimpleDisplayText(); if (modelExplorer.Metadata.HtmlEncode) { return(new StringHtmlContent(text)); } return(new HtmlString(text)); } var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices; var viewEngine = serviceProvider.GetRequiredService <ICompositeViewEngine>(); var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>(); var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count); foreach (var propertyExplorer in modelExplorer.Properties) { var propertyMetadata = propertyExplorer.Metadata; if (!ShouldShow(propertyExplorer, templateInfo)) { continue; } var templateBuilder = new TemplateBuilder( viewEngine, viewBufferScope, htmlHelper.ViewContext, htmlHelper.ViewData, propertyExplorer, htmlFieldName: propertyMetadata.PropertyName, templateName: null, readOnly: false, additionalViewData: null); var templateBuilderResult = templateBuilder.Build(); if (!propertyMetadata.HideSurroundingHtml) { var label = htmlHelper.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: null); using (var writer = new HasContentTextWriter()) { label.WriteTo(writer, PassThroughHtmlEncoder.Default); if (writer.HasContent) { var labelTag = new TagBuilder("div"); labelTag.AddCssClass("editor-label"); labelTag.InnerHtml.SetHtmlContent(label); content.AppendLine(labelTag); } } var valueDivTag = new TagBuilder("div"); valueDivTag.AddCssClass("editor-field"); valueDivTag.InnerHtml.AppendHtml(templateBuilderResult); valueDivTag.InnerHtml.AppendHtml(" "); valueDivTag.InnerHtml.AppendHtml(htmlHelper.ValidationMessage( propertyMetadata.PropertyName, message: null, htmlAttributes: null, tag: null)); content.AppendLine(valueDivTag); } else { content.AppendHtml(templateBuilderResult); } } return(content); }
private IHtmlContent GenerateGroupsAndOptions( string optionLabel, IEnumerable <GdsSelectListItem> selectList, ICollection <string> currentValues) { var itemsList = selectList as IList <GdsSelectListItem>; if (itemsList == null) { itemsList = selectList.ToList(); } var count = itemsList.Count; if (optionLabel != null) { count++; } // Short-circuit work below if there's nothing to add. if (count == 0) { return(HtmlString.Empty); } var listItemBuilder = new HtmlContentBuilder(count); // Make optionLabel the first item that gets rendered. if (optionLabel != null) { listItemBuilder.AppendLine(GenerateOption( new GdsSelectListItem() { Text = optionLabel, Value = string.Empty, Selected = false, }, currentValues: null)); } // Group items in the SelectList if requested. // The worst case complexity of this algorithm is O(number of groups*n). // If there aren't any groups, it is O(n) where n is number of items in the list. var optionGenerated = new bool[itemsList.Count]; for (var i = 0; i < itemsList.Count; i++) { if (!optionGenerated[i]) { var item = itemsList[i]; var optGroup = item.Group; if (optGroup != null) { var groupBuilder = new TagBuilder("optgroup"); if (optGroup.Name != null) { groupBuilder.MergeAttribute("label", optGroup.Name); } if (optGroup.Disabled) { groupBuilder.MergeAttribute("disabled", "disabled"); } groupBuilder.InnerHtml.AppendLine(); for (var j = i; j < itemsList.Count; j++) { var groupItem = itemsList[j]; if (!optionGenerated[j] && object.ReferenceEquals(optGroup, groupItem.Group)) { groupBuilder.InnerHtml.AppendLine(GenerateOption(groupItem, currentValues)); optionGenerated[j] = true; } } listItemBuilder.AppendLine(groupBuilder); } else { listItemBuilder.AppendLine(GenerateOption(item, currentValues)); optionGenerated[i] = true; } } } return(listItemBuilder); }
public static IHtmlContent ObjectTemplate(IHtmlHelper htmlHelper) { if (htmlHelper == null) { throw new ArgumentNullException(nameof(htmlHelper)); } var viewData = htmlHelper.ViewData; var templateInfo = viewData.TemplateInfo; var modelExplorer = viewData.ModelExplorer; if (templateInfo.TemplateDepth > 1) { if (modelExplorer.Model == null) { return(new HtmlString(modelExplorer.Metadata.NullDisplayText)); } var text = modelExplorer.GetSimpleDisplayText(); if (modelExplorer.Metadata.HtmlEncode) { return(new StringHtmlContent(text)); } return(new HtmlString(text)); } var serviceProvider = htmlHelper.ViewContext.HttpContext.RequestServices; var viewEngine = serviceProvider.GetRequiredService <ICompositeViewEngine>(); var viewBufferScope = serviceProvider.GetRequiredService <IViewBufferScope>(); var content = new HtmlContentBuilder(modelExplorer.Metadata.Properties.Count); foreach (var propertyExplorer in modelExplorer.Properties) { var propertyMetadata = propertyExplorer.Metadata; if (!ShouldShow(propertyExplorer, templateInfo)) { continue; } var templateBuilder = new TemplateBuilder( viewEngine, viewBufferScope, htmlHelper.ViewContext, htmlHelper.ViewData, propertyExplorer, htmlFieldName: propertyMetadata.PropertyName, templateName: null, readOnly: false, additionalViewData: null); var templateBuilderResult = templateBuilder.Build(); if (!propertyMetadata.HideSurroundingHtml) { var groupTag = new TagBuilder("div"); var isBooleanProperty = propertyMetadata.ModelType == typeof(bool) || propertyMetadata.ModelType == typeof(bool?); if (!isBooleanProperty) { groupTag.AddCssClass("form-group"); var label = htmlHelper.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: null); using (var writer = new HasContentTextWriter()) { label.WriteTo(writer, PassThroughHtmlEncoder.Default); if (writer.HasContent) { groupTag.InnerHtml.AppendHtml(label); } } } groupTag.InnerHtml.AppendHtml(templateBuilderResult); if (isBooleanProperty) { groupTag.AddCssClass("form-check"); var label = htmlHelper.Label(propertyMetadata.PropertyName, labelText: null, htmlAttributes: new { @class = "form-check-label" }); using (var writer = new HasContentTextWriter()) { label.WriteTo(writer, PassThroughHtmlEncoder.Default); if (writer.HasContent) { groupTag.InnerHtml.AppendHtml(label); } } } content.AppendLine(groupTag); } else { content.AppendHtml(templateBuilderResult); } } return(content); }