public static RenderFragment RenderHtmlField(this BlazorItemField <string> htmlField, string tag, IDictionary <string, object>?htmlAttributes) => builder => { if (htmlField?.Value == null) { return; } if (string.IsNullOrWhiteSpace(tag)) { tag = "p"; } builder.OpenElement(0, tag); if (htmlAttributes != null) { builder.AddMultipleAttributes(1, htmlAttributes); } builder.AddMarkupContent(2, htmlField.Value); builder.CloseElement(); };
public static RenderFragment RenderLinkField(this BlazorItemField<BlazorFieldValueLink> linkField, IDictionary<string, object>? htmlAttributes) => builder => { if(linkField?.Value == null) return; var anchorAttrs = new Dictionary<string, object> { { "href", linkField.Value.Href }, { "class", linkField.Value.Class }, { "title", linkField.Value.Text }, { "target", linkField.Value.Target } }; if (htmlAttributes != null && ("_blank".Equals(anchorAttrs["target"]?.ToString(), StringComparison.InvariantCultureIgnoreCase) && !htmlAttributes.ContainsKey("rel"))) { // information disclosure attack prevention keeps target blank site from getting ref to window.opener anchorAttrs["rel"] = "noopener noreferrer"; } builder.OpenElement(0, "a"); builder.AddMultipleAttributes(1, anchorAttrs); if (htmlAttributes != null) builder.AddMultipleAttributes(1, htmlAttributes); if (!string.IsNullOrWhiteSpace(linkField.Value.Text)) builder.AddContent(2, linkField.Value.Text); builder.CloseElement(); };
public static RenderFragment RenderImageField(this BlazorItemField<BlazorFieldValueImage> imageField, ImageSizeParameters? imageParams, IDictionary<string, object>? htmlAttributes, IEnumerable<ImageSizeParameters>? srcSet) => builder => { if (imageField?.Value == null) return; var imageAttributes = GetImageAttributes(imageField.Value.Src, srcSet, htmlAttributes, imageParams); if (imageAttributes == null) return; builder.OpenElement(0, "img"); builder.AddMultipleAttributes(1, imageAttributes); builder.CloseElement(); };
private IBlazorItemField CreateBlazorItemField <T>(KeyValuePair <string, BlazorRouteField> blazorRouteField) { if (blazorRouteField.Value == null) { return(null); } BlazorItemField <T> field = null; T fieldValue = default; try { switch (blazorRouteField.Value.Type) { case FieldTypes.HtmlField: case FieldTypes.PlainTextField: case FieldTypes.CheckboxField: fieldValue = (T)Convert.ChangeType(blazorRouteField.Value.Value.ToString(), typeof(T)); break; default: fieldValue = JsonSerializer.Deserialize <T>(blazorRouteField.Value.Value.ToString()); break; } field = new BlazorItemField <T> { FieldName = blazorRouteField.Key, Value = fieldValue, Editable = blazorRouteField.Value?.Editable, Type = blazorRouteField.Value?.Type }; } catch (Exception ex) { Console.WriteLine($"Error creating field {blazorRouteField.Value.Value}. Error { ex.Message}"); } return(field); }
private IBlazorItemField CreateComplexBlazorItemField <T>(KeyValuePair <string, BlazorRouteField> blazorRouteField) where T : class { if (blazorRouteField.Value == null) { return(null); } BlazorItemField <T> blazorItemField = null; T fieldValue = default; try { switch (blazorRouteField.Value.Type) { case FieldTypes.MultiListField: BlazorFieldValueMultiList fieldValueMultiList = new BlazorFieldValueMultiList() { Values = new List <BlazorFieldValueMultiListItem>() }; foreach (var item in blazorRouteField.Value.Values) { if (item == null || string.IsNullOrWhiteSpace(item.ToString())) { continue; } BlazorFieldValueMultiListItem multiListItem = new BlazorFieldValueMultiListItem() { Id = item.Id, Url = item.Url }; var fields = CreateBlazorItemFields(item.Fields); multiListItem.BlazorItemFields = fields; fieldValueMultiList.Values.Add(multiListItem); } fieldValue = fieldValueMultiList as T; break; default: break; } blazorItemField = new BlazorItemField <T> { FieldName = blazorRouteField.Key, Value = fieldValue, Editable = blazorRouteField.Value?.Editable, Type = blazorRouteField.Value?.Type }; } catch (Exception ex) { Console.WriteLine($"Error creating complex field {fieldValue?.GetType()}. Error { ex.Message}"); } return(blazorItemField); }