@twobirds/microcomponents 0.0.34 → 0.0.39

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.
Files changed (120) hide show
  1. package/README.md +40 -27
  2. package/assets/documentation/{MicroComponents.md → MC.md} +17 -17
  3. package/assets/documentation/comparision.md +0 -0
  4. package/assets/documentation/elements.md +276 -0
  5. package/assets/documentation/magic.md +10 -68
  6. package/assets/my-tag.css +6 -2
  7. package/dist/MC.d.ts +3 -4
  8. package/dist/MC.d.ts.map +1 -1
  9. package/dist/MC.min.js +89 -66
  10. package/dist/MC.ts +213 -178
  11. package/dist/autoloader.ts +1 -1
  12. package/dist/{customElements.d.ts → elements.d.ts} +1 -1
  13. package/dist/elements.d.ts.map +1 -0
  14. package/dist/elements.min.js +1 -0
  15. package/dist/{customElements.ts → elements.ts} +23 -14
  16. package/dist/helpers.d.ts +6 -4
  17. package/dist/helpers.d.ts.map +1 -1
  18. package/dist/helpers.ts +95 -128
  19. package/dist/microcomponents.d.ts +3 -3
  20. package/dist/microcomponents.d.ts.map +1 -1
  21. package/dist/microcomponents.min.js +1 -1
  22. package/dist/microcomponents.ts +3 -3
  23. package/dist/{customObservables.d.ts → observables.d.ts} +3 -3
  24. package/dist/observables.d.ts.map +1 -0
  25. package/dist/observables.min.js +1 -0
  26. package/dist/observables.ts +353 -0
  27. package/dist/{customSelectors.d.ts → selectors.d.ts} +1 -1
  28. package/dist/selectors.d.ts.map +1 -0
  29. package/dist/selectors.min.js +1 -0
  30. package/dist/test.ts +135 -156
  31. package/klemmstein.png +0 -0
  32. package/package.json +52 -52
  33. package/public/MC.js +90 -67
  34. package/public/MC.js.map +1 -1
  35. package/public/MC.min.js +89 -66
  36. package/public/assets/search.js +1 -1
  37. package/public/autoloader.js +1 -1
  38. package/public/autoloader.js.map +1 -1
  39. package/public/classes/{DC.html → MC.DC.html} +60 -59
  40. package/public/classes/{MC.html → MC.MC.html} +41 -35
  41. package/public/classes/{McBase.html → MC.McBase.html} +28 -20
  42. package/public/classes/{McEvent.html → MC.McEvent.html} +48 -42
  43. package/public/documentation/{MicroComponents.md → MC.md} +17 -17
  44. package/public/documentation/comparision.md +0 -0
  45. package/public/documentation/elements.md +276 -0
  46. package/public/documentation/magic.md +10 -68
  47. package/public/{customElements.js → elements.js} +9 -10
  48. package/public/elements.js.map +1 -0
  49. package/public/elements.min.js +1 -0
  50. package/public/functions/{addListener.html → MC.addListener.html} +18 -12
  51. package/public/functions/{removeListener.html → MC.removeListener.html} +18 -12
  52. package/public/functions/elements.defineCE.html +78 -0
  53. package/public/functions/elements.getCE.html +63 -0
  54. package/public/functions/formValues.formValues.html +68 -0
  55. package/public/functions/observables.observe.html +65 -0
  56. package/public/functions/selectors._d.html +69 -0
  57. package/public/functions/selectors._t.html +69 -0
  58. package/public/helpers.js +66 -89
  59. package/public/helpers.js.map +1 -1
  60. package/public/index.html +48 -33
  61. package/public/interfaces/{HTMLMcElement.html → MC.HTMLMcElement.html} +601 -595
  62. package/public/interfaces/selectors.DSelector.html +1013 -0
  63. package/public/interfaces/selectors.TSelector.html +712 -0
  64. package/public/microcomponents.js +3 -3
  65. package/public/microcomponents.js.map +1 -1
  66. package/public/microcomponents.min.js +1 -1
  67. package/public/modules/MC.html +78 -0
  68. package/public/modules/elements.html +62 -0
  69. package/public/modules/formValues.html +60 -0
  70. package/public/modules/observables.html +65 -0
  71. package/public/modules/selectors.html +76 -0
  72. package/public/modules.html +11 -23
  73. package/public/my-tag.css +6 -2
  74. package/public/observables.js +259 -0
  75. package/public/observables.js.map +1 -0
  76. package/public/observables.min.js +1 -0
  77. package/public/{customSelectors.js → selectors.js} +1 -1
  78. package/public/selectors.js.map +1 -0
  79. package/public/selectors.min.js +1 -0
  80. package/public/src/MC.ts +413 -0
  81. package/public/src/McEvent.ts +96 -0
  82. package/public/src/autoloader.ts +98 -0
  83. package/{src/customElements.ts → public/src/elements.ts} +23 -14
  84. package/public/src/formValues.ts +275 -0
  85. package/public/src/helpers.ts +140 -0
  86. package/public/src/microcomponents.ts +8 -0
  87. package/public/src/observables.ts +353 -0
  88. package/public/src/test.ts +190 -0
  89. package/public/test.js +93 -118
  90. package/public/test.js.map +1 -1
  91. package/public/types/observables.Observable.html +91 -0
  92. package/public/types/selectors.selection.html +58 -0
  93. package/public/types/selectors.singleSelector.html +58 -0
  94. package/src/MC.ts +213 -178
  95. package/src/autoloader.ts +1 -1
  96. package/src/elements.ts +76 -0
  97. package/src/helpers.ts +95 -128
  98. package/src/microcomponents.ts +3 -3
  99. package/src/observables.ts +353 -0
  100. package/src/selectors.ts +1634 -0
  101. package/src/test.ts +135 -156
  102. package/typedoc.json +1 -1
  103. package/webpack.config.js +9 -9
  104. package/dist/customElements.d.ts.map +0 -1
  105. package/dist/customElements.min.js +0 -1
  106. package/dist/customObservables.d.ts.map +0 -1
  107. package/dist/customObservables.min.js +0 -1
  108. package/dist/customObservables.ts +0 -348
  109. package/dist/customSelectors.d.ts.map +0 -1
  110. package/dist/customSelectors.min.js +0 -1
  111. package/public/customElements.js.map +0 -1
  112. package/public/customElements.min.js +0 -1
  113. package/public/customObservables.js +0 -256
  114. package/public/customObservables.js.map +0 -1
  115. package/public/customObservables.min.js +0 -1
  116. package/public/customSelectors.js.map +0 -1
  117. package/public/customSelectors.min.js +0 -1
  118. package/src/customObservables.ts +0 -348
  119. /package/dist/{customSelectors.ts → selectors.ts} +0 -0
  120. /package/{src/customSelectors.ts → public/src/selectors.ts} +0 -0
package/dist/MC.min.js CHANGED
@@ -136,16 +136,18 @@ function traverseNodes(node, search, firstonly = true) {
136
136
  ;// ./build/MC.js
137
137
 
138
138
 
139
- var MC_isBrowser = new Function("try {return this===window;}catch(e){ return false;}");
139
+ var MC_isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
140
140
  let MC_nativeEventNames = new Set();
141
141
  // HTMLElement native event names
142
142
  if (MC_isBrowser()) {
143
- Object.keys(HTMLElement.prototype).filter(key => /^on/.test(key)).forEach(eventName => MC_nativeEventNames.add(eventName));
143
+ Object.keys(HTMLElement.prototype)
144
+ .filter((key) => /^on/.test(key))
145
+ .forEach((eventName) => MC_nativeEventNames.add(eventName));
144
146
  }
145
147
  // get all microcomponents from a list of HTMLElements
146
148
  function MC_getMicroComponents(elements, search) {
147
149
  let mcValues = [];
148
- elements.forEach(e => {
150
+ elements.forEach((e) => {
149
151
  if (e) {
150
152
  if (e instanceof HTMLElement) {
151
153
  const _mc = e?._mc;
@@ -227,44 +229,51 @@ function remove(target, key) {
227
229
  delete _mc[key];
228
230
  }
229
231
  // remove the _mc attribute if the DOM collection is empty
230
- if (!Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('_mc')) {
232
+ if (!Object.keys(_mc).length &&
233
+ target instanceof HTMLElement &&
234
+ target.hasAttribute('_mc')) {
231
235
  target.removeAttribute('_mc');
232
236
  }
233
237
  return;
234
238
  }
235
239
  // trigger an event
236
- function trigger(target, ev, data = {}) {
240
+ function trigger(target, ev, data = {}, bubble = 'l') {
237
241
  let _mc = target?._mc;
238
242
  if (!_mc)
239
243
  return;
244
+ let event = ev instanceof McEvent
245
+ ? new McEvent(ev.type, ev.data, 'l')
246
+ : new McEvent(ev, data, 'l'), mcEvent = typeof ev !== 'string' ? ev : new McEvent(ev, data, bubble);
240
247
  [...Object.values(_mc)].forEach((mc) => {
241
248
  //console.log( 'mc [', mc.constructor.name, ']', mc.constructor.prototype?.oneInit );
242
249
  if (mc?.trigger && typeof mc?.trigger === 'function') {
243
- mc?.trigger(ev, data);
250
+ mc?.trigger(event);
244
251
  }
245
252
  });
253
+ if (/[ud]/.test(mcEvent.bubble)) {
254
+ bubbleEvent(target, mcEvent);
255
+ }
246
256
  return;
247
257
  }
248
- // run the "init" lifecycle event on the completemcr structure
258
+ // run the "init" lifecycle event on the complete _mc structure
249
259
  function init(element) {
250
260
  if (element.constructor === DC)
251
261
  return;
252
262
  setTimeout(() => {
253
263
  element.trigger('Init');
254
- setTimeout(() => {
255
- let keys = Object.keys(element?._mc);
256
- if (keys.length) {
264
+ /*
265
+ let keys = Object.keys( ( element as any)?._mc );
266
+ if ( keys.length ){
257
267
  //console.log( 'inner Inits on [', mc.constructor.name, ']', keys );
258
- keys?.forEach((key) => {
259
- let mc = element?._mc?.[key];
268
+ keys?.forEach( (key: string) => {
269
+ let mc = ( element as any)?._mc?.[key];
260
270
  //console.log( key, typeof mc?.oneInit, mc );
261
- if (mc['oneInit'] && typeof mc.oneInit == 'function') {
262
- mc.oneInit(new McEvent('Init'));
271
+ if ( ( mc as any )['oneInit'] && typeof ( mc as any ).oneInit == 'function' ){
272
+ ( mc as any ).oneInit( new McEvent('Init') );
263
273
  }
264
274
  });
265
- }
266
- ;
267
- }, 0);
275
+ };
276
+ */
268
277
  }, 0);
269
278
  }
270
279
  function autoAttachListeners(element) {
@@ -273,13 +282,14 @@ function autoAttachListeners(element) {
273
282
  const target = element.target;
274
283
  if (target instanceof HTMLElement) {
275
284
  let that = element;
276
- Object
277
- .getOwnPropertyNames(that.constructor.prototype)
278
- .filter(key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function')
279
- .forEach(key => {
285
+ Object.getOwnPropertyNames(that.constructor.prototype)
286
+ .filter((key) => /^on[A-Z]|one[A-Z]/.test(key) &&
287
+ typeof that.constructor.prototype[key] === 'function')
288
+ .forEach((key) => {
280
289
  let once = /^one/.test(key), withoutOnOne = key.replace(/^on[e]{0,1}/, ''), // preserves upper/lowercase for non-native events
281
- nativeEventName = withoutOnOne.toLowerCase(), listener = (ev) => { that.trigger(withoutOnOne, ev); };
282
- ;
290
+ nativeEventName = withoutOnOne.toLowerCase(), listener = (ev) => {
291
+ that.trigger(withoutOnOne, ev);
292
+ };
283
293
  if (once) {
284
294
  // console.log( '...is only called once', that.constructor.prototype[key] );
285
295
  Object.defineProperty(element, 'on' + withoutOnOne, {
@@ -288,20 +298,49 @@ function autoAttachListeners(element) {
288
298
  writable: true,
289
299
  value: (ev) => {
290
300
  // console.log( 'onceHandler execute:', that.constructor.prototype[key] );
291
- (that.constructor.prototype[key]).bind(that)(ev);
292
- }
301
+ that.constructor.prototype[key].bind(that)(ev);
302
+ },
293
303
  });
294
304
  }
295
- if (MC_nativeEventNames.has('on' + nativeEventName)) { // native event defined in HTMLElement
305
+ if (MC_nativeEventNames.has('on' + nativeEventName)) {
306
+ // native event defined in HTMLElement
296
307
  // console.log('added as native listener', target, nativeEventName );
297
308
  addListener(target, nativeEventName, listener, false, once);
298
309
  }
299
310
  });
300
311
  }
301
312
  }
313
+ function bubbleEvent(target, ev) {
314
+ // at this point, either 'u' or 'd' bubbling should be indicated
315
+ // mcEvent bubbling is asynchronous, and the order of target MC execution in case of multiple matches is not guaranteed!
316
+ if (/[ud]/.test(ev.bubble)) {
317
+ setTimeout(() => {
318
+ // bubbling must include 'l' for local, or else no handlers are executed'
319
+ if (ev.bubble.indexOf('l') === -1) {
320
+ ev.bubble += 'l';
321
+ }
322
+ // bubble up towards root
323
+ if (ev.bubble.indexOf('u') > -1) {
324
+ let parent = MC_traverseNodes(target, '', true)?.[0].target;
325
+ if (parent)
326
+ DC.trigger(parent, ev);
327
+ }
328
+ // bubble down
329
+ if (ev.bubble.indexOf('d') > -1) {
330
+ let targets = new Set(target._mc
331
+ .children()
332
+ .map((c) => c?.target));
333
+ targets.forEach((target) => {
334
+ if (target)
335
+ DC.trigger(target, ev);
336
+ });
337
+ }
338
+ }, 0);
339
+ }
340
+ }
302
341
  class McBase {
303
342
  #target; // avoid circular reference
304
- constructor(target) {
343
+ constructor(target, debug = false) {
305
344
  let that = this, element = that;
306
345
  this.#target = new WeakRef(target || {});
307
346
  init(element); // async lifecycle event
@@ -317,30 +356,40 @@ class MC extends McBase {
317
356
  super(target);
318
357
  }
319
358
  static trigger = trigger;
320
- trigger(ev, data = {}, bubble) {
359
+ trigger(ev, data = {}, bubble = 'l') {
321
360
  let that = this;
322
361
  //console.log( that, '.trigger(', ev, data, bubble, ')' );
323
- bubble = bubble || 'l';
324
362
  // run all triggers
325
363
  if (that.constructor === MC || that.constructor === DC) {
364
+ let event = ev instanceof McEvent
365
+ ? new McEvent(ev.type, ev.data, 'l')
366
+ : new McEvent(ev, data, 'l'), mcEvent = typeof ev !== 'string'
367
+ ? ev
368
+ : new McEvent(ev, data, bubble);
326
369
  [...Object.getOwnPropertyNames(that)].forEach((mc) => {
327
- if (that[mc]?.trigger && typeof that[mc]?.trigger === 'function') {
328
- that[mc]?.trigger(ev, data, bubble);
370
+ if (that[mc]?.trigger &&
371
+ typeof that[mc]?.trigger === 'function') {
372
+ that[mc]?.trigger(event);
329
373
  }
330
374
  });
375
+ if (/[ud]/.test(mcEvent.bubble)) {
376
+ bubbleEvent(that.target, mcEvent);
377
+ }
331
378
  return; // must return here!
332
379
  }
333
380
  // only this instance
334
381
  if (that instanceof MC || that instanceof DC) {
335
- const that = this, mcEvent = typeof ev !== 'string' ? ev : new McEvent(ev, data, bubble), handlerName = 'on' + mcEvent.type[0].toUpperCase() + mcEvent.type.slice(1);
336
- ;
382
+ const that = this, mcEvent = typeof ev !== 'string'
383
+ ? ev
384
+ : new McEvent(ev, data, bubble), handlerName = 'on' + mcEvent.type[0].toUpperCase() + mcEvent.type.slice(1);
337
385
  // console.log('trigger', handlerName, mcEvent, data, bubble );
338
386
  if (mcEvent.bubble.indexOf('l') > -1) {
339
- if (!mcEvent.immediateStopped
340
- && typeof that[handlerName] === 'function') {
387
+ if (!mcEvent.immediateStopped &&
388
+ typeof that[handlerName] === 'function') {
341
389
  // console.log( 'execute handler:', handlerName );
342
390
  that[handlerName](mcEvent);
343
- if (that.hasOwnProperty(handlerName)) { // it is a once handler
391
+ if (that.hasOwnProperty(handlerName)) {
392
+ // it is a once handler
344
393
  delete that[handlerName]; // remove after first execution
345
394
  }
346
395
  }
@@ -349,49 +398,24 @@ class MC extends McBase {
349
398
  if (mcEvent.stopped || mcEvent.bubble === 'l') {
350
399
  return that;
351
400
  }
352
- // at this point, either 'u' or 'd' bubbling should be indicated
353
- // mcEvent bubbling is asynchronous, and the order of target MC execution in case of multiple matches is not guaranteed!
354
- if (MC_isBrowser() && /[ud]/.test(mcEvent.bubble) && that instanceof MC) {
355
- setTimeout(() => {
356
- // bubbling must include 'l' for local, or else no handlers are executed'
357
- if (mcEvent.bubble.indexOf('l') === -1) {
358
- mcEvent.bubble += 'l';
359
- }
360
- // bubble up towards root
361
- if (mcEvent.bubble.indexOf('u') > -1) {
362
- that
363
- .parent()
364
- .forEach((mc) => {
365
- mc instanceof MC ? mc.trigger(mcEvent) : null;
366
- });
367
- }
368
- // bubble down
369
- if (mcEvent.bubble.indexOf('d') > -1) {
370
- that
371
- .children()
372
- .forEach((mc) => {
373
- mc instanceof MC ? mc.trigger(mcEvent) : null;
374
- });
375
- }
376
- }, 0);
401
+ //console.log('bubble', mcEvent);
402
+ if (/[ud]/.test(mcEvent.bubble)) {
403
+ bubbleEvent(that.target, mcEvent);
377
404
  }
378
405
  }
379
406
  }
380
407
  }
381
- ;
382
408
  class DC extends McBase {
383
409
  _mc = {};
384
- constructor(target) {
410
+ constructor(target, debug = false) {
385
411
  super(target);
386
412
  }
387
- static debug = false;
388
413
  static add = add;
389
414
  static remove = remove;
390
415
  static trigger = trigger;
391
416
  trigger(ev, data = {}, bubble) {
392
417
  MC.prototype.trigger.call(this, ev, data, bubble);
393
418
  }
394
- ;
395
419
  parent(search = '') {
396
420
  let target = this.target;
397
421
  if (!MC_isDOM(target))
@@ -424,7 +448,6 @@ class DC extends McBase {
424
448
  return MC_getMicroComponents([...myDomElements], search);
425
449
  }
426
450
  }
427
- ;
428
451
 
429
452
 
430
453
  /******/ return __webpack_exports__;