Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper functions for working with html controls #1198

Merged
merged 1 commit into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 57 additions & 1 deletion src/Framework/Framework/Controls/DotvvmBindableObjectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static TControl SetAttribute<TControl>(this TControl control, string attr

if (value is not null)
{
control.Attributes[attribute] = value;
control.Attributes.Set(attribute, value);
}
else
{
Expand All @@ -153,6 +153,62 @@ public static TControl SetAttribute<TControl>(this TControl control, string attr
return SetAttribute(control, attribute, value?.UnwrapToObject());
}

/// <summary> Appends a value into the specified html attribute. If the attribute already exists, the old and new values are merged. Returns <paramref name="control"/> for fluent API usage. </summary>
public static TControl AddAttribute<TControl>(this TControl control, string attribute, object? value)
where TControl : IControlWithHtmlAttributes
{
if (value is not null)
control.Attributes.Add(attribute, value);
return control;
}
/// <summary> Appends a value into the specified html attribute. If the attribute already exists, the old and new values are merged. Returns <paramref name="control"/> for fluent API usage. </summary>
public static TControl AddAttribute<TControl, TValue>(this TControl control, string attribute, ValueOrBinding<TValue>? value)
where TControl : IControlWithHtmlAttributes
{
return AddAttribute(control, attribute, value?.UnwrapToObject());
}

/// <summary> Appends a list of css attributes to the control. If the attributes already exist, the old and new values are merged. Returns <paramref name="control"/> for fluent API usage. </summary>
public static TControl AddAttributes<TControl, TValue>(this TControl control, IEnumerable<KeyValuePair<string, TValue>> attributes)
where TControl : IControlWithHtmlAttributes
{
foreach (var a in attributes)
AddAttribute(control, a.Key, a.Value);
return control;
}

/// <summary> Appends a list of css attributes to the control. If the attributes already exist, the old and new values are merged. Returns <paramref name="control"/> for fluent API usage. </summary>
public static TControl AddAttributes<TControl, TValue>(this TControl control, VirtualPropertyGroupDictionary<TValue> attributes)
where TControl : IControlWithHtmlAttributes
{
foreach (var a in attributes.RawValues)
AddAttribute(control, a.Key, a.Value);
return control;
}

/// <summary> Appends a css class to this control. Note that it is currently not supported if multiple bindings would have to be joined together. Returns <paramref name="control"/> for fluent API usage. </summary>
public static TControl AddCssClass<TControl>(this TControl control, ValueOrBinding<string> className)
where TControl : IControlWithHtmlAttributes
{
return AddAttribute(control, "class", className.UnwrapToObject());
}

/// <summary> Appends a list of css classes to this control. Returns <paramref name="control"/> for fluent API usage. </summary>
public static TControl AddCssClasses<TControl>(this TControl control, params string[] classes)
where TControl : IControlWithHtmlAttributes
{
if (classes is null || classes.Length == 0)
return control;
return AddCssClass(control, string.Join(" ", classes));
}

/// <summary> Adds a css inline style - the `style` attribute. Returns <paramref name="control"/> for fluent API usage. </summary>
public static TControl AddCssStyle<TControl>(this TControl control, string name, string styleValue)
where TControl : IControlWithHtmlAttributes
{
return AddAttribute(control, "style", name + ":" + styleValue);
}

/// <summary> Sets all properties from the capability into this control. If the control does not support the capability, exception is thrown. Returns <paramref name="control"/> for fluent API usage. </summary>
public static TControl SetCapability<TControl, TCapability>(this TControl control, [AllowNull] TCapability capability, string prefix = "")
where TControl: DotvvmBindableObject
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/Framework/Controls/DotvvmControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected void AddDotvvmUniqueIdAttribute()
{
throw new DotvvmControlException(this, "Postback.Update can not be set on property which don't render html attributes.");
}
htmlAttributes.Attributes["data-dotvvm-id"] = GetDotvvmUniqueId();
htmlAttributes.Attributes.Set("data-dotvvm-id", GetDotvvmUniqueId());
}

protected struct RenderState
Expand Down
2 changes: 0 additions & 2 deletions src/Framework/Framework/Controls/HtmlGenericControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ public HtmlCapability HtmlCapability
/// </summary>
protected virtual bool RendersHtmlTag => TagName is object;

IDictionary<string, object?> IControlWithHtmlAttributes.Attributes => this.Attributes;

protected new struct RenderState
{
public object? Visible;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DotVVM.Framework.Binding;

namespace DotVVM.Framework.Controls
{
Expand All @@ -9,8 +10,6 @@ namespace DotVVM.Framework.Controls
/// </summary>
public interface IControlWithHtmlAttributes
{

IDictionary<string, object?> Attributes { get; }

VirtualPropertyGroupDictionary<object?> Attributes { get; }
}
}
8 changes: 3 additions & 5 deletions src/Tests/ControlTests/CompositeControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public class RepeatedButton: CompositeControl
string additionalCssClass = "my-repeated-button"
)
{
var r = new Repeater() {
return new Repeater() {
RenderAsNamedTemplate = false,
WrapperTagName = wrapperTagName,
ItemTemplate = new DelegateTemplate(_ =>
Expand All @@ -203,10 +203,8 @@ public class RepeatedButton: CompositeControl
)
}
.SetProperty(Repeater.DataSourceProperty, dataSource)
.SetCapability(html);

r.Attributes.Add("class", additionalCssClass);
return r;
.SetCapability(html)
.AddCssClass(additionalCssClass);
}
}

Expand Down