@twobirds/microcomponents 0.0.26 → 0.0.28
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/assets/3d.html +42 -0
- package/assets/test.html +0 -1
- package/dist/MC.d.ts +8 -1
- package/dist/MC.d.ts.map +1 -1
- package/dist/MC.min.js +64 -51
- package/dist/MC.ts +90 -73
- package/dist/customElements.min.js +1 -1
- package/dist/microcomponents.min.js +1 -1
- package/dist/test.ts +21 -7
- package/package.json +3 -3
- package/public/3d.html +42 -0
- package/public/MC.js +64 -51
- package/public/MC.js.map +1 -1
- package/public/MC.min.js +64 -51
- package/public/assets/search.js +1 -1
- package/public/classes/DC.html +44 -69
- package/public/classes/MC.html +41 -33
- package/public/classes/McBase.html +7 -30
- 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.html +0 -1
- package/public/test.js +15 -6
- package/public/test.js.map +1 -1
- package/src/MC.ts +90 -73
- package/src/test.ts +21 -7
package/src/MC.ts
CHANGED
|
@@ -128,7 +128,8 @@ function remove( target: HTMLMcElement, key: string) {
|
|
|
128
128
|
function trigger( target: unknown, ev: McEvent | string, data: any = {}) {
|
|
129
129
|
let _mc = (target as any)?._mc;
|
|
130
130
|
if ( !_mc ) return;
|
|
131
|
-
[ ...Object.values( _mc ) ].forEach( (mc) => {
|
|
131
|
+
[ ...Object.values( _mc ) ].forEach( (mc: any) => {
|
|
132
|
+
//console.log( 'mc [', mc.constructor.name, ']', mc.constructor.prototype?.oneInit );
|
|
132
133
|
if ( (mc as any)?.trigger && typeof (mc as any)?.trigger === 'function' ){
|
|
133
134
|
(mc as any)?.trigger( ev, data );
|
|
134
135
|
}
|
|
@@ -136,22 +137,94 @@ function trigger( target: unknown, ev: McEvent | string, data: any = {}) {
|
|
|
136
137
|
return;
|
|
137
138
|
}
|
|
138
139
|
|
|
140
|
+
// run the "init" lifecycle event on the completemcr structure
|
|
141
|
+
function init( element: MC | DC ){
|
|
142
|
+
let isDomMcInstance = element.constructor === DC;
|
|
143
|
+
|
|
144
|
+
setTimeout(
|
|
145
|
+
() => {
|
|
146
|
+
element.trigger('Init');
|
|
147
|
+
if ( !isDomMcInstance ) {
|
|
148
|
+
setTimeout(
|
|
149
|
+
() => {
|
|
150
|
+
let keys = Object.keys( element?._mc );
|
|
151
|
+
if ( keys.length ){
|
|
152
|
+
//console.log( 'inner Inits on [', mc.constructor.name, ']', keys );
|
|
153
|
+
keys?.forEach( (key: string) => {
|
|
154
|
+
let mc = element?._mc?.[key];
|
|
155
|
+
//console.log( key, typeof mc?.oneInit, mc );
|
|
156
|
+
if ( ( mc as any )['oneInit'] && typeof ( mc as any ).oneInit == 'function' ){
|
|
157
|
+
( mc as any ).oneInit( new McEvent('Init') );
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
, 0
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
, 0
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function autoAttachListeners( element: MC | DC ){
|
|
172
|
+
if ( !isBrowser() || element.constructor === DC ) return;
|
|
173
|
+
|
|
174
|
+
const target = element.target;
|
|
175
|
+
|
|
176
|
+
if ( target instanceof HTMLElement ){
|
|
177
|
+
let that: LooseObject = element;
|
|
178
|
+
Object
|
|
179
|
+
.getOwnPropertyNames( that.constructor.prototype )
|
|
180
|
+
.filter( key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function' )
|
|
181
|
+
.forEach( key => {
|
|
182
|
+
let
|
|
183
|
+
once = /^one/.test(key),
|
|
184
|
+
withoutOnOne = key.replace( /^on[e]{0,1}/, '' ), // preserves upper/lowercase for non-native events
|
|
185
|
+
nativeEventName = withoutOnOne.toLowerCase(),
|
|
186
|
+
listener = ( ev: McEvent | Event | CustomEvent ) => { that.trigger( withoutOnOne, ev ); };
|
|
187
|
+
;
|
|
188
|
+
|
|
189
|
+
if ( once ){
|
|
190
|
+
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
191
|
+
Object.defineProperty(
|
|
192
|
+
element,
|
|
193
|
+
'on' + withoutOnOne,
|
|
194
|
+
{
|
|
195
|
+
configurable: true,
|
|
196
|
+
enumerable: false,
|
|
197
|
+
writable: true,
|
|
198
|
+
value: (ev: any) => {
|
|
199
|
+
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
200
|
+
( that.constructor.prototype[key] ).bind(that)(ev);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if ( nativeEventNames.has( 'on' + nativeEventName ) ){ // native event defined in HTMLElement
|
|
207
|
+
// console.log('added as native listener', target, nativeEventName );
|
|
208
|
+
addListener( target, nativeEventName, listener, false, once );
|
|
209
|
+
}
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
139
214
|
class McBase {
|
|
140
215
|
|
|
141
216
|
#target: WeakRef<any>; // avoid circular reference
|
|
142
217
|
|
|
143
|
-
#_mc: { [key:string]: any } = {};
|
|
144
|
-
|
|
145
218
|
constructor( target: { [key:string]: any } ) {
|
|
146
|
-
let that = this
|
|
219
|
+
let that: unknown = this,
|
|
220
|
+
element = (that as MC | DC );
|
|
147
221
|
|
|
148
222
|
this.#target = new WeakRef( target );
|
|
149
223
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
};
|
|
224
|
+
|
|
225
|
+
init( element ); // async lifecycle event
|
|
226
|
+
|
|
227
|
+
autoAttachListeners( element );
|
|
155
228
|
}
|
|
156
229
|
|
|
157
230
|
get target(): any {
|
|
@@ -162,22 +235,18 @@ class McBase {
|
|
|
162
235
|
|
|
163
236
|
class MC extends McBase {
|
|
164
237
|
|
|
238
|
+
_mc: { [key:string]: any } = {};
|
|
239
|
+
|
|
165
240
|
constructor(target: any) {
|
|
166
241
|
super(target);
|
|
167
|
-
let that = this;
|
|
168
|
-
|
|
169
|
-
setTimeout(
|
|
170
|
-
() => {
|
|
171
|
-
that.trigger('init');
|
|
172
|
-
}
|
|
173
|
-
,0
|
|
174
|
-
);
|
|
175
242
|
}
|
|
176
243
|
|
|
244
|
+
static trigger = trigger;
|
|
245
|
+
|
|
177
246
|
trigger( ev: McEvent | string, data: any = {}, bubble?: string ) {
|
|
178
247
|
let that = this;
|
|
179
248
|
|
|
180
|
-
//
|
|
249
|
+
//console.log( that, '.trigger(', ev, data, bubble, ')' );
|
|
181
250
|
bubble = bubble || 'l';
|
|
182
251
|
|
|
183
252
|
// run all triggers
|
|
@@ -259,17 +328,10 @@ class MC extends McBase {
|
|
|
259
328
|
|
|
260
329
|
class DC extends McBase {
|
|
261
330
|
|
|
331
|
+
protected __mc: { [key:string]: any } = {};
|
|
332
|
+
|
|
262
333
|
constructor(target: HTMLElement ) {
|
|
263
334
|
super(target);
|
|
264
|
-
|
|
265
|
-
let that = this;
|
|
266
|
-
|
|
267
|
-
setTimeout(
|
|
268
|
-
() => {
|
|
269
|
-
that.trigger('init');
|
|
270
|
-
}
|
|
271
|
-
,0
|
|
272
|
-
);
|
|
273
335
|
}
|
|
274
336
|
|
|
275
337
|
static debug: boolean = false;
|
|
@@ -279,7 +341,7 @@ class DC extends McBase {
|
|
|
279
341
|
static trigger: Function = trigger;
|
|
280
342
|
|
|
281
343
|
get _mc(): { [key:string]: any } {
|
|
282
|
-
return this.
|
|
344
|
+
return this.__mc;
|
|
283
345
|
}
|
|
284
346
|
|
|
285
347
|
trigger( ev: McEvent | string, data: any = {}, bubble?: string ) {
|
|
@@ -317,51 +379,6 @@ class DC extends McBase {
|
|
|
317
379
|
return getMicroComponents([...myDomElements], search);
|
|
318
380
|
}
|
|
319
381
|
|
|
320
|
-
autoAttachListeners(){
|
|
321
|
-
if ( !isBrowser() ) return;
|
|
322
|
-
|
|
323
|
-
const target = this.target;
|
|
324
|
-
|
|
325
|
-
if ( target instanceof HTMLElement ){
|
|
326
|
-
let that: LooseObject = this;
|
|
327
|
-
Object
|
|
328
|
-
.getOwnPropertyNames( that.constructor.prototype )
|
|
329
|
-
.filter( key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function' )
|
|
330
|
-
.forEach( key => {
|
|
331
|
-
let
|
|
332
|
-
once = /^one/.test(key),
|
|
333
|
-
withoutOnOne = key.replace( /^on[e]{0,1}/, '' ), // preserves upper/lowercase for non-native events
|
|
334
|
-
nativeEventName = withoutOnOne.toLowerCase(),
|
|
335
|
-
listener = ( ev: McEvent | Event | CustomEvent ) => { that.trigger( withoutOnOne, ev ); };
|
|
336
|
-
;
|
|
337
|
-
|
|
338
|
-
// console.log('autoAttach', nativeEventName, withoutOnOne, once, listener);
|
|
339
|
-
|
|
340
|
-
if ( once ){
|
|
341
|
-
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
342
|
-
Object.defineProperty(
|
|
343
|
-
this,
|
|
344
|
-
'on' + withoutOnOne,
|
|
345
|
-
{
|
|
346
|
-
configurable: true,
|
|
347
|
-
enumerable: false,
|
|
348
|
-
writable: true,
|
|
349
|
-
value: (ev: any) => {
|
|
350
|
-
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
351
|
-
( that.constructor.prototype[key] ).bind(that)(ev);
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if ( nativeEventNames.has( 'on' + nativeEventName ) ){ // native event defined in HTMLElement
|
|
358
|
-
// console.log('added as native listener', target, nativeEventName );
|
|
359
|
-
addListener( target, nativeEventName, listener, false, once );
|
|
360
|
-
}
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
|
|
365
382
|
};
|
|
366
383
|
|
|
367
384
|
export { addListener, removeListener, McBase, MC, DC, McEvent };
|
package/src/test.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import { MC, DC, McEvent } from "./
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
3
|
+
import { MC, DC, McEvent, defineCE, getCE, makeCO } from "./microcomponents.js";
|
|
4
|
+
//import { MC, DC, McEvent } from "./MC.js";
|
|
5
|
+
//import { defineCE, getCE } from "./customElements.js";
|
|
6
|
+
//import { makeCO } from "./customObservables.js";
|
|
6
7
|
|
|
7
8
|
//MC.debug = true;
|
|
8
9
|
|
|
@@ -14,6 +15,17 @@ class Adder {
|
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
class DeeplyNestedEvTarget extends MC {
|
|
19
|
+
constructor(target: any) {
|
|
20
|
+
super(target);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
oneInit(ev: McEvent) {
|
|
24
|
+
let logger = this.target?.target?.target?._mc?.logger;
|
|
25
|
+
logger?.warn("DeeplyNestedEvTarget .oneInit(ev)", ev);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
17
29
|
class EvTarget extends MC {
|
|
18
30
|
constructor(target: any) {
|
|
19
31
|
super(target);
|
|
@@ -25,8 +37,13 @@ class EvTarget extends MC {
|
|
|
25
37
|
}
|
|
26
38
|
|
|
27
39
|
oneInit(ev: McEvent) {
|
|
28
|
-
let
|
|
40
|
+
let that = this,
|
|
41
|
+
_mc = that._mc!,
|
|
42
|
+
logger = this.target?.target?._mc?.logger;
|
|
43
|
+
|
|
29
44
|
logger?.warn("EvTarget .oneInit(ev)", ev);
|
|
45
|
+
|
|
46
|
+
_mc.deeplyNestedEvTarget = new DeeplyNestedEvTarget( that );
|
|
30
47
|
}
|
|
31
48
|
}
|
|
32
49
|
|
|
@@ -85,9 +102,6 @@ defineCE(
|
|
|
85
102
|
|
|
86
103
|
// render
|
|
87
104
|
target.innerHTML = "Click Me";
|
|
88
|
-
|
|
89
|
-
// automatically attach native DOM event handlers
|
|
90
|
-
that.autoAttachListeners();
|
|
91
105
|
}
|
|
92
106
|
|
|
93
107
|
/*
|