@dooboostore/simple-web-component 1.0.33 → 1.0.34
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/dist/cjs/decorators/addEventListener.js.map +1 -1
- package/dist/cjs/decorators/applyAttribute.js +67 -57
- package/dist/cjs/decorators/applyAttribute.js.map +2 -2
- package/dist/cjs/decorators/applyClass.js +80 -70
- package/dist/cjs/decorators/applyClass.js.map +2 -2
- package/dist/cjs/decorators/applyNode.js +1 -8
- package/dist/cjs/decorators/applyNode.js.map +2 -2
- package/dist/cjs/decorators/applyStyle.js +66 -56
- package/dist/cjs/decorators/applyStyle.js.map +2 -2
- package/dist/cjs/decorators/attribute.js +35 -29
- package/dist/cjs/decorators/attribute.js.map +2 -2
- package/dist/cjs/decorators/changedAttributeHost.js +32 -12
- package/dist/cjs/decorators/changedAttributeHost.js.map +2 -2
- package/dist/cjs/decorators/elementDefine.js +29 -16
- package/dist/cjs/decorators/elementDefine.js.map +2 -2
- package/dist/cjs/decorators/emitCustomEvent.js +57 -44
- package/dist/cjs/decorators/emitCustomEvent.js.map +2 -2
- package/dist/cjs/decorators/setProperty.js +50 -39
- package/dist/cjs/decorators/setProperty.js.map +2 -2
- package/dist/cjs/decorators/subscribeSwcAppMessageWhileConnected.js.map +1 -1
- package/dist/cjs/decorators/subscribeSwcAppRouteChangeWhileConnected.js.map +1 -1
- package/dist/cjs/elements/SwcAppEngine.js +7 -7
- package/dist/cjs/elements/SwcAppEngine.js.map +3 -3
- package/dist/cjs/elements/SwcAppMixin.js +60 -42
- package/dist/cjs/elements/SwcAppMixin.js.map +2 -2
- package/dist/cjs/elements/SwcChoose.js +19 -15
- package/dist/cjs/elements/SwcChoose.js.map +2 -2
- package/dist/cjs/elements/register.js +21 -16
- package/dist/cjs/elements/register.js.map +2 -2
- package/dist/cjs/utils/Utils.js +7 -3
- package/dist/cjs/utils/Utils.js.map +2 -2
- package/dist/esm/decorators/addEventListener.js.map +1 -1
- package/dist/esm/decorators/applyAttribute.js +67 -57
- package/dist/esm/decorators/applyAttribute.js.map +2 -2
- package/dist/esm/decorators/applyClass.js +80 -70
- package/dist/esm/decorators/applyClass.js.map +2 -2
- package/dist/esm/decorators/applyNode.js +1 -8
- package/dist/esm/decorators/applyNode.js.map +2 -2
- package/dist/esm/decorators/applyStyle.js +66 -56
- package/dist/esm/decorators/applyStyle.js.map +2 -2
- package/dist/esm/decorators/attribute.js +35 -29
- package/dist/esm/decorators/attribute.js.map +2 -2
- package/dist/esm/decorators/changedAttributeHost.js +32 -12
- package/dist/esm/decorators/changedAttributeHost.js.map +2 -2
- package/dist/esm/decorators/elementDefine.js +30 -17
- package/dist/esm/decorators/elementDefine.js.map +2 -2
- package/dist/esm/decorators/emitCustomEvent.js +57 -44
- package/dist/esm/decorators/emitCustomEvent.js.map +2 -2
- package/dist/esm/decorators/setProperty.js +50 -39
- package/dist/esm/decorators/setProperty.js.map +2 -2
- package/dist/esm/decorators/subscribeSwcAppMessageWhileConnected.js.map +1 -1
- package/dist/esm/decorators/subscribeSwcAppRouteChangeWhileConnected.js.map +1 -1
- package/dist/esm/elements/SwcAppEngine.js +6 -6
- package/dist/esm/elements/SwcAppEngine.js.map +2 -2
- package/dist/esm/elements/SwcAppMixin.js +60 -42
- package/dist/esm/elements/SwcAppMixin.js.map +2 -2
- package/dist/esm/elements/SwcChoose.js +20 -16
- package/dist/esm/elements/SwcChoose.js.map +2 -2
- package/dist/esm/elements/register.js +22 -17
- package/dist/esm/elements/register.js.map +2 -2
- package/dist/esm/utils/Utils.js +7 -3
- package/dist/esm/utils/Utils.js.map +2 -2
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js +425 -335
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js.map +3 -3
- package/dist/types/decorators/addEventListener.d.ts +4 -1
- package/dist/types/decorators/addEventListener.d.ts.map +1 -1
- package/dist/types/decorators/applyAttribute.d.ts +5 -1
- package/dist/types/decorators/applyAttribute.d.ts.map +1 -1
- package/dist/types/decorators/applyClass.d.ts +4 -0
- package/dist/types/decorators/applyClass.d.ts.map +1 -1
- package/dist/types/decorators/applyNode.d.ts +4 -1
- package/dist/types/decorators/applyNode.d.ts.map +1 -1
- package/dist/types/decorators/applyStyle.d.ts +5 -1
- package/dist/types/decorators/applyStyle.d.ts.map +1 -1
- package/dist/types/decorators/attribute.d.ts +5 -0
- package/dist/types/decorators/attribute.d.ts.map +1 -1
- package/dist/types/decorators/changedAttributeHost.d.ts +13 -3
- package/dist/types/decorators/changedAttributeHost.d.ts.map +1 -1
- package/dist/types/decorators/elementDefine.d.ts +1 -1
- package/dist/types/decorators/elementDefine.d.ts.map +1 -1
- package/dist/types/decorators/emitCustomEvent.d.ts +5 -1
- package/dist/types/decorators/emitCustomEvent.d.ts.map +1 -1
- package/dist/types/decorators/setProperty.d.ts +5 -1
- package/dist/types/decorators/setProperty.d.ts.map +1 -1
- package/dist/types/decorators/subscribeSwcAppMessageWhileConnected.d.ts +2 -2
- package/dist/types/decorators/subscribeSwcAppMessageWhileConnected.d.ts.map +1 -1
- package/dist/types/decorators/subscribeSwcAppRouteChangeWhileConnected.d.ts +2 -2
- package/dist/types/decorators/subscribeSwcAppRouteChangeWhileConnected.d.ts.map +1 -1
- package/dist/types/elements/SwcAppEngine.d.ts +6 -3
- package/dist/types/elements/SwcAppEngine.d.ts.map +1 -1
- package/dist/types/elements/SwcAppMixin.d.ts +5 -3
- package/dist/types/elements/SwcAppMixin.d.ts.map +1 -1
- package/dist/types/elements/register.d.ts.map +1 -1
- package/dist/types/utils/Utils.d.ts.map +1 -1
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js +425 -335
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js.map +3 -3
- package/package.json +3 -3
- package/src/decorators/addEventListener.ts +1 -1
- package/src/decorators/applyAttribute.ts +74 -61
- package/src/decorators/applyClass.ts +91 -77
- package/src/decorators/applyNode.ts +3 -12
- package/src/decorators/applyStyle.ts +75 -61
- package/src/decorators/attribute.ts +55 -43
- package/src/decorators/changedAttributeHost.ts +47 -14
- package/src/decorators/elementDefine.ts +38 -20
- package/src/decorators/emitCustomEvent.ts +65 -49
- package/src/decorators/setProperty.ts +58 -44
- package/src/decorators/subscribeSwcAppMessageWhileConnected.ts +2 -2
- package/src/decorators/subscribeSwcAppRouteChangeWhileConnected.ts +2 -2
- package/src/elements/SwcAppEngine.ts +11 -12
- package/src/elements/SwcAppMixin.ts +76 -44
- package/src/elements/SwcChoose.ts +28 -7
- package/src/elements/register.ts +28 -7
- package/src/utils/Utils.ts +15 -5
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dooboostore/simple-web-component",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.34",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -92,9 +92,9 @@
|
|
|
92
92
|
"webpack": "^5.93.0",
|
|
93
93
|
"webpack-cli": "^5.1.4",
|
|
94
94
|
"webpack-dev-server": "^5.2.0",
|
|
95
|
+
"@dooboostore/core": "1.0.31",
|
|
96
|
+
"@dooboostore/simple-boot": "1.0.55",
|
|
95
97
|
"@dooboostore/core-web": "1.0.10",
|
|
96
|
-
"@dooboostore/core": "1.0.30",
|
|
97
|
-
"@dooboostore/simple-boot": "1.0.54",
|
|
98
98
|
"@dooboostore/core-node": "1.0.12"
|
|
99
99
|
},
|
|
100
100
|
"scripts": {
|
|
@@ -10,7 +10,7 @@ export interface AddEventListenerBaseOptions extends EventListenerOptions {
|
|
|
10
10
|
preventDefault?: boolean;
|
|
11
11
|
removeOnDisconnected?: boolean;
|
|
12
12
|
delegate?: boolean;
|
|
13
|
-
filter?: (target: Event | CustomEvent, helper: HelperHostSet) => boolean;
|
|
13
|
+
filter?: (target: Event | CustomEvent, meta:{currentThis: any, helper: HelperHostSet}) => boolean;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export interface AddEventListenerMetadata {
|
|
@@ -7,6 +7,7 @@ export type AttributeAction = 'set' | 'update' | 'remove';
|
|
|
7
7
|
|
|
8
8
|
export interface AttributeApplyOptions extends SwcQueryOptions {
|
|
9
9
|
name?: string;
|
|
10
|
+
filter?: (target: HTMLElement, value: any, meta: {currentThis: any, helper: HelperHostSet}) => boolean;
|
|
10
11
|
}
|
|
11
12
|
|
|
12
13
|
export interface AttributeApplyMetadata {
|
|
@@ -43,76 +44,88 @@ function createAttributeDecorator(action: AttributeAction) {
|
|
|
43
44
|
});
|
|
44
45
|
|
|
45
46
|
const original = descriptor.value;
|
|
46
|
-
descriptor.value =
|
|
47
|
+
descriptor.value = function (...args: any[]) {
|
|
47
48
|
ensureInit(this);
|
|
48
|
-
const res =
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
49
|
+
const res = (original as any).apply(this, args);
|
|
50
|
+
|
|
51
|
+
const handleResult = (resolvedValue: any) => {
|
|
52
|
+
if (resolvedValue !== undefined) {
|
|
53
|
+
const win = (this as any).ownerDocument?.defaultView || window;
|
|
54
|
+
const host = this as unknown as HTMLElement;
|
|
55
|
+
const hostSet = { ...SwcUtils.getHelperAndHostSet(win, host), $this: this };
|
|
56
|
+
const r = finalOptions.root || 'auto';
|
|
57
|
+
const targetEls: HTMLElement[] = [];
|
|
58
|
+
|
|
59
|
+
const applyRoot = (t: any) => {
|
|
60
|
+
if (!t || !(t instanceof win.HTMLElement)) return;
|
|
61
|
+
targetEls.push(t);
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
if (selector === ':parentHost') applyRoot(hostSet.$parentHost);
|
|
65
|
+
else if (selector === ':appHost') applyRoot(hostSet.$appHost);
|
|
66
|
+
else if (selector === ':firstHost') applyRoot(hostSet.$firstHost);
|
|
67
|
+
else if (selector === ':lastHost') applyRoot(hostSet.$lastHost);
|
|
68
|
+
else if (selector === ':firstAppHost') applyRoot(hostSet.$firstAppHost);
|
|
69
|
+
else if (selector === ':lastAppHost') applyRoot(hostSet.$lastAppHost);
|
|
70
|
+
else if (selector === ':hosts') hostSet.$hosts.forEach(applyRoot);
|
|
71
|
+
else if (selector === ':appHosts') hostSet.$appHosts.forEach(applyRoot);
|
|
72
|
+
else if (selector === ':host' || !selector) applyRoot(host);
|
|
73
|
+
else {
|
|
74
|
+
const targetRoot = r === 'auto' ? (host.shadowRoot || host) : (r === 'shadow' ? host.shadowRoot : host);
|
|
75
|
+
if (targetRoot) {
|
|
76
|
+
const found = targetRoot.querySelectorAll(selector);
|
|
77
|
+
if (found) targetEls.push(...(Array.from(found) as HTMLElement[]));
|
|
78
|
+
}
|
|
75
79
|
}
|
|
76
|
-
}
|
|
77
80
|
|
|
78
|
-
|
|
79
|
-
|
|
81
|
+
targetEls.forEach(targetEl => {
|
|
82
|
+
if (finalOptions.filter && !finalOptions.filter(targetEl, resolvedValue, {currentThis: this, helper: hostSet})) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const resolvedRes = typeof resolvedValue === 'function' ? (resolvedValue as any)(targetEl, hostSet) : resolvedValue;
|
|
80
86
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
if (action === 'set' || action === 'update') {
|
|
88
|
+
if (attributeName) {
|
|
89
|
+
// Case: @setAttribute(selector, 'name')
|
|
90
|
+
if (resolvedRes === null || resolvedRes === undefined || resolvedRes === false) {
|
|
91
|
+
targetEl.removeAttribute(attributeName);
|
|
92
|
+
} else {
|
|
93
|
+
targetEl.setAttribute(attributeName, resolvedRes === true ? '' : String(resolvedRes));
|
|
94
|
+
}
|
|
95
|
+
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
96
|
+
// Case: @applyAttribute(selector) with Object return
|
|
97
|
+
Object.entries(resolvedRes as any).forEach(([attr, val]) => {
|
|
98
|
+
const finalVal = typeof val === 'function' ? val(targetEl, hostSet) : val;
|
|
99
|
+
if (finalVal === null || finalVal === undefined || finalVal === false) {
|
|
100
|
+
targetEl.removeAttribute(attr);
|
|
101
|
+
} else {
|
|
102
|
+
targetEl.setAttribute(attr, finalVal === true ? '' : String(finalVal));
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
} else if (action === 'remove') {
|
|
107
|
+
if (attributeName) {
|
|
85
108
|
targetEl.removeAttribute(attributeName);
|
|
109
|
+
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
110
|
+
Object.entries(resolvedRes as any).forEach(([attr, val]) => {
|
|
111
|
+
const shouldRemove = typeof val === 'function' ? val(targetEl, hostSet) : !!val;
|
|
112
|
+
if (shouldRemove) targetEl.removeAttribute(attr);
|
|
113
|
+
});
|
|
86
114
|
} else {
|
|
87
|
-
|
|
115
|
+
const attrs = Array.isArray(resolvedRes) ? resolvedRes : [resolvedRes];
|
|
116
|
+
attrs.forEach(a => targetEl.removeAttribute(String(a)));
|
|
88
117
|
}
|
|
89
|
-
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
90
|
-
// Case: @applyAttribute(selector) with Object return
|
|
91
|
-
Object.entries(resolvedRes as any).forEach(([attr, val]) => {
|
|
92
|
-
const finalVal = typeof val === 'function' ? val(targetEl, hostSet) : val;
|
|
93
|
-
if (finalVal === null || finalVal === undefined || finalVal === false) {
|
|
94
|
-
targetEl.removeAttribute(attr);
|
|
95
|
-
} else {
|
|
96
|
-
targetEl.setAttribute(attr, finalVal === true ? '' : String(finalVal));
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
} else if (action === 'remove') {
|
|
101
|
-
if (attributeName) {
|
|
102
|
-
targetEl.removeAttribute(attributeName);
|
|
103
|
-
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
104
|
-
Object.entries(resolvedRes as any).forEach(([attr, val]) => {
|
|
105
|
-
const shouldRemove = typeof val === 'function' ? val(targetEl, hostSet) : !!val;
|
|
106
|
-
if (shouldRemove) targetEl.removeAttribute(attr);
|
|
107
|
-
});
|
|
108
|
-
} else {
|
|
109
|
-
const attrs = Array.isArray(resolvedRes) ? resolvedRes : [resolvedRes];
|
|
110
|
-
attrs.forEach(a => targetEl.removeAttribute(String(a)));
|
|
111
118
|
}
|
|
112
|
-
}
|
|
113
|
-
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
return resolvedValue;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
if (res instanceof Promise) {
|
|
125
|
+
return res.then(handleResult);
|
|
126
|
+
} else {
|
|
127
|
+
return handleResult(res);
|
|
114
128
|
}
|
|
115
|
-
return res;
|
|
116
129
|
};
|
|
117
130
|
return descriptor;
|
|
118
131
|
};
|
|
@@ -7,7 +7,9 @@ export const CLASS_METADATA_KEY = Symbol.for('simple-web-component:class');
|
|
|
7
7
|
|
|
8
8
|
export type ClassAction = 'set' | 'update' | 'add' | 'remove' | 'toggle';
|
|
9
9
|
|
|
10
|
-
export interface ClassApplyOptions extends SwcQueryOptions {
|
|
10
|
+
export interface ClassApplyOptions extends SwcQueryOptions {
|
|
11
|
+
filter?: (target: HTMLElement, value: any, meta: {currentThis: any, helper: HelperHostSet}) => boolean;
|
|
12
|
+
}
|
|
11
13
|
|
|
12
14
|
export interface ClassApplyMetadata {
|
|
13
15
|
propertyKey: string | symbol;
|
|
@@ -38,88 +40,100 @@ function createClassDecorator(action: ClassAction) {
|
|
|
38
40
|
metaList.set(propertyKey, { propertyKey, selector, action, options: finalOptions });
|
|
39
41
|
|
|
40
42
|
const original = descriptor.value;
|
|
41
|
-
descriptor.value =
|
|
43
|
+
descriptor.value = function (...args: any[]) {
|
|
42
44
|
ensureInit(this);
|
|
43
|
-
const res =
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
45
|
+
const res = (original as any).apply(this, args);
|
|
46
|
+
|
|
47
|
+
const handleResult = (resolvedValue: any) => {
|
|
48
|
+
if (resolvedValue !== undefined) {
|
|
49
|
+
const win = (this as any).ownerDocument?.defaultView || window;
|
|
50
|
+
const host = this as unknown as HTMLElement;
|
|
51
|
+
const hostSet = { ...SwcUtils.getHelperAndHostSet(win, host), $this: this };
|
|
52
|
+
const r = finalOptions.root || 'auto';
|
|
53
|
+
const targetEls: HTMLElement[] = [];
|
|
54
|
+
|
|
55
|
+
const applyRoot = (t: any) => {
|
|
56
|
+
if (!t || !(t instanceof win.HTMLElement)) return;
|
|
57
|
+
targetEls.push(t);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
if (selector === ':parentHost') applyRoot(hostSet.$parentHost);
|
|
61
|
+
else if (selector === ':appHost') applyRoot(hostSet.$appHost);
|
|
62
|
+
else if (selector === ':firstHost') applyRoot(hostSet.$firstHost);
|
|
63
|
+
else if (selector === ':lastHost') applyRoot(hostSet.$lastHost);
|
|
64
|
+
else if (selector === ':firstAppHost') applyRoot(hostSet.$firstAppHost);
|
|
65
|
+
else if (selector === ':lastAppHost') applyRoot(hostSet.$lastAppHost);
|
|
66
|
+
else if (selector === ':hosts') hostSet.$hosts.forEach(applyRoot);
|
|
67
|
+
else if (selector === ':appHosts') hostSet.$appHosts.forEach(applyRoot);
|
|
68
|
+
else if (selector === ':host' || !selector) applyRoot(host);
|
|
69
|
+
else {
|
|
70
|
+
const targetRoot = r === 'auto' ? (host.shadowRoot || host) : (r === 'shadow' ? host.shadowRoot : host);
|
|
71
|
+
if (targetRoot) {
|
|
72
|
+
const found = targetRoot.querySelectorAll(selector);
|
|
73
|
+
if (found) targetEls.push(...(Array.from(found) as HTMLElement[]));
|
|
74
|
+
}
|
|
70
75
|
}
|
|
71
|
-
}
|
|
72
76
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
if (action === 'set') {
|
|
77
|
-
let classes = '';
|
|
78
|
-
if (classMap && typeof classMap === 'string') {
|
|
79
|
-
classes = resolvedRes ? classMap : '';
|
|
80
|
-
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
81
|
-
classes = Object.entries(resolvedRes as any)
|
|
82
|
-
.filter(([_, force]) => typeof force === 'function' ? force(targetEl, hostSet) : !!force)
|
|
83
|
-
.map(([cls]) => cls)
|
|
84
|
-
.join(' ');
|
|
85
|
-
} else {
|
|
86
|
-
classes = Array.isArray(resolvedRes) ? (resolvedRes as any[]).join(' ') : String(resolvedRes);
|
|
87
|
-
}
|
|
88
|
-
targetEl.className = classes;
|
|
89
|
-
} else if (action === 'update' || action === 'toggle') {
|
|
90
|
-
if (classMap && typeof classMap === 'string') {
|
|
91
|
-
targetEl.classList.toggle(classMap, !!resolvedRes);
|
|
92
|
-
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
93
|
-
Object.entries(resolvedRes as any).forEach(([cls, force]) => {
|
|
94
|
-
const isActive = typeof force === 'function' ? force(targetEl, hostSet) : !!force;
|
|
95
|
-
targetEl.classList.toggle(cls, isActive);
|
|
96
|
-
});
|
|
97
|
-
} else {
|
|
98
|
-
const classes = Array.isArray(resolvedRes) ? (resolvedRes as any[]).map(String) : [String(resolvedRes)];
|
|
99
|
-
if (action === 'update') targetEl.classList.add(...classes);
|
|
100
|
-
else classes.forEach(c => targetEl.classList.toggle(c));
|
|
77
|
+
targetEls.forEach(targetEl => {
|
|
78
|
+
if (finalOptions.filter && !finalOptions.filter(targetEl, resolvedValue, {currentThis: this, helper: hostSet})) {
|
|
79
|
+
return;
|
|
101
80
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
81
|
+
const resolvedRes = typeof resolvedValue === 'function' ? (resolvedValue as any)(targetEl, hostSet) : resolvedValue;
|
|
82
|
+
|
|
83
|
+
if (action === 'set') {
|
|
84
|
+
let classes = '';
|
|
85
|
+
if (classMap && typeof classMap === 'string') {
|
|
86
|
+
classes = resolvedRes ? classMap : '';
|
|
87
|
+
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
88
|
+
classes = Object.entries(resolvedRes as any)
|
|
89
|
+
.filter(([_, force]) => typeof force === 'function' ? force(targetEl, hostSet) : !!force)
|
|
90
|
+
.map(([cls]) => cls)
|
|
91
|
+
.join(' ');
|
|
92
|
+
} else {
|
|
93
|
+
classes = Array.isArray(resolvedRes) ? (resolvedRes as any[]).join(' ') : String(resolvedRes);
|
|
94
|
+
}
|
|
95
|
+
targetEl.className = classes;
|
|
96
|
+
} else if (action === 'update' || action === 'toggle') {
|
|
97
|
+
if (classMap && typeof classMap === 'string') {
|
|
98
|
+
targetEl.classList.toggle(classMap, !!resolvedRes);
|
|
99
|
+
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
100
|
+
Object.entries(resolvedRes as any).forEach(([cls, force]) => {
|
|
101
|
+
const isActive = typeof force === 'function' ? force(targetEl, hostSet) : !!force;
|
|
102
|
+
targetEl.classList.toggle(cls, isActive);
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
const classes = Array.isArray(resolvedRes) ? (resolvedRes as any[]).map(String) : [String(resolvedRes)];
|
|
106
|
+
if (action === 'update') targetEl.classList.add(...classes);
|
|
107
|
+
else classes.forEach(c => targetEl.classList.toggle(c));
|
|
108
|
+
}
|
|
109
|
+
} else if (action === 'add' || action === 'remove') {
|
|
110
|
+
if (classMap && typeof classMap === 'string' && !!resolvedRes) {
|
|
111
|
+
if (action === 'add') targetEl.classList.add(classMap);
|
|
112
|
+
else targetEl.classList.remove(classMap);
|
|
113
|
+
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
114
|
+
Object.entries(resolvedRes as any).forEach(([cls, force]) => {
|
|
115
|
+
const shouldExecute = typeof force === 'function' ? force(targetEl, hostSet) : !!force;
|
|
116
|
+
if (shouldExecute) {
|
|
117
|
+
if (action === 'add') targetEl.classList.add(cls);
|
|
118
|
+
else targetEl.classList.remove(cls);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
} else {
|
|
122
|
+
const classes = Array.isArray(resolvedRes) ? (resolvedRes as any[]).map(String) : [String(resolvedRes)];
|
|
123
|
+
if (action === 'add') targetEl.classList.add(...classes);
|
|
124
|
+
else targetEl.classList.remove(...classes);
|
|
125
|
+
}
|
|
118
126
|
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return resolvedValue;
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
if (res instanceof Promise) {
|
|
133
|
+
return res.then(handleResult);
|
|
134
|
+
} else {
|
|
135
|
+
return handleResult(res);
|
|
121
136
|
}
|
|
122
|
-
return res;
|
|
123
137
|
};
|
|
124
138
|
return descriptor;
|
|
125
139
|
};
|
|
@@ -12,7 +12,7 @@ export interface ApplyNodeOptions {
|
|
|
12
12
|
* Filter function to determine whether to perform DOM operation.
|
|
13
13
|
* If it returns false, the operation is skipped.
|
|
14
14
|
*/
|
|
15
|
-
filter?: (target: HTMLElement | ShadowRoot, newValue: any, helper: HelperHostSet) => boolean;
|
|
15
|
+
filter?: (target: HTMLElement | ShadowRoot, newValue: any, meta:{currentThis: any,helper: HelperHostSet}) => boolean;
|
|
16
16
|
/**
|
|
17
17
|
* Optional loading content to display while an async method is executing.
|
|
18
18
|
*/
|
|
@@ -113,17 +113,8 @@ export function applyNode(selector: string, options: ApplyNodeOptions = { positi
|
|
|
113
113
|
const runApply = (target: any, val: any) => {
|
|
114
114
|
const pos = options.position || 'beforeEnd';
|
|
115
115
|
|
|
116
|
-
//
|
|
117
|
-
|
|
118
|
-
if (v instanceof Node) return !t.contains(v);
|
|
119
|
-
if (pos === 'innerHtml' && typeof v === 'string') return t.innerHTML !== v;
|
|
120
|
-
if (pos === 'replaceChildren' && v instanceof Node) return !t.contains(v);
|
|
121
|
-
return true;
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
const activeFilter = options.filter || defaultFilter;
|
|
125
|
-
|
|
126
|
-
if (activeFilter(target, val, hostSet)) {
|
|
116
|
+
// 사용자가 filter를 명시하면 그 조건으로, 아니면 항상 적용
|
|
117
|
+
if (!options.filter || options.filter(target, val, {currentThis: this, helper: hostSet})) {
|
|
127
118
|
applyToDom(target, val, pos, currentDoc);
|
|
128
119
|
}
|
|
129
120
|
};
|
|
@@ -5,7 +5,9 @@ import { SwcQueryOptions, HelperHostSet } from '../types';
|
|
|
5
5
|
|
|
6
6
|
export type StyleAction = 'set' | 'update' | 'remove';
|
|
7
7
|
|
|
8
|
-
export interface StyleApplyOptions extends SwcQueryOptions {
|
|
8
|
+
export interface StyleApplyOptions extends SwcQueryOptions {
|
|
9
|
+
filter?: (target: HTMLElement, value: any, meta: {currentThis: any, helper: HelperHostSet}) => boolean;
|
|
10
|
+
}
|
|
9
11
|
|
|
10
12
|
export interface StyleApplyMetadata {
|
|
11
13
|
propertyKey: string | symbol;
|
|
@@ -28,72 +30,84 @@ function createStyleDecorator(action: StyleAction) {
|
|
|
28
30
|
metaList.set(propertyKey, { propertyKey, selector, action, options });
|
|
29
31
|
|
|
30
32
|
const original = descriptor.value;
|
|
31
|
-
descriptor.value =
|
|
33
|
+
descriptor.value = function (...args: any[]) {
|
|
32
34
|
ensureInit(this);
|
|
33
|
-
const res =
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
35
|
+
const res = (original as any).apply(this, args);
|
|
36
|
+
|
|
37
|
+
const handleResult = (resolvedValue: any) => {
|
|
38
|
+
if (resolvedValue !== undefined) {
|
|
39
|
+
const win = (this as any).ownerDocument?.defaultView || window;
|
|
40
|
+
const host = this as unknown as HTMLElement;
|
|
41
|
+
const hostSet = SwcUtils.getHelperAndHostSet(win, host);
|
|
42
|
+
const r = options.root || 'auto';
|
|
43
|
+
const targetEls: HTMLElement[] = [];
|
|
44
|
+
|
|
45
|
+
const applyRoot = (t: any) => {
|
|
46
|
+
if (!t || !(t instanceof win.HTMLElement)) return;
|
|
47
|
+
targetEls.push(t);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (selector === ':parentHost') applyRoot(hostSet.$parentHost);
|
|
51
|
+
else if (selector === ':appHost') applyRoot(hostSet.$appHost);
|
|
52
|
+
else if (selector === ':firstHost') applyRoot(hostSet.$firstHost);
|
|
53
|
+
else if (selector === ':lastHost') applyRoot(hostSet.$lastHost);
|
|
54
|
+
else if (selector === ':firstAppHost') applyRoot(hostSet.$firstAppHost);
|
|
55
|
+
else if (selector === ':lastAppHost') applyRoot(hostSet.$lastAppHost);
|
|
56
|
+
else if (selector === ':hosts') hostSet.$hosts.forEach(applyRoot);
|
|
57
|
+
else if (selector === ':appHosts') hostSet.$appHosts.forEach(applyRoot);
|
|
58
|
+
else if (selector === ':host' || !selector) applyRoot(host);
|
|
59
|
+
else {
|
|
60
|
+
const targetRoot = r === 'auto' ? (host.shadowRoot || host) : (r === 'shadow' ? host.shadowRoot : host);
|
|
61
|
+
if (targetRoot) {
|
|
62
|
+
const found = targetRoot.querySelectorAll(selector);
|
|
63
|
+
if (found) targetEls.push(...(Array.from(found) as HTMLElement[]));
|
|
64
|
+
}
|
|
60
65
|
}
|
|
61
|
-
}
|
|
62
66
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (action === 'set' || action === 'update') {
|
|
67
|
-
if (action === 'set') targetEl.style.cssText = '';
|
|
68
|
-
|
|
69
|
-
if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
70
|
-
Object.entries(resolvedRes as any).forEach(([prop, val]) => {
|
|
71
|
-
const finalVal = typeof val === 'function' ? val(targetEl, hostSet) : val;
|
|
72
|
-
if (finalVal === null || finalVal === undefined) {
|
|
73
|
-
targetEl.style.removeProperty(prop);
|
|
74
|
-
} else {
|
|
75
|
-
targetEl.style[prop as any] = String(finalVal);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
} else if (typeof resolvedRes === 'string') {
|
|
79
|
-
targetEl.style.cssText = resolvedRes;
|
|
67
|
+
targetEls.forEach(targetEl => {
|
|
68
|
+
if (options.filter && !options.filter(targetEl, resolvedValue, {currentThis: this, helper: hostSet})) {
|
|
69
|
+
return;
|
|
80
70
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
71
|
+
const resolvedRes = typeof resolvedValue === 'function' ? (resolvedValue as any)(targetEl, hostSet) : resolvedValue;
|
|
72
|
+
|
|
73
|
+
if (action === 'set' || action === 'update') {
|
|
74
|
+
if (action === 'set') targetEl.style.cssText = '';
|
|
75
|
+
|
|
76
|
+
if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
77
|
+
Object.entries(resolvedRes as any).forEach(([prop, val]) => {
|
|
78
|
+
const finalVal = typeof val === 'function' ? val(targetEl, hostSet) : val;
|
|
79
|
+
if (finalVal === null || finalVal === undefined) {
|
|
80
|
+
targetEl.style.removeProperty(prop);
|
|
81
|
+
} else {
|
|
82
|
+
targetEl.style[prop as any] = String(finalVal);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
} else if (typeof resolvedRes === 'string') {
|
|
86
|
+
targetEl.style.cssText = resolvedRes;
|
|
87
|
+
}
|
|
88
|
+
} else if (action === 'remove') {
|
|
89
|
+
if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
90
|
+
Object.entries(resolvedRes as any).forEach(([prop, val]) => {
|
|
91
|
+
const shouldRemove = typeof val === 'function' ? val(targetEl, hostSet) : !!val;
|
|
92
|
+
if (shouldRemove) {
|
|
93
|
+
targetEl.style.removeProperty(prop);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
} else {
|
|
97
|
+
const props = Array.isArray(resolvedRes) ? resolvedRes : [resolvedRes];
|
|
98
|
+
props.forEach(p => targetEl.style.removeProperty(String(p)));
|
|
99
|
+
}
|
|
92
100
|
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
return resolvedValue;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
if (res instanceof Promise) {
|
|
107
|
+
return res.then(handleResult);
|
|
108
|
+
} else {
|
|
109
|
+
return handleResult(res);
|
|
95
110
|
}
|
|
96
|
-
return res;
|
|
97
111
|
};
|
|
98
112
|
return descriptor;
|
|
99
113
|
};
|