Skip to content

Commit

Permalink
Override renderring attribute (#316)
Browse files Browse the repository at this point in the history
* just build

* BaseAttribute in progress

* Added test for presentationBase atribute

* Rename PresentationBaseAttribute to RenderTemplateOverrideAttribute

---------

Co-authored-by: blazej.kuhajda <blazej.kuhajda@mts.sk>
  • Loading branch information
Brano5 and blazej.kuhajda authored Jun 10, 2024
1 parent 23a2e2b commit f27c819
Show file tree
Hide file tree
Showing 27 changed files with 304 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// AXSharp.Abstractions
// Copyright (c) 2023 Peter Kurhajec (PTKu), MTS, and Contributors. All Rights Reserved.
// Contributors: https://github.com/ix-ax/axsharp/graphs/contributors
// See the LICENSE file in the repository root for more information.
// https://github.com/ix-ax/axsharp/blob/dev/LICENSE
// Third party licenses: https://github.com/ix-ax/axsharp/blob/master/notices.md

using System;
using System.Collections.Generic;
using System.Linq;
public class RenderTemplateOverrideAttribute : Attribute
{

public RenderTemplateOverrideAttribute(string templateOverrideName)
{
this.TemplateOverrideName = templateOverrideName;
}

public string TemplateOverrideName { get; protected set; }

}

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,44 @@
if (element == null)
return;

var overrideAttribute = AttributesHandler.GetRenderTemplateOverrideAttribute(element);

if (overrideAttribute != null)
{

if (!string.IsNullOrWhiteSpace(overrideAttribute.TemplateOverrideName))
{
var presentation = GetDisplayPresentationIfEmpty();
var pipeline = presentation.Split('-');

foreach (var item in pipeline) // apply pipeline
{
string fullPresentationType = overrideAttribute.TemplateOverrideName + GetDisplayPresentationIfEmpty() + "View";
var tcomponent = ComponentService.GetComponent(fullPresentationType);

if (tcomponent != null)
{
if (element is ITwinPrimitive)
{
@CreatePrimitiveComponent((ITwinPrimitive)element, tcomponent)
;
return;
}
if (element is ITwinObject)
{
@CreateComplexComponent((ITwinObject)element, tcomponent)
;
return;
}

}
}

}

}


// if it is primitive type, just generate
if (element is ITwinPrimitive)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,37 @@ public RenderIgnoreAttribute GetIgnoreRenderingAttribute(ITwinElement twinObject
return null;
}

public RenderTemplateOverrideAttribute GetRenderTemplateOverrideAttribute(ITwinElement twinObject)
{
if (twinObject == null) return null;

try
{
var propertyInfo = GetPropertyViaSymbol(twinObject);
if (propertyInfo != null)
{
if (propertyInfo
.GetCustomAttributes().FirstOrDefault(p => p is RenderTemplateOverrideAttribute) is RenderTemplateOverrideAttribute propertyAttribute)
{
return propertyAttribute;
}
}

var typeAttribute = twinObject
.GetType()
.GetCustomAttributes(true)
.FirstOrDefault(p => p is RenderTemplateOverrideAttribute) as RenderTemplateOverrideAttribute;

return typeAttribute;
}
catch (Exception)
{
//throw;
}

return null;
}

public PropertyInfo GetPropertyViaSymbol(ITwinElement twinObject)
{
if (twinObject == null) return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div name="testingProgram-testRenderTemplateOverrideStruct" b-gswuladdww>
<div name="testingProgram-testRenderTemplateOverrideStruct-testInteger" class="w-100 form-group mb-2">
<label for:ignore="testingProgram.testRenderTemplateOverrideStruct.testInteger_bb8cdc40-b3a2-4f63-a246-c626fdf69f49">Test Integer</label>
<input id:ignore="testingProgram.testRenderTemplateOverrideStruct.testInteger_bb8cdc40-b3a2-4f63-a246-c626fdf69f49" readonly="readonly" class="w-100 form-control " style="background-color:transparent" type="text" step="any" value="0" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-custom-class="custom-tooltip" data-bs-title="testingProgram.testRenderTemplateOverrideStruct.Test Integer" />

<div class="invalid-feedback"></div>
</div><div name="testingProgram-testRenderTemplateOverrideStruct-testString" class="w-100 form-group mb-2">
<label for:ignore="testingProgram.testRenderTemplateOverrideStruct.testString_ad32b01b-b9da-4234-86f0-14cbee7ec07b">Test String</label>
<input id:ignore="testingProgram.testRenderTemplateOverrideStruct.testString_ad32b01b-b9da-4234-86f0-14cbee7ec07b" readonly="readonly" class="w-100 form-control " style="background-color:transparent" type="text" step="any" value="" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-custom-class="custom-tooltip" data-bs-title="testingProgram.testRenderTemplateOverrideStruct.Test String" />

<div class="invalid-feedback"></div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div name="testingProgram-testRenderTemplateOverrideTag" b-gswuladdww>
<div name="testingProgram-testRenderTemplateOverrideTag" class="w-100 form-group mb-2">
<label for:ignore="testingProgram.testRenderTemplateOverrideTag_9a154b6a-7421-4b58-b3ca-0120db12dc53">testRenderTemplateOverrideTag</label>
<input id:ignore="testingProgram.testRenderTemplateOverrideTag_9a154b6a-7421-4b58-b3ca-0120db12dc53" readonly="readonly" class="w-100 form-control " style="background-color:transparent" type="text" step="any" value="0" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-custom-class="custom-tooltip" data-bs-title="testingProgram.testRenderTemplateOverrideTag" />

<div class="invalid-feedback"></div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public RenderableContentTests(RenderableContentTestsFixture fixture)
_projectDirectory = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName;
}

private void Compare(string fileName, object twin, string presentation)
{
// Arrange
var path = Path.Combine(_projectDirectory, "HtmlFiles", fileName);
var html = File.ReadAllText(path);
// Act
var cut = RenderComponent<RenderableContentControl>(param => param
.Add(p => p.Context, twin)
.Add(p => p.Presentation, presentation));

// File.WriteAllText(path, cut.Markup);

// Assert
cut.MarkupMatches(html);
}

[Fact]
public void Render_prgWeatherStations_Tabs_Successfull()
Expand All @@ -47,22 +62,6 @@ public void Render_prgWeatherStations_Tabs_Successfull()
Compare("prgWeatherStations.html", _fixture.Connector.prgWeatherStations, "Display");
}

private void Compare(string fileName, object twin, string presentation)
{
// Arrange
var path = Path.Combine(_projectDirectory, "HtmlFiles", fileName);
var html = File.ReadAllText(path);
// Act
var cut = RenderComponent<RenderableContentControl>(param => param
.Add(p => p.Context, twin)
.Add(p => p.Presentation, presentation));

// File.WriteAllText(path, cut.Markup);

// Assert
cut.MarkupMatches(html);
}

[Fact]
public void Render_stTestPrimitive_Wrap_Success()
{
Expand Down Expand Up @@ -522,7 +521,16 @@ public void Render_stTestLayouts_Border_UniformGrid_Successfull()

//}

[Fact]
public void Render_stTestRenderTemplateOverrideAttributeStruct_Success()
{
Compare("stTestRenderTemplateOverrideStruct.html", _fixture.Connector.testingProgram.testRenderTemplateOverrideStruct, "Display");
}


[Fact]
public void Render_stTestRenderTemplateOverrideAttributeTag_Success()
{
Compare("stTestRenderTemplateOverrideTag.html", _fixture.Connector.testingProgram.testRenderTemplateOverrideTag, "Display");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@namespace ixBlazor.App.Custom
@inherits RenderableComplexComponentBase<ITwinObject>
@using AXSharp.Connector.ValueTypes;

<div class="w-100 form-group">
<div>
<h1>My Simpl ePrimitive Struct Display View</h1>
</div>
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@namespace ixBlazor.App.Custom
@inherits RenderableComponentBase
@using AXSharp.Connector.ValueTypes;

<div class="w-100 form-group">
<div>
<h1>tested primitive tag</h1>
</div>
</div>

@code{

[Parameter]
public ITwinPrimitive Onliner { get; set; }
[Parameter]
public bool IsReadOnly { get; set; }

protected override void OnInitialized()
{
UpdateValuesOnChange(Onliner);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@page "/attributeTests"
<h3>WrapLayout</h3>


<div>
<p>------- "TAG Display"--------------</p>
</div>
<RenderableContentControl Presentation="Display" Context="@Entry.Plc.MAINC.OverridedSimpleTag">
</RenderableContentControl>

<div>
<p>------- "TAG Control"--------------</p>
</div>
<RenderableContentControl Presentation="Control" Context="@Entry.Plc.MAINC.OverridedSimpleTag">
</RenderableContentControl>

<div>
<p>------- "STRUCT Display"--------------</p>
</div>
<RenderableContentControl Presentation="Display" Context="@Entry.Plc.MAINC.OverridedSimpleStruct">
</RenderableContentControl>

<div>
<p>------- "STRUCT Control"--------------</p>
</div>
<RenderableContentControl Presentation="Control" Context="@Entry.Plc.MAINC.OverridedSimpleStruct">
</RenderableContentControl>
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">Menu</a>
<a class="navbar-brand" href="">Menu</a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>

<div class="@NavMenuCssClass" @onclick="ToggleNavMenu">

<ul class="nav flex-column">
<li class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Home
</NavLink>
</li>

<li class="nav-item px-3">
<NavLink class="nav-link" href="/attributeTests" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Attribute Tests
</NavLink>
</li>

<li class="nav-item px-3">
<NavLink class="nav-link" href="/stBlazor" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> StBlazor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ CLASS MAIN
groupBox_test: GroupBox_other;
cu00: CU00x;
cuBase: CUBase;

{#ix-attr:[RenderTemplateOverrideAttribute("ixBlazor.App.Custom.MySimplePrimitiveStruct")]}
OverridedSimpleStruct : stSimplePrimitive;

{#ix-attr:[RenderTemplateOverrideAttribute("ixBlazor.App.Custom.MySimplePrimitiveTag")]}
OverridedSimpleTag : WORD;

END_VAR

END_CLASS
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ CLASS testingProgram
testMultipleNested: stTestMultipleNested;

testLayouts : stTestLayouts;

{#ix-attr:[RenderTemplateOverrideAttribute("ixBlazor.App.Custom.MySimplePrimitiveStruct")]}
testRenderTemplateOverrideStruct : stSimplePrimitive;

{#ix-attr:[RenderTemplateOverrideAttribute("ixBlazor.App.Custom.MySimplePrimitiveTag")]}
testRenderTemplateOverrideTag : WORD;
END_VAR

END_CLASS
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Pocos
{
public partial class all_primitives : AXSharp.Connector.IPlain
{
public all_primitives()
{
}

public Boolean myBOOL { get; set; }

public Byte myBYTE { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Pocos
{
public partial class example : AXSharp.Connector.IPlain
{
public example()
{
}

public test_primitive primitives_stack { get; set; } = new test_primitive();
public test_primitive primitives_wrap { get; set; } = new test_primitive();
public test_primitive primitives_tabs { get; set; } = new test_primitive();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Pocos
{
public partial class GeoLocation : AXSharp.Connector.IPlain
{
public GeoLocation()
{
}

public Single Latitude { get; set; }

public Single Longitude { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace Pocos
{
public partial class ixcomponent : AXSharp.Connector.IPlain
{
public ixcomponent()
{
}

public Int16 my_int { get; set; }

public string my_string { get; set; } = string.Empty;
Expand All @@ -14,6 +18,10 @@ namespace MySecondNamespace
{
public partial class ixcomponent : AXSharp.Connector.IPlain
{
public ixcomponent()
{
}

public Int16 my_int { get; set; }

public string my_string { get; set; } = string.Empty;
Expand All @@ -25,6 +33,10 @@ namespace ThirdNamespace
{
public partial class ixcomponent : AXSharp.Connector.IPlain
{
public ixcomponent()
{
}

public Int16 my_int { get; set; }

public string my_string { get; set; } = string.Empty;
Expand Down
Loading

0 comments on commit f27c819

Please sign in to comment.