@xtia/jel 0.1.3 → 0.2.1
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 +14 -6
- package/index.js +86 -89
- package/package.json +2 -2
package/index.d.ts
CHANGED
|
@@ -11,6 +11,9 @@ type JelEntity<API, EventDataMap> = API & {
|
|
|
11
11
|
on<E extends keyof EventDataMap>(eventId: E, handler: (this: JelEntity<API, EventDataMap>, data: EventDataMap[E]) => void): void;
|
|
12
12
|
readonly [componentDataSymbol]: JelComponentData;
|
|
13
13
|
};
|
|
14
|
+
type Styles = Partial<{
|
|
15
|
+
[key in keyof CSSStyleDeclaration]: string | number;
|
|
16
|
+
}> & Record<`--${string}`, string | number>;
|
|
14
17
|
interface ElementDescriptor {
|
|
15
18
|
classes?: ElementClassSpec;
|
|
16
19
|
content?: DOMContent;
|
|
@@ -18,9 +21,7 @@ interface ElementDescriptor {
|
|
|
18
21
|
on?: Partial<{
|
|
19
22
|
[E in keyof HTMLElementEventMap]: (event: HTMLElementEventMap[E]) => void;
|
|
20
23
|
}>;
|
|
21
|
-
style?:
|
|
22
|
-
[key in keyof CSSStyleDeclaration]: string | number;
|
|
23
|
-
}> & Record<string, string | number>;
|
|
24
|
+
style?: Styles;
|
|
24
25
|
}
|
|
25
26
|
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>) & {
|
|
26
27
|
[T in keyof HTMLElementTagNameMap]: (descriptor: ElementDescriptor) => DomEntity<HTMLElementTagNameMap[T]>;
|
|
@@ -30,8 +31,6 @@ type DomHelper = ((<T extends keyof HTMLElementTagNameMap>(tagName: T, descripto
|
|
|
30
31
|
type JelComponentData = {
|
|
31
32
|
dom: DOMContent;
|
|
32
33
|
};
|
|
33
|
-
export declare const $: DomHelper;
|
|
34
|
-
declare const componentDataSymbol: unique symbol;
|
|
35
34
|
type ElementAPI<T extends HTMLElement> = {
|
|
36
35
|
readonly element: T;
|
|
37
36
|
content: DOMContent;
|
|
@@ -45,5 +44,14 @@ type ElementAPI<T extends HTMLElement> = {
|
|
|
45
44
|
append(...content: DOMContent[]): void;
|
|
46
45
|
remove(): void;
|
|
47
46
|
};
|
|
48
|
-
|
|
47
|
+
type OptionalKeys<T> = {
|
|
48
|
+
[K in keyof T]-?: {} extends Pick<T, K> ? K : never;
|
|
49
|
+
}[keyof T];
|
|
50
|
+
type Optionals<T> = {
|
|
51
|
+
[K in OptionalKeys<T>]-?: Exclude<T[K], undefined>;
|
|
52
|
+
};
|
|
53
|
+
export declare const $: DomHelper;
|
|
54
|
+
declare const componentDataSymbol: unique symbol;
|
|
55
|
+
type Reserve<T> = Record<string, any> & Partial<Record<keyof T, never>>;
|
|
56
|
+
export declare function definePart<Spec extends Reserve<CommonOptions<any>>, API extends Reserve<JelEntity<void, any>> | 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): JelConstructor<Spec, API, EventDataMap>;
|
|
49
57
|
export {};
|
package/index.js
CHANGED
|
@@ -19,8 +19,9 @@ function createElement(tag, descriptor) {
|
|
|
19
19
|
descriptor = { content: descriptor };
|
|
20
20
|
var ent = getWrappedElement(document.createElement(tag));
|
|
21
21
|
var applyClasses = function (classes) {
|
|
22
|
-
if (Array.isArray(classes))
|
|
22
|
+
if (Array.isArray(classes)) {
|
|
23
23
|
return classes.forEach(function (c) { return applyClasses(c); });
|
|
24
|
+
}
|
|
24
25
|
if (typeof classes == "string") {
|
|
25
26
|
classes.trim().split(/\s+/).forEach(function (c) { return ent.classes.add(c); });
|
|
26
27
|
return;
|
|
@@ -32,7 +33,7 @@ function createElement(tag, descriptor) {
|
|
|
32
33
|
});
|
|
33
34
|
};
|
|
34
35
|
applyClasses(descriptor.classes || []);
|
|
35
|
-
if (descriptor.attribs)
|
|
36
|
+
if (descriptor.attribs) {
|
|
36
37
|
Object.entries(descriptor.attribs).forEach(function (_a) {
|
|
37
38
|
var k = _a[0], v = _a[1];
|
|
38
39
|
if (v === false) {
|
|
@@ -40,27 +41,26 @@ function createElement(tag, descriptor) {
|
|
|
40
41
|
}
|
|
41
42
|
ent.element.setAttribute(k, v === true ? k : v);
|
|
42
43
|
});
|
|
43
|
-
|
|
44
|
-
if (Array.isArray(content))
|
|
45
|
-
return content.forEach(function (c) { return addContent(c); });
|
|
46
|
-
if (content)
|
|
47
|
-
ent.append(content);
|
|
48
|
-
};
|
|
44
|
+
}
|
|
49
45
|
if (descriptor.content)
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
46
|
+
recursiveAppend(ent.element, descriptor.content);
|
|
47
|
+
if (descriptor.style) {
|
|
48
|
+
Object.entries(descriptor.style).forEach(function (_a) {
|
|
49
|
+
var prop = _a[0], val = _a[1];
|
|
50
|
+
if (/\-/.test(prop)) {
|
|
51
|
+
ent.element.style.setProperty(prop, val.toString());
|
|
52
|
+
}
|
|
53
|
+
else {
|
|
54
|
+
ent.element.style[prop] = val;
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
if (descriptor.on) {
|
|
59
|
+
Object.entries(descriptor.on).forEach(function (_a) {
|
|
60
|
+
var eventName = _a[0], handler = _a[1];
|
|
61
|
+
return ent.on(eventName, handler);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
64
|
return ent;
|
|
65
65
|
}
|
|
66
66
|
;
|
|
@@ -72,7 +72,7 @@ var isContent = function (value) {
|
|
|
72
72
|
|| !value;
|
|
73
73
|
};
|
|
74
74
|
exports.$ = new Proxy(createElement, {
|
|
75
|
-
apply: function (create,
|
|
75
|
+
apply: function (create, _, _a) {
|
|
76
76
|
var _b;
|
|
77
77
|
var selectorOrTagName = _a[0], contentOrDescriptor = _a[1];
|
|
78
78
|
if (selectorOrTagName instanceof HTMLElement)
|
|
@@ -105,53 +105,6 @@ exports.$ = new Proxy(createElement, {
|
|
|
105
105
|
});
|
|
106
106
|
var componentDataSymbol = Symbol("jelComponentData");
|
|
107
107
|
var elementWrapCache = new WeakMap();
|
|
108
|
-
function definePart(defaultOptions, init) {
|
|
109
|
-
return (function (partialSpec) {
|
|
110
|
-
var _a, _b;
|
|
111
|
-
if (partialSpec === void 0) { partialSpec = {}; }
|
|
112
|
-
var spec = __assign(__assign({}, defaultOptions), partialSpec);
|
|
113
|
-
var eventHandlers = {};
|
|
114
|
-
var addEventListener = function (eventId, fn) {
|
|
115
|
-
if (!eventHandlers[eventId])
|
|
116
|
-
eventHandlers[eventId] = [];
|
|
117
|
-
eventHandlers[eventId].push(fn);
|
|
118
|
-
};
|
|
119
|
-
if (spec.on)
|
|
120
|
-
Object.entries(spec.on).forEach(function (_a) {
|
|
121
|
-
var eventId = _a[0], handler = _a[1];
|
|
122
|
-
addEventListener(eventId, handler);
|
|
123
|
-
});
|
|
124
|
-
var entity;
|
|
125
|
-
var content = [];
|
|
126
|
-
var append = function (c) {
|
|
127
|
-
if (entity)
|
|
128
|
-
throw new Error("Component root content can only be added during initialisation");
|
|
129
|
-
content.push(c);
|
|
130
|
-
};
|
|
131
|
-
var trigger = function (eventId, data) {
|
|
132
|
-
var _a;
|
|
133
|
-
(_a = eventHandlers[eventId]) === null || _a === void 0 ? void 0 : _a.forEach(function (fn) { return fn.call(entity, data); });
|
|
134
|
-
};
|
|
135
|
-
var api = init(spec, append, trigger);
|
|
136
|
-
entity = api ? Object.create(api, (_a = {},
|
|
137
|
-
_a[componentDataSymbol] = {
|
|
138
|
-
value: {
|
|
139
|
-
dom: content
|
|
140
|
-
}
|
|
141
|
-
},
|
|
142
|
-
_a.on = {
|
|
143
|
-
value: addEventListener,
|
|
144
|
-
},
|
|
145
|
-
_a)) : (_b = {},
|
|
146
|
-
_b[componentDataSymbol] = {
|
|
147
|
-
dom: content,
|
|
148
|
-
},
|
|
149
|
-
_b.on = addEventListener,
|
|
150
|
-
_b);
|
|
151
|
-
return entity;
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
;
|
|
155
108
|
var attribsProxy = {
|
|
156
109
|
get: function (element, key) {
|
|
157
110
|
return element.getAttribute(key);
|
|
@@ -161,26 +114,24 @@ var attribsProxy = {
|
|
|
161
114
|
return true;
|
|
162
115
|
}
|
|
163
116
|
};
|
|
117
|
+
var recursiveAppend = function (parent, c) {
|
|
118
|
+
if (c === null)
|
|
119
|
+
return;
|
|
120
|
+
if (Array.isArray(c)) {
|
|
121
|
+
c.forEach(function (item) { return recursiveAppend(parent, item); });
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
if (isJelEntity(c)) {
|
|
125
|
+
recursiveAppend(parent, c[componentDataSymbol].dom);
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (typeof c == "number")
|
|
129
|
+
c = c.toString();
|
|
130
|
+
parent.append(c);
|
|
131
|
+
};
|
|
164
132
|
function getWrappedElement(element) {
|
|
165
133
|
var _a;
|
|
166
134
|
if (!elementWrapCache.has(element)) {
|
|
167
|
-
var recursiveAppend_1 = function (c) {
|
|
168
|
-
if (c === undefined)
|
|
169
|
-
debugger;
|
|
170
|
-
if (c === null)
|
|
171
|
-
return;
|
|
172
|
-
if (Array.isArray(c)) {
|
|
173
|
-
c.forEach(function (item) { return recursiveAppend_1(item); });
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
if (isJelEntity(c)) {
|
|
177
|
-
recursiveAppend_1(c[componentDataSymbol].dom);
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
if (typeof c == "number")
|
|
181
|
-
c = c.toString();
|
|
182
|
-
element.append(c);
|
|
183
|
-
};
|
|
184
135
|
var domEntity_1 = (_a = {},
|
|
185
136
|
_a[componentDataSymbol] = {
|
|
186
137
|
dom: element,
|
|
@@ -200,7 +151,7 @@ function getWrappedElement(element) {
|
|
|
200
151
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
201
152
|
content[_i] = arguments[_i];
|
|
202
153
|
}
|
|
203
|
-
|
|
154
|
+
recursiveAppend(element, content);
|
|
204
155
|
},
|
|
205
156
|
_a.remove = function () {
|
|
206
157
|
element.remove();
|
|
@@ -219,7 +170,7 @@ function getWrappedElement(element) {
|
|
|
219
170
|
},
|
|
220
171
|
set: function (v) {
|
|
221
172
|
element.innerHTML = "";
|
|
222
|
-
|
|
173
|
+
recursiveAppend(element, v);
|
|
223
174
|
},
|
|
224
175
|
enumerable: false,
|
|
225
176
|
configurable: true
|
|
@@ -244,3 +195,49 @@ function getWrappedElement(element) {
|
|
|
244
195
|
function isJelEntity(content) {
|
|
245
196
|
return typeof content == "object" && !!content && componentDataSymbol in content;
|
|
246
197
|
}
|
|
198
|
+
function definePart(defaultOptions, init) {
|
|
199
|
+
return (function (spec) {
|
|
200
|
+
var _a, _b;
|
|
201
|
+
var fullSpec = __assign(__assign({}, defaultOptions), spec);
|
|
202
|
+
var eventHandlers = {};
|
|
203
|
+
var addEventListener = function (eventId, fn) {
|
|
204
|
+
if (!eventHandlers[eventId])
|
|
205
|
+
eventHandlers[eventId] = [];
|
|
206
|
+
eventHandlers[eventId].push(fn);
|
|
207
|
+
};
|
|
208
|
+
if (fullSpec.on)
|
|
209
|
+
Object.entries(fullSpec.on).forEach(function (_a) {
|
|
210
|
+
var eventId = _a[0], handler = _a[1];
|
|
211
|
+
addEventListener(eventId, handler);
|
|
212
|
+
});
|
|
213
|
+
var entity;
|
|
214
|
+
var content = [];
|
|
215
|
+
var append = function (c) {
|
|
216
|
+
if (entity)
|
|
217
|
+
throw new Error("Component root content can only be added during initialisation");
|
|
218
|
+
content.push(c);
|
|
219
|
+
};
|
|
220
|
+
var trigger = function (eventId, data) {
|
|
221
|
+
var _a;
|
|
222
|
+
(_a = eventHandlers[eventId]) === null || _a === void 0 ? void 0 : _a.forEach(function (fn) { return fn.call(entity, data); });
|
|
223
|
+
};
|
|
224
|
+
var api = init(fullSpec, append, trigger);
|
|
225
|
+
entity = api ? Object.create(api, (_a = {},
|
|
226
|
+
_a[componentDataSymbol] = {
|
|
227
|
+
value: {
|
|
228
|
+
dom: content
|
|
229
|
+
}
|
|
230
|
+
},
|
|
231
|
+
_a.on = {
|
|
232
|
+
value: addEventListener,
|
|
233
|
+
},
|
|
234
|
+
_a)) : (_b = {},
|
|
235
|
+
_b[componentDataSymbol] = {
|
|
236
|
+
dom: content,
|
|
237
|
+
},
|
|
238
|
+
_b.on = addEventListener,
|
|
239
|
+
_b);
|
|
240
|
+
return entity;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xtia/jel",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"repository": {
|
|
5
5
|
"url": "https://github.com/tiadrop/jel-ts",
|
|
6
6
|
"type": "github"
|
|
7
7
|
},
|
|
8
|
-
"description": "Lightweight DOM
|
|
8
|
+
"description": "Lightweight DOM manipulation and componentisation",
|
|
9
9
|
"main": "index.js",
|
|
10
10
|
"keywords": [
|
|
11
11
|
"dom"
|