This repository has been archived by the owner on May 28, 2019. It is now read-only.
/
Dynamic.cs
357 lines (331 loc) · 11.9 KB
/
Dynamic.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.RenderTree;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Blazorous
{
public class Dynamic : ComponentBase
{
[Inject] IBlazorousInterop BlazorousInterop { get; set; }
[Inject] ICssCreator CssCreator { get; set; }
[Parameter] private string TagName { get; set; }
private Dictionary<string, object> _attributesToRender;
private RenderFragment _childContent;
private string _classname;
private string _debug;
private List<object> _css;
[Parameter] private ElementRef ElementRef { get; set; }
public IReadOnlyDictionary<string, object> Attributes
{
// The property is only declared for intellisense. It's not used at runtime.
get => throw new NotImplementedException();
set => throw new NotImplementedException();
}
public override Task SetParametersAsync(ParameterCollection parameters)
{
_css = parameters.GetParameterList("css");
_attributesToRender = (Dictionary<string, object>)parameters.ToDictionary();
_attributesToRender.Remove("css");
TagName = GetAndRemove<string>(_attributesToRender, nameof(TagName))
?? throw new InvalidOperationException($"No value was supplied for required parameter '{nameof(TagName)}'.");
_childContent = GetAndRemove<RenderFragment>(_attributesToRender, RenderTreeBuilder.ChildContent);
_classname = GetAndRemove<string>(_attributesToRender, "class");
_debug = GetAndRemove<string>(_attributesToRender, "debug");
// Combine any explicitly-supplied attributes with the remaining parameters
var attributesParam = GetAndRemove<IReadOnlyDictionary<string, object>>(_attributesToRender, nameof(Attributes));
if (attributesParam != null)
{
foreach (var kvp in attributesParam)
{
if (kvp.Value != null
&& _attributesToRender.TryGetValue(kvp.Key, out var existingValue)
&& existingValue != null)
{
_attributesToRender[kvp.Key] = existingValue
+ " " + kvp.Value;
}
else
{
_attributesToRender[kvp.Key] = kvp.Value;
}
}
}
return base.SetParametersAsync(ParameterCollection.Empty);
}
protected override Task OnParametersSetAsync()
{
return UpdateCss(_css, _debug, _classname, _attributesToRender);
}
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);
builder.OpenElement(0, TagName);
foreach (var param in _attributesToRender)
{
builder.AddAttribute(1, param.Key, param.Value);
}
if (_classname != null) builder.AddAttribute(1, "class", _classname);
builder.AddElementReferenceCapture(2, capturedRef => { ElementRef = capturedRef; });
builder.AddContent(3, _childContent);
builder.CloseElement();
}
private async Task UpdateCss(IReadOnlyCollection<object> css, string debug, string classname, IDictionary<string, object> attributesToRender)
{
var ret = classname;
var addedClasses = GetClassesFromProps(css, attributesToRender);
foreach (var cls in addedClasses)
{
ret = ret != null ? $"{ret} {cls}" : cls;
}
var glamorClasses = await GetCssFromProps(css, attributesToRender, debug);
foreach (var cls in glamorClasses)
{
ret = ret != null ? $"{ret} {cls}".Trim() : cls;
}
var dynamicCss = (Css)CreateDynamicCssFromAttributes(attributesToRender);
if (dynamicCss.Count > 0)
{
var dynamicClass = await BlazorousInterop.Css(await dynamicCss.ToCss(attributesToRender, debug), debug);
ret = ret != null ? $"{ret} {dynamicClass}" : dynamicClass;
}
var pseudoCss = await CreatePseudoCssFromAttributes(attributesToRender, debug);
foreach (var str in pseudoCss)
{
ret = ret != null ? $"{ret} {str}".Trim() : str;
}
_classname = ret;
}
private async Task<List<string>> CreatePseudoCssFromAttributes(IDictionary<string, object> attributesToRender, string debug)
{
var list = new List<string>();
foreach (var pseudo in PseudoProps)
{
if (attributesToRender.TryGetValue(pseudo, out var value))
{
switch (value)
{
case string s:
list.Add(await BlazorousInterop.Css($"{{\":{pseudo}\":{s}}}", debug));
break;
case Css c:
var css = await c.ToCss(attributesToRender, debug);
if (c.Count > 0) list.Add(await BlazorousInterop.Css($"{{\":{pseudo}\":{css}}}", debug));
break;
default:
throw new InvalidOperationException($"Pseudo attribute {pseudo} must be string or of type Blazorous.Css");
}
attributesToRender.Remove(pseudo);
}
}
return list;
}
private ICss CreateDynamicCssFromAttributes(IDictionary<string, object> attributesToRender)
{
var css = CssCreator.CreateNew();
foreach (var cssProp in CssProps)
{
if (attributesToRender.TryGetValue(cssProp, out var value))
{
css.AddRule(cssProp, value.ToString());
attributesToRender.Remove(cssProp);
}
}
return css;
}
private static IEnumerable<string> GetClassesFromProps(IEnumerable<object> css, IDictionary<string, object> attributesToRender)
{
var list = new List<string>();
foreach (var item in css)
{
switch (item)
{
case string _:
break;
case Css c:
list.Add(c.ToClasses(attributesToRender));
break;
default:
throw new InvalidOperationException("css attribute must be string or of type Blazorous.Css");
}
}
return list;
}
private async Task<List<string>> GetCssFromProps(IEnumerable<object> css, IDictionary<string, object> attributesToRender, string debug)
{
var list = new List<string>();
foreach (var item in css)
{
switch (item)
{
case string s:
list.Add(await BlazorousInterop.Css(s, debug));
break;
case Css c:
if (c.Count > 0)
{
var cssVar = await BlazorousInterop.Css(await c.ToCss(attributesToRender, debug), debug);
list.Add(cssVar.Trim());
}
break;
default:
throw new InvalidOperationException("css attribute must be string or of type Blazorous.Css");
}
}
return list;
}
private static T GetAndRemove<T>(IDictionary<string, object> values, string key)
{
if (values.TryGetValue(key, out var value))
{
values.Remove(key);
return (T)value;
}
else
{
return default;
}
}
private static readonly IList<string> PseudoProps = new List<string>
{
"after",
"before",
"focus",
"hover",
"active",
"visited"
};
private static readonly IList<string> CssProps = new List<string>
{
"azimuth",
"background",
"background-attachment",
"background-color",
"background-image",
"background-position",
"background-repeat",
"border",
"border-bottom",
"border-bottom-color",
"border-bottom-style",
"border-bottom-width",
"border-collapse",
"border-color",
"border-left",
"border-left-color",
"border-left-style",
"border-left-width",
"border-right",
"border-right-color",
"border-right-style",
"border-right-width",
"border-spacing",
"border-style",
"border-top",
"border-top-color",
"border-top-style",
"border-top-width",
"border-width",
"bottom",
"box-sizing",
"caption-side",
"caret-color",
"chains",
"clear",
"clip",
"color",
"content",
"counter-increment",
"counter-reset",
"cue",
"cue-after",
"cue-before",
"cursor",
"direction",
"display",
"elevation",
"empty-cells",
"float",
"flow",
"font",
"font-family",
"font-size",
"font-style",
"font-variant",
"font-weight",
"grid",
"grid-template",
"grid-template-areas",
"grid-template-columns",
"grid-template-rows",
"height",
"left",
"letter-spacing",
"line-height",
"list-style",
"list-style-image",
"list-style-position",
"list-style-type",
"margin",
"margin-bottom",
"margin-left",
"margin-right",
"margin-top",
"max-height",
"max-width",
"min-height",
"min-width",
"opacity",
"orphans",
"outline",
"outline-color",
"outline-offset",
"outline-style",
"outline-width",
"overflow",
"padding",
"padding-bottom",
"padding-left",
"padding-right",
"padding-top",
"page-break-after",
"page-break-before",
"page-break-inside",
"pause",
"pause-after",
"pause-before",
"pitch",
"pitch-range",
"play-during",
"position",
"presentation-level",
"quotes",
"resize",
"richness",
"right",
"speak",
"speak-header",
"speak-numeral",
"speak-punctuation",
"speech-rate",
"stress",
"table-layout",
"text-align",
"text-decoration",
"text-indent",
"text-overflow",
"text-transform",
"top",
"unicode-bidi",
"vertical-align",
"visibility",
"voice-family",
"volume",
"white-space",
"widows",
"width",
"word-spacing",
"z-index"
};
}
}