|
| 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