@twobirds/microcomponents 0.1.20 → 0.1.21

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/README.md CHANGED
@@ -18,8 +18,6 @@ The distinguishing feature - compared to other frameworks - is its **minimalism*
18
18
 
19
19
  ## Status
20
20
 
21
- For a quick glance try [this](https://gitlab.com/twoBirds/mc_template_webcomponents)...
22
-
23
21
  In [manual testing](https://twobirds.gitlab.io/microcomponents/test/test.html) ( also a speed test).
24
22
 
25
23
  Check the [typedoc API reference](https://twobirds.gitlab.io/microcomponents/typedoc/index.html) if you want to drill into the source code.
@@ -30,9 +28,9 @@ The progress of coding is being streamed on [twitch](https://twitch.tv/frankieth
30
28
 
31
29
  ### **Micro Components**
32
30
 
33
- **Hint:** This code only shows how Micro Components are stored in the DOM.
31
+ **Hint:** The following code only shows how Micro Components are stored in the DOM.
34
32
 
35
- before:
33
+ html before:
36
34
  ```HTML
37
35
  <div></div>
38
36
  ```
@@ -69,7 +67,7 @@ element
69
67
  .helloWorld();
70
68
  ```
71
69
 
72
- after:
70
+ html after:
73
71
  ```HTML
74
72
  <div data-mc></div>
75
73
  ```
@@ -134,7 +132,7 @@ class MyMC extends DC{
134
132
  this.state.bind( target ); // links the state to DOM placeholders
135
133
 
136
134
  // attach your own callbacks
137
- this.state.observe( (value) => {
135
+ this.state.subscribe( (value) => {
138
136
  console.log(value);
139
137
  });
140
138
 
@@ -155,9 +153,10 @@ class MyMC extends DC{
155
153
  constructor( target ){
156
154
  super( target );
157
155
 
158
- this.form = { textinput: 'Hello world!' };
159
-
160
- this.target.innerHTML = '<input name="textinput" />';
156
+ this.target.innerHTML = /*HTML*/ `
157
+ <form>
158
+ <input name="textinput" value="Hallo Welt!"/>
159
+ </form>`;
161
160
 
162
161
  // link the form property to the DOM input fields
163
162
  formValues( this, 'form', this.target );
@@ -181,16 +180,13 @@ class MyMC extends DC{
181
180
  constructor( target ){
182
181
  super( target );
183
182
 
184
- // attach event handlers programatically
185
- const onClickHandler = this.on( 'click', () => {
186
- // ...
187
- });
188
-
189
- this.one( 'click', () => {
183
+ // attach event handler programatically
184
+ let onHoverHandler = this.on( 'hover', () => {
190
185
  // ...
191
186
  });
192
187
 
193
- this.off( 'click', onClickHandler );
188
+ // remove event handler
189
+ this.off( 'hover', onHoverHandler );
194
190
  }
195
191
 
196
192
  // this event handler is attached automatically
package/dist/MC.d.ts CHANGED
@@ -1,16 +1,8 @@
1
1
  import { LooseObject } from './helpers.js';
2
+ import { BubbleController } from './events.js';
2
3
  export interface HTMLMcElement extends HTMLElement {
3
4
  _mc: LooseObject;
4
5
  }
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
6
  declare class McBase {
15
7
  private _target;
16
8
  constructor(target?: {
@@ -35,5 +27,5 @@ declare class DC extends McBase {
35
27
  descendants(): DC[] | null;
36
28
  }
37
29
  declare function autoload(enable?: boolean): boolean;
38
- export { McBase, MC, DC, autoload, on, one, off, trigger };
30
+ export { MC, DC, McBase, autoload };
39
31
  //# sourceMappingURL=MC.d.ts.map
package/dist/MC.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"MC.d.ts","sourceRoot":"","sources":["MC.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAI3C,MAAM,WAAW,aAAc,SAAQ,WAAW;IACjD,GAAG,EAAE,WAAW,CAAC;CACjB;AAkED,iBAAS,GAAG,CAAC,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,EAA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E,EAAE,CACD,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,GACzC,kCAAkC;IAarC,GAAG,CACF,SAAS,EAAE,MAAM,EACjB,OAAO,EAAE,kCAAkC,GACzC,kCAAkC;IAcrC,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,GAAE,GAAQ,EAAE,MAAM,GAAE,MAAY,EAAE,UAAU,CAAC,EAAE,gBAAgB;IAc9F,SAAS,IAAI,EAAE,EAAE,GAAG,IAAI;IAsBxB,OAAO,IAAI,EAAE,GAAG,IAAI;IAcpB,QAAQ,IAAI,EAAE,EAAE,GAAG,IAAI;IAcvB,WAAW,IAAI,EAAE,EAAE,GAAG,IAAI;CAQ1B;AAwKD,iBAAS,QAAQ,CAAC,MAAM,GAAE,OAAc,GAAG,OAAO,CAuBjD;AAUD,OAAO,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC"}
package/dist/MC.js CHANGED
@@ -1,4 +1,5 @@
1
1
  'use strict';
2
+ import { trigger, on, one, off } from './events.js';
2
3
  const isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
3
4
  function isDOM(element) {
4
5
  return element && element instanceof HTMLElement;
@@ -36,60 +37,6 @@ function init(element) {
36
37
  element.trigger('Init');
37
38
  }, 0);
38
39
  }
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
40
  function autoAttachListeners(element) {
94
41
  if (!isBrowser() || element.constructor === DC)
95
42
  return;
@@ -278,22 +225,25 @@ function loadUndefinedMCs() {
278
225
  });
279
226
  });
280
227
  }
281
- let autoloadEnabled = false;
282
228
  let observer = null;
283
229
  function autoload(enable = true) {
284
230
  if (!enable) {
285
- observer?.disconnect();
286
- observer = null;
287
- return autoloadEnabled;
231
+ if (observer) {
232
+ observer.disconnect();
233
+ observer = null;
234
+ }
235
+ return false;
288
236
  }
289
- autoloadEnabled = enable;
237
+ const wasEnabled = !!observer;
238
+ if (wasEnabled)
239
+ return true;
290
240
  observer = new MutationObserver(loadUndefinedMCs);
291
241
  setTimeout(() => {
292
242
  observer.observe(document.body, { childList: true, subtree: true });
293
243
  loadUndefinedMCs();
294
244
  }, 5);
295
245
  window.autoload = window.autoload || autoload;
296
- return autoloadEnabled;
246
+ return true;
297
247
  }
298
248
  autoload.state = loadingDCs;
299
- export { McBase, MC, DC, autoload, on, one, off, trigger };
249
+ export { MC, DC, McBase, autoload };
package/dist/MC.min.js CHANGED
@@ -21,54 +21,17 @@
21
21
  /******/
22
22
  /************************************************************************/
23
23
  var __webpack_exports__ = {};
24
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
25
- /* harmony export */ AU: () => (/* binding */ off),
26
- /* harmony export */ DC: () => (/* binding */ DC),
27
- /* harmony export */ E_: () => (/* binding */ autoload),
28
- /* harmony export */ MC: () => (/* binding */ MC),
29
- /* harmony export */ Oc: () => (/* binding */ McBase),
30
- /* harmony export */ hZ: () => (/* binding */ trigger),
31
- /* harmony export */ on: () => (/* binding */ on),
32
- /* harmony export */ xH: () => (/* binding */ one)
33
- /* harmony export */ });
34
24
 
35
- const isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
36
- function isDOM(element) {
37
- return element && element instanceof HTMLElement;
38
- }
39
- function add(target, key, element = {}) {
40
- let node = target;
41
- if (!node._mc) {
42
- node._mc = new DC(target);
43
- }
44
- const _mc = node._mc;
45
- if (key.length && !_mc[key]) {
46
- _mc[key] = element;
47
- if (target instanceof HTMLElement && !target.getAttribute('data-mc')) {
48
- target.setAttribute('data-mc', '');
49
- }
50
- }
51
- return node;
52
- }
53
- function remove(target, key) {
54
- if (!key)
55
- return;
56
- const _mc = target?._mc || {};
57
- if (_mc && _mc[key]) {
58
- delete _mc[key];
59
- }
60
- if (!Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('data-mc')) {
61
- target.removeAttribute('data-mc');
62
- }
63
- return;
64
- }
65
- function init(element) {
66
- setTimeout(() => {
67
- if (element.constructor === DC)
68
- return;
69
- element.trigger('Init');
70
- }, 0);
71
- }
25
+ // EXPORTS
26
+ __webpack_require__.d(__webpack_exports__, {
27
+ DC: () => (/* binding */ DC),
28
+ MC: () => (/* binding */ MC),
29
+ O: () => (/* binding */ McBase),
30
+ E: () => (/* binding */ autoload)
31
+ });
32
+
33
+ ;// ./build/events.js
34
+
72
35
  function getMcEventName(eventName) {
73
36
  return '_mc' + eventName[0].toUpperCase() + eventName.substring(1);
74
37
  }
@@ -123,6 +86,47 @@ function trigger(domNode, event, data = {}, bubble = 'l', controller) {
123
86
  }, 0);
124
87
  }
125
88
  }
89
+
90
+ ;// ./build/MC.js
91
+
92
+
93
+ const isBrowser = new Function('try {return this===window;}catch(e){ return false;}');
94
+ function isDOM(element) {
95
+ return element && element instanceof HTMLElement;
96
+ }
97
+ function add(target, key, element = {}) {
98
+ let node = target;
99
+ if (!node._mc) {
100
+ node._mc = new DC(target);
101
+ }
102
+ const _mc = node._mc;
103
+ if (key.length && !_mc[key]) {
104
+ _mc[key] = element;
105
+ if (target instanceof HTMLElement && !target.getAttribute('data-mc')) {
106
+ target.setAttribute('data-mc', '');
107
+ }
108
+ }
109
+ return node;
110
+ }
111
+ function remove(target, key) {
112
+ if (!key)
113
+ return;
114
+ const _mc = target?._mc || {};
115
+ if (_mc && _mc[key]) {
116
+ delete _mc[key];
117
+ }
118
+ if (!Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('data-mc')) {
119
+ target.removeAttribute('data-mc');
120
+ }
121
+ return;
122
+ }
123
+ function init(element) {
124
+ setTimeout(() => {
125
+ if (element.constructor === DC)
126
+ return;
127
+ element.trigger('Init');
128
+ }, 0);
129
+ }
126
130
  function autoAttachListeners(element) {
127
131
  if (!isBrowser() || element.constructor === DC)
128
132
  return;
@@ -311,32 +315,31 @@ function loadUndefinedMCs() {
311
315
  });
312
316
  });
313
317
  }
314
- let autoloadEnabled = false;
315
318
  let observer = null;
316
319
  function autoload(enable = true) {
317
320
  if (!enable) {
318
- observer?.disconnect();
319
- observer = null;
320
- return autoloadEnabled;
321
+ if (observer) {
322
+ observer.disconnect();
323
+ observer = null;
324
+ }
325
+ return false;
321
326
  }
322
- autoloadEnabled = enable;
327
+ const wasEnabled = !!observer;
328
+ if (wasEnabled)
329
+ return true;
323
330
  observer = new MutationObserver(loadUndefinedMCs);
324
331
  setTimeout(() => {
325
332
  observer.observe(document.body, { childList: true, subtree: true });
326
333
  loadUndefinedMCs();
327
334
  }, 5);
328
335
  window.autoload = window.autoload || autoload;
329
- return autoloadEnabled;
336
+ return true;
330
337
  }
331
338
  autoload.state = loadingDCs;
332
339
 
333
340
 
334
341
  var __webpack_exports__DC = __webpack_exports__.DC;
335
342
  var __webpack_exports__MC = __webpack_exports__.MC;
336
- var __webpack_exports__McBase = __webpack_exports__.Oc;
337
- var __webpack_exports__autoload = __webpack_exports__.E_;
338
- var __webpack_exports__off = __webpack_exports__.AU;
339
- var __webpack_exports__on = __webpack_exports__.on;
340
- var __webpack_exports__one = __webpack_exports__.xH;
341
- var __webpack_exports__trigger = __webpack_exports__.hZ;
342
- export { __webpack_exports__DC as DC, __webpack_exports__MC as MC, __webpack_exports__McBase as McBase, __webpack_exports__autoload as autoload, __webpack_exports__off as off, __webpack_exports__on as on, __webpack_exports__one as one, __webpack_exports__trigger as trigger };
343
+ var __webpack_exports__McBase = __webpack_exports__.O;
344
+ var __webpack_exports__autoload = __webpack_exports__.E;
345
+ export { __webpack_exports__DC as DC, __webpack_exports__MC as MC, __webpack_exports__McBase as McBase, __webpack_exports__autoload as autoload };