@twobirds/microcomponents 0.1.21 → 0.2.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/LICENSE +21 -0
- package/README.md +91 -92
- package/dist/MC.min.js +1 -345
- package/dist/autoload.min.js +1 -0
- package/dist/core/MC.d.ts +401 -0
- package/dist/core/MC.d.ts.map +1 -0
- package/dist/core/MC.js +471 -0
- package/dist/core/autoload.d.ts +54 -0
- package/dist/core/autoload.d.ts.map +1 -0
- package/dist/core/autoload.js +158 -0
- package/dist/core/elements.d.ts +232 -0
- package/dist/core/elements.d.ts.map +1 -0
- package/dist/core/elements.js +123 -0
- package/dist/core/events.d.ts +231 -0
- package/dist/core/events.d.ts.map +1 -0
- package/dist/core/events.js +228 -0
- package/dist/core/mcGuards.d.ts +49 -0
- package/dist/core/mcGuards.d.ts.map +1 -0
- package/dist/core/mcGuards.js +19 -0
- package/dist/elements.min.js +1 -1
- package/dist/events.min.js +1 -0
- package/dist/formValues.min.js +1 -1
- package/dist/mcGuards.min.js +1 -0
- package/dist/microcomponents.d.ts +8 -5
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.js +8 -5
- package/dist/microcomponents.min.js +1 -1
- package/dist/observables.min.js +1 -1
- package/dist/state/formValues.d.ts +70 -0
- package/dist/state/formValues.d.ts.map +1 -0
- package/dist/state/formValues.js +334 -0
- package/dist/state/observables.d.ts +276 -0
- package/dist/state/observables.d.ts.map +1 -0
- package/dist/state/observables.js +382 -0
- package/dist/state/placeholders.d.ts +62 -0
- package/dist/state/placeholders.d.ts.map +1 -0
- package/dist/state/placeholders.js +181 -0
- package/dist/types/mcTypes.d.ts +201 -0
- package/dist/types/mcTypes.d.ts.map +1 -0
- package/dist/types/mcTypes.js +9 -0
- package/dist/utils/helpers.d.ts +195 -0
- package/dist/utils/helpers.d.ts.map +1 -0
- package/dist/utils/helpers.js +340 -0
- package/package.json +96 -62
- package/dist/MC.d.ts +0 -31
- package/dist/MC.d.ts.map +0 -1
- package/dist/MC.js +0 -249
- package/dist/MC.ts +0 -613
- package/dist/MicroComponents.svg +0 -15
- package/dist/elements.d.ts +0 -5
- package/dist/elements.d.ts.map +0 -1
- package/dist/elements.js +0 -49
- package/dist/elements.ts +0 -114
- package/dist/events.d.ts +0 -11
- package/dist/events.d.ts.map +0 -1
- package/dist/events.js +0 -55
- package/dist/events.ts +0 -158
- package/dist/formValues.d.ts +0 -3
- package/dist/formValues.d.ts.map +0 -1
- package/dist/formValues.js +0 -156
- package/dist/formValues.ts +0 -371
- package/dist/helpers.d.ts +0 -15
- package/dist/helpers.d.ts.map +0 -1
- package/dist/helpers.js +0 -113
- package/dist/helpers.ts +0 -219
- package/dist/main.mjs +0 -1
- package/dist/microcomponents.ts +0 -6
- package/dist/observables.d.ts +0 -11
- package/dist/observables.d.ts.map +0 -1
- package/dist/observables.js +0 -228
- package/dist/observables.ts +0 -449
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@module
|
|
3
|
+
|
|
4
|
+
This module provides event utilities for interfacing **between microcomponents and external
|
|
5
|
+
frameworks or native DOM/JS environments**. It is not intended for communication between
|
|
6
|
+
frontend microcomponents themselves — for that purpose, use the inherited methods
|
|
7
|
+
( `.on()`, `.one()`, `.off()`, `.trigger()`) that exist when you extend `DC`.
|
|
8
|
+
|
|
9
|
+
Import this module when you need to exchange information with microcomponents from outside
|
|
10
|
+
the microcomponent system (e.g., vanilla JS, React via `useRef()`, or other frameworks).
|
|
11
|
+
|
|
12
|
+
The core feature is the ability to explicitly control event flow direction when using {@link trigger}:
|
|
13
|
+
- upward propagation to ancestor components ('u')
|
|
14
|
+
- local dispatch only ('l') [default]
|
|
15
|
+
- downward propagation to descendant components ('d').
|
|
16
|
+
|
|
17
|
+
@example
|
|
18
|
+
html:
|
|
19
|
+
```html
|
|
20
|
+
<div class="grandparent">
|
|
21
|
+
<div class="parent">
|
|
22
|
+
<div class="child">
|
|
23
|
+
</div>
|
|
24
|
+
</div>
|
|
25
|
+
</div>
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
javascript:
|
|
29
|
+
```javascript
|
|
30
|
+
import { on, one, trigger, off } from '@twobirds/microcomponents/events';
|
|
31
|
+
|
|
32
|
+
// Attach listeners (querySelector may return null — guard first)
|
|
33
|
+
const grandParent = document.querySelector('.grandparent');
|
|
34
|
+
const parent = document.querySelector('.parent');
|
|
35
|
+
const child = document.querySelector('.child');
|
|
36
|
+
if (!grandParent || !parent || !child) throw new Error('example markup missing');
|
|
37
|
+
|
|
38
|
+
const grandParentHandler = on(
|
|
39
|
+
grandParent,
|
|
40
|
+
'dataReady',
|
|
41
|
+
(ev) => console.log(ev.detail.data)
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const parentHandler = on(
|
|
45
|
+
parent,
|
|
46
|
+
'dataReady',
|
|
47
|
+
(ev) => ev.detail.control.stop()
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
// this event bubbles toward root from the innermost div ..
|
|
51
|
+
trigger( child, 'dataReady', { value: 42 }, 'u');
|
|
52
|
+
// ...but the 'dataReady' handler on grandParent will never be reached !!!
|
|
53
|
+
|
|
54
|
+
// Clean up handlers when done
|
|
55
|
+
off( parent, 'dataReady', parentHandler );
|
|
56
|
+
off( grandParent, 'dataReady', grandParentHandler );
|
|
57
|
+
```
|
|
58
|
+
*/
|
|
59
|
+
'use strict';
|
|
60
|
+
import { trueEventName } from '../utils/helpers.js';
|
|
61
|
+
/**
|
|
62
|
+
* Converts a standard event name to its microcomponent-prefixed variant.
|
|
63
|
+
*
|
|
64
|
+
* This utility is used internally to namespace custom events (e.g., 'Click' → '_mcClick').
|
|
65
|
+
*
|
|
66
|
+
* @param eventName - The original event name (e.g., 'click', 'update').
|
|
67
|
+
* @returns The microcomponent-prefixed event name.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* getMcEventName('update'); // '_mcUpdate'
|
|
72
|
+
* ```
|
|
73
|
+
*/
|
|
74
|
+
export function getMcEventName(eventName) {
|
|
75
|
+
return '_mc' + eventName[0].toUpperCase() + eventName.substring(1);
|
|
76
|
+
}
|
|
77
|
+
export function off(target, eventName, handler) {
|
|
78
|
+
/* v8 ignore next */
|
|
79
|
+
if (!target)
|
|
80
|
+
return;
|
|
81
|
+
if (typeof target === 'string') {
|
|
82
|
+
document
|
|
83
|
+
.querySelectorAll(target)
|
|
84
|
+
.forEach((el) => off(el, eventName, handler));
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
// McEventHandler<D> narrows the runtime EventListener contract type-side only;
|
|
88
|
+
// the cast re-widens it at the DOM boundary.
|
|
89
|
+
target.removeEventListener(trueEventName(target, eventName), handler);
|
|
90
|
+
}
|
|
91
|
+
export function on(target, eventName, handler, once = false) {
|
|
92
|
+
/* v8 ignore next */
|
|
93
|
+
if (!target)
|
|
94
|
+
return;
|
|
95
|
+
if (typeof target === 'string') {
|
|
96
|
+
document
|
|
97
|
+
.querySelectorAll(target)
|
|
98
|
+
.forEach((el) => on(el, eventName, handler));
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
target.addEventListener(trueEventName(target, eventName), handler, {
|
|
102
|
+
capture: false,
|
|
103
|
+
once,
|
|
104
|
+
});
|
|
105
|
+
return handler;
|
|
106
|
+
}
|
|
107
|
+
export function one(target, eventName, handler) {
|
|
108
|
+
/* v8 ignore next */
|
|
109
|
+
if (!target)
|
|
110
|
+
return;
|
|
111
|
+
if (typeof target === 'string') {
|
|
112
|
+
document
|
|
113
|
+
.querySelectorAll(target)
|
|
114
|
+
.forEach((el) => one(el, eventName, handler));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
on(target, eventName, handler, true); // true indicates once
|
|
118
|
+
return handler;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* @hidden
|
|
122
|
+
* A controller object used to manage and potentially abort event bubbling.
|
|
123
|
+
*
|
|
124
|
+
* The `trigger()` function internally creates instance of this class,
|
|
125
|
+
* allowing the an eventual handler to call `ev.detail.control.stop()` to
|
|
126
|
+
* prevent further propagation in either direction (up/down).
|
|
127
|
+
*
|
|
128
|
+
* This is only used internally.
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* trigger(myElement, 'update', { value: 42 }, 'u');
|
|
132
|
+
*
|
|
133
|
+
* // inside a handler you can call ev.detail.control.stop() to halt propagation
|
|
134
|
+
* onUpdate(ev) {
|
|
135
|
+
* ev.detail.control.stop();
|
|
136
|
+
* }
|
|
137
|
+
* ```
|
|
138
|
+
*/
|
|
139
|
+
export class BubbleController {
|
|
140
|
+
stopped = false;
|
|
141
|
+
constructor() { }
|
|
142
|
+
/**
|
|
143
|
+
* Signals that event bubbling should be halted.
|
|
144
|
+
*
|
|
145
|
+
* After calling this method, subsequent `trigger()` calls will abort
|
|
146
|
+
* before dispatching the event to any targets.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* trigger(myElement, 'update', { value: 42 }, 'u');
|
|
151
|
+
*
|
|
152
|
+
* // inside a handler you can call ev.detail.control.stop() to halt propagation
|
|
153
|
+
* onUpdate(ev) {
|
|
154
|
+
* ev.detail.control.stop();
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
stop() {
|
|
159
|
+
this.stopped = true;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Creates and dispatches a custom event on the specified DOM node.
|
|
164
|
+
*
|
|
165
|
+
* The event is namespaced with an `_mc` prefix by default, unless already prefixed.
|
|
166
|
+
* Supports bidirectional bubbling (up/down the DOM tree) based on the `bubble` parameter:
|
|
167
|
+
* - 'l': local dispatch only
|
|
168
|
+
* - 'u': bubble up towards ancestors
|
|
169
|
+
* - 'd': bubble down to descendants
|
|
170
|
+
*
|
|
171
|
+
* The dispatched event is a `CustomEvent<McDetail<D>>`: handlers find the payload
|
|
172
|
+
* under `ev.detail.data` and can call `ev.detail.control.stop()` to cancel bubbling.
|
|
173
|
+
*
|
|
174
|
+
* @param target - The DOM element on which to trigger the event, or a querySelectorAll(selector) selector string.
|
|
175
|
+
* @param event - The name of the event (will be prefixed if needed).
|
|
176
|
+
* @param data - Additional data payload attached to `event.detail.data`.
|
|
177
|
+
* @param bubble - Bubbling direction ('l', 'u', 'd' or combinations).
|
|
178
|
+
* @param controller - Optional controller for managing bubbling behavior.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* // Trigger a local event with data
|
|
183
|
+
* trigger(myElement, 'update', { value: 42 }, 'l');
|
|
184
|
+
*
|
|
185
|
+
* // Bubble up to ancestors
|
|
186
|
+
* trigger(myElement, 'dataReady', { ready: true }, 'u');
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export function trigger(target, event, data = {}, bubble = 'l', // direction of bubbling
|
|
190
|
+
controller) {
|
|
191
|
+
/* v8 ignore next */
|
|
192
|
+
if (!target)
|
|
193
|
+
return;
|
|
194
|
+
if (typeof target === 'string') {
|
|
195
|
+
document
|
|
196
|
+
.querySelectorAll(target)
|
|
197
|
+
.forEach((el) => trigger(el, event, data, bubble));
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const eventName = /^_mc/.test(event) ? event : trueEventName(target, event), bubbleController = controller ? controller : new BubbleController(), ev = new CustomEvent(eventName, {
|
|
201
|
+
detail: { data, control: bubbleController },
|
|
202
|
+
bubbles: false,
|
|
203
|
+
});
|
|
204
|
+
if (bubbleController.stopped) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (/l/.test(bubble)) {
|
|
208
|
+
target.dispatchEvent(ev);
|
|
209
|
+
if (bubbleController.stopped) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// bubble if up or down is indicated
|
|
214
|
+
if (/[ud]/.test(bubble)) {
|
|
215
|
+
queueMicrotask(() => {
|
|
216
|
+
// bubble up towards document.body
|
|
217
|
+
if (bubble.indexOf('u') > -1) {
|
|
218
|
+
trigger(target.parentElement, eventName, data, 'lu', bubbleController);
|
|
219
|
+
}
|
|
220
|
+
// bubble down towards DOM children
|
|
221
|
+
if (bubble.indexOf('d') > -1) {
|
|
222
|
+
for (const child of target.children) {
|
|
223
|
+
trigger(child, eventName, data, 'ld'); // event multiplex - dont carry bubble controller
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@module Guards
|
|
3
|
+
|
|
4
|
+
Type guards for microcomponent hosts.
|
|
5
|
+
|
|
6
|
+
This module deliberately contains nothing but the guard: it imports types only,
|
|
7
|
+
executes no module-level code, and never touches the DC/autoload machinery in
|
|
8
|
+
MC.ts — so re-query sites all over an app can import the predicate and a
|
|
9
|
+
bundler can tree-shake everything else away.
|
|
10
|
+
*/
|
|
11
|
+
import type { McHost } from '../types/mcTypes.js';
|
|
12
|
+
/**
|
|
13
|
+
Checks whether the given value is a microcomponent host element ({@link McHost}).
|
|
14
|
+
|
|
15
|
+
Bare form: narrows to the controller API only — no registry entries are claimed,
|
|
16
|
+
because different elements carry different `_mc` records and the type system
|
|
17
|
+
cannot know which keys a specific element holds.
|
|
18
|
+
|
|
19
|
+
Keyed form: additionally checks `key in element._mc` at runtime and narrows the
|
|
20
|
+
entry to the type bound on the global `McRegistry` interface — *non-optional*,
|
|
21
|
+
because presence was just verified. The binding is an app-wide naming
|
|
22
|
+
convention (key name → component type); where type identity must be proven or
|
|
23
|
+
key names may collide, use `DC.get(el, key, Class)` instead.
|
|
24
|
+
|
|
25
|
+
@param element - The value to test.
|
|
26
|
+
@param key - Optional registry key (declared on `McRegistry`) whose presence to verify.
|
|
27
|
+
@returns True if `element` is an `HTMLElement` carrying an `_mc` hash (and the key, if given).
|
|
28
|
+
@example
|
|
29
|
+
```ts
|
|
30
|
+
declare global {
|
|
31
|
+
interface McRegistry {
|
|
32
|
+
cart: Cart;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const el = document.querySelector('form');
|
|
37
|
+
if (el && isMcDOM(el)) {
|
|
38
|
+
el._mc.trigger('Ping'); // controller API — nothing else claimed
|
|
39
|
+
}
|
|
40
|
+
if (el && isMcDOM(el, 'cart')) {
|
|
41
|
+
el._mc.cart.addItem(item); // Cart — presence checked by the guard
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
*/
|
|
45
|
+
export declare function isMcDOM<T extends HTMLElement, K extends keyof McRegistry & string>(element: T, key: K): element is McHost<T, Pick<McRegistry, K>>;
|
|
46
|
+
export declare function isMcDOM<K extends keyof McRegistry & string>(element: unknown, key: K): element is McHost<HTMLElement, Pick<McRegistry, K>>;
|
|
47
|
+
export declare function isMcDOM<T extends HTMLElement>(element: T): element is McHost<T>;
|
|
48
|
+
export declare function isMcDOM(element: unknown): element is McHost;
|
|
49
|
+
//# sourceMappingURL=mcGuards.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcGuards.d.ts","sourceRoot":"","sources":["../../src/core/mcGuards.ts"],"names":[],"mappings":"AAAA;;;;;;;;;EASE;AAEF,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgCE;AACF,wBAAgB,OAAO,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,SAAS,MAAM,UAAU,GAAG,MAAM,EACjF,OAAO,EAAE,CAAC,EACV,GAAG,EAAE,CAAC,GACJ,OAAO,IAAI,MAAM,CAAC,CAAC,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC;AAC7C,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,UAAU,GAAG,MAAM,EAC1D,OAAO,EAAE,OAAO,EAChB,GAAG,EAAE,CAAC,GACJ,OAAO,IAAI,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC;AACvD,wBAAgB,OAAO,CAAC,CAAC,SAAS,WAAW,EAAE,OAAO,EAAE,CAAC,GAAG,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC;AACjF,wBAAgB,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,IAAI,MAAM,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@module Guards
|
|
3
|
+
|
|
4
|
+
Type guards for microcomponent hosts.
|
|
5
|
+
|
|
6
|
+
This module deliberately contains nothing but the guard: it imports types only,
|
|
7
|
+
executes no module-level code, and never touches the DC/autoload machinery in
|
|
8
|
+
MC.ts — so re-query sites all over an app can import the predicate and a
|
|
9
|
+
bundler can tree-shake everything else away.
|
|
10
|
+
*/
|
|
11
|
+
export function isMcDOM(element, key) {
|
|
12
|
+
return (
|
|
13
|
+
// `typeof` first: in DOM-less runtimes (server-side MC) the global does
|
|
14
|
+
// not exist, and a guard must answer false there - never throw.
|
|
15
|
+
typeof HTMLElement !== 'undefined' &&
|
|
16
|
+
element instanceof HTMLElement &&
|
|
17
|
+
'_mc' in element &&
|
|
18
|
+
(key === undefined || key in element._mc));
|
|
19
|
+
}
|
package/dist/elements.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
function e(e){return e.split(`-`).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``)}function t(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function n(e,r,i){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>n(e,r,i));return}e.removeEventListener(t(e,r),i)}}function r(e,n,i,a=!1){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>r(e,n,i));return}return e.addEventListener(t(e,n),i,{capture:!1,once:a}),i}}function i(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>i(e,t,n));return}return r(e,t,n,!0),n}}var a=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function o(e,n,r={},i=`l`,s){if(!e)return;if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>o(e,n,r,i));return}let c=/^_mc/.test(n)?n:t(e,n),l=s||new a,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(i)&&(e.dispatchEvent(u),l.stopped)||/[ud]/.test(i)&&queueMicrotask(()=>{if(i.indexOf(`u`)>-1&&o(e.parentElement,c,r,`lu`,l),i.indexOf(`d`)>-1)for(let t of e.children)o(t,c,r,`ld`)})}function s(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}function c(e,t,n={}){let r=e;`_mc`in e||(r._mc=new h(e));let i=r._mc;return t.length&&!i[t]&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function l(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function u(e){queueMicrotask(()=>{e.trigger(`Init`)})}function d(e){let t=e.target,n=Object.getPrototypeOf(e),i=e,a=Object.getOwnPropertyNames(n).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof n[e]==`function`);for(let e of a){let n=/^one/.test(e);r(t,e.replace(/^one?/,``),t=>i[e](t),n)}}function f(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function p(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var m=class{_target;constructor(e){this._target=new WeakRef(e||{}),this instanceof h&&(d(this),u(this))}get target(){return this._target.deref()}},h=class extends m{constructor(e){super(e)}static add(e,t,n={}){return c(e,t,n)}static remove(e,t){l(e,t)}static get(e,t,n){if(!s(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,t){n(this.target,e,t)}on(e,t){return r(this.target,e,t)}one(e,t){return i(this.target,e,t)}trigger(e,t={},n=`l`,r){o(this.target,e,t,n,r)}ancestors(e){let t=[],n=p(this.target,e);for(;n;)t.push(n),n=p(n.target,e);return t}closest(e){return p(this.target,e)}children(e){let t=f(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}};function g(e,t){return Object.defineProperty(t,"name",{value:e})}function _(e){return class extends e{constructor(...e){super(...e)}}}function v(t,n,r=[],i){if(customElements.get(t)){console.warn(`IGNORED: Custom element "${t}" is already defined!`);return}if(!t.includes(`-`))throw Error(`createCustomElement: tagName must contain at least one hyphen ("-").`);let a=HTMLElement;i&&(a=document.createElement(i).constructor);let o=e(t);if(!(n.prototype instanceof m))return customElements.define(t,n),n;let s=g(o,class extends _(a){constructor(e){super(),h.add(this,o,new n(this,e))}connectedCallback(){this.dispatchEvent(new CustomEvent(`_mcConnected`))}connectedMoveCallback(){this.dispatchEvent(new CustomEvent(`_mcConnectedMove`))}disconnectedCallback(){this.dispatchEvent(new CustomEvent(`_mcDisconnected`))}adoptedCallback(){this.dispatchEvent(new CustomEvent(`_mcAdopted`))}attributeChangedCallback(e,t,n){this.dispatchEvent(new CustomEvent(`_mcAttributeChanged`,{detail:{name:e,oldValue:t,newValue:n}}))}formResetCallback(){this.dispatchEvent(new CustomEvent(`_mcFormReset`))}static get observedAttributes(){return[...r]}});return customElements.define(t,s,{extends:i}),s}function y(e){return customElements.get(e)}export{v as defineCE,y as getCE};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function t(e){return`_mc`+e[0].toUpperCase()+e.substring(1)}function n(t,r,i){if(t){if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>n(e,r,i));return}t.removeEventListener(e(t,r),i)}}function r(t,n,i,a=!1){if(t){if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>r(e,n,i));return}return t.addEventListener(e(t,n),i,{capture:!1,once:a}),i}}function i(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>i(e,t,n));return}return r(e,t,n,!0),n}}var a=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function o(t,n,r={},i=`l`,s){if(!t)return;if(typeof t==`string`){document.querySelectorAll(t).forEach(e=>o(e,n,r,i));return}let c=/^_mc/.test(n)?n:e(t,n),l=s||new a,u=new CustomEvent(c,{detail:{data:r,control:l},bubbles:!1});l.stopped||/l/.test(i)&&(t.dispatchEvent(u),l.stopped)||/[ud]/.test(i)&&queueMicrotask(()=>{if(i.indexOf(`u`)>-1&&o(t.parentElement,c,r,`lu`,l),i.indexOf(`d`)>-1)for(let e of t.children)o(e,c,r,`ld`)})}export{a as BubbleController,t as getMcEventName,n as off,r as on,i as one,o as trigger};
|
package/dist/formValues.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
function e(){return typeof window<`u`&&window.document!==void 0}function t(e){return typeof e==`object`&&!!e&&e.constructor===Object}function n(e){let r={},i=Object.keys(e).sort();for(let a=0;a<i.length;a++){let o=i[a],s=e[o];t(s)?r[o]=n(s):r[o]=s}return r}function r(e,t=!0){let i={},a=e,o=Object.getOwnPropertyNames(e);for(let e=0;e<o.length;e++){let t=o[e],n=a[t];if(n instanceof Object){let e=r(n,!1);for(let n in e)i[t+`.`+n]=e[n]}else i[t]=n}return t?n(i):i}function i(e,t){if(e===t||e!==e&&t!==t)return!0;if(typeof e!=`object`||!e||typeof t!=`object`||!t||e.constructor!==t.constructor)return!1;if(e instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp)return e.toString()===t.toString();if(Array.isArray(e)){let n=t;if(e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(!i(e[t],n[t]))return!1;return!0}let n=e,r=t,a=Object.keys(n);if(a.length!==Object.keys(r).length)return!1;for(let e=0;e<a.length;e++){let t=a[e];if(!Object.prototype.hasOwnProperty.call(r,t)||!i(n[t],r[t]))return!1}return!0}function a(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function o(e,t){let n=Object.getOwnPropertyDescriptors(e);for(let[e,r]of Object.entries(n))(`get`in r||`set`in r)&&Object.defineProperty(t,e,r)}function s(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>s(e,t,n));return}e.removeEventListener(a(e,t),n)}}function c(e,t,n,r=!1){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>c(e,t,n));return}return e.addEventListener(a(e,t),n,{capture:!1,once:r}),n}}function l(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>l(e,t,n));return}return c(e,t,n,!0),n}}var u=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function d(e,t,n={},r=`l`,i){if(!e)return;if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>d(e,t,n,r));return}let o=/^_mc/.test(t)?t:a(e,t),s=i||new u,c=new CustomEvent(o,{detail:{data:n,control:s},bubbles:!1});s.stopped||/l/.test(r)&&(e.dispatchEvent(c),s.stopped)||/[ud]/.test(r)&&queueMicrotask(()=>{if(r.indexOf(`u`)>-1&&d(e.parentElement,o,n,`lu`,s),r.indexOf(`d`)>-1)for(let t of e.children)d(t,o,n,`ld`)})}function f(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}function p(e,t,n={}){let r=e;`_mc`in e||(r._mc=new b(e));let i=r._mc;return t.length&&!i[t]&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function m(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function h(e){queueMicrotask(()=>{e.trigger(`Init`)})}function g(e){let t=e.target,n=Object.getPrototypeOf(e),r=e,i=Object.getOwnPropertyNames(n).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof n[e]==`function`);for(let e of i){let n=/^one/.test(e);c(t,e.replace(/^one?/,``),t=>r[e](t),n)}}function _(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function v(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var y=class{_target;constructor(e){this._target=new WeakRef(e||{}),this instanceof b&&(g(this),h(this))}get target(){return this._target.deref()}},b=class extends y{constructor(e){super(e)}static add(e,t,n={}){return p(e,t,n)}static remove(e,t){m(e,t)}static get(e,t,n){if(!f(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,t){s(this.target,e,t)}on(e,t){return c(this.target,e,t)}one(e,t){return l(this.target,e,t)}trigger(e,t={},n=`l`,r){d(this.target,e,t,n,r)}ancestors(e){let t=[],n=v(this.target,e);for(;n;)t.push(n),n=v(n.target,e);return t}closest(e){return v(this.target,e)}children(e){let t=_(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}};function x(e){let t=new Set,n=/\{[^\{\}]*\}/g;return(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(n)).forEach(r=>{new Set(e.getAttribute(r)?.match(n)).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(n)).forEach(e=>{(e?.nodeValue||``).match(n).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...t]}var S=[`INPUT`,`SELECT`,`TEXTAREA`],C=class extends b{data={};oldDisplay=``;constructor(e){super(e),x(e).forEach(e=>this.data[e]=``),j(this),this.#attachCallbacks()}#attachCallbacks(){let e=this.target,t=!1;(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)).forEach(t=>{let n=new Set(e.getAttribute(t)?.match(/\{[^\{\}]*\}/g));this.data.subscribe((r=>i=>{let a=r;n.forEach(e=>{a=a.replace(e,String(i[e.replace(/[\{\}]/g,``)]))}),e.setAttribute(t,a)})(e.getAttribute(t)??``))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(/\{[^\{\}]*\}/g)).forEach(n=>{t=!0;let r=new Set((n.nodeValue??``).match(/\{[^\{\}]*\}/g));this.data.subscribe((t=>i=>{let a=t;r.forEach(e=>{a=a.replace(e,String(i[e.replace(/[\{\}]/g,``)]))}),this.oldDisplay?e.style=this.oldDisplay:e.removeAttribute(`style`),delete this.oldDisplay,n.nodeValue=a})(n.nodeValue??``)),this.oldDisplay=e.getAttribute(`style`)||``,t&&!S.includes(e.tagName)&&e.tagName!==`FIELDSET`&&(e.style.display=`none`)})}onData(e){let t={};Object.keys(e.detail.data).forEach(n=>{this.data.hasOwnProperty(n)&&(t[n]=e.detail.data[n])}),JSON.stringify(t)!==JSON.stringify(this.data)&&Object.assign(this.data,t)}};function w(e){return[e,...e.querySelectorAll(`*`)].filter(e=>!!x(e).length).filter(e=>e?.tagName!==`STYLE`).reverse()}function T(e){w(e).forEach(e=>{e?._mc?._placeholders||b.add(e,`_placeholders`,new C(e))})}function E(e){return typeof e==`object`&&!!e}var D={};function O(t){let n=t.constructor,i=n.name,a=class extends n{#notify=!0;#callbacks=[];constructor(e){super(e);let t=this.constructor;t.callbacks=[],E(e)&&Object.getOwnPropertyNames(e).forEach(t=>{this[t]=e[t]})}get callbacks(){return this.#callbacks}set callbacks(e){this.#callbacks=e}[Symbol.toPrimitive](e){let t=this.valueOf();return e===`string`?String(t):e===`number`?Number(t):t}subscribe(e){return this.#callbacks.push(e),this}unsubscribe(e){return this.#callbacks=this.#callbacks.filter(t=>t!==e),this}bind(t){if(e()){if(E(this.valueOf())){T(t);let e=[t,...t.querySelectorAll(`[data-mc]`)].filter(e=>!!e?._mc?._placeholders);this.#callbacks.push(function(t){let n=r(t);e.forEach(e=>{e&&d(e,`data`,n)})}),this.notify()}else console.warn(`IGNORED - cannot bind a non-Object to the dom, IS:`,typeof this.valueOf(),this);return this}}notify(e){let t=E(this.valueOf())&&!(this.valueOf()instanceof Array)?structuredClone(Object.assign({},this)):this.valueOf();return e!==void 0&&(this.#notify=e),this.#notify&&this.#callbacks.forEach(e=>{e(t)}),this}};return D[i]=a,a}function k(e){return D[e.constructor.name]||O(e)}function A(e){return(typeof e==`object`||typeof e==`function`)&&e!==null&&`subscribe`in e}function j(e,t){let n=e;return(t?[t]:[...Object.getOwnPropertyNames(e)]).forEach(t=>{let r=n[t];if(Object.prototype.hasOwnProperty.call(e,t)&&typeof r!=`function`&&!A(r)){if(t[0]===`_`||r===void 0)return;let a=r,s=new(k(a))(a),c=function(){queueMicrotask(function(){s.notify()})};Object.defineProperty(e,t,{enumerable:!0,get(){if(E(s.valueOf())){let e=structuredClone(Object.assign({},s));queueMicrotask(function(){i(structuredClone(Object.assign({},s)),e)||c()})}return s},set(e){if(typeof e==typeof s.valueOf())if(E(s.valueOf())&&!(s.valueOf()instanceof Array))A(s)||(s=new(k(e))(structuredClone(Object.assign({},e))),o(e,s)),Object.assign(s,e),s.notify();else{let t=s.valueOf(),n=s.callbacks;e!==t&&(s=new(k(e))(e),s.callbacks=n,c())}else console.warn(`IGNORED - cannot change observable types, IS:`,typeof s.valueOf(),s,`- CANNOT BECOME:`,typeof e)}}),n[t]=a}}),e}var M=e=>e.tagName.toLowerCase(),N=class{constructor(e){}},P=class extends N{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.form?new FormData(this.#e.form).get(this.#e.name)??null:null}set val(e){let t=[...this.#e.form?.querySelectorAll(`input[type="radio"][name="${this.#e.name}"]`)??[]];t.filter(t=>t.getAttribute(`value`)===e).shift().checked=!0}},F=class extends N{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.checked}set val(e){this.#e.checked=e}},I=class extends N{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.value}set val(e){this.#e.value=e}},L=class extends N{#e;#multi=!1;constructor(e){super(e),this.#e=e,this.#multi=e.multiple}get val(){let e=[];return[...this.#e.querySelectorAll(`option`)].filter(e=>e.selected).forEach(t=>{e.push(t.value)}),this.#multi?e.join(`,`):e[0]}set val(e){e=typeof e==`string`?[e]:e,[...this.#e.querySelectorAll(`option`)].forEach(t=>{e.indexOf(t.value)>-1?t.selected=!0:t.selected=!1})}},R=class extends N{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.value||``}set val(e){this.#e.value=e}},z=e=>{let t=[...(e||document).querySelectorAll(`input,select,textarea`)],n={};return t.forEach(e=>{let t=e.getAttribute(`name`);if(t){let r=!1;switch(M(e)){case`input`:switch(e.type){case`radio`:r=new P(e);break;case`checkbox`:r=new F(e);break;default:r=new I(e)}break;case`select`:r=new L(e);break;case`textarea`:r=new R(e);break}if(r){let i=r;b.add(e,t,i),Object.defineProperty(n,t,{enumerable:!0,configurable:!0,get:()=>i.val,set:e=>{i.val!==e&&(i.val=e)}})}}}),n};function B(t,n,r){if(!e())return;let i=z(r),a=t;a[n]=Object.assign({},i),j(t,n);let s=a[n];o(i,s),[...r.querySelectorAll(`input, textarea, select`)].forEach(e=>{e.addEventListener(`input`,()=>{s.notify()})})}export{B as formValues};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
function e(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}export{e as isMcDOM};
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
export * from './helpers.js';
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
1
|
+
export * from './utils/helpers.js';
|
|
2
|
+
export * from './core/mcGuards.js';
|
|
3
|
+
export * from './core/MC.js';
|
|
4
|
+
export * from './core/autoload.js';
|
|
5
|
+
export * from './core/events.js';
|
|
6
|
+
export * from './core/elements.js';
|
|
7
|
+
export * from './state/observables.js';
|
|
8
|
+
export * from './state/formValues.js';
|
|
6
9
|
//# sourceMappingURL=microcomponents.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"microcomponents.d.ts","sourceRoot":"","sources":["../src/microcomponents.ts"],"names":[],"mappings":"AACA,cAAc,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"microcomponents.d.ts","sourceRoot":"","sources":["../src/microcomponents.ts"],"names":[],"mappings":"AACA,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC"}
|
package/dist/microcomponents.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
export * from './helpers.js';
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
2
|
+
export * from './utils/helpers.js';
|
|
3
|
+
export * from './core/mcGuards.js';
|
|
4
|
+
export * from './core/MC.js';
|
|
5
|
+
export * from './core/autoload.js';
|
|
6
|
+
export * from './core/events.js';
|
|
7
|
+
export * from './core/elements.js';
|
|
8
|
+
export * from './state/observables.js';
|
|
9
|
+
export * from './state/formValues.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var t={d:(e,n)=>{for(var r in n)t.o(n,r)&&!t.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:n[r]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};function n(){return!!window}function r(t){return t&&t.constructor&&t.constructor===Object}function s(t){let e={};return[...Object.keys(t)].sort().forEach((n=>{r(t[n])?e[n]=s(t[n]):e[n]=t[n]})),e}function a(t,e=!0){let n={};return[...Object.getOwnPropertyNames(t)].forEach((e=>{if(t[e]instanceof Object){const r=a(t[e],!1);n[e]={};for(const t in r)n[e+"."+t]=r[t]}else n[e]=t[e]})),e?s(n):n}function c(t,e){return JSON.stringify(a(t))===JSON.stringify(a(e))}function o(t,e={}){let n=t.constructor===String?t.split("."):t;if(e&&n[0]&&e[n[0]])return e[n[0]].constructor===Object&&n.length>1?o(n.splice(1),e[n[0]]):e[n[0]];console.warn("ns",n[0],"not in",e)}function i(t,e){var n=Array.from(arguments);if(n.length>2){for(;n.length>1;)n[0]=i(n[0],n[1]),n.splice(1,1);return n[0]}if("string"==typeof t)t.match(/\{[^\{\}]*\}/g)?.forEach((function(n){var r=o(n.substring(1,n.length-1),e);void 0!==r&&(t=t.replace(n,r))}));else if(r(t))switch(t.constructor){case Object:Object.keys(t).forEach((function(n){t.hasOwnProperty(n)&&(t[n]=i(t[n],e))}));break;case Array:t.forEach((function(n,r,s){s[r]=i(t[r],e)}))}return t}function l(t){return t.split("-").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join("")}function u(t,e){let n;return()=>{clearTimeout(n),n=setTimeout((function(){t(...arguments)}),e)}}function d(t){let e=document.createElement("template");return t=t.replace(/<([A-Za-z0-9\-]*)([^>\/]*)(\/>)/gi,"<$1$2></$1>"),e.innerHTML=t,e.content.normalize(),Array.from(e.content.childNodes)}function f(t,e){const n=Object.getOwnPropertyDescriptors(t);for(const[t,r]of Object.entries(n))("get"in r||"set"in r)&&Object.defineProperty(e,t,r)}function h(t){return"_mc"+t[0].toUpperCase()+t.substring(1)}function m(t,e,n,r=!1){return t.addEventListener(h(e),n,{capture:!1,once:r}),n}t.d(e,{DC:()=>O,MC:()=>v,Oc:()=>E,E_:()=>_,IT:()=>f,sg:()=>u,bD:()=>c,c2:()=>S,YO:()=>a,Rc:()=>F,tn:()=>N,qS:()=>d,Bd:()=>n,nz:()=>r,TK:()=>l,k5:()=>o,lB:()=>P,qg:()=>i,mf:()=>s});class p{stopped=!1;constructor(){}stop(){this.stopped=!0}}function b(t,e,n={},r="l",s){const a=/^_mc/.test(e)?e:h(e),c=s||new p,o=new CustomEvent(a,{detail:{data:n,control:c},bubbles:!1});c.stopped||/l/.test(r)&&(t.dispatchEvent(o),o.stopImmediatePropagation(),c.stopped)||/[ud]/.test(r)&&(-1===r.indexOf("l")&&(r+="l"),setTimeout((()=>{if(r.indexOf("u")>-1){let e=t.parentElement?.closest("[data-mc]");e&&e?._mc.trigger(a,n,r,c)}if(r.indexOf("d")>-1){let e=t._mc.children();e&&e.forEach((t=>t.trigger(a,n,r)))}}),0))}const g=new Function("try {return this===window;}catch(e){ return false;}");function y(t){return t&&t instanceof HTMLElement}class E{_target;constructor(t){var e;this._target=new WeakRef(t||{}),function(t){if(!g()||t.constructor===O)return;const e=t.target;if(e instanceof HTMLElement){let n=t;Object.getOwnPropertyNames(n.constructor.prototype).filter((t=>/^(on[A-Z]|one[A-Z])/.test(t)&&"function"==typeof n.constructor.prototype[t])).forEach((t=>{let r=/^one/.test(t),s=t.replace(/^on[e]{0,1}/,""),a=s.toLowerCase();if("on"+a in e){let r=e=>{n[t](e)};e.addEventListener(a,r)}else m(e,s,(e=>{n[t](e)}),r)}))}}(this),e=this,setTimeout((()=>{e.constructor!==O&&e.trigger("Init")}),0)}get target(){return this._target.deref()}}class v extends E{constructor(t){super(t)}}class O extends E{constructor(t){super(t)}static add(t,e,n={}){return function(t,e,n={}){let r=t;r._mc||(r._mc=new O(t));const s=r._mc;return e.length&&!s[e]&&(s[e]=n,t instanceof HTMLElement&&!t.getAttribute("data-mc")&&t.setAttribute("data-mc","")),r}(t,e,n)}static remove(t,e){!function(t,e){if(!e)return;const n=t?._mc||{};n&&n[e]&&delete n[e],!Object.keys(n).length&&t instanceof HTMLElement&&t.hasAttribute("data-mc")&&t.removeAttribute("data-mc")}(t,e)}off(t,e){!function(t,e,n){t.removeEventListener(h(e),n)}(this.target,t,e)}on(t,e){return m(this.target,t,e)}one(t,e){return function(t,e,n){return m(t,e,n,!0),n}(this.target,t,e)}trigger(t,e={},n="l",r){b(this.target,t,e,n,r)}ancestors(){let t=this?.target,e=[];if(!y(t))return[];for(;t=t.closest("[data-mc]");)t?._mc&&e.push(t?._mc);return e.length?e:null}closest(){let t=this.target;return y(t)&&t.closest("[data-mc]")?._mc||null}children(){let t=this?.target;return y(t)?function(t){const e=Math.random().toString().replace(".",""),n='[data-mc]:not([temp_id="'+e+'"] [data-mc] [data-mc])';t.setAttribute("temp_id",e);const r=[...t.querySelectorAll(n)].filter((t=>!!t._mc)).map((t=>t._mc));return t.removeAttribute("temp_id"),r.length?r:null}(t):null}descendants(){const t=this?.target;if(!y(t))return[];const e=[...t.querySelectorAll("[data-mc]")].filter((t=>!!t._mc)).map((t=>t._mc));return e.length?e:null}}const w=new Set;function A(t){return()=>{const e=Array.from(document.querySelectorAll(`[data-mc*="${t}"]`)).filter((t=>t instanceof HTMLElement));if(0===e.length)return;const n=customElements.get(t);e.forEach((e=>{try{O.add(e,n.name,new n(e))}catch(t){if(!n.create)throw`Class ${n.name} doesn't have a callable constructor AND class doesn't have a static .create() method.`;{let t=n.create(e);t.setAttribute("data-mc",e.getAttribute("data-mc")),e.replaceWith(t),e=t}}const r=e.getAttribute("data-mc").split(" ").filter((e=>e!==t&&""!==e)).join(" ").trim();e.setAttribute("data-mc",r)}))}}function C(t){if(w.has(t))return;w.add(t);const e="./"+t.split("-").join("/")+".js",n=document.createElement("script");n.setAttribute("src",e),n.setAttribute("blocking","render"),n.async=!0,n.setAttribute("type","module"),n.setAttribute("data-name",t),n.setAttribute("loading",""),customElements.whenDefined(t).then(A(t)),n.addEventListener("load",(e=>{w.delete(t),document.head.querySelector(`script[data-name="${t}"]`)?.remove()})),n.addEventListener("error",(t=>{console.error("could not load DC code from",e)})),document.head.append(n)}function k(){[...document.querySelectorAll(":not(:defined)")].filter((t=>!!t&&!customElements.get(t.tagName.toLowerCase())&&!w.has(t.tagName.toLowerCase()))).forEach((t=>{C(t.tagName.toLowerCase())})),[...document.querySelectorAll('[data-mc]:not([data-mc=""]')].filter((t=>!!t)).forEach((t=>{const e=t.getAttribute("data-mc")?.split(" ").filter((t=>""!==t));e?.forEach((t=>{const e=customElements.get(t);w.has(t)||e?e&&A(t)():C(t)}))}))}let j=null;function _(t=!0){return t?(j||(j=new MutationObserver(k),setTimeout((()=>{j.observe(document.body,{childList:!0,subtree:!0}),k()}),5),window.autoload=window.autoload||_),!0):(j&&(j.disconnect(),j=null),!1)}function S(t,e,n=[]){if(customElements.get(t))return void console.warn(`IGNORED: Custom element "${t}" is already defined!`);if(!t.includes("-"))throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');let r=l(t);e.prototype instanceof E?customElements.define(t,function(t,e){return Object.defineProperty(e,"name",{value:t})}(r,class extends HTMLElement{constructor(t){super(),O.add(this,r,new e(this,t))}connectedCallback(){this.dispatchEvent(new CustomEvent("_mcConnected"))}connectedMoveCallback(){this.dispatchEvent(new CustomEvent("_mcConnectedMove"))}disconnectedCallback(){this.dispatchEvent(new CustomEvent("_mcDisconnected"))}adoptedCallback(){this.dispatchEvent(new CustomEvent("_mcAdopted"))}attributeChangedCallback(t,e,n){this.dispatchEvent(new CustomEvent("_mcAttributeChanged",{detail:{name:t,oldValue:e,newValue:n}}))}formResetCallback(){this.dispatchEvent(new CustomEvent("_mcFormReset"))}static get observedAttributes(){return n}})):customElements.define(t,e)}function N(t){return customElements.get(t)}function T(t){return null!=t&&"object"==typeof t}function L(t){let e=new Set,n=/\{[^\{\}]*\}/g;return(t.getAttributeNames?t.getAttributeNames():[]).filter((e=>t.getAttribute(e)?.match(n))).forEach((r=>{new Set(t.getAttribute(r)?.match(n)).forEach((t=>e.add(t.replace(/[\{\}]/g,""))))})),[...t.childNodes].filter((t=>3===t.nodeType&&(t.nodeValue||"").match(n))).forEach((t=>{(t?.nodeValue||"").match(n).forEach((t=>e.add(t.replace(/[\{\}]/g,""))))})),[...e]}_.state=w;const D=[HTMLInputElement,HTMLSelectElement,HTMLTextAreaElement];class x extends O{data={};oldDisplay="";constructor(t){super(t),L(t).forEach((t=>this.data[t]="")),P(this),this.#t()}#t(){let t=this.target,e=!1;(t.getAttributeNames?t.getAttributeNames():[]).filter((e=>t.getAttribute(e)?.match(/\{[^\{\}]*\}/g))).forEach((e=>{let n=new Set(t.getAttribute(e)?.match(/\{[^\{\}]*\}/g));var r;this.data.subscribe((r=t.getAttribute(e),s=>{let a=r;n.forEach((t=>{a=a.replace(t,s[t.replace(/[\{\}]/g,"")])})),t.setAttribute(e,a)}))})),[...t.childNodes].filter((t=>3===t.nodeType&&(t.nodeValue||"").match(/\{[^\{\}]*\}/g))).forEach((n=>{e=!0;let r=new Set(n.nodeValue.match(/\{[^\{\}]*\}/g));this.data.subscribe((e=>s=>{let a=e;r.forEach((t=>{a=a.replace(t,s[t.replace(/[\{\}]/g,"")])})),this.oldDisplay?t.style=this.oldDisplay:t.removeAttribute("style"),delete this.oldDisplay,n.nodeValue=a})(n.nodeValue)),this.oldDisplay=t.getAttribute("style")||"",e&&!D.includes(t.constructor)&&"FIELDSET"!==t.tagName&&(t.style.display="none")}))}onData(t){let e={};Object.keys(t.detail.data).forEach((n=>{this.data.hasOwnProperty(n)&&(e[n]=t.detail.data[n])})),JSON.stringify(e)!==JSON.stringify(this.data)&&Object.assign(this.data,e)}}const M={};function q(t){let e=t.constructor.name;return M[e]||function(t){const e=t.constructor,n=e.name;let r=class extends e{#e=!0;#n=[];constructor(t){super(t),this.constructor.callbacks=[],T(t)&&Object.getOwnPropertyNames(t).forEach((e=>{this[e]=t[e]}))}get callbacks(){return this.#n}set callbacks(t){this.#n=t}subscribe(t){return this.#n.push(t),this}bind(t){if(T(this.valueOf())){!function(t){var e;(e=t,[e].concat([...e.querySelectorAll("*")]).filter((t=>!!L(t).length)).filter((t=>"STYLE"!==t?.tagName)).reverse()).forEach((t=>{t?._mc?._placeholders||O.add(t,"_placeholders",new x(t))}))}(t);const e=[t,...t.querySelectorAll("[data-mc]")].filter((t=>!!t?._mc?._placeholders)),n=function(t){let n=a(t);e.forEach((t=>{t&&b(t,"data",n)}))};this.#n.push(n),this.notify()}else console.warn("IGNORED - cannot bind a non-Object to the dom, IS:",typeof this.valueOf(),this);return this}notify(t){let e=T(this.valueOf())&&this.valueOf()instanceof Array==0?structuredClone(Object.assign({},this)):this.valueOf();return void 0!==t&&(this.#e=t),this.#e&&this.#n.forEach((t=>{t(e)})),this}};return M[n]=r,r}(t)}const P=(t,e)=>((e?[e]:[...Object.getOwnPropertyNames(t)]).forEach((e=>{if(t.hasOwnProperty(e)&&"function"!=typeof t[e]&&!t?.[e]?.constructor?.prototype?.subscribe){if("_"===e[0]||void 0===t?.[e])return t;let n=t[e],r=new(q(n))(n),s=u((function(){r.notify()}),0);Object.defineProperty(t,e,{enumerable:!0,get(){if(T(r.valueOf())){let t=structuredClone(Object.assign({},r));setTimeout((function(){c(structuredClone(Object.assign({},r)),t)||s()}),0)}return r},set(t){if(typeof t==typeof r.valueOf())if(T(r.valueOf())&&r.valueOf()instanceof Array==0)void 0===r?.constructor?.prototype?.subscribe&&(r=new(q(t))(structuredClone(Object.assign({},t))),f(t,r)),Object.assign(r,t),r.notify();else{let e=r.valueOf(),n=r.callbacks;t!==e&&(r=new(q(t))(t),r.callbacks=n,s())}else console.warn("IGNORED - cannot change observable types, IS:",typeof r.valueOf(),r,"- CANNOT BECOME:",typeof t)}}),t[e]=n}})),t);class I{constructor(t){}}class H extends I{#r;constructor(t){super(t),this.#r=t}get val(){return new FormData(this.#r.form).get(this.#r.name)}set val(t){[...this.#r.form.querySelectorAll(`input[type="radio"][name="${this.#r.name}"]`)].filter((e=>e.getAttribute("value")===t)).shift().checked=!0}}class V extends I{#r;constructor(t){super(t),this.#r=t}get val(){return this.#r.checked}set val(t){this.#r.checked=t}}class R extends I{#r;constructor(t){super(t),this.#r=t}get val(){return this.#r.value}set val(t){this.#r.value=t}}class $ extends I{#r;#s=!1;constructor(t){super(t),this.#r=t,this.#s=t.multiple}get val(){const t=[];return[...this.#r.querySelectorAll("option")].filter((t=>t.selected)).forEach((e=>{t.push(e.value)})),this.#s?t:t.length?t[0]:""}set val(t){t="string"==typeof t?[t]:t,[...this.#r.querySelectorAll("option")].forEach((e=>{t.indexOf(e.value)>-1?e.selected=!0:e.selected=!1}))}}class B extends I{#r;constructor(t){super(t),this.#r=t}get val(){return this.#r.value||""}set val(t){this.#r.value=t}}const z=t=>{let e=[...(t||document).querySelectorAll("input,select,textarea")],n={};return e.forEach((t=>{const e=t.getAttribute("name");if(e){let r=!1;switch((t=>t.tagName.toLowerCase())(t)){case"input":switch(t.type){case"radio":r=new H(t);break;case"checkbox":r=new V(t);break;default:r=new R(t)}break;case"select":r=new $(t);break;case"textarea":r=new B(t);break;default:console.warn("handler missing:",e,"for",t.tagName.toLowerCase(),t)}r&&(O.add(t,e,r),Object.defineProperty(n,e,{enumerable:!0,configurable:!0,get:()=>r.val,set:t=>{r.val!==t&&(r.val=t)}}))}})),n};function F(t,e,n){let r=z(n);t[e]=Object.assign({},r),P(t,e),f(r,t[e]),[...n.querySelectorAll("input, textarea, select")].forEach((n=>{n.addEventListener("input",(n=>{t[e].notify()}))}))}var G=e.DC,J=e.MC,Y=e.Oc,Z=e.E_,K=e.IT,U=e.sg,W=e.bD,Q=e.c2,X=e.YO,tt=e.Rc,et=e.tn,nt=e.qS,rt=e.Bd,st=e.nz,at=e.TK,ct=e.k5,ot=e.lB,it=e.qg,lt=e.mf;export{G as DC,J as MC,Y as McBase,Z as autoload,K as copyGettersSetters,U as debounce,W as deepEqual,Q as defineCE,X as flattenObject,tt as formValues,et as getCE,nt as htmlToElements,rt as isBrowser,st as isNativeObject,at as kebabToPascal,ct as nameSpace,ot as observe,it as parse,lt as sortObject};
|
|
1
|
+
function e(){return typeof window<`u`&&window.document!==void 0}function t(e){return typeof e==`object`&&!!e&&e.constructor===Object}function n(e){let r={},i=Object.keys(e).sort();for(let a=0;a<i.length;a++){let o=i[a],s=e[o];t(s)?r[o]=n(s):r[o]=s}return r}function r(e,t=!0){let i={},a=e,o=Object.getOwnPropertyNames(e);for(let e=0;e<o.length;e++){let t=o[e],n=a[t];if(n instanceof Object){let e=r(n,!1);for(let n in e)i[t+`.`+n]=e[n]}else i[t]=n}return t?n(i):i}function i(e,t){if(e===t||e!==e&&t!==t)return!0;if(typeof e!=`object`||!e||typeof t!=`object`||!t||e.constructor!==t.constructor)return!1;if(e instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp)return e.toString()===t.toString();if(Array.isArray(e)){let n=t;if(e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(!i(e[t],n[t]))return!1;return!0}let n=e,r=t,a=Object.keys(n);if(a.length!==Object.keys(r).length)return!1;for(let e=0;e<a.length;e++){let t=a[e];if(!Object.prototype.hasOwnProperty.call(r,t)||!i(n[t],r[t]))return!1}return!0}function a(e,n={}){let r=n,i=typeof e==`string`?e.split(`.`):e;if(!(!r||!i[0]||!r[i[0]])){if(i.length>1){let e=r[i[0]];return t(e)?a(i.slice(1),e):void 0}return r[i[0]]}}function o(e,n,...r){let i=[e,n,...r];if(i.length>2){for(;i.length>1;)i[0]=o(i[0],i[1]),i.splice(1,1);return i[0]}if(typeof e==`string`){let t=e;return e.match(/\{[^\{\}]*\}/g)?.forEach(e=>{let r=a(e.substring(1,e.length-1),n);r!==void 0&&(t=t.replace(e,String(r)))}),t}else if(t(e)){let t=e;Object.keys(t).forEach(e=>{Object.prototype.hasOwnProperty.call(t,e)&&(t[e]=o(t[e],n))})}return e}function s(e){return e.split(`-`).map(e=>e.charAt(0).toUpperCase()+e.slice(1)).join(``)}function c(e,t){let n;return(...r)=>{clearTimeout(n),n=setTimeout(()=>{e(...r)},t)}}function l(e){let t=document.createElement(`template`);return e=e.replace(/<([A-Za-z0-9\-]*)([^>\/]*)(\/>)/gi,`<$1$2></$1>`),t.innerHTML=e,t.content.normalize(),Array.from(t.content.childNodes)}function u(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function d(e,t){let n=Object.getOwnPropertyDescriptors(e);for(let[e,r]of Object.entries(n))(`get`in r||`set`in r)&&Object.defineProperty(t,e,r)}function f(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}function p(e){return`_mc`+e[0].toUpperCase()+e.substring(1)}function m(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>m(e,t,n));return}e.removeEventListener(u(e,t),n)}}function h(e,t,n,r=!1){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>h(e,t,n));return}return e.addEventListener(u(e,t),n,{capture:!1,once:r}),n}}function g(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>g(e,t,n));return}return h(e,t,n,!0),n}}var _=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function v(e,t,n={},r=`l`,i){if(!e)return;if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>v(e,t,n,r));return}let a=/^_mc/.test(t)?t:u(e,t),o=i||new _,s=new CustomEvent(a,{detail:{data:n,control:o},bubbles:!1});o.stopped||/l/.test(r)&&(e.dispatchEvent(s),o.stopped)||/[ud]/.test(r)&&queueMicrotask(()=>{if(r.indexOf(`u`)>-1&&v(e.parentElement,a,n,`lu`,o),r.indexOf(`d`)>-1)for(let t of e.children)v(t,a,n,`ld`)})}function y(e,t,n={}){let r=e;`_mc`in e||(r._mc=new D(e));let i=r._mc;return t.length&&!i[t]&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function b(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function x(e){queueMicrotask(()=>{e.trigger(`Init`)})}function S(e){let t=e.target,n=Object.getPrototypeOf(e),r=e,i=Object.getOwnPropertyNames(n).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof n[e]==`function`);for(let e of i){let n=/^one/.test(e);h(t,e.replace(/^one?/,``),t=>r[e](t),n)}}function C(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function w(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var T=class{_target;constructor(e){this._target=new WeakRef(e||{}),this instanceof D&&(S(this),x(this))}get target(){return this._target.deref()}},E=class extends T{constructor(e){super(e)}},D=class extends T{constructor(e){super(e)}static add(e,t,n={}){return y(e,t,n)}static remove(e,t){b(e,t)}static get(e,t,n){if(!f(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,t){m(this.target,e,t)}on(e,t){return h(this.target,e,t)}one(e,t){return g(this.target,e,t)}trigger(e,t={},n=`l`,r){v(this.target,e,t,n,r)}ancestors(e){let t=[],n=w(this.target,e);for(;n;)t.push(n),n=w(n.target,e);return t}closest(e){return w(this.target,e)}children(e){let t=C(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}},O=new Set;function k(e){return()=>{let t=Array.from(document.querySelectorAll(`[data-mc*="${e}"]`)).filter(e=>e instanceof HTMLElement);if(t.length===0)return;let n=customElements.get(e);for(let r of t){try{D.add(r,n.name,new n(r))}catch{if(`create`in n&&typeof n.create==`function`){let e=n.create(r);if(!f(e))throw`create() of Class ${n.name} did not return a McHost.`;e.setAttribute(`data-mc`,r.getAttribute(`data-mc`)??``),r.replaceWith(e),r=e}else throw`Class ${n.name} doesn't have a callable constructor AND class doesn't have a static .create() method.`}let t=r.getAttribute(`data-mc`).split(` `).filter(t=>t!==e&&t!==``).join(` `).trim();r.setAttribute(`data-mc`,t)}}}function A(e){if(O.has(e))return;O.add(e);let t=`./`+e.split(`-`).join(`/`)+`.js`,n=document.createElement(`script`);n.setAttribute(`src`,t),n.setAttribute(`blocking`,`render`),n.async=!0,n.setAttribute(`type`,`module`),n.setAttribute(`data-name`,e),n.setAttribute(`loading`,``),customElements.whenDefined(e).then(k(e)),n.addEventListener(`load`,t=>{O.delete(e),document.head.querySelector(`script[data-name="${e}"]`)?.remove()}),n.addEventListener(`error`,e=>{console.error(`could not load DC code from`,t)}),document.head.append(n)}function j(){[...document.querySelectorAll(`:not(:defined)`)].filter(e=>!!e&&!customElements.get(e.tagName.toLowerCase())&&!O.has(e.tagName.toLowerCase())).forEach(e=>{A(e.tagName.toLowerCase())}),[...document.querySelectorAll(`[data-mc]:not([data-mc=""]`)].filter(e=>!!e).forEach(e=>{(e.getAttribute(`data-mc`)?.split(` `).filter(e=>e!==``))?.forEach(e=>{let t=customElements.get(e);!O.has(e)&&!t?A(e):t&&k(e)()})})}var M=null,N=function(e=!0){return e?M?!0:(M=new MutationObserver(j),setTimeout(()=>{M.observe(document.body,{childList:!0,subtree:!0}),j()},5),window.autoload??=N,!0):(M&&=(M.disconnect(),null),!1)};N.state=O;function P(e,t){return Object.defineProperty(t,"name",{value:e})}function F(e){return class extends e{constructor(...e){super(...e)}}}function I(e,t,n=[],r){if(customElements.get(e)){console.warn(`IGNORED: Custom element "${e}" is already defined!`);return}if(!e.includes(`-`))throw Error(`createCustomElement: tagName must contain at least one hyphen ("-").`);let i=HTMLElement;r&&(i=document.createElement(r).constructor);let a=s(e);if(!(t.prototype instanceof T))return customElements.define(e,t),t;let o=P(a,class extends F(i){constructor(e){super(),D.add(this,a,new t(this,e))}connectedCallback(){this.dispatchEvent(new CustomEvent(`_mcConnected`))}connectedMoveCallback(){this.dispatchEvent(new CustomEvent(`_mcConnectedMove`))}disconnectedCallback(){this.dispatchEvent(new CustomEvent(`_mcDisconnected`))}adoptedCallback(){this.dispatchEvent(new CustomEvent(`_mcAdopted`))}attributeChangedCallback(e,t,n){this.dispatchEvent(new CustomEvent(`_mcAttributeChanged`,{detail:{name:e,oldValue:t,newValue:n}}))}formResetCallback(){this.dispatchEvent(new CustomEvent(`_mcFormReset`))}static get observedAttributes(){return[...n]}});return customElements.define(e,o,{extends:r}),o}function L(e){return customElements.get(e)}function R(e){let t=new Set,n=/\{[^\{\}]*\}/g;return(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(n)).forEach(r=>{new Set(e.getAttribute(r)?.match(n)).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(n)).forEach(e=>{(e?.nodeValue||``).match(n).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...t]}var z=[`INPUT`,`SELECT`,`TEXTAREA`],B=class extends D{data={};oldDisplay=``;constructor(e){super(e),R(e).forEach(e=>this.data[e]=``),J(this),this.#attachCallbacks()}#attachCallbacks(){let e=this.target,t=!1;(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)).forEach(t=>{let n=new Set(e.getAttribute(t)?.match(/\{[^\{\}]*\}/g));this.data.subscribe((r=>i=>{let a=r;n.forEach(e=>{a=a.replace(e,String(i[e.replace(/[\{\}]/g,``)]))}),e.setAttribute(t,a)})(e.getAttribute(t)??``))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(/\{[^\{\}]*\}/g)).forEach(n=>{t=!0;let r=new Set((n.nodeValue??``).match(/\{[^\{\}]*\}/g));this.data.subscribe((t=>i=>{let a=t;r.forEach(e=>{a=a.replace(e,String(i[e.replace(/[\{\}]/g,``)]))}),this.oldDisplay?e.style=this.oldDisplay:e.removeAttribute(`style`),delete this.oldDisplay,n.nodeValue=a})(n.nodeValue??``)),this.oldDisplay=e.getAttribute(`style`)||``,t&&!z.includes(e.tagName)&&e.tagName!==`FIELDSET`&&(e.style.display=`none`)})}onData(e){let t={};Object.keys(e.detail.data).forEach(n=>{this.data.hasOwnProperty(n)&&(t[n]=e.detail.data[n])}),JSON.stringify(t)!==JSON.stringify(this.data)&&Object.assign(this.data,t)}};function V(e){return[e,...e.querySelectorAll(`*`)].filter(e=>!!R(e).length).filter(e=>e?.tagName!==`STYLE`).reverse()}function H(e){V(e).forEach(e=>{e?._mc?._placeholders||D.add(e,`_placeholders`,new B(e))})}function U(e){return typeof e==`object`&&!!e}var W={};function G(t){let n=t.constructor,i=n.name,a=class extends n{#notify=!0;#callbacks=[];constructor(e){super(e);let t=this.constructor;t.callbacks=[],U(e)&&Object.getOwnPropertyNames(e).forEach(t=>{this[t]=e[t]})}get callbacks(){return this.#callbacks}set callbacks(e){this.#callbacks=e}[Symbol.toPrimitive](e){let t=this.valueOf();return e===`string`?String(t):e===`number`?Number(t):t}subscribe(e){return this.#callbacks.push(e),this}unsubscribe(e){return this.#callbacks=this.#callbacks.filter(t=>t!==e),this}bind(t){if(e()){if(U(this.valueOf())){H(t);let e=[t,...t.querySelectorAll(`[data-mc]`)].filter(e=>!!e?._mc?._placeholders);this.#callbacks.push(function(t){let n=r(t);e.forEach(e=>{e&&v(e,`data`,n)})}),this.notify()}else console.warn(`IGNORED - cannot bind a non-Object to the dom, IS:`,typeof this.valueOf(),this);return this}}notify(e){let t=U(this.valueOf())&&!(this.valueOf()instanceof Array)?structuredClone(Object.assign({},this)):this.valueOf();return e!==void 0&&(this.#notify=e),this.#notify&&this.#callbacks.forEach(e=>{e(t)}),this}};return W[i]=a,a}function K(e){return W[e.constructor.name]||G(e)}function q(e){return(typeof e==`object`||typeof e==`function`)&&e!==null&&`subscribe`in e}function J(e,t){let n=e;return(t?[t]:[...Object.getOwnPropertyNames(e)]).forEach(t=>{let r=n[t];if(Object.prototype.hasOwnProperty.call(e,t)&&typeof r!=`function`&&!q(r)){if(t[0]===`_`||r===void 0)return;let a=r,o=new(K(a))(a),s=function(){queueMicrotask(function(){o.notify()})};Object.defineProperty(e,t,{enumerable:!0,get(){if(U(o.valueOf())){let e=structuredClone(Object.assign({},o));queueMicrotask(function(){i(structuredClone(Object.assign({},o)),e)||s()})}return o},set(e){if(typeof e==typeof o.valueOf())if(U(o.valueOf())&&!(o.valueOf()instanceof Array))q(o)||(o=new(K(e))(structuredClone(Object.assign({},e))),d(e,o)),Object.assign(o,e),o.notify();else{let t=o.valueOf(),n=o.callbacks;e!==t&&(o=new(K(e))(e),o.callbacks=n,s())}else console.warn(`IGNORED - cannot change observable types, IS:`,typeof o.valueOf(),o,`- CANNOT BECOME:`,typeof e)}}),n[t]=a}}),e}var Y=e=>e.tagName.toLowerCase(),X=class{constructor(e){}},Z=class extends X{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.form?new FormData(this.#e.form).get(this.#e.name)??null:null}set val(e){let t=[...this.#e.form?.querySelectorAll(`input[type="radio"][name="${this.#e.name}"]`)??[]];t.filter(t=>t.getAttribute(`value`)===e).shift().checked=!0}},Q=class extends X{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.checked}set val(e){this.#e.checked=e}},$=class extends X{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.value}set val(e){this.#e.value=e}},ee=class extends X{#e;#multi=!1;constructor(e){super(e),this.#e=e,this.#multi=e.multiple}get val(){let e=[];return[...this.#e.querySelectorAll(`option`)].filter(e=>e.selected).forEach(t=>{e.push(t.value)}),this.#multi?e.join(`,`):e[0]}set val(e){e=typeof e==`string`?[e]:e,[...this.#e.querySelectorAll(`option`)].forEach(t=>{e.indexOf(t.value)>-1?t.selected=!0:t.selected=!1})}},te=class extends X{#e;constructor(e){super(e),this.#e=e}get val(){return this.#e.value||``}set val(e){this.#e.value=e}},ne=e=>{let t=[...(e||document).querySelectorAll(`input,select,textarea`)],n={};return t.forEach(e=>{let t=e.getAttribute(`name`);if(t){let r=!1;switch(Y(e)){case`input`:switch(e.type){case`radio`:r=new Z(e);break;case`checkbox`:r=new Q(e);break;default:r=new $(e)}break;case`select`:r=new ee(e);break;case`textarea`:r=new te(e);break}if(r){let i=r;D.add(e,t,i),Object.defineProperty(n,t,{enumerable:!0,configurable:!0,get:()=>i.val,set:e=>{i.val!==e&&(i.val=e)}})}}}),n};function re(t,n,r){if(!e())return;let i=ne(r),a=t;a[n]=Object.assign({},i),J(t,n);let o=a[n];d(i,o),[...r.querySelectorAll(`input, textarea, select`)].forEach(e=>{e.addEventListener(`input`,()=>{o.notify()})})}export{_ as BubbleController,D as DC,E as MC,T as McBase,N as autoload,d as copyGettersSetters,c as debounce,i as deepEqual,I as defineCE,r as flattenObject,re as formValues,L as getCE,p as getMcEventName,l as htmlToElements,q as isAlreadyObserved,e as isBrowser,f as isMcDOM,t as isNativeObject,s as kebabToPascal,a as nameSpace,J as observe,m as off,h as on,g as one,o as parse,n as sortObject,v as trigger,u as trueEventName};
|
package/dist/observables.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
function e(){return typeof window<`u`&&window.document!==void 0}function t(e){return typeof e==`object`&&!!e&&e.constructor===Object}function n(e){let r={},i=Object.keys(e).sort();for(let a=0;a<i.length;a++){let o=i[a],s=e[o];t(s)?r[o]=n(s):r[o]=s}return r}function r(e,t=!0){let i={},a=e,o=Object.getOwnPropertyNames(e);for(let e=0;e<o.length;e++){let t=o[e],n=a[t];if(n instanceof Object){let e=r(n,!1);for(let n in e)i[t+`.`+n]=e[n]}else i[t]=n}return t?n(i):i}function i(e,t){if(e===t||e!==e&&t!==t)return!0;if(typeof e!=`object`||!e||typeof t!=`object`||!t||e.constructor!==t.constructor)return!1;if(e instanceof Date)return e.getTime()===t.getTime();if(e instanceof RegExp)return e.toString()===t.toString();if(Array.isArray(e)){let n=t;if(e.length!==n.length)return!1;for(let t=0;t<e.length;t++)if(!i(e[t],n[t]))return!1;return!0}let n=e,r=t,a=Object.keys(n);if(a.length!==Object.keys(r).length)return!1;for(let e=0;e<a.length;e++){let t=a[e];if(!Object.prototype.hasOwnProperty.call(r,t)||!i(n[t],r[t]))return!1}return!0}function a(e,t){let n=t.toLowerCase();return`on`+n in e?n:`_mc`+t[0].toUpperCase()+t.substring(1)}function o(e,t){let n=Object.getOwnPropertyDescriptors(e);for(let[e,r]of Object.entries(n))(`get`in r||`set`in r)&&Object.defineProperty(t,e,r)}function s(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>s(e,t,n));return}e.removeEventListener(a(e,t),n)}}function c(e,t,n,r=!1){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>c(e,t,n));return}return e.addEventListener(a(e,t),n,{capture:!1,once:r}),n}}function l(e,t,n){if(e){if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>l(e,t,n));return}return c(e,t,n,!0),n}}var u=class{stopped=!1;constructor(){}stop(){this.stopped=!0}};function d(e,t,n={},r=`l`,i){if(!e)return;if(typeof e==`string`){document.querySelectorAll(e).forEach(e=>d(e,t,n,r));return}let o=/^_mc/.test(t)?t:a(e,t),s=i||new u,c=new CustomEvent(o,{detail:{data:n,control:s},bubbles:!1});s.stopped||/l/.test(r)&&(e.dispatchEvent(c),s.stopped)||/[ud]/.test(r)&&queueMicrotask(()=>{if(r.indexOf(`u`)>-1&&d(e.parentElement,o,n,`lu`,s),r.indexOf(`d`)>-1)for(let t of e.children)d(t,o,n,`ld`)})}function f(e,t){return typeof HTMLElement<`u`&&e instanceof HTMLElement&&`_mc`in e&&(t===void 0||t in e._mc)}function p(e,t,n={}){let r=e;`_mc`in e||(r._mc=new b(e));let i=r._mc;return t.length&&!i[t]&&(i[t]=n,e instanceof HTMLElement&&!e.getAttribute(`data-mc`)&&e.setAttribute(`data-mc`,``)),r}function m(e,t){if(!t)return;let n=e._mc;n&&(n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute(`data-mc`)&&e.removeAttribute(`data-mc`))}function h(e){queueMicrotask(()=>{e.trigger(`Init`)})}function g(e){let t=e.target,n=Object.getPrototypeOf(e),r=e,i=Object.getOwnPropertyNames(n).filter(e=>/^(on[A-Z]|one[A-Z])/.test(e)&&typeof n[e]==`function`);for(let e of i){let n=/^one/.test(e);c(t,e.replace(/^one?/,``),t=>r[e](t),n)}}function _(e){return Array.from(e.querySelectorAll(`[data-mc]:not(:scope [data-mc] [data-mc])`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc)}function v(e,t){let n=e.parentElement?.closest(`[data-mc]`);for(;n instanceof HTMLElement;){if(`_mc`in n){let e=n._mc;if(t===void 0||t in e)return e}n=n.parentElement?.closest(`[data-mc]`)}return null}var y=class{_target;constructor(e){this._target=new WeakRef(e||{}),this instanceof b&&(g(this),h(this))}get target(){return this._target.deref()}},b=class extends y{constructor(e){super(e)}static add(e,t,n={}){return p(e,t,n)}static remove(e,t){m(e,t)}static get(e,t,n){if(!f(e))return;let r=e._mc[t];return r instanceof n?r:void 0}off(e,t){s(this.target,e,t)}on(e,t){return c(this.target,e,t)}one(e,t){return l(this.target,e,t)}trigger(e,t={},n=`l`,r){d(this.target,e,t,n,r)}ancestors(e){let t=[],n=v(this.target,e);for(;n;)t.push(n),n=v(n.target,e);return t}closest(e){return v(this.target,e)}children(e){let t=_(this.target);return e===void 0?t:t.filter(t=>e in t)}descendants(e){return Array.from(this.target.querySelectorAll(`[data-mc]`)).filter(e=>e instanceof HTMLElement&&`_mc`in e).map(e=>e._mc).filter(t=>e===void 0||e in t)}};function x(e){let t=new Set,n=/\{[^\{\}]*\}/g;return(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(n)).forEach(r=>{new Set(e.getAttribute(r)?.match(n)).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(n)).forEach(e=>{(e?.nodeValue||``).match(n).forEach(e=>t.add(e.replace(/[\{\}]/g,``)))}),[...t]}var S=[`INPUT`,`SELECT`,`TEXTAREA`],C=class extends b{data={};oldDisplay=``;constructor(e){super(e),x(e).forEach(e=>this.data[e]=``),j(this),this.#attachCallbacks()}#attachCallbacks(){let e=this.target,t=!1;(e.getAttributeNames?e.getAttributeNames():[]).filter(t=>e.getAttribute(t)?.match(/\{[^\{\}]*\}/g)).forEach(t=>{let n=new Set(e.getAttribute(t)?.match(/\{[^\{\}]*\}/g));this.data.subscribe((r=>i=>{let a=r;n.forEach(e=>{a=a.replace(e,String(i[e.replace(/[\{\}]/g,``)]))}),e.setAttribute(t,a)})(e.getAttribute(t)??``))}),[...e.childNodes].filter(e=>e.nodeType===3&&(e.nodeValue||``).match(/\{[^\{\}]*\}/g)).forEach(n=>{t=!0;let r=new Set((n.nodeValue??``).match(/\{[^\{\}]*\}/g));this.data.subscribe((t=>i=>{let a=t;r.forEach(e=>{a=a.replace(e,String(i[e.replace(/[\{\}]/g,``)]))}),this.oldDisplay?e.style=this.oldDisplay:e.removeAttribute(`style`),delete this.oldDisplay,n.nodeValue=a})(n.nodeValue??``)),this.oldDisplay=e.getAttribute(`style`)||``,t&&!S.includes(e.tagName)&&e.tagName!==`FIELDSET`&&(e.style.display=`none`)})}onData(e){let t={};Object.keys(e.detail.data).forEach(n=>{this.data.hasOwnProperty(n)&&(t[n]=e.detail.data[n])}),JSON.stringify(t)!==JSON.stringify(this.data)&&Object.assign(this.data,t)}};function w(e){return[e,...e.querySelectorAll(`*`)].filter(e=>!!x(e).length).filter(e=>e?.tagName!==`STYLE`).reverse()}function T(e){w(e).forEach(e=>{e?._mc?._placeholders||b.add(e,`_placeholders`,new C(e))})}function E(e){return typeof e==`object`&&!!e}var D={};function O(t){let n=t.constructor,i=n.name,a=class extends n{#notify=!0;#callbacks=[];constructor(e){super(e);let t=this.constructor;t.callbacks=[],E(e)&&Object.getOwnPropertyNames(e).forEach(t=>{this[t]=e[t]})}get callbacks(){return this.#callbacks}set callbacks(e){this.#callbacks=e}[Symbol.toPrimitive](e){let t=this.valueOf();return e===`string`?String(t):e===`number`?Number(t):t}subscribe(e){return this.#callbacks.push(e),this}unsubscribe(e){return this.#callbacks=this.#callbacks.filter(t=>t!==e),this}bind(t){if(e()){if(E(this.valueOf())){T(t);let e=[t,...t.querySelectorAll(`[data-mc]`)].filter(e=>!!e?._mc?._placeholders);this.#callbacks.push(function(t){let n=r(t);e.forEach(e=>{e&&d(e,`data`,n)})}),this.notify()}else console.warn(`IGNORED - cannot bind a non-Object to the dom, IS:`,typeof this.valueOf(),this);return this}}notify(e){let t=E(this.valueOf())&&!(this.valueOf()instanceof Array)?structuredClone(Object.assign({},this)):this.valueOf();return e!==void 0&&(this.#notify=e),this.#notify&&this.#callbacks.forEach(e=>{e(t)}),this}};return D[i]=a,a}function k(e){return D[e.constructor.name]||O(e)}function A(e){return(typeof e==`object`||typeof e==`function`)&&e!==null&&`subscribe`in e}function j(e,t){let n=e;return(t?[t]:[...Object.getOwnPropertyNames(e)]).forEach(t=>{let r=n[t];if(Object.prototype.hasOwnProperty.call(e,t)&&typeof r!=`function`&&!A(r)){if(t[0]===`_`||r===void 0)return;let a=r,s=new(k(a))(a),c=function(){queueMicrotask(function(){s.notify()})};Object.defineProperty(e,t,{enumerable:!0,get(){if(E(s.valueOf())){let e=structuredClone(Object.assign({},s));queueMicrotask(function(){i(structuredClone(Object.assign({},s)),e)||c()})}return s},set(e){if(typeof e==typeof s.valueOf())if(E(s.valueOf())&&!(s.valueOf()instanceof Array))A(s)||(s=new(k(e))(structuredClone(Object.assign({},e))),o(e,s)),Object.assign(s,e),s.notify();else{let t=s.valueOf(),n=s.callbacks;e!==t&&(s=new(k(e))(e),s.callbacks=n,c())}else console.warn(`IGNORED - cannot change observable types, IS:`,typeof s.valueOf(),s,`- CANNOT BECOME:`,typeof e)}}),n[t]=a}}),e}export{A as isAlreadyObserved,j as observe};
|