@ecopages/radiant 0.1.5 → 0.1.6
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/dist/context/context-provider.js +282 -3
- package/dist/context/context-provider.js.map +5 -3
- package/dist/context/create-context.js +6 -3
- package/dist/context/create-context.js.map +2 -2
- package/dist/context/decorators/consume-context.js +64 -3
- package/dist/context/decorators/consume-context.js.map +4 -3
- package/dist/context/decorators/context-selector.js +71 -3
- package/dist/context/decorators/context-selector.js.map +5 -4
- package/dist/context/decorators/provide-context.js +293 -3
- package/dist/context/decorators/provide-context.js.map +6 -3
- package/dist/context/events.js +53 -3
- package/dist/context/events.js.map +2 -2
- package/dist/context/index.js +340 -2
- package/dist/context/index.js.map +10 -3
- package/dist/core/index.js +82 -2
- package/dist/core/index.js.map +4 -3
- package/dist/core/radiant-element.d.ts +21 -1
- package/dist/core/radiant-element.js +82 -3
- package/dist/core/radiant-element.js.map +3 -3
- package/dist/decorators/custom-element.js +14 -3
- package/dist/decorators/custom-element.js.map +2 -2
- package/dist/decorators/debounce.js +20 -2
- package/dist/decorators/debounce.js.map +2 -2
- package/dist/decorators/event.js +46 -3
- package/dist/decorators/event.js.map +4 -3
- package/dist/decorators/on-event.js +47 -3
- package/dist/decorators/on-event.js.map +3 -3
- package/dist/decorators/on-updated.d.ts +1 -1
- package/dist/decorators/on-updated.js +29 -3
- package/dist/decorators/on-updated.js.map +3 -3
- package/dist/decorators/query.js +36 -3
- package/dist/decorators/query.js.map +3 -3
- package/dist/decorators/reactive-field.js +26 -3
- package/dist/decorators/reactive-field.js.map +2 -2
- package/dist/decorators/reactive-prop.d.ts +1 -1
- package/dist/decorators/reactive-prop.js +214 -3
- package/dist/decorators/reactive-prop.js.map +5 -4
- package/dist/decorators.js +400 -2
- package/dist/decorators.js.map +12 -3
- package/dist/index.js +719 -2
- package/dist/index.js.map +21 -3
- package/dist/mixins/index.js +23 -2
- package/dist/mixins/index.js.map +4 -3
- package/dist/mixins/with-kita.js +23 -3
- package/dist/mixins/with-kita.js.map +2 -2
- package/dist/tools/event-emitter.js +22 -3
- package/dist/tools/event-emitter.js.map +2 -2
- package/dist/tools/index.js +28 -2
- package/dist/tools/index.js.map +5 -3
- package/dist/tools/stringify-attribute.js +8 -3
- package/dist/tools/stringify-attribute.js.map +2 -2
- package/dist/utils/attribute-utils.d.ts +2 -0
- package/dist/utils/attribute-utils.js +137 -3
- package/dist/utils/attribute-utils.js.map +3 -3
- package/dist/utils/index.js +137 -2
- package/dist/utils/index.js.map +4 -3
- package/package.json +5 -4
package/dist/index.js
CHANGED
|
@@ -1,3 +1,720 @@
|
|
|
1
|
-
|
|
1
|
+
// src/decorators/custom-element.ts
|
|
2
|
+
function customElement(name) {
|
|
3
|
+
return (target) => {
|
|
4
|
+
if (!globalThis.window)
|
|
5
|
+
return;
|
|
6
|
+
if (!window.customElements.get(name)) {
|
|
7
|
+
window.customElements.define(name, target);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
}
|
|
2
11
|
|
|
3
|
-
|
|
12
|
+
// src/tools/event-emitter.ts
|
|
13
|
+
class EventEmitter {
|
|
14
|
+
host;
|
|
15
|
+
eventConfig;
|
|
16
|
+
constructor(host, eventConfig) {
|
|
17
|
+
this.host = host;
|
|
18
|
+
this.eventConfig = eventConfig;
|
|
19
|
+
}
|
|
20
|
+
emit(detail) {
|
|
21
|
+
const event = new CustomEvent(this.eventConfig.name, {
|
|
22
|
+
detail,
|
|
23
|
+
bubbles: this.eventConfig.bubbles,
|
|
24
|
+
cancelable: this.eventConfig.cancelable,
|
|
25
|
+
composed: this.eventConfig.composed
|
|
26
|
+
});
|
|
27
|
+
this.host.dispatchEvent(event);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/decorators/event.ts
|
|
32
|
+
function event(eventConfig) {
|
|
33
|
+
return (target, propertyKey) => {
|
|
34
|
+
if (!propertyKey) {
|
|
35
|
+
throw new Error("The propertyKey is missing for the event decorator.");
|
|
36
|
+
}
|
|
37
|
+
if (!eventConfig || !eventConfig.name) {
|
|
38
|
+
throw new Error("Invalid eventConfig provided.");
|
|
39
|
+
}
|
|
40
|
+
const uniqueKey = Symbol(eventConfig.name);
|
|
41
|
+
Object.defineProperty(target, propertyKey, {
|
|
42
|
+
get() {
|
|
43
|
+
const emittersMap = eventEmitters.get(this) || new Map;
|
|
44
|
+
if (!emittersMap.has(uniqueKey)) {
|
|
45
|
+
emittersMap.set(uniqueKey, new EventEmitter(this, eventConfig));
|
|
46
|
+
eventEmitters.set(this, emittersMap);
|
|
47
|
+
}
|
|
48
|
+
return emittersMap.get(uniqueKey);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
var eventEmitters = new WeakMap;
|
|
54
|
+
|
|
55
|
+
// src/decorators/on-event.ts
|
|
56
|
+
function onEvent(eventConfig) {
|
|
57
|
+
return (proto, _, descriptor) => {
|
|
58
|
+
const originalConnectedCallback = proto.connectedCallback;
|
|
59
|
+
const originalDisconnectedCallback = proto.disconnectedCallback;
|
|
60
|
+
if ("window" in eventConfig) {
|
|
61
|
+
proto.connectedCallback = function() {
|
|
62
|
+
window.addEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);
|
|
63
|
+
originalConnectedCallback.call(this);
|
|
64
|
+
};
|
|
65
|
+
proto.disconnectedCallback = function() {
|
|
66
|
+
window.removeEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);
|
|
67
|
+
originalDisconnectedCallback.call(this);
|
|
68
|
+
};
|
|
69
|
+
return descriptor;
|
|
70
|
+
}
|
|
71
|
+
if ("document" in eventConfig) {
|
|
72
|
+
proto.connectedCallback = function() {
|
|
73
|
+
document.addEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);
|
|
74
|
+
originalConnectedCallback.call(this);
|
|
75
|
+
};
|
|
76
|
+
proto.disconnectedCallback = function() {
|
|
77
|
+
document.removeEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);
|
|
78
|
+
originalDisconnectedCallback.call(this);
|
|
79
|
+
};
|
|
80
|
+
return descriptor;
|
|
81
|
+
}
|
|
82
|
+
const selector = "selector" in eventConfig ? eventConfig.selector : `[data-ref="${eventConfig.ref}"]`;
|
|
83
|
+
const originalMethod = descriptor.value;
|
|
84
|
+
const subscriptionId = `${eventConfig.type}-${selector}`;
|
|
85
|
+
proto.connectedCallback = function() {
|
|
86
|
+
this.subscribeEvent({
|
|
87
|
+
id: subscriptionId,
|
|
88
|
+
selector,
|
|
89
|
+
type: eventConfig.type,
|
|
90
|
+
listener: originalMethod.bind(this),
|
|
91
|
+
options: eventConfig?.options ?? undefined
|
|
92
|
+
});
|
|
93
|
+
originalConnectedCallback.call(this);
|
|
94
|
+
};
|
|
95
|
+
return descriptor;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// src/decorators/on-updated.ts
|
|
100
|
+
function onUpdated(keyOrKeys) {
|
|
101
|
+
return (proto, methodName) => {
|
|
102
|
+
if (!("updatesRegistry" in proto)) {
|
|
103
|
+
Object.defineProperty(proto, "updatesRegistry", {
|
|
104
|
+
value: new Map,
|
|
105
|
+
configurable: true
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const updatesRegistry = proto.updatesRegistry;
|
|
109
|
+
if (Array.isArray(keyOrKeys)) {
|
|
110
|
+
for (const key of keyOrKeys) {
|
|
111
|
+
if (!updatesRegistry.has(key)) {
|
|
112
|
+
updatesRegistry.set(key, new Set);
|
|
113
|
+
}
|
|
114
|
+
updatesRegistry.get(key)?.add(methodName);
|
|
115
|
+
}
|
|
116
|
+
} else if (typeof keyOrKeys === "string") {
|
|
117
|
+
if (!updatesRegistry.has(keyOrKeys)) {
|
|
118
|
+
updatesRegistry.set(keyOrKeys, new Set);
|
|
119
|
+
}
|
|
120
|
+
updatesRegistry.get(keyOrKeys)?.add(methodName);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/decorators/query.ts
|
|
126
|
+
function query({
|
|
127
|
+
cache: shouldBeCached = true,
|
|
128
|
+
...options
|
|
129
|
+
}) {
|
|
130
|
+
const cache = new WeakMap;
|
|
131
|
+
return (proto, propertyKey) => {
|
|
132
|
+
const doQuery = function() {
|
|
133
|
+
if (shouldBeCached) {
|
|
134
|
+
const cachedResult = cache.get(this);
|
|
135
|
+
if (cachedResult !== undefined) {
|
|
136
|
+
return cachedResult;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
const selector = "selector" in options ? options.selector : `[data-ref="${options.ref}"]`;
|
|
140
|
+
const queryResult = options.all ? this.querySelectorAll(selector) : this.querySelector(selector);
|
|
141
|
+
if (shouldBeCached) {
|
|
142
|
+
cache.set(this, queryResult);
|
|
143
|
+
}
|
|
144
|
+
return queryResult;
|
|
145
|
+
};
|
|
146
|
+
const originalConnectedCallback = proto.connectedCallback;
|
|
147
|
+
proto.connectedCallback = function() {
|
|
148
|
+
Object.defineProperty(this, propertyKey, {
|
|
149
|
+
get: doQuery,
|
|
150
|
+
enumerable: true,
|
|
151
|
+
configurable: true
|
|
152
|
+
});
|
|
153
|
+
originalConnectedCallback.call(this);
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/utils/attribute-utils.ts
|
|
159
|
+
function parseAttributeTypeConstant(constant) {
|
|
160
|
+
switch (constant) {
|
|
161
|
+
case Array:
|
|
162
|
+
return "array";
|
|
163
|
+
case Boolean:
|
|
164
|
+
return "boolean";
|
|
165
|
+
case Number:
|
|
166
|
+
return "number";
|
|
167
|
+
case Object:
|
|
168
|
+
return "object";
|
|
169
|
+
case String:
|
|
170
|
+
return "string";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function parseAttributeTypeDefault(defaultValue) {
|
|
174
|
+
switch (typeof defaultValue) {
|
|
175
|
+
case "boolean":
|
|
176
|
+
return "boolean";
|
|
177
|
+
case "number":
|
|
178
|
+
return "number";
|
|
179
|
+
case "string":
|
|
180
|
+
return "string";
|
|
181
|
+
}
|
|
182
|
+
if (Array.isArray(defaultValue))
|
|
183
|
+
return "array";
|
|
184
|
+
if (Object.prototype.toString.call(defaultValue) === "[object Object]")
|
|
185
|
+
return "object";
|
|
186
|
+
}
|
|
187
|
+
function defaultValueForType(type) {
|
|
188
|
+
switch (type) {
|
|
189
|
+
case Number:
|
|
190
|
+
return 0;
|
|
191
|
+
case String:
|
|
192
|
+
return "";
|
|
193
|
+
case Boolean:
|
|
194
|
+
return false;
|
|
195
|
+
default:
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
function parseJSON(value) {
|
|
200
|
+
try {
|
|
201
|
+
return JSON.parse(value);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
throw new TypeError("Invalid JSON string");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function writeJSON(value) {
|
|
207
|
+
return JSON.stringify(value);
|
|
208
|
+
}
|
|
209
|
+
function writeString(value) {
|
|
210
|
+
return `${value}`;
|
|
211
|
+
}
|
|
212
|
+
function readAttributeValue(value, type) {
|
|
213
|
+
const readerType = parseAttributeTypeConstant(type);
|
|
214
|
+
if (!readerType)
|
|
215
|
+
throw new TypeError(`[radiant-element] Unknown type "${type}"`);
|
|
216
|
+
return readers[readerType](value);
|
|
217
|
+
}
|
|
218
|
+
function writeAttributeValue(value, type) {
|
|
219
|
+
const writerType = parseAttributeTypeConstant(type);
|
|
220
|
+
if (!writerType)
|
|
221
|
+
throw new TypeError(`[radiant-element] Unknown type "${type}"`);
|
|
222
|
+
return (writers[writerType] || writers.default)(value);
|
|
223
|
+
}
|
|
224
|
+
function isBoolean(value) {
|
|
225
|
+
return typeof value === "boolean";
|
|
226
|
+
}
|
|
227
|
+
function isNumber(value) {
|
|
228
|
+
return typeof value === "number";
|
|
229
|
+
}
|
|
230
|
+
function isString(value) {
|
|
231
|
+
return typeof value === "string";
|
|
232
|
+
}
|
|
233
|
+
function isArray(value) {
|
|
234
|
+
return Array.isArray(value);
|
|
235
|
+
}
|
|
236
|
+
function isObject(value) {
|
|
237
|
+
return typeof value === "object" && !Array.isArray(value) && value !== null;
|
|
238
|
+
}
|
|
239
|
+
function isValueOfType(type, defaultValue) {
|
|
240
|
+
switch (type) {
|
|
241
|
+
case Boolean:
|
|
242
|
+
return isBoolean(defaultValue);
|
|
243
|
+
case Number:
|
|
244
|
+
return isNumber(defaultValue);
|
|
245
|
+
case String:
|
|
246
|
+
return isString(defaultValue);
|
|
247
|
+
case Array:
|
|
248
|
+
return isArray(defaultValue);
|
|
249
|
+
case Object:
|
|
250
|
+
return isObject(defaultValue);
|
|
251
|
+
default:
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
var readers = {
|
|
256
|
+
array(value) {
|
|
257
|
+
const array = parseJSON(value);
|
|
258
|
+
if (!Array.isArray(array)) {
|
|
259
|
+
throw new TypeError(`Expected an array but got a value of type "${typeof array}"`);
|
|
260
|
+
}
|
|
261
|
+
return array;
|
|
262
|
+
},
|
|
263
|
+
boolean(value) {
|
|
264
|
+
return !(value === "0" || String(value).toLowerCase() === "false");
|
|
265
|
+
},
|
|
266
|
+
number(value) {
|
|
267
|
+
const number = Number(value.replace(/_/g, ""));
|
|
268
|
+
return number;
|
|
269
|
+
},
|
|
270
|
+
object(value) {
|
|
271
|
+
const object = JSON.parse(value);
|
|
272
|
+
if (object === null || typeof object !== "object" || Array.isArray(object)) {
|
|
273
|
+
throw new TypeError(`expected value of type "object" but instead got value "${value}" of type "${parseAttributeTypeDefault(object)}"`);
|
|
274
|
+
}
|
|
275
|
+
return object;
|
|
276
|
+
},
|
|
277
|
+
string(value) {
|
|
278
|
+
return value;
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
var writers = {
|
|
282
|
+
default: writeString,
|
|
283
|
+
array: writeJSON,
|
|
284
|
+
object: writeJSON
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
// src/decorators/reactive-prop.ts
|
|
288
|
+
function reactiveProp({ type, attribute, reflect, defaultValue }) {
|
|
289
|
+
if (defaultValue !== undefined && !isValueOfType(type, defaultValue)) {
|
|
290
|
+
throw new Error(`defaultValue does not match the expected type for ${type.name}`);
|
|
291
|
+
}
|
|
292
|
+
return (target, propertyName) => {
|
|
293
|
+
const originalValues = new WeakMap;
|
|
294
|
+
const attributeKey = attribute ?? propertyName;
|
|
295
|
+
if (propertyName in target) {
|
|
296
|
+
throw new Error(`Property "${propertyName}" already exists on ${target.constructor.name}`);
|
|
297
|
+
}
|
|
298
|
+
const propertyMapping = {
|
|
299
|
+
type,
|
|
300
|
+
propertyName,
|
|
301
|
+
attributeKey,
|
|
302
|
+
converter: {
|
|
303
|
+
fromAttribute: (value) => readAttributeValue(value, type),
|
|
304
|
+
toAttribute: (value) => writeAttributeValue(value, type)
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
addPropertyToMappings(target, propertyMapping);
|
|
308
|
+
Object.defineProperty(target, propertyName, {
|
|
309
|
+
get: function() {
|
|
310
|
+
if (!originalValues.has(this)) {
|
|
311
|
+
const initialValue = getInitialValue(this, type, attributeKey, defaultValue);
|
|
312
|
+
originalValues.set(this, initialValue);
|
|
313
|
+
}
|
|
314
|
+
return originalValues.get(this);
|
|
315
|
+
},
|
|
316
|
+
set: function(newValue) {
|
|
317
|
+
const oldValue = originalValues.get(this);
|
|
318
|
+
if (oldValue === newValue)
|
|
319
|
+
return;
|
|
320
|
+
originalValues.set(this, newValue);
|
|
321
|
+
if (reflect) {
|
|
322
|
+
const attributeValue = propertyMapping.converter.toAttribute(newValue);
|
|
323
|
+
this.setAttribute(attributeKey, attributeValue);
|
|
324
|
+
}
|
|
325
|
+
this.updated(propertyName, oldValue, newValue);
|
|
326
|
+
},
|
|
327
|
+
enumerable: true,
|
|
328
|
+
configurable: true
|
|
329
|
+
});
|
|
330
|
+
const originalConnectedCallback = target.connectedCallback;
|
|
331
|
+
target.connectedCallback = function() {
|
|
332
|
+
originalConnectedCallback.call(this);
|
|
333
|
+
this.updated(propertyName, null, defaultValue);
|
|
334
|
+
};
|
|
335
|
+
addObservedAttribute(target, attributeKey);
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
function addObservedAttribute(target, attribute) {
|
|
339
|
+
const ctor = target.constructor;
|
|
340
|
+
const existingObservedAttributes = ctor.observedAttributes || [];
|
|
341
|
+
if (!existingObservedAttributes.includes(attribute)) {
|
|
342
|
+
const newObservedAttributes = [...existingObservedAttributes, attribute];
|
|
343
|
+
Object.defineProperty(ctor, "observedAttributes", {
|
|
344
|
+
get() {
|
|
345
|
+
return newObservedAttributes;
|
|
346
|
+
},
|
|
347
|
+
configurable: true
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
var getInitialValue = (target, type, attributeKey, defaultValue) => {
|
|
352
|
+
if (type === Boolean) {
|
|
353
|
+
const hasAttribute = target.hasAttribute(attributeKey);
|
|
354
|
+
return hasAttribute || defaultValue;
|
|
355
|
+
}
|
|
356
|
+
const attributeValue = target.getAttribute(attributeKey);
|
|
357
|
+
return attributeValue !== null ? readAttributeValue(attributeValue, type) : defaultValue || defaultValueForType(type);
|
|
358
|
+
};
|
|
359
|
+
var addPropertyToMappings = (target, propertyMapping) => {
|
|
360
|
+
if (!("propertyConfigMap" in target)) {
|
|
361
|
+
Object.defineProperty(target, "propertyConfigMap", {
|
|
362
|
+
value: new Map,
|
|
363
|
+
configurable: true
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
target.propertyConfigMap.set(propertyMapping.propertyName, propertyMapping);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
// src/decorators/reactive-field.ts
|
|
370
|
+
function reactiveField(proto, propertyKey) {
|
|
371
|
+
const originalValues = new WeakMap;
|
|
372
|
+
const isDefined = new WeakSet;
|
|
373
|
+
Object.defineProperty(proto, propertyKey, {
|
|
374
|
+
get: function() {
|
|
375
|
+
return originalValues.get(this);
|
|
376
|
+
},
|
|
377
|
+
set: function(newValue) {
|
|
378
|
+
if (isDefined.has(this)) {
|
|
379
|
+
const oldValue = originalValues.get(this);
|
|
380
|
+
if (oldValue !== newValue) {
|
|
381
|
+
originalValues.set(this, newValue);
|
|
382
|
+
this.updated(propertyKey, oldValue, newValue);
|
|
383
|
+
}
|
|
384
|
+
} else {
|
|
385
|
+
originalValues.set(this, newValue);
|
|
386
|
+
isDefined.add(this);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
// src/core/radiant-element.ts
|
|
392
|
+
class RadiantElement extends HTMLElement {
|
|
393
|
+
eventSubscriptions = new Map;
|
|
394
|
+
elementReady = false;
|
|
395
|
+
connectedCallback() {
|
|
396
|
+
this.elementReady = true;
|
|
397
|
+
}
|
|
398
|
+
connectedContextCallback(_contextName) {
|
|
399
|
+
}
|
|
400
|
+
disconnectedCallback() {
|
|
401
|
+
this.removeAllSubscribedEvents();
|
|
402
|
+
}
|
|
403
|
+
updated(changedProperty, oldValue, value) {
|
|
404
|
+
if (!this.elementReady || !this.updatesRegistry || oldValue === value)
|
|
405
|
+
return;
|
|
406
|
+
const updates = this.updatesRegistry.get(changedProperty);
|
|
407
|
+
if (updates) {
|
|
408
|
+
for (const update of updates) {
|
|
409
|
+
this[update]();
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
414
|
+
if (oldValue === newValue || !this.elementReady)
|
|
415
|
+
return;
|
|
416
|
+
if (name in this) {
|
|
417
|
+
const config = this.propertyConfigMap.get(name);
|
|
418
|
+
const transformedValue = newValue ? config?.converter.fromAttribute(newValue) : newValue;
|
|
419
|
+
const transformedOldValue = oldValue ? config?.converter.fromAttribute(oldValue) : oldValue;
|
|
420
|
+
this[name] = transformedValue;
|
|
421
|
+
this.updated(name, transformedOldValue, transformedValue);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
renderTemplate({
|
|
425
|
+
target = this,
|
|
426
|
+
template,
|
|
427
|
+
insert = "replace"
|
|
428
|
+
}) {
|
|
429
|
+
switch (insert) {
|
|
430
|
+
case "replace":
|
|
431
|
+
target.innerHTML = template;
|
|
432
|
+
break;
|
|
433
|
+
case "beforeend":
|
|
434
|
+
target.insertAdjacentHTML("beforeend", template);
|
|
435
|
+
break;
|
|
436
|
+
case "afterbegin":
|
|
437
|
+
target.insertAdjacentHTML("afterbegin", template);
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
subscribeEvents(events) {
|
|
442
|
+
for (const event3 of events) {
|
|
443
|
+
this.subscribeEvent(event3);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
subscribeEvent(eventConfig) {
|
|
447
|
+
const delegatedListener = (delegatedEvent) => {
|
|
448
|
+
if (delegatedEvent.target && delegatedEvent.target.matches(eventConfig.selector)) {
|
|
449
|
+
eventConfig.listener.call(this, delegatedEvent);
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
this.addEventListener(eventConfig.type, delegatedListener, eventConfig.options);
|
|
453
|
+
this.eventSubscriptions.set(eventConfig.id, { ...eventConfig, listener: delegatedListener });
|
|
454
|
+
}
|
|
455
|
+
unsubscribeEvent(id) {
|
|
456
|
+
const eventSubscription = this.eventSubscriptions.get(id);
|
|
457
|
+
if (eventSubscription) {
|
|
458
|
+
this.removeEventListener(eventSubscription.type, eventSubscription.listener, eventSubscription.options);
|
|
459
|
+
this.eventSubscriptions.delete(id);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
removeAllSubscribedEvents() {
|
|
463
|
+
for (const eventSubscription of this.eventSubscriptions.values()) {
|
|
464
|
+
this.removeEventListener(eventSubscription.type, eventSubscription.listener, eventSubscription.options);
|
|
465
|
+
}
|
|
466
|
+
this.eventSubscriptions.clear();
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
// src/context/create-context.ts
|
|
470
|
+
var createContext = (key) => key;
|
|
471
|
+
|
|
472
|
+
// src/context/events.ts
|
|
473
|
+
var ContextEventsTypes;
|
|
474
|
+
((ContextEventsTypes2) => {
|
|
475
|
+
ContextEventsTypes2["SUBSCRIPTION_REQUEST"] = "context--subscription-request";
|
|
476
|
+
ContextEventsTypes2["CONTEXT_REQUEST"] = "context-request";
|
|
477
|
+
ContextEventsTypes2["ON_MOUNT"] = "context--on-mount";
|
|
478
|
+
})(ContextEventsTypes ||= {});
|
|
479
|
+
|
|
480
|
+
class ContextRequestEvent extends Event {
|
|
481
|
+
context;
|
|
482
|
+
callback;
|
|
483
|
+
subscribe;
|
|
484
|
+
constructor(context, callback, subscribe) {
|
|
485
|
+
super("context-request" /* CONTEXT_REQUEST */, { bubbles: true, composed: true });
|
|
486
|
+
this.context = context;
|
|
487
|
+
this.callback = callback;
|
|
488
|
+
this.subscribe = subscribe;
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
class ContextOnMountEvent extends CustomEvent {
|
|
493
|
+
constructor(context) {
|
|
494
|
+
super("context--on-mount" /* ON_MOUNT */, {
|
|
495
|
+
detail: { context },
|
|
496
|
+
bubbles: true,
|
|
497
|
+
composed: true
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
class ContextSubscriptionRequestEvent extends Event {
|
|
503
|
+
context;
|
|
504
|
+
callback;
|
|
505
|
+
select;
|
|
506
|
+
subscribe;
|
|
507
|
+
constructor(context, callback, select, subscribe) {
|
|
508
|
+
super("context--subscription-request" /* SUBSCRIPTION_REQUEST */, {
|
|
509
|
+
bubbles: true,
|
|
510
|
+
composed: true
|
|
511
|
+
});
|
|
512
|
+
this.context = context;
|
|
513
|
+
this.callback = callback;
|
|
514
|
+
this.select = select;
|
|
515
|
+
this.subscribe = subscribe;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// src/context/context-provider.ts
|
|
520
|
+
var HYDRATE_ATTRIBUTE = "hydrate-context";
|
|
521
|
+
|
|
522
|
+
class ContextProvider {
|
|
523
|
+
host;
|
|
524
|
+
context;
|
|
525
|
+
value;
|
|
526
|
+
subscriptions = [];
|
|
527
|
+
constructor(host, options) {
|
|
528
|
+
this.host = host;
|
|
529
|
+
this.context = options.context;
|
|
530
|
+
let contextValue = options.initialValue;
|
|
531
|
+
if (options.hydrate) {
|
|
532
|
+
const hydrationValue = this.host.getAttribute(HYDRATE_ATTRIBUTE);
|
|
533
|
+
if (hydrationValue) {
|
|
534
|
+
const parsedHydrationValue = readAttributeValue(hydrationValue, options.hydrate);
|
|
535
|
+
this.host.removeAttribute(HYDRATE_ATTRIBUTE);
|
|
536
|
+
if (options.hydrate === Object && this.isObject(parsedHydrationValue) && (this.isObject(contextValue) || typeof contextValue === "undefined")) {
|
|
537
|
+
contextValue = {
|
|
538
|
+
...contextValue ?? {},
|
|
539
|
+
...parsedHydrationValue
|
|
540
|
+
};
|
|
541
|
+
} else {
|
|
542
|
+
contextValue = parsedHydrationValue;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
this.value = contextValue;
|
|
547
|
+
this.registerEvents();
|
|
548
|
+
this.host.dispatchEvent(new ContextOnMountEvent(this.context));
|
|
549
|
+
}
|
|
550
|
+
setContext = (update, callback) => {
|
|
551
|
+
if (typeof this.value === "object") {
|
|
552
|
+
const oldContext = { ...this.value };
|
|
553
|
+
this.value = { ...this.value, ...update };
|
|
554
|
+
if (callback)
|
|
555
|
+
callback(this.value);
|
|
556
|
+
this.notifySubscribers(this.value, oldContext);
|
|
557
|
+
}
|
|
558
|
+
};
|
|
559
|
+
getContext = () => {
|
|
560
|
+
return this.value;
|
|
561
|
+
};
|
|
562
|
+
subscribe = ({ select, callback }) => {
|
|
563
|
+
this.subscriptions.push({ select, callback });
|
|
564
|
+
};
|
|
565
|
+
isObject(value) {
|
|
566
|
+
return typeof value === "object" && !Array.isArray(value) && value !== null;
|
|
567
|
+
}
|
|
568
|
+
notifySubscribers = (newContext, prevContext) => {
|
|
569
|
+
for (const sub of this.subscriptions) {
|
|
570
|
+
if (!sub.select)
|
|
571
|
+
return this.sendSubscriptionUpdate(sub, newContext);
|
|
572
|
+
const newSelected = sub.select(newContext);
|
|
573
|
+
const prevSelected = sub.select(prevContext);
|
|
574
|
+
if (newSelected !== prevSelected) {
|
|
575
|
+
this.sendSubscriptionUpdate(sub, newContext);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
sendSubscriptionUpdate = ({ select, callback }, context) => {
|
|
580
|
+
if (!select)
|
|
581
|
+
callback(context);
|
|
582
|
+
else
|
|
583
|
+
callback(select(context));
|
|
584
|
+
};
|
|
585
|
+
handleSubscriptionRequest = ({
|
|
586
|
+
select,
|
|
587
|
+
callback,
|
|
588
|
+
subscribe
|
|
589
|
+
}) => {
|
|
590
|
+
if (subscribe)
|
|
591
|
+
this.subscribe({ select, callback });
|
|
592
|
+
if (!this.value)
|
|
593
|
+
return;
|
|
594
|
+
if (select) {
|
|
595
|
+
callback(select(this.value));
|
|
596
|
+
} else {
|
|
597
|
+
callback(this.value);
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
onSubscriptionRequest = (event3) => {
|
|
601
|
+
const { context, callback, subscribe, select, target } = event3;
|
|
602
|
+
if (context !== this.context)
|
|
603
|
+
return;
|
|
604
|
+
event3.stopPropagation();
|
|
605
|
+
target.dispatchEvent(new ContextOnMountEvent(this.context));
|
|
606
|
+
this.handleSubscriptionRequest({ select, callback, subscribe });
|
|
607
|
+
};
|
|
608
|
+
onContextRequest = (event3) => {
|
|
609
|
+
const { context, callback } = event3;
|
|
610
|
+
if (context !== this.context)
|
|
611
|
+
return;
|
|
612
|
+
event3.stopPropagation();
|
|
613
|
+
callback(this);
|
|
614
|
+
};
|
|
615
|
+
registerEvents = () => {
|
|
616
|
+
this.host.addEventListener("context--subscription-request" /* SUBSCRIPTION_REQUEST */, this.onSubscriptionRequest);
|
|
617
|
+
this.host.addEventListener("context-request" /* CONTEXT_REQUEST */, this.onContextRequest);
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// src/context/decorators/provide-context.ts
|
|
622
|
+
function provideContext({ context, initialValue, hydrate }) {
|
|
623
|
+
return (proto, propertyKey) => {
|
|
624
|
+
const originalConnectedCallback = proto.connectedCallback;
|
|
625
|
+
proto.connectedCallback = function() {
|
|
626
|
+
originalConnectedCallback.call(this);
|
|
627
|
+
this[propertyKey] = new ContextProvider(this, { context, initialValue, hydrate });
|
|
628
|
+
this.connectedContextCallback(context);
|
|
629
|
+
};
|
|
630
|
+
};
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
// src/context/decorators/consume-context.ts
|
|
634
|
+
function consumeContext(contextToProvide) {
|
|
635
|
+
return (proto, propertyKey) => {
|
|
636
|
+
const originalConnectedCallback = proto.connectedCallback;
|
|
637
|
+
proto.connectedCallback = function() {
|
|
638
|
+
originalConnectedCallback.call(this);
|
|
639
|
+
this.dispatchEvent(new ContextRequestEvent(contextToProvide, (context) => {
|
|
640
|
+
this[propertyKey] = context;
|
|
641
|
+
this.connectedContextCallback(contextToProvide);
|
|
642
|
+
}));
|
|
643
|
+
};
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// src/context/decorators/context-selector.ts
|
|
648
|
+
function contextSelector({
|
|
649
|
+
context,
|
|
650
|
+
select,
|
|
651
|
+
subscribe = true
|
|
652
|
+
}) {
|
|
653
|
+
return (proto, _, descriptor) => {
|
|
654
|
+
const originalMethod = descriptor.value;
|
|
655
|
+
const originalConnectedCallback = proto.connectedCallback;
|
|
656
|
+
proto.connectedCallback = function() {
|
|
657
|
+
originalConnectedCallback.call(this);
|
|
658
|
+
this.dispatchEvent(new ContextSubscriptionRequestEvent(context, originalMethod.bind(this), select, subscribe));
|
|
659
|
+
};
|
|
660
|
+
descriptor.value = function(...args) {
|
|
661
|
+
const result = originalMethod.apply(this, args);
|
|
662
|
+
return result;
|
|
663
|
+
};
|
|
664
|
+
return descriptor;
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
// src/mixins/with-kita.ts
|
|
668
|
+
function WithKita(Base) {
|
|
669
|
+
return class extends Base {
|
|
670
|
+
async renderTemplate({ target = this, template, insert = "replace" }) {
|
|
671
|
+
const safeTemplate = typeof template !== "string" ? template.toString() : template;
|
|
672
|
+
switch (insert) {
|
|
673
|
+
case "replace":
|
|
674
|
+
target.innerHTML = safeTemplate;
|
|
675
|
+
break;
|
|
676
|
+
case "beforeend":
|
|
677
|
+
target.insertAdjacentHTML("beforeend", safeTemplate);
|
|
678
|
+
break;
|
|
679
|
+
case "afterbegin":
|
|
680
|
+
target.insertAdjacentHTML("afterbegin", safeTemplate);
|
|
681
|
+
break;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
};
|
|
685
|
+
}
|
|
686
|
+
// src/tools/stringify-attribute.ts
|
|
687
|
+
function stringifyAttribute(value) {
|
|
688
|
+
return JSON.stringify(value);
|
|
689
|
+
}
|
|
690
|
+
export {
|
|
691
|
+
writeAttributeValue,
|
|
692
|
+
stringifyAttribute,
|
|
693
|
+
readAttributeValue,
|
|
694
|
+
reactiveProp,
|
|
695
|
+
reactiveField,
|
|
696
|
+
query,
|
|
697
|
+
provideContext,
|
|
698
|
+
parseAttributeTypeDefault,
|
|
699
|
+
parseAttributeTypeConstant,
|
|
700
|
+
onUpdated,
|
|
701
|
+
onEvent,
|
|
702
|
+
isValueOfType,
|
|
703
|
+
event,
|
|
704
|
+
defaultValueForType,
|
|
705
|
+
customElement,
|
|
706
|
+
createContext,
|
|
707
|
+
contextSelector,
|
|
708
|
+
consumeContext,
|
|
709
|
+
WithKita,
|
|
710
|
+
RadiantElement,
|
|
711
|
+
HYDRATE_ATTRIBUTE,
|
|
712
|
+
EventEmitter,
|
|
713
|
+
ContextSubscriptionRequestEvent,
|
|
714
|
+
ContextRequestEvent,
|
|
715
|
+
ContextProvider,
|
|
716
|
+
ContextOnMountEvent,
|
|
717
|
+
ContextEventsTypes
|
|
718
|
+
};
|
|
719
|
+
|
|
720
|
+
//# debugId=9F8C73D65206CDB164756E2164756E21
|