@yannosay/sinth 1.8.1 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,137 +1,137 @@
1
- import { MixedBlockEntry } from "../types.ts";
2
-
3
- export function buildRenderBody(opts: {
4
- bodyHTML: string;
5
- logicBlocks: string[];
6
- mixedBlocks: MixedBlockEntry[]
7
- needsLogic: boolean;
8
- needsMixed: boolean;
9
- needsIf: boolean;
10
- needsFor: boolean;
11
- needsExpr: boolean;
12
- needsDelay: boolean;
13
- exprVarUpdates?: string;
14
- }): string {
15
- const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay, exprVarUpdates } = opts;
16
- let renderBody = "";
17
- renderBody += ` let _sx = window.scrollX, _sy = window.scrollY;\n`;
18
-
19
- if (needsLogic) {
20
- renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
21
- }
22
-
23
- if (needsMixed) {
24
- for (const mb of mixedBlocks) {
25
- const ifJS = mb.ifJS ? mb.ifJS.trim() : "";
26
- const elseJS = mb.elseJS ? mb.elseJS.trim() : "";
27
- renderBody += ` (function() {
28
- let __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
29
- if (__el) {
30
- let __condFn = __X[${mb.conditionJS}];
31
- if (__condFn ? __condFn() : false) {
32
- ${ifJS}
33
- __el.innerHTML = ${JSON.stringify(mb.ifHTML)};
34
- } else {
35
- ${elseJS}
36
- __el.innerHTML = ${JSON.stringify(mb.elseHTML)};
37
- }
38
- __el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
39
- __el.querySelectorAll('.sinth-expr').forEach(function(el) { el.classList.remove('sinth-expr'); });
40
- __el.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);
41
- ${needsDelay ? `__el.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
42
- __el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });` : ""}
43
- }
44
- })();\n`;
45
- }
46
- }
47
-
48
- renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
49
- let target = document.getElementById(el.dataset.sinthRemove);
50
- if (target) target.remove();
51
- });\n`;
52
- if (needsFor) {
53
- renderBody += ` document.querySelectorAll('template[data-sinth-for], template[data-sinth-for-expr]').forEach(sinthForBlock);\n`;
54
- }
55
- if (needsIf) {
56
- renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(function(t) { var p = t.parentElement; while(p && p.tagName !== 'TEMPLATE') p = p.parentElement; if (!p || (!p.hasAttribute('data-sinth-for') && !p.hasAttribute('data-sinth-for-expr'))) sinthIfBlock(t); });\n`;
57
- }
58
- renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
59
- try { if (document.activeElement !== el) { let v=el.dataset.sinthValue; el.value = __X[v] ? __X[v]({}) : ''; } } catch(e) {}
60
- });\n`;
61
- renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
62
- try { let s=el.dataset.sinthStep; el.step = __X[s] ? __X[s]({}) : 1; } catch(e) {}
63
- });\n`;
64
- renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
65
- try { let c=el.dataset.sinthChecked; el.checked = __X[c] ? !!__X[c]({}) : false; } catch(e) {}
66
- });\n`;
67
- if (exprVarUpdates) {
68
- renderBody += ` ${exprVarUpdates}\n`;
69
- }
70
- if (bodyHTML.includes("data-sinth-checked-expr")) {
71
- renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
72
- try {
73
- let exprFn = __X[el.dataset.sinthCheckedExpr];
74
- if (exprFn) el.checked = !!exprFn({});
75
- } catch(e) {}
76
- });\n`;
77
- }
78
- if (needsDelay) {
79
- renderBody += ` document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
80
- let newContent = '';
81
- el.querySelectorAll('.sinth-expr').forEach(function(exprEl) {
82
- let exprFn = __X[exprEl.dataset.exprId];
83
- if (exprFn) newContent += exprFn({});
84
- });
85
- if (el._sinthLastContent === undefined) {
86
- el._sinthLastContent = newContent;
87
- } else if (el._sinthLastContent !== newContent) {
88
- el._sinthLastContent = newContent;
89
- delete el.dataset.sinthDelayDone;
90
- }
91
- });
92
- document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });\n`;
93
- }
94
- if (needsExpr) {
95
- renderBody += ` document.querySelectorAll('.sinth-expr').forEach(function(el) {
96
- let delayParent = el.closest('[data-sinth-delay]');
97
- if (!delayParent || delayParent.dataset.sinthDelayDone) {
98
- sinthExpr(el);
99
- }
100
- });\n`;
101
- }
102
- renderBody += ` document.querySelectorAll('[data-sinth-hide]').forEach(function(el) {
103
- let exprId = el.dataset.sinthHide;
104
- if (exprId) {
105
- try {
106
- let exprFn = __X[exprId];
107
- if (exprFn) el.style.display = exprFn({}) ? 'none' : '';
108
- } catch(e) {}
109
- } else {
110
- el.style.display = 'none';
111
- }
112
- });\n`;
113
- renderBody += ` document.querySelectorAll('[data-sinth-fullscreen]').forEach(function(el) {
114
- let exprId = el.dataset.sinthFullscreen;
115
- if (exprId) {
116
- try {
117
- let exprFn = __X[exprId];
118
- if (exprFn) {
119
- if (exprFn({})) {
120
- if (document.documentElement.requestFullscreen) document.documentElement.requestFullscreen();
121
- } else {
122
- if (document.fullscreenElement && document.exitFullscreen) document.exitFullscreen();
123
- }
124
- }
125
- } catch(e) {}
126
- }
127
- });\n`;
128
- if (needsDelay) {
129
- renderBody += ` setTimeout(function() {
130
- document.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
131
- document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
132
- }, 0);\n`;
133
- }
134
- renderBody += ` window.scrollTo(_sx, _sy);\n`;
135
-
136
- return renderBody;
1
+ import { MixedBlockEntry } from "../types.ts";
2
+
3
+ export function buildRenderBody(opts: {
4
+ bodyHTML: string;
5
+ logicBlocks: string[];
6
+ mixedBlocks: MixedBlockEntry[]
7
+ needsLogic: boolean;
8
+ needsMixed: boolean;
9
+ needsIf: boolean;
10
+ needsFor: boolean;
11
+ needsExpr: boolean;
12
+ needsDelay: boolean;
13
+ exprVarUpdates?: string;
14
+ }): string {
15
+ const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay, exprVarUpdates } = opts;
16
+ let renderBody = "";
17
+ renderBody += ` let _sx = window.scrollX, _sy = window.scrollY;\n`;
18
+
19
+ if (needsLogic) {
20
+ renderBody += logicBlocks.map(b => b.replace(/^/gm, " ")).join("\n") + "\n";
21
+ }
22
+
23
+ if (needsMixed) {
24
+ for (const mb of mixedBlocks) {
25
+ const ifJS = mb.ifJS ? mb.ifJS.trim() : "";
26
+ const elseJS = mb.elseJS ? mb.elseJS.trim() : "";
27
+ renderBody += ` (function() {
28
+ let __el = document.getElementById(${JSON.stringify(mb.replaceId || mb.id)});
29
+ if (__el) {
30
+ let __condFn = __X[${mb.conditionJS}];
31
+ if (__condFn ? __condFn() : false) {
32
+ ${ifJS}
33
+ __el.innerHTML = ${JSON.stringify(mb.ifHTML)};
34
+ } else {
35
+ ${elseJS}
36
+ __el.innerHTML = ${JSON.stringify(mb.elseHTML)};
37
+ }
38
+ __el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
39
+ __el.querySelectorAll('.sinth-expr').forEach(function(el) { el.classList.remove('sinth-expr'); });
40
+ __el.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);
41
+ ${needsDelay ? `__el.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
42
+ __el.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });` : ""}
43
+ }
44
+ })();\n`;
45
+ }
46
+ }
47
+
48
+ renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
49
+ let target = document.getElementById(el.dataset.sinthRemove);
50
+ if (target) target.remove();
51
+ });\n`;
52
+ if (needsFor) {
53
+ renderBody += ` document.querySelectorAll('template[data-sinth-for], template[data-sinth-for-expr]').forEach(sinthForBlock);\n`;
54
+ }
55
+ if (needsIf) {
56
+ renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(function(t) { var p = t.parentElement; while(p && p.tagName !== 'TEMPLATE') p = p.parentElement; if (!p || (!p.hasAttribute('data-sinth-for') && !p.hasAttribute('data-sinth-for-expr'))) sinthIfBlock(t); });\n`;
57
+ }
58
+ renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
59
+ try { if (document.activeElement !== el) { let v=el.dataset.sinthValue; el.value = __X[v] ? __X[v]({}) : ''; } } catch(e) {}
60
+ });\n`;
61
+ renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
62
+ try { let s=el.dataset.sinthStep; el.step = __X[s] ? __X[s]({}) : 1; } catch(e) {}
63
+ });\n`;
64
+ renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
65
+ try { let c=el.dataset.sinthChecked; el.checked = __X[c] ? !!__X[c]({}) : false; } catch(e) {}
66
+ });\n`;
67
+ if (exprVarUpdates) {
68
+ renderBody += ` ${exprVarUpdates}\n`;
69
+ }
70
+ if (bodyHTML.includes("data-sinth-checked-expr")) {
71
+ renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
72
+ try {
73
+ let exprFn = __X[el.dataset.sinthCheckedExpr];
74
+ if (exprFn) el.checked = !!exprFn({});
75
+ } catch(e) {}
76
+ });\n`;
77
+ }
78
+ if (needsDelay) {
79
+ renderBody += ` document.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
80
+ let newContent = '';
81
+ el.querySelectorAll('.sinth-expr').forEach(function(exprEl) {
82
+ let exprFn = __X[exprEl.dataset.exprId];
83
+ if (exprFn) newContent += exprFn({});
84
+ });
85
+ if (el._sinthLastContent === undefined) {
86
+ el._sinthLastContent = newContent;
87
+ } else if (el._sinthLastContent !== newContent) {
88
+ el._sinthLastContent = newContent;
89
+ delete el.dataset.sinthDelayDone;
90
+ }
91
+ });
92
+ document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });\n`;
93
+ }
94
+ if (needsExpr) {
95
+ renderBody += ` document.querySelectorAll('.sinth-expr').forEach(function(el) {
96
+ let delayParent = el.closest('[data-sinth-delay]');
97
+ if (!delayParent || delayParent.dataset.sinthDelayDone) {
98
+ sinthExpr(el);
99
+ }
100
+ });\n`;
101
+ }
102
+ renderBody += ` document.querySelectorAll('[data-sinth-hide]').forEach(function(el) {
103
+ let exprId = el.dataset.sinthHide;
104
+ if (exprId) {
105
+ try {
106
+ let exprFn = __X[exprId];
107
+ if (exprFn) el.style.display = exprFn({}) ? 'none' : '';
108
+ } catch(e) {}
109
+ } else {
110
+ el.style.display = 'none';
111
+ }
112
+ });\n`;
113
+ renderBody += ` document.querySelectorAll('[data-sinth-fullscreen]').forEach(function(el) {
114
+ let exprId = el.dataset.sinthFullscreen;
115
+ if (exprId) {
116
+ try {
117
+ let exprFn = __X[exprId];
118
+ if (exprFn) {
119
+ if (exprFn({})) {
120
+ if (document.documentElement.requestFullscreen) document.documentElement.requestFullscreen();
121
+ } else {
122
+ if (document.fullscreenElement && document.exitFullscreen) document.exitFullscreen();
123
+ }
124
+ }
125
+ } catch(e) {}
126
+ }
127
+ });\n`;
128
+ if (needsDelay) {
129
+ renderBody += ` setTimeout(function() {
130
+ document.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
131
+ document.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
132
+ }, 0);\n`;
133
+ }
134
+ renderBody += ` window.scrollTo(_sx, _sy);\n`;
135
+
136
+ return renderBody;
137
137
  }