@cossackframework/renderer 0.5.2 → 0.7.0

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,449 +0,0 @@
1
- //#region src/component.ts
2
- var e = (e) => typeof e == "object" && !!e && e._type === "COMPONENT", t = [], n = (e) => t.push(e), r = () => t.pop(), i = class {
3
- static properties = {};
4
- static _isCossackElement = !0;
5
- static components = {};
6
- children = null;
7
- props = {};
8
- __parent = null;
9
- _id = "";
10
- _childCounter = 0;
11
- __providedContexts = /* @__PURE__ */ new Map();
12
- __properties = /* @__PURE__ */ new Map();
13
- __updatePromise = null;
14
- __changedProperties = /* @__PURE__ */ new Map();
15
- __renderListeners = /* @__PURE__ */ new Set();
16
- __controllers = /* @__PURE__ */ new Set();
17
- __hasConnected = !1;
18
- __eventListeners = /* @__PURE__ */ new Map();
19
- constructor() {
20
- this.initialize();
21
- }
22
- static get currentRenderingInstance() {
23
- return t.length > 0 ? t[t.length - 1] : null;
24
- }
25
- resetRenderState() {
26
- this._childCounter = 0;
27
- }
28
- addController(e) {
29
- this.__controllers.add(e), this.__hasConnected && e.hostConnected && e.hostConnected();
30
- }
31
- removeController(e) {
32
- this.__controllers.delete(e);
33
- }
34
- get updateComplete() {
35
- return this.__updatePromise || Promise.resolve(!0);
36
- }
37
- initialize() {
38
- let e = this.constructor.properties;
39
- if (e) for (let [t, n] of Object.entries(e)) this.createProperty(t, n);
40
- }
41
- createProperty(e, t) {
42
- Object.prototype.hasOwnProperty.call(this, e) && this.__properties.set(e, this[e]), Object.defineProperty(this, e, {
43
- get() {
44
- return this.__properties.get(e);
45
- },
46
- set(t) {
47
- let n = this.__properties.get(e);
48
- n !== t && (this.__properties.set(e, t), this.requestUpdate(e, n));
49
- },
50
- configurable: !0,
51
- enumerable: !0
52
- });
53
- }
54
- provide(e, t) {
55
- this.__providedContexts.set(e, t);
56
- }
57
- consume(e) {
58
- let t = this.__parent;
59
- for (; t;) {
60
- if (t.__providedContexts.has(e)) return t.__providedContexts.get(e);
61
- t = t.__parent;
62
- }
63
- return e.defaultValue;
64
- }
65
- requestUpdate(e, t) {
66
- return e !== void 0 && (this.__changedProperties.has(e) || this.__changedProperties.set(e, t)), this.__updatePromise ||= this.performUpdate(), this.__updatePromise;
67
- }
68
- async performUpdate() {
69
- await Promise.resolve();
70
- let e = !1;
71
- try {
72
- if (e = this.shouldUpdate(this.__changedProperties), e) {
73
- this.__controllers.forEach((e) => e.hostUpdate && e.hostUpdate()), this.willUpdate(this.__changedProperties), this.resetRenderState(), n(this);
74
- let e = this.render();
75
- this.__notifyListeners(e), r(), this.updated(this.__changedProperties), this.__controllers.forEach((e) => e.hostUpdated && e.hostUpdated());
76
- }
77
- } catch (e) {
78
- console.error("Error during update:", e), t[t.length - 1] === this && r();
79
- }
80
- return this.__changedProperties = /* @__PURE__ */ new Map(), this.__updatePromise = null, e;
81
- }
82
- __notifyListeners(e) {
83
- this.__renderListeners.forEach((t) => t(e));
84
- }
85
- addRenderListener(e) {
86
- this.__renderListeners.add(e);
87
- }
88
- removeRenderListener(e) {
89
- this.__renderListeners.delete(e);
90
- }
91
- mount(e) {
92
- this.addRenderListener((t) => {
93
- t && (h(t) ? O(t, e) : O(f`${t}`, e));
94
- }), this.requestUpdate();
95
- }
96
- addEventListener(e, t, n) {
97
- t && (this.__eventListeners.has(e) || this.__eventListeners.set(e, /* @__PURE__ */ new Set()), this.__eventListeners.get(e).add(t));
98
- }
99
- removeEventListener(e, t, n) {
100
- if (!t) return;
101
- let r = this.__eventListeners.get(e);
102
- r && r.delete(t);
103
- }
104
- dispatchEvent(e) {
105
- let t = this.__eventListeners.get(e.type);
106
- return t && t.forEach((t) => {
107
- typeof t == "function" ? t.call(this, e) : t.handleEvent(e);
108
- }), !e.defaultPrevented;
109
- }
110
- shouldUpdate(e) {
111
- return !0;
112
- }
113
- willUpdate(e) {}
114
- render() {
115
- return null;
116
- }
117
- updated(e) {}
118
- connectedCallback() {
119
- this.__hasConnected = !0, this.__controllers.forEach((e) => e.hostConnected && e.hostConnected());
120
- }
121
- disconnectedCallback() {
122
- this.__hasConnected = !1, this.__controllers.forEach((e) => e.hostDisconnected && e.hostDisconnected());
123
- }
124
- }, a = class {
125
- value;
126
- constructor(e) {
127
- this.value = e;
128
- }
129
- }, o = class {
130
- items;
131
- keyFn;
132
- templateFn;
133
- constructor(e, t, n) {
134
- this.items = e, this.keyFn = t, this.templateFn = n;
135
- }
136
- }, s = (e) => new a(e), c = (e, t, n) => n === void 0 ? new o(e, (e, t) => t, t) : new o(e, t, n), l = (e) => Object.keys(e).filter((t) => e[t]).join(" "), u = (e) => Object.keys(e).filter((t) => e[t] != null).map((t) => `${t}:${e[t]}`).join(";"), d = class {
137
- strings;
138
- values;
139
- _cossack_template_result = !0;
140
- constructor(e, t) {
141
- this.strings = e, this.values = t;
142
- }
143
- }, f = (e, ...t) => new d(e, t), p = f, m = (e, t = {}, n) => f`${{
144
- _type: "COMPONENT",
145
- clazz: e,
146
- props: t,
147
- children: n
148
- }}`, h = (e) => typeof e == "object" && !!e && e._cossack_template_result === !0, g = class {
149
- value;
150
- constructor(e) {
151
- this.value = e;
152
- }
153
- }, _ = (e) => new g(e), v = (e) => e instanceof g, y = (e) => String(e).replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;"), b = (t) => {
154
- if (t == null || t === !1) return "";
155
- if (Array.isArray(t)) return t.map(b).join("");
156
- if (h(t)) return C(t);
157
- if (t instanceof a) return b(t.value);
158
- if (t instanceof o) return t.items.map((e, n) => b(t.templateFn(e, n))).join("");
159
- if (e(t)) {
160
- let e = new t.clazz();
161
- Object.assign(e, t.props), "props" in e && (e.props = t.props), e.children = t.children, e.__parent = i.currentRenderingInstance, e.__parent && (e._id = `${e.__parent._id}:${e.__parent._childCounter++}`), e.connectedCallback(), n(e), e.willUpdate(/* @__PURE__ */ new Map());
162
- let a = e.render(), o = "";
163
- return a && (o = h(a) ? C(a) : C(f`${a}`)), r(), o;
164
- }
165
- return v(t) ? t.value : typeof t == "function" || typeof t == "object" && t && "value" in t ? "" : y(t);
166
- }, x = (e) => {
167
- let t = "";
168
- if (typeof e == "object" && e) for (let [n, r] of Object.entries(e)) {
169
- if (n.startsWith("@")) continue;
170
- let e = n, i = r;
171
- if (n.startsWith("?")) {
172
- r && (t += ` ${n.slice(1)}`);
173
- continue;
174
- }
175
- n.startsWith(".") && (e = n.slice(1)), typeof i == "boolean" ? i && (t += ` ${e}`) : typeof i == "function" || i != null && (t += ` ${e}="${y(i)}"`);
176
- }
177
- return t;
178
- }, S = class {
179
- resultObj;
180
- result = "";
181
- stringIdx = 0;
182
- charIdxForNext = 0;
183
- constructor(e) {
184
- this.resultObj = e;
185
- }
186
- scan() {
187
- let { strings: e, values: t } = this.resultObj;
188
- for (; this.stringIdx < e.length;) {
189
- let n = e[this.stringIdx].substring(this.charIdxForNext || 0), r = n.match(/(\.\.\.|[.@?]?[a-zA-Z0-9_-]+)=["']?$/);
190
- if (this.result += n, this.charIdxForNext = 0, this.stringIdx < e.length - 1) {
191
- let n = t[this.stringIdx];
192
- if (r) {
193
- let t = r[0], i = r[1];
194
- this.result = this.result.substring(0, this.result.length - t.length);
195
- let a = t.endsWith("\"") ? "\"" : t.endsWith("'") ? "'" : "", o = !0;
196
- if (i === "..." ? (this.result = this.result.trimEnd(), this.result += x(n)) : i.startsWith("@") ? this.result = this.result.trimEnd() : i.startsWith("?") ? n ? (this.result = this.result.trimEnd(), this.result += ` ${i.slice(1)}`) : this.result = this.result.trimEnd() : i.startsWith(".") ? n != null && n !== !1 ? (this.result = this.result.trimEnd(), this.result += ` ${i.slice(1)}="${b(n)}"`) : this.result = this.result.trimEnd() : (this.result += t, this.result += b(n), o = !1), a && o) {
197
- let t = e[this.stringIdx + 1];
198
- t && t.startsWith(a) && (this.charIdxForNext = 1);
199
- }
200
- } else this.result += b(n);
201
- }
202
- this.stringIdx++;
203
- }
204
- return this.result;
205
- }
206
- }, C = (e) => new S(e).scan(), w = class t {
207
- startNode;
208
- endNode;
209
- componentInstance = null;
210
- renderListener = null;
211
- _childParts = [];
212
- _partKeys = [];
213
- _cachedTemplateStrings = null;
214
- _cachedParts = null;
215
- constructor(e, t) {
216
- this.startNode = e, this.endNode = t;
217
- }
218
- update(t) {
219
- this.componentInstance && (!e(t) || t.clazz !== this.componentInstance.constructor) && this.disposeComponent(), !(t instanceof o) && !Array.isArray(t) && this._childParts.length > 0 && this.clearChildParts(), e(t) ? this.updateComponent(t) : t instanceof o ? this.updateRepeat(t) : Array.isArray(t) ? this.updateArray(t) : this.updateNode(t);
220
- }
221
- disposeComponent() {
222
- this.componentInstance && (this.renderListener && this.componentInstance.removeRenderListener(this.renderListener), this.componentInstance.disconnectedCallback(), this.componentInstance = null, this.renderListener = null, this._clearTemplateCache());
223
- }
224
- updateComponent(e) {
225
- this.componentInstance || (this.componentInstance = new e.clazz(), this.componentInstance.__parent = i.currentRenderingInstance, this.componentInstance.__parent && (this.componentInstance._id = `${this.componentInstance.__parent._id}:${this.componentInstance.__parent._childCounter++}`), this.renderListener = (e) => {
226
- this.updateNode(e);
227
- }, this.componentInstance.addRenderListener(this.renderListener), this.componentInstance.connectedCallback());
228
- let t = this.componentInstance;
229
- Object.assign(t, e.props), "props" in t && (t.props = e.props), t.children = e.children, t.requestUpdate();
230
- }
231
- updateRepeat(e) {
232
- let { items: t, keyFn: n, templateFn: r } = e, i = /* @__PURE__ */ new Map();
233
- this._childParts.forEach((e, t) => {
234
- let n = this._partKeys[t];
235
- n !== void 0 && i.set(n, e);
236
- });
237
- let a = [], o = [], s = this.startNode;
238
- t.forEach((e, t) => {
239
- let c = n(e, t);
240
- o.push(c);
241
- let l = i.get(c);
242
- l ? (i.delete(c), l.startNode.previousSibling !== s && this.movePart(l, s), l.update(r(e, t))) : (l = this.createChildPart(s), l.update(r(e, t))), a.push(l), s = l.endNode;
243
- }), i.forEach((e) => e.dispose()), this._childParts = a, this._partKeys = o;
244
- }
245
- updateArray(e) {
246
- this._partKeys = [];
247
- let t = Math.min(e.length, this._childParts.length);
248
- for (let n = 0; n < t; n++) this._childParts[n].update(e[n]);
249
- if (e.length > this._childParts.length) {
250
- let n = this._childParts.length > 0 ? this._childParts[this._childParts.length - 1].endNode : this.startNode;
251
- for (let r = t; r < e.length; r++) {
252
- let t = this.createChildPart(n);
253
- this._childParts.push(t), t.update(e[r]), n = t.endNode;
254
- }
255
- }
256
- if (e.length < this._childParts.length) for (let t = this._childParts.length - 1; t >= e.length; t--) this._childParts[t].dispose(), this._childParts.pop();
257
- }
258
- createChildPart(e) {
259
- let n = document.createComment("CRP-Item"), r = document.createComment("/CRP-Item"), i = e.nextSibling;
260
- return e.parentNode.insertBefore(n, i), e.parentNode.insertBefore(r, i), new t(n, r);
261
- }
262
- movePart(e, t) {
263
- let n = t.parentNode, r = [], i = e.startNode;
264
- for (; i && i !== e.endNode;) r.push(i), i = i.nextSibling;
265
- r.push(e.endNode);
266
- let a = t.nextSibling;
267
- r.forEach((e) => n.insertBefore(e, a));
268
- }
269
- clearChildParts() {
270
- this._childParts.forEach((e) => e.dispose()), this._childParts = [], this._partKeys = [];
271
- }
272
- dispose() {
273
- this.clear(), this._clearTemplateCache(), this.clearChildParts(), this.startNode.parentNode && this.startNode.parentNode.removeChild(this.startNode), this.endNode.parentNode && this.endNode.parentNode.removeChild(this.endNode), this.componentInstance && (this.renderListener && this.componentInstance.removeRenderListener(this.renderListener), this.componentInstance.disconnectedCallback());
274
- }
275
- updateNode(t) {
276
- if (this.startNode.parentNode) if (h(t)) if (this._cachedTemplateStrings === t.strings && this._cachedParts) this._cachedParts.forEach((e, n) => {
277
- e.update(t.values[n]);
278
- });
279
- else {
280
- this._clearTemplateCache(), this.clear();
281
- let e = document.createDocumentFragment();
282
- O(t, e), this.startNode.parentNode.insertBefore(e, this.endNode);
283
- let n = D.get(e.firstChild?.childNodes[0] || e);
284
- n && (this._cachedTemplateStrings = n.strings, this._cachedParts = n.parts);
285
- }
286
- else if (v(t)) {
287
- this._clearTemplateCache(), this.clear();
288
- let e = document.createElement("template");
289
- e.innerHTML = t.value, this.startNode.parentNode.insertBefore(e.content, this.endNode);
290
- } else if (e(t) || typeof t == "object" && t && !(t instanceof Node) && !Array.isArray(t)) {
291
- this._clearTemplateCache(), this.clear();
292
- let e = document.createDocumentFragment();
293
- O(f`${t}`, e), this.startNode.parentNode.insertBefore(e, this.endNode);
294
- } else if (t instanceof Node) this._clearTemplateCache(), this.clear(), this.startNode.parentNode.insertBefore(t, this.endNode);
295
- else if (t == null || t === !1) this._clearTemplateCache(), this.clear();
296
- else {
297
- let e = String(t), n = this.startNode.nextSibling;
298
- if (n && n.nodeType === Node.TEXT_NODE && n.nextSibling === this.endNode) n.nodeValue !== e && (n.nodeValue = e);
299
- else {
300
- this._clearTemplateCache(), this.clear();
301
- let t = document.createTextNode(e);
302
- this.startNode.parentNode.insertBefore(t, this.endNode);
303
- }
304
- }
305
- }
306
- _clearTemplateCache() {
307
- this._cachedTemplateStrings = null, this._cachedParts = null;
308
- }
309
- clear() {
310
- let e = this.startNode.nextSibling;
311
- for (; e && e !== this.endNode;) {
312
- let t = e.nextSibling;
313
- e.parentNode.removeChild(e), e = t;
314
- }
315
- }
316
- }, T = class {
317
- element;
318
- previousValues = {};
319
- constructor(e) {
320
- this.element = e;
321
- }
322
- update(e) {
323
- if (typeof e != "object" || !e) return;
324
- let t = e;
325
- for (let e of Object.keys(this.previousValues)) if (!(e in t)) if (e.startsWith("@")) {
326
- let t = e.slice(1), n = `__crp_handler_${t}`, r = this.element[n];
327
- r && this.element.removeEventListener(t, r);
328
- } else e.startsWith(".") || (e.startsWith("?") ? this.element.removeAttribute(e.slice(1)) : this.element.removeAttribute(e));
329
- for (let [e, n] of Object.entries(t)) if (e.startsWith("@") && typeof n == "function") {
330
- let t = e.slice(1), r = `__crp_handler_${t}`, i = this.element[r];
331
- i && this.element.removeEventListener(t, i), this.element[r] = n, this.element.addEventListener(t, n);
332
- } else e.startsWith(".") ? this.element[e.slice(1)] = n : e.startsWith("?") ? n ? this.element.setAttribute(e.slice(1), "") : this.element.removeAttribute(e.slice(1)) : typeof n == "boolean" ? n ? this.element.setAttribute(e, "") : this.element.removeAttribute(e) : n != null && this.element.setAttribute(e, String(n));
333
- this.previousValues = { ...t };
334
- }
335
- }, E = class {
336
- element;
337
- name;
338
- segments;
339
- isMulti;
340
- multiState;
341
- constructor(e, t, n, r) {
342
- this.element = e, this.name = t;
343
- let i = [...n.matchAll(/__CRP_(\d+)__/g)];
344
- this.segments = [];
345
- let a = 0;
346
- for (let e of i) {
347
- let t = e.index;
348
- this.segments.push(n.substring(a, t)), a = t + e[0].length;
349
- }
350
- this.segments.push(n.substring(a)), this.isMulti = i.length > 1, this.multiState = r ?? null;
351
- }
352
- update(e) {
353
- if (e instanceof a && (e = e.value), this.name === "ref") {
354
- typeof e == "function" ? e(this.element) : e && typeof e == "object" && "value" in e && (e.value = this.element), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
355
- return;
356
- }
357
- if (this.isMulti && this.multiState) {
358
- let { currentValues: t, position: n } = this.multiState;
359
- t[n] = e;
360
- let r = this.segments[0];
361
- for (let e = 0; e < t.length; e++) r += String(t[e]) + this.segments[e + 1];
362
- this.element.setAttribute(this.name, r);
363
- return;
364
- }
365
- if (this.name.startsWith("@") && typeof e == "function") {
366
- let t = this.name.slice(1), n = `__crp_handler_${t}`, r = this.element[n];
367
- r && this.element.removeEventListener(t, r), this.element[n] = e, this.element.addEventListener(t, e), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
368
- } else if (this.name.startsWith(".")) {
369
- let t = this.name.slice(1);
370
- this.element[t] = e, this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
371
- } else if (this.name.startsWith("?")) {
372
- let t = this.name.slice(1);
373
- e ? this.element.setAttribute(t, "") : this.element.removeAttribute(t), this.element.hasAttribute(this.name) && this.element.removeAttribute(this.name);
374
- } else if (this.name === "value" && (this.element instanceof HTMLInputElement || this.element instanceof HTMLTextAreaElement)) this.element.value !== String(e) && (this.element.value = String(e)), this.element.setAttribute(this.name, String(e));
375
- else if (this.name === "checked" && this.element instanceof HTMLInputElement) {
376
- let t = !!e;
377
- this.element.checked !== t && (this.element.checked = t), t ? this.element.setAttribute(this.name, "") : this.element.removeAttribute(this.name);
378
- } else typeof e == "boolean" ? e ? this.element.setAttribute(this.name, "") : this.element.removeAttribute(this.name) : this.element.setAttribute(this.name, this.segments[0] + String(e) + this.segments[1]);
379
- }
380
- }, D = /* @__PURE__ */ new WeakMap(), O = (e, t) => {
381
- let n = D.get(t);
382
- if (n && n.strings === e.strings) {
383
- n.parts.forEach((t, n) => {
384
- t.update(e.values[n]);
385
- });
386
- return;
387
- }
388
- let r = [], i = e.values, a = "", o = !1, s = null, c = /(\.\.\.|[.@?]?[a-zA-Z0-9_-]+)=["']?$/;
389
- for (let t = 0; t < e.strings.length - 1; t++) {
390
- let n = e.strings[t];
391
- for (let e = 0; e < n.length; e++) s ? n[e] === s && (s = null) : n[e] === "\"" || n[e] === "'" ? e > 0 && n[e - 1] === "=" && (s = n[e]) : n[e] === "<" && n[e + 1] !== "!" && n[e + 1] !== "/" ? o = !0 : n[e] === ">" && (o = !1);
392
- a += n;
393
- let r = n.match(c);
394
- if (o && (r || s)) {
395
- if (a += `__CRP_${t}__`, r && !s) {
396
- let e = r[0].endsWith("\"") ? "\"" : r[0].endsWith("'") ? "'" : null;
397
- e && (s = e);
398
- }
399
- } else a += `<!--CRP_${t}-->`;
400
- }
401
- let l = e.strings[e.strings.length - 1];
402
- for (let e = 0; e < l.length; e++) s ? l[e] === s && (s = null) : l[e] === "<" && l[e + 1] !== "!" && l[e + 1] !== "/" ? o = !0 : l[e] === ">" && (o = !1);
403
- a += l;
404
- let u = document.createElement("template");
405
- u.innerHTML = a;
406
- let d = u.content.cloneNode(!0), f = document.createTreeWalker(d, NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_COMMENT), p, m = [];
407
- for (; p = f.nextNode();) m.push(p);
408
- m.forEach((e) => {
409
- if (e.nodeType === Node.COMMENT_NODE) {
410
- let t = e.nodeValue?.match(/CRP_(\d+)/);
411
- if (t) {
412
- let n = parseInt(t[1]), i = document.createComment("/CRP");
413
- e.parentNode.insertBefore(i, e.nextSibling), r[n] = new w(e, i);
414
- }
415
- } else if (e.nodeType === Node.ELEMENT_NODE) {
416
- let t = e;
417
- Array.from(t.attributes).forEach((e) => {
418
- let n = [...e.value.matchAll(/__CRP_(\d+)__/g)];
419
- if (n.length === 0) return;
420
- if (n.length === 1) {
421
- let i = parseInt(n[0][1]);
422
- r[i] || (e.name === "..." ? (r[i] = new T(t), t.removeAttribute("...")) : r[i] = new E(t, e.name, e.value));
423
- return;
424
- }
425
- if (e.name === "...") return;
426
- let i = n.map((e) => parseInt(e[1])), a = Array(n.length).fill(void 0);
427
- for (let n = 0; n < i.length; n++) {
428
- let o = i[n];
429
- r[o] || (r[o] = new E(t, e.name, e.value, {
430
- currentValues: a,
431
- position: n
432
- }));
433
- }
434
- });
435
- }
436
- });
437
- for (let e = 0; e < i.length; e++) r[e] && r[e].update(i[e]);
438
- if (t instanceof HTMLElement) t.innerHTML = "", t.appendChild(d);
439
- else if (t instanceof DocumentFragment) {
440
- for (; t.firstChild;) t.removeChild(t.firstChild);
441
- t.appendChild(d);
442
- }
443
- D.set(t, {
444
- strings: e.strings,
445
- parts: r
446
- });
447
- };
448
- //#endregion
449
- export { e as S, u as _, f as a, r as b, O as c, _ as d, a as f, c as g, s as h, y as i, C as l, l as m, g as n, h as o, o as p, m as r, v as s, d as t, p as u, i as v, n as x, t as y };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};