Skip to content

Commit 82e855f

Browse files
authored
Fix classes and styles (#69)
* Now using StyleBuilders * Added nuget.config to help locate packages folder appropriately
1 parent 81914f8 commit 82e855f

File tree

10 files changed

+67
-96
lines changed

10 files changed

+67
-96
lines changed

nuget.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<config>
4+
<add key="repositoryPath" value="src\packages" />
5+
</config>
6+
</configuration>

src/BlazorWebFormsComponents/BlazorWebFormsComponents.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
<ItemGroup>
21+
<PackageReference Include="BlazorComponentUtilities" Version="1.6.0" />
2122
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.1" />
2223
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.1" />
2324
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="3.1.1" />

src/BlazorWebFormsComponents/DataList.razor.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using BlazorWebFormsComponents.Enums;
1+
using BlazorComponentUtilities;
2+
using BlazorWebFormsComponents.Enums;
23
using Microsoft.AspNetCore.Components;
34
using System;
45
using System.Collections.Generic;
@@ -136,18 +137,7 @@ protected override void OnInitialized()
136137

137138
this.SetFontsFromAttributes(AdditionalAttributes);
138139

139-
var styleBuilder = new StringBuilder();
140-
styleBuilder.Append(Style);
141-
if (!Style?.EndsWith(";") ?? false) styleBuilder.Append(";");
142-
143-
this.ToStyleString(styleBuilder);
144-
145-
if (RepeatLayout is FlowRepeatLayout)
146-
{
147-
CalculatedStyle = styleBuilder.Length == 0 ? null : styleBuilder.ToString();
148-
} else {
149-
CalculatedStyle = styleBuilder.ToString();
150-
}
140+
CalculatedStyle = this.ToStyle().AddStyle(Style).NullIfEmpty();
151141

152142
base.OnInitialized();
153143
}

src/BlazorWebFormsComponents/IHasTableItemStyle.cs

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using BlazorWebFormsComponents.Enums;
1+
using BlazorComponentUtilities;
2+
using BlazorWebFormsComponents.Enums;
3+
using System;
24
using System.Collections.Generic;
35
using System.Drawing;
46
using System.Text;
@@ -92,82 +94,63 @@ public static void CopyTo(this IHasStyle source, IHasStyle destination)
9294

9395
}
9496

95-
public static string ToStyleString(this IHasTableItemStyle hasStyle)
96-
{
97-
98-
return ToStyleString(hasStyle, new StringBuilder()).ToString();
99-
100-
}
101-
102-
public static StringBuilder ToStyleString(this IHasTableItemStyle hasStyle, StringBuilder sb)
103-
{
104-
105-
((IHasStyle)hasStyle).ToStyleString(sb);
97+
public static StyleBuilder ToStyle(this IHasTableItemStyle hasStyle) =>
98+
((IHasStyle)hasStyle).ToStyle().AddStyle("white-space", "nowrap", !hasStyle.Wrap);
10699

107-
if (!hasStyle.Wrap) sb.Append("white-space:nowrap;");
108100

109-
return sb;
101+
public static StyleBuilder ToStyle(this IHasStyle hasStyle) =>
102+
StyleBuilder.Empty().AddStyle("background-color", () => ColorTranslator.ToHtml(hasStyle.BackColor.ToColor()).Trim(),
103+
when: hasStyle.BackColor != default(WebColor))
110104

111-
}
112-
113-
public static string ToStyleString(this IHasStyle hasStyle)
114-
{
105+
.AddStyle("color", () => ColorTranslator.ToHtml(hasStyle.ForeColor.ToColor()).Trim(),
106+
when: hasStyle.ForeColor != default(WebColor))
115107

116-
return ToStyleString(hasStyle, new StringBuilder()).ToString();
108+
.AddStyle("border", v => v.AddValue(hasStyle.BorderWidth.ToString())
109+
.AddValue(hasStyle.BorderStyle.ToString().ToLowerInvariant())
110+
.AddValue(() => ColorTranslator.ToHtml(hasStyle.BorderColor.ToColor()), HasBorders(hasStyle)),
111+
when: HasBorders(hasStyle))
117112

118-
}
113+
.AddStyle("font-weight", "bold", hasStyle.Font_Bold)
114+
.AddStyle("font-style", "italic", hasStyle.Font_Italic)
115+
.AddStyle("font-family", hasStyle.Font_Names, !string.IsNullOrEmpty(hasStyle.Font_Names))
116+
.AddStyle("font-size", hasStyle.Font_Size.ToString(), hasStyle.Font_Size != FontUnit.Empty)
117+
.AddStyle("text-decoration", v => v.AddValue("underline", hasStyle.Font_Underline)
118+
.AddValue("overline", hasStyle.Font_Overline)
119+
.AddValue("line-through", hasStyle.Font_Strikeout)
120+
, HasTextDecorations(hasStyle));
119121

120-
public static StringBuilder ToStyleString(this IHasStyle hasStyle, StringBuilder sb)
121-
{
122-
123-
if (hasStyle.BackColor != default(WebColor)) sb.Append($"background-color:{ColorTranslator.ToHtml(hasStyle.BackColor.ToColor()).Trim()};");
124-
if (hasStyle.ForeColor != default(WebColor)) sb.Append($"color:{ColorTranslator.ToHtml(hasStyle.ForeColor.ToColor())};");
125-
if (hasStyle.BorderStyle != BorderStyle.None && hasStyle.BorderStyle != BorderStyle.NotSet && hasStyle.BorderWidth.Value > 0 && hasStyle.BorderColor != default(WebColor))
126-
{
122+
private static bool HasTextDecorations(IHasStyle hasStyle) =>
123+
hasStyle.Font_Underline ||
124+
hasStyle.Font_Overline ||
125+
hasStyle.Font_Strikeout;
127126

128-
sb.Append($"border:{hasStyle.BorderWidth.ToString()} {hasStyle.BorderStyle.ToString().ToLowerInvariant()} {ColorTranslator.ToHtml(hasStyle.BorderColor.ToColor())};");
129-
130-
}
131-
132-
if (hasStyle.Font_Bold) sb.Append("font-weight:bold;");
133-
if (hasStyle.Font_Italic) sb.Append("font-style:italic;");
134-
if (!string.IsNullOrEmpty(hasStyle.Font_Names)) sb.Append($"font-family:{hasStyle.Font_Names};");
135-
if (hasStyle.Font_Size != FontUnit.Empty) sb.Append($"font-size:{hasStyle.Font_Size.ToString()};");
136-
if (hasStyle.Font_Underline || hasStyle.Font_Overline || hasStyle.Font_Strikeout)
137-
{
138-
sb.Append("text-decoration:");
139-
140-
var td = new StringBuilder();
141-
if (hasStyle.Font_Underline) td.Append("underline ");
142-
if (hasStyle.Font_Overline) td.Append("overline ");
143-
if (hasStyle.Font_Strikeout) td.Append("line-through");
144-
sb.Append(td.ToString().Trim());
145-
sb.Append(";");
146-
}
147-
148-
return sb;
149-
150-
151-
}
127+
private static bool HasBorders(IHasStyle hasStyle) =>
128+
hasStyle.BorderStyle != BorderStyle.None &&
129+
hasStyle.BorderStyle != BorderStyle.NotSet &&
130+
hasStyle.BorderWidth.Value > 0 &&
131+
hasStyle.BorderColor != default(WebColor);
152132

153133
public static void SetFontsFromAttributes(this IHasFontStyle hasStyle, Dictionary<string, object> additionalAttributes)
154134
{
155135

156136
if (additionalAttributes != null)
157137
{
158-
if (additionalAttributes.ContainsKey("Font-Bold")) hasStyle.Font_Bold = bool.Parse(additionalAttributes["Font-Bold"].ToString());
159-
if (additionalAttributes.ContainsKey("Font-Italic")) hasStyle.Font_Italic = bool.Parse(additionalAttributes["Font-Italic"].ToString());
160-
if (additionalAttributes.ContainsKey("Font-Names")) hasStyle.Font_Names = additionalAttributes["Font-Names"].ToString();
161-
if (additionalAttributes.ContainsKey("Font-Overline")) hasStyle.Font_Overline = bool.Parse(additionalAttributes["Font-Overline"].ToString());
162-
if (additionalAttributes.ContainsKey("Font-Size")) hasStyle.Font_Size = FontUnit.Parse(additionalAttributes["Font-Size"].ToString());
163-
if (additionalAttributes.ContainsKey("Font-Strikeout")) hasStyle.Font_Strikeout = bool.Parse(additionalAttributes["Font-Strikeout"].ToString());
164-
if (additionalAttributes.ContainsKey("Font-Underline")) hasStyle.Font_Underline = bool.Parse(additionalAttributes["Font-Underline"].ToString());
138+
hasStyle.Font_Bold = additionalAttributes.GetValue("Font-Bold", bool.Parse, hasStyle.Font_Bold);
139+
hasStyle.Font_Italic = additionalAttributes.GetValue("Font-Italic", bool.Parse, hasStyle.Font_Italic);
140+
hasStyle.Font_Underline = additionalAttributes.GetValue("Font-Underline", bool.Parse, hasStyle.Font_Underline);
141+
142+
hasStyle.Font_Names = additionalAttributes.GetValue("Font-Names", a => a, hasStyle.Font_Names);
143+
hasStyle.Font_Overline = additionalAttributes.GetValue("Font-Overline", bool.Parse, hasStyle.Font_Overline);
144+
hasStyle.Font_Size = additionalAttributes.GetValue("Font-Size", FontUnit.Parse, hasStyle.Font_Size);
145+
hasStyle.Font_Strikeout = additionalAttributes.GetValue("Font-Strikeout", bool.Parse, hasStyle.Font_Strikeout);
165146
}
166147

167-
168148
}
169149

170-
}
150+
public static T GetValue<T>(this Dictionary<string, object> additionalAttributes, string key, Func<string, T> parser, T defaultValue) =>
151+
additionalAttributes.TryGetValue(key, out var x) ? parser(x.ToString()) : defaultValue;
152+
171153

154+
}
172155

173156
}

src/BlazorWebFormsComponents/LoginControls/LoginName.razor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using System.Threading.Tasks;
5+
using BlazorComponentUtilities;
56
using BlazorWebFormsComponents.Enums;
67
using Microsoft.AspNetCore.Components;
78
using Microsoft.AspNetCore.Components.Authorization;
@@ -16,7 +17,7 @@ public partial class LoginName : BaseWebFormsComponent, IHasStyle
1617
[Parameter]
1718
public string FormatString { get; set; } = "{0}";
1819

19-
public string CalculatedStyle => this.ToStyleString();
20+
public StyleBuilder CalculatedStyle => this.ToStyle();
2021

2122
private bool UserAuthenticated { get; set; }
2223

src/BlazorWebFormsComponents/LoginControls/LoginStatus.razor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Text;
44
using System.Threading.Tasks;
5+
using BlazorComponentUtilities;
56
using BlazorWebFormsComponents.Enums;
67
using Microsoft.AspNetCore.Components;
78
using Microsoft.AspNetCore.Components.Authorization;
@@ -43,7 +44,7 @@ public partial class LoginStatus : BaseWebFormsComponent, IHasStyle
4344

4445
#endregion
4546

46-
public string CalculatedStyle => this.ToStyleString();
47+
public StyleBuilder CalculatedStyle => this.ToStyle();
4748

4849
private bool UserAuthenticated { get; set; }
4950

src/BlazorWebFormsComponents/Style.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using BlazorWebFormsComponents.Enums;
1+
using BlazorComponentUtilities;
2+
using BlazorWebFormsComponents.Enums;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -40,15 +41,7 @@ internal Style() { }
4041

4142
public Unit Width { get; set; }
4243

43-
public override string ToString()
44-
{
45-
46-
var theStyle = this.ToStyleString();
47-
if (string.IsNullOrEmpty(theStyle)) return null;
48-
49-
return theStyle;
50-
51-
}
44+
public override string ToString() => this.ToStyle().NullIfEmpty();
5245

5346
public void FromUnknownAttributes(Dictionary<string,object> attributes, string prefix) {
5447

src/BlazorWebFormsComponents/TableItemStyle.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BlazorWebFormsComponents.Enums;
2+
using BlazorComponentUtilities;
23
using System;
34
using System.Collections.Generic;
45
using System.Drawing;
@@ -20,10 +21,7 @@ internal TableItemStyle() { }
2021
public override string ToString()
2122
{
2223

23-
var theStyle = ((IHasTableItemStyle)this).ToStyleString();
24-
if (string.IsNullOrEmpty(theStyle)) return null;
25-
26-
return theStyle;
24+
return ((IHasTableItemStyle)this).ToStyle().NullIfEmpty();
2725

2826
}
2927

src/BlazorWebFormsComponents/Validations/AspNetValidationSummary.razor.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Drawing;
44
using System.Linq;
55
using System.Text;
6+
using BlazorComponentUtilities;
67
using BlazorWebFormsComponents.Enums;
78
using Microsoft.AspNetCore.Components;
89
using Microsoft.AspNetCore.Components.Forms;
@@ -33,7 +34,7 @@ public partial class AspNetValidationSummary : BaseWebFormsComponent, IHasStyle,
3334
[Parameter] public bool Font_Strikeout { get; set; }
3435
[Parameter] public bool Font_Underline { get; set; }
3536

36-
protected string CalculatedStyle { get; set; }
37+
protected StyleBuilder CalculatedStyle { get; set; }
3738

3839
public bool IsValid => CurrentEditContext.GetValidationMessages().Any();
3940

@@ -67,11 +68,7 @@ protected override void OnInitialized()
6768

6869
this.SetFontsFromAttributes(AdditionalAttributes);
6970

70-
var styleBuilder = new StringBuilder();
71-
72-
this.ToStyleString(styleBuilder);
73-
74-
CalculatedStyle = styleBuilder.ToString();
71+
CalculatedStyle = this.ToStyle();
7572

7673
base.OnInitialized();
7774

src/BlazorWebFormsComponents/Validations/BaseValidator.razor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq.Expressions;
44
using System.Reflection;
55
using System.Threading.Tasks;
6+
using BlazorComponentUtilities;
67
using BlazorWebFormsComponents.Enums;
78
using Microsoft.AspNetCore.Components;
89
using Microsoft.AspNetCore.Components.Forms;
@@ -49,7 +50,7 @@ public IHasStyle Style
4950

5051
public abstract bool Validate(string value);
5152

52-
protected string CalculatedStyle => this.ToStyleString();
53+
protected StyleBuilder CalculatedStyle => this.ToStyle();
5354

5455
protected override void OnInitialized()
5556
{

0 commit comments

Comments
 (0)