@dooboostore/simple-web-component 1.0.16 → 1.0.18
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.
- package/README.md +109 -48
- package/dist/cjs/decorators/addEventListener.js.map +2 -2
- package/dist/cjs/decorators/appendChild.js +48 -0
- package/dist/cjs/decorators/appendChild.js.map +7 -0
- package/dist/cjs/decorators/elementDefine.js +216 -67
- package/dist/cjs/decorators/elementDefine.js.map +3 -3
- package/dist/cjs/decorators/index.js +2 -1
- package/dist/cjs/decorators/index.js.map +2 -2
- package/dist/cjs/decorators/{innerHtml.js → onConnectedInnerHtml.js} +8 -8
- package/dist/cjs/decorators/onConnectedInnerHtml.js.map +7 -0
- package/dist/cjs/decorators/query.js +38 -0
- package/dist/cjs/decorators/query.js.map +2 -2
- package/dist/cjs/decorators/queryAll.js +32 -0
- package/dist/cjs/decorators/queryAll.js.map +2 -2
- package/dist/cjs/decorators/replaceChildren.js +53 -0
- package/dist/cjs/decorators/replaceChildren.js.map +7 -0
- package/dist/cjs/elements/SwcAppEngine.js +2 -1
- package/dist/cjs/elements/SwcAppEngine.js.map +2 -2
- package/dist/cjs/elements/SwcAsync.js +188 -0
- package/dist/cjs/elements/SwcAsync.js.map +7 -0
- package/dist/cjs/elements/SwcChoose.js +155 -0
- package/dist/cjs/elements/SwcChoose.js.map +7 -0
- package/dist/cjs/elements/SwcIf.js +167 -0
- package/dist/cjs/elements/SwcIf.js.map +7 -0
- package/dist/cjs/elements/SwcLoop.js +246 -0
- package/dist/cjs/elements/SwcLoop.js.map +7 -0
- package/dist/cjs/elements/SwcSubTemplates.js +65 -0
- package/dist/cjs/elements/SwcSubTemplates.js.map +7 -0
- package/dist/cjs/elements/index.js +7 -0
- package/dist/cjs/elements/index.js.map +2 -2
- package/dist/cjs/index.js +12 -2
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utils/Utils.js +74 -37
- package/dist/cjs/utils/Utils.js.map +3 -3
- package/dist/esm/decorators/addEventListener.js.map +2 -2
- package/dist/esm/decorators/appendChild.js +29 -0
- package/dist/esm/decorators/appendChild.js.map +7 -0
- package/dist/esm/decorators/elementDefine.js +216 -67
- package/dist/esm/decorators/elementDefine.js.map +3 -3
- package/dist/esm/decorators/index.js +2 -1
- package/dist/esm/decorators/index.js.map +2 -2
- package/dist/esm/decorators/{innerHtml.js → onConnectedInnerHtml.js} +5 -5
- package/dist/esm/decorators/onConnectedInnerHtml.js.map +7 -0
- package/dist/esm/decorators/query.js +38 -0
- package/dist/esm/decorators/query.js.map +2 -2
- package/dist/esm/decorators/queryAll.js +32 -0
- package/dist/esm/decorators/queryAll.js.map +2 -2
- package/dist/esm/decorators/replaceChildren.js +34 -0
- package/dist/esm/decorators/replaceChildren.js.map +7 -0
- package/dist/esm/elements/SwcAppEngine.js +2 -1
- package/dist/esm/elements/SwcAppEngine.js.map +2 -2
- package/dist/esm/elements/SwcAsync.js +169 -0
- package/dist/esm/elements/SwcAsync.js.map +7 -0
- package/dist/esm/elements/SwcChoose.js +136 -0
- package/dist/esm/elements/SwcChoose.js.map +7 -0
- package/dist/esm/elements/SwcIf.js +148 -0
- package/dist/esm/elements/SwcIf.js.map +7 -0
- package/dist/esm/elements/SwcLoop.js +227 -0
- package/dist/esm/elements/SwcLoop.js.map +7 -0
- package/dist/esm/elements/SwcSubTemplates.js +46 -0
- package/dist/esm/elements/SwcSubTemplates.js.map +7 -0
- package/dist/esm/elements/index.js +7 -0
- package/dist/esm/elements/index.js.map +2 -2
- package/dist/esm/index.js +8 -2
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utils/Utils.js +74 -37
- package/dist/esm/utils/Utils.js.map +3 -3
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js +1226 -149
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js.map +4 -4
- package/dist/types/decorators/addEventListener.d.ts +1 -0
- package/dist/types/decorators/addEventListener.d.ts.map +1 -1
- package/dist/types/decorators/appendChild.d.ts +17 -0
- package/dist/types/decorators/appendChild.d.ts.map +1 -0
- package/dist/types/decorators/elementDefine.d.ts.map +1 -1
- package/dist/types/decorators/index.d.ts +2 -1
- package/dist/types/decorators/index.d.ts.map +1 -1
- package/dist/types/decorators/onConnectedInnerHtml.d.ts +15 -0
- package/dist/types/decorators/onConnectedInnerHtml.d.ts.map +1 -0
- package/dist/types/decorators/query.d.ts +0 -2
- package/dist/types/decorators/query.d.ts.map +1 -1
- package/dist/types/decorators/queryAll.d.ts +0 -2
- package/dist/types/decorators/queryAll.d.ts.map +1 -1
- package/dist/types/decorators/replaceChildren.d.ts +9 -0
- package/dist/types/decorators/replaceChildren.d.ts.map +1 -0
- package/dist/types/elements/SwcAppEngine.d.ts.map +1 -1
- package/dist/types/elements/SwcAsync.d.ts +16 -0
- package/dist/types/elements/SwcAsync.d.ts.map +1 -0
- package/dist/types/elements/SwcChoose.d.ts +13 -0
- package/dist/types/elements/SwcChoose.d.ts.map +1 -0
- package/dist/types/elements/SwcIf.d.ts +15 -0
- package/dist/types/elements/SwcIf.d.ts.map +1 -0
- package/dist/types/elements/SwcLoop.d.ts +20 -0
- package/dist/types/elements/SwcLoop.d.ts.map +1 -0
- package/dist/types/elements/SwcSubTemplates.d.ts +13 -0
- package/dist/types/elements/SwcSubTemplates.d.ts.map +1 -0
- package/dist/types/elements/index.d.ts +7 -0
- package/dist/types/elements/index.d.ts.map +1 -1
- package/dist/types/index.d.ts +5 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +8 -0
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/Utils.d.ts +16 -10
- package/dist/types/utils/Utils.d.ts.map +1 -1
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js +1226 -149
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js.map +4 -4
- package/package.json +10 -3
- package/src/decorators/addEventListener.ts +1 -0
- package/src/decorators/appendChild.ts +42 -0
- package/src/decorators/elementDefine.ts +225 -71
- package/src/decorators/index.ts +2 -1
- package/src/decorators/{innerHtml.ts → onConnectedInnerHtml.ts} +7 -6
- package/src/decorators/query.ts +32 -2
- package/src/decorators/queryAll.ts +32 -2
- package/src/decorators/replaceChildren.ts +35 -0
- package/src/elements/SwcAppEngine.ts +2 -1
- package/src/elements/SwcAsync.ts +163 -0
- package/src/elements/SwcChoose.ts +127 -0
- package/src/elements/SwcIf.ts +134 -0
- package/src/elements/SwcLoop.ts +215 -0
- package/src/elements/SwcSubTemplates.ts +19 -0
- package/src/elements/index.ts +8 -3
- package/src/index.ts +5 -2
- package/src/types.ts +9 -1
- package/src/utils/Utils.ts +86 -36
- package/dist/cjs/decorators/innerHtml.js.map +0 -7
- package/dist/esm/decorators/innerHtml.js.map +0 -7
- package/dist/types/decorators/innerHtml.d.ts +0 -12
- package/dist/types/decorators/innerHtml.d.ts.map +0 -1
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
var __decorate = function(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = function(k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { elementDefine } from "../decorators/elementDefine.js";
|
|
11
|
+
import { changedAttribute } from "../decorators/changedAttribute.js";
|
|
12
|
+
import { SwcUtils } from "../utils/Utils.js";
|
|
13
|
+
import { FunctionUtils } from "@dooboostore/core/function/FunctionUtils";
|
|
14
|
+
let SwcAsync = class SwcAsync2 extends HTMLTemplateElement {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
this._nodes = [];
|
|
18
|
+
this._currentPromise = null;
|
|
19
|
+
this._value = null;
|
|
20
|
+
}
|
|
21
|
+
get value() {
|
|
22
|
+
return this._value;
|
|
23
|
+
}
|
|
24
|
+
set value(nv) {
|
|
25
|
+
if (nv instanceof Promise) {
|
|
26
|
+
this.handlePromise(nv);
|
|
27
|
+
} else {
|
|
28
|
+
this._value = nv;
|
|
29
|
+
this.render(nv === null ? "default" : "success", nv);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
refresh(newValue) {
|
|
33
|
+
const script = newValue !== void 0 ? newValue : this.getAttribute("on-get-value");
|
|
34
|
+
if (!script) {
|
|
35
|
+
this.value = null;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
if (!this.parentElement)
|
|
39
|
+
return;
|
|
40
|
+
const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);
|
|
41
|
+
try {
|
|
42
|
+
const result = FunctionUtils.executeReturn({
|
|
43
|
+
script,
|
|
44
|
+
context: this,
|
|
45
|
+
args: helperHostSet
|
|
46
|
+
});
|
|
47
|
+
if (result instanceof Promise) {
|
|
48
|
+
this.handlePromise(result);
|
|
49
|
+
} else {
|
|
50
|
+
this.value = result;
|
|
51
|
+
}
|
|
52
|
+
} catch (e) {
|
|
53
|
+
this.render("error", e);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async handlePromise(promise) {
|
|
57
|
+
this._currentPromise = promise;
|
|
58
|
+
this.render("loading");
|
|
59
|
+
try {
|
|
60
|
+
const result = await promise;
|
|
61
|
+
if (this._currentPromise !== promise)
|
|
62
|
+
return;
|
|
63
|
+
this._value = result;
|
|
64
|
+
this.render("success", result);
|
|
65
|
+
} catch (e) {
|
|
66
|
+
if (this._currentPromise !== promise)
|
|
67
|
+
return;
|
|
68
|
+
this.render("error", e);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
render(state, data) {
|
|
72
|
+
this.cleanup();
|
|
73
|
+
if (!this.parentElement)
|
|
74
|
+
return;
|
|
75
|
+
let fragment;
|
|
76
|
+
const ctx = {
|
|
77
|
+
$nodes: [],
|
|
78
|
+
$elements: [],
|
|
79
|
+
$state: state,
|
|
80
|
+
$value: null,
|
|
81
|
+
$error: null
|
|
82
|
+
};
|
|
83
|
+
if (state === "default") {
|
|
84
|
+
const t = this.content.querySelector('template[is="swc-default"]');
|
|
85
|
+
fragment = t ? t.content.cloneNode(true) : document.createDocumentFragment();
|
|
86
|
+
} else if (state === "loading") {
|
|
87
|
+
const t = this.content.querySelector('template[is="swc-loading"]');
|
|
88
|
+
fragment = t ? t.content.cloneNode(true) : document.createDocumentFragment();
|
|
89
|
+
} else if (state === "error") {
|
|
90
|
+
const t = this.content.querySelector('template[is="swc-error"]');
|
|
91
|
+
ctx.$error = data;
|
|
92
|
+
fragment = t ? t.content.cloneNode(true) : document.createDocumentFragment();
|
|
93
|
+
} else {
|
|
94
|
+
const t = this.content.querySelector('template[is="swc-success"]');
|
|
95
|
+
ctx.$value = data;
|
|
96
|
+
if (t) {
|
|
97
|
+
fragment = t.content.cloneNode(true);
|
|
98
|
+
} else {
|
|
99
|
+
const clone = this.content.cloneNode(true);
|
|
100
|
+
Array.from(clone.querySelectorAll('template[is="swc-loading"], template[is="swc-error"], template[is="swc-success"], template[is="swc-default"]')).forEach((tmp) => tmp.remove());
|
|
101
|
+
fragment = clone;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
this.mount(fragment, ctx);
|
|
105
|
+
}
|
|
106
|
+
mount(fragment, ctx) {
|
|
107
|
+
const nodes = Array.from(fragment.childNodes);
|
|
108
|
+
const elements = nodes.filter((n) => n.nodeType === 1);
|
|
109
|
+
Object.assign(ctx, {
|
|
110
|
+
$nodes: nodes,
|
|
111
|
+
$elements: elements,
|
|
112
|
+
$firstNode: nodes[0],
|
|
113
|
+
$lastNode: nodes[nodes.length - 1],
|
|
114
|
+
$firstElement: elements[0],
|
|
115
|
+
$lastElement: elements[elements.length - 1]
|
|
116
|
+
});
|
|
117
|
+
this._executeCallback("on-clone-nodes", ctx);
|
|
118
|
+
nodes.forEach((node, nodeIndex) => {
|
|
119
|
+
if (node instanceof HTMLElement) {
|
|
120
|
+
node.__swc_host = this;
|
|
121
|
+
}
|
|
122
|
+
this._executeCallback("on-clone-node", {
|
|
123
|
+
...ctx,
|
|
124
|
+
$node: node,
|
|
125
|
+
$nodeIndex: nodeIndex,
|
|
126
|
+
$isElement: node.nodeType === 1
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
this._nodes = nodes;
|
|
130
|
+
this.parentElement?.insertBefore(fragment, this.nextSibling);
|
|
131
|
+
}
|
|
132
|
+
_executeCallback(attrName, extraArgs) {
|
|
133
|
+
const script = this.getAttribute(attrName);
|
|
134
|
+
if (!script)
|
|
135
|
+
return;
|
|
136
|
+
const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);
|
|
137
|
+
try {
|
|
138
|
+
FunctionUtils.execute({ script, context: this, args: { ...helperHostSet, ...extraArgs } });
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error(`[SwcAsync] Error in ${attrName}:`, e);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
cleanup() {
|
|
144
|
+
this._nodes.forEach((n) => {
|
|
145
|
+
if (n.parentElement)
|
|
146
|
+
n.parentElement.removeChild(n);
|
|
147
|
+
});
|
|
148
|
+
this._nodes = [];
|
|
149
|
+
}
|
|
150
|
+
connectedCallback() {
|
|
151
|
+
this.refresh();
|
|
152
|
+
}
|
|
153
|
+
disconnectedCallback() {
|
|
154
|
+
this.cleanup();
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
__decorate([
|
|
158
|
+
changedAttribute("on-get-value"),
|
|
159
|
+
__metadata("design:type", Function),
|
|
160
|
+
__metadata("design:paramtypes", [String]),
|
|
161
|
+
__metadata("design:returntype", void 0)
|
|
162
|
+
], SwcAsync.prototype, "refresh", null);
|
|
163
|
+
SwcAsync = __decorate([
|
|
164
|
+
elementDefine({ name: "swc-async", extends: "template" })
|
|
165
|
+
], SwcAsync);
|
|
166
|
+
export {
|
|
167
|
+
SwcAsync
|
|
168
|
+
};
|
|
169
|
+
//# sourceMappingURL=SwcAsync.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/elements/SwcAsync.ts"],
|
|
4
|
+
"sourcesContent": ["import { elementDefine } from '../decorators/elementDefine';\nimport { changedAttribute } from '../decorators/changedAttribute';\nimport { SwcUtils } from '../utils/Utils';\nimport { FunctionUtils } from '@dooboostore/core/function/FunctionUtils';\nimport { SwcLoading, SwcError, SwcSuccess, SwcDefault } from './SwcSubTemplates';\n\n@elementDefine({ name: 'swc-async', extends: 'template' })\nexport class SwcAsync extends HTMLTemplateElement {\n private _nodes: Node[] = [];\n private _currentPromise: Promise<any> | null = null;\n private _value: any = null;\n\n get value(): any {\n return this._value;\n }\n\n set value(nv: any) {\n if (nv instanceof Promise) {\n this.handlePromise(nv);\n } else {\n this._value = nv;\n this.render(nv === null ? 'default' : 'success', nv);\n }\n }\n\n @changedAttribute('on-get-value')\n private refresh(newValue?: string | null) {\n const script = newValue !== undefined ? newValue : this.getAttribute('on-get-value');\n if (!script) {\n this.value = null; // Triggers default render\n return;\n }\n if (!this.parentElement) return;\n\n const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);\n try {\n const result = FunctionUtils.executeReturn({\n script,\n context: this,\n args: helperHostSet\n });\n\n if (result instanceof Promise) {\n this.handlePromise(result);\n } else {\n this.value = result;\n }\n } catch (e) {\n this.render('error', e);\n }\n }\n\n private async handlePromise(promise: Promise<any>) {\n this._currentPromise = promise;\n this.render('loading');\n\n try {\n const result = await promise;\n if (this._currentPromise !== promise) return;\n this._value = result;\n this.render('success', result);\n } catch (e) {\n if (this._currentPromise !== promise) return;\n this.render('error', e);\n }\n }\n\n private render(state: 'loading' | 'error' | 'success' | 'default', data?: any) {\n this.cleanup();\n if (!this.parentElement) return;\n\n let fragment: DocumentFragment;\n const ctx: any = {\n $nodes: [],\n $elements: [],\n $state: state,\n $value: null,\n $error: null\n };\n\n if (state === 'default') {\n const t = this.content.querySelector('template[is=\"swc-default\"]') as HTMLTemplateElement;\n fragment = t ? (t.content.cloneNode(true) as DocumentFragment) : document.createDocumentFragment();\n } else if (state === 'loading') {\n const t = this.content.querySelector('template[is=\"swc-loading\"]') as HTMLTemplateElement;\n fragment = t ? (t.content.cloneNode(true) as DocumentFragment) : document.createDocumentFragment();\n } else if (state === 'error') {\n const t = this.content.querySelector('template[is=\"swc-error\"]') as HTMLTemplateElement;\n ctx.$error = data;\n fragment = t ? (t.content.cloneNode(true) as DocumentFragment) : document.createDocumentFragment();\n } else {\n // Success\n const t = this.content.querySelector('template[is=\"swc-success\"]') as HTMLTemplateElement;\n ctx.$value = data;\n if (t) {\n fragment = t.content.cloneNode(true) as DocumentFragment;\n } else {\n const clone = this.content.cloneNode(true) as DocumentFragment;\n Array.from(clone.querySelectorAll('template[is=\"swc-loading\"], template[is=\"swc-error\"], template[is=\"swc-success\"], template[is=\"swc-default\"]')).forEach(tmp => tmp.remove());\n fragment = clone;\n }\n }\n\n this.mount(fragment, ctx);\n }\n\n private mount(fragment: DocumentFragment, ctx: any) {\n const nodes = Array.from(fragment.childNodes);\n const elements = nodes.filter(n => n.nodeType === 1) as HTMLElement[];\n\n Object.assign(ctx, {\n $nodes: nodes,\n $elements: elements,\n $firstNode: nodes[0],\n $lastNode: nodes[nodes.length - 1],\n $firstElement: elements[0],\n $lastElement: elements[elements.length - 1]\n });\n\n // Execution Callbacks\n this._executeCallback('on-clone-nodes', ctx);\n nodes.forEach((node, nodeIndex) => {\n if (node instanceof HTMLElement) {\n (node as any).__swc_host = this;\n }\n this._executeCallback('on-clone-node', {\n ...ctx,\n $node: node,\n $nodeIndex: nodeIndex,\n $isElement: node.nodeType === 1\n });\n });\n\n this._nodes = nodes;\n this.parentElement?.insertBefore(fragment, this.nextSibling);\n }\n\n private _executeCallback(attrName: string, extraArgs: Record<string, any>) {\n const script = this.getAttribute(attrName);\n if (!script) return;\n const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);\n try {\n FunctionUtils.execute({ script, context: this, args: { ...helperHostSet, ...extraArgs } });\n } catch (e) {\n console.error(`[SwcAsync] Error in ${attrName}:`, e);\n }\n }\n\n private cleanup() {\n this._nodes.forEach(n => {\n if (n.parentElement) n.parentElement.removeChild(n);\n });\n this._nodes = [];\n }\n\n connectedCallback() {\n this.refresh();\n }\n\n disconnectedCallback() {\n this.cleanup();\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;AAAA,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AACjC,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAIvB,IAAM,WAAN,MAAMA,kBAAiB,oBAAmB;EAA1C,cAAA;;AACG,SAAA,SAAiB,CAAA;AACjB,SAAA,kBAAuC;AACvC,SAAA,SAAc;EAwJxB;EAtJE,IAAI,QAAK;AACP,WAAO,KAAK;EACd;EAEA,IAAI,MAAM,IAAO;AACf,QAAI,cAAc,SAAS;AACzB,WAAK,cAAc,EAAE;IACvB,OAAO;AACL,WAAK,SAAS;AACd,WAAK,OAAO,OAAO,OAAO,YAAY,WAAW,EAAE;IACrD;EACF;EAGQ,QAAQ,UAAwB;AACtC,UAAM,SAAS,aAAa,SAAY,WAAW,KAAK,aAAa,cAAc;AACnF,QAAI,CAAC,QAAQ;AACX,WAAK,QAAQ;AACb;IACF;AACA,QAAI,CAAC,KAAK;AAAe;AAEzB,UAAM,gBAAgB,SAAS,oBAAoB,QAAQ,IAAI;AAC/D,QAAI;AACF,YAAM,SAAS,cAAc,cAAc;QACzC;QACA,SAAS;QACT,MAAM;OACP;AAED,UAAI,kBAAkB,SAAS;AAC7B,aAAK,cAAc,MAAM;M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c;;;;;AAlBrB,WAAQ,WAAA;EADpB,cAAc,EAAE,MAAM,aAAa,SAAS,WAAU,CAAE;GAC5C,QAAQ;",
|
|
6
|
+
"names": ["SwcAsync"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
var __decorate = function(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = function(k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { elementDefine } from "../decorators/elementDefine.js";
|
|
11
|
+
import { changedAttribute } from "../decorators/changedAttribute.js";
|
|
12
|
+
import { SwcUtils } from "../utils/Utils.js";
|
|
13
|
+
import { FunctionUtils } from "@dooboostore/core/function/FunctionUtils";
|
|
14
|
+
let SwcChoose = class SwcChoose2 extends HTMLTemplateElement {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
this._nodes = [];
|
|
18
|
+
this._value = null;
|
|
19
|
+
}
|
|
20
|
+
get value() {
|
|
21
|
+
return this._value;
|
|
22
|
+
}
|
|
23
|
+
set value(nv) {
|
|
24
|
+
this._value = nv;
|
|
25
|
+
this.render();
|
|
26
|
+
}
|
|
27
|
+
_executeCallback(attrName, extraArgs) {
|
|
28
|
+
const script = this.getAttribute(attrName);
|
|
29
|
+
if (!script)
|
|
30
|
+
return;
|
|
31
|
+
const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);
|
|
32
|
+
try {
|
|
33
|
+
FunctionUtils.execute({ script, context: this, args: { ...helperHostSet, ...extraArgs } });
|
|
34
|
+
} catch (e) {
|
|
35
|
+
console.error(`[SwcChoose] Error in ${attrName}:`, e);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
refresh(newValue) {
|
|
39
|
+
const script = newValue !== void 0 ? newValue : this.getAttribute("on-get-value");
|
|
40
|
+
if (!script) {
|
|
41
|
+
this.value = null;
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (!this.parentElement)
|
|
45
|
+
return;
|
|
46
|
+
const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);
|
|
47
|
+
try {
|
|
48
|
+
const result = FunctionUtils.executeReturn({
|
|
49
|
+
script,
|
|
50
|
+
context: this,
|
|
51
|
+
args: helperHostSet
|
|
52
|
+
});
|
|
53
|
+
this.value = result;
|
|
54
|
+
} catch (e) {
|
|
55
|
+
console.error("[SwcChoose] Error in on-get-value:", e);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
render() {
|
|
59
|
+
this.cleanup();
|
|
60
|
+
if (!this.parentElement)
|
|
61
|
+
return;
|
|
62
|
+
const templates = Array.from(this.content.querySelectorAll("template"));
|
|
63
|
+
let selected = null;
|
|
64
|
+
let otherwise = null;
|
|
65
|
+
let defaultTmp = null;
|
|
66
|
+
for (const t of templates) {
|
|
67
|
+
const is = t.getAttribute("is");
|
|
68
|
+
if (is === "swc-when") {
|
|
69
|
+
const whenVal = t.getAttribute("value");
|
|
70
|
+
if (String(whenVal) === String(this._value)) {
|
|
71
|
+
selected = t;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
} else if (is === "swc-otherwise") {
|
|
75
|
+
otherwise = t;
|
|
76
|
+
} else if (is === "swc-default") {
|
|
77
|
+
defaultTmp = t;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const targetTemplate = (this._value == null ? defaultTmp : selected) || otherwise || defaultTmp;
|
|
81
|
+
if (targetTemplate) {
|
|
82
|
+
const clone = targetTemplate.content.cloneNode(true);
|
|
83
|
+
const nodes = Array.from(clone.childNodes);
|
|
84
|
+
const elements = nodes.filter((n) => n.nodeType === 1);
|
|
85
|
+
const ctx = {
|
|
86
|
+
$value: this._value,
|
|
87
|
+
$nodes: nodes,
|
|
88
|
+
$elements: elements,
|
|
89
|
+
$firstNode: nodes[0],
|
|
90
|
+
$lastNode: nodes[nodes.length - 1],
|
|
91
|
+
$firstElement: elements[0],
|
|
92
|
+
$lastElement: elements[elements.length - 1]
|
|
93
|
+
};
|
|
94
|
+
this._executeCallback("on-clone-nodes", ctx);
|
|
95
|
+
nodes.forEach((node, nodeIndex) => {
|
|
96
|
+
if (node instanceof HTMLElement) {
|
|
97
|
+
node.__swc_host = this;
|
|
98
|
+
}
|
|
99
|
+
this._executeCallback("on-clone-node", {
|
|
100
|
+
...ctx,
|
|
101
|
+
$node: node,
|
|
102
|
+
$nodeIndex: nodeIndex,
|
|
103
|
+
$isElement: node.nodeType === 1
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
this._nodes = nodes;
|
|
107
|
+
this.parentElement.insertBefore(clone, this.nextSibling);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
cleanup() {
|
|
111
|
+
this._nodes.forEach((n) => {
|
|
112
|
+
if (n.parentElement)
|
|
113
|
+
n.parentElement.removeChild(n);
|
|
114
|
+
});
|
|
115
|
+
this._nodes = [];
|
|
116
|
+
}
|
|
117
|
+
connectedCallback() {
|
|
118
|
+
this.refresh();
|
|
119
|
+
}
|
|
120
|
+
disconnectedCallback() {
|
|
121
|
+
this.cleanup();
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
__decorate([
|
|
125
|
+
changedAttribute("on-get-value"),
|
|
126
|
+
__metadata("design:type", Function),
|
|
127
|
+
__metadata("design:paramtypes", [String]),
|
|
128
|
+
__metadata("design:returntype", void 0)
|
|
129
|
+
], SwcChoose.prototype, "refresh", null);
|
|
130
|
+
SwcChoose = __decorate([
|
|
131
|
+
elementDefine({ name: "swc-choose", extends: "template" })
|
|
132
|
+
], SwcChoose);
|
|
133
|
+
export {
|
|
134
|
+
SwcChoose
|
|
135
|
+
};
|
|
136
|
+
//# sourceMappingURL=SwcChoose.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/elements/SwcChoose.ts"],
|
|
4
|
+
"sourcesContent": ["import { elementDefine } from '../decorators/elementDefine';\nimport { changedAttribute } from '../decorators/changedAttribute';\nimport { SwcUtils } from '../utils/Utils';\nimport { FunctionUtils } from '@dooboostore/core/function/FunctionUtils';\nimport { SwcWhen, SwcOtherwise } from './SwcSubTemplates';\n\n@elementDefine({ name: 'swc-choose', extends: 'template' })\nexport class SwcChoose extends HTMLTemplateElement {\n private _nodes: Node[] = [];\n private _value: any = null;\n\n get value(): any {\n return this._value;\n }\n\n set value(nv: any) {\n this._value = nv;\n this.render();\n }\n\n private _executeCallback(attrName: string, extraArgs: Record<string, any>) {\n const script = this.getAttribute(attrName);\n if (!script) return;\n const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);\n try {\n FunctionUtils.execute({ script, context: this, args: { ...helperHostSet, ...extraArgs } });\n } catch (e) {\n console.error(`[SwcChoose] Error in ${attrName}:`, e);\n }\n }\n\n @changedAttribute('on-get-value')\n private refresh(newValue?: string | null) {\n const script = newValue !== undefined ? newValue : this.getAttribute('on-get-value');\n if (!script) {\n this.value = null;\n return;\n }\n if (!this.parentElement) return;\n\n const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);\n try {\n const result = FunctionUtils.executeReturn({\n script,\n context: this,\n args: helperHostSet\n });\n this.value = result;\n } catch (e) {\n console.error('[SwcChoose] Error in on-get-value:', e);\n }\n }\n\n private render() {\n this.cleanup();\n if (!this.parentElement) return;\n\n const templates = Array.from(this.content.querySelectorAll('template'));\n let selected: HTMLTemplateElement | null = null;\n let otherwise: HTMLTemplateElement | null = null;\n let defaultTmp: HTMLTemplateElement | null = null;\n\n for (const t of templates) {\n const is = t.getAttribute('is');\n if (is === 'swc-when') {\n const whenVal = t.getAttribute('value');\n if (String(whenVal) === String(this._value)) {\n selected = t;\n break;\n }\n } else if (is === 'swc-otherwise') {\n otherwise = t;\n } else if (is === 'swc-default') {\n defaultTmp = t;\n }\n }\n\n const targetTemplate = (this._value == null ? defaultTmp : selected) || otherwise || defaultTmp;\n if (targetTemplate) {\n const clone = targetTemplate.content.cloneNode(true) as DocumentFragment;\n const nodes = Array.from(clone.childNodes);\n const elements = nodes.filter(n => n.nodeType === 1) as HTMLElement[];\n\n const ctx = {\n $value: this._value,\n $nodes: nodes,\n $elements: elements,\n $firstNode: nodes[0],\n $lastNode: nodes[nodes.length - 1],\n $firstElement: elements[0],\n $lastElement: elements[elements.length - 1]\n };\n\n // Execution Callbacks\n this._executeCallback('on-clone-nodes', ctx);\n nodes.forEach((node, nodeIndex) => {\n if (node instanceof HTMLElement) {\n (node as any).__swc_host = this;\n }\n this._executeCallback('on-clone-node', {\n ...ctx,\n $node: node,\n $nodeIndex: nodeIndex,\n $isElement: node.nodeType === 1\n });\n });\n\n this._nodes = nodes;\n this.parentElement.insertBefore(clone, this.nextSibling);\n }\n }\n\n private cleanup() {\n this._nodes.forEach(n => {\n if (n.parentElement) n.parentElement.removeChild(n);\n });\n this._nodes = [];\n }\n\n connectedCallback() {\n this.refresh();\n }\n\n disconnectedCallback() {\n this.cleanup();\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;AAAA,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AACjC,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAIvB,IAAM,YAAN,MAAMA,mBAAkB,oBAAmB;EAA3C,cAAA;;AACG,SAAA,SAAiB,CAAA;AACjB,SAAA,SAAc;EAqHxB;EAnHE,IAAI,QAAK;AACP,WAAO,KAAK;EACd;EAEA,IAAI,MAAM,IAAO;AACf,SAAK,SA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Aa;AACrF,QAAI,gBAAgB;AAClB,YAAM,QAAQ,eAAe,QAAQ,UAAU,IAAI;AACnD,YAAM,QAAQ,MAAM,KAAK,MAAM,UAAU;AACzC,YAAM,WAAW,MAAM,OAAO,OAAK,EAAE,aAAa,CAAC;AAEnD,YAAM,MAAM;QACV,QAAQ,KAAK;QACb,QAAQ;QACR,WAAW;QACX,YAAY,MAAM,CAAC;QACnB,WAAW,MAAM,MAAM,SAAS,CAAC;QACjC,eAAe,SAAS,CAAC;QACzB,cAAc,SAAS,SAAS,SAAS,CAAC;;AAI5C,WAAK,iBAAiB,kBAAkB,GAAG;AAC3C,YAAM,QAAQ,CAAC,MAAM,cAAa;AAChC,YAAI,gBAAgB,aAAa;AAC9B,eAAa,aAAa;QAC7B;AACA,aAAK,iBAAiB,iBAAiB;UACrC,GAAG;UACH,OAAO;UACP,YAAY;UACZ,YAAY,KAAK,aAAa;SAC/B;MACH,CAAC;AAED,WAAK,SAAS;AACd,WAAK,cAAc,aAAa,OAAO,KAAK,WAAW;IACzD;EACF;EAEQ,UAAO;AACb,SAAK,OAAO,QAAQ,OAAI;AACtB,UAAI,EAAE;AAAe,UAAE,cAAc,YAAY,CAAC;IACpD,CAAC;AACD,SAAK,SAAS,CAAA;EAChB;EAEA,oBAAiB;AACf,SAAK,QAAO;EACd;EAEA,uBAAoB;AAClB,SAAK,QAAO;EACd;;AA7FQ,WAAA;EADP,iBAAiB,cAAc;;;;;AAxBrB,YAAS,WAAA;EADrB,cAAc,EAAE,MAAM,cAAc,SAAS,WAAU,CAAE;GAC7C,SAAS;",
|
|
6
|
+
"names": ["SwcChoose"]
|
|
7
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
var __decorate = function(decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = function(k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { elementDefine } from "../decorators/elementDefine.js";
|
|
11
|
+
import { changedAttribute } from "../decorators/changedAttribute.js";
|
|
12
|
+
import { SwcUtils } from "../utils/Utils.js";
|
|
13
|
+
import { FunctionUtils } from "@dooboostore/core/function/FunctionUtils";
|
|
14
|
+
let SwcIf = class SwcIf2 extends HTMLTemplateElement {
|
|
15
|
+
constructor() {
|
|
16
|
+
super(...arguments);
|
|
17
|
+
this._nodes = [];
|
|
18
|
+
this._value = null;
|
|
19
|
+
}
|
|
20
|
+
get value() {
|
|
21
|
+
return this._value;
|
|
22
|
+
}
|
|
23
|
+
set value(nv) {
|
|
24
|
+
this._value = nv;
|
|
25
|
+
this.render();
|
|
26
|
+
}
|
|
27
|
+
_getTarget() {
|
|
28
|
+
const script = this.getAttribute("on-get-portal");
|
|
29
|
+
if (!script)
|
|
30
|
+
return null;
|
|
31
|
+
try {
|
|
32
|
+
const res = FunctionUtils.executeReturn({
|
|
33
|
+
script,
|
|
34
|
+
context: this,
|
|
35
|
+
args: SwcUtils.getHelperAndHostSet(window, this)
|
|
36
|
+
});
|
|
37
|
+
if (typeof res === "string")
|
|
38
|
+
return document.querySelector(res);
|
|
39
|
+
if (res instanceof Node)
|
|
40
|
+
return res;
|
|
41
|
+
} catch (e) {
|
|
42
|
+
console.error("[SwcIf] Portal target error:", e);
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
_executeCallback(attrName, extraArgs) {
|
|
47
|
+
const script = this.getAttribute(attrName);
|
|
48
|
+
if (!script)
|
|
49
|
+
return;
|
|
50
|
+
const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);
|
|
51
|
+
try {
|
|
52
|
+
FunctionUtils.execute({ script, context: this, args: { ...helperHostSet, ...extraArgs } });
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.error(`[SwcIf] Error in ${attrName}:`, e);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
refresh(newValue) {
|
|
58
|
+
const script = newValue !== void 0 ? newValue : this.getAttribute("on-get-value");
|
|
59
|
+
if (!script) {
|
|
60
|
+
this.value = null;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (!this.parentElement)
|
|
64
|
+
return;
|
|
65
|
+
const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);
|
|
66
|
+
try {
|
|
67
|
+
const result = FunctionUtils.executeReturn({
|
|
68
|
+
script,
|
|
69
|
+
context: this,
|
|
70
|
+
args: helperHostSet
|
|
71
|
+
});
|
|
72
|
+
this.value = result;
|
|
73
|
+
} catch (e) {
|
|
74
|
+
console.error("[SwcIf] Error in on-get-value:", e);
|
|
75
|
+
this.value = null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
render() {
|
|
79
|
+
this.cleanup();
|
|
80
|
+
if (!this.parentElement)
|
|
81
|
+
return;
|
|
82
|
+
let fragment = null;
|
|
83
|
+
const ctx = { $value: this._value, $nodes: [], $elements: [] };
|
|
84
|
+
if (this._value) {
|
|
85
|
+
const clone = this.content.cloneNode(true);
|
|
86
|
+
Array.from(clone.querySelectorAll('template[is="swc-default"]')).forEach((t) => t.remove());
|
|
87
|
+
fragment = clone;
|
|
88
|
+
} else {
|
|
89
|
+
const t = this.content.querySelector('template[is="swc-default"]');
|
|
90
|
+
if (t)
|
|
91
|
+
fragment = t.content.cloneNode(true);
|
|
92
|
+
}
|
|
93
|
+
if (fragment) {
|
|
94
|
+
this.mount(fragment, ctx);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
mount(fragment, ctx) {
|
|
98
|
+
const nodes = Array.from(fragment.childNodes);
|
|
99
|
+
const elements = nodes.filter((n) => n.nodeType === 1);
|
|
100
|
+
Object.assign(ctx, {
|
|
101
|
+
$nodes: nodes,
|
|
102
|
+
$elements: elements,
|
|
103
|
+
$firstNode: nodes[0],
|
|
104
|
+
$lastNode: nodes[nodes.length - 1],
|
|
105
|
+
$firstElement: elements[0],
|
|
106
|
+
$lastElement: elements[elements.length - 1]
|
|
107
|
+
});
|
|
108
|
+
this._executeCallback("on-clone-nodes", ctx);
|
|
109
|
+
nodes.forEach((node, nodeIndex) => {
|
|
110
|
+
if (node instanceof HTMLElement)
|
|
111
|
+
node.__swc_host = this;
|
|
112
|
+
this._executeCallback("on-clone-node", { ...ctx, $node: node, $nodeIndex: nodeIndex, $isElement: node.nodeType === 1 });
|
|
113
|
+
});
|
|
114
|
+
this._nodes = nodes;
|
|
115
|
+
const target = this._getTarget();
|
|
116
|
+
if (target) {
|
|
117
|
+
target.appendChild(fragment);
|
|
118
|
+
} else {
|
|
119
|
+
this.parentElement?.insertBefore(fragment, this.nextSibling);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
cleanup() {
|
|
123
|
+
this._nodes.forEach((n) => {
|
|
124
|
+
if (n.parentElement)
|
|
125
|
+
n.parentElement.removeChild(n);
|
|
126
|
+
});
|
|
127
|
+
this._nodes = [];
|
|
128
|
+
}
|
|
129
|
+
connectedCallback() {
|
|
130
|
+
this.refresh();
|
|
131
|
+
}
|
|
132
|
+
disconnectedCallback() {
|
|
133
|
+
this.cleanup();
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
__decorate([
|
|
137
|
+
changedAttribute("on-get-value"),
|
|
138
|
+
__metadata("design:type", Function),
|
|
139
|
+
__metadata("design:paramtypes", [String]),
|
|
140
|
+
__metadata("design:returntype", void 0)
|
|
141
|
+
], SwcIf.prototype, "refresh", null);
|
|
142
|
+
SwcIf = __decorate([
|
|
143
|
+
elementDefine({ name: "swc-if", extends: "template" })
|
|
144
|
+
], SwcIf);
|
|
145
|
+
export {
|
|
146
|
+
SwcIf
|
|
147
|
+
};
|
|
148
|
+
//# sourceMappingURL=SwcIf.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../../src/elements/SwcIf.ts"],
|
|
4
|
+
"sourcesContent": ["import { elementDefine } from '../decorators/elementDefine';\nimport { changedAttribute } from '../decorators/changedAttribute';\nimport { SwcUtils } from '../utils/Utils';\nimport { FunctionUtils } from '@dooboostore/core/function/FunctionUtils';\n\n@elementDefine({ name: 'swc-if', extends: 'template' })\nexport class SwcIf extends HTMLTemplateElement {\n private _nodes: Node[] = [];\n private _value: any = null;\n\n get value(): any {\n return this._value;\n }\n\n set value(nv: any) {\n this._value = nv;\n this.render();\n }\n\n private _getTarget(): Element | DocumentFragment | null {\n const script = this.getAttribute('on-get-portal');\n if (!script) return null;\n try {\n const res = FunctionUtils.executeReturn({\n script,\n context: this,\n args: SwcUtils.getHelperAndHostSet(window, this)\n });\n if (typeof res === 'string') return document.querySelector(res);\n if (res instanceof Node) return res as any;\n } catch (e) {\n console.error('[SwcIf] Portal target error:', e);\n }\n return null;\n }\n\n private _executeCallback(attrName: string, extraArgs: Record<string, any>) {\n const script = this.getAttribute(attrName);\n if (!script) return;\n const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);\n try {\n FunctionUtils.execute({ script, context: this, args: { ...helperHostSet, ...extraArgs } });\n } catch (e) {\n console.error(`[SwcIf] Error in ${attrName}:`, e);\n }\n }\n\n @changedAttribute('on-get-value')\n private refresh(newValue?: string | null) {\n const script = newValue !== undefined ? newValue : this.getAttribute('on-get-value');\n if (!script) {\n this.value = null;\n return;\n }\n if (!this.parentElement) return;\n\n const helperHostSet = SwcUtils.getHelperAndHostSet(window, this);\n try {\n const result = FunctionUtils.executeReturn({\n script,\n context: this,\n args: helperHostSet\n });\n this.value = result;\n } catch (e) {\n console.error('[SwcIf] Error in on-get-value:', e);\n this.value = null;\n }\n }\n\n private render() {\n this.cleanup();\n if (!this.parentElement) return;\n\n let fragment: DocumentFragment | null = null;\n const ctx: any = { $value: this._value, $nodes: [], $elements: [] };\n\n if (this._value) {\n const clone = this.content.cloneNode(true) as DocumentFragment;\n Array.from(clone.querySelectorAll('template[is=\"swc-default\"]')).forEach(t => t.remove());\n fragment = clone;\n } else {\n const t = this.content.querySelector('template[is=\"swc-default\"]') as HTMLTemplateElement;\n if (t) fragment = t.content.cloneNode(true) as DocumentFragment;\n }\n\n if (fragment) {\n this.mount(fragment, ctx);\n }\n }\n\n private mount(fragment: DocumentFragment, ctx: any) {\n const nodes = Array.from(fragment.childNodes);\n const elements = nodes.filter(n => n.nodeType === 1) as HTMLElement[];\n\n Object.assign(ctx, {\n $nodes: nodes,\n $elements: elements,\n $firstNode: nodes[0],\n $lastNode: nodes[nodes.length - 1],\n $firstElement: elements[0],\n $lastElement: elements[elements.length - 1]\n });\n\n this._executeCallback('on-clone-nodes', ctx);\n nodes.forEach((node, nodeIndex) => {\n if (node instanceof HTMLElement) (node as any).__swc_host = this;\n this._executeCallback('on-clone-node', { ...ctx, $node: node, $nodeIndex: nodeIndex, $isElement: node.nodeType === 1 });\n });\n\n this._nodes = nodes;\n const target = this._getTarget();\n if (target) {\n target.appendChild(fragment);\n } else {\n this.parentElement?.insertBefore(fragment, this.nextSibling);\n }\n }\n\n private cleanup() {\n this._nodes.forEach(n => {\n if (n.parentElement) n.parentElement.removeChild(n);\n });\n this._nodes = [];\n }\n\n connectedCallback() {\n this.refresh();\n }\n\n disconnectedCallback() {\n this.cleanup();\n }\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;AAAA,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AACjC,SAAS,gBAAgB;AACzB,SAAS,qBAAqB;AAGvB,IAAM,QAAN,MAAMA,eAAc,oBAAmB;EAAvC,cAAA;;AACG,SAAA,SAAiB,CAAA;AACjB,SAA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