Skip to content

Commit

Permalink
New AddTemplateDecorator control
Browse files Browse the repository at this point in the history
The decorator appends or prepends content to the decorated control.

It can be used to add additional rows to GridView
  • Loading branch information
exyi committed Jan 13, 2024
1 parent 5b6b6a3 commit 817d6bd
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/Framework/Framework/Controls/AddTemplateDecorator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using DotVVM.Framework.Hosting;
using DotVVM.Framework.Binding;

namespace DotVVM.Framework.Controls
{
/// <summary> Adds a template before or after the decorated control. </summary>
[ControlMarkupOptions(AllowContent = false)]
public class AddTemplateDecorator: Decorator
{
/// <summary> Template is rendered after the decorated control. </summary>
[MarkupOptions(MappingMode = MappingMode.InnerElement)]
public ITemplate After
{
get => (ITemplate)GetValue(AfterProperty)!;
set => SetValue(AfterProperty, value);
}
public static readonly DotvvmProperty AfterProperty =
DotvvmProperty.Register<ITemplate, AddTemplateDecorator>(nameof(After));

/// <summary> Template is rendered before the decorated control. </summary>
[MarkupOptions(MappingMode = MappingMode.InnerElement)]
public ITemplate Before
{
get => (ITemplate)GetValue(BeforeProperty)!;
set => SetValue(BeforeProperty, value);
}
public static readonly DotvvmProperty BeforeProperty =
DotvvmProperty.Register<ITemplate, AddTemplateDecorator>(nameof(Before));

protected internal override void OnInit(IDotvvmRequestContext context)
{
var after = this.After;
var before = this.Before;

if (after is {})
{
Children.Add(new TemplateHost(after));
}
if (before is {})
{
Children.Insert(0, new TemplateHost(before));
}
}
}
}
34 changes: 34 additions & 0 deletions src/Tests/ControlTests/GridViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,40 @@ public async Task RequiredResourceInEditTemplate()

check.CheckString(r.FormattedHtml, fileExtension: "html");
}
[TestMethod]
public async Task GridView_RowDecorators_AddChildren()
{
var r = await cth.RunPage(typeof(BasicTestViewModel), """
<dot:GridView DataSource={value: Customers} RenderSettings.Mode=Client>
<RowDecorators>
<dot:Decorator Class-enabled="{value: Enabled}" />
<dot:AddTemplateDecorator>
<Before>
<tr colspan=3 IncludeInPage={value: Enabled}>
<td>
˯˯˯ enabled customer ˯˯˯
</td>
</tr>
</Before>
<After>
<tr colspan=3 IncludeInPage={value: Enabled}>
<td>
˄˄˄ enabled customer ˄˄˄
</td>
</tr>
</After>
</dot:AddTemplateDecorator>
</RowDecorators>
<Columns>
<dot:GridViewTextColumn HeaderText=Name ValueBinding={value: Name} />
</Columns>
</dot:GridView>
""");

check.CheckString(r.FormattedHtml, fileExtension: "html");

Check failure on line 154 in src/Tests/ControlTests/GridViewTests.cs

View workflow job for this annotation

GitHub Actions / .NET unit tests (windows-2022)

GridView_RowDecorators_AddChildren

Test method DotVVM.Framework.Tests.ControlTests.GridViewTests.GridView_RowDecorators_AddChildren threw exception: System.Exception: Check GridViewTests.GridView_RowDecorators_AddChildren.html - the file is probably untracked in git
}

public class BasicTestViewModel: DotvvmViewModelBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<html>
<head></head>
<body>

<!-- ko if: Customers()?.Items()?.length -->
<table data-bind="dotvvm-gridviewdataset: {'mapping':{},'dataSet':Customers()}">
<thead>
<tr>
<th>
<span>Name</span>
</th>
</tr>
</thead>
<tbody data-bind="foreach: Customers()?.Items">

<!-- ko if: Enabled -->
<tr colspan="3">
<td>
˯˯˯ enabled customer ˯˯˯
</td>
</tr>
<!-- /ko -->
<tr data-bind="css: { enabled: Enabled }">
<td>
<span data-bind="text: Name"></span>
</td>
</tr>

<!-- ko if: Enabled -->
<tr colspan="3">
<td>
˄˄˄ enabled customer ˄˄˄
</td>
</tr>
<!-- /ko -->
</tbody>
</table>
<!-- /ko -->
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@
"isCompileTimeOnly": true
}
},
"DotVVM.Framework.Controls.AddTemplateDecorator": {
"After": {
"type": "DotVVM.Framework.Controls.ITemplate, DotVVM.Framework",
"mappingMode": "InnerElement"
},
"Before": {
"type": "DotVVM.Framework.Controls.ITemplate, DotVVM.Framework",
"mappingMode": "InnerElement"
}
},
"DotVVM.Framework.Controls.AuthenticatedView": {
"AuthenticatedTemplate": {
"type": "DotVVM.Framework.Controls.ITemplate, DotVVM.Framework",
Expand Down Expand Up @@ -1971,6 +1981,11 @@
"assembly": "DotVVM.Framework",
"baseType": "DotVVM.Framework.Controls.DotvvmControl, DotVVM.Framework"
},
"DotVVM.Framework.Controls.AddTemplateDecorator": {
"assembly": "DotVVM.Framework",
"baseType": "DotVVM.Framework.Controls.Decorator, DotVVM.Framework",
"withoutContent": true
},
"DotVVM.Framework.Controls.AuthenticatedView": {
"assembly": "DotVVM.Framework",
"baseType": "DotVVM.Framework.Controls.ConfigurableHtmlControl, DotVVM.Framework",
Expand Down

0 comments on commit 817d6bd

Please sign in to comment.