Skip to content

Commit 03b8892

Browse files
authored
Style Refactoring (#243)
* Style refactoring * Add calculated css class
1 parent b4b94e8 commit 03b8892

31 files changed

Lines changed: 242 additions & 530 deletions

samples/AfterBlazorServerSide/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5555
});
5656
}
5757

58-
public static Version ComponentVersion = typeof(IHasStyle).Assembly.GetName().Version;
58+
public static Version ComponentVersion = typeof(IStyle).Assembly.GetName().Version;
5959

6060
}
6161
}

src/BlazorWebFormsComponents.Test/GridView/AutoGenerateColumns.razor

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
@inherits BaseWebFormsComponent
1+
@inherits BaseStyledComponent
22

33
@{
44
var activeAdvertisment = GetActiveAdvertisment();
55
}
66

77
@if (activeAdvertisment != null)
88
{
9-
<a href="@activeAdvertisment.NavigateUrl" target="@Target" style="@CalculatedStyle" class="@CssClass"><img src="@activeAdvertisment.ImageUrl" width="@activeAdvertisment.Width" height="@activeAdvertisment.Height" alt="@activeAdvertisment.AlternateText" /></a>
9+
<a href="@activeAdvertisment.NavigateUrl" target="@Target" style="@Style" class="@CssClass"><img src="@activeAdvertisment.ImageUrl" width="@activeAdvertisment.Width" height="@activeAdvertisment.Height" alt="@activeAdvertisment.AlternateText" /></a>
1010
}

src/BlazorWebFormsComponents/AdRotator.razor.cs

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44
using System.IO;
55
using System.Linq;
66
using System.Xml.Linq;
7-
using Microsoft.AspNetCore.Components;
8-
using BlazorWebFormsComponents.Enums;
9-
using BlazorComponentUtilities;
107

118
namespace BlazorWebFormsComponents
129
{
13-
public partial class AdRotator : BaseWebFormsComponent, IHasStyle
10+
public partial class AdRotator : BaseStyledComponent
1411
{
1512
private static readonly string DefaultAlternateTextField = "AlternateText";
1613
private static readonly string DefaultImageUrlField = "ImageUrl";
@@ -37,53 +34,6 @@ public partial class AdRotator : BaseWebFormsComponent, IHasStyle
3734
[Parameter]
3835
public EventCallback<AdCreatedEventArgs> OnAdCreated { get; set; }
3936

40-
[Parameter]
41-
public WebColor BackColor { get; set; }
42-
43-
[Parameter]
44-
public WebColor BorderColor { get; set; }
45-
46-
[Parameter]
47-
public BorderStyle BorderStyle { get; set; }
48-
49-
[Parameter]
50-
public Unit BorderWidth { get; set; }
51-
52-
[Parameter]
53-
public string CssClass { get; set; }
54-
55-
[Parameter]
56-
public WebColor ForeColor { get; set; }
57-
58-
[Parameter]
59-
public Unit Height { get; set; }
60-
61-
[Parameter]
62-
public Unit Width { get; set; }
63-
64-
[Parameter]
65-
public bool Font_Bold { get; set; }
66-
67-
[Parameter]
68-
public bool Font_Italic { get; set; }
69-
70-
[Parameter]
71-
public string Font_Names { get; set; }
72-
73-
[Parameter]
74-
public bool Font_Overline { get; set; }
75-
76-
[Parameter]
77-
public FontUnit Font_Size { get; set; }
78-
79-
[Parameter]
80-
public bool Font_Strikeout { get; set; }
81-
82-
[Parameter]
83-
public bool Font_Underline { get; set; }
84-
85-
private string CalculatedStyle => this.ToStyle().Build().NullIfEmpty();
86-
8737
internal Advertisment GetActiveAdvertisment()
8838
{
8939
if (string.IsNullOrEmpty(AlternateTextField))
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using BlazorComponentUtilities;
2+
using BlazorWebFormsComponents.Enums;
3+
using Microsoft.AspNetCore.Components;
4+
5+
namespace BlazorWebFormsComponents
6+
{
7+
public abstract class BaseStyledComponent : BaseWebFormsComponent, IStyle
8+
{
9+
[Parameter]
10+
public WebColor BackColor { get; set; }
11+
12+
[Parameter]
13+
public WebColor BorderColor { get; set; }
14+
15+
[Parameter]
16+
public BorderStyle BorderStyle { get; set; }
17+
18+
[Parameter]
19+
public Unit BorderWidth { get; set; }
20+
21+
[Parameter]
22+
public string CssClass { get; set; }
23+
24+
[Parameter]
25+
public WebColor ForeColor { get; set; }
26+
27+
[Parameter]
28+
public Unit Height { get; set; }
29+
30+
[Parameter]
31+
public Unit Width { get; set; }
32+
33+
[Parameter]
34+
public FontInfo Font { get; set; } = new FontInfo();
35+
36+
protected string Style => this.ToStyle().Build().NullIfEmpty();
37+
}
38+
}

src/BlazorWebFormsComponents/Button.razor

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
<button type="@CalculatedButtonType"
66
@onclick="Click"
77
OnClientClick="@OnClientClick"
8-
style="@CalculatedStyle"
8+
style="@Style"
99
class="@CalculatedCssClass"
1010
disabled="@(Enabled ? null : "disabled")"
1111
title="@ToolTip"
1212
>@Text</button>
1313
}
14-
else { }
Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
1-
using BlazorComponentUtilities;
2-
using BlazorWebFormsComponents.Enums;
31
using Microsoft.AspNetCore.Components;
42
using System;
53

64
namespace BlazorWebFormsComponents
75
{
8-
public partial class Button : ButtonBaseComponent, IHasStyle
6+
public partial class Button : ButtonBaseComponent
97
{
10-
11-
[Parameter] public WebColor BackColor { get; set; }
12-
[Parameter] public WebColor BorderColor { get; set; }
13-
[Parameter] public BorderStyle BorderStyle { get; set; }
14-
[Parameter] public Unit BorderWidth { get; set; }
15-
[Parameter] public string CssClass { get; set; }
16-
[Parameter] public WebColor ForeColor { get; set; }
17-
[Parameter] public Unit Height { get; set; }
18-
[Parameter] public Unit Width { get; set; }
19-
[Parameter] public bool Font_Bold { get; set; }
20-
[Parameter] public bool Font_Italic { get; set; }
21-
[Parameter] public string Font_Names { get; set; }
22-
[Parameter] public bool Font_Overline { get; set; }
23-
[Parameter] public FontUnit Font_Size { get; set; }
24-
[Parameter] public bool Font_Strikeout { get; set; }
25-
[Parameter] public bool Font_Underline { get; set; }
26-
27-
private string CalculatedStyle => this.ToStyle().Build().NullIfEmpty();
28-
298
internal string CalculatedButtonType => CausesValidation ? "submit" : "button";
309

3110
internal string CalculatedCssClass => Enabled ? CssClass : string.Concat(CssClass, " aspNetDisabled").Trim();
@@ -38,6 +17,5 @@ public partial class Button : ButtonBaseComponent, IHasStyle
3817

3918
[Parameter]
4019
public string ToolTip { get; set; }
41-
4220
}
4321
}

src/BlazorWebFormsComponents/ButtonBaseComponent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace BlazorWebFormsComponents
55
{
6-
public abstract class ButtonBaseComponent : BaseWebFormsComponent, IButtonComponent
6+
public abstract class ButtonBaseComponent : BaseStyledComponent, IButtonComponent
77
{
88

99
[Parameter]

src/BlazorWebFormsComponents/DataList.razor.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace BlazorWebFormsComponents
99
{
10-
public partial class DataList<ItemType> : DataBoundComponent<ItemType>, IHasStyle
10+
public partial class DataList<ItemType> : DataBoundComponent<ItemType>, IStyle
1111
{
1212
private static readonly Dictionary<DataListEnum, string?> _GridLines = new Dictionary<DataListEnum, string?> {
1313
{DataListEnum.None, null },
@@ -47,13 +47,7 @@ public partial class DataList<ItemType> : DataBoundComponent<ItemType>, IHasStyl
4747
[Parameter] public BorderStyle BorderStyle { get; set; }
4848
[Parameter] public Unit BorderWidth { get; set; }
4949
[Parameter] public string CssClass { get; set; }
50-
[Parameter] public bool Font_Bold { get; set; }
51-
[Parameter] public bool Font_Italic { get; set; }
52-
[Parameter] public string Font_Names { get; set; }
53-
[Parameter] public bool Font_Overline { get; set; }
54-
[Parameter] public FontUnit Font_Size { get; set; }
55-
[Parameter] public bool Font_Strikeout { get; set; }
56-
[Parameter] public bool Font_Underline { get; set; }
50+
[Parameter] public FontInfo Font { get; set; } = new FontInfo();
5751
[Parameter] public WebColor ForeColor { get; set; }
5852
[Parameter] public Unit Height { get; set; }
5953
[Parameter] public Unit Width { get; set; }
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using BlazorComponentUtilities;
2+
using BlazorWebFormsComponents.Enums;
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace BlazorWebFormsComponents
7+
{
8+
public static class HasStyleExtensions
9+
{
10+
public static void CopyTo(this IStyle source, IStyle destination)
11+
{
12+
13+
destination.BackColor = source.BackColor;
14+
destination.BorderColor = source.BorderColor;
15+
destination.BorderStyle = source.BorderStyle;
16+
destination.BorderWidth = source.BorderWidth;
17+
destination.CssClass = source.CssClass;
18+
destination.ForeColor = source.ForeColor;
19+
destination.Height = source.Height;
20+
destination.Width = source.Width;
21+
destination.Font.Bold = source.Font.Bold;
22+
destination.Font.Italic = source.Font.Italic;
23+
destination.Font.Names = source.Font.Names;
24+
destination.Font.Overline = source.Font.Overline;
25+
destination.Font.Size = source.Font.Size;
26+
destination.Font.Strikeout = source.Font.Strikeout;
27+
destination.Font.Underline = source.Font.Underline;
28+
29+
}
30+
31+
public static StyleBuilder ToStyle(this IHasTableItemStyle hasStyle) =>
32+
((IStyle)hasStyle).ToStyle().AddStyle("white-space", "nowrap", !hasStyle.Wrap);
33+
34+
35+
public static StyleBuilder ToStyle(this IStyle hasStyle) =>
36+
StyleBuilder.Empty().AddStyle("background-color", () => ColorTranslator.ToHtml(hasStyle.BackColor.ToColor()).Trim(),
37+
when: hasStyle.BackColor != default(WebColor))
38+
39+
.AddStyle("color", () => ColorTranslator.ToHtml(hasStyle.ForeColor.ToColor()).Trim(),
40+
when: hasStyle.ForeColor != default(WebColor))
41+
42+
.AddStyle("border", v => v.AddValue(hasStyle.BorderWidth.ToString())
43+
.AddValue(hasStyle.BorderStyle.ToString().ToLowerInvariant())
44+
.AddValue(() => ColorTranslator.ToHtml(hasStyle.BorderColor.ToColor()), HasBorders(hasStyle)),
45+
when: HasBorders(hasStyle))
46+
47+
.AddStyle("font-weight", "bold", hasStyle.Font.Bold)
48+
.AddStyle("font-style", "italic", hasStyle.Font.Italic)
49+
.AddStyle("font-family", hasStyle.Font.Names, !string.IsNullOrEmpty(hasStyle.Font.Names))
50+
.AddStyle("font-size", hasStyle.Font.Size.ToString(), hasStyle.Font.Size != FontUnit.Empty)
51+
.AddStyle("text-decoration", v => v.AddValue("underline", hasStyle.Font.Underline)
52+
.AddValue("overline", hasStyle.Font.Overline)
53+
.AddValue("line-through", hasStyle.Font.Strikeout)
54+
, HasTextDecorations(hasStyle));
55+
56+
private static bool HasTextDecorations(IStyle hasStyle) =>
57+
hasStyle.Font.Underline ||
58+
hasStyle.Font.Overline ||
59+
hasStyle.Font.Strikeout;
60+
61+
private static bool HasBorders(IStyle hasStyle) =>
62+
hasStyle.BorderStyle != BorderStyle.None &&
63+
hasStyle.BorderStyle != BorderStyle.NotSet &&
64+
hasStyle.BorderWidth.Value > 0 &&
65+
hasStyle.BorderColor != default(WebColor);
66+
67+
public static void SetFontsFromAttributes(this IFontStyle hasStyle, Dictionary<string, object> additionalAttributes)
68+
{
69+
if (additionalAttributes != null)
70+
{
71+
hasStyle.Font.Bold = additionalAttributes.GetValue("Font-Bold", bool.Parse, hasStyle.Font.Bold);
72+
hasStyle.Font.Italic = additionalAttributes.GetValue("Font-Italic", bool.Parse, hasStyle.Font.Italic);
73+
hasStyle.Font.Underline = additionalAttributes.GetValue("Font-Underline", bool.Parse, hasStyle.Font.Underline);
74+
75+
hasStyle.Font.Names = additionalAttributes.GetValue("Font-Names", a => a, hasStyle.Font.Names);
76+
hasStyle.Font.Overline = additionalAttributes.GetValue("Font-Overline", bool.Parse, hasStyle.Font.Overline);
77+
hasStyle.Font.Size = additionalAttributes.GetValue("Font-Size", FontUnit.Parse, hasStyle.Font.Size);
78+
hasStyle.Font.Strikeout = additionalAttributes.GetValue("Font-Strikeout", bool.Parse, hasStyle.Font.Strikeout);
79+
}
80+
}
81+
82+
public static T GetValue<T>(this Dictionary<string, object> additionalAttributes, string key, Func<string, T> parser, T defaultValue) =>
83+
additionalAttributes.TryGetValue(key, out var x) ? parser(x.ToString()) : defaultValue;
84+
85+
86+
}
87+
88+
}

0 commit comments

Comments
 (0)