@twobirds/microcomponents 0.0.25 → 0.0.27
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.d.ts +9 -9
- package/dist/MC.d.ts.map +1 -1
- package/dist/MC.min.js +91 -55
- package/dist/MC.ts +118 -72
- package/dist/customElements.min.js +1 -1
- package/dist/microcomponents.min.js +1 -1
- package/dist/test.ts +6 -11
- package/package.json +1 -1
- package/public/MC.js +91 -55
- package/public/MC.js.map +1 -1
- package/public/MC.min.js +91 -55
- package/public/assets/search.js +1 -1
- package/public/classes/DC.html +40 -54
- package/public/classes/MC.html +42 -23
- package/public/classes/McBase.html +7 -18
- package/public/classes/McEvent.html +13 -13
- package/public/customElements.min.js +1 -1
- package/public/functions/addListener.html +1 -1
- package/public/functions/removeListener.html +1 -1
- package/public/interfaces/HTMLMcElement.html +2 -2
- package/public/microcomponents.min.js +1 -1
- package/public/test.js +5 -9
- package/public/test.js.map +1 -1
- package/src/MC.ts +118 -72
- package/src/test.ts +6 -11
package/dist/MC.d.ts
CHANGED
|
@@ -5,11 +5,9 @@ export interface HTMLMcElement extends HTMLElement {
|
|
|
5
5
|
}
|
|
6
6
|
declare function addListener(domNode: Node, eventName: string, handler: EventListenerOrEventListenerObject | null, capture?: boolean, once?: boolean): void;
|
|
7
7
|
declare function removeListener(domNode: Node, eventName: string, handler: EventListenerOrEventListenerObject | null): void;
|
|
8
|
+
declare function trigger(target: unknown, ev: McEvent | string, data?: any): void;
|
|
8
9
|
declare class McBase {
|
|
9
10
|
#private;
|
|
10
|
-
protected _mc: {
|
|
11
|
-
[key: string]: any;
|
|
12
|
-
};
|
|
13
11
|
constructor(target: {
|
|
14
12
|
[key: string]: any;
|
|
15
13
|
});
|
|
@@ -17,23 +15,25 @@ declare class McBase {
|
|
|
17
15
|
}
|
|
18
16
|
declare class MC extends McBase {
|
|
19
17
|
constructor(target: any);
|
|
18
|
+
static trigger: typeof trigger;
|
|
20
19
|
trigger(ev: McEvent | string, data?: any, bubble?: string): this | undefined;
|
|
20
|
+
autoAttachListeners(): void;
|
|
21
21
|
}
|
|
22
|
-
declare class DC extends
|
|
23
|
-
#private;
|
|
24
|
-
protected _mc: {
|
|
25
|
-
[key: string]: any;
|
|
26
|
-
};
|
|
22
|
+
declare class DC extends McBase {
|
|
27
23
|
constructor(target: HTMLElement);
|
|
28
24
|
static debug: boolean;
|
|
29
25
|
static add: Function;
|
|
30
26
|
static remove: Function;
|
|
31
27
|
static trigger: Function;
|
|
28
|
+
get _mc(): {
|
|
29
|
+
[key: string]: any;
|
|
30
|
+
};
|
|
31
|
+
trigger(ev: McEvent | string, data?: any, bubble?: string): void;
|
|
32
|
+
autoAttachListeners(): void;
|
|
32
33
|
parent(search?: string): any[];
|
|
33
34
|
ancestors(search?: string): any[];
|
|
34
35
|
children(search?: string): any[];
|
|
35
36
|
descendants(search?: string): any[];
|
|
36
|
-
autoAttachListeners(): void;
|
|
37
37
|
}
|
|
38
38
|
export { addListener, removeListener, McBase, MC, DC, McEvent };
|
|
39
39
|
//# sourceMappingURL=MC.d.ts.map
|
package/dist/MC.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MC.d.ts","sourceRoot":"","sources":["../src/MC.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAWvC,MAAM,WAAW,aAAc,SAAQ,WAAW;IACjD,GAAG,EAAE,WAAW,CAAC;CACjB;AA6CD,iBAAS,WAAW,CAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,kCAAkC,GAAG,IAAI,EAAE,OAAO,GAAE,OAAe,EAAE,IAAI,GAAE,OAAe,QAe1J;AAED,iBAAS,cAAc,CAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,kCAAkC,GAAG,IAAI,QAE5G;
|
|
1
|
+
{"version":3,"file":"MC.d.ts","sourceRoot":"","sources":["../src/MC.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAWvC,MAAM,WAAW,aAAc,SAAQ,WAAW;IACjD,GAAG,EAAE,WAAW,CAAC;CACjB;AA6CD,iBAAS,WAAW,CAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,kCAAkC,GAAG,IAAI,EAAE,OAAO,GAAE,OAAe,EAAE,IAAI,GAAE,OAAe,QAe1J;AAED,iBAAS,cAAc,CAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,kCAAkC,GAAG,IAAI,QAE5G;AA+CD,iBAAS,OAAO,CAAE,MAAM,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,GAAG,MAAM,EAAE,IAAI,GAAE,GAAQ,QAUtE;AAED,cAAM,MAAM;;gBAME,MAAM,EAAE;QAAE,CAAC,GAAG,EAAC,MAAM,GAAG,GAAG,CAAA;KAAE;IAY1C,IAAI,MAAM,IAAI,GAAG,CAEhB;CAED;AAED,cAAM,EAAG,SAAQ,MAAM;gBAEV,MAAM,EAAE,GAAG;IA8BpB,MAAM,CAAC,OAAO,iBAAW;IAE5B,OAAO,CAAE,EAAE,EAAE,OAAO,GAAG,MAAM,EAAE,IAAI,GAAE,GAAQ,EAAE,MAAM,CAAC,EAAE,MAAM;IAiF9D,mBAAmB;CA2CnB;AAED,cAAM,EAAG,SAAQ,MAAM;gBAEV,MAAM,EAAE,WAAW;IAkC/B,MAAM,CAAC,KAAK,EAAE,OAAO,CAAS;IAE9B,MAAM,CAAC,GAAG,EAAE,QAAQ,CAAO;IAC3B,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAU;IACjC,MAAM,CAAC,OAAO,EAAE,QAAQ,CAAW;IAEnC,IAAI,GAAG,IAAI;QAAE,CAAC,GAAG,EAAC,MAAM,GAAG,GAAG,CAAA;KAAE,CAE/B;IAED,OAAO,CAAE,EAAE,EAAE,OAAO,GAAG,MAAM,EAAE,IAAI,GAAE,GAAQ,EAAE,MAAM,CAAC,EAAE,MAAM;IAI9D,mBAAmB;IAInB,MAAM,CAAC,MAAM,GAAE,MAAW,GAAG,GAAG,EAAE;IAMlC,SAAS,CAAC,MAAM,GAAE,MAAW,GAAG,GAAG,EAAE;IAMrC,QAAQ,CAAC,MAAM,GAAE,MAAW,GAAG,GAAG,EAAE;IAYpC,WAAW,CAAC,MAAM,GAAE,MAAW,GAAG,GAAG,EAAE;CAOvC;AAED,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE,CAAC"}
|
package/dist/MC.min.js
CHANGED
|
@@ -200,10 +200,10 @@ function add(target, key, element = {}) {
|
|
|
200
200
|
let node = target;
|
|
201
201
|
// create the _mc property if necessary
|
|
202
202
|
if (!node._mc) {
|
|
203
|
-
let
|
|
204
|
-
|
|
205
|
-
node._mc = new
|
|
206
|
-
|
|
203
|
+
let debug = DC?.debug;
|
|
204
|
+
DC.debug = false;
|
|
205
|
+
node._mc = new DC(target);
|
|
206
|
+
DC.debug = debug;
|
|
207
207
|
}
|
|
208
208
|
const _mc = node._mc;
|
|
209
209
|
// add the element with the given property key to the _mc property
|
|
@@ -237,6 +237,7 @@ function trigger(target, ev, data = {}) {
|
|
|
237
237
|
if (!_mc)
|
|
238
238
|
return;
|
|
239
239
|
[...Object.values(_mc)].forEach((mc) => {
|
|
240
|
+
//console.log( 'mc [', mc.constructor.name, ']', mc.constructor.prototype?.oneInit );
|
|
240
241
|
if (mc?.trigger && typeof mc?.trigger === 'function') {
|
|
241
242
|
mc?.trigger(ev, data);
|
|
242
243
|
}
|
|
@@ -246,17 +247,15 @@ function trigger(target, ev, data = {}) {
|
|
|
246
247
|
class McBase {
|
|
247
248
|
#target; // avoid circular reference
|
|
248
249
|
#_mc = {};
|
|
249
|
-
_mc = {};
|
|
250
250
|
constructor(target) {
|
|
251
251
|
let that = this;
|
|
252
252
|
this.#target = new WeakRef(target);
|
|
253
|
-
if (this.constructor !==
|
|
253
|
+
if (this.constructor !== DC) {
|
|
254
254
|
Object.defineProperty(this, "_mc", {
|
|
255
|
-
get() {
|
|
256
|
-
return this.#_mc;
|
|
257
|
-
}
|
|
255
|
+
get() { return this.#_mc; }
|
|
258
256
|
});
|
|
259
257
|
}
|
|
258
|
+
;
|
|
260
259
|
}
|
|
261
260
|
get target() {
|
|
262
261
|
return this.#target.deref();
|
|
@@ -266,14 +265,29 @@ class MC extends McBase {
|
|
|
266
265
|
constructor(target) {
|
|
267
266
|
super(target);
|
|
268
267
|
let that = this;
|
|
269
|
-
if (that.constructor === MC || that.constructor === DC)
|
|
270
|
-
return;
|
|
271
268
|
setTimeout(() => {
|
|
272
|
-
that.trigger('
|
|
269
|
+
that.trigger('Init');
|
|
270
|
+
setTimeout(() => {
|
|
271
|
+
let keys = Object.keys(that?._mc);
|
|
272
|
+
if (keys.length) {
|
|
273
|
+
//console.log( 'inner Inits on [', that.constructor.name, ']', keys );
|
|
274
|
+
keys?.forEach((key) => {
|
|
275
|
+
let mc = that?._mc?.[key];
|
|
276
|
+
//console.log( key, typeof mc?.oneInit, mc );
|
|
277
|
+
if (mc?.oneInit && typeof mc?.oneInit == 'function') {
|
|
278
|
+
mc.oneInit(new McEvent('Init'));
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
;
|
|
283
|
+
}, 0);
|
|
273
284
|
}, 0);
|
|
285
|
+
this.autoAttachListeners();
|
|
274
286
|
}
|
|
287
|
+
static trigger = trigger;
|
|
275
288
|
trigger(ev, data = {}, bubble) {
|
|
276
289
|
let that = this;
|
|
290
|
+
//console.log( that, '.trigger(', ev, data, bubble, ')' );
|
|
277
291
|
bubble = bubble || 'l';
|
|
278
292
|
// run all triggers
|
|
279
293
|
if (that.constructor === MC || that.constructor === DC) {
|
|
@@ -331,25 +345,80 @@ class MC extends McBase {
|
|
|
331
345
|
}
|
|
332
346
|
}
|
|
333
347
|
}
|
|
348
|
+
autoAttachListeners() {
|
|
349
|
+
if (!MC_isBrowser())
|
|
350
|
+
return;
|
|
351
|
+
const target = this.target;
|
|
352
|
+
if (target instanceof HTMLElement) {
|
|
353
|
+
let that = this;
|
|
354
|
+
Object
|
|
355
|
+
.getOwnPropertyNames(that.constructor.prototype)
|
|
356
|
+
.filter(key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function')
|
|
357
|
+
.forEach(key => {
|
|
358
|
+
let once = /^one/.test(key), withoutOnOne = key.replace(/^on[e]{0,1}/, ''), // preserves upper/lowercase for non-native events
|
|
359
|
+
nativeEventName = withoutOnOne.toLowerCase(), listener = (ev) => { that.trigger(withoutOnOne, ev); };
|
|
360
|
+
;
|
|
361
|
+
if (once) {
|
|
362
|
+
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
363
|
+
Object.defineProperty(this, 'on' + withoutOnOne, {
|
|
364
|
+
configurable: true,
|
|
365
|
+
enumerable: false,
|
|
366
|
+
writable: true,
|
|
367
|
+
value: (ev) => {
|
|
368
|
+
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
369
|
+
(that.constructor.prototype[key]).bind(that)(ev);
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
if (MC_nativeEventNames.has('on' + nativeEventName)) { // native event defined in HTMLElement
|
|
374
|
+
// console.log('added as native listener', target, nativeEventName );
|
|
375
|
+
addListener(target, nativeEventName, listener, false, once);
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
}
|
|
334
380
|
}
|
|
335
381
|
;
|
|
336
|
-
class DC extends
|
|
337
|
-
#_mc = {};
|
|
338
|
-
_mc = {};
|
|
382
|
+
class DC extends McBase {
|
|
339
383
|
constructor(target) {
|
|
340
384
|
super(target);
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
385
|
+
let that = this, isDomMcInstance = that.constructor === DC;
|
|
386
|
+
setTimeout(() => {
|
|
387
|
+
that.trigger('Init');
|
|
388
|
+
if (!isDomMcInstance) {
|
|
389
|
+
setTimeout(() => {
|
|
390
|
+
let keys = Object.keys(that?._mc);
|
|
391
|
+
if (keys.length) {
|
|
392
|
+
//console.log( 'inner Inits on [', that.constructor.name, ']', keys );
|
|
393
|
+
keys?.forEach((key) => {
|
|
394
|
+
let mc = that?._mc?.[key];
|
|
395
|
+
//console.log( key, typeof mc?.oneInit, mc );
|
|
396
|
+
if (mc?.oneInit && typeof mc?.oneInit == 'function') {
|
|
397
|
+
mc.oneInit(new McEvent('Init'));
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
;
|
|
402
|
+
}, 0);
|
|
403
|
+
}
|
|
404
|
+
}, 0);
|
|
405
|
+
this.autoAttachListeners();
|
|
348
406
|
}
|
|
349
407
|
static debug = false;
|
|
350
408
|
static add = add;
|
|
351
409
|
static remove = remove;
|
|
352
410
|
static trigger = trigger;
|
|
411
|
+
get _mc() {
|
|
412
|
+
return this._mc;
|
|
413
|
+
}
|
|
414
|
+
trigger(ev, data = {}, bubble) {
|
|
415
|
+
MC.prototype.trigger.call(this, ev, data, bubble);
|
|
416
|
+
}
|
|
417
|
+
;
|
|
418
|
+
autoAttachListeners() {
|
|
419
|
+
MC.prototype.autoAttachListeners.call(this);
|
|
420
|
+
}
|
|
421
|
+
;
|
|
353
422
|
parent(search = '') {
|
|
354
423
|
let target = this.target;
|
|
355
424
|
if (!MC_isDOM(target))
|
|
@@ -381,39 +450,6 @@ class DC extends MC {
|
|
|
381
450
|
const myDomElements = target.querySelectorAll('*[_mc]');
|
|
382
451
|
return MC_getMicroComponents([...myDomElements], search);
|
|
383
452
|
}
|
|
384
|
-
autoAttachListeners() {
|
|
385
|
-
if (!MC_isBrowser())
|
|
386
|
-
return;
|
|
387
|
-
const target = this.target;
|
|
388
|
-
if (target instanceof HTMLElement) {
|
|
389
|
-
let that = this;
|
|
390
|
-
Object
|
|
391
|
-
.getOwnPropertyNames(that.constructor.prototype)
|
|
392
|
-
.filter(key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function')
|
|
393
|
-
.forEach(key => {
|
|
394
|
-
let once = /^one/.test(key), withoutOnOne = key.replace(/^on[e]{0,1}/, ''), // preserves upper/lowercase for non-native events
|
|
395
|
-
nativeEventName = withoutOnOne.toLowerCase(), listener = (ev) => { that.trigger(withoutOnOne, ev); };
|
|
396
|
-
;
|
|
397
|
-
// console.log('autoAttach', nativeEventName, withoutOnOne, once, listener);
|
|
398
|
-
if (once) {
|
|
399
|
-
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
400
|
-
Object.defineProperty(this, 'on' + withoutOnOne, {
|
|
401
|
-
configurable: true,
|
|
402
|
-
enumerable: false,
|
|
403
|
-
writable: true,
|
|
404
|
-
value: (ev) => {
|
|
405
|
-
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
406
|
-
(that.constructor.prototype[key]).bind(that)(ev);
|
|
407
|
-
}
|
|
408
|
-
});
|
|
409
|
-
}
|
|
410
|
-
if (MC_nativeEventNames.has('on' + nativeEventName)) { // native event defined in HTMLElement
|
|
411
|
-
// console.log('added as native listener', target, nativeEventName );
|
|
412
|
-
addListener(target, nativeEventName, listener, false, once);
|
|
413
|
-
}
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
453
|
}
|
|
418
454
|
;
|
|
419
455
|
|
package/dist/MC.ts
CHANGED
|
@@ -87,11 +87,10 @@ function add( target: HTMLElement, key: string, element = {} ): any {
|
|
|
87
87
|
|
|
88
88
|
// create the _mc property if necessary
|
|
89
89
|
if (!node._mc) {
|
|
90
|
-
let
|
|
91
|
-
|
|
92
|
-
(
|
|
93
|
-
|
|
94
|
-
( MC as any ).debug = debug;
|
|
90
|
+
let debug = (DC as any)?.debug;
|
|
91
|
+
( DC as any ).debug = false;
|
|
92
|
+
node._mc = new ( DC as any )( target );
|
|
93
|
+
( DC as any ).debug = debug;
|
|
95
94
|
}
|
|
96
95
|
|
|
97
96
|
const _mc = node._mc;
|
|
@@ -129,7 +128,8 @@ function remove( target: HTMLMcElement, key: string) {
|
|
|
129
128
|
function trigger( target: unknown, ev: McEvent | string, data: any = {}) {
|
|
130
129
|
let _mc = (target as any)?._mc;
|
|
131
130
|
if ( !_mc ) return;
|
|
132
|
-
[ ...Object.values( _mc ) ].forEach( (mc) => {
|
|
131
|
+
[ ...Object.values( _mc ) ].forEach( (mc: any) => {
|
|
132
|
+
//console.log( 'mc [', mc.constructor.name, ']', mc.constructor.prototype?.oneInit );
|
|
133
133
|
if ( (mc as any)?.trigger && typeof (mc as any)?.trigger === 'function' ){
|
|
134
134
|
(mc as any)?.trigger( ev, data );
|
|
135
135
|
}
|
|
@@ -143,20 +143,16 @@ class McBase {
|
|
|
143
143
|
|
|
144
144
|
#_mc: { [key:string]: any } = {};
|
|
145
145
|
|
|
146
|
-
protected _mc: { [key:string]: any } = {};
|
|
147
|
-
|
|
148
146
|
constructor( target: { [key:string]: any } ) {
|
|
149
147
|
let that = this;
|
|
150
148
|
|
|
151
149
|
this.#target = new WeakRef( target );
|
|
152
150
|
|
|
153
|
-
if ( this.constructor !==
|
|
151
|
+
if ( this.constructor !== DC){
|
|
154
152
|
Object.defineProperty( this, "_mc", {
|
|
155
|
-
get(){
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
})
|
|
159
|
-
}
|
|
153
|
+
get(){ return this.#_mc }
|
|
154
|
+
});
|
|
155
|
+
};
|
|
160
156
|
}
|
|
161
157
|
|
|
162
158
|
get target(): any {
|
|
@@ -166,21 +162,43 @@ class McBase {
|
|
|
166
162
|
}
|
|
167
163
|
|
|
168
164
|
class MC extends McBase {
|
|
165
|
+
|
|
169
166
|
constructor(target: any) {
|
|
170
167
|
super(target);
|
|
171
168
|
let that = this;
|
|
172
|
-
|
|
169
|
+
|
|
173
170
|
setTimeout(
|
|
174
171
|
() => {
|
|
175
|
-
that.trigger('
|
|
172
|
+
that.trigger('Init');
|
|
173
|
+
setTimeout(
|
|
174
|
+
() => {
|
|
175
|
+
let keys = Object.keys( (that as any)?._mc );
|
|
176
|
+
if ( keys.length ){
|
|
177
|
+
//console.log( 'inner Inits on [', that.constructor.name, ']', keys );
|
|
178
|
+
keys?.forEach( (key: string) => {
|
|
179
|
+
let mc = (that as any)?._mc?.[key];
|
|
180
|
+
//console.log( key, typeof mc?.oneInit, mc );
|
|
181
|
+
if ( mc?.oneInit && typeof mc?.oneInit == 'function' ){
|
|
182
|
+
mc.oneInit( new McEvent('Init') );
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
, 0
|
|
188
|
+
);
|
|
176
189
|
}
|
|
177
190
|
,0
|
|
178
191
|
);
|
|
192
|
+
|
|
193
|
+
this.autoAttachListeners();
|
|
179
194
|
}
|
|
180
195
|
|
|
196
|
+
static trigger = trigger;
|
|
197
|
+
|
|
181
198
|
trigger( ev: McEvent | string, data: any = {}, bubble?: string ) {
|
|
182
199
|
let that = this;
|
|
183
200
|
|
|
201
|
+
//console.log( that, '.trigger(', ev, data, bubble, ')' );
|
|
184
202
|
bubble = bubble || 'l';
|
|
185
203
|
|
|
186
204
|
// run all triggers
|
|
@@ -258,24 +276,85 @@ class MC extends McBase {
|
|
|
258
276
|
|
|
259
277
|
}
|
|
260
278
|
|
|
261
|
-
|
|
279
|
+
autoAttachListeners(){
|
|
280
|
+
if ( !isBrowser() ) return;
|
|
262
281
|
|
|
263
|
-
|
|
282
|
+
const target = this.target;
|
|
264
283
|
|
|
265
|
-
|
|
284
|
+
if ( target instanceof HTMLElement ){
|
|
285
|
+
let that: LooseObject = this;
|
|
286
|
+
Object
|
|
287
|
+
.getOwnPropertyNames( that.constructor.prototype )
|
|
288
|
+
.filter( key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function' )
|
|
289
|
+
.forEach( key => {
|
|
290
|
+
let
|
|
291
|
+
once = /^one/.test(key),
|
|
292
|
+
withoutOnOne = key.replace( /^on[e]{0,1}/, '' ), // preserves upper/lowercase for non-native events
|
|
293
|
+
nativeEventName = withoutOnOne.toLowerCase(),
|
|
294
|
+
listener = ( ev: McEvent | Event | CustomEvent ) => { that.trigger( withoutOnOne, ev ); };
|
|
295
|
+
;
|
|
266
296
|
|
|
267
|
-
|
|
297
|
+
if ( once ){
|
|
298
|
+
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
299
|
+
Object.defineProperty(
|
|
300
|
+
this,
|
|
301
|
+
'on' + withoutOnOne,
|
|
302
|
+
{
|
|
303
|
+
configurable: true,
|
|
304
|
+
enumerable: false,
|
|
305
|
+
writable: true,
|
|
306
|
+
value: (ev: any) => {
|
|
307
|
+
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
308
|
+
( that.constructor.prototype[key] ).bind(that)(ev);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
if ( nativeEventNames.has( 'on' + nativeEventName ) ){ // native event defined in HTMLElement
|
|
315
|
+
// console.log('added as native listener', target, nativeEventName );
|
|
316
|
+
addListener( target, nativeEventName, listener, false, once );
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
}
|
|
268
321
|
|
|
269
|
-
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
class DC extends McBase {
|
|
325
|
+
|
|
326
|
+
constructor(target: HTMLElement ) {
|
|
270
327
|
super(target);
|
|
271
328
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
329
|
+
let that = this,
|
|
330
|
+
isDomMcInstance = that.constructor === DC;
|
|
331
|
+
|
|
332
|
+
setTimeout(
|
|
333
|
+
() => {
|
|
334
|
+
that.trigger('Init');
|
|
335
|
+
if ( !isDomMcInstance ) {
|
|
336
|
+
setTimeout(
|
|
337
|
+
() => {
|
|
338
|
+
let keys = Object.keys( (that as any)?._mc );
|
|
339
|
+
if ( keys.length ){
|
|
340
|
+
//console.log( 'inner Inits on [', that.constructor.name, ']', keys );
|
|
341
|
+
keys?.forEach( (key: string) => {
|
|
342
|
+
let mc = (that as any)?._mc?.[key];
|
|
343
|
+
//console.log( key, typeof mc?.oneInit, mc );
|
|
344
|
+
if ( mc?.oneInit && typeof mc?.oneInit == 'function' ){
|
|
345
|
+
mc.oneInit( new McEvent('Init') );
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
, 0
|
|
351
|
+
);
|
|
276
352
|
}
|
|
277
|
-
}
|
|
278
|
-
|
|
353
|
+
}
|
|
354
|
+
, 0
|
|
355
|
+
);
|
|
356
|
+
|
|
357
|
+
this.autoAttachListeners();
|
|
279
358
|
}
|
|
280
359
|
|
|
281
360
|
static debug: boolean = false;
|
|
@@ -284,6 +363,18 @@ class DC extends MC {
|
|
|
284
363
|
static remove: Function = remove;
|
|
285
364
|
static trigger: Function = trigger;
|
|
286
365
|
|
|
366
|
+
get _mc(): { [key:string]: any } {
|
|
367
|
+
return this._mc;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
trigger( ev: McEvent | string, data: any = {}, bubble?: string ) {
|
|
371
|
+
MC.prototype.trigger.call( this, ev, data, bubble );
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
autoAttachListeners() {
|
|
375
|
+
MC.prototype.autoAttachListeners.call( this );
|
|
376
|
+
};
|
|
377
|
+
|
|
287
378
|
parent(search: string = ''): any[] {
|
|
288
379
|
let target: HTMLElement = (this as any).target;
|
|
289
380
|
if (!isDOM(target)) return [];
|
|
@@ -315,51 +406,6 @@ class DC extends MC {
|
|
|
315
406
|
return getMicroComponents([...myDomElements], search);
|
|
316
407
|
}
|
|
317
408
|
|
|
318
|
-
autoAttachListeners(){
|
|
319
|
-
if ( !isBrowser() ) return;
|
|
320
|
-
|
|
321
|
-
const target = this.target;
|
|
322
|
-
|
|
323
|
-
if ( target instanceof HTMLElement ){
|
|
324
|
-
let that: LooseObject = this;
|
|
325
|
-
Object
|
|
326
|
-
.getOwnPropertyNames( that.constructor.prototype )
|
|
327
|
-
.filter( key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function' )
|
|
328
|
-
.forEach( key => {
|
|
329
|
-
let
|
|
330
|
-
once = /^one/.test(key),
|
|
331
|
-
withoutOnOne = key.replace( /^on[e]{0,1}/, '' ), // preserves upper/lowercase for non-native events
|
|
332
|
-
nativeEventName = withoutOnOne.toLowerCase(),
|
|
333
|
-
listener = ( ev: McEvent | Event | CustomEvent ) => { that.trigger( withoutOnOne, ev ); };
|
|
334
|
-
;
|
|
335
|
-
|
|
336
|
-
// console.log('autoAttach', nativeEventName, withoutOnOne, once, listener);
|
|
337
|
-
|
|
338
|
-
if ( once ){
|
|
339
|
-
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
340
|
-
Object.defineProperty(
|
|
341
|
-
this,
|
|
342
|
-
'on' + withoutOnOne,
|
|
343
|
-
{
|
|
344
|
-
configurable: true,
|
|
345
|
-
enumerable: false,
|
|
346
|
-
writable: true,
|
|
347
|
-
value: (ev: any) => {
|
|
348
|
-
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
349
|
-
( that.constructor.prototype[key] ).bind(that)(ev);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
if ( nativeEventNames.has( 'on' + nativeEventName ) ){ // native event defined in HTMLElement
|
|
356
|
-
// console.log('added as native listener', target, nativeEventName );
|
|
357
|
-
addListener( target, nativeEventName, listener, false, once );
|
|
358
|
-
}
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
409
|
};
|
|
364
410
|
|
|
365
411
|
export { addListener, removeListener, McBase, MC, DC, McEvent };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.customElements=e():t.customElements=e()}(this,(()=>(()=>{"use strict";var t={d:(e,
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.customElements=e():t.customElements=e()}(this,(()=>(()=>{"use strict";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),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{defineCE:()=>b,getCE:()=>y});var n=new Function("try {return this===window;}catch(e){ return false;}");let r=new Set;n()&&Object.keys(HTMLElement.prototype).filter((t=>/^on/.test(t))).forEach((t=>r.add(t)));class o{#t;#e=!1;#n=!1;constructor(t,e={},n="l"){this.#t=t,this.data=e,this.bubble=n}get type(){return this.#t}get stopped(){return this.#e}get immediateStopped(){return this.#n}stopPropagation(){this.#e=!0}stopImmediatePropagation(){this.stopPropagation(),this.#n=!0}}var i=new Function("try {return this===window;}catch(e){ return false;}");let s=new Set;function c(t,e){let n=[];return t.forEach((t=>{if(t&&t instanceof HTMLElement){const r=t?._mc;!e&&r?n=n.concat(Object.values(t._mc)):e&&r&&r[e]&&n.push(r[e])}})),n}function a(t,e,n=!0){let r=[];for(;t;){if(t._mc&&(e?e&&t._mc[e]&&r.push(t._mc[e]):r=r.concat(Object.values(t._mc)),n))return r;t=t.parentNode}return r}function u(t){return t&&t instanceof HTMLElement}function l(t,e,n={}){let r=t;if(!r._mc){let e=g?.debug;g.debug=!1,r._mc=new g(t),g.debug=e}const o=r._mc;return e.length&&!o[e]&&(o[e]=n,t instanceof HTMLElement&&t.setAttribute("_mc","")),n}function p(t,e){if(!e)return;const n=t?._mc||{};n&&n[e]&&delete n[e],!Object.keys(n).length&&t instanceof HTMLElement&&t.hasAttribute("_mc")&&t.removeAttribute("_mc")}function d(t,e,n={}){let r=t?._mc;r&&[...Object.values(r)].forEach((t=>{t?.trigger&&"function"==typeof t?.trigger&&t?.trigger(e,n)}))}i()&&Object.keys(HTMLElement.prototype).filter((t=>/^on/.test(t))).forEach((t=>s.add(t)));class f{#r;#o={};constructor(t){this.#r=new WeakRef(t),this.constructor!==g&&Object.defineProperty(this,"_mc",{get(){return this.#o}})}get target(){return this.#r.deref()}}class m extends f{constructor(t){super(t);let e=this;setTimeout((()=>{e.trigger("Init"),setTimeout((()=>{let t=Object.keys(e?._mc);t.length&&t?.forEach((t=>{let n=e?._mc?.[t];n?.oneInit&&"function"==typeof n?.oneInit&&n.oneInit(new o("Init"))}))}),0)}),0),this.autoAttachListeners()}static trigger=d;trigger(t,e={},n){let r=this;if(n=n||"l",r.constructor!==m&&r.constructor!==g){if(r instanceof m||r instanceof g){const r=this,s="string"!=typeof t?t:new o(t,e,n),c="on"+s.type[0].toUpperCase()+s.type.slice(1);if(s.bubble.indexOf("l")>-1&&(s.immediateStopped||"function"!=typeof r[c]||(r[c](s),r.hasOwnProperty(c)&&delete r[c])),s.stopped||"l"===s.bubble)return r;i()&&/[ud]/.test(s.bubble)&&r instanceof m&&setTimeout((()=>{-1===s.bubble.indexOf("l")&&(s.bubble+="l"),s.bubble.indexOf("u")>-1&&r.parent().forEach((t=>{t instanceof m&&t.trigger(s)})),s.bubble.indexOf("d")>-1&&r.children().forEach((t=>{t instanceof m&&t.trigger(s)}))}),0)}}else[...Object.getOwnPropertyNames(r)].forEach((o=>{r[o]?.trigger&&"function"==typeof r[o]?.trigger&&r[o]?.trigger(t,e,n)}))}autoAttachListeners(){if(!i())return;const t=this.target;if(t instanceof HTMLElement){let e=this;Object.getOwnPropertyNames(e.constructor.prototype).filter((t=>/^on[A-Z]|one[A-Z]/.test(t)&&"function"==typeof e.constructor.prototype[t])).forEach((n=>{let r=/^one/.test(n),o=n.replace(/^on[e]{0,1}/,""),i=o.toLowerCase();r&&Object.defineProperty(this,"on"+o,{configurable:!0,enumerable:!1,writable:!0,value:t=>{e.constructor.prototype[n].bind(e)(t)}}),s.has("on"+i)&&function(t,e,n,r=!1,o=!1){let i={once:!1};r&&(i.capture=r),o&&(i.once=o),t.addEventListener(e,n,i)}(t,i,(t=>{e.trigger(o,t)}),!1,r)}))}}}class g extends f{constructor(t){super(t);let e=this,n=e.constructor===g;setTimeout((()=>{e.trigger("Init"),n||setTimeout((()=>{let t=Object.keys(e?._mc);t.length&&t?.forEach((t=>{let n=e?._mc?.[t];n?.oneInit&&"function"==typeof n?.oneInit&&n.oneInit(new o("Init"))}))}),0)}),0),this.autoAttachListeners()}static debug=!1;static add=l;static remove=p;static trigger=d;get _mc(){return this._mc}trigger(t,e={},n){m.prototype.trigger.call(this,t,e,n)}autoAttachListeners(){m.prototype.autoAttachListeners.call(this)}parent(t=""){let e=this.target;return u(e)?a(e.parentNode,t,!0):[]}ancestors(t=""){let e=this?.target;return u(e)?a(this.target.parentNode,t,!1):[]}children(t=""){let e=this?.target;if(!u(e))return[];const n=Math.random().toString().replace(".",""),r='[_mc]:not([temp_id="'+n+'"] [_mc] [_mc])';e.setAttribute("temp_id",n);const o=c([...e.querySelectorAll(r)],t);return e.removeAttribute("temp_id"),o}descendants(t=""){let e=this?.target;return u(e)?c([...e.querySelectorAll("*[_mc]")],t):[]}}class h extends HTMLElement{constructor(){super()}connectedCallback(){g.trigger(this,"Connected")}disconnectedCallback(){g.trigger(this,"Disconnected")}adoptedCallback(){g.trigger(this,"Adopted")}attributeChangedCallback(t,e,n){g.trigger(this,"AttributeChanged",{name:t,oldValue:e,newValue:n})}}function b(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=t.split("-").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join("");customElements.define(t,function(t,e){return Object.defineProperty(e,"name",{value:t})}(r,class extends h{instance;constructor(t){super();let n=new e(this,t);g.add(this,r,n)}static get observedAttributes(){return n}}))}function y(t){return customElements.get(t)}return e})()));
|