forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlScriptBlocks.cs
More file actions
341 lines (312 loc) · 11 KB
/
HtmlScriptBlocks.cs
File metadata and controls
341 lines (312 loc) · 11 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace ServiceStack.Script
{
/* Usages:
{{#ul {class:'nav'}}} <li>item</li> {{/ul}}
{{#ul {each:items, class:'nav'}}} <li>{{it}}</li> {{/ul}}
{{#ul {each:numbers, it:'num', class:'nav'}}} <li>{{num}}</li> {{/ul}}
{{#ul {if:hasAccess, each:items, where:'Age > 27',
class:['nav', !disclaimerAccepted ? 'blur' : ''],
id:`ul-${id}`, selected:true} }}
{{#li {class: {alt:isOdd(index), active:Name==highlight} }}
{{Name}}
{{/li}}
{{else}}
<div>no items</div>
{{/ul}}
// Equivalent to:
{{#if hasAccess}}
{{ items | where => it.Age > 27 | assignTo: items }}
{{#if !isEmpty(items)}}
<ul {{ ['nav', !disclaimerAccepted ? 'blur' : ''] | htmlClass }} id="menu-{{id}}">
{{#each items}}
<li {{ {alt:isOdd(index), active:Name==highlight} | htmlClass }}>{{Name}}</li>
{{/each}}
</ul>
{{else}}
<div>no items</div>
{{/if}}
{{/if}}
// Razor:
@{
var persons = (items as IEnumerable<Person>)?.Where(x => x.Age > 27);
}
@if (hasAccess)
{
if (persons?.Any() == true)
{
<ul id="menu-@id" class="nav @(!disclaimerAccepted ? "hide" : "")">
@{
var index = 0;
}
@foreach (var person in persons)
{
<li class="@(index++ % 2 == 1 ? "alt " : "" )@(person.Name == activeName ? "active" : "")">
@person.Name
</li>
}
</ul>
}
else
{
<div>no items</div>
}
}
*/
public class ScriptUlBlock : ScriptHtmlBlock
{
public override string Tag => "ul";
}
public class ScriptOlBlock : ScriptHtmlBlock
{
public override string Tag => "ol";
}
public class ScriptLiBlock : ScriptHtmlBlock
{
public override string Tag => "li";
}
public class ScriptDivBlock : ScriptHtmlBlock
{
public override string Tag => "div";
}
public class ScriptPBlock : ScriptHtmlBlock
{
public override string Tag => "p";
}
public class ScriptFormBlock : ScriptHtmlBlock
{
public override string Tag => "form";
}
public class ScriptInputBlock : ScriptHtmlBlock
{
public override string Tag => "input";
}
public class ScriptSelectBlock : ScriptHtmlBlock
{
public override string Tag => "select";
}
public class ScriptOptionBlock : ScriptHtmlBlock
{
public override string Tag => "option";
}
public class ScriptTextAreaBlock : ScriptHtmlBlock
{
public override string Tag => "textarea";
}
public class ScriptButtonBlock : ScriptHtmlBlock
{
public override string Tag => "button";
}
public class ScriptTableBlock : ScriptHtmlBlock
{
public override string Tag => "table";
}
public class ScriptTrBlock : ScriptHtmlBlock
{
public override string Tag => "tr";
}
public class ScriptTdBlock : ScriptHtmlBlock
{
public override string Tag => "td";
}
public class ScriptTHeadBlock : ScriptHtmlBlock
{
public override string Tag => "thead";
}
public class ScriptTBodyBlock : ScriptHtmlBlock
{
public override string Tag => "tbody";
}
public class ScriptTFootBlock : ScriptHtmlBlock
{
public override string Tag => "tfoot";
}
public class ScriptDlBlock : ScriptHtmlBlock
{
public override string Tag => "dl";
}
public class ScriptDtBlock : ScriptHtmlBlock
{
public override string Tag => "dt";
}
public class ScriptDdBlock : ScriptHtmlBlock
{
public override string Tag => "dd";
}
//Don't emit new line on in-line elements
public class ScriptSpanBlock : ScriptHtmlBlock
{
public override string Tag => "span";
public override string Suffix => "";
}
public class ScriptABlock : ScriptHtmlBlock
{
public override string Tag => "a";
public override string Suffix => "";
}
public class ScriptImgBlock : ScriptHtmlBlock
{
public override string Tag => "img";
public override string Suffix => "";
}
public class ScriptEmBlock : ScriptHtmlBlock
{
public override string Tag => "em";
public override string Suffix => "";
}
public class ScriptBBlock : ScriptHtmlBlock
{
public override string Tag => "b";
public override string Suffix => "";
}
public class ScriptIBlock : ScriptHtmlBlock
{
public override string Tag => "i";
public override string Suffix => "";
}
public class ScriptStrongBlock : ScriptHtmlBlock
{
public override string Tag => "strong";
public override string Suffix => "";
}
public abstract class ScriptHtmlBlock : ScriptBlock
{
public override string Name => Tag;
public abstract string Tag { get; }
public virtual string Suffix { get; } = Environment.NewLine;
public override async Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token)
{
var htmlAttrs = block.Argument.GetJsExpressionAndEvaluate(scope) as Dictionary<string, object>;
var hasEach = false;
IEnumerable each = null;
var binding = "it";
var hasExplicitBinding = false;
JsToken where = null;
if (htmlAttrs != null)
{
if (htmlAttrs.TryGetValue("if", out var oIf))
{
if (Script.DefaultScripts.isFalsy(oIf))
return;
htmlAttrs.Remove("if");
}
if (htmlAttrs.TryGetValue(nameof(where), out var oWhere))
{
if (!(oWhere is string whereExpr))
throw new NotSupportedException($"'where' should be a string expression but instead found '{oWhere.GetType().Name}'");
where = whereExpr.GetCachedJsExpression(scope);
htmlAttrs.Remove(nameof(where));
}
if (htmlAttrs.TryGetValue(nameof(each), out var oEach))
{
hasEach = true;
htmlAttrs.Remove(nameof(each));
}
each = oEach as IEnumerable;
if (htmlAttrs.TryGetValue("it", out var oIt) && oIt is string it)
{
binding = it;
hasExplicitBinding = true;
htmlAttrs.Remove("it");
}
if (htmlAttrs.TryGetValue("class", out var oClass))
{
var cls = scope.Context.HtmlMethods.htmlClassList(oClass);
if (string.IsNullOrEmpty(cls))
htmlAttrs.Remove("class");
else
htmlAttrs["class"] = cls;
}
}
var attrString = scope.Context.HtmlMethods.htmlAttrsList(htmlAttrs);
if (HtmlScripts.VoidElements.Contains(Tag)) //e.g. img, input, br, etc
{
await scope.OutputStream.WriteAsync($"<{Tag}{attrString}>{Suffix}", token);
}
else
{
if (hasEach)
{
var hasElements = each != null && each.GetEnumerator().MoveNext();
if (hasElements)
{
await scope.OutputStream.WriteAsync($"<{Tag}{attrString}>{Suffix}", token);
var index = 0;
var whereIndex = 0;
foreach (var element in each)
{
// Add all properties into scope if called without explicit in argument
var scopeArgs = !hasExplicitBinding && CanExportScopeArgs(element)
? element.ToObjectDictionary()
: new Dictionary<string, object>();
scopeArgs[binding] = element;
scopeArgs[nameof(index)] = AssertWithinMaxQuota(whereIndex++);
var itemScope = scope.ScopeWithParams(scopeArgs);
if (where != null)
{
var result = where.EvaluateToBool(itemScope);
if (!result)
continue;
}
itemScope.ScopedParams[nameof(index)] = AssertWithinMaxQuota(index++);
await WriteBodyAsync(itemScope, block, token);
}
await scope.OutputStream.WriteAsync($"</{Tag}>{Suffix}", token);
}
else
{
await WriteElseAsync(scope, block.ElseBlocks, token);
}
}
else
{
await scope.OutputStream.WriteAsync($"<{Tag}{attrString}>{Suffix}", token);
await WriteBodyAsync(scope, block, token);
await scope.OutputStream.WriteAsync($"</{Tag}>{Suffix}", token);
}
}
}
}
public class HtmlScriptBlocks : IScriptPlugin
{
/// <summary>
/// Usages: {{#ul {each:items, class:'nav'} }} <li>{{it}}</li> {{/ul}}
/// </summary>
public void Register(ScriptContext context)
{
context.ScriptBlocks.AddRange(new ScriptBlock[] {
new ScriptUlBlock(),
new ScriptOlBlock(),
new ScriptLiBlock(),
new ScriptDivBlock(),
new ScriptPBlock(),
new ScriptFormBlock(),
new ScriptInputBlock(),
new ScriptSelectBlock(),
new ScriptOptionBlock(),
new ScriptTextAreaBlock(),
new ScriptButtonBlock(),
new ScriptTableBlock(),
new ScriptTrBlock(),
new ScriptTdBlock(),
new ScriptTHeadBlock(),
new ScriptTBodyBlock(),
new ScriptTFootBlock(),
new ScriptDlBlock(),
new ScriptDtBlock(),
new ScriptDdBlock(),
new ScriptSpanBlock(),
new ScriptABlock(),
new ScriptImgBlock(),
new ScriptEmBlock(),
new ScriptBBlock(),
new ScriptIBlock(),
new ScriptStrongBlock(),
});
}
}
}