@twobirds/microcomponents 0.0.64 → 0.0.66

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 (119) hide show
  1. package/.vscode/settings.json +1 -1
  2. package/assets/documentation/MC.md +146 -28
  3. package/assets/documentation/elements.md +4 -2
  4. package/assets/documentation/observables.md +2 -0
  5. package/assets/test.html +1 -1
  6. package/dist/MC.d.ts.map +1 -1
  7. package/dist/MC.min.js +20 -15
  8. package/dist/MC.ts +23 -16
  9. package/dist/elements.min.js +1 -1
  10. package/dist/formvValues.min.js +1 -1
  11. package/dist/microcomponents.min.js +1 -1
  12. package/dist/observables.d.ts.map +1 -1
  13. package/dist/observables.min.js +1 -1
  14. package/dist/observables.ts +0 -3
  15. package/dist/selectors.min.js +1 -1
  16. package/package.json +4 -4
  17. package/public/MC.js +20 -15
  18. package/public/MC.js.map +1 -1
  19. package/public/MC.min.js +20 -15
  20. package/public/assets/highlight.css +99 -0
  21. package/public/assets/icons.js +18 -0
  22. package/public/assets/icons.svg +1 -0
  23. package/public/assets/main.js +60 -0
  24. package/public/assets/navigation.js +1 -0
  25. package/public/assets/search.js +1 -0
  26. package/public/assets/style.css +1493 -0
  27. package/public/classes/MC.DC.html +12 -0
  28. package/public/classes/MC.MC.html +6 -0
  29. package/public/classes/MC.McBase.html +3 -0
  30. package/public/classes/MC.McEvent.html +9 -0
  31. package/public/elements.min.js +1 -1
  32. package/public/formvValues.min.js +1 -1
  33. package/public/functions/MC.addListener.html +1 -0
  34. package/public/functions/MC.autoload.html +1 -0
  35. package/public/functions/MC.removeListener.html +1 -0
  36. package/public/functions/elements.defineCE.html +1 -0
  37. package/public/functions/elements.getCE.html +1 -0
  38. package/public/functions/formValues.formValues.html +1 -0
  39. package/public/functions/observables.observe.html +1 -0
  40. package/public/functions/selectors._d.html +1 -0
  41. package/public/functions/selectors._t.html +1 -0
  42. package/public/hierarchy.html +1 -0
  43. package/public/index.html +56 -0
  44. package/public/interfaces/MC.HTMLMcElement.html +758 -0
  45. package/public/interfaces/selectors.DSelector.html +50 -0
  46. package/public/interfaces/selectors.TSelector.html +34 -0
  47. package/public/microcomponents.min.js +1 -1
  48. package/public/modules/MC.html +9 -0
  49. package/public/modules/elements.html +3 -0
  50. package/public/modules/formValues.html +2 -0
  51. package/public/modules/observables.html +3 -0
  52. package/public/modules/selectors.html +7 -0
  53. package/public/modules.html +6 -0
  54. package/public/observables.js.map +1 -1
  55. package/public/observables.min.js +1 -1
  56. package/public/selectors.min.js +1 -1
  57. package/public/types/observables.Observable.html +1 -0
  58. package/public/types/selectors.selection.html +1 -0
  59. package/public/types/selectors.singleSelector.html +1 -0
  60. package/src/MC.ts +23 -16
  61. package/src/observables.ts +0 -3
  62. package/src/test/testmc.ts +7 -1
  63. package/webpack.config.js +2 -2
  64. package/assets/documentation/comparision.md +0 -0
  65. package/assets/documentation/how_tos.md +0 -0
  66. package/assets/documentation/introduction.md +0 -0
  67. package/assets/documentation/proposal_draft.md +0 -37
  68. package/public/3d.html +0 -42
  69. package/public/documentation/MC.md +0 -470
  70. package/public/documentation/ajaxian-1.png +0 -0
  71. package/public/documentation/ajaxian-2.png +0 -0
  72. package/public/documentation/ajaxian-3.png +0 -0
  73. package/public/documentation/comparision.md +0 -0
  74. package/public/documentation/elements.md +0 -276
  75. package/public/documentation/functional_requirements.md +0 -8
  76. package/public/documentation/history.md +0 -436
  77. package/public/documentation/how_tos.md +0 -0
  78. package/public/documentation/introduction.md +0 -0
  79. package/public/documentation/magic.md +0 -29
  80. package/public/documentation/observables.md +0 -105
  81. package/public/documentation/proposal_draft.md +0 -37
  82. package/public/documentation/rnd0023-1.jpg +0 -0
  83. package/public/documentation/rnd0023-2.jpg +0 -0
  84. package/public/documentation/rnd0023-3.jpg +0 -0
  85. package/public/documentation/tB_react.md +0 -207
  86. package/public/documentation/usage.md +0 -142
  87. package/public/documentation/why.md +0 -25
  88. package/public/fiddle/MicroComponents.svg +0 -15
  89. package/public/fiddle/fiddle.css +0 -0
  90. package/public/fiddle/fiddle.js +0 -0
  91. package/public/fiddle/index.html +0 -0
  92. package/public/grandparent.css +0 -13
  93. package/public/my-tag.css +0 -19
  94. package/public/src/MC.ts +0 -598
  95. package/public/src/McEvent.ts +0 -96
  96. package/public/src/autoloader.ts +0 -98
  97. package/public/src/elements.ts +0 -76
  98. package/public/src/formValues.ts +0 -233
  99. package/public/src/helpers.ts +0 -149
  100. package/public/src/microcomponents.ts +0 -8
  101. package/public/src/observables.ts +0 -404
  102. package/public/src/selectors.ts +0 -1634
  103. package/public/src/test.ts +0 -193
  104. package/public/test/child.js +0 -15
  105. package/public/test/child.js.map +0 -1
  106. package/public/test/grandchild.js +0 -15
  107. package/public/test/grandchild.js.map +0 -1
  108. package/public/test/grandparent.js +0 -15
  109. package/public/test/grandparent.js.map +0 -1
  110. package/public/test/greatgrandchild.js +0 -29
  111. package/public/test/greatgrandchild.js.map +0 -1
  112. package/public/test/parent.js +0 -15
  113. package/public/test/parent.js.map +0 -1
  114. package/public/test/testform.js +0 -141
  115. package/public/test/testform.js.map +0 -1
  116. package/public/test/testmc.js +0 -10
  117. package/public/test/testmc.js.map +0 -1
  118. package/public/test.html +0 -27
  119. /package/public/{MicroComponents.svg → media/MicroComponents.svg} +0 -0
@@ -1,37 +0,0 @@
1
- ### RFC Proposal to WHATWG
2
-
3
- **Title:** Proposal for a **Micro Component Interface** in the DOM
4
-
5
- **Abstract:**
6
-
7
- This proposal introduces a standardized interface for managing micro components within HTML elements, enhancing modularity and reusability in web development.
8
-
9
- **Introduction:**
10
-
11
- Microcomponents provide a way to encapsulate functionality and state within a target DOM element. This proposal outlines methods for adding, removing, and querying micro components.
12
-
13
- **Proposed API:**
14
-
15
- - **Static Methods:**
16
- - `MC.add( target, key, element)`: Attach a top level micro component to an HTMLElement.
17
- - `MC.remove( target, key )`: Detach a top level micro component from an HTMLElement.
18
- - `MC.siblings( target )`: get top level micro components from an HTMLElement.
19
-
20
- - **Instance Methods:**
21
- - `add( key, element )`: Attach a private Micro Component to a Micro Component.
22
- - `remove( key )`: Detach a private Micro Component from a Micro Component.
23
- - `siblings(search?)`: Retrieve sibling micro components.
24
- - `parent(search?)`: Get parent top level micro components.
25
- - `parents(search?)`: Get all ancestor top level micro components.
26
- - `children(search?)`: Retrieve child top level micro components.
27
- - `descendants(search?)`: Get all descendant top level micro components.
28
-
29
- **Use Cases:**
30
- 1. Modular web applications.
31
- 2. Enhancing component-based architecture.
32
-
33
- **Conclusion:**
34
- Implementing this interface will streamline microcomponent management, fostering cleaner code and improved performance.
35
-
36
- **References:**
37
- - [Microcomponents Repository](https://gitlab.com/twoBirds/microcomponents) for a polyfill implementation and usage examples.
package/public/3d.html DELETED
@@ -1,42 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
- <title>3D Positioned Image with CSS</title>
7
- <style>
8
- body {
9
- display: flex;
10
- justify-content: center;
11
- align-items: center;
12
- height: 100vh;
13
- background-color: #222;
14
- perspective: 800px; /* Depth effect */
15
- }
16
-
17
- .container {
18
- width: 300px;
19
- height: 200px;
20
- transform-style: preserve-3d; /* Needed for 3D transformations */
21
- }
22
-
23
- .image3d {
24
- width: 100%;
25
- height: 100%;
26
- transform: rotateY(20deg) rotateX(10deg) translateZ(50px);
27
- transition: transform 0.5s ease-in-out;
28
- box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.5);
29
- }
30
-
31
- /* Hover effect */
32
- .container:hover .image3d {
33
- transform: rotateY(-20deg) rotateX(-10deg) translateZ(100px);
34
- }
35
- </style>
36
- </head>
37
- <body>
38
- <div class="container">
39
- <img class="image3d" src="https://via.placeholder.com/300x200" alt="3D Image">
40
- </div>
41
- </body>
42
- </html>
@@ -1,470 +0,0 @@
1
- # Microcomponent Framework Documentation <span style="float:right; height:1em;"><img height="24px" src="./MicroComponents.svg" /></span>
2
-
3
- This document details a lightweight JavaScript framework for creating and managing "microcomponents" (MCs) associated with DOM elements or other microcomponents. It allows you to define reusable components and attach them to build complex functionalities.
4
-
5
- The framework exports 2 main classes:
6
-
7
- - **MC**: Micro Component. The base component class, suitable for logic that doesn't directly interact with the DOM or for use on the server.
8
- - **DC**: DOM Component. Extends `MC` and adds DOM-specific methods (like traversal). Use this for components that need to interact with the browser's DOM.
9
-
10
- ## Table of Contents
11
-
12
- - [Usage Guidance](#usage-guidance)
13
- - [Core Concepts](#core-concepts)
14
- - [Microcomponents (MCs)](#microcomponents-mcs)
15
- - [`_mc` Property](#_mc-property)
16
- - [DOM Association & `target`](#dom-association--target)
17
- - [Event System](#event-system)
18
- - [Observables (Mention)](#observables-mention)
19
- - [DOM Traversal](#dom-traversal)
20
- - [Autoloading Concept](#autoloading-concept)
21
- - [Important Rule](#important-rule)
22
- - [Framework Classes](#framework-classes)
23
- - [Not Exported: `McBase` (Superclass)](#not-exported-mcbase-superclass)
24
- - [Exported: `MC` Class](#exported-mc-class)
25
- - [Exported: `DC` Class](#exported-dc-class)
26
- - [Examples](#examples)
27
- - [Typical Workflow](#typical-workflow)
28
- - [Attach Micro Components (`DC.add`)](#attach-micro-components-dcadd)
29
- - [Remove Micro Components (`DC.remove`)](#remove-micro-components-dcremove)
30
- - [Retrieve Micro Components](#retrieve-micro-components)
31
- - [Triggering Events](#triggering-events)
32
- - [Retrieving Inner Micro Components](#retrieving-inner-micro-components)
33
- - [Navigating Parent/Ancestor MCs](#navigating-parentancestor-mcs)
34
- - [Retrieving Child/Descendant MCs](#retrieving-childdescendant-mcs)
35
- - [Auto-attaching DOM Listeners](#auto-attaching-dom-listeners)
36
- - [Lifecycle Events](#lifecycle-events)
37
- - [Init](#init)
38
- - [Destroy (TBD)](#destroy-tbd)
39
- - [Helper Functions (Internal)](#helper-functions-internal)
40
- - [`isBrowser()`](#isbrowser)
41
- - [`nativeEventNames`](#nativeeventnames)
42
- - [`getMicroComponents(...)`](#getmicrocomponents)
43
- - [`traverseNodes(...)`](#traversenodes)
44
- - [`isDOM(...)`](#isdom)
45
- - [`addListener(...)`](#addlistener)
46
- - [`removeListener(...)`](#removelistener)
47
- - [`add(...)`](#add)
48
- - [`remove(...)`](#remove)
49
- - [`trigger(...)`](#trigger)
50
- - [`init(...)`](#init-helper)
51
- - [`autoAttachListeners(...)`](#autoattachlisteners)
52
- - [`bubbleEvent(...)`](#bubbleevent)
53
- - [Autoloading Implementation](#autoloading-implementation)
54
- - [`autoload(enable?)`](#autoload)
55
- - [`loadit()`](#loadit)
56
- - [Interfaces and Types](#interfaces-and-types)
57
- - [`LooseObject`](#looseobject)
58
- - [`McEvent`](#mcevent)
59
- - [`HTMLMcElement`](#htmlmcelement)
60
-
61
- ## Usage Guidance
62
-
63
- - **If your component needs to access or manipulate the DOM** (e.g., find other components in the DOM, attach listeners directly, read element properties), **your class MUST extend `DC`**. This is client-side only.
64
- - **Otherwise** (e.g., for pure logic, state management, or server-side usage), **your class should extend `MC`**.
65
-
66
- Think **DRY** (Don't Repeat Yourself) and **SOLID** principles when designing your components.
67
-
68
- ## Core Concepts
69
-
70
- Fundamental ideas behind the framework.
71
-
72
- ### Microcomponents (MCs)
73
-
74
- Plain JavaScript objects or class instances (extending `MC` or `DC`) that encapsulate behavior and state. They can be attached to DOM elements (via `DC`) or nested within other MCs.
75
-
76
- ### `_mc` Property
77
-
78
- A special property attached to `HTMLElement` instances (when using `DC.add`) or parent `MC`/`DC` instances. It acts as an object container (`{ key: mcInstance, ... }`) holding one or more named microcomponent instances.
79
-
80
- - On HTMLElements, the presence of attached components is also marked by a `_mc` attribute for CSS selection and discovery. This attribute is added by `DC.add` and removed by `DC.remove` when the `_mc` object becomes empty.
81
-
82
- ### DOM Association & `target`
83
-
84
- Each microcomponent instance has a read-only `target` property providing a back-reference:
85
-
86
- - If the instance extends **`DC`** and was added via `DC.add`, `target` refers to the **DOM HTMLElement** it's attached to.
87
- - If the instance extends **`MC`** (or `DC`) and is nested within another component's `_mc` property, `target` refers to the **parent MC/DC instance**.
88
-
89
- This reference is managed using `WeakRef` internally by `McBase` to prevent circular references and potential memory leaks.
90
-
91
- ### Event System
92
-
93
- A custom event system using the `McEvent` class allows components to communicate.
94
-
95
- - **Triggering:** Events are dispatched using the `trigger` method:
96
- - `DC.trigger(targetElement, ...)`: Dispatches to all MCs on a specific DOM element. Handles bubbling (`u`/`d`).
97
- - `this.trigger(...)`: Dispatches within the current MC instance and its potential nested MCs. Can also initiate bubbling if the instance is a `DC`.
98
- - **Handling:**
99
- - Components listen for events by defining methods named `onEventName` or `oneEventName` (for once-off handling).
100
- - These are automatically detected by `autoAttachListeners`.
101
- - Native DOM events matching this pattern on `DC` instances are also automatically bound to the target element.
102
- - **Bubbling:** Events triggered via `DC.trigger` or `dcInstance.trigger` can bubble:
103
- - `'l'`: Local only (default). Handlers on the target element's/instance's MCs are executed.
104
- - `'u'`: Upwards. Event propagates to MCs on parent elements (DOM tree).
105
- - `'d'`: Downwards. Event propagates to MCs on descendant elements (DOM tree).
106
- - Combinations are possible (e.g., `'lu'`, `'ldu'`). Bubbling (`u`/`d`) occurs asynchronously via `setTimeout`.
107
-
108
- ### Observables (Mention)
109
-
110
- - MCs can utilize observables (implementation not detailed here) for data-driven workflows.
111
- - `DC` components can leverage observables and potentially form values for 1-way or 2-way data binding with the DOM.
112
-
113
- ### DOM Traversal
114
-
115
- The `DC` class provides methods (`parent`, `ancestors`, `children`, `descendants`) to find related `DC` instances attached to elements elsewhere in the DOM hierarchy, skipping over elements without attached microcomponents.
116
-
117
- ### Autoloading Concept
118
-
119
- An optional feature (`autoload()`) that automatically detects specific HTML tags (e.g., `<my-component>`) or elements with an `_mc="MyComponent"` attribute and attempts to dynamically load the corresponding JavaScript module (e.g., `/my/Component.js`).
120
-
121
- ### Important Rule
122
-
123
- **Your own application code MUST NOT overwrite the core properties and methods provided by the `McBase`, `MC`, and `DC` classes (like `_mc`, `target`, `trigger`, `parent`, etc.). Doing so may break framework functionality.**
124
-
125
- ## Framework Classes
126
-
127
- ### Not Exported: `McBase` (Superclass)
128
-
129
- The internal base class for `MC` and `DC`. You don't extend this directly.
130
-
131
- - **Instance Properties**:
132
- - `.target`: Read-only getter returning the containing DOM element (for DCs added via `DC.add`) or parent MC/DC instance (for nested MCs). Uses `WeakRef`.
133
- - **Constructor Behavior**:
134
- - Initializes the `.target` reference.
135
- - Calls `init(this)`: Triggers the 'Init' lifecycle event asynchronously.
136
- - Calls `autoAttachListeners(this)`: Sets up automatic event listeners based on method names (`onEventName`, `oneEventName`).
137
- - **Instance Methods**:
138
- - `.trigger(eventName, data)`: Base trigger logic used by `MC` and `DC`.
139
-
140
- ### Exported: `MC` Class
141
-
142
- Base class for non-DOM or server-side components. Extends `McBase`.
143
-
144
- - **Instance Properties**:
145
- - `.target`: (Inherited from `McBase`) Refers to the parent MC/DC instance it's nested within.
146
- - `._mc`: An object `{}` to hold nested microcomponent instances.
147
- - **Instance Methods**:
148
- - `.trigger(eventName, data)`: (Inherited) Triggers events locally within the instance and its nested `_mc` structure. Does not handle DOM bubbling.
149
-
150
- ### Exported: `DC` Class
151
-
152
- Class for DOM-aware components. Extends `McBase`. **Client-side only.**
153
-
154
- - **Static Methods**:
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.
156
- - `DC.remove(domElement, key)`: Removes the MC instance associated with `key` from `domElement._mc`. Removes the `_mc` attribute if the collection becomes empty.
157
- - `DC.trigger(domElement, eventName, data, bubble)`: Triggers an event on all MCs attached to `domElement`. Handles DOM bubbling (`'l'`, `'u'`, `'d'`).
158
- - **Instance Properties**:
159
- - `.target`: (Inherited from `McBase`) Refers to the DOM HTMLElement this `DC` instance is attached to (via `DC.add`).
160
- - `._mc`: An object `{}` to hold nested microcomponent instances.
161
- - **Instance Methods**:
162
- - `.parent(search?)`: Gets MC instances from the closest parent element that has attached MCs. Optionally filters by `search` key.
163
- - `.ancestors(search?)`: Gets MC instances from all ancestor elements with attached MCs. Optionally filters by `search` key.
164
- - `.children(search?)`: Gets MC instances from direct child elements with attached MCs. Optionally filters by `search` key.
165
- - `.descendants(search?)`: Gets MC instances from all descendant elements with attached MCs. Optionally filters by `search` key.
166
- - `.trigger(eventName, data, bubble)`: Triggers an event on this `DC` instance and its nested `_mc` structure. Can initiate DOM bubbling (`'l'`, `'u'`, `'d'`).
167
-
168
- ## Examples
169
-
170
- ### Typical Workflow
171
-
172
- 1. Define your component classes extending `MC` or `DC`.
173
- 2. Attach top-level `DC` instances to HTMLElements using `DC.add()`.
174
- 3. Nest `MC` or `DC` instances within others using the `._mc` property if needed.
175
- 4. Access other components via the `_mc` property, DOM traversal methods (`ancestors()`, `children()`, etc.), or event communication (`trigger`).
176
-
177
- ### Attach Micro Components (`DC.add`)
178
-
179
- Use the static `DC.add()` method to attach a component (usually a `DC` instance) to an HTMLElement.
180
-
181
- ```javascript
182
- const myElement = document.getElementById('myDiv');
183
-
184
- // Define an inner MC (doesn't need DOM access itself)
185
- class MyInnerClass extends MC {
186
- constructor(target) {
187
- // target will be the parent MyPublicClass instance
188
- super(target);
189
- console.log('Inner MC target:', this.target);
190
- }
191
- doSomethingInner() {
192
- console.log('Inner action!');
193
- }
194
- }
195
-
196
- // Define an outer DC (needs DOM access or is top-level)
197
- class MyPublicClass extends DC {
198
- constructor(target) {
199
- // target will be myElement
200
- super(target);
201
- console.log('Outer DC target:', this.target);
202
- // Add an inner MC instance, nested within this DC instance
203
- this._mc.myInner = new MyInnerClass(this); // 'this' becomes the target for MyInnerClass
204
- }
205
- doSomethingOuter() {
206
- console.log('Outer action!');
207
- }
208
-
209
- // Example DOM event handler
210
- onClick(event) {
211
- console.log('Div clicked!', event);
212
- // Access nested MC
213
- this._mc.myInner?.doSomethingInner();
214
- }
215
- }
216
-
217
- // Attach the outer DC instance to the DOM element
218
- // The key 'myComponent' is used to access it later via myElement._mc.myComponent
219
- DC.add(myElement, 'myComponent', new MyPublicClass(myElement));
220
-
221
- // Hint: After adding, myElement now has:
222
- // 1. A property: myElement._mc = { myComponent: instanceOfMyPublicClass }
223
- // 2. An attribute: <div id="myDiv" _mc>...</div>
224
- ```
225
-
226
- ### Remove Micro Components (DC.remove)
227
-
228
- Use the static DC.remove() method to detach a top-level component.
229
-
230
- ```javascript
231
- const myElement = document.getElementById('myDiv');
232
-
233
- DC.remove(myElement, 'myComponent');
234
-
235
- // If myElement._mc is now empty, the '_mc' attribute is also removed.
236
- ```
237
-
238
- ### Retrieve Micro Components
239
-
240
- Access attached components via the \_mc property of the target element or parent component.
241
-
242
- ```javascript
243
- // Get the container object for all MCs on an element
244
- const myElement = document.getElementById('myDiv');
245
- const mcContainer = myElement._mc;
246
- console.log(mcContainer); // { myComponent: instanceOfMyPublicClass, ... }
247
-
248
- // Get a specific instance by its key
249
- const specificInstance = myElement._mc?.myComponent;
250
- if (specificInstance) {
251
- specificInstance.doSomethingOuter();
252
- }
253
- ```
254
-
255
- ### Triggering Events
256
-
257
- 1. Triggering from outside onto components on an element:
258
-
259
- ```javascript
260
- const targetElement = document.getElementById('myDiv');
261
- const eventData = { message: 'Hello from outside!' };
262
-
263
- // Trigger 'customEvent' locally on MCs attached to targetElement
264
- DC.trigger(targetElement, 'customEvent', eventData, 'l');
265
-
266
- // Trigger 'customEvent' and bubble up the DOM tree
267
- DC.trigger(targetElement, 'customEvent', eventData, 'u');
268
-
269
- // Trigger 'customEvent' locally, bubble up, and bubble down
270
- DC.trigger(targetElement, 'customEvent', eventData, 'lud');
271
- ```
272
-
273
- 2. Triggering from inside a component instance:
274
-
275
- ```javascript
276
- class MyTriggeringComponent extends DC {
277
- // ... constructor ...
278
-
279
- someMethod() {
280
- const eventData = { source: 'MyTriggeringComponent' };
281
-
282
- // Trigger 'internalEvent' only on this instance and its nested _mc components
283
- this.trigger('internalEvent', eventData); // Default bubble 'l'
284
-
285
- // Trigger 'externalEvent' on sibling components attached to the same target element
286
- // (and potentially bubble up/down based on the 'bubble' parameter)
287
- DC.trigger(this.target, 'externalEvent', eventData, 'l'); // Trigger locally on siblings
288
-
289
- // Trigger 'downwardEvent' on components attached to descendant elements
290
- this.trigger('downwardEvent', eventData, 'd'); // Bubbles down from this component's target
291
- }
292
-
293
- // Event handler methods
294
- onCustomEvent(event) {
295
- console.log(`[${this.constructor.name}] received customEvent:`, event.data);
296
- }
297
-
298
- onInternalEvent(event) {
299
- console.log(
300
- `[${this.constructor.name}] received internalEvent:`,
301
- event.data
302
- );
303
- }
304
-
305
- onExternalEvent(event) {
306
- console.log(
307
- `[${this.constructor.name}] received externalEvent:`,
308
- event.data
309
- );
310
- }
311
-
312
- onDownwardEvent(event) {
313
- console.log(
314
- `[${this.constructor.name}] received downwardEvent:`,
315
- event.data
316
- );
317
- }
318
- }
319
- ```
320
-
321
- ### Retrieving Inner Micro Components
322
-
323
- Access nested components directly via the parent's `_mc` property.
324
-
325
- ```javascript
326
- class Inner extends MC {
327
- constructor(target) {
328
- super(target);
329
- }
330
- someInnerMethod() {
331
- console.log('Inner method called!');
332
- }
333
- }
334
-
335
- class Outer extends DC {
336
- constructor(target) {
337
- super(target);
338
- this._mc.inner = new Inner(this);
339
- }
340
-
341
- accessInner() {
342
- this._mc?.inner.someInnerMethod();
343
- }
344
- }
345
- ```
346
-
347
- ### Navigating Parent/Ancestor MCs
348
-
349
- Use .parent() and .ancestors() (available on DC instances) to find components attached to elements higher up the DOM tree.
350
-
351
- These methods skip elements that don't have any MCs attached.
352
-
353
- ```javascript
354
- // Inside a method of a DC instance:
355
- const parentMcs = this.parent(); // Array of MC instances on the first parent element with MCs
356
- console.log(parentMcs);
357
-
358
- const allAncestorMcs = this.ancestors(); // Array of all MC instances on all ancestor elements with MCs
359
- console.log(allAncestorMcs);
360
-
361
- // Find a specific component type/key among ancestors
362
- const specificAncestor = this.ancestors('someSpecificKey')?.[0];
363
-
364
- if (specificAncestor) {
365
- console.log('Found specific ancestor:', specificAncestor);
366
- }
367
- ```
368
-
369
- ### Retrieving Child/Descendant MCs
370
-
371
- Use .children() and .descendants() (available on DC instances) to find components attached to elements lower down the DOM tree.
372
-
373
- ```javascript
374
- // Inside a method of a DC instance:
375
- const childMcs = this.children(); // Array of MCs on direct child elements with MCs
376
- console.log(childMcs);
377
-
378
- const descendantMcs = this.descendants(); // Array of all MCs on all descendant elements with MCs
379
- console.log(descendantMcs);
380
-
381
- // Find specific components among descendants
382
- const specificDescendants = this.descendants('someSpecificKey');
383
- console.log(specificDescendants); // Array of matching descendant MCs
384
- ```
385
-
386
- ### Auto-attaching DOM Listeners
387
-
388
- Methods defined in your DC class starting with `on` or `one` followed by a capital letter, which also match native DOM event names (case-insensitive check), will automatically have listeners attached to the target DOM element:
389
-
390
- ```javascript
391
- class McWithHandlers extends DC {
392
- constructor(target) {
393
- super(target);
394
- }
395
-
396
- // This method name matches the native 'click' event.
397
- // A listener will be automatically added to this.target.
398
- onClick(domEvent) {
399
- console.log('Target element clicked!', domEvent);
400
- // 'this' refers to the MyClickHandler instance
401
- this.target.style.backgroundColor = 'lightblue';
402
- }
403
-
404
- // This method name matches the native 'mouseover' event.
405
- // 'one' means the listener will be removed after the first trigger.
406
- oneMouseover(domEvent) {
407
- console.log('Mouseover detected once!', domEvent);
408
- this.target.style.border = '2px solid red';
409
- }
410
-
411
- // This is a custom event handler, not a native DOM event.
412
- // It will be triggered by this.trigger('CustomAction') or DC.trigger(this.target, 'CustomAction')
413
- onCustomAction(mcEvent) {
414
- console.log('Custom action triggered!', mcEvent.data);
415
- }
416
- }
417
-
418
- const myDiv = document.getElementById('clickableDiv');
419
- DC.add(myDiv, 'clickHandler', new McWithHandlers(myDiv));
420
- // Now clicking or mousing over 'clickableDiv' will trigger the methods.
421
- ```
422
-
423
- ### Lifecycle Events
424
-
425
- The framework defines specific methods that are called at certain points in a component's life.
426
-
427
- #### Init
428
-
429
- Method Name: oneInit(event)
430
-
431
- Triggered: Asynchronously (setTimeout(0)) after the component instance is created (called from McBase constructor via the init helper).
432
-
433
- Execution Order:
434
-
435
- The component's oneInit is called first, then it recursively calls oneInit on any components nested within its `_mc` property.
436
-
437
- Requirement:
438
-
439
- - You must define a method named `oneInit` in your class for it to be called.
440
- - The `one` prefix implies it's intended to run once during initialization.
441
-
442
- ```javascript
443
- class MyLifecycleComponent extends DC {
444
- constructor(target) {
445
- super(target);
446
- console.log('Constructor finished');
447
- this._mc.nested = new NestedComponent(this);
448
- }
449
-
450
- oneInit(event) {
451
- // This code runs shortly after the constructor finishes.
452
- console.log('MyLifecycleComponent oneInit called!', event);
453
- // Safe to access this.target or setup things that depend on the instance being fully constructed.
454
- }
455
- }
456
- ```
457
-
458
- #### Destroy (TBD)
459
-
460
- Method Name: oneDestroy(event) (Proposed)
461
-
462
- Triggered: When a component is removed (e.g., via DC.remove or potentially parent removal).
463
-
464
- Execution Order:
465
-
466
- Expected to run synchronously, starting from the deepest nested components upwards, finally calling oneDestroy on the component being removed itself.
467
-
468
- Requirement:
469
-
470
- - Would require defining a method named `oneDestroy` in your class.
Binary file
Binary file
Binary file
File without changes