Libraryless. Click here for Pure Java version (5981L/35K).
1 | sclass HTMLFramer1 { |
2 | S title; |
3 | bool titleIsHTML; |
4 | new L<NavItem> navItems; |
5 | new L<O> contents; // items will be evaluated to strings upon rendering |
6 | new LinkedHashSet<S> headElements; // automatically avoid duplication |
7 | bool navBeforeTitle; |
8 | new L<Runnable> willRender; |
9 | |
10 | srecord NavItem(S link, S html) { |
11 | bool targetBlank; |
12 | } |
13 | |
14 | public S get() { ret render(); } |
15 | |
16 | swappable S render() { |
17 | callFAll(willRender); |
18 | ret hhtml(hhead(htitle(titleIsHTML ? hTitleClean(title) : title) |
19 | + lines(headElements)) |
20 | + hbody( |
21 | (navBeforeTitle ? renderNav() + renderTitle() |
22 | : renderTitle() + renderNav()) |
23 | + nemptyLines(contents))); |
24 | } |
25 | |
26 | swappable S renderTitle() { |
27 | ret h1(encodedTitle()); |
28 | } |
29 | |
30 | S encodedTitle() { |
31 | //printVars_str encodedTitle(+title, +titleIsHTML); |
32 | ret titleIsHTML ? title : htmlEncode2(title); |
33 | } |
34 | |
35 | swappable S renderNav() { |
36 | if (empty(navItems)) ret ""; |
37 | ret div_vbar(map renderNavItem(navItems), style := "margin-bottom: 0.5em"); |
38 | } |
39 | |
40 | S renderNavItem(NavItem ni) { |
41 | ret ahref_possiblyTargetBlank(ni.link, ni.html, ni.targetBlank); |
42 | } |
43 | |
44 | void addNavItem(S html) { |
45 | navItems.add(new NavItem(null, html)); |
46 | } |
47 | |
48 | void addNavItems(Iterable<S> htmls) { |
49 | forEach addNavItem(htmls); |
50 | } |
51 | |
52 | void addNavItem(S link, S html, O... _) { |
53 | navItems.add(setAll(new NavItem(link, html), filterParams(_, 'targetBlank))); |
54 | } |
55 | |
56 | void addInHead aka addInHEAD(O html) { |
57 | headElements.add(str(html)); |
58 | } |
59 | |
60 | // legacy signature for binary compatibility |
61 | void add(S html) { add((O) html); } |
62 | |
63 | // convenience signature for lambdas |
64 | void add(IF0<O> html) { add(lambdaToToString(html)); } |
65 | |
66 | // add contents: TODO move things to head automatically |
67 | void add(O html) { |
68 | if (notNullOrEmptyString(html)) |
69 | contents.add(html); |
70 | } |
71 | |
72 | void addAll(Cl<S> l) { forEach add(l); } |
73 | |
74 | void clearNavItems { |
75 | navItems.clear(); |
76 | } |
77 | |
78 | selfType setTitle(S title) { |
79 | this.title = title; |
80 | this; |
81 | } |
82 | |
83 | S htmlTitle(S title) { |
84 | this.title = title; |
85 | titleIsHTML = true; |
86 | ret title; |
87 | } |
88 | |
89 | Cl<S> headElements() { ret headElements; } |
90 | } |
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1029581 |
Snippet name: | HTMLFramer1 |
Eternal ID of this version: | #1029581/36 |
Text MD5: | bed4f935759c177b1f38be02ae3108b4 |
Transpilation MD5: | 7eb4b61f9993e23ad606f1ed25b3e142 |
Author: | stefan |
Category: | javax / web |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-09-24 23:33:50 |
Source code size: | 2243 bytes / 90 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 352 / 769 |
Version history: | 35 change(s) |
Referenced in: | [show references] |