@dooboostore/simple-web-component 1.0.23 → 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 -3212
- 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 +97 -109
- package/dist/cjs/decorators/applyNode.js.map +2 -2
- package/dist/cjs/decorators/applyStyle.js +135 -0
- package/dist/cjs/decorators/applyStyle.js.map +7 -0
- package/dist/cjs/decorators/attribute.js +24 -11
- 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 +78 -3
- package/dist/cjs/decorators/query.js.map +2 -2
- package/dist/cjs/decorators/queryAll.js +78 -3
- 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/index.js +4 -4
- package/dist/cjs/index.js.map +2 -2
- package/dist/cjs/utils/Utils.js +141 -2
- 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 +97 -109
- package/dist/esm/decorators/applyNode.js.map +2 -2
- package/dist/esm/decorators/applyStyle.js +116 -0
- package/dist/esm/decorators/applyStyle.js.map +7 -0
- package/dist/esm/decorators/attribute.js +24 -11
- 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 +78 -3
- package/dist/esm/decorators/query.js.map +2 -2
- package/dist/esm/decorators/queryAll.js +78 -3
- 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/index.js +4 -4
- package/dist/esm/index.js.map +2 -2
- package/dist/esm/utils/Utils.js +141 -2
- package/dist/esm/utils/Utils.js.map +2 -2
- package/dist/esm-bundle/dooboostore-simple-web-component.esm.js +3191 -506
- 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 +18 -104
- 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 +12 -14
- 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 +68 -4
- package/dist/types/decorators/query.d.ts.map +1 -1
- package/dist/types/decorators/queryAll.d.ts +70 -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/index.d.ts +1 -2
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/utils/Utils.d.ts +49 -0
- package/dist/types/utils/Utils.d.ts.map +1 -1
- package/dist/umd-bundle/dooboostore-simple-web-component.umd.js +3191 -506
- 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 +126 -225
- package/src/decorators/applyStyle.ts +128 -0
- package/src/decorators/attribute.ts +42 -43
- package/src/decorators/changedAttributeHost.ts +2 -2
- 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 +148 -6
- package/src/decorators/queryAll.ts +149 -3
- package/src/elements/SwcAppEngine.ts +32 -4
- package/src/elements/SwcAppMixin.ts +2 -0
- package/src/index.ts +6 -6
- package/src/utils/Utils.ts +146 -1
- package/dist/cjs/decorators/classList.js +0 -134
- 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 +0 -114
- package/dist/cjs/decorators/style.js.map +0 -7
- package/dist/cjs/utils/TemplateUtils.js +0 -109
- package/dist/cjs/utils/TemplateUtils.js.map +0 -7
- package/dist/esm/decorators/classList.js +0 -115
- 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 +0 -95
- package/dist/esm/decorators/style.js.map +0 -7
- package/dist/esm/utils/TemplateUtils.js +0 -90
- package/dist/esm/utils/TemplateUtils.js.map +0 -7
- package/dist/types/decorators/classList.d.ts +0 -61
- 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 -49
- package/dist/types/decorators/style.d.ts.map +0 -1
- package/dist/types/utils/TemplateUtils.d.ts +0 -59
- package/dist/types/utils/TemplateUtils.d.ts.map +0 -1
- package/src/decorators/classList.ts +0 -172
- package/src/decorators/setAttribute.ts +0 -113
- package/src/decorators/style.ts +0 -140
- package/src/utils/TemplateUtils.ts +0 -92
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import { ReflectUtils } from '@dooboostore/core';
|
|
2
|
-
import { ensureInit } from './elementDefine';
|
|
3
|
-
import { SwcUtils } from '../utils/Utils';
|
|
4
|
-
import { SwcQueryOptions } from '../types';
|
|
5
|
-
|
|
6
|
-
export const CLASS_LIST_METADATA_KEY = Symbol('simple-web-component:class-list');
|
|
7
|
-
|
|
8
|
-
export type ClassListAction = 'set' | 'update' | 'add' | 'remove' | 'toggle';
|
|
9
|
-
|
|
10
|
-
export interface ClassListMetadata {
|
|
11
|
-
propertyKey: string | symbol;
|
|
12
|
-
selector: string;
|
|
13
|
-
action: ClassListAction;
|
|
14
|
-
options: SwcQueryOptions;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function createClassListDecorator(action: ClassListAction) {
|
|
18
|
-
return (selector: string, options: SwcQueryOptions = {}): MethodDecorator => {
|
|
19
|
-
return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
20
|
-
const constructor = target.constructor;
|
|
21
|
-
let metaList = ReflectUtils.getOwnMetadata(CLASS_LIST_METADATA_KEY, constructor) as Map<string | symbol, ClassListMetadata>;
|
|
22
|
-
if (!metaList) {
|
|
23
|
-
metaList = new Map<string | symbol, ClassListMetadata>();
|
|
24
|
-
ReflectUtils.defineMetadata(CLASS_LIST_METADATA_KEY, metaList, constructor);
|
|
25
|
-
}
|
|
26
|
-
metaList.set(propertyKey, { propertyKey, selector, action, options });
|
|
27
|
-
|
|
28
|
-
const original = descriptor.value;
|
|
29
|
-
descriptor.value = async function (...args: any[]) {
|
|
30
|
-
ensureInit(this);
|
|
31
|
-
const res = await original.apply(this, args);
|
|
32
|
-
if (res !== undefined) {
|
|
33
|
-
const r = options.root || 'auto';
|
|
34
|
-
const hostSet = SwcUtils.getHostSet(this as any);
|
|
35
|
-
const targetEls: HTMLElement[] = [];
|
|
36
|
-
|
|
37
|
-
if (selector === ':host' || !selector) {
|
|
38
|
-
targetEls.push(this as any);
|
|
39
|
-
} else if (selector === ':parentHost') {
|
|
40
|
-
if (hostSet.$parentHost) targetEls.push(hostSet.$parentHost);
|
|
41
|
-
} else if (selector === ':appHost') {
|
|
42
|
-
if (hostSet.$appHost) targetEls.push(hostSet.$appHost);
|
|
43
|
-
} else {
|
|
44
|
-
if (r === 'shadow') {
|
|
45
|
-
const found = this.shadowRoot?.querySelectorAll(selector);
|
|
46
|
-
if (found) targetEls.push(...found);
|
|
47
|
-
} else if (r === 'light') {
|
|
48
|
-
const found = (this as any).querySelectorAll(selector);
|
|
49
|
-
if (found) targetEls.push(...found);
|
|
50
|
-
} else if (r === 'all') {
|
|
51
|
-
const sFound = this.shadowRoot?.querySelectorAll(selector);
|
|
52
|
-
const lFound = (this as any).querySelectorAll(selector);
|
|
53
|
-
if (sFound) targetEls.push(...sFound);
|
|
54
|
-
if (lFound) targetEls.push(...lFound);
|
|
55
|
-
} else {
|
|
56
|
-
// auto
|
|
57
|
-
const found = (this.shadowRoot || (this as any)).querySelectorAll(selector);
|
|
58
|
-
if (found) targetEls.push(...found);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
targetEls.forEach(targetEl => {
|
|
63
|
-
if (action === 'set') {
|
|
64
|
-
let classes = '';
|
|
65
|
-
if (typeof res === 'object' && res !== null && !Array.isArray(res)) {
|
|
66
|
-
classes = Object.entries(res)
|
|
67
|
-
.filter(([_, force]) => !!force)
|
|
68
|
-
.map(([cls]) => cls)
|
|
69
|
-
.join(' ');
|
|
70
|
-
} else {
|
|
71
|
-
classes = Array.isArray(res) ? res.join(' ') : String(res);
|
|
72
|
-
}
|
|
73
|
-
targetEl.className = classes;
|
|
74
|
-
} else if (action === 'update') {
|
|
75
|
-
if (typeof res === 'object' && res !== null && !Array.isArray(res)) {
|
|
76
|
-
Object.entries(res).forEach(([cls, force]) => {
|
|
77
|
-
targetEl.classList.toggle(cls, !!force);
|
|
78
|
-
});
|
|
79
|
-
} else if (res !== undefined && res !== null) {
|
|
80
|
-
const classes = Array.isArray(res) ? res.map(String) : [String(res)];
|
|
81
|
-
targetEl.classList.add(...classes);
|
|
82
|
-
}
|
|
83
|
-
} else if (action === 'add') {
|
|
84
|
-
const classes = Array.isArray(res) ? res.map(String) : [String(res)];
|
|
85
|
-
targetEl.classList.add(...classes);
|
|
86
|
-
} else if (action === 'remove') {
|
|
87
|
-
const classes = Array.isArray(res) ? res.map(String) : [String(res)];
|
|
88
|
-
targetEl.classList.remove(...classes);
|
|
89
|
-
} else if (action === 'toggle') {
|
|
90
|
-
const classes = Array.isArray(res) ? res.map(String) : [String(res)];
|
|
91
|
-
classes.forEach(c => targetEl.classList.toggle(c));
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
return res;
|
|
96
|
-
};
|
|
97
|
-
};
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* @setClassList decorator to surgically replace all classes on a target element.
|
|
103
|
-
* Returns: string | string[]
|
|
104
|
-
*/
|
|
105
|
-
export const setClassList = (selector: string, options: SwcQueryOptions = {}) => createClassListDecorator('set')(selector, options);
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* @updateClassList decorator to surgically update specific classes.
|
|
109
|
-
* Returns: Record<string, boolean> (toggle with force) | string | string[] (add)
|
|
110
|
-
*/
|
|
111
|
-
export const updateClassList = (selector: string, options: SwcQueryOptions = {}) => createClassListDecorator('update')(selector, options);
|
|
112
|
-
|
|
113
|
-
/**
|
|
114
|
-
* @addClassList decorator to surgically add classes to a target element.
|
|
115
|
-
* Returns: string | string[]
|
|
116
|
-
*/
|
|
117
|
-
export const addClassList = (selector: string, options: SwcQueryOptions = {}) => createClassListDecorator('add')(selector, options);
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* @removeClassList decorator to surgically remove classes from a target element.
|
|
121
|
-
* Returns: string | string[]
|
|
122
|
-
*/
|
|
123
|
-
export const removeClassList = (selector: string, options: SwcQueryOptions = {}) => createClassListDecorator('remove')(selector, options);
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* @toggleClassList decorator to surgically toggle classes on a target element.
|
|
127
|
-
* Returns: string | string[]
|
|
128
|
-
*/
|
|
129
|
-
export const toggleClassList = (selector: string, options: SwcQueryOptions = {}) => createClassListDecorator('toggle')(selector, options);
|
|
130
|
-
|
|
131
|
-
// Host-specific versions (for :host selector)
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* @setClassListHost decorator to set classes on :host element.
|
|
135
|
-
* Returns: string | string[]
|
|
136
|
-
*/
|
|
137
|
-
export const setClassListHost = (options: SwcQueryOptions = {}) => createClassListDecorator('set')(':host', options);
|
|
138
|
-
|
|
139
|
-
/**
|
|
140
|
-
* @updateClassListHost decorator to update specific classes on :host.
|
|
141
|
-
* Returns: Record<string, boolean> (toggle with force) | string | string[] (add)
|
|
142
|
-
*/
|
|
143
|
-
export const updateClassListHost = (options: SwcQueryOptions = {}) => createClassListDecorator('update')(':host', options);
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* @addClassListHost decorator to add classes to :host element.
|
|
147
|
-
* Returns: string | string[]
|
|
148
|
-
*/
|
|
149
|
-
export const addClassListHost = (options: SwcQueryOptions = {}) => createClassListDecorator('add')(':host', options);
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* @removeClassListHost decorator to remove classes from :host element.
|
|
153
|
-
* Returns: string | string[]
|
|
154
|
-
*/
|
|
155
|
-
export const removeClassListHost = (options: SwcQueryOptions = {}) => createClassListDecorator('remove')(':host', options);
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* @toggleClassListHost decorator to toggle classes on :host element.
|
|
159
|
-
* Returns: string | string[]
|
|
160
|
-
*/
|
|
161
|
-
export const toggleClassListHost = (options: SwcQueryOptions = {}) => createClassListDecorator('toggle')(':host', options);
|
|
162
|
-
|
|
163
|
-
export const findAllClassListMetadata = (target: any): Map<string | symbol, ClassListMetadata> => {
|
|
164
|
-
const result = new Map<string | symbol, ClassListMetadata>();
|
|
165
|
-
const maps = ReflectUtils.findAllMetadata<Map<string | symbol, ClassListMetadata>>(CLASS_LIST_METADATA_KEY, target);
|
|
166
|
-
maps.forEach(map => {
|
|
167
|
-
if (map instanceof Map) {
|
|
168
|
-
map.forEach((v, k) => result.set(k, v));
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
return result;
|
|
172
|
-
};
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { ReflectUtils } from '@dooboostore/core';
|
|
2
|
-
import { ensureInit, getElementConfig } from './elementDefine';
|
|
3
|
-
import { SwcUtils } from '../utils/Utils';
|
|
4
|
-
import { SwcQueryOptions } from '../types';
|
|
5
|
-
|
|
6
|
-
export interface SetAttributeMetadata {
|
|
7
|
-
propertyKey: string | symbol;
|
|
8
|
-
selector: string;
|
|
9
|
-
name: string;
|
|
10
|
-
options: SwcQueryOptions;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const SET_ATTRIBUTE_METADATA_KEY = Symbol('simple-web-component:set-attribute');
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @setAttribute decorator to surgically update an attribute of a target element.
|
|
17
|
-
* Usage: @setAttribute(':host', 'count')
|
|
18
|
-
* Usage: @setAttribute('#child', 'disabled')
|
|
19
|
-
*/
|
|
20
|
-
export function setAttribute(selector: string, name: string): MethodDecorator;
|
|
21
|
-
export function setAttribute(selector: string, name: string, options?: SwcQueryOptions): MethodDecorator;
|
|
22
|
-
export function setAttribute(selector: string, name: string, options: SwcQueryOptions = {}): MethodDecorator {
|
|
23
|
-
return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
24
|
-
const constructor = target.constructor;
|
|
25
|
-
let metaList = ReflectUtils.getOwnMetadata(SET_ATTRIBUTE_METADATA_KEY, constructor) as Map<string | symbol, SetAttributeMetadata>;
|
|
26
|
-
if (!metaList) {
|
|
27
|
-
metaList = new Map<string | symbol, SetAttributeMetadata>();
|
|
28
|
-
ReflectUtils.defineMetadata(SET_ATTRIBUTE_METADATA_KEY, metaList, constructor);
|
|
29
|
-
}
|
|
30
|
-
metaList.set(propertyKey, { propertyKey, selector, name, options });
|
|
31
|
-
|
|
32
|
-
const original = descriptor.value;
|
|
33
|
-
descriptor.value = async function (...args: any[]) {
|
|
34
|
-
ensureInit(this);
|
|
35
|
-
const res = await original.apply(this, args);
|
|
36
|
-
if (res !== undefined) {
|
|
37
|
-
const hostSet = SwcUtils.getHostSet(this as any);
|
|
38
|
-
const conf = getElementConfig(this);
|
|
39
|
-
const currentWin = (this as any)._resolveWindow?.(conf) || ((typeof window !== 'undefined' ? window : undefined) as Window);
|
|
40
|
-
|
|
41
|
-
const targetEls: any[] = [];
|
|
42
|
-
const r = options.root || 'auto';
|
|
43
|
-
|
|
44
|
-
const applyRoot = (t: any) => {
|
|
45
|
-
if (!t) return;
|
|
46
|
-
if (r === 'auto') {
|
|
47
|
-
targetEls.push((t as any).shadowRoot || t);
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
if (r === 'light' || r === 'all') targetEls.push(t);
|
|
51
|
-
if ((r === 'shadow' || r === 'all') && (t as any).shadowRoot) targetEls.push((t as any).shadowRoot);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
if (selector === ':window') targetEls.push(currentWin);
|
|
55
|
-
else if (selector === ':document') targetEls.push(currentWin.document as any);
|
|
56
|
-
else 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(this);
|
|
65
|
-
else {
|
|
66
|
-
const roots: any[] = [];
|
|
67
|
-
if (r === 'shadow') {
|
|
68
|
-
if ((this as any).shadowRoot) roots.push((this as any).shadowRoot);
|
|
69
|
-
} else if (r === 'light') {
|
|
70
|
-
roots.push(this as any);
|
|
71
|
-
} else if (r === 'all') {
|
|
72
|
-
roots.push(this as any);
|
|
73
|
-
if ((this as any).shadowRoot) roots.push((this as any).shadowRoot);
|
|
74
|
-
} else {
|
|
75
|
-
roots.push((this as any).shadowRoot || (this as any));
|
|
76
|
-
}
|
|
77
|
-
roots.forEach(root => targetEls.push(...root.querySelectorAll(selector)));
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
targetEls.forEach(targetEl => {
|
|
81
|
-
if (!targetEl || typeof (targetEl as any).setAttribute !== 'function') return;
|
|
82
|
-
if (res === null) (targetEl as any).removeAttribute(name);
|
|
83
|
-
else (targetEl as any).setAttribute(name, String(res));
|
|
84
|
-
})
|
|
85
|
-
}
|
|
86
|
-
return res;
|
|
87
|
-
};
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export const getSetAttributeMetadata = (target: any): Map<string | symbol, SetAttributeMetadata> | undefined => {
|
|
92
|
-
const constructor = target instanceof Function ? target : target.constructor;
|
|
93
|
-
return ReflectUtils.getMetadata(SET_ATTRIBUTE_METADATA_KEY, constructor);
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
export const findAllSetAttributeMetadata = (target: any): Map<string | symbol, SetAttributeMetadata> => {
|
|
97
|
-
const result = new Map<string | symbol, SetAttributeMetadata>();
|
|
98
|
-
const maps = ReflectUtils.findAllMetadata<Map<string | symbol, SetAttributeMetadata>>(SET_ATTRIBUTE_METADATA_KEY, target);
|
|
99
|
-
maps.forEach(map => {
|
|
100
|
-
if (map instanceof Map) {
|
|
101
|
-
map.forEach((v, k) => result.set(k, v));
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
return result;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* @setAttributeHost decorator - simplified version of @setAttribute for :host selector
|
|
109
|
-
* Usage: @setAttributeHost('aria-disabled')
|
|
110
|
-
*/
|
|
111
|
-
export function setAttributeHost(name: string, options?: SwcQueryOptions): MethodDecorator {
|
|
112
|
-
return setAttribute(':host', name, options);
|
|
113
|
-
}
|
package/src/decorators/style.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { ReflectUtils } from '@dooboostore/core';
|
|
2
|
-
import { ensureInit } from './elementDefine';
|
|
3
|
-
import { SwcUtils } from '../utils/Utils';
|
|
4
|
-
import { SwcQueryOptions } from '../types';
|
|
5
|
-
|
|
6
|
-
export type StyleAction = 'set' | 'update' | 'remove';
|
|
7
|
-
|
|
8
|
-
export interface StyleOptions extends SwcQueryOptions {
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface StyleMetadata {
|
|
12
|
-
propertyKey: string | symbol;
|
|
13
|
-
selector: string;
|
|
14
|
-
action: StyleAction;
|
|
15
|
-
options: StyleOptions;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const STYLE_METADATA_KEY = Symbol('simple-web-component:style');
|
|
19
|
-
|
|
20
|
-
function createStyleDecorator(action: StyleAction) {
|
|
21
|
-
return (selector: string, options: StyleOptions = {}): MethodDecorator => {
|
|
22
|
-
return (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
|
|
23
|
-
const constructor = target.constructor;
|
|
24
|
-
let metaList = ReflectUtils.getOwnMetadata(STYLE_METADATA_KEY, constructor) as Map<string | symbol, StyleMetadata>;
|
|
25
|
-
if (!metaList) {
|
|
26
|
-
metaList = new Map<string | symbol, StyleMetadata>();
|
|
27
|
-
ReflectUtils.defineMetadata(STYLE_METADATA_KEY, metaList, constructor);
|
|
28
|
-
}
|
|
29
|
-
metaList.set(propertyKey, { propertyKey, selector, action, options });
|
|
30
|
-
|
|
31
|
-
const original = descriptor.value;
|
|
32
|
-
descriptor.value = async function (...args: any[]) {
|
|
33
|
-
ensureInit(this);
|
|
34
|
-
const res = await original.apply(this, args);
|
|
35
|
-
if (res !== undefined) {
|
|
36
|
-
const r = options.root || 'auto';
|
|
37
|
-
const hostSet = SwcUtils.getHostSet(this as any);
|
|
38
|
-
const targetEls: HTMLElement[] = [];
|
|
39
|
-
|
|
40
|
-
if (selector === ':host' || !selector) {
|
|
41
|
-
targetEls.push(this as any);
|
|
42
|
-
} else if (selector === ':parentHost') {
|
|
43
|
-
if (hostSet.$parentHost) targetEls.push(hostSet.$parentHost);
|
|
44
|
-
} else if (selector === ':appHost') {
|
|
45
|
-
if (hostSet.$appHost) targetEls.push(hostSet.$appHost);
|
|
46
|
-
} else {
|
|
47
|
-
if (r === 'shadow') {
|
|
48
|
-
const found = this.shadowRoot?.querySelectorAll(selector);
|
|
49
|
-
if (found) targetEls.push(...found);
|
|
50
|
-
} else if (r === 'light') {
|
|
51
|
-
const found = (this as any).querySelectorAll(selector);
|
|
52
|
-
if (found) targetEls.push(...found);
|
|
53
|
-
} else if (r === 'all') {
|
|
54
|
-
const sFound = this.shadowRoot?.querySelectorAll(selector);
|
|
55
|
-
const lFound = (this as any).querySelectorAll(selector);
|
|
56
|
-
if (sFound) targetEls.push(...sFound);
|
|
57
|
-
if (lFound) targetEls.push(...lFound);
|
|
58
|
-
} else {
|
|
59
|
-
// auto
|
|
60
|
-
const found = (this.shadowRoot || (this as any)).querySelectorAll(selector);
|
|
61
|
-
if (found) targetEls.push(...found);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
targetEls.forEach(targetEl => {
|
|
66
|
-
if (action === 'set') {
|
|
67
|
-
targetEl.style.cssText = '';
|
|
68
|
-
if (typeof res === 'object' && res !== null) {
|
|
69
|
-
Object.assign(targetEl.style, res);
|
|
70
|
-
} else if (typeof res === 'string') {
|
|
71
|
-
targetEl.style.cssText = res;
|
|
72
|
-
}
|
|
73
|
-
} else if (action === 'update') {
|
|
74
|
-
if (typeof res === 'object' && res !== null) {
|
|
75
|
-
Object.assign(targetEl.style, res);
|
|
76
|
-
}
|
|
77
|
-
} else if (action === 'remove') {
|
|
78
|
-
const props = Array.isArray(res) ? res : [res];
|
|
79
|
-
props.forEach(p => targetEl.style.removeProperty(String(p)));
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
return res;
|
|
84
|
-
};
|
|
85
|
-
};
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* @setStyle decorator to surgically set styles on a target element (replaces all existing styles).
|
|
91
|
-
* Usage: @setStyle('#box') getStyles() { return { color: 'red', display: 'block' }; }
|
|
92
|
-
* Usage: @setStyle('#box', { root: 'shadow' }) getStyles() { return { color: 'red' }; }
|
|
93
|
-
*/
|
|
94
|
-
export const setStyle = (selector: string, options: StyleOptions = {}) => createStyleDecorator('set')(selector, options);
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* @setStyleHost decorator - simplified version of @setStyle for :host selector.
|
|
98
|
-
* Usage: @setStyleHost getStyles() { return { color: 'red', display: 'block' }; }
|
|
99
|
-
* Usage: @setStyleHost({ root: 'shadow' }) getStyles() { return { color: 'red' }; }
|
|
100
|
-
*/
|
|
101
|
-
export const setStyleHost = (options: StyleOptions = {}) => createStyleDecorator('set')(':host', options);
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @updateStyle decorator to surgically update styles on a target element (merges with existing styles).
|
|
105
|
-
* Usage: @updateStyle('#box') updateStyles() { return { color: 'blue' }; }
|
|
106
|
-
* Usage: @updateStyle('#box', { root: 'shadow' }) updateStyles() { return { color: 'blue' }; }
|
|
107
|
-
*/
|
|
108
|
-
export const updateStyle = (selector: string, options: StyleOptions = {}) => createStyleDecorator('update')(selector, options);
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* @updateStyleHost decorator - simplified version of @updateStyle for :host selector.
|
|
112
|
-
* Usage: @updateStyleHost updateStyles() { return { color: 'blue' }; }
|
|
113
|
-
* Usage: @updateStyleHost({ root: 'shadow' }) updateStyles() { return { color: 'blue' }; }
|
|
114
|
-
*/
|
|
115
|
-
export const updateStyleHost = (options: StyleOptions = {}) => createStyleDecorator('update')(':host', options);
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* @removeStyle decorator to surgically remove styles from a target element.
|
|
119
|
-
* Usage: @removeStyle('#box') clearStyles() { return ['color', 'display']; }
|
|
120
|
-
* Usage: @removeStyle('#box', { root: 'shadow' }) clearStyles() { return ['color']; }
|
|
121
|
-
*/
|
|
122
|
-
export const removeStyle = (selector: string, options: StyleOptions = {}) => createStyleDecorator('remove')(selector, options);
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* @removeStyleHost decorator - simplified version of @removeStyle for :host selector.
|
|
126
|
-
* Usage: @removeStyleHost clearStyles() { return ['color', 'display']; }
|
|
127
|
-
* Usage: @removeStyleHost({ root: 'shadow' }) clearStyles() { return ['color']; }
|
|
128
|
-
*/
|
|
129
|
-
export const removeStyleHost = (options: StyleOptions = {}) => createStyleDecorator('remove')(':host', options);
|
|
130
|
-
|
|
131
|
-
export const findAllStyleMetadata = (target: any): Map<string | symbol, StyleMetadata> => {
|
|
132
|
-
const result = new Map<string | symbol, StyleMetadata>();
|
|
133
|
-
const maps = ReflectUtils.findAllMetadata<Map<string | symbol, StyleMetadata>>(STYLE_METADATA_KEY, target);
|
|
134
|
-
maps.forEach(map => {
|
|
135
|
-
if (map instanceof Map) {
|
|
136
|
-
map.forEach((v, k) => result.set(k, v));
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
return result;
|
|
140
|
-
};
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
export interface CreateElementInfo {
|
|
2
|
-
tagName: string;
|
|
3
|
-
innerHtml?: string;
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export namespace TemplateUtils {
|
|
7
|
-
/**
|
|
8
|
-
* Tagged template function to create a DocumentFragment from HTML string.
|
|
9
|
-
*/
|
|
10
|
-
export const htmlFragment = (strings: TemplateStringsArray, ...values: any[]): DocumentFragment => {
|
|
11
|
-
const raw = strings.reduce((acc, str, i) => acc + str + (values[i] !== undefined ? values[i] : ''), '');
|
|
12
|
-
const template = document.createElement('template');
|
|
13
|
-
template.innerHTML = raw.trim();
|
|
14
|
-
return template.content;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Tagged template function to create a Text node from string.
|
|
19
|
-
*/
|
|
20
|
-
export const textNode = (strings: TemplateStringsArray, ...values: any[]): Text => {
|
|
21
|
-
const raw = strings.reduce((acc, str, i) => acc + str + (values[i] !== undefined ? values[i] : ''), '');
|
|
22
|
-
return document.createTextNode(raw);
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Tagged template function to create a Comment node from string.
|
|
27
|
-
*/
|
|
28
|
-
export const commentNode = (strings: TemplateStringsArray, ...values: any[]): Comment => {
|
|
29
|
-
const raw = strings.reduce((acc, str, i) => acc + str + (values[i] !== undefined ? values[i] : ''), '');
|
|
30
|
-
return document.createComment(raw);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Generic element creator using a configuration object and attribute map.
|
|
35
|
-
*/
|
|
36
|
-
export const createElement = <T extends HTMLElement>(info: CreateElementInfo, attrs: Record<string, any> = {}): T => {
|
|
37
|
-
const el = document.createElement(info.tagName) as T;
|
|
38
|
-
if (info.innerHtml !== undefined) el.innerHTML = info.innerHtml;
|
|
39
|
-
Object.entries(attrs).forEach(([k, v]) => el.setAttribute(k, String(v)));
|
|
40
|
-
return el;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Internal factory to create tagged template functions for specific HTML elements.
|
|
45
|
-
*/
|
|
46
|
-
const createTagFunction = <T extends HTMLElement>(tagName: string) => {
|
|
47
|
-
return (strings: TemplateStringsArray, ...values: any[]): T => {
|
|
48
|
-
const el = document.createElement(tagName) as T;
|
|
49
|
-
el.innerHTML = strings.reduce((acc, str, i) => acc + str + (values[i] !== undefined ? values[i] : ''), '');
|
|
50
|
-
return el;
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
// Specific Element Factories
|
|
55
|
-
export const htmlDivElement = createTagFunction<HTMLDivElement>('div');
|
|
56
|
-
export const htmlSpanElement = createTagFunction<HTMLSpanElement>('span');
|
|
57
|
-
export const htmlButtonElement = createTagFunction<HTMLButtonElement>('button');
|
|
58
|
-
export const htmlUlElement = createTagFunction<HTMLUListElement>('ul');
|
|
59
|
-
export const htmlLiElement = createTagFunction<HTMLLIElement>('li');
|
|
60
|
-
export const htmlInputElement = createTagFunction<HTMLInputElement>('input');
|
|
61
|
-
export const htmlAElement = createTagFunction<HTMLAnchorElement>('a');
|
|
62
|
-
export const htmlPElement = createTagFunction<HTMLParagraphElement>('p');
|
|
63
|
-
export const htmlH1Element = createTagFunction<HTMLHeadingElement>('h1');
|
|
64
|
-
export const htmlH2Element = createTagFunction<HTMLHeadingElement>('h2');
|
|
65
|
-
export const htmlH3Element = createTagFunction<HTMLHeadingElement>('h3');
|
|
66
|
-
export const htmlH4Element = createTagFunction<HTMLHeadingElement>('h4');
|
|
67
|
-
export const htmlH5Element = createTagFunction<HTMLHeadingElement>('h5');
|
|
68
|
-
export const htmlH6Element = createTagFunction<HTMLHeadingElement>('h6');
|
|
69
|
-
export const htmlSectionElement = createTagFunction<HTMLElement>('section');
|
|
70
|
-
export const htmlArticleElement = createTagFunction<HTMLElement>('article');
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export const htmlFragment = TemplateUtils.htmlFragment;
|
|
74
|
-
export const textNode = TemplateUtils.textNode;
|
|
75
|
-
export const commentNode = TemplateUtils.commentNode;
|
|
76
|
-
export const createElement = TemplateUtils.createElement;
|
|
77
|
-
export const htmlDivElement = TemplateUtils.htmlDivElement;
|
|
78
|
-
export const htmlSpanElement = TemplateUtils.htmlSpanElement;
|
|
79
|
-
export const htmlButtonElement = TemplateUtils.htmlButtonElement;
|
|
80
|
-
export const htmlUlElement = TemplateUtils.htmlUlElement;
|
|
81
|
-
export const htmlLiElement = TemplateUtils.htmlLiElement;
|
|
82
|
-
export const htmlInputElement = TemplateUtils.htmlInputElement;
|
|
83
|
-
export const htmlAElement = TemplateUtils.htmlAElement;
|
|
84
|
-
export const htmlPElement = TemplateUtils.htmlPElement;
|
|
85
|
-
export const htmlH1Element = TemplateUtils.htmlH1Element;
|
|
86
|
-
export const htmlH2Element = TemplateUtils.htmlH2Element;
|
|
87
|
-
export const htmlH3Element = TemplateUtils.htmlH3Element;
|
|
88
|
-
export const htmlH4Element = TemplateUtils.htmlH4Element;
|
|
89
|
-
export const htmlH5Element = TemplateUtils.htmlH5Element;
|
|
90
|
-
export const htmlH6Element = TemplateUtils.htmlH6Element;
|
|
91
|
-
export const htmlSectionElement = TemplateUtils.htmlSectionElement;
|
|
92
|
-
export const htmlArticleElement = TemplateUtils.htmlArticleElement;
|