Skip to content

Commit

Permalink
Merge pull request #1760 from riganti/add-AddTemplateDecorator
Browse files Browse the repository at this point in the history
New AddTemplateDecorator control
  • Loading branch information
tomasherceg authored Feb 8, 2024
2 parents b9e0390 + 71ee2f4 commit 33a7fec
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 1 deletion.
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 AfterTemplate
{
get => (ITemplate)GetValue(AfterTemplateProperty)!;
set => SetValue(AfterTemplateProperty, value);
}
public static readonly DotvvmProperty AfterTemplateProperty =
DotvvmProperty.Register<ITemplate, AddTemplateDecorator>(nameof(AfterTemplate));

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

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

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>
<BeforeTemplate>
<tr colspan=3 IncludeInPage={value: Enabled}>
<td>
vvv enabled customer vvv
</td>
</tr>
</BeforeTemplate>
<AfterTemplate>
<tr colspan=3 IncludeInPage={value: Enabled}>
<td>
^^^ enabled customer ^^^
</td>
</tr>
</AfterTemplate>
</dot:AddTemplateDecorator>
</RowDecorators>
<Columns>
<dot:GridViewTextColumn HeaderText=Name ValueBinding={value: Name} />
</Columns>
</dot:GridView>
""");

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

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>
vvv enabled customer vvv
</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>
2 changes: 1 addition & 1 deletion src/Tests/DotVVM.Framework.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net472'">
<ProjectReference Include="../Framework/Hosting.Owin/DotVVM.Framework.Hosting.Owin.csproj" />
<PackageReference Include="CheckTestOutput" Version="0.4.3" />
<PackageReference Include="CheckTestOutput" Version="0.6.3" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="6.0.0" />
<Compile Remove="AspCore/CachingTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,16 @@
"isCompileTimeOnly": true
}
},
"DotVVM.Framework.Controls.AddTemplateDecorator": {
"AfterTemplate": {
"type": "DotVVM.Framework.Controls.ITemplate, DotVVM.Framework",
"mappingMode": "InnerElement"
},
"BeforeTemplate": {
"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 33a7fec

Please sign in to comment.