@yannosay/sinth 1.8.1 → 1.8.3

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,333 +1,333 @@
1
- export function generateHelpers(opts: {
2
- needsExpr: boolean;
3
- needsIf: boolean;
4
- needsFor: boolean;
5
- needsDelay: boolean;
6
- needsMixed: boolean;
7
- }): string {
8
- const { needsExpr, needsIf, needsFor, needsDelay, needsMixed } = opts;
9
- let helpers = "";
10
-
11
- if (needsExpr || needsIf || needsFor) {
12
- helpers += `
13
- var sinthExpr = function(el) {
14
- try {
15
- let exprFn = __X[el.dataset.exprId];
16
- if (exprFn) el.textContent = exprFn({});
17
- } catch(e) {}
18
- };
19
- `;
20
- }
21
-
22
- if (needsIf || needsMixed) {
23
- helpers += `
24
- function sinthReplaceInsert(t, anchor, ifId, replaceId) {
25
- if (anchor) {
26
- let cur = anchor.nextSibling;
27
- while (cur && cur !== t) { let nx = cur.nextSibling; cur.remove(); cur = nx; }
28
- } else {
29
- anchor = document.createElement('span');
30
- anchor.style.display = 'none';
31
- anchor.dataset.sinthIfAnchor = ifId;
32
- t.parentNode.insertBefore(anchor, t);
33
- }
34
- let _rp = null, _rpParent = null, _rpNext = null;
35
- if (replaceId) {
36
- if (anchor._sinthReplaced) {
37
- _rp = anchor._sinthReplaced;
38
- _rpParent = anchor._sinthReplacedParent;
39
- _rpNext = anchor._sinthReplacedNext;
40
- if (_rp.parentNode) _rp.parentNode.removeChild(_rp);
41
- } else {
42
- _rp = document.getElementById(replaceId);
43
- if (_rp) {
44
- _rpParent = _rp.parentNode;
45
- _rpNext = _rp.nextSibling;
46
- _rp.parentNode.removeChild(_rp);
47
- anchor._sinthReplaced = _rp;
48
- anchor._sinthReplacedParent = _rpParent;
49
- anchor._sinthReplacedNext = _rpNext;
50
- }
51
- }
52
- }
53
- let frag = document.createRange().createContextualFragment(t.innerHTML);
54
- frag.querySelectorAll('.sinth-expr').forEach(sinthExpr);
55
- if (${needsDelay}) {
56
- frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
57
- frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
58
- }
59
- let fragFirst = frag.firstChild;
60
- let fragLast = frag.lastChild;
61
- if (_rpParent && _rpNext) {
62
- _rpParent.insertBefore(frag, _rpNext);
63
- } else if (_rpParent) {
64
- _rpParent.appendChild(frag);
65
- } else {
66
- t.parentNode.insertBefore(frag, t);
67
- }
68
- if (replaceId && anchor) {
69
- anchor._sinthInsertedFirst = fragFirst;
70
- anchor._sinthInsertedLast = fragLast;
71
- }
72
- return anchor;
73
- }
74
- `;
75
- }
76
-
77
- if (needsDelay) {
78
- helpers += `
79
- function sinthDelay(el) {
80
- if (el.dataset.sinthDelayDone) { return; }
81
- el.dataset.sinthDelayDone = '1';
82
- let ms = parseInt(el.dataset.sinthDelay) || 0;
83
- let hideEl = el.dataset.sinthDelayHide !== 'false';
84
- let show = function() {
85
- if (hideEl) el.style.display = '';
86
- el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
87
- };
88
- if (hideEl) {
89
- el.style.display = 'none';
90
- if (ms > 0) setTimeout(show, ms);
91
- else show();
92
- } else {
93
- if (ms > 0) setTimeout(show, ms);
94
- else show();
95
- }
96
- }
97
- function sinthDelayExpr(el, _ctx) {
98
- if (el.dataset.sinthDelayDone) return;
99
- el.dataset.sinthDelayDone = '1';
100
- _ctx = _ctx || {};
101
- try {
102
- let fn = __X[el.dataset.sinthDelayExprId];
103
- let ms = fn ? parseInt(fn(_ctx)) || 0 : 0;
104
- let hideEl = el.dataset.sinthDelayHide !== 'false';
105
- let show = function() {
106
- if (hideEl) el.style.display = '';
107
- el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
108
- };
109
- if (hideEl) {
110
- el.style.display = 'none';
111
- if (ms > 0) setTimeout(show, ms);
112
- else show();
113
- } else {
114
- if (ms > 0) setTimeout(show, ms);
115
- else show();
116
- }
117
- } catch(e) {}
118
- }
119
- `;
120
- }
121
-
122
- if (needsIf) {
123
- helpers += `
124
- function sinthIfBlock(t) {
125
- let ifId = t.dataset.sinthIfId;
126
- let anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
127
- let condFn = __X[t.dataset.sinthIfExpr];
128
- let cond = condFn ? condFn() : false;
129
- if (cond) {
130
- let elseA = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
131
- if (elseA) {
132
- let c = elseA.nextSibling;
133
- while (c && c !== t) { let n = c.nextSibling; c.remove(); c = n; }
134
- elseA.remove();
135
- }
136
- if (t.dataset.sinthIfPersist === "true" && anchor) return;
137
- if (anchor && anchor._sinthReplaced && !t.dataset.sinthIfDelayHide) return;
138
- let _hasContent = anchor || t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
139
- if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
140
- let dms = parseInt(t.dataset.sinthIfDelay) || 0;
141
- setTimeout(function() {
142
- sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
143
- }, dms);
144
- } else {
145
- anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
146
- }
147
- } else {
148
- let runElse = function() {
149
- if (t.dataset.sinthIfPersist === "true") return;
150
- if (anchor) {
151
- if (anchor._sinthReplaced) {
152
- let insFirst = anchor._sinthInsertedFirst;
153
- let insLast = anchor._sinthInsertedLast;
154
- let rpParent = anchor._sinthReplacedParent;
155
- let rpNext = anchor._sinthReplacedNext;
156
- if (insFirst && insLast) {
157
- let cur = insFirst;
158
- while (cur && cur !== insLast) {
159
- let next = cur.nextSibling;
160
- cur.remove();
161
- cur = next;
162
- }
163
- if (insLast) insLast.remove();
164
- }
165
- if (rpParent && rpNext) {
166
- rpParent.insertBefore(anchor._sinthReplaced, rpNext);
167
- } else if (rpParent) {
168
- rpParent.appendChild(anchor._sinthReplaced);
169
- }
170
- if (anchor._sinthReplaced) {
171
- anchor._sinthReplaced.querySelectorAll('.sinth-expr').forEach(sinthExpr);
172
- if (anchor._sinthReplaced.dataset.sinthDelay) {
173
- delete anchor._sinthReplaced.dataset.sinthDelayDone;
174
- sinthDelay(anchor._sinthReplaced);
175
- }
176
- anchor._sinthReplaced = null;
177
- anchor._sinthReplacedParent = null;
178
- anchor._sinthReplacedNext = null;
179
- anchor._sinthInsertedFirst = null;
180
- anchor._sinthInsertedLast = null;
181
- }
182
- } else {
183
- let cur2 = anchor.nextSibling;
184
- while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
185
- }
186
- anchor.remove();
187
- }
188
- let elseT = t.nextElementSibling;
189
- if (elseT && elseT.hasAttribute('data-sinth-else')) {
190
- let elseIfId = elseT.dataset.sinthIfId;
191
- let ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + elseIfId + '"]');
192
- if (ea) {
193
- let cur3 = ea.nextSibling;
194
- while (cur3 && cur3 !== t) { let nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
195
- } else {
196
- ea = document.createElement('span');
197
- ea.style.display = 'none';
198
- ea.dataset.sinthIfAnchor = '__else__' + elseIfId;
199
- t.parentNode.insertBefore(ea, t);
200
- }
201
- let ef = document.createRange().createContextualFragment(elseT.innerHTML);
202
- ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
203
- if (${needsDelay}) {
204
- ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
205
- ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
206
- }
207
- let innerTemplates = ef.querySelectorAll('template[data-sinth-if-expr]');
208
- t.parentNode.insertBefore(ef, t);
209
- for (let i = 0; i < innerTemplates.length; i++) sinthIfBlock(innerTemplates[i]);
210
- ef.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
211
- delete el.dataset.sinthDelayDone;
212
- sinthDelay(el);
213
- });
214
- } else {
215
- let ea2 = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
216
- if (ea2) {
217
- let ec = ea2.nextSibling;
218
- while (ec && ec !== t) { let en = ec.nextSibling; ec.remove(); ec = en; }
219
- ea2.remove();
220
- }
221
- if (anchor) {
222
- let ac = anchor.nextSibling;
223
- while (ac && ac !== t) { let an = ac.nextSibling; ac.remove(); ac = an; }
224
- }
225
- }
226
- };
227
- let elseT = t.nextElementSibling;
228
- if (elseT && elseT.hasAttribute('data-sinth-else') && elseT.dataset.sinthIfDelayHide === 'false' && elseT.dataset.sinthIfDelay && anchor) {
229
- let edms = parseInt(elseT.dataset.sinthIfDelay) || 0;
230
- setTimeout(runElse, edms);
231
- } else {
232
- runElse();
233
- }
234
- }
235
- }
236
- `;
237
- }
238
-
239
- if (needsFor) {
240
- helpers += `
241
- function hashString(str) {
242
- let hash = 0, i, chr;
243
- for (i = 0; i < str.length; i++) {
244
- chr = str.charCodeAt(i);
245
- hash = ((hash << 5) - hash) + chr;
246
- hash |= 0;
247
- }
248
- return String(hash);
249
- }
250
- function sinthForBlock(t) {
251
- t.querySelectorAll('template[data-sinth-if-expr]').forEach(function(innerT) {
252
- innerT.dataset.sinthForHandled = '1';
253
- });
254
- let source = _sinthForData[t.dataset.sinthFor];
255
- if (source === undefined) source = [];
256
- let newHash = '';
257
- try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
258
- if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) return;
259
- t.dataset.sinthForHash = newHash;
260
- let isObj = (typeof source === 'object' && source !== null && !Array.isArray(source));
261
- let entries;
262
- if (isObj) {
263
- entries = Object.entries(source);
264
- } else {
265
- if (!Array.isArray(source)) source = [];
266
- entries = source.map(function(item, index) { return [index, item]; });
267
- }
268
- let anchorId = t.dataset.sinthForExpr || t.dataset.sinthFor;
269
- let anchor = null;
270
- if (t.parentNode) {
271
- let all = t.parentNode.querySelectorAll('[data-sinth-for-anchor]');
272
- for (let i = 0; i < all.length; i++) {
273
- if (all[i].dataset.sinthForAnchor === anchorId) { anchor = all[i]; break; }
274
- }
275
- }
276
- if (anchor) {
277
- let cur2 = anchor.nextSibling;
278
- while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
279
- anchor.remove();
280
- }
281
- let fa = document.createElement('span');
282
- fa.style.display = 'none';
283
- fa.dataset.sinthForAnchor = anchorId;
284
- t.parentNode && t.parentNode.insertBefore(fa, t);
285
- let _loopIdx = 0;
286
- let tplContent = t.innerHTML;
287
- entries.forEach(function(entry) {
288
- let _k = entry[0], _v = entry[1];
289
- let _item = t.dataset.sinthItem || '__item__';
290
- let _key = t.dataset.sinthKey || null;
291
- let _idx = t.dataset.sinthIndex || null;
292
- _loopIdx++;
293
- let loopCtx = {};
294
- if (_item) loopCtx[_item] = _v;
295
- if (_key) loopCtx[_key] = _k;
296
- if (_idx) loopCtx[_idx] = _loopIdx - 1;
297
- let frag = document.createRange().createContextualFragment(tplContent);
298
- frag.querySelectorAll('.sinth-expr').forEach(function(el) {
299
- try { let fn = __X[el.dataset.exprId]; if (fn) el.textContent = fn(loopCtx); } catch(e) {}
300
- el.classList.remove('sinth-expr');
301
- });
302
- frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
303
- sinthDelayExpr(el, loopCtx);
304
- });
305
- frag.querySelectorAll('[onclick*="_ctx."]').forEach(function(el) {
306
- var onclick = el.getAttribute('onclick');
307
- for (var key in loopCtx) {
308
- onclick = onclick.split('_ctx.' + key).join(JSON.stringify(loopCtx[key]));
309
- }
310
- el.setAttribute('onclick', onclick);
311
- });
312
- frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
313
- let condFn = __X[ifT.dataset.sinthIfExpr];
314
- let cond = condFn ? condFn(loopCtx) : false;
315
- if (cond) {
316
- let ifFrag = document.createRange().createContextualFragment(ifT.innerHTML);
317
- ifFrag.querySelectorAll('.sinth-expr').forEach(function(el2) {
318
- try { let fn2 = __X[el2.dataset.exprId]; if (fn2) el2.textContent = fn2(loopCtx); } catch(e) {}
319
- el2.classList.remove('sinth-expr');
320
- });
321
- ifT.replaceWith(ifFrag);
322
- } else {
323
- ifT.remove();
324
- }
325
- });
326
- t.parentNode && t.parentNode.insertBefore(frag, t);
327
- });
328
- }
329
- `;
330
- }
331
-
332
- return helpers;
1
+ export function generateHelpers(opts: {
2
+ needsExpr: boolean;
3
+ needsIf: boolean;
4
+ needsFor: boolean;
5
+ needsDelay: boolean;
6
+ needsMixed: boolean;
7
+ }): string {
8
+ const { needsExpr, needsIf, needsFor, needsDelay, needsMixed } = opts;
9
+ let helpers = "";
10
+
11
+ if (needsExpr || needsIf || needsFor) {
12
+ helpers += `
13
+ var sinthExpr = function(el) {
14
+ try {
15
+ let exprFn = __X[el.dataset.exprId];
16
+ if (exprFn) el.textContent = exprFn({});
17
+ } catch(e) {}
18
+ };
19
+ `;
20
+ }
21
+
22
+ if (needsIf || needsMixed) {
23
+ helpers += `
24
+ function sinthReplaceInsert(t, anchor, ifId, replaceId) {
25
+ if (anchor) {
26
+ let cur = anchor.nextSibling;
27
+ while (cur && cur !== t) { let nx = cur.nextSibling; cur.remove(); cur = nx; }
28
+ } else {
29
+ anchor = document.createElement('span');
30
+ anchor.style.display = 'none';
31
+ anchor.dataset.sinthIfAnchor = ifId;
32
+ t.parentNode.insertBefore(anchor, t);
33
+ }
34
+ let _rp = null, _rpParent = null, _rpNext = null;
35
+ if (replaceId) {
36
+ if (anchor._sinthReplaced) {
37
+ _rp = anchor._sinthReplaced;
38
+ _rpParent = anchor._sinthReplacedParent;
39
+ _rpNext = anchor._sinthReplacedNext;
40
+ if (_rp.parentNode) _rp.parentNode.removeChild(_rp);
41
+ } else {
42
+ _rp = document.getElementById(replaceId);
43
+ if (_rp) {
44
+ _rpParent = _rp.parentNode;
45
+ _rpNext = _rp.nextSibling;
46
+ _rp.parentNode.removeChild(_rp);
47
+ anchor._sinthReplaced = _rp;
48
+ anchor._sinthReplacedParent = _rpParent;
49
+ anchor._sinthReplacedNext = _rpNext;
50
+ }
51
+ }
52
+ }
53
+ let frag = document.createRange().createContextualFragment(t.innerHTML);
54
+ frag.querySelectorAll('.sinth-expr').forEach(sinthExpr);
55
+ if (${needsDelay}) {
56
+ frag.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
57
+ frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
58
+ }
59
+ let fragFirst = frag.firstChild;
60
+ let fragLast = frag.lastChild;
61
+ if (_rpParent && _rpNext) {
62
+ _rpParent.insertBefore(frag, _rpNext);
63
+ } else if (_rpParent) {
64
+ _rpParent.appendChild(frag);
65
+ } else {
66
+ t.parentNode.insertBefore(frag, t);
67
+ }
68
+ if (replaceId && anchor) {
69
+ anchor._sinthInsertedFirst = fragFirst;
70
+ anchor._sinthInsertedLast = fragLast;
71
+ }
72
+ return anchor;
73
+ }
74
+ `;
75
+ }
76
+
77
+ if (needsDelay) {
78
+ helpers += `
79
+ function sinthDelay(el) {
80
+ if (el.dataset.sinthDelayDone) { return; }
81
+ el.dataset.sinthDelayDone = '1';
82
+ let ms = parseInt(el.dataset.sinthDelay) || 0;
83
+ let hideEl = el.dataset.sinthDelayHide !== 'false';
84
+ let show = function() {
85
+ if (hideEl) el.style.display = '';
86
+ el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
87
+ };
88
+ if (hideEl) {
89
+ el.style.display = 'none';
90
+ if (ms > 0) setTimeout(show, ms);
91
+ else show();
92
+ } else {
93
+ if (ms > 0) setTimeout(show, ms);
94
+ else show();
95
+ }
96
+ }
97
+ function sinthDelayExpr(el, _ctx) {
98
+ if (el.dataset.sinthDelayDone) return;
99
+ el.dataset.sinthDelayDone = '1';
100
+ _ctx = _ctx || {};
101
+ try {
102
+ let fn = __X[el.dataset.sinthDelayExprId];
103
+ let ms = fn ? parseInt(fn(_ctx)) || 0 : 0;
104
+ let hideEl = el.dataset.sinthDelayHide !== 'false';
105
+ let show = function() {
106
+ if (hideEl) el.style.display = '';
107
+ el.querySelectorAll('.sinth-expr').forEach(sinthExpr);
108
+ };
109
+ if (hideEl) {
110
+ el.style.display = 'none';
111
+ if (ms > 0) setTimeout(show, ms);
112
+ else show();
113
+ } else {
114
+ if (ms > 0) setTimeout(show, ms);
115
+ else show();
116
+ }
117
+ } catch(e) {}
118
+ }
119
+ `;
120
+ }
121
+
122
+ if (needsIf) {
123
+ helpers += `
124
+ function sinthIfBlock(t) {
125
+ let ifId = t.dataset.sinthIfId;
126
+ let anchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + ifId + '"]');
127
+ let condFn = __X[t.dataset.sinthIfExpr];
128
+ let cond = condFn ? condFn() : false;
129
+ if (cond) {
130
+ let elseA = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
131
+ if (elseA) {
132
+ let c = elseA.nextSibling;
133
+ while (c && c !== t) { let n = c.nextSibling; c.remove(); c = n; }
134
+ elseA.remove();
135
+ }
136
+ if (t.dataset.sinthIfPersist === "true" && anchor) return;
137
+ if (anchor && anchor._sinthReplaced && !t.dataset.sinthIfDelayHide) return;
138
+ let _hasContent = anchor || t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
139
+ if (t.dataset.sinthIfDelayHide === 'false' && t.dataset.sinthIfDelay && _hasContent) {
140
+ let dms = parseInt(t.dataset.sinthIfDelay) || 0;
141
+ setTimeout(function() {
142
+ sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
143
+ }, dms);
144
+ } else {
145
+ anchor = sinthReplaceInsert(t, anchor, ifId, t.dataset.sinthIfReplace);
146
+ }
147
+ } else {
148
+ let runElse = function() {
149
+ if (t.dataset.sinthIfPersist === "true") return;
150
+ if (anchor) {
151
+ if (anchor._sinthReplaced) {
152
+ let insFirst = anchor._sinthInsertedFirst;
153
+ let insLast = anchor._sinthInsertedLast;
154
+ let rpParent = anchor._sinthReplacedParent;
155
+ let rpNext = anchor._sinthReplacedNext;
156
+ if (insFirst && insLast) {
157
+ let cur = insFirst;
158
+ while (cur && cur !== insLast) {
159
+ let next = cur.nextSibling;
160
+ cur.remove();
161
+ cur = next;
162
+ }
163
+ if (insLast) insLast.remove();
164
+ }
165
+ if (rpParent && rpNext) {
166
+ rpParent.insertBefore(anchor._sinthReplaced, rpNext);
167
+ } else if (rpParent) {
168
+ rpParent.appendChild(anchor._sinthReplaced);
169
+ }
170
+ if (anchor._sinthReplaced) {
171
+ anchor._sinthReplaced.querySelectorAll('.sinth-expr').forEach(sinthExpr);
172
+ if (anchor._sinthReplaced.dataset.sinthDelay) {
173
+ delete anchor._sinthReplaced.dataset.sinthDelayDone;
174
+ sinthDelay(anchor._sinthReplaced);
175
+ }
176
+ anchor._sinthReplaced = null;
177
+ anchor._sinthReplacedParent = null;
178
+ anchor._sinthReplacedNext = null;
179
+ anchor._sinthInsertedFirst = null;
180
+ anchor._sinthInsertedLast = null;
181
+ }
182
+ } else {
183
+ let cur2 = anchor.nextSibling;
184
+ while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
185
+ }
186
+ anchor.remove();
187
+ }
188
+ let elseT = t.nextElementSibling;
189
+ if (elseT && elseT.hasAttribute('data-sinth-else')) {
190
+ let elseIfId = elseT.dataset.sinthIfId;
191
+ let ea = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + elseIfId + '"]');
192
+ if (ea) {
193
+ let cur3 = ea.nextSibling;
194
+ while (cur3 && cur3 !== t) { let nx3 = cur3.nextSibling; cur3.remove(); cur3 = nx3; }
195
+ } else {
196
+ ea = document.createElement('span');
197
+ ea.style.display = 'none';
198
+ ea.dataset.sinthIfAnchor = '__else__' + elseIfId;
199
+ t.parentNode.insertBefore(ea, t);
200
+ }
201
+ let ef = document.createRange().createContextualFragment(elseT.innerHTML);
202
+ ef.querySelectorAll('.sinth-expr').forEach(sinthExpr);
203
+ if (${needsDelay}) {
204
+ ef.querySelectorAll('[data-sinth-delay]').forEach(sinthDelay);
205
+ ef.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) { sinthDelayExpr(el, {}); });
206
+ }
207
+ let innerTemplates = ef.querySelectorAll('template[data-sinth-if-expr]');
208
+ t.parentNode.insertBefore(ef, t);
209
+ for (let i = 0; i < innerTemplates.length; i++) sinthIfBlock(innerTemplates[i]);
210
+ ef.querySelectorAll('[data-sinth-delay]').forEach(function(el) {
211
+ delete el.dataset.sinthDelayDone;
212
+ sinthDelay(el);
213
+ });
214
+ } else {
215
+ let ea2 = t.parentNode.querySelector('[data-sinth-if-anchor="__else__' + ifId + '"]');
216
+ if (ea2) {
217
+ let ec = ea2.nextSibling;
218
+ while (ec && ec !== t) { let en = ec.nextSibling; ec.remove(); ec = en; }
219
+ ea2.remove();
220
+ }
221
+ if (anchor) {
222
+ let ac = anchor.nextSibling;
223
+ while (ac && ac !== t) { let an = ac.nextSibling; ac.remove(); ac = an; }
224
+ }
225
+ }
226
+ };
227
+ let elseT = t.nextElementSibling;
228
+ if (elseT && elseT.hasAttribute('data-sinth-else') && elseT.dataset.sinthIfDelayHide === 'false' && elseT.dataset.sinthIfDelay && anchor) {
229
+ let edms = parseInt(elseT.dataset.sinthIfDelay) || 0;
230
+ setTimeout(runElse, edms);
231
+ } else {
232
+ runElse();
233
+ }
234
+ }
235
+ }
236
+ `;
237
+ }
238
+
239
+ if (needsFor) {
240
+ helpers += `
241
+ function hashString(str) {
242
+ let hash = 0, i, chr;
243
+ for (i = 0; i < str.length; i++) {
244
+ chr = str.charCodeAt(i);
245
+ hash = ((hash << 5) - hash) + chr;
246
+ hash |= 0;
247
+ }
248
+ return String(hash);
249
+ }
250
+ function sinthForBlock(t) {
251
+ t.querySelectorAll('template[data-sinth-if-expr]').forEach(function(innerT) {
252
+ innerT.dataset.sinthForHandled = '1';
253
+ });
254
+ let source = _sinthForData[t.dataset.sinthFor];
255
+ if (source === undefined) source = [];
256
+ let newHash = '';
257
+ try { newHash = hashString(JSON.stringify(source)); } catch(e) { newHash = ''; }
258
+ if (t.dataset.sinthForHash && t.dataset.sinthForHash === newHash) return;
259
+ t.dataset.sinthForHash = newHash;
260
+ let isObj = (typeof source === 'object' && source !== null && !Array.isArray(source));
261
+ let entries;
262
+ if (isObj) {
263
+ entries = Object.entries(source);
264
+ } else {
265
+ if (!Array.isArray(source)) source = [];
266
+ entries = source.map(function(item, index) { return [index, item]; });
267
+ }
268
+ let anchorId = t.dataset.sinthForExpr || t.dataset.sinthFor;
269
+ let anchor = null;
270
+ if (t.parentNode) {
271
+ let all = t.parentNode.querySelectorAll('[data-sinth-for-anchor]');
272
+ for (let i = 0; i < all.length; i++) {
273
+ if (all[i].dataset.sinthForAnchor === anchorId) { anchor = all[i]; break; }
274
+ }
275
+ }
276
+ if (anchor) {
277
+ let cur2 = anchor.nextSibling;
278
+ while (cur2 && cur2 !== t) { let nx2 = cur2.nextSibling; cur2.remove(); cur2 = nx2; }
279
+ anchor.remove();
280
+ }
281
+ let fa = document.createElement('span');
282
+ fa.style.display = 'none';
283
+ fa.dataset.sinthForAnchor = anchorId;
284
+ t.parentNode && t.parentNode.insertBefore(fa, t);
285
+ let _loopIdx = 0;
286
+ let tplContent = t.innerHTML;
287
+ entries.forEach(function(entry) {
288
+ let _k = entry[0], _v = entry[1];
289
+ let _item = t.dataset.sinthItem || '__item__';
290
+ let _key = t.dataset.sinthKey || null;
291
+ let _idx = t.dataset.sinthIndex || null;
292
+ _loopIdx++;
293
+ let loopCtx = {};
294
+ if (_item) loopCtx[_item] = _v;
295
+ if (_key) loopCtx[_key] = _k;
296
+ if (_idx) loopCtx[_idx] = _loopIdx - 1;
297
+ let frag = document.createRange().createContextualFragment(tplContent);
298
+ frag.querySelectorAll('.sinth-expr').forEach(function(el) {
299
+ try { let fn = __X[el.dataset.exprId]; if (fn) el.textContent = fn(loopCtx); } catch(e) {}
300
+ el.classList.remove('sinth-expr');
301
+ });
302
+ frag.querySelectorAll('[data-sinth-delay-expr-id]').forEach(function(el) {
303
+ sinthDelayExpr(el, loopCtx);
304
+ });
305
+ frag.querySelectorAll('[onclick*="_ctx."]').forEach(function(el) {
306
+ var onclick = el.getAttribute('onclick');
307
+ for (var key in loopCtx) {
308
+ onclick = onclick.split('_ctx.' + key).join(JSON.stringify(loopCtx[key]));
309
+ }
310
+ el.setAttribute('onclick', onclick);
311
+ });
312
+ frag.querySelectorAll('template[data-sinth-if-expr]').forEach(function(ifT) {
313
+ let condFn = __X[ifT.dataset.sinthIfExpr];
314
+ let cond = condFn ? condFn(loopCtx) : false;
315
+ if (cond) {
316
+ let ifFrag = document.createRange().createContextualFragment(ifT.innerHTML);
317
+ ifFrag.querySelectorAll('.sinth-expr').forEach(function(el2) {
318
+ try { let fn2 = __X[el2.dataset.exprId]; if (fn2) el2.textContent = fn2(loopCtx); } catch(e) {}
319
+ el2.classList.remove('sinth-expr');
320
+ });
321
+ ifT.replaceWith(ifFrag);
322
+ } else {
323
+ ifT.remove();
324
+ }
325
+ });
326
+ t.parentNode && t.parentNode.insertBefore(frag, t);
327
+ });
328
+ }
329
+ `;
330
+ }
331
+
332
+ return helpers;
333
333
  }