-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathcustomElement.ts
62 lines (55 loc) · 1.73 KB
/
customElement.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
60
61
62
import type UI5Element from "../UI5Element.js";
import type { Renderer } from "../UI5Element.js";
import type { TemplateFunction as Template } from "../renderer/executeTemplate.js";
import type { ComponentStylesData as Styles } from "../types.js";
/**
* Returns a custom element class decorator.
*
* @param { string | object } tagNameOrComponentSettings
* @returns { ClassDecorator }
*/
const customElement = (tagNameOrComponentSettings: string | {
tag?: string,
renderer?: Renderer,
styles?: Styles,
template?: Template,
dependencies?: Array<typeof UI5Element>,
staticAreaStyles?: Styles,
staticAreaTemplate?: Template,
languageAware?: boolean,
themeAware?: boolean,
fastNavigation?: boolean,
} = {}): ClassDecorator => {
return (target: any) => {
if (!Object.prototype.hasOwnProperty.call(target, "metadata")) {
target.metadata = {};
}
if (typeof tagNameOrComponentSettings === "string") {
target.metadata.tag = tagNameOrComponentSettings;
return;
}
const {
tag,
languageAware,
themeAware,
fastNavigation,
} = tagNameOrComponentSettings;
target.metadata.tag = tag;
if (languageAware) {
target.metadata.languageAware = languageAware;
}
if (themeAware) {
target.metadata.themeAware = themeAware;
}
if (fastNavigation) {
target.metadata.fastNavigation = fastNavigation;
}
["renderer", "template", "staticAreaTemplate", "styles", "staticAreaStyles", "dependencies"].forEach((customElementEntity: string) => {
const customElementEntityValue = tagNameOrComponentSettings[customElementEntity as keyof typeof tag];
customElementEntityValue && Object.defineProperty(target, customElementEntity, {
get: () => customElementEntityValue,
});
});
};
};
export default customElement;