@xtia/jel 0.3.2 → 0.4.0
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/index.d.ts +33 -30
- package/index.js +153 -183
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,32 +1,34 @@
|
|
|
1
1
|
export type ElementClassDescriptor = string | Record<string, boolean> | ElementClassDescriptor[];
|
|
2
|
-
export type DOMContent = number | null | string | Element | JelEntity<object
|
|
3
|
-
export type DomEntity<T extends HTMLElement> = JelEntity<ElementAPI<T
|
|
4
|
-
type
|
|
5
|
-
type
|
|
6
|
-
on?:
|
|
7
|
-
[EventID in keyof EventDataMap]
|
|
8
|
-
}
|
|
2
|
+
export type DOMContent = number | null | string | Element | JelEntity<object> | Text | DOMContent[];
|
|
3
|
+
export type DomEntity<T extends HTMLElement> = JelEntity<ElementAPI<T>>;
|
|
4
|
+
type PartConstructor<Spec, API extends object | void, EventDataMap> = (spec: Spec & EventSpec<EventDataMap>) => JelEntity<EventHost<API, EventDataMap>>;
|
|
5
|
+
type EventSpec<EventDataMap> = EventDataMap extends object ? {
|
|
6
|
+
on?: {
|
|
7
|
+
[EventID in keyof EventDataMap]+?: (data: EventDataMap[EventID]) => void;
|
|
8
|
+
};
|
|
9
|
+
} : {};
|
|
10
|
+
type JelEntity<API extends object | void> = (API extends void ? {} : API) & {
|
|
11
|
+
readonly [entityDataSymbol]: JelEntityData;
|
|
9
12
|
};
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
|
|
13
|
+
type CSSValue = string | number | null;
|
|
14
|
+
type StylesDescriptor = {
|
|
15
|
+
[K in keyof CSSStyleDeclaration as [
|
|
16
|
+
K,
|
|
17
|
+
CSSStyleDeclaration[K]
|
|
18
|
+
] extends [string, string] ? K : never]+?: CSSValue;
|
|
13
19
|
};
|
|
14
|
-
type
|
|
15
|
-
type StylesDescriptor = Partial<{
|
|
16
|
-
[key in keyof CSSStyleDeclaration]: CSSValue;
|
|
17
|
-
}>;
|
|
20
|
+
type StyleAccessor = StylesDescriptor & ((styles: StylesDescriptor) => void) & ((property: keyof StylesDescriptor, value: CSSValue) => void);
|
|
18
21
|
interface ElementDescriptor {
|
|
19
22
|
classes?: ElementClassDescriptor;
|
|
20
23
|
content?: DOMContent;
|
|
21
24
|
attribs?: Record<string, string | number | boolean>;
|
|
22
|
-
on?:
|
|
23
|
-
[E in keyof HTMLElementEventMap]
|
|
24
|
-
}
|
|
25
|
+
on?: {
|
|
26
|
+
[E in keyof HTMLElementEventMap]+?: (event: HTMLElementEventMap[E]) => void;
|
|
27
|
+
};
|
|
25
28
|
style?: StylesDescriptor;
|
|
26
29
|
cssVariables?: Record<string, CSSValue>;
|
|
27
30
|
}
|
|
28
|
-
type
|
|
29
|
-
type ElementAPI<T extends HTMLElement> = {
|
|
31
|
+
type ElementAPI<T extends HTMLElement> = EventHost<{
|
|
30
32
|
readonly element: T;
|
|
31
33
|
content: DOMContent;
|
|
32
34
|
classes: DOMTokenList;
|
|
@@ -37,7 +39,7 @@ type ElementAPI<T extends HTMLElement> = {
|
|
|
37
39
|
innerHTML: string;
|
|
38
40
|
setCSSVariable(table: Record<string, CSSValue>): void;
|
|
39
41
|
setCSSVariable(variableName: string, value: CSSValue): void;
|
|
40
|
-
qsa(selector: string): (Element | DomEntity<
|
|
42
|
+
qsa(selector: string): (Element | DomEntity<HTMLElement>)[];
|
|
41
43
|
append(...content: DOMContent[]): void;
|
|
42
44
|
remove(): void;
|
|
43
45
|
getRect(): DOMRect;
|
|
@@ -49,18 +51,14 @@ type ElementAPI<T extends HTMLElement> = {
|
|
|
49
51
|
} : T extends HTMLCanvasElement ? {
|
|
50
52
|
width: number;
|
|
51
53
|
height: number;
|
|
52
|
-
getContext
|
|
53
|
-
|
|
54
|
-
getContext(contextId: "webgl", options?: WebGLContextAttributes): WebGLRenderingContext | null;
|
|
55
|
-
getContext(contextId: "webgl2", options?: WebGLContextAttributes): WebGL2RenderingContext | null;
|
|
56
|
-
getContext(contextId: string, options?: any): RenderingContext | null;
|
|
57
|
-
} : {});
|
|
54
|
+
getContext: HTMLCanvasElement["getContext"];
|
|
55
|
+
} : {}), HTMLElementEventMap>;
|
|
58
56
|
type DomHelper = ((<T extends keyof HTMLElementTagNameMap>(tagName: T, descriptor: ElementDescriptor) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends keyof HTMLElementTagNameMap>(selector: `${T}#${string}`, content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends keyof HTMLElementTagNameMap>(selector: `${T}.${string}`, content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends keyof HTMLElementTagNameMap>(selector: T, content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>) & (<T extends HTMLElement>(element: T) => DomEntity<T>) & {
|
|
59
57
|
[T in keyof HTMLElementTagNameMap]: (descriptor: ElementDescriptor) => DomEntity<HTMLElementTagNameMap[T]>;
|
|
60
58
|
} & {
|
|
61
59
|
[T in keyof HTMLElementTagNameMap]: (content?: DOMContent) => DomEntity<HTMLElementTagNameMap[T]>;
|
|
62
60
|
});
|
|
63
|
-
type
|
|
61
|
+
type JelEntityData = {
|
|
64
62
|
dom: DOMContent;
|
|
65
63
|
};
|
|
66
64
|
type OptionalKeys<T> = {
|
|
@@ -69,8 +67,13 @@ type OptionalKeys<T> = {
|
|
|
69
67
|
type Optionals<T> = {
|
|
70
68
|
[K in OptionalKeys<T>]-?: Exclude<T[K], undefined>;
|
|
71
69
|
};
|
|
70
|
+
type ForbidKey<K extends string | symbol> = Record<string | symbol, any> & Partial<Record<K, never>>;
|
|
71
|
+
type EventHost<API extends object | void, EventDataMap> = (API extends object ? API : {}) & {
|
|
72
|
+
on<E extends keyof EventDataMap>(eventId: E, handler: (this: JelEntity<EventHost<API, EventDataMap>>, data: EventDataMap[E]) => void): void;
|
|
73
|
+
};
|
|
72
74
|
export declare const $: DomHelper;
|
|
73
|
-
declare const
|
|
74
|
-
|
|
75
|
-
export declare function
|
|
75
|
+
declare const entityDataSymbol: unique symbol;
|
|
76
|
+
export declare function createEntity<API extends object>(content: DOMContent, api: API extends DOMContent ? never : API): JelEntity<API>;
|
|
77
|
+
export declare function createEntity(content: DOMContent, api?: undefined): JelEntity<undefined>;
|
|
78
|
+
export declare function definePart<Spec extends ForbidKey<"on">, API extends ForbidKey<"on"> | void = void, EventDataMap extends Record<string, any> = {}>(defaultOptions: Optionals<Spec>, init: (spec: Required<Spec>, append: (content: DOMContent) => void, trigger: <K extends keyof EventDataMap>(eventId: K, eventData: EventDataMap[K]) => void) => API): PartConstructor<Spec, API, EventDataMap>;
|
|
76
79
|
export {};
|
package/index.js
CHANGED
|
@@ -1,62 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.$ = void 0;
|
|
4
|
+
exports.createEntity = createEntity;
|
|
15
5
|
exports.definePart = definePart;
|
|
16
|
-
|
|
17
|
-
get
|
|
18
|
-
return
|
|
6
|
+
const styleProxy = {
|
|
7
|
+
get(getStyle, prop) {
|
|
8
|
+
return getStyle()[prop];
|
|
19
9
|
},
|
|
20
|
-
set
|
|
21
|
-
|
|
10
|
+
set(getStyle, prop, value) {
|
|
11
|
+
getStyle()[prop] = value;
|
|
22
12
|
return true;
|
|
23
13
|
},
|
|
24
|
-
apply
|
|
25
|
-
|
|
26
|
-
var style = getStyle();
|
|
14
|
+
apply(getStyle, _, [stylesOrProp, value]) {
|
|
15
|
+
const style = getStyle();
|
|
27
16
|
if (typeof stylesOrProp == "object") {
|
|
28
|
-
Object.entries(stylesOrProp).forEach(
|
|
29
|
-
var prop = _a[0], val = _a[1];
|
|
30
|
-
return style[prop] = val;
|
|
31
|
-
});
|
|
17
|
+
Object.entries(stylesOrProp).forEach(([prop, val]) => style[prop] = val);
|
|
32
18
|
return;
|
|
33
19
|
}
|
|
34
|
-
style
|
|
20
|
+
style[stylesOrProp] = value;
|
|
35
21
|
},
|
|
22
|
+
deleteProperty(getStyle, prop) {
|
|
23
|
+
getStyle()[prop] = null;
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
36
26
|
};
|
|
37
|
-
function createElement(tag, descriptor) {
|
|
38
|
-
if (descriptor === void 0) { descriptor = {}; }
|
|
27
|
+
function createElement(tag, descriptor = {}) {
|
|
39
28
|
if (isContent(descriptor))
|
|
40
29
|
descriptor = { content: descriptor };
|
|
41
|
-
|
|
42
|
-
|
|
30
|
+
const ent = getWrappedElement(document.createElement(tag));
|
|
31
|
+
const applyClasses = (classes) => {
|
|
43
32
|
if (Array.isArray(classes)) {
|
|
44
|
-
return classes.forEach(
|
|
33
|
+
return classes.forEach(c => applyClasses(c));
|
|
45
34
|
}
|
|
46
35
|
if (typeof classes == "string") {
|
|
47
|
-
classes.trim().split(/\s+/).forEach(
|
|
36
|
+
classes.trim().split(/\s+/).forEach(c => ent.classes.add(c));
|
|
48
37
|
return;
|
|
49
38
|
}
|
|
50
|
-
Object.entries(classes).forEach(
|
|
51
|
-
var className = _a[0], state = _a[1];
|
|
39
|
+
Object.entries(classes).forEach(([className, state]) => {
|
|
52
40
|
if (state)
|
|
53
41
|
applyClasses(className);
|
|
54
42
|
});
|
|
55
43
|
};
|
|
56
44
|
applyClasses(descriptor.classes || []);
|
|
57
45
|
if (descriptor.attribs) {
|
|
58
|
-
Object.entries(descriptor.attribs).forEach(
|
|
59
|
-
var k = _a[0], v = _a[1];
|
|
46
|
+
Object.entries(descriptor.attribs).forEach(([k, v]) => {
|
|
60
47
|
if (v === false) {
|
|
61
48
|
return;
|
|
62
49
|
}
|
|
@@ -72,38 +59,27 @@ function createElement(tag, descriptor) {
|
|
|
72
59
|
ent.setCSSVariable(descriptor.cssVariables);
|
|
73
60
|
}
|
|
74
61
|
if (descriptor.on) {
|
|
75
|
-
Object.entries(descriptor.on).forEach(
|
|
76
|
-
var eventName = _a[0], handler = _a[1];
|
|
77
|
-
return ent.on(eventName, handler);
|
|
78
|
-
});
|
|
62
|
+
Object.entries(descriptor.on).forEach(([eventName, handler]) => ent.on(eventName, handler));
|
|
79
63
|
}
|
|
80
64
|
return ent;
|
|
81
65
|
}
|
|
82
66
|
;
|
|
83
|
-
var isContent = function (value) {
|
|
84
|
-
return ["string", "number"].includes(typeof value)
|
|
85
|
-
|| value instanceof Element
|
|
86
|
-
|| value instanceof Text
|
|
87
|
-
|| Array.isArray(value)
|
|
88
|
-
|| !value;
|
|
89
|
-
};
|
|
90
67
|
exports.$ = new Proxy(createElement, {
|
|
91
|
-
apply
|
|
92
|
-
var
|
|
93
|
-
var selectorOrTagName = _a[0], contentOrDescriptor = _a[1];
|
|
68
|
+
apply(create, _, [selectorOrTagName, contentOrDescriptor]) {
|
|
69
|
+
var _a;
|
|
94
70
|
if (selectorOrTagName instanceof HTMLElement)
|
|
95
71
|
return getWrappedElement(selectorOrTagName);
|
|
96
|
-
|
|
72
|
+
const tagName = ((_a = selectorOrTagName.match(/^[^.#]*/)) === null || _a === void 0 ? void 0 : _a[0]) || "";
|
|
97
73
|
if (!tagName)
|
|
98
74
|
throw new Error("Invalid tag");
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
classes
|
|
75
|
+
const matches = selectorOrTagName.slice(tagName.length).match(/[.#][^.#]+/g);
|
|
76
|
+
const classes = {};
|
|
77
|
+
const descriptor = {
|
|
78
|
+
classes,
|
|
103
79
|
content: contentOrDescriptor,
|
|
104
80
|
};
|
|
105
|
-
matches === null || matches === void 0 ? void 0 : matches.forEach(
|
|
106
|
-
|
|
81
|
+
matches === null || matches === void 0 ? void 0 : matches.forEach((m) => {
|
|
82
|
+
const value = m.slice(1);
|
|
107
83
|
if (m[0] == ".") {
|
|
108
84
|
classes[value] = true;
|
|
109
85
|
}
|
|
@@ -113,38 +89,38 @@ exports.$ = new Proxy(createElement, {
|
|
|
113
89
|
});
|
|
114
90
|
return create(tagName, descriptor);
|
|
115
91
|
},
|
|
116
|
-
get
|
|
117
|
-
return
|
|
92
|
+
get(create, tagName) {
|
|
93
|
+
return (descriptorOrContent) => {
|
|
118
94
|
return create(tagName, descriptorOrContent);
|
|
119
95
|
};
|
|
120
96
|
}
|
|
121
97
|
});
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
get:
|
|
98
|
+
const entityDataSymbol = Symbol("jelComponentData");
|
|
99
|
+
const elementWrapCache = new WeakMap();
|
|
100
|
+
const attribsProxy = {
|
|
101
|
+
get: (element, key) => {
|
|
126
102
|
return element.getAttribute(key);
|
|
127
103
|
},
|
|
128
|
-
set:
|
|
104
|
+
set: (element, key, value) => {
|
|
129
105
|
element.setAttribute(key, value);
|
|
130
106
|
return true;
|
|
131
107
|
},
|
|
132
|
-
has:
|
|
108
|
+
has: (element, key) => {
|
|
133
109
|
return element.hasAttribute(key);
|
|
134
110
|
},
|
|
135
|
-
ownKeys:
|
|
111
|
+
ownKeys: (element) => {
|
|
136
112
|
return element.getAttributeNames();
|
|
137
113
|
},
|
|
138
114
|
};
|
|
139
|
-
|
|
115
|
+
const recursiveAppend = (parent, c) => {
|
|
140
116
|
if (c === null)
|
|
141
117
|
return;
|
|
142
118
|
if (Array.isArray(c)) {
|
|
143
|
-
c.forEach(
|
|
119
|
+
c.forEach(item => recursiveAppend(parent, item));
|
|
144
120
|
return;
|
|
145
121
|
}
|
|
146
122
|
if (isJelEntity(c)) {
|
|
147
|
-
recursiveAppend(parent, c[
|
|
123
|
+
recursiveAppend(parent, c[entityDataSymbol].dom);
|
|
148
124
|
return;
|
|
149
125
|
}
|
|
150
126
|
if (typeof c == "number")
|
|
@@ -152,170 +128,164 @@ var recursiveAppend = function (parent, c) {
|
|
|
152
128
|
parent.append(c);
|
|
153
129
|
};
|
|
154
130
|
function getWrappedElement(element) {
|
|
155
|
-
var _a;
|
|
156
131
|
if (!elementWrapCache.has(element)) {
|
|
157
|
-
|
|
158
|
-
|
|
132
|
+
const setCSSVariable = (k, v) => {
|
|
133
|
+
if (v === null) {
|
|
134
|
+
element.style.removeProperty("--" + k);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
element.style.setProperty("--" + k, v);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
const domEntity = {
|
|
141
|
+
[entityDataSymbol]: {
|
|
159
142
|
dom: element,
|
|
160
143
|
},
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}),
|
|
166
|
-
_a.on = function (eventId, handler) {
|
|
167
|
-
element.addEventListener(eventId, function (eventData) {
|
|
168
|
-
handler.call(domEntity_1, eventData);
|
|
144
|
+
get element() { return element; },
|
|
145
|
+
on(eventId, handler) {
|
|
146
|
+
element.addEventListener(eventId, eventData => {
|
|
147
|
+
handler.call(domEntity, eventData);
|
|
169
148
|
});
|
|
170
149
|
},
|
|
171
|
-
|
|
172
|
-
var content = [];
|
|
173
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
174
|
-
content[_i] = arguments[_i];
|
|
175
|
-
}
|
|
150
|
+
append(...content) {
|
|
176
151
|
recursiveAppend(element, content);
|
|
177
152
|
},
|
|
178
|
-
|
|
153
|
+
remove() {
|
|
179
154
|
element.remove();
|
|
180
155
|
},
|
|
181
|
-
|
|
156
|
+
setCSSVariable(variableNameOrTable, value) {
|
|
182
157
|
if (typeof variableNameOrTable == "object") {
|
|
183
|
-
Object.entries(variableNameOrTable).forEach(
|
|
184
|
-
var k = _a[0], v = _a[1];
|
|
185
|
-
element.style.setProperty("--" + k, v);
|
|
186
|
-
});
|
|
158
|
+
Object.entries(variableNameOrTable).forEach(([k, v]) => setCSSVariable(k, v));
|
|
187
159
|
return;
|
|
188
160
|
}
|
|
189
|
-
|
|
161
|
+
setCSSVariable(variableNameOrTable, value);
|
|
190
162
|
},
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
element.querySelectorAll(selector).forEach(
|
|
163
|
+
classes: element.classList,
|
|
164
|
+
qsa(selector) {
|
|
165
|
+
const results = [];
|
|
166
|
+
element.querySelectorAll(selector).forEach((el) => results.push(el instanceof HTMLElement ? getWrappedElement(el) : el));
|
|
195
167
|
return results;
|
|
196
168
|
},
|
|
197
|
-
|
|
169
|
+
getRect() {
|
|
198
170
|
return element.getBoundingClientRect();
|
|
199
171
|
},
|
|
200
|
-
|
|
172
|
+
focus() {
|
|
201
173
|
element.focus();
|
|
202
174
|
},
|
|
203
|
-
|
|
175
|
+
blur() {
|
|
204
176
|
element.blur();
|
|
205
177
|
},
|
|
206
|
-
|
|
178
|
+
select() {
|
|
207
179
|
element.select();
|
|
208
180
|
},
|
|
209
|
-
|
|
181
|
+
getContext(mode, options) {
|
|
210
182
|
return element.getContext(mode, options);
|
|
211
183
|
},
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
},
|
|
252
|
-
set: function (v) {
|
|
253
|
-
element.width = v;
|
|
254
|
-
},
|
|
255
|
-
enumerable: false,
|
|
256
|
-
configurable: true
|
|
257
|
-
}),
|
|
258
|
-
Object.defineProperty(_a, "height", {
|
|
259
|
-
get: function () {
|
|
260
|
-
return element.width;
|
|
261
|
-
},
|
|
262
|
-
set: function (v) {
|
|
263
|
-
element.height = v;
|
|
264
|
-
},
|
|
265
|
-
enumerable: false,
|
|
266
|
-
configurable: true
|
|
267
|
-
}),
|
|
268
|
-
_a.style = new Proxy(function () { return element.style; }, styleProxy),
|
|
269
|
-
_a);
|
|
270
|
-
elementWrapCache.set(element, domEntity_1);
|
|
184
|
+
get content() {
|
|
185
|
+
return [].slice.call(element.children).map((child) => {
|
|
186
|
+
if (child instanceof HTMLElement)
|
|
187
|
+
return getWrappedElement(child);
|
|
188
|
+
return child;
|
|
189
|
+
});
|
|
190
|
+
},
|
|
191
|
+
set content(v) {
|
|
192
|
+
element.innerHTML = "";
|
|
193
|
+
recursiveAppend(element, v);
|
|
194
|
+
},
|
|
195
|
+
attribs: new Proxy(element, attribsProxy),
|
|
196
|
+
get innerHTML() {
|
|
197
|
+
return element.innerHTML;
|
|
198
|
+
},
|
|
199
|
+
set innerHTML(v) {
|
|
200
|
+
element.innerHTML = v;
|
|
201
|
+
},
|
|
202
|
+
get value() {
|
|
203
|
+
return element.value;
|
|
204
|
+
},
|
|
205
|
+
set value(v) {
|
|
206
|
+
element.value = v;
|
|
207
|
+
},
|
|
208
|
+
get width() {
|
|
209
|
+
return element.width;
|
|
210
|
+
},
|
|
211
|
+
set width(v) {
|
|
212
|
+
element.width = v;
|
|
213
|
+
},
|
|
214
|
+
get height() {
|
|
215
|
+
return element.width;
|
|
216
|
+
},
|
|
217
|
+
set height(v) {
|
|
218
|
+
element.height = v;
|
|
219
|
+
},
|
|
220
|
+
style: new Proxy(() => element.style, styleProxy),
|
|
221
|
+
};
|
|
222
|
+
elementWrapCache.set(element, domEntity);
|
|
271
223
|
}
|
|
272
224
|
return elementWrapCache.get(element);
|
|
273
225
|
}
|
|
226
|
+
const isContent = (value) => {
|
|
227
|
+
if (value === undefined)
|
|
228
|
+
return false;
|
|
229
|
+
return typeof value == "string"
|
|
230
|
+
|| typeof value == "number"
|
|
231
|
+
|| !value
|
|
232
|
+
|| value instanceof Element
|
|
233
|
+
|| value instanceof Text
|
|
234
|
+
|| entityDataSymbol in value
|
|
235
|
+
|| Array.isArray(value);
|
|
236
|
+
};
|
|
274
237
|
function isJelEntity(content) {
|
|
275
|
-
return typeof content == "object" && !!content &&
|
|
238
|
+
return typeof content == "object" && !!content && entityDataSymbol in content;
|
|
276
239
|
}
|
|
240
|
+
function createEntity(content, api) {
|
|
241
|
+
if (isContent(api)) {
|
|
242
|
+
throw new TypeError("API object is already valid content");
|
|
243
|
+
}
|
|
244
|
+
return Object.create(api !== null && api !== void 0 ? api : {}, {
|
|
245
|
+
[entityDataSymbol]: {
|
|
246
|
+
value: {
|
|
247
|
+
dom: content
|
|
248
|
+
}
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
;
|
|
277
253
|
function definePart(defaultOptions, init) {
|
|
278
|
-
return (
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
var addEventListener = function (eventId, fn) {
|
|
254
|
+
return ((spec) => {
|
|
255
|
+
const fullSpec = Object.assign(Object.assign({}, defaultOptions), spec);
|
|
256
|
+
const eventHandlers = {};
|
|
257
|
+
const addEventListener = (eventId, fn) => {
|
|
283
258
|
if (!eventHandlers[eventId])
|
|
284
259
|
eventHandlers[eventId] = [];
|
|
285
260
|
eventHandlers[eventId].push(fn);
|
|
286
261
|
};
|
|
287
262
|
if (fullSpec.on)
|
|
288
|
-
Object.entries(fullSpec.on).forEach(
|
|
289
|
-
var eventId = _a[0], handler = _a[1];
|
|
263
|
+
Object.entries(fullSpec.on).forEach(([eventId, handler]) => {
|
|
290
264
|
addEventListener(eventId, handler);
|
|
291
265
|
});
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
266
|
+
let entity;
|
|
267
|
+
const content = [];
|
|
268
|
+
const append = (c) => {
|
|
295
269
|
if (entity)
|
|
296
270
|
throw new Error("Component root content can only be added during initialisation");
|
|
297
271
|
content.push(c);
|
|
298
272
|
};
|
|
299
|
-
|
|
273
|
+
const trigger = (eventId, data) => {
|
|
300
274
|
var _a;
|
|
301
|
-
(_a = eventHandlers[eventId]) === null || _a === void 0 ? void 0 : _a.forEach(
|
|
275
|
+
(_a = eventHandlers[eventId]) === null || _a === void 0 ? void 0 : _a.forEach(fn => fn.call(entity, data));
|
|
302
276
|
};
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
277
|
+
const api = init(fullSpec, append, trigger);
|
|
278
|
+
Object.defineProperties(api, {
|
|
279
|
+
[entityDataSymbol]: {
|
|
306
280
|
value: {
|
|
307
281
|
dom: content
|
|
308
282
|
}
|
|
309
283
|
},
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
dom: content,
|
|
316
|
-
},
|
|
317
|
-
_b.on = addEventListener,
|
|
318
|
-
_b);
|
|
284
|
+
on: {
|
|
285
|
+
get: () => addEventListener
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
entity = api;
|
|
319
289
|
return entity;
|
|
320
290
|
});
|
|
321
291
|
}
|