|
1 |
| -using BlazorWebFormsComponents.Enums; |
| 1 | +using BlazorComponentUtilities; |
| 2 | +using BlazorWebFormsComponents.Enums; |
| 3 | +using System; |
2 | 4 | using System.Collections.Generic;
|
3 | 5 | using System.Drawing;
|
4 | 6 | using System.Text;
|
@@ -92,82 +94,63 @@ public static void CopyTo(this IHasStyle source, IHasStyle destination)
|
92 | 94 |
|
93 | 95 | }
|
94 | 96 |
|
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); |
106 | 99 |
|
107 |
| - if (!hasStyle.Wrap) sb.Append("white-space:nowrap;"); |
108 | 100 |
|
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)) |
110 | 104 |
|
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)) |
115 | 107 |
|
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)) |
117 | 112 |
|
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)); |
119 | 121 |
|
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; |
127 | 126 |
|
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); |
152 | 132 |
|
153 | 133 | public static void SetFontsFromAttributes(this IHasFontStyle hasStyle, Dictionary<string, object> additionalAttributes)
|
154 | 134 | {
|
155 | 135 |
|
156 | 136 | if (additionalAttributes != null)
|
157 | 137 | {
|
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); |
165 | 146 | }
|
166 | 147 |
|
167 |
| - |
168 | 148 | }
|
169 | 149 |
|
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 | + |
171 | 153 |
|
| 154 | + } |
172 | 155 |
|
173 | 156 | }
|
0 commit comments