@dooboostore/simple-web-component 1.0.24 → 1.0.25
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 +64 -3406
- package/dist/cjs/decorators/addEventListener.js +1 -1
- package/dist/cjs/decorators/addEventListener.js.map +2 -2
- package/dist/cjs/decorators/applyAttribute.js +148 -0
- package/dist/cjs/decorators/applyAttribute.js.map +7 -0
- package/dist/cjs/decorators/applyClass.js +164 -0
- package/dist/cjs/decorators/applyClass.js.map +7 -0
- package/dist/cjs/decorators/applyNode.js +87 -194
- package/dist/cjs/decorators/applyNode.js.map +2 -2
- package/dist/cjs/decorators/{style.js → applyStyle.js} +50 -60
- package/dist/cjs/decorators/applyStyle.js.map +7 -0
- package/dist/cjs/decorators/attribute.js +10 -16
- package/dist/cjs/decorators/attribute.js.map +2 -2
- package/dist/cjs/decorators/changedAttributeHost.js +1 -1
- package/dist/cjs/decorators/changedAttributeHost.js.map +2 -2
- package/dist/cjs/decorators/elementDefine.js +14 -23
- package/dist/cjs/decorators/elementDefine.js.map +2 -2
- package/dist/cjs/decorators/emitCustomEvent.js +5 -2
- package/dist/cjs/decorators/emitCustomEvent.js.map +2 -2
- package/dist/cjs/decorators/index.js +3 -3
- package/dist/cjs/decorators/index.js.map +2 -2
- package/dist/cjs/decorators/lifecycles.js +9 -9
- package/dist/cjs/decorators/lifecycles.js.map +2 -2
- package/dist/cjs/decorators/query.js +1 -1
- package/dist/cjs/decorators/query.js.map +2 -2
- package/dist/cjs/decorators/queryAll.js +1 -1
- package/dist/cjs/decorators/queryAll.js.map +2 -2
- package/dist/cjs/elements/SwcAppEngine.js +19 -4
- package/dist/cjs/elements/SwcAppEngine.js.map +2 -2
- package/dist/cjs/elements/SwcAppMixin.js +2 -0
- package/dist/cjs/elements/SwcAppMixin.js.map +2 -2
- package/dist/cjs/utils/Utils.js +1 -1
- package/dist/cjs/utils/Utils.js.map +2 -2
- package/dist/esm/decorators/addEventListener.js +1 -1
- package/dist/esm/decorators/addEventListener.js.map +2 -2
- package/dist/esm/decorators/applyAttribute.js +129 -0
- package/dist/esm/decorators/applyAttribute.js.map +7 -0
- package/dist/esm/decorators/applyClass.js +145 -0
- package/dist/esm/decorators/applyClass.js.map +7 -0
- package/dist/esm/decorators/applyNode.js +87 -194
- package/dist/esm/decorators/applyNode.js.map +2 -2
- package/dist/esm/decorators/{style.js → applyStyle.js} +47 -57
- package/dist/esm/decorators/applyStyle.js.map +7 -0
- package/dist/esm/decorators/attribute.js +10 -16
- package/dist/esm/decorators/attribute.js.map +2 -2
- package/dist/esm/decorators/changedAttributeHost.js +1 -1
- package/dist/esm/decorators/changedAttributeHost.js.map +2 -2
- package/dist/esm/decorators/elementDefine.js +14 -23
- package/dist/esm/decorators/elementDefine.js.map +2 -2
- package/dist/esm/decorators/emitCustomEvent.js +5 -2
- package/dist/esm/decorators/emitCustomEvent.js.map +2 -2
- package/dist/esm/decorators/index.js +3 -3
- package/dist/esm/decorators/index.js.map +1 -1
- package/dist/esm/decorators/lifecycles.js +9 -9
- package/dist/esm/decorators/lifecycles.js.map +2 -2
- package/dist/esm/decorators/query.js +1 -1
- package/dist/esm/decorators/query.js.map +2 -2
- package/dist/esm/decorators/queryAll.js +1 -1
- package/dist/esm/decorators/queryAll.js.map +2 -2
- package/dist/esm/elements/SwcAppEngine.js +19 -4
- package/dist/esm/elements/SwcAppEngine.js.map +2 -2
- package/dist/esm/elements/SwcAppMixin.js +2 -0
- package/dist/esm/elements/SwcAppMixin.js.map +2 -2
- package/dist/esm/utils/Utils.js +1 -1
- package/dist/esm/utils/Utils.js.map +2 -2
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js +2884 -586
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js.map +4 -4
- package/dist/types/decorators/addEventListener.d.ts.map +1 -1
- package/dist/types/decorators/applyAttribute.d.ts +22 -0
- package/dist/types/decorators/applyAttribute.d.ts.map +1 -0
- package/dist/types/decorators/applyClass.d.ts +65 -0
- package/dist/types/decorators/applyClass.d.ts.map +1 -0
- package/dist/types/decorators/applyNode.d.ts +15 -123
- package/dist/types/decorators/applyNode.d.ts.map +1 -1
- package/dist/types/decorators/applyStyle.d.ts +21 -0
- package/dist/types/decorators/applyStyle.d.ts.map +1 -0
- package/dist/types/decorators/attribute.d.ts +10 -19
- package/dist/types/decorators/attribute.d.ts.map +1 -1
- package/dist/types/decorators/changedAttributeHost.d.ts.map +1 -1
- package/dist/types/decorators/elementDefine.d.ts.map +1 -1
- package/dist/types/decorators/emitCustomEvent.d.ts +1 -1
- package/dist/types/decorators/emitCustomEvent.d.ts.map +1 -1
- package/dist/types/decorators/index.d.ts +3 -3
- package/dist/types/decorators/index.d.ts.map +1 -1
- package/dist/types/decorators/lifecycles.d.ts.map +1 -1
- package/dist/types/decorators/query.d.ts.map +1 -1
- package/dist/types/decorators/queryAll.d.ts.map +1 -1
- package/dist/types/elements/SwcAppEngine.d.ts +7 -0
- package/dist/types/elements/SwcAppEngine.d.ts.map +1 -1
- package/dist/types/elements/SwcAppMixin.d.ts +1 -1
- package/dist/types/elements/SwcAppMixin.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 +2884 -586
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js.map +4 -4
- package/package.json +3 -3
- package/src/decorators/addEventListener.ts +1 -1
- package/src/decorators/applyAttribute.ts +147 -0
- package/src/decorators/applyClass.ts +158 -0
- package/src/decorators/applyNode.ts +112 -330
- package/src/decorators/applyStyle.ts +128 -0
- package/src/decorators/attribute.ts +21 -55
- package/src/decorators/changedAttributeHost.ts +1 -1
- package/src/decorators/elementDefine.ts +18 -39
- package/src/decorators/emitCustomEvent.ts +6 -3
- package/src/decorators/index.ts +3 -3
- package/src/decorators/lifecycles.ts +9 -11
- package/src/decorators/query.ts +1 -1
- package/src/decorators/queryAll.ts +1 -1
- package/src/elements/SwcAppEngine.ts +32 -4
- package/src/elements/SwcAppMixin.ts +2 -0
- package/src/utils/Utils.ts +2 -1
- package/dist/cjs/decorators/classList.js +0 -177
- package/dist/cjs/decorators/classList.js.map +0 -7
- package/dist/cjs/decorators/setAttribute.js +0 -111
- package/dist/cjs/decorators/setAttribute.js.map +0 -7
- package/dist/cjs/decorators/style.js.map +0 -7
- package/dist/esm/decorators/classList.js +0 -158
- package/dist/esm/decorators/classList.js.map +0 -7
- package/dist/esm/decorators/setAttribute.js +0 -92
- package/dist/esm/decorators/setAttribute.js.map +0 -7
- package/dist/esm/decorators/style.js.map +0 -7
- package/dist/types/decorators/classList.d.ts +0 -71
- package/dist/types/decorators/classList.d.ts.map +0 -1
- package/dist/types/decorators/setAttribute.d.ts +0 -23
- package/dist/types/decorators/setAttribute.d.ts.map +0 -1
- package/dist/types/decorators/style.d.ts +0 -58
- package/dist/types/decorators/style.d.ts.map +0 -1
- package/src/decorators/classList.ts +0 -234
- package/src/decorators/setAttribute.ts +0 -114
- package/src/decorators/style.ts +0 -185
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.25",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -94,8 +94,8 @@
|
|
|
94
94
|
"webpack-dev-server": "^5.2.0",
|
|
95
95
|
"@dooboostore/core": "1.0.30",
|
|
96
96
|
"@dooboostore/core-web": "1.0.10",
|
|
97
|
-
"@dooboostore/
|
|
98
|
-
"@dooboostore/
|
|
97
|
+
"@dooboostore/simple-boot": "1.0.54",
|
|
98
|
+
"@dooboostore/core-node": "1.0.12"
|
|
99
99
|
},
|
|
100
100
|
"scripts": {
|
|
101
101
|
"clean": "rm -rf dist",
|
|
@@ -19,7 +19,7 @@ export interface AddEventListenerMetadata {
|
|
|
19
19
|
options: AddEventListenerBaseOptions & SwcQueryOptions & { delegate?: boolean };
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export const ADD_EVENT_LISTENER_METADATA_KEY = Symbol('simple-web-component:add-event-listener');
|
|
22
|
+
export const ADD_EVENT_LISTENER_METADATA_KEY = Symbol.for('simple-web-component:add-event-listener');
|
|
23
23
|
|
|
24
24
|
export function addEventListener(target: SpecialSelector, type: string, options?: AddEventListenerBaseOptions): MethodDecorator;
|
|
25
25
|
export function addEventListener(selector: string, type: string, options?: AddEventListenerBaseOptions & SwcQueryOptions & { delegate?: boolean }): MethodDecorator;
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { ReflectUtils } from '@dooboostore/core';
|
|
2
|
+
import { ensureInit } from './elementDefine';
|
|
3
|
+
import { SwcUtils } from '../utils/Utils';
|
|
4
|
+
import { SwcQueryOptions, HelperHostSet } from '../types';
|
|
5
|
+
|
|
6
|
+
export type AttributeAction = 'set' | 'update' | 'remove';
|
|
7
|
+
|
|
8
|
+
export interface AttributeApplyOptions extends SwcQueryOptions {
|
|
9
|
+
name?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface AttributeApplyMetadata {
|
|
13
|
+
propertyKey: string | symbol;
|
|
14
|
+
selector: string;
|
|
15
|
+
action: AttributeAction;
|
|
16
|
+
options: AttributeApplyOptions;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const ATTRIBUTE_APPLY_METADATA_KEY = Symbol.for('simple-web-component:attribute-apply');
|
|
20
|
+
|
|
21
|
+
function createAttributeDecorator(action: AttributeAction) {
|
|
22
|
+
return (selector: string, nameOrOptions?: string | AttributeApplyOptions, options: AttributeApplyOptions = {}): MethodDecorator => {
|
|
23
|
+
return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
24
|
+
const constructor = target.constructor;
|
|
25
|
+
let metaList = ReflectUtils.getOwnMetadata(ATTRIBUTE_APPLY_METADATA_KEY, constructor) as Map<string | symbol, AttributeApplyMetadata>;
|
|
26
|
+
if (!metaList) {
|
|
27
|
+
metaList = new Map<string | symbol, AttributeApplyMetadata>();
|
|
28
|
+
ReflectUtils.defineMetadata(ATTRIBUTE_APPLY_METADATA_KEY, metaList, constructor);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let attributeName = typeof nameOrOptions === 'string' ? nameOrOptions : undefined;
|
|
32
|
+
let finalOptions = options;
|
|
33
|
+
|
|
34
|
+
if (typeof nameOrOptions === 'object' && nameOrOptions !== null) {
|
|
35
|
+
finalOptions = nameOrOptions as AttributeApplyOptions;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
metaList.set(propertyKey, {
|
|
39
|
+
propertyKey,
|
|
40
|
+
selector,
|
|
41
|
+
action,
|
|
42
|
+
options: { ...finalOptions, name: attributeName }
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const original = descriptor.value;
|
|
46
|
+
descriptor.value = async function (...args: any[]) {
|
|
47
|
+
ensureInit(this);
|
|
48
|
+
const res = await (original as any).apply(this, args);
|
|
49
|
+
if (res !== undefined) {
|
|
50
|
+
const win = (this as any).ownerDocument?.defaultView || window;
|
|
51
|
+
const host = this as unknown as HTMLElement;
|
|
52
|
+
const hostSet = { ...SwcUtils.getHelperAndHostSet(win, host), $this: this };
|
|
53
|
+
const r = finalOptions.root || 'auto';
|
|
54
|
+
const targetEls: HTMLElement[] = [];
|
|
55
|
+
|
|
56
|
+
const applyRoot = (t: any) => {
|
|
57
|
+
if (!t || !(t instanceof win.HTMLElement)) return;
|
|
58
|
+
targetEls.push(t);
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
if (selector === ':parentHost') applyRoot(hostSet.$parentHost);
|
|
62
|
+
else if (selector === ':appHost') applyRoot(hostSet.$appHost);
|
|
63
|
+
else if (selector === ':firstHost') applyRoot(hostSet.$firstHost);
|
|
64
|
+
else if (selector === ':lastHost') applyRoot(hostSet.$lastHost);
|
|
65
|
+
else if (selector === ':firstAppHost') applyRoot(hostSet.$firstAppHost);
|
|
66
|
+
else if (selector === ':lastAppHost') applyRoot(hostSet.$lastAppHost);
|
|
67
|
+
else if (selector === ':hosts') hostSet.$hosts.forEach(applyRoot);
|
|
68
|
+
else if (selector === ':appHosts') hostSet.$appHosts.forEach(applyRoot);
|
|
69
|
+
else if (selector === ':host' || !selector) applyRoot(host);
|
|
70
|
+
else {
|
|
71
|
+
const targetRoot = r === 'auto' ? (host.shadowRoot || host) : (r === 'shadow' ? host.shadowRoot : host);
|
|
72
|
+
if (targetRoot) {
|
|
73
|
+
const found = targetRoot.querySelectorAll(selector);
|
|
74
|
+
if (found) targetEls.push(...(Array.from(found) as HTMLElement[]));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
targetEls.forEach(targetEl => {
|
|
79
|
+
const resolvedRes = typeof res === 'function' ? (res as any)(targetEl, hostSet) : res;
|
|
80
|
+
|
|
81
|
+
if (action === 'set' || action === 'update') {
|
|
82
|
+
if (attributeName) {
|
|
83
|
+
// Case: @setAttribute(selector, 'name')
|
|
84
|
+
if (resolvedRes === null || resolvedRes === undefined || resolvedRes === false) {
|
|
85
|
+
targetEl.removeAttribute(attributeName);
|
|
86
|
+
} else {
|
|
87
|
+
targetEl.setAttribute(attributeName, resolvedRes === true ? '' : String(resolvedRes));
|
|
88
|
+
}
|
|
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
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
return res;
|
|
116
|
+
};
|
|
117
|
+
return descriptor;
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function applyAttribute(selector: string, action: AttributeAction = 'update', options: AttributeApplyOptions = {}): MethodDecorator {
|
|
123
|
+
return createAttributeDecorator(action)(selector, options);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export const setAttribute = (selector: string, nameOrOptions?: string | AttributeApplyOptions, options: AttributeApplyOptions = {}) => createAttributeDecorator('set')(selector, nameOrOptions, options);
|
|
127
|
+
export const updateAttribute = (selector: string, nameOrOptions?: string | AttributeApplyOptions, options: AttributeApplyOptions = {}) => createAttributeDecorator('update')(selector, nameOrOptions, options);
|
|
128
|
+
export const removeAttribute = (selector: string, nameOrOptions?: string | AttributeApplyOptions, options: AttributeApplyOptions = {}) => createAttributeDecorator('remove')(selector, nameOrOptions, options);
|
|
129
|
+
|
|
130
|
+
// Host versions
|
|
131
|
+
export function applyAttributeHost(action: AttributeAction = 'update', options: AttributeApplyOptions = {}): MethodDecorator {
|
|
132
|
+
return createAttributeDecorator(action)(':host', options);
|
|
133
|
+
}
|
|
134
|
+
export const setAttributeHost = (nameOrOptions: string | AttributeApplyOptions, options: AttributeApplyOptions = {}) => createAttributeDecorator('set')(':host', nameOrOptions, options);
|
|
135
|
+
export const updateAttributeHost = (nameOrOptions: string | AttributeApplyOptions, options: AttributeApplyOptions = {}) => createAttributeDecorator('update')(':host', nameOrOptions, options);
|
|
136
|
+
export const removeAttributeHost = (nameOrOptions?: string | AttributeApplyOptions, options: AttributeApplyOptions = {}) => createAttributeDecorator('remove')(':host', nameOrOptions, options);
|
|
137
|
+
|
|
138
|
+
export const findAllAttributeApplyMetadata = (target: any): Map<string | symbol, AttributeApplyMetadata> => {
|
|
139
|
+
const result = new Map<string | symbol, AttributeApplyMetadata>();
|
|
140
|
+
const maps = ReflectUtils.findAllMetadata<Map<string | symbol, AttributeApplyMetadata>>(ATTRIBUTE_APPLY_METADATA_KEY, target);
|
|
141
|
+
maps.forEach(map => {
|
|
142
|
+
if (map instanceof Map) {
|
|
143
|
+
map.forEach((v, k) => result.set(k, v));
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
return result;
|
|
147
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { ReflectUtils } from '@dooboostore/core';
|
|
2
|
+
import { ensureInit } from './elementDefine';
|
|
3
|
+
import { SwcUtils } from '../utils/Utils';
|
|
4
|
+
import { SwcQueryOptions, HelperHostSet } from '../types';
|
|
5
|
+
|
|
6
|
+
export const CLASS_METADATA_KEY = Symbol.for('simple-web-component:class');
|
|
7
|
+
|
|
8
|
+
export type ClassAction = 'set' | 'update' | 'add' | 'remove' | 'toggle';
|
|
9
|
+
|
|
10
|
+
export interface ClassApplyOptions extends SwcQueryOptions {}
|
|
11
|
+
|
|
12
|
+
export interface ClassApplyMetadata {
|
|
13
|
+
propertyKey: string | symbol;
|
|
14
|
+
selector: string;
|
|
15
|
+
action: ClassAction;
|
|
16
|
+
options: ClassApplyOptions;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function createClassDecorator(action: ClassAction) {
|
|
20
|
+
return (selector: string, classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}): MethodDecorator => {
|
|
21
|
+
return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
22
|
+
const constructor = target.constructor;
|
|
23
|
+
let metaList = ReflectUtils.getOwnMetadata(CLASS_METADATA_KEY, constructor) as Map<string | symbol, ClassApplyMetadata>;
|
|
24
|
+
if (!metaList) {
|
|
25
|
+
metaList = new Map<string | symbol, ClassApplyMetadata>();
|
|
26
|
+
ReflectUtils.defineMetadata(CLASS_METADATA_KEY, metaList, constructor);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let classMap: any = undefined;
|
|
30
|
+
let finalOptions = options;
|
|
31
|
+
|
|
32
|
+
if (typeof classMapOrOptions === 'string' || (typeof classMapOrOptions === 'object' && classMapOrOptions !== null && !('root' in classMapOrOptions))) {
|
|
33
|
+
classMap = classMapOrOptions;
|
|
34
|
+
} else if (typeof classMapOrOptions === 'object' && classMapOrOptions !== null && 'root' in classMapOrOptions) {
|
|
35
|
+
finalOptions = classMapOrOptions as ClassApplyOptions;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
metaList.set(propertyKey, { propertyKey, selector, action, options: finalOptions });
|
|
39
|
+
|
|
40
|
+
const original = descriptor.value;
|
|
41
|
+
descriptor.value = async function (...args: any[]) {
|
|
42
|
+
ensureInit(this);
|
|
43
|
+
const res = await (original as any).apply(this, args);
|
|
44
|
+
if (res !== undefined) {
|
|
45
|
+
const win = (this as any).ownerDocument?.defaultView || window;
|
|
46
|
+
const host = this as unknown as HTMLElement;
|
|
47
|
+
const hostSet = { ...SwcUtils.getHelperAndHostSet(win, host), $this: this };
|
|
48
|
+
const r = finalOptions.root || 'auto';
|
|
49
|
+
const targetEls: HTMLElement[] = [];
|
|
50
|
+
|
|
51
|
+
const applyRoot = (t: any) => {
|
|
52
|
+
if (!t || !(t instanceof win.HTMLElement)) return;
|
|
53
|
+
targetEls.push(t);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
if (selector === ':parentHost') applyRoot(hostSet.$parentHost);
|
|
57
|
+
else if (selector === ':appHost') applyRoot(hostSet.$appHost);
|
|
58
|
+
else if (selector === ':firstHost') applyRoot(hostSet.$firstHost);
|
|
59
|
+
else if (selector === ':lastHost') applyRoot(hostSet.$lastHost);
|
|
60
|
+
else if (selector === ':firstAppHost') applyRoot(hostSet.$firstAppHost);
|
|
61
|
+
else if (selector === ':lastAppHost') applyRoot(hostSet.$lastAppHost);
|
|
62
|
+
else if (selector === ':hosts') hostSet.$hosts.forEach(applyRoot);
|
|
63
|
+
else if (selector === ':appHosts') hostSet.$appHosts.forEach(applyRoot);
|
|
64
|
+
else if (selector === ':host' || !selector) applyRoot(host);
|
|
65
|
+
else {
|
|
66
|
+
const targetRoot = r === 'auto' ? (host.shadowRoot || host) : (r === 'shadow' ? host.shadowRoot : host);
|
|
67
|
+
if (targetRoot) {
|
|
68
|
+
const found = targetRoot.querySelectorAll(selector);
|
|
69
|
+
if (found) targetEls.push(...(Array.from(found) as HTMLElement[]));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
targetEls.forEach(targetEl => {
|
|
74
|
+
const resolvedRes = typeof res === 'function' ? (res as any)(targetEl, hostSet) : res;
|
|
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));
|
|
101
|
+
}
|
|
102
|
+
} else if (action === 'add' || action === 'remove') {
|
|
103
|
+
if (classMap && typeof classMap === 'string' && !!resolvedRes) {
|
|
104
|
+
if (action === 'add') targetEl.classList.add(classMap);
|
|
105
|
+
else targetEl.classList.remove(classMap);
|
|
106
|
+
} else if (typeof resolvedRes === 'object' && resolvedRes !== null && !Array.isArray(resolvedRes)) {
|
|
107
|
+
Object.entries(resolvedRes as any).forEach(([cls, force]) => {
|
|
108
|
+
const shouldExecute = typeof force === 'function' ? force(targetEl, hostSet) : !!force;
|
|
109
|
+
if (shouldExecute) {
|
|
110
|
+
if (action === 'add') targetEl.classList.add(cls);
|
|
111
|
+
else targetEl.classList.remove(cls);
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
const classes = Array.isArray(resolvedRes) ? (resolvedRes as any[]).map(String) : [String(resolvedRes)];
|
|
116
|
+
if (action === 'add') targetEl.classList.add(...classes);
|
|
117
|
+
else targetEl.classList.remove(...classes);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return res;
|
|
123
|
+
};
|
|
124
|
+
return descriptor;
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function applyClass(selector: string, action: ClassAction = 'update', options: ClassApplyOptions = {}): MethodDecorator {
|
|
130
|
+
return createClassDecorator(action)(selector, options);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const setClass = (selector: string, classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('set')(selector, classMapOrOptions, options);
|
|
134
|
+
export const updateClass = (selector: string, classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('update')(selector, classMapOrOptions, options);
|
|
135
|
+
export const addClass = (selector: string, classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('add')(selector, classMapOrOptions, options);
|
|
136
|
+
export const removeClass = (selector: string, classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('remove')(selector, classMapOrOptions, options);
|
|
137
|
+
export const toggleClass = (selector: string, classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('toggle')(selector, classMapOrOptions, options);
|
|
138
|
+
|
|
139
|
+
// Host versions
|
|
140
|
+
export function applyClassHost(action: ClassAction = 'update', options: ClassApplyOptions = {}): MethodDecorator {
|
|
141
|
+
return createClassDecorator(action)(':host', options);
|
|
142
|
+
}
|
|
143
|
+
export const setClassHost = (classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('set')(':host', classMapOrOptions, options);
|
|
144
|
+
export const updateClassHost = (classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('update')(':host', classMapOrOptions, options);
|
|
145
|
+
export const addClassHost = (classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('add')(':host', classMapOrOptions, options);
|
|
146
|
+
export const removeClassHost = (classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('remove')(':host', classMapOrOptions, options);
|
|
147
|
+
export const toggleClassHost = (classMapOrOptions?: string | { [className: string]: (el: HTMLElement, value: any, helper: HelperHostSet & { $this: any }) => boolean } | ClassApplyOptions, options: ClassApplyOptions = {}) => createClassDecorator('toggle')(':host', classMapOrOptions, options);
|
|
148
|
+
|
|
149
|
+
export const findAllClassMetadata = (target: any): Map<string | symbol, ClassApplyMetadata> => {
|
|
150
|
+
const result = new Map<string | symbol, ClassApplyMetadata>();
|
|
151
|
+
const maps = ReflectUtils.findAllMetadata<Map<string | symbol, ClassApplyMetadata>>(CLASS_METADATA_KEY, target);
|
|
152
|
+
maps.forEach(map => {
|
|
153
|
+
if (map instanceof Map) {
|
|
154
|
+
map.forEach((v, k) => result.set(k, v));
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
return result;
|
|
158
|
+
};
|