@twobirds/microcomponents 0.0.83 → 0.0.85

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 (202) hide show
  1. package/README.md +22 -6
  2. package/assets/documentation/MC.md +60 -81
  3. package/assets/documentation/elements.md +6 -9
  4. package/assets/documentation/observables.md +97 -11
  5. package/assets/test/test.html +1 -1
  6. package/dist/MC.d.ts +14 -16
  7. package/dist/MC.d.ts.map +1 -1
  8. package/dist/MC.js +150 -218
  9. package/dist/MC.min.js +164 -308
  10. package/dist/MC.ts +213 -319
  11. package/dist/elements.d.ts.map +1 -1
  12. package/dist/elements.js +10 -4
  13. package/dist/elements.min.js +1 -1
  14. package/dist/elements.ts +17 -4
  15. package/dist/formValues.js +2 -2
  16. package/dist/formValues.min.js +1 -1
  17. package/dist/formValues.ts +3 -3
  18. package/dist/microcomponents.d.ts +0 -1
  19. package/dist/microcomponents.d.ts.map +1 -1
  20. package/dist/microcomponents.js +0 -1
  21. package/dist/microcomponents.min.js +1 -1
  22. package/dist/microcomponents.ts +1 -1
  23. package/dist/observables.d.ts.map +1 -1
  24. package/dist/observables.js +14 -12
  25. package/dist/observables.min.js +1 -1
  26. package/dist/observables.ts +32 -32
  27. package/package.json +1 -1
  28. package/public/MC.d.ts +14 -16
  29. package/public/MC.d.ts.map +1 -1
  30. package/public/MC.js +150 -218
  31. package/public/MC.js.map +1 -1
  32. package/public/MC.min.js +164 -308
  33. package/public/MC.ts +213 -319
  34. package/public/documentation/MC.md +60 -81
  35. package/public/documentation/elements.md +6 -9
  36. package/public/documentation/observables.md +97 -11
  37. package/public/elements.d.ts.map +1 -1
  38. package/public/elements.js +10 -4
  39. package/public/elements.js.map +1 -1
  40. package/public/elements.min.js +1 -1
  41. package/public/elements.ts +17 -4
  42. package/public/formValues.js +2 -2
  43. package/public/formValues.js.map +1 -1
  44. package/public/formValues.min.js +1 -1
  45. package/public/formValues.ts +3 -3
  46. package/public/microcomponents.d.ts +0 -1
  47. package/public/microcomponents.d.ts.map +1 -1
  48. package/public/microcomponents.js +0 -1
  49. package/public/microcomponents.js.map +1 -1
  50. package/public/microcomponents.min.js +1 -1
  51. package/public/microcomponents.ts +1 -1
  52. package/public/observables.d.ts.map +1 -1
  53. package/public/observables.js +14 -12
  54. package/public/observables.js.map +1 -1
  55. package/public/observables.min.js +1 -1
  56. package/public/observables.ts +32 -32
  57. package/public/src/MC.d.ts +14 -16
  58. package/public/src/MC.d.ts.map +1 -1
  59. package/public/src/MC.js +150 -218
  60. package/public/src/MC.js.map +1 -1
  61. package/public/src/MC.ts +213 -319
  62. package/public/src/elements.d.ts.map +1 -1
  63. package/public/src/elements.js +10 -4
  64. package/public/src/elements.js.map +1 -1
  65. package/public/src/elements.ts +17 -4
  66. package/public/src/formValues.js +2 -2
  67. package/public/src/formValues.js.map +1 -1
  68. package/public/src/formValues.ts +3 -3
  69. package/public/src/microcomponents.d.ts +0 -1
  70. package/public/src/microcomponents.d.ts.map +1 -1
  71. package/public/src/microcomponents.js +0 -1
  72. package/public/src/microcomponents.js.map +1 -1
  73. package/public/src/microcomponents.ts +1 -1
  74. package/public/src/observables.d.ts.map +1 -1
  75. package/public/src/observables.js +14 -12
  76. package/public/src/observables.js.map +1 -1
  77. package/public/src/observables.ts +32 -32
  78. package/public/src/test/test/grandparent.d.ts.map +1 -1
  79. package/public/src/test/test/grandparent.js +1 -1
  80. package/public/src/test/test/grandparent.js.map +1 -1
  81. package/public/src/test/test/grandparent.ts +12 -4
  82. package/public/src/test/test/testform.js +1 -1
  83. package/public/src/test/test/testform.js.map +1 -1
  84. package/public/src/test/test/testform.ts +6 -8
  85. package/public/src/test/test.js +6 -5
  86. package/public/src/test/test.js.map +1 -1
  87. package/public/src/test/test.ts +10 -9
  88. package/public/test/MC.d.ts +14 -16
  89. package/public/test/MC.d.ts.map +1 -1
  90. package/public/test/MC.js +150 -218
  91. package/public/test/MC.min.js +164 -308
  92. package/public/test/MC.ts +213 -319
  93. package/public/test/elements.d.ts.map +1 -1
  94. package/public/test/elements.js +10 -4
  95. package/public/test/elements.min.js +1 -1
  96. package/public/test/elements.ts +17 -4
  97. package/public/test/formValues.js +2 -2
  98. package/public/test/formValues.min.js +1 -1
  99. package/public/test/formValues.ts +3 -3
  100. package/public/test/microcomponents.d.ts +0 -1
  101. package/public/test/microcomponents.d.ts.map +1 -1
  102. package/public/test/microcomponents.js +0 -1
  103. package/public/test/microcomponents.min.js +1 -1
  104. package/public/test/microcomponents.ts +1 -1
  105. package/public/test/observables.d.ts.map +1 -1
  106. package/public/test/observables.js +14 -12
  107. package/public/test/observables.min.js +1 -1
  108. package/public/test/observables.ts +32 -32
  109. package/public/test/test/grandparent.d.ts.map +1 -1
  110. package/public/test/test/grandparent.js +1 -1
  111. package/public/test/test/grandparent.js.map +1 -1
  112. package/public/test/test/grandparent.ts +12 -4
  113. package/public/test/test/testform.js +1 -1
  114. package/public/test/test/testform.js.map +1 -1
  115. package/public/test/test/testform.ts +6 -8
  116. package/public/test/test.html +1 -1
  117. package/public/test/test.js +6 -5
  118. package/public/test/test.js.map +1 -1
  119. package/public/test/test.ts +10 -9
  120. package/public/typedoc/assets/navigation.js +1 -1
  121. package/public/typedoc/assets/search.js +1 -1
  122. package/public/typedoc/classes/MC.DC.html +7 -6
  123. package/public/typedoc/classes/MC.MC.html +3 -6
  124. package/public/typedoc/classes/MC.McBase.html +3 -4
  125. package/public/typedoc/functions/MC.autoload.html +1 -1
  126. package/public/typedoc/functions/MC.off.html +1 -0
  127. package/public/typedoc/functions/{selectors._d.html → MC.on.html} +1 -1
  128. package/public/typedoc/functions/MC.one.html +1 -0
  129. package/public/typedoc/functions/MC.trigger.html +1 -0
  130. package/public/typedoc/functions/elements.defineCE.html +1 -1
  131. package/public/typedoc/functions/elements.getCE.html +1 -1
  132. package/public/typedoc/functions/formValues.formValues.html +1 -1
  133. package/public/typedoc/functions/observables.observe.html +1 -1
  134. package/public/typedoc/index.html +21 -10
  135. package/public/typedoc/interfaces/MC.HTMLMcElement.html +2 -2
  136. package/public/typedoc/modules/MC.html +6 -5
  137. package/public/typedoc/modules/elements.html +1 -1
  138. package/public/typedoc/modules/formValues.html +1 -1
  139. package/public/typedoc/modules/observables.html +1 -1
  140. package/public/typedoc/modules.html +0 -1
  141. package/public/typedoc/types/observables.Observable.html +1 -1
  142. package/src/MC.ts +213 -319
  143. package/src/elements.ts +17 -4
  144. package/src/formValues.ts +3 -3
  145. package/src/microcomponents.ts +1 -1
  146. package/src/observables.ts +32 -32
  147. package/src/test/test/grandparent.ts +12 -4
  148. package/src/test/test/testform.ts +6 -8
  149. package/src/test/test.ts +10 -9
  150. package/tsconfig.json +2 -2
  151. package/typedoc.json +1 -1
  152. package/webpack.config.js +2 -2
  153. package/dist/McEvent.d.ts +0 -15
  154. package/dist/McEvent.d.ts.map +0 -1
  155. package/dist/McEvent.js +0 -71
  156. package/dist/selectors.d.ts +0 -106
  157. package/dist/selectors.d.ts.map +0 -1
  158. package/dist/selectors.js +0 -759
  159. package/dist/selectors.min.js +0 -1
  160. package/public/McEvent.d.ts +0 -15
  161. package/public/McEvent.d.ts.map +0 -1
  162. package/public/McEvent.js +0 -71
  163. package/public/McEvent.js.map +0 -1
  164. package/public/selectors.d.ts +0 -106
  165. package/public/selectors.d.ts.map +0 -1
  166. package/public/selectors.js +0 -759
  167. package/public/selectors.js.map +0 -1
  168. package/public/selectors.min.js +0 -1
  169. package/public/src/McEvent.d.ts +0 -15
  170. package/public/src/McEvent.d.ts.map +0 -1
  171. package/public/src/McEvent.js +0 -71
  172. package/public/src/McEvent.js.map +0 -1
  173. package/public/src/selectors.d.ts +0 -106
  174. package/public/src/selectors.d.ts.map +0 -1
  175. package/public/src/selectors.js +0 -759
  176. package/public/src/selectors.js.map +0 -1
  177. package/public/test/McEvent.d.ts +0 -15
  178. package/public/test/McEvent.d.ts.map +0 -1
  179. package/public/test/McEvent.js +0 -71
  180. package/public/test/McEvent.ts +0 -96
  181. package/public/test/selectors.d.ts +0 -106
  182. package/public/test/selectors.d.ts.map +0 -1
  183. package/public/test/selectors.js +0 -759
  184. package/public/test/selectors.min.js +0 -1
  185. package/public/test/selectors.ts +0 -1269
  186. package/public/typedoc/classes/MC.McEvent.html +0 -9
  187. package/public/typedoc/functions/MC.addListener.html +0 -1
  188. package/public/typedoc/functions/MC.removeListener.html +0 -1
  189. package/public/typedoc/functions/selectors._t.html +0 -1
  190. package/public/typedoc/interfaces/selectors.DSelector.html +0 -48
  191. package/public/typedoc/interfaces/selectors.TSelector.html +0 -34
  192. package/public/typedoc/modules/selectors.html +0 -7
  193. package/public/typedoc/types/selectors.selection.html +0 -1
  194. package/public/typedoc/types/selectors.singleSelector.html +0 -1
  195. package/src/McEvent.ts +0 -96
  196. package/src/selectors.ts +0 -1269
  197. /package/{dist/McEvent.ts → public/McEvent._ts} +0 -0
  198. /package/{dist/selectors.ts → public/selectors._ts} +0 -0
  199. /package/public/{McEvent.ts → src/McEvent._ts} +0 -0
  200. /package/public/{selectors.ts → src/selectors._ts} +0 -0
  201. /package/{public/src/McEvent.ts → src/McEvent._ts} +0 -0
  202. /package/{public/src/selectors.ts → src/selectors._ts} +0 -0
@@ -68,27 +68,43 @@ A special property attached to `HTMLElement` instances (when using `DC.add`) or
68
68
 
69
69
  Each microcomponent instance has a read-only `target` property providing a back-reference:
70
70
 
71
- - If the instance extends **`DC`** and was added via `DC.add`, `target` refers to the **DOM HTMLElement** it's attached to.
72
- - If the instance extends **`MC`** (or `DC`) and is nested within another component's `_mc` property, `target` refers to the **parent MC/DC instance**.
71
+ - If the instance extends **`DC`** `.target` refers to the **DOM HTMLElement** it's attached to.
72
+ - If the instance extends **`MC`** (or `DC`) and is nested within another component, `.target` refers to the **parent MC/DC instance**.
73
73
 
74
74
  This reference is managed using `WeakRef` internally by `McBase` to prevent circular references and potential memory leaks.
75
75
 
76
76
  ### Event System
77
77
 
78
- A custom event system using the `McEvent` class allows components to communicate.
78
+ A CustomEvent system allows components to communicate. These event handlers bubble differently than native events.
79
+
80
+ Any MC method name starting with `on` or `one` followed by an uppercase character will automatically be registered as a custom event handler in the DOM element.
81
+
82
+ The names maps like this:
83
+ **class method** `onHandlerMethod()` => **custom event handler** `_mcHandlerMethod`
84
+
85
+
86
+ - **Triggering:**
87
+
88
+ Events are dispatched using the `trigger` method:
89
+ - `myDomElement._mc.trigger('handlerMethod', { ... } )` ... from outside.
90
+ - `this.trigger( 'handlerMethod', data )` ...inside the current MC instance.
91
+
92
+ native:
93
+ - `myDomElement.dispatchEvent('_mcHandlerMethod', { detail: { ... } } )`;
94
+
79
95
 
80
- - **Triggering:** Events are dispatched using the `trigger` method:
81
- - `DC.trigger(targetElement, ...)`: Dispatches to all MCs on a specific DOM element. Handles bubbling (`u`/`d`).
82
- - `this.trigger(...)`: Dispatches within the current MC instance and its potential nested MCs. Can also initiate bubbling if the instance is a `DC`.
83
96
  - **Handling:**
84
97
  - Components listen for events by defining methods named `onEventName` or `oneEventName` (for once-off handling).
85
- - These are automatically detected by `autoAttachListeners`.
98
+ - These are automatically detected by `autoAttachListeners` internally.
86
99
  - Native DOM events matching this pattern on `DC` instances are also automatically bound to the target element.
87
- - **Bubbling:** Events triggered via `DC.trigger` or `dcInstance.trigger` can bubble:
100
+
101
+
102
+ - **Bubbling:** Events triggered via `dcInstance.trigger` can bubble:
88
103
  - `'l'`: Local only (default). Handlers on the target element's/instance's MCs are executed.
89
104
  - `'u'`: Upwards. Event propagates to MCs on parent elements (DOM tree).
90
105
  - `'d'`: Downwards. Event propagates to MCs on descendant elements (DOM tree).
91
- - Combinations are possible (e.g., `'lu'`, `'ldu'`). Bubbling (`u`/`d`) occurs asynchronously via `setTimeout`.
106
+ - Any combination of the characters 'l','u', 'd' are possible (e.g., `'lu'`, `'ldu'`).
107
+ - Bubbling (`u`/`d`) occurs asynchronously via `setTimeout`.
92
108
 
93
109
  ### Observables (Mention)
94
110
 
@@ -101,7 +117,7 @@ The `DC` class provides methods (`parent`, `ancestors`, `children`, `descendants
101
117
 
102
118
  ### Autoloading Concept
103
119
 
104
- An optional feature (`autoload()`) that automatically detects specific HTML tags (e.g., `<my-component>`) or elements with an `_mc="my-component"` attribute and attempts to dynamically load the corresponding JavaScript module (e.g., `/my/component.js`).
120
+ An optional feature (`autoload()`) that automatically detects specific HTML tags (e.g., `<my-component>`) or elements with an `data-mc="my-component"` attribute and attempts to dynamically load the corresponding JavaScript module (e.g., `/my/component.js`).
105
121
 
106
122
  ### Important Rule
107
123
 
@@ -128,7 +144,6 @@ Base class for non-DOM or server-side components. Extends `McBase`.
128
144
 
129
145
  - **Instance Properties**:
130
146
  - `.target`: (Inherited from `McBase`) Refers to the parent MC/DC instance it's nested within.
131
- - `._mc`: An object `{}` to hold nested microcomponent instances.
132
147
  - **Instance Methods**:
133
148
  - `.trigger(eventName, data)`: (Inherited) Triggers events locally within the instance and its nested `_mc` structure. Does not handle DOM bubbling.
134
149
 
@@ -138,26 +153,27 @@ Class for DOM-aware components. Extends `McBase`. **Client-side only.**
138
153
 
139
154
  - **Static Methods**:
140
155
  - `DC.add(domElement, key, mcInstance)`: Attaches an `mcInstance` (should be a `DC` or `MC`) to a `domElement` under the specified `key`. Creates `domElement._mc` if needed and sets the `_mc` attribute.
141
- - `DC.remove(domElement, key)`: Removes the MC instance associated with `key` from `domElement._mc`. Removes the `_mc` attribute if the collection becomes empty.
156
+ - `DC.remove(domElement, key)`: Removes the MC instance associated with `key` from `domElement._mc`. Removes the `data-mc` attribute if the collection becomes empty.
142
157
  - `DC.trigger(domElement, eventName, data, bubble)`: Triggers an event on all MCs attached to `domElement`. Handles DOM bubbling (`'l'`, `'u'`, `'d'`).
143
158
  - **Instance Properties**:
144
159
  - `.target`: (Inherited from `McBase`) Refers to the DOM HTMLElement this `DC` instance is attached to (via `DC.add`).
145
- - `._mc`: An object `{}` to hold nested microcomponent instances.
146
160
  - **Instance Methods**:
147
- - `.parent(search?)`: Gets MC instances from the closest parent element that has attached MCs. Optionally filters by `search` key.
148
- - `.ancestors(search?)`: Gets MC instances from all ancestor elements with attached MCs. Optionally filters by `search` key.
149
- - `.children(search?)`: Gets MC instances from direct child elements with attached MCs. Optionally filters by `search` key.
150
- - `.descendants(search?)`: Gets MC instances from all descendant elements with attached MCs. Optionally filters by `search` key.
151
- - `.trigger(eventName, data, bubble)`: Triggers an event on this `DC` instance and its nested `_mc` structure. Can initiate DOM bubbling (`'l'`, `'u'`, `'d'`).
161
+ - `.parent()`: Gets MC instances from the closest parent element that has attached MCs.
162
+ - `.ancestors()`: Gets MC instances from all ancestor elements with attached MCs.
163
+ - `.children()`: Gets MC instances from direct child elements with attached MCs.
164
+ - `.descendants()`: Gets MC instances from all descendant elements with attached MCs.
165
+ - `.trigger(eventName, data, bubble)`: Triggers an event on this `DC` instance. Can initiate DOM bubbling (`'l'`, `'u'`, `'d'`).
152
166
 
153
167
  ## Examples
154
168
 
155
169
  ### Typical Workflow
156
170
 
157
- 1. Define your component classes extending `MC` or `DC`.
158
- 2. Attach top-level `DC` instances to HTMLElements using `DC.add()`.
159
- 3. Nest `MC` or `DC` instances within others using the `._mc` property if needed.
160
- 4. Access other components via the `_mc` property, DOM traversal methods (`ancestors()`, `children()`, etc.), or event communication (`trigger`).
171
+ 1. Define your component classes extending `MC` or `DC`. These contain plain data, observable state, event callbacks and optional internal MCs.
172
+ 2. Attach defined top-level `DC` instances to HTMLElements using `DC.add()`.
173
+ 3. Then...
174
+ - Select and access other MCs using DOM traversal methods (`ancestors()`, `children()`, etc.)
175
+ - Dynamically attach event handlers (`on`, `one`, `off`).
176
+ - Communicate with other components via Events (`trigger`).
161
177
 
162
178
  ### Attach Micro Components (`DC.add`)
163
179
 
@@ -168,34 +184,38 @@ const myElement = document.getElementById('myDiv');
168
184
 
169
185
  // Define an inner MC (doesn't need DOM access itself)
170
186
  class MyInnerClass extends MC {
187
+
171
188
  constructor(target) {
172
189
  // target will be the parent MyPublicClass instance
173
190
  super(target);
174
191
  console.log('Inner MC target:', this.target);
175
192
  }
193
+
176
194
  doSomethingInner() {
177
195
  console.log('Inner action!');
178
196
  }
197
+
179
198
  }
180
199
 
181
200
  // Define an outer DC (needs DOM access or is top-level)
182
201
  class MyPublicClass extends DC {
202
+
183
203
  constructor(target) {
184
204
  // target will be myElement
185
205
  super(target);
186
206
  console.log('Outer DC target:', this.target);
187
207
  // Add an inner MC instance, nested within this DC instance
188
- this._mc.myInner = new MyInnerClass(this); // 'this' becomes the target for MyInnerClass
208
+ this.myInner = new MyInnerClass(this); // 'this' becomes the target for MyInnerClass
189
209
  }
210
+
190
211
  doSomethingOuter() {
191
212
  console.log('Outer action!');
192
213
  }
193
214
 
194
- // Example DOM event handler
195
215
  onClick(event) {
196
216
  console.log('Div clicked!', event);
197
217
  // Access nested MC
198
- this._mc.myInner?.doSomethingInner();
218
+ this.myInner.doSomethingInner();
199
219
  }
200
220
  }
201
221
 
@@ -217,12 +237,13 @@ const myElement = document.getElementById('myDiv');
217
237
 
218
238
  DC.remove(myElement, 'myComponent');
219
239
 
220
- // If myElement._mc is now empty, the '_mc' attribute is also removed.
240
+ // If myElement._mc is now empty, the 'data-mc' attribute is also removed.
221
241
  ```
242
+ Hint: I have yet to find a usage example for this... so far it simply exists as a counterpart to `DC.add()`.
222
243
 
223
244
  ### Retrieve Micro Components
224
245
 
225
- Access attached components via the \_mc property of the target element or parent component.
246
+ Access attached components via the `_mc` property of the target element or parent component.
226
247
 
227
248
  ```javascript
228
249
  // Get the container object for all MCs on an element
@@ -246,13 +267,13 @@ const targetElement = document.getElementById('myDiv');
246
267
  const eventData = { message: 'Hello from outside!' };
247
268
 
248
269
  // Trigger 'customEvent' locally on MCs attached to targetElement
249
- DC.trigger(targetElement, 'customEvent', eventData, 'l');
270
+ trigger(targetElement, 'customEvent', eventData, 'l');
250
271
 
251
272
  // Trigger 'customEvent' and bubble up the DOM tree
252
- DC.trigger(targetElement, 'customEvent', eventData, 'u');
273
+ trigger(targetElement, 'customEvent', eventData, 'u');
253
274
 
254
275
  // Trigger 'customEvent' locally, bubble up, and bubble down
255
- DC.trigger(targetElement, 'customEvent', eventData, 'lud');
276
+ trigger(targetElement, 'customEvent', eventData, 'lud');
256
277
  ```
257
278
 
258
279
  2. Triggering from inside a component instance:
@@ -267,10 +288,6 @@ class MyTriggeringComponent extends DC {
267
288
  // Trigger 'internalEvent' only on this instance and its nested _mc components
268
289
  this.trigger('internalEvent', eventData); // Default bubble 'l'
269
290
 
270
- // Trigger 'externalEvent' on sibling components attached to the same target element
271
- // (and potentially bubble up/down based on the 'bubble' parameter)
272
- DC.trigger(this.target, 'externalEvent', eventData, 'l'); // Trigger locally on siblings
273
-
274
291
  // Trigger 'downwardEvent' on components attached to descendant elements
275
292
  this.trigger('downwardEvent', eventData, 'd'); // Bubbles down from this component's target
276
293
  }
@@ -287,48 +304,15 @@ class MyTriggeringComponent extends DC {
287
304
  );
288
305
  }
289
306
 
290
- onExternalEvent(event) {
291
- console.log(
292
- `[${this.constructor.name}] received externalEvent:`,
293
- event.data
294
- );
295
- }
296
-
297
307
  onDownwardEvent(event) {
298
308
  console.log(
299
- `[${this.constructor.name}] received downwardEvent:`,
309
+ `[${this.constructor.name}] ERROR: received downwardEvent`,
300
310
  event.data
301
311
  );
302
312
  }
303
313
  }
304
314
  ```
305
315
 
306
- ### Retrieving Inner Micro Components
307
-
308
- Access nested components directly via the parent's `_mc` property.
309
-
310
- ```javascript
311
- class Inner extends MC {
312
- constructor(target) {
313
- super(target);
314
- }
315
- someInnerMethod() {
316
- console.log('Inner method called!');
317
- }
318
- }
319
-
320
- class Outer extends DC {
321
- constructor(target) {
322
- super(target);
323
- this._mc.inner = new Inner(this);
324
- }
325
-
326
- accessInner() {
327
- this._mc?.inner.someInnerMethod();
328
- }
329
- }
330
- ```
331
-
332
316
  ### Navigating Parent/Ancestor MCs
333
317
 
334
318
  Use .parent() and .ancestors() (available on DC instances) to find components attached to elements higher up the DOM tree.
@@ -337,7 +321,7 @@ These methods skip elements that don't have any MCs attached.
337
321
 
338
322
  ```javascript
339
323
  // Inside a method of a DC instance:
340
- const parentMcs = this.parent(); // Array of MC instances on the first parent element with MCs
324
+ const parentMcs = this.parent(); // Array of MC instances on the closest parent element that contains MCs
341
325
  console.log(parentMcs);
342
326
 
343
327
  const allAncestorMcs = this.ancestors(); // Array of all MC instances on all ancestor elements with MCs
@@ -394,9 +378,9 @@ class McWithHandlers extends DC {
394
378
  }
395
379
 
396
380
  // This is a custom event handler, not a native DOM event.
397
- // It will be triggered by this.trigger('CustomAction') or DC.trigger(this.target, 'CustomAction')
381
+ // It will be triggered by this.trigger('CustomAction')
398
382
  onCustomAction(mcEvent) {
399
- console.log('Custom action triggered!', mcEvent.data);
383
+ console.log('Custom action triggered!', mcEvent.data, mcEvent.bubble);
400
384
  }
401
385
  }
402
386
 
@@ -433,7 +417,7 @@ class MyLifecycleComponent extends DC {
433
417
  }
434
418
 
435
419
  oneInit(event) {
436
- // This code runs shortly after the constructor finishes.
420
+ // called asynchronous after the constructor finishes.
437
421
  console.log('MyLifecycleComponent oneInit called!', event);
438
422
  // Safe to access this.target or setup things that depend on the instance being fully constructed.
439
423
  }
@@ -475,7 +459,7 @@ The necessary code is fetched and executed only when these components actually a
475
459
  **./test/mymc.ts**
476
460
  ```typescript
477
461
  'use strict';
478
- import { DC, LooseObject } from '../microcomponents.js';
462
+ import { DC } from '../microcomponents.js';
479
463
 
480
464
  export default class myMC extends DC {
481
465
  constructor(target: HTMLElement) {
@@ -483,7 +467,7 @@ export default class myMC extends DC {
483
467
  }
484
468
 
485
469
  oneInit() {
486
- // console.log('testmc connected to DOM');
470
+ console.log('testmc connected to DOM');
487
471
  }
488
472
  }
489
473
  ```
@@ -502,13 +486,8 @@ defineCE(
502
486
  constructor(target: HTMLElement) {
503
487
  super(target);
504
488
 
505
- const that = this; // to help minification, you can use 'this' throughout the constructor as well
506
-
507
- delete that._mc; // no inner classes needed
508
-
509
- let shadow = (that as DC).target.attachShadow({ mode: 'open' });
510
- shadow.innerHTML = `<style>${that.css}</style>${that.template}`;
511
-
489
+ let shadow = (this as DC).target.attachShadow({ mode: 'open' });
490
+ shadow.innerHTML = `<style>${this.css}</style>${this.template}`;
512
491
  }
513
492
 
514
493
  get template() {
@@ -50,15 +50,15 @@ document.body.append( myTag );
50
50
  After:
51
51
  ```html
52
52
  <body>
53
- <my-tag _mc>Hello World</my-tag>
53
+ <my-tag data-mc>Hello World</my-tag>
54
54
  </body>
55
55
  ```
56
56
 
57
57
  **Hints**
58
58
  - Your code extends the **[DC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MicroComponents.md?ref_type=heads#dc-class-extends-mcbase)** superclass. This superclass provides methods to traverse the DOM and select other micro components contained in HTML elements.
59
59
  - it is essential to understand that your web component code is only one of the numerous micro components that can reside inside `<my-tag>`
60
- - After you created a custom element, the custom element will contain a **property `<my-tag>._mc`** which contains your class instance.
61
- - Also the custom element then has an **attribute '_mc'** which will indicate that at least one micro component is attached to it.
60
+ - After you created a custom element, the custom element will contain a **property `_mc`** which contains your class instance.
61
+ - Also the custom element then has an **attribute `data-mc`** which will indicate that at least one micro component is attached to it.
62
62
  - Note that you can hand over data natively via parameters, when handling custom elements in above way.
63
63
  - The native way of usage, simply adding the tag to the template, is still possible. You have to use attributes then to hand in data.
64
64
 
@@ -98,7 +98,7 @@ defineCE(
98
98
  ['my-attribute'] // DOM node attributes to observe
99
99
  );
100
100
 
101
- let MyTag = getCE("my-tag"), // customElemtentRegistry.get("my-tag")
101
+ let MyTag = getCE("my-tag"), // customElementRegistry.get("my-tag")
102
102
  myTag = new MyTag(); // programmatically create a custom element instance
103
103
 
104
104
  document.body.append( myTag );
@@ -147,7 +147,7 @@ document.body.append( new MyTag() );
147
147
  **Hints**
148
148
  - You can add as many other micro components to the DOM node as you like.
149
149
  - The native web component lifecycle events will be triggered on **all** micro components.
150
- - The event parameter handed over is an internal event instance, that contains the original native event in its `data` property.
150
+ - The event parameter handed over is an CustomEvent instance, that contains the original native event in its `detail` property.
151
151
 
152
152
  ## Native HTMLElement Event Handlers
153
153
 
@@ -182,7 +182,6 @@ document.body.append( new MyTag() );
182
182
 
183
183
  **Hints**
184
184
  - The native web component lifecycle events will be triggered on **all** micro components contained in the custom element, provided the MC contains a matching named handler.
185
- - The event parameter handed over is an internal event instance, that contains the original native event in its `data` property.
186
185
 
187
186
  # Lifecycle Event Handlers
188
187
 
@@ -216,7 +215,6 @@ document.body.append( new MyTag() );
216
215
 
217
216
  **Hints**
218
217
  - The native web component lifecycle events will be triggered on **all** micro components contained in the custom element, provided the specific micro component contains a matching named handler.
219
- - The event parameter handed over is an internal event instance that contains no data.
220
218
 
221
219
  # Custom Events
222
220
 
@@ -255,8 +253,7 @@ Alternatives:
255
253
 
256
254
  document
257
255
  .querySelector( 'my-tag' )
258
- ?._mc
259
- .trigger('specialEvent'); // ...on all micro components contained in <my-tag>
256
+ .dispatchEvent('specialEvent'); // ...on all micro components contained in <my-tag>
260
257
 
261
258
  /*
262
259
  traversing the DOM:
@@ -53,7 +53,7 @@ export type Observable = {
53
53
  **Methods**
54
54
 
55
55
  - `observe(cb: Callback)`
56
- Registers a callback to be notified of changes.
56
+ - Registers a callback to be notified of changes.
57
57
 
58
58
  - `notify( enable?: boolean)`
59
59
  - Enables / Disables notifications.
@@ -62,10 +62,12 @@ export type Observable = {
62
62
  - Otherwise calls all registered callbacks with the current data.
63
63
 
64
64
  - `bind(target: HTMLElement | DocumentFragment | ShadowRoot)`
65
- Binds the observable to a DOM element, replacing placeholders with data values.
65
+ - Binds the observable to a DOM element, replacing placeholders with data values.
66
66
 
67
67
 
68
- ## Example
68
+ ## Examples
69
+
70
+ ### How to use observables in native JS Code
69
71
 
70
72
  HTML:
71
73
  ```html
@@ -79,23 +81,107 @@ JavaScript:
79
81
  ```javascript
80
82
  import { observe } from './observables.js';
81
83
 
82
- const data = {
83
- name: 'John',
84
- age: 30,
84
+ // define an object
85
+ const address = {
86
+ data: {
87
+ name: 'John',
88
+ age: 30,
89
+ },
90
+
91
+ otherData: {
92
+ whatever: true
93
+ }
94
+
85
95
  };
86
96
 
87
- observe(data); // all properties of the data object will be observed.
97
+ // make all properties of the object observable
98
+ observe(address);
99
+
100
+ // OR: make one property of the object observable
101
+ observe(address, 'data');
88
102
 
103
+ // connect the observable to DOM structure placeholders
89
104
  const element = document.getElementById('user-card');
90
- data.bind(element);
105
+ address.data.bind(element);
106
+ ```
107
+
108
+ When `address.data.name` or `address.data.age` changes, or you reassign `address.data` completely, the DOM updates automatically.
109
+
110
+ ---
111
+
112
+ ### How to use observables in Micro Components
113
+
114
+ HTML:
115
+ ```html
116
+ <div id="user-card">
117
+ <p>Hello, {name}</p>
118
+ <p>Age: {age}</p>
119
+ </div>
91
120
  ```
92
121
 
93
- When `data.name` or `data.age` changes, the DOM updates automatically.
122
+ Micro Components are instances of classes. So you can store the class in the CustomElementRegistry:
123
+
124
+ JavaScript: (**define** the micro component)
125
+ `./user/card/data.js`
126
+ ```javascript
127
+ import {
128
+ DC,
129
+ defineCE,
130
+ getCE,
131
+ Observable,
132
+ observe
133
+ } from './microcomponents.js';
134
+
135
+ // define the micro component
136
+ defineCE(
137
+ 'user-card-data',
138
+
139
+ class UserCardData extends DC {
140
+ data: unknown;
141
+
142
+ constructor(target: HTMLElement, data?: { name: string = '', age: number = 0 }) {
143
+ super(target);
144
+
145
+ this.data = data;
146
+
147
+ // create this.data observable
148
+ observe(this);
149
+
150
+ // 1 way bind formdata to the form DOM
151
+ (this.data as Observable).bind(target);
152
+ }
153
+
154
+ }
155
+ );
156
+
157
+ export default getCE('user-card-data');
158
+ ```
159
+
160
+ JavaScript: (**use** the micro component)
161
+ ```javascript
162
+ import {
163
+ getCE,
164
+ } from './microcomponents.js';
165
+
166
+ const element = document.getElementById('user-card');
167
+ const Dc = getCE('user-card-data');
168
+ const dc = new Dc( element, { name: 'Sarah', age: 33 } );
169
+
170
+ // add the micro component to the DOM element
171
+ DC.add(
172
+ element,
173
+ 'userCardData',
174
+ dc
175
+ );
176
+
177
+ // change data
178
+ element._mc.userCardData.data = { name: 'John', age: 27 };
179
+ ```
94
180
 
95
181
  ---
96
182
 
97
- ## Notes
183
+ ## Hints
98
184
 
185
+ - Native values (strings, numbers, booleans) can be made observable.
99
186
  - Only objects can be bound to the DOM.
100
- - Native values (strings, numbers, booleans) are wrapped in observable proxies.
101
187
  - Changes to observable properties trigger updates in bound DOM elements.
@@ -1 +1 @@
1
- {"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AAuCA,wBAAgB,QAAQ,CACvB,OAAO,EAAE,MAAM,EACf,eAAe,EAAE;IAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;CAAE,GAAG,QAAQ,EACzD,kBAAkB,GAAE,MAAM,EAAO,QA2DjC;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB,GAAG,SAAS,CAE3E"}
1
+ {"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AAoDA,wBAAgB,QAAQ,CACvB,OAAO,EAAE,MAAM,EACf,eAAe,EAAE;IAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;CAAE,GAAG,QAAQ,EACzD,kBAAkB,GAAE,MAAM,EAAO,QA2DjC;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB,GAAG,SAAS,CAE3E"}
@@ -6,16 +6,22 @@ class CeBaseClass extends HTMLElement {
6
6
  super();
7
7
  }
8
8
  connectedCallback() {
9
- DC.trigger(this, 'Connected');
9
+ this.dispatchEvent(new CustomEvent('_mcConnected'));
10
+ }
11
+ connectedMoveCallback() {
12
+ this.dispatchEvent(new CustomEvent('_mcConnectedMove'));
10
13
  }
11
14
  disconnectedCallback() {
12
- DC.trigger(this, 'Disconnected');
15
+ this.dispatchEvent(new CustomEvent('_mcDisconnected'));
13
16
  }
14
17
  adoptedCallback() {
15
- DC.trigger(this, 'Adopted');
18
+ this.dispatchEvent(new CustomEvent('_mcAdopted'));
16
19
  }
17
20
  attributeChangedCallback(name, oldValue, newValue) {
18
- DC.trigger(this, 'AttributeChanged', { name, oldValue, newValue });
21
+ this.dispatchEvent(new CustomEvent('_mcAttributeChanged', { detail: { name, oldValue, newValue } }));
22
+ }
23
+ formResetCallback() {
24
+ this.dispatchEvent(new CustomEvent('_mcFormReset'));
19
25
  }
20
26
  }
21
27
  function createNamedClass(name, ClassBody) {
@@ -1 +1 @@
1
- {"version":3,"file":"elements.js","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAE7C,MAAM,WAAY,SAAQ,WAAW;IACpC;QACC,KAAK,EAAE,CAAC;IACT,CAAC;IAED,iBAAiB;QACf,EAAU,CAAC,OAAO,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;IACxC,CAAC;IAED,oBAAoB;QAClB,EAAU,CAAC,OAAO,CAAC,IAAI,EAAE,cAAc,CAAC,CAAC;IAC3C,CAAC;IAED,eAAe;QACb,EAAU,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;IACtC,CAAC;IAED,wBAAwB,CAAC,IAAY,EAAE,QAAa,EAAE,QAAa;QACjE,EAAU,CAAC,OAAO,CAAC,IAAI,EAAE,kBAAkB,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;IAC7E,CAAC;CACD;AAED,SAAS,gBAAgB,CACxB,IAAY,EACZ,SAAwC;IAExC,OAAO,MAAM,CAAC,cAAc,CAAC,SAAS,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAClE,CAAC;AAED,SAAS,OAAO,CAAC,IAAS;IACxB,OAAO,OAAO,IAAI,KAAK,UAAU;WAC5B,UAAU,CAAC,IAAI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EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QAC9B,CAAC;QAED,MAAM,KAAK,kBAAkB;YAC5B,OAAO,kBAAkB,CAAC;QAC3B,CAAC;KACD,CACD,CACD,CAAC;AACH,CAAC;AAED,MAAM,UAAU,KAAK,CAAC,OAAe;IACpC,OAAO,cAAc,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACpC,CAAC"}
@@ -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.elements=e():t.elements=e()}(this,(()=>(()=>{"use strict";var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},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:()=>_,getCE:()=>w});var r=new Function("try {return this===window;}catch(e){ return false;}");let n=new Set;r()&&Object.keys(HTMLElement.prototype).filter((t=>/^on/.test(t))).forEach((t=>n.add(t)));class o{#t;#e=!1;#r=!1;constructor(t,e={},r="l"){this.#t=t,this.data=e,this.bubble=r}get type(){return this.#t}get stopped(){return this.#e}get immediateStopped(){return this.#r}stopPropagation(){this.#e=!0}stopImmediatePropagation(){this.stopPropagation(),this.#r=!0}}var i=new Function("try {return this===window;}catch(e){ return false;}");let c=new Set;function s(t,e){let r=[];return t.forEach((t=>{if(t&&t instanceof HTMLElement){const n=t?._mc;!e&&n?r=r.concat(Object.values(t._mc)):e&&n&&n[e]&&r.push(n[e])}})),r}function a(t,e,r=!0){let n=[];for(;t;){if(t._mc&&(e?e&&t._mc[e]&&n.push(t._mc[e]):n=n.concat(Object.values(t._mc)),r))return n;t=t.parentNode}return n}function u(t){return t&&t instanceof HTMLElement}function l(t,e,r={}){let n=t;if(!n._mc){let e=b?.debug;b.debug=!1,n._mc=new b(t),b.debug=e,delete n._mc._mc}const o=n._mc;return e.length&&!o[e]&&(o[e]=r,t instanceof HTMLElement&&!t.getAttribute("_mc")&&t.setAttribute("_mc","")),r}function p(t,e){if(!e)return;const r=t?._mc||{};r&&r[e]&&delete r[e],!Object.keys(r).length&&t instanceof HTMLElement&&t.hasAttribute("_mc")&&t.removeAttribute("_mc")}function d(t,e,r={},n="l"){let i=t?._mc;if(!i)return;let c=e instanceof o?new o(e.type,e.data,"l"):new o(e,r,"l"),s="string"!=typeof e?e:new o(e,r,n);[...Object.values(i)].forEach((t=>{t?.trigger&&"function"==typeof t?.trigger&&t?.trigger(c)})),/[ud]/.test(s.bubble)&&f(t,s)}function f(t,e){/[ud]/.test(e.bubble)&&setTimeout((()=>{if(-1===e.bubble.indexOf("l")&&(e.bubble+="l"),e.bubble.indexOf("u")>-1){let r=a(t,"",!0)?.[0].target;r&&b.trigger(r,e)}e.bubble.indexOf("d")>-1&&new Set(t._mc.children().map((t=>t?.target))).forEach((t=>{t&&b.trigger(t,e)}))}),0)}i()&&Object.keys(HTMLElement.prototype).filter((t=>/^on/.test(t))).forEach((t=>c.add(t)));class g{_target;__mc={};constructor(t,e=!1){var r;this._target=new WeakRef(t||{}),(r=this).constructor!==b&&setTimeout((()=>{r.trigger("Init")}),0),function(t){if(!i()||t.constructor===b)return;const e=t.target;if(e instanceof HTMLElement){let r=t;Object.getOwnPropertyNames(r.constructor.prototype).filter((t=>/^on[A-Z]|one[A-Z]/.test(t)&&"function"==typeof r.constructor.prototype[t])).forEach((n=>{let o=/^one/.test(n),i=n.replace(/^on[e]{0,1}/,""),s=i.toLowerCase();o&&Object.defineProperty(t,"on"+i,{configurable:!0,enumerable:!1,writable:!0,value:t=>{r.constructor.prototype[n].bind(r)(t)}}),c.has("on"+s)&&function(t,e,r,n=!1,o=!1){let i={once:!1};n&&(i.capture=n),o&&(i.once=o),t.addEventListener(e,r,i)}(e,s,(t=>{r.trigger(i,t)}),!1,o)}))}}(this)}get target(){return this._target.deref()}get _mc(){return this.__mc}}class m extends g{constructor(t){super(t)}static trigger=d;trigger(t,e={},r="l"){let n=this;if(n.constructor===m||n.constructor===b){let i=t instanceof o?new o(t.type,t.data,"l"):new o(t,e,"l"),c="string"!=typeof t?t:new o(t,e,r);return[...Object.getOwnPropertyNames(n)].forEach((t=>{n[t]?.trigger&&"function"==typeof n[t]?.trigger&&n[t]?.trigger(i)})),void(/[ud]/.test(c.bubble)&&f(n.target,c))}if(n instanceof m||n instanceof b){const n=this,i="string"!=typeof t?t:new o(t,e,r),c="on"+i.type[0].toUpperCase()+i.type.slice(1);if(i.bubble.indexOf("l")>-1&&(i.immediateStopped||"function"!=typeof n[c]||(n[c](i),n.hasOwnProperty(c)&&delete n[c])),i.stopped||"l"===i.bubble)return n;/[ud]/.test(i.bubble)&&f(n.target,i)}}}class b extends g{constructor(t,e=!1){super(t)}static add=l;static remove=p;static trigger=d;trigger(t,e={},r){m.prototype.trigger.call(this,t,e,r)}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 r=Math.random().toString().replace(".",""),n='[_mc]:not([temp_id="'+r+'"] [_mc] [_mc])';e.setAttribute("temp_id",r);const o=s([...e.querySelectorAll(n)],t);return e.removeAttribute("temp_id"),o}descendants(t=""){let e=this?.target;return u(e)?s([...e.querySelectorAll("*[_mc]")],t):[]}}new Set;class h extends HTMLElement{constructor(){super()}connectedCallback(){b.trigger(this,"Connected")}disconnectedCallback(){b.trigger(this,"Disconnected")}adoptedCallback(){b.trigger(this,"Adopted")}attributeChangedCallback(t,e,r){b.trigger(this,"AttributeChanged",{name:t,oldValue:e,newValue:r})}}function y(t){return"function"==typeof t&&/^class\s/.test(Function.prototype.toString.call(t))}function _(t,e,r=[]){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 n=t.split("-").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join("");y(e.prototype.constructor)&&e.prototype instanceof g?customElements.define(t,function(t,e){return Object.defineProperty(e,"name",{value:t})}(n,class extends h{instance;constructor(t){super();const r=y(e)?new e(this,t):e;b.add(this,n,r)}static get observedAttributes(){return r}})):customElements.define(t,e)}function w(t){return customElements.get(t)}return 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.elements=e():t.elements=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:()=>m,getCE:()=>f});const n=new Function("try {return this===window;}catch(e){ return false;}");function r(t){return t&&t instanceof HTMLElement}function o(t,e,n={}){let r=t;r._mc||(r._mc=new l(t));const o=r._mc;return e.length&&!o[e]&&(o[e]=n,t instanceof HTMLElement&&!t.getAttribute("data-mc")&&t.setAttribute("data-mc","")),r}function c(t,e){if(!e)return;const n=t?._mc||{};n&&n[e]&&delete n[e],!Object.keys(n).length&&t instanceof HTMLElement&&t.hasAttribute("data-mc")&&t.removeAttribute("data-mc")}function s(t){return"_mc"+t[0].toUpperCase()+t.substring(1)}function a(t,e,n,r=!1){t.addEventListener(s(e),n,{capture:!1,once:r})}class i{_target;constructor(t){var e;this._target=new WeakRef(t||{}),e=this,setTimeout((()=>{e.constructor!==l&&e.trigger("Init")}),0),function(t){if(!n()||t.constructor===l)return;const e=t.target;if(e instanceof HTMLElement){let n=t;Object.getOwnPropertyNames(n.constructor.prototype).filter((t=>/^on[A-Z]|one[A-Z]/.test(t)&&"function"==typeof n.constructor.prototype[t])).forEach((t=>{let r=/^one/.test(t),o=t.replace(/^on[e]{0,1}/,""),c=o.toLowerCase(),s=e=>{n[t](e)};e["on"+c]?e.addEventListener(c,s):a(e,o,s,r)}))}}(this)}get target(){return this._target.deref()}}class l extends i{constructor(t,e=!1){super(t)}static add=o;static remove=c;off(t,e){!function(t,e,n){t.removeEventListener(s(e),n)}(this.target,t,e)}on(t,e){a(this.target,t,e)}one(t,e){!function(t,e,n){a(t,e,n,!0)}(this.target,t,e)}trigger(t,e={},n="l"){!function(t,e,n={},r="l"){const o="string"==typeof e?new CustomEvent(s(e),{detail:{data:n,bubble:r}}):e;r.length&&r.indexOf("l")>-1&&t.dispatchEvent(o),/[u|d]/.test(r)&&function(t,e){setTimeout((()=>{-1===e.detail.bubble.indexOf("l")&&(e.detail.bubble+="l"),e.detail.bubble.indexOf("u")>-1&&t.closest("[data-mc]")?.dispatchEvent(e),e.detail.bubble.indexOf("d")>-1&&new Set(t._mc.children().map((t=>t?.target))).forEach((t=>{t&&t._mc.trigger(t,e)}))}),0)}(t,o)}(this.target,t,e,n)}parent(){let t=this.target;return r(t)&&t.closest("[data-mc]")?._mc||null}ancestors(){let t=this?.target,e=[];if(!r(t))return[];for(;t=t.closest("[data-mc]");)t?._mc&&e.push(t?._mc);return e.length?e:null}children(){let t=this?.target;return r(t)?function(t){const e=Math.random().toString().replace(".",""),n='[data-mc]:not([temp_id="'+e+'"] [data-mc] [data-mc])';t.setAttribute("temp_id",e);const r=[...t.querySelectorAll(n)].filter((t=>!!t._mc)).map((t=>t._mc));return t.removeAttribute("temp_id"),r.length?r:null}(t):null}descendants(){const t=this?.target;if(!r(t))return[];const e=[...t.querySelectorAll("[data-mc]")].filter((t=>!!t._mc)).map((t=>t._mc));return e.length?e:null}}new Set;class u extends HTMLElement{constructor(){super()}connectedCallback(){this.dispatchEvent(new CustomEvent("_mcConnected"))}connectedMoveCallback(){this.dispatchEvent(new CustomEvent("_mcConnectedMove"))}disconnectedCallback(){this.dispatchEvent(new CustomEvent("_mcDisconnected"))}adoptedCallback(){this.dispatchEvent(new CustomEvent("_mcAdopted"))}attributeChangedCallback(t,e,n){this.dispatchEvent(new CustomEvent("_mcAttributeChanged",{detail:{name:t,oldValue:e,newValue:n}}))}formResetCallback(){this.dispatchEvent(new CustomEvent("_mcFormReset"))}}function d(t){return"function"==typeof t&&/^class\s/.test(Function.prototype.toString.call(t))}function m(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("");d(e.prototype.constructor)&&e.prototype instanceof i?customElements.define(t,function(t,e){return Object.defineProperty(e,"name",{value:t})}(r,class extends u{instance;constructor(t){super();const n=d(e)?new e(this,t):e;l.add(this,r,n)}static get observedAttributes(){return n}})):customElements.define(t,e)}function f(t){return customElements.get(t)}return e})()));
@@ -9,19 +9,32 @@ class CeBaseClass extends HTMLElement {
9
9
  }
10
10
 
11
11
  connectedCallback() {
12
- (DC as any).trigger(this, 'Connected');
12
+ this.dispatchEvent( new CustomEvent('_mcConnected') );
13
+ }
14
+
15
+ connectedMoveCallback() {
16
+ this.dispatchEvent( new CustomEvent('_mcConnectedMove') );
13
17
  }
14
18
 
15
19
  disconnectedCallback() {
16
- (DC as any).trigger(this, 'Disconnected');
20
+ this.dispatchEvent( new CustomEvent('_mcDisconnected') );
17
21
  }
18
22
 
19
23
  adoptedCallback() {
20
- (DC as any).trigger(this, 'Adopted');
24
+ this.dispatchEvent( new CustomEvent('_mcAdopted') );
21
25
  }
22
26
 
23
27
  attributeChangedCallback(name: string, oldValue: any, newValue: any) {
24
- (DC as any).trigger(this, 'AttributeChanged', { name, oldValue, newValue });
28
+ this.dispatchEvent(
29
+ new CustomEvent(
30
+ '_mcAttributeChanged',
31
+ { detail: { name, oldValue, newValue } }
32
+ )
33
+ );
34
+ }
35
+
36
+ formResetCallback(){
37
+ this.dispatchEvent( new CustomEvent('_mcFormReset') );
25
38
  }
26
39
  }
27
40
 
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
  import { copyGettersSetters } from './helpers.js';
3
- import { DC, addListener } from './MC.js';
3
+ import { DC } from './MC.js';
4
4
  import { observe } from './observables.js';
5
5
  const getInputName = (e) => {
6
6
  return e.tagName.toLowerCase();
@@ -154,7 +154,7 @@ export function formValues(targetObject, propertyName, dom) {
154
154
  observe(targetObject, propertyName);
155
155
  copyGettersSetters(handlers, targetObject[propertyName]);
156
156
  [...dom.querySelectorAll('input, textarea, select')].forEach((input) => {
157
- addListener(input, 'input', (ev) => {
157
+ input.addEventListener('input', (ev) => {
158
158
  targetObject[propertyName].notify();
159
159
  });
160
160
  });