@twobirds/microcomponents 0.1.18 → 0.1.19
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/MC.ts +11 -2
- package/dist/elements.ts +38 -54
- package/dist/observables.ts +10 -2
- package/package.json +4 -3
- package/dist/MC.d.ts +0 -39
- package/dist/MC.d.ts.map +0 -1
- package/dist/MC.js +0 -299
- package/dist/MC.min.js +0 -342
- package/dist/elements.d.ts +0 -5
- package/dist/elements.d.ts.map +0 -1
- package/dist/elements.js +0 -67
- package/dist/elements.min.js +0 -1
- package/dist/formValues.d.ts +0 -3
- package/dist/formValues.d.ts.map +0 -1
- package/dist/formValues.js +0 -158
- package/dist/formValues.min.js +0 -1
- package/dist/helpers.d.ts +0 -14
- package/dist/helpers.d.ts.map +0 -1
- package/dist/helpers.js +0 -111
- package/dist/microcomponents.d.ts +0 -6
- package/dist/microcomponents.d.ts.map +0 -1
- package/dist/microcomponents.js +0 -6
- package/dist/microcomponents.min.js +0 -1
- package/dist/observables.d.ts +0 -11
- package/dist/observables.d.ts.map +0 -1
- package/dist/observables.js +0 -228
- package/dist/observables.min.js +0 -1
package/dist/MC.ts
CHANGED
|
@@ -32,6 +32,8 @@ function add(target: HTMLElement, key: string, element: any = {}): HTMLMcElement
|
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
//console.warn('add() args: target, key, element', arguments, node);
|
|
36
|
+
|
|
35
37
|
return node;
|
|
36
38
|
}
|
|
37
39
|
|
|
@@ -55,6 +57,7 @@ function remove(target: HTMLElement, key: string) {
|
|
|
55
57
|
|
|
56
58
|
// run the "init" lifecycle event on the complete _mc structure
|
|
57
59
|
function init(element: DC) {
|
|
60
|
+
//console.warn('init(): args', ...arguments);
|
|
58
61
|
setTimeout(() => {
|
|
59
62
|
if (element.constructor === DC) return;
|
|
60
63
|
element.trigger('Init');
|
|
@@ -108,6 +111,8 @@ function trigger(
|
|
|
108
111
|
bubble: string = 'l', // direction of bubbling
|
|
109
112
|
controller?: BubbleController
|
|
110
113
|
) {
|
|
114
|
+
//console.warn('trigger args', ...arguments);
|
|
115
|
+
|
|
111
116
|
const eventName = /^_mc/.test(event) ? event : getMcEventName(event),
|
|
112
117
|
bubbleController = controller ? controller : new BubbleController(),
|
|
113
118
|
ev = new CustomEvent(eventName, {
|
|
@@ -126,8 +131,6 @@ function trigger(
|
|
|
126
131
|
|
|
127
132
|
// bubble if up or down is indicated
|
|
128
133
|
if (/[ud]/.test(bubble)) {
|
|
129
|
-
//console.log( 'bubble', bubble, domNode );
|
|
130
|
-
|
|
131
134
|
// add missing 'l', or else no handlers are executed
|
|
132
135
|
if (bubble.indexOf('l') === -1) {
|
|
133
136
|
bubble += 'l';
|
|
@@ -209,6 +212,8 @@ class McBase {
|
|
|
209
212
|
|
|
210
213
|
this._target = new WeakRef(target || {});
|
|
211
214
|
|
|
215
|
+
//console.warn('McBase: this, element, args', target, this, element, arguments);
|
|
216
|
+
|
|
212
217
|
autoAttachListeners(element);
|
|
213
218
|
|
|
214
219
|
init(element as DC); // async lifecycle event
|
|
@@ -228,9 +233,11 @@ class MC extends McBase {
|
|
|
228
233
|
class DC extends McBase {
|
|
229
234
|
constructor(target: HTMLElement) {
|
|
230
235
|
super(target);
|
|
236
|
+
//console.warn('DC constructor:', arguments);
|
|
231
237
|
}
|
|
232
238
|
|
|
233
239
|
static add(target: HTMLElement, key: string, element: any = {}): HTMLMcElement {
|
|
240
|
+
//console.warn('static DC.add() args', arguments);
|
|
234
241
|
return add(target, key, element);
|
|
235
242
|
}
|
|
236
243
|
|
|
@@ -258,6 +265,8 @@ class DC extends McBase {
|
|
|
258
265
|
}
|
|
259
266
|
|
|
260
267
|
trigger(eventName: string, data: any = {}, bubble: string = 'l', controller?: BubbleController) {
|
|
268
|
+
//console.warn('MC.trigger args', ...arguments, this);
|
|
269
|
+
|
|
261
270
|
trigger(this.target, eventName, data, bubble, controller);
|
|
262
271
|
}
|
|
263
272
|
|
package/dist/elements.ts
CHANGED
|
@@ -3,51 +3,17 @@
|
|
|
3
3
|
import { DC, McBase } from './MC.js';
|
|
4
4
|
import { kebabToPascal } from './helpers.js';
|
|
5
5
|
|
|
6
|
-
class CeBaseClass extends HTMLElement {
|
|
7
|
-
constructor() {
|
|
8
|
-
super();
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
connectedCallback() {
|
|
12
|
-
this.dispatchEvent(new CustomEvent('_mcConnected'));
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
connectedMoveCallback() {
|
|
16
|
-
this.dispatchEvent(new CustomEvent('_mcConnectedMove'));
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
disconnectedCallback() {
|
|
20
|
-
this.dispatchEvent(new CustomEvent('_mcDisconnected'));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
adoptedCallback() {
|
|
24
|
-
this.dispatchEvent(new CustomEvent('_mcAdopted'));
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
attributeChangedCallback(name: string, oldValue: any, newValue: any) {
|
|
28
|
-
this.dispatchEvent(
|
|
29
|
-
new CustomEvent('_mcAttributeChanged', { detail: { name, oldValue, newValue } })
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
formResetCallback() {
|
|
34
|
-
this.dispatchEvent(new CustomEvent('_mcFormReset'));
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
6
|
function createNamedClass(name: string, ClassBody: { new (...args: any[]): any }) {
|
|
39
7
|
return Object.defineProperty(ClassBody, 'name', { value: name });
|
|
40
8
|
}
|
|
41
9
|
|
|
42
|
-
function isClass(func: any) {
|
|
43
|
-
return typeof func === 'function' && /^class\s/.test(Function.prototype.toString.call(func));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
10
|
export function defineCE(
|
|
47
11
|
tagName: string,
|
|
48
12
|
DefinitionClass: { new (...args: any[]): any } | Function,
|
|
49
13
|
observedAttributes: string[] = []
|
|
50
14
|
) {
|
|
15
|
+
//console.warn('defineCE args', arguments);
|
|
16
|
+
|
|
51
17
|
if (customElements.get(tagName)) {
|
|
52
18
|
console.warn(`IGNORED: Custom element "${tagName}" is already defined!`);
|
|
53
19
|
return;
|
|
@@ -59,18 +25,9 @@ export function defineCE(
|
|
|
59
25
|
|
|
60
26
|
let name = kebabToPascal(tagName);
|
|
61
27
|
|
|
62
|
-
// not a class, but a pure function
|
|
63
|
-
if (!isClass(DefinitionClass.prototype.constructor)) {
|
|
64
|
-
customElements.define(
|
|
65
|
-
tagName,
|
|
66
|
-
// @ts-ignore
|
|
67
|
-
DefinitionClass
|
|
68
|
-
);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
28
|
// a class, but not a MC
|
|
73
29
|
if (!(DefinitionClass.prototype instanceof McBase)) {
|
|
30
|
+
//console.warn('pure class', tagName, DefinitionClass);
|
|
74
31
|
customElements.define(
|
|
75
32
|
tagName,
|
|
76
33
|
// @ts-ignore
|
|
@@ -79,22 +36,49 @@ export function defineCE(
|
|
|
79
36
|
return;
|
|
80
37
|
}
|
|
81
38
|
|
|
39
|
+
//console.warn('MC class', tagName, DefinitionClass);
|
|
82
40
|
// a MC
|
|
83
41
|
customElements.define(
|
|
84
42
|
tagName,
|
|
85
43
|
createNamedClass(
|
|
86
44
|
name,
|
|
87
|
-
class extends
|
|
88
|
-
instance: any;
|
|
89
|
-
|
|
45
|
+
class extends HTMLElement {
|
|
90
46
|
constructor(data?: any) {
|
|
91
47
|
super();
|
|
48
|
+
// console.log('[named class constructor]', tagName, name, this, data);
|
|
92
49
|
// @ts-ignore
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
50
|
+
DC.add(this, name, new DefinitionClass(this, data));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
connectedCallback() {
|
|
54
|
+
//console.warn('onConnected', this);
|
|
55
|
+
this.dispatchEvent(new CustomEvent('_mcConnected'));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
connectedMoveCallback() {
|
|
59
|
+
//console.warn('onConnectedMove', this);
|
|
60
|
+
this.dispatchEvent(new CustomEvent('_mcConnectedMove'));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
disconnectedCallback() {
|
|
64
|
+
//console.warn('onDisconnected', this);
|
|
65
|
+
this.dispatchEvent(new CustomEvent('_mcDisconnected'));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
adoptedCallback() {
|
|
69
|
+
//console.warn('onAdopted', this);
|
|
70
|
+
this.dispatchEvent(new CustomEvent('_mcAdopted'));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
attributeChangedCallback(name: string, oldValue: any, newValue: any) {
|
|
74
|
+
//console.warn('onAttributeChanged', this);
|
|
75
|
+
this.dispatchEvent(
|
|
76
|
+
new CustomEvent('_mcAttributeChanged', { detail: { name, oldValue, newValue } })
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
formResetCallback() {
|
|
81
|
+
this.dispatchEvent(new CustomEvent('_mcFormReset'));
|
|
98
82
|
}
|
|
99
83
|
|
|
100
84
|
static get observedAttributes() {
|
package/dist/observables.ts
CHANGED
|
@@ -67,7 +67,11 @@ class Placeholders extends DC {
|
|
|
67
67
|
((template: string) => (data: LooseObject) => {
|
|
68
68
|
let result: string = template;
|
|
69
69
|
placeholders.forEach((placeholder) => {
|
|
70
|
-
result = result.replace(
|
|
70
|
+
result = result.replace(
|
|
71
|
+
placeholder,
|
|
72
|
+
// JSON.stringify(data[placeholder.replace(/[\{\}]/g, '')])
|
|
73
|
+
data[placeholder.replace(/[\{\}]/g, '')]
|
|
74
|
+
);
|
|
71
75
|
});
|
|
72
76
|
target.setAttribute(attr, result);
|
|
73
77
|
})(target.getAttribute(attr))
|
|
@@ -91,7 +95,11 @@ class Placeholders extends DC {
|
|
|
91
95
|
((template: string) => (data: LooseObject) => {
|
|
92
96
|
let result: string = template;
|
|
93
97
|
placeholders.forEach((placeholder) => {
|
|
94
|
-
result = result.replace(
|
|
98
|
+
result = result.replace(
|
|
99
|
+
placeholder,
|
|
100
|
+
// JSON.stringify(data[placeholder.replace(/[\{\}]/g, '')])
|
|
101
|
+
data[placeholder.replace(/[\{\}]/g, '')]
|
|
102
|
+
);
|
|
95
103
|
});
|
|
96
104
|
|
|
97
105
|
if (this.oldDisplay) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twobirds/microcomponents",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.19",
|
|
4
4
|
"description": "Micro Components Organization Class",
|
|
5
5
|
"main": "./dist/microcomponents.ts",
|
|
6
6
|
"types": "./dist/microcomponents.d.ts",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"clean": "rm -rf build/*; rm -rf dist/*; rm -rf public/*;",
|
|
23
23
|
"prep": "cp -R src/* build || true; mkdir public || true; npm run typedoc; npm run format; npm run lint;",
|
|
24
24
|
"copyToDist": "cp build/*.js dist; cp public/microcomponents.min.js public/test; cp -R build/*.ts* dist; cp *.svg dist;",
|
|
25
|
-
"copyToPublic1": "cp
|
|
26
|
-
"copyToPublic2": "cp -R build/
|
|
25
|
+
"copyToPublic1": "cp dist/*.* public; cp dist/*.* public/test;",
|
|
26
|
+
"copyToPublic2": "cp -R assets/* public || true; cp -R build/src/* public/; cp dist/MicroComponents.svg public; cp public/MicroComponents.svg public/test;",
|
|
27
27
|
"build": "npm run clean; npm run prep; tsc; npm run webpack; npm run copyToDist; npm run copyToPublic1; npm run copyToPublic2;",
|
|
28
28
|
"dev": "cd public; php -S 0.0.0.0:10000; cd ..",
|
|
29
29
|
"doc": "cd public/typedoc; php -S 0.0.0.0:11000; cd ../..",
|
|
@@ -42,6 +42,7 @@
|
|
|
42
42
|
"ts-loader": "^9.5.2",
|
|
43
43
|
"typedoc": "^0.26.11",
|
|
44
44
|
"typescript": "^5.5.4",
|
|
45
|
+
"vite": "^7.3.1",
|
|
45
46
|
"webpack": "^5.97.1",
|
|
46
47
|
"webpack-cli": "^6.0.1"
|
|
47
48
|
},
|
package/dist/MC.d.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { LooseObject } from './helpers.js';
|
|
2
|
-
export interface HTMLMcElement extends HTMLElement {
|
|
3
|
-
_mc: LooseObject;
|
|
4
|
-
}
|
|
5
|
-
declare function off(domNode: Node, eventName: string, handler: EventListenerOrEventListenerObject): void;
|
|
6
|
-
declare function on(domNode: Node, eventName: string, handler: EventListenerOrEventListenerObject, once?: boolean): EventListenerOrEventListenerObject;
|
|
7
|
-
declare function one(domNode: Node, eventName: string, handler: EventListenerOrEventListenerObject): EventListenerOrEventListenerObject;
|
|
8
|
-
declare class BubbleController {
|
|
9
|
-
stopped: boolean;
|
|
10
|
-
constructor();
|
|
11
|
-
stop(): void;
|
|
12
|
-
}
|
|
13
|
-
declare function trigger(domNode: HTMLElement, event: string, data?: any, bubble?: string, controller?: BubbleController): void;
|
|
14
|
-
declare class McBase {
|
|
15
|
-
private _target;
|
|
16
|
-
constructor(target?: {
|
|
17
|
-
[key: string]: any;
|
|
18
|
-
});
|
|
19
|
-
get target(): any;
|
|
20
|
-
}
|
|
21
|
-
declare class MC extends McBase {
|
|
22
|
-
constructor(target?: any);
|
|
23
|
-
}
|
|
24
|
-
declare class DC extends McBase {
|
|
25
|
-
constructor(target: HTMLElement);
|
|
26
|
-
static add(target: HTMLElement, key: string, element?: any): HTMLMcElement;
|
|
27
|
-
static remove(target: HTMLElement, key: string): void;
|
|
28
|
-
off(eventName: string, handler: EventListenerOrEventListenerObject): void;
|
|
29
|
-
on(eventName: string, handler: EventListenerOrEventListenerObject): EventListenerOrEventListenerObject;
|
|
30
|
-
one(eventName: string, handler: EventListenerOrEventListenerObject): EventListenerOrEventListenerObject;
|
|
31
|
-
trigger(eventName: string, data?: any, bubble?: string, controller?: BubbleController): void;
|
|
32
|
-
ancestors(): DC[] | null;
|
|
33
|
-
closest(): DC | null;
|
|
34
|
-
children(): DC[] | null;
|
|
35
|
-
descendants(): DC[] | null;
|
|
36
|
-
}
|
|
37
|
-
declare function autoload(enable?: boolean): boolean;
|
|
38
|
-
export { McBase, MC, DC, autoload, on, one, off, trigger };
|
|
39
|
-
//# sourceMappingURL=MC.d.ts.map
|
package/dist/MC.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MC.d.ts","sourceRoot":"","sources":["../src/MC.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAI3C,MAAM,WAAW,aAAc,SAAQ,WAAW;IACjD,GAAG,EAAE,WAAW,CAAC;CACjB;AA+DD,iBAAS,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,kCAAkC,QAEzF;AAED,iBAAS,EAAE,CACV,OAAO,EAAE,IAAI,EACb,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,EAC3C,IAAI,GAAE,OAAe,GACnB,kCAAkC,CAGpC;AAED,iBAAS,GAAG,CACX,OAAO,EAAE,IAAI,EACb,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,GACzC,kCAAkC,CAGpC;AAED,cAAM,gBAAgB;IACrB,OAAO,UAAS;;IAEhB,IAAI;CAGJ;AAGD,iBAAS,OAAO,CACf,OAAO,EAAE,WAAW,EACpB,KAAK,EAAE,MAAM,EACb,IAAI,GAAE,GAAQ,EACd,MAAM,GAAE,MAAY,EACpB,UAAU,CAAC,EAAE,gBAAgB,QA8C7B;AAiDD,cAAM,MAAM;IACX,OAAO,CAAC,OAAO,CAAe;gBAElB,MAAM,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE;IAU3C,IAAI,MAAM,IAAI,GAAG,CAEhB;CACD;AAED,cAAM,EAAG,SAAQ,MAAM;gBACV,MAAM,CAAC,EAAE,GAAG;CAGxB;AAED,cAAM,EAAG,SAAQ,MAAM;gBACV,MAAM,EAAE,WAAW;IAI/B,MAAM,CAAC,GAAG,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,GAAQ,GAAG,aAAa;IAI9E,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM;IAK9C,GAAG,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,kCAAkC;IAIlE,EAAE,CACD,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,GACzC,kCAAkC;IAIrC,GAAG,CACF,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,GACzC,kCAAkC;IAIrC,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,GAAE,GAAQ,EAAE,MAAM,GAAE,MAAY,EAAE,UAAU,CAAC,EAAE,gBAAgB;IAK9F,SAAS,IAAI,EAAE,EAAE,GAAG,IAAI;IAexB,OAAO,IAAI,EAAE,GAAG,IAAI;IAMpB,QAAQ,IAAI,EAAE,EAAE,GAAG,IAAI;IAMvB,WAAW,IAAI,EAAE,EAAE,GAAG,IAAI;CAQ1B;AA8HD,iBAAS,QAAQ,CAAC,MAAM,GAAE,OAAc,GAAG,OAAO,CAkBjD;AAID,OAAO,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,CAAC"}
|
package/dist/MC.js
DELETED
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
|
|
3
|
-
function isDOM(element) {
|
|
4
|
-
return element && element instanceof HTMLElement;
|
|
5
|
-
}
|
|
6
|
-
function add(target, key, element = {}) {
|
|
7
|
-
let node = target;
|
|
8
|
-
if (!node._mc) {
|
|
9
|
-
node._mc = new DC(target);
|
|
10
|
-
}
|
|
11
|
-
const _mc = node._mc;
|
|
12
|
-
if (key.length && !_mc[key]) {
|
|
13
|
-
_mc[key] = element;
|
|
14
|
-
if (target instanceof HTMLElement && !target.getAttribute('data-mc')) {
|
|
15
|
-
target.setAttribute('data-mc', '');
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
return node;
|
|
19
|
-
}
|
|
20
|
-
function remove(target, key) {
|
|
21
|
-
if (!key)
|
|
22
|
-
return;
|
|
23
|
-
const _mc = target?._mc || {};
|
|
24
|
-
if (_mc && _mc[key]) {
|
|
25
|
-
delete _mc[key];
|
|
26
|
-
}
|
|
27
|
-
if (!Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('data-mc')) {
|
|
28
|
-
target.removeAttribute('data-mc');
|
|
29
|
-
}
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
function init(element) {
|
|
33
|
-
setTimeout(() => {
|
|
34
|
-
if (element.constructor === DC)
|
|
35
|
-
return;
|
|
36
|
-
element.trigger('Init');
|
|
37
|
-
}, 0);
|
|
38
|
-
}
|
|
39
|
-
function getMcEventName(eventName) {
|
|
40
|
-
return '_mc' + eventName[0].toUpperCase() + eventName.substring(1);
|
|
41
|
-
}
|
|
42
|
-
function off(domNode, eventName, handler) {
|
|
43
|
-
domNode.removeEventListener(getMcEventName(eventName), handler);
|
|
44
|
-
}
|
|
45
|
-
function on(domNode, eventName, handler, once = false) {
|
|
46
|
-
domNode.addEventListener(getMcEventName(eventName), handler, { capture: false, once });
|
|
47
|
-
return handler;
|
|
48
|
-
}
|
|
49
|
-
function one(domNode, eventName, handler) {
|
|
50
|
-
on(domNode, eventName, handler, true);
|
|
51
|
-
return handler;
|
|
52
|
-
}
|
|
53
|
-
class BubbleController {
|
|
54
|
-
stopped = false;
|
|
55
|
-
constructor() { }
|
|
56
|
-
stop() {
|
|
57
|
-
this.stopped = true;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
function trigger(domNode, event, data = {}, bubble = 'l', controller) {
|
|
61
|
-
const eventName = /^_mc/.test(event) ? event : getMcEventName(event), bubbleController = controller ? controller : new BubbleController(), ev = new CustomEvent(eventName, {
|
|
62
|
-
detail: { data, control: bubbleController },
|
|
63
|
-
bubbles: false,
|
|
64
|
-
});
|
|
65
|
-
if (bubbleController.stopped)
|
|
66
|
-
return;
|
|
67
|
-
if (/l/.test(bubble)) {
|
|
68
|
-
domNode.dispatchEvent(ev);
|
|
69
|
-
ev.stopImmediatePropagation();
|
|
70
|
-
if (bubbleController.stopped)
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
if (/[ud]/.test(bubble)) {
|
|
74
|
-
if (bubble.indexOf('l') === -1) {
|
|
75
|
-
bubble += 'l';
|
|
76
|
-
}
|
|
77
|
-
setTimeout(() => {
|
|
78
|
-
if (bubble.indexOf('u') > -1) {
|
|
79
|
-
let parentMc = domNode.parentElement?.closest('[data-mc]');
|
|
80
|
-
if (parentMc) {
|
|
81
|
-
parentMc?._mc.trigger(eventName, data, bubble, bubbleController);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
if (bubble.indexOf('d') > -1) {
|
|
85
|
-
let childNodes = domNode._mc.children();
|
|
86
|
-
if (childNodes) {
|
|
87
|
-
childNodes.forEach((_mc) => _mc.trigger(eventName, data, bubble));
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}, 0);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
function autoAttachListeners(element) {
|
|
94
|
-
if (!isBrowser() || element.constructor === DC)
|
|
95
|
-
return;
|
|
96
|
-
const target = element.target;
|
|
97
|
-
if (target instanceof HTMLElement) {
|
|
98
|
-
let that = element;
|
|
99
|
-
Object.getOwnPropertyNames(that.constructor.prototype)
|
|
100
|
-
.filter((key) => /^(on[A-Z]|one[A-Z])/.test(key) && typeof that.constructor.prototype[key] === 'function')
|
|
101
|
-
.forEach((key) => {
|
|
102
|
-
let once = /^one/.test(key), withoutOnOne = key.replace(/^on[e]{0,1}/, ''), nativeEventName = withoutOnOne.toLowerCase();
|
|
103
|
-
if ('on' + nativeEventName in target) {
|
|
104
|
-
let listener = (ev) => {
|
|
105
|
-
that[key](ev);
|
|
106
|
-
};
|
|
107
|
-
target.addEventListener(nativeEventName, listener);
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
let listener = (ev) => {
|
|
111
|
-
that[key](ev);
|
|
112
|
-
};
|
|
113
|
-
on(target, withoutOnOne, listener, once);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
function getMcChildNodes(domNode) {
|
|
119
|
-
const id = Math.random().toString().replace('.', '');
|
|
120
|
-
const selector = '[data-mc]:not([temp_id="' + id + '"] [data-mc] [data-mc])';
|
|
121
|
-
domNode.setAttribute('temp_id', id);
|
|
122
|
-
const mcArray = [...domNode.querySelectorAll(selector)]
|
|
123
|
-
.filter((e) => !!e._mc)
|
|
124
|
-
.map((e) => e._mc);
|
|
125
|
-
domNode.removeAttribute('temp_id');
|
|
126
|
-
return mcArray.length ? mcArray : null;
|
|
127
|
-
}
|
|
128
|
-
class McBase {
|
|
129
|
-
_target;
|
|
130
|
-
constructor(target) {
|
|
131
|
-
let element = this;
|
|
132
|
-
this._target = new WeakRef(target || {});
|
|
133
|
-
autoAttachListeners(element);
|
|
134
|
-
init(element);
|
|
135
|
-
}
|
|
136
|
-
get target() {
|
|
137
|
-
return this._target.deref();
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
class MC extends McBase {
|
|
141
|
-
constructor(target) {
|
|
142
|
-
super(target);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
class DC extends McBase {
|
|
146
|
-
constructor(target) {
|
|
147
|
-
super(target);
|
|
148
|
-
}
|
|
149
|
-
static add(target, key, element = {}) {
|
|
150
|
-
return add(target, key, element);
|
|
151
|
-
}
|
|
152
|
-
static remove(target, key) {
|
|
153
|
-
remove(target, key);
|
|
154
|
-
}
|
|
155
|
-
off(eventName, handler) {
|
|
156
|
-
off(this.target, eventName, handler);
|
|
157
|
-
}
|
|
158
|
-
on(eventName, handler) {
|
|
159
|
-
return on(this.target, eventName, handler);
|
|
160
|
-
}
|
|
161
|
-
one(eventName, handler) {
|
|
162
|
-
return one(this.target, eventName, handler);
|
|
163
|
-
}
|
|
164
|
-
trigger(eventName, data = {}, bubble = 'l', controller) {
|
|
165
|
-
trigger(this.target, eventName, data, bubble, controller);
|
|
166
|
-
}
|
|
167
|
-
ancestors() {
|
|
168
|
-
let currentNode = this?.target, result = [];
|
|
169
|
-
if (!isDOM(currentNode))
|
|
170
|
-
return [];
|
|
171
|
-
while ((currentNode = currentNode.closest('[data-mc]'))) {
|
|
172
|
-
if (currentNode?._mc) {
|
|
173
|
-
result.push(currentNode?._mc);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
return result.length ? result : null;
|
|
177
|
-
}
|
|
178
|
-
closest() {
|
|
179
|
-
let target = this.target;
|
|
180
|
-
if (!isDOM(target))
|
|
181
|
-
return null;
|
|
182
|
-
return target.closest('[data-mc]')?._mc || null;
|
|
183
|
-
}
|
|
184
|
-
children() {
|
|
185
|
-
let target = this?.target;
|
|
186
|
-
if (!isDOM(target))
|
|
187
|
-
return null;
|
|
188
|
-
return getMcChildNodes(target);
|
|
189
|
-
}
|
|
190
|
-
descendants() {
|
|
191
|
-
const target = this?.target;
|
|
192
|
-
if (!isDOM(target))
|
|
193
|
-
return [];
|
|
194
|
-
const mcArray = [...target.querySelectorAll('[data-mc]')]
|
|
195
|
-
.filter((e) => !!e._mc)
|
|
196
|
-
.map((e) => e._mc);
|
|
197
|
-
return mcArray.length ? mcArray : null;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
const loadingDCs = new Set();
|
|
201
|
-
function attachDC(element) {
|
|
202
|
-
return () => {
|
|
203
|
-
const elements = Array.from(document.querySelectorAll(`[data-mc*="${element}"]`)).filter((element) => element instanceof HTMLElement);
|
|
204
|
-
if (elements.length === 0)
|
|
205
|
-
return;
|
|
206
|
-
const Klass = customElements.get(element);
|
|
207
|
-
elements.forEach((node) => {
|
|
208
|
-
try {
|
|
209
|
-
DC.add(node, Klass.name, new Klass(node));
|
|
210
|
-
}
|
|
211
|
-
catch (error) {
|
|
212
|
-
if (Klass['create']) {
|
|
213
|
-
let element = Klass.create(node);
|
|
214
|
-
element.setAttribute('data-mc', node.getAttribute('data-mc'));
|
|
215
|
-
node.replaceWith(element);
|
|
216
|
-
node = element;
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
throw `Class ${Klass.name} doesn't have a callable constructor AND class doesn't have a static .create() method.`;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
const new_mc = node
|
|
223
|
-
.getAttribute('data-mc')
|
|
224
|
-
.split(' ')
|
|
225
|
-
.filter((name) => name !== element && name !== '')
|
|
226
|
-
.join(' ')
|
|
227
|
-
.trim();
|
|
228
|
-
node.setAttribute('data-mc', new_mc);
|
|
229
|
-
});
|
|
230
|
-
};
|
|
231
|
-
}
|
|
232
|
-
function makeLoadScript(element) {
|
|
233
|
-
if (loadingDCs.has(element))
|
|
234
|
-
return;
|
|
235
|
-
loadingDCs.add(element);
|
|
236
|
-
const fileName = './' + element.split('-').join('/') + '.js';
|
|
237
|
-
const se = document.createElement('script');
|
|
238
|
-
se.setAttribute('src', fileName);
|
|
239
|
-
se.setAttribute('blocking', 'render');
|
|
240
|
-
se.async = true;
|
|
241
|
-
se.setAttribute('type', 'module');
|
|
242
|
-
se.setAttribute('data-name', element);
|
|
243
|
-
se.setAttribute('loading', '');
|
|
244
|
-
const loadedPromise = customElements.whenDefined(element);
|
|
245
|
-
loadedPromise.then(attachDC(element));
|
|
246
|
-
se.addEventListener('load', (ev) => {
|
|
247
|
-
loadingDCs.delete(element);
|
|
248
|
-
document.head.querySelector(`script[data-name="${element}"]`)?.remove();
|
|
249
|
-
});
|
|
250
|
-
se.addEventListener('error', (ev) => {
|
|
251
|
-
console.error('could not load DC code from', fileName);
|
|
252
|
-
});
|
|
253
|
-
document.head.append(se);
|
|
254
|
-
}
|
|
255
|
-
function loadUndefinedMCs() {
|
|
256
|
-
[...document.querySelectorAll(':not(:defined)')]
|
|
257
|
-
.filter((element) => !!element &&
|
|
258
|
-
!customElements.get(element.tagName.toLowerCase()) &&
|
|
259
|
-
!loadingDCs.has(element.tagName.toLowerCase()))
|
|
260
|
-
.forEach((el) => {
|
|
261
|
-
makeLoadScript(el.tagName.toLowerCase());
|
|
262
|
-
});
|
|
263
|
-
[...document.querySelectorAll('[data-mc]:not([data-mc=""]')]
|
|
264
|
-
.filter((element) => !!element)
|
|
265
|
-
.forEach((el) => {
|
|
266
|
-
const _mc = el
|
|
267
|
-
.getAttribute('data-mc')
|
|
268
|
-
?.split(' ')
|
|
269
|
-
.filter((name) => name !== '');
|
|
270
|
-
_mc?.forEach((element) => {
|
|
271
|
-
const isDefined = customElements.get(element);
|
|
272
|
-
if (!loadingDCs.has(element) && !isDefined) {
|
|
273
|
-
makeLoadScript(element);
|
|
274
|
-
}
|
|
275
|
-
else if (isDefined) {
|
|
276
|
-
attachDC(element)();
|
|
277
|
-
}
|
|
278
|
-
});
|
|
279
|
-
});
|
|
280
|
-
}
|
|
281
|
-
let autoloadEnabled = false;
|
|
282
|
-
let observer = null;
|
|
283
|
-
function autoload(enable = true) {
|
|
284
|
-
if (!enable) {
|
|
285
|
-
observer?.disconnect();
|
|
286
|
-
observer = null;
|
|
287
|
-
return autoloadEnabled;
|
|
288
|
-
}
|
|
289
|
-
autoloadEnabled = enable;
|
|
290
|
-
observer = new MutationObserver(loadUndefinedMCs);
|
|
291
|
-
setTimeout(() => {
|
|
292
|
-
observer.observe(document.body, { childList: true, subtree: true });
|
|
293
|
-
loadUndefinedMCs();
|
|
294
|
-
}, 5);
|
|
295
|
-
window.autoload = window.autoload || autoload;
|
|
296
|
-
return autoloadEnabled;
|
|
297
|
-
}
|
|
298
|
-
autoload.state = loadingDCs;
|
|
299
|
-
export { McBase, MC, DC, autoload, on, one, off, trigger };
|