@twobirds/microcomponents 0.0.48 → 0.0.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/Grandparent.css +13 -0
- package/assets/documentation/MC.md +380 -166
- package/assets/test.html +5 -1
- package/dist/MC.d.ts +1 -0
- package/dist/MC.d.ts.map +1 -1
- package/dist/MC.min.js +44 -0
- package/dist/MC.ts +65 -0
- package/dist/elements.min.js +1 -1
- package/dist/elements.ts +10 -10
- package/dist/formValues.d.ts +3 -2
- package/dist/formValues.d.ts.map +1 -1
- package/dist/formValues.ts +205 -203
- package/dist/formvValues.min.js +1 -1
- package/dist/helpers.d.ts +1 -0
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.ts +9 -0
- package/dist/microcomponents.min.js +1 -1
- package/dist/observables.d.ts +1 -1
- package/dist/observables.d.ts.map +1 -1
- package/dist/observables.min.js +1 -1
- package/dist/observables.ts +64 -31
- package/dist/selectors.min.js +1 -1
- package/dist/test.ts +8 -5
- package/package.json +1 -1
- package/public/assets/search.js +1 -1
- package/public/classes/MC.DC.html +13 -13
- package/public/classes/MC.MC.html +7 -7
- package/public/classes/MC.McBase.html +4 -4
- package/public/classes/MC.McEvent.html +13 -13
- package/public/classes/formValues.FormValues.html +86 -0
- package/public/functions/MC.addListener.html +2 -1
- package/public/functions/{formValues.formValues.html → MC.autoload.html} +18 -17
- package/public/functions/MC.removeListener.html +2 -1
- package/public/functions/elements.defineCE.html +1 -1
- package/public/functions/elements.getCE.html +1 -1
- package/public/functions/observables.observe.html +3 -3
- package/public/functions/selectors._d.html +1 -1
- package/public/functions/selectors._t.html +1 -1
- package/public/interfaces/MC.HTMLMcElement.html +2 -2
- package/public/interfaces/selectors.DSelector.html +55 -55
- package/public/interfaces/selectors.TSelector.html +37 -37
- package/public/modules/MC.html +3 -1
- package/public/modules/elements.html +1 -1
- package/public/modules/formValues.html +4 -4
- package/public/modules/observables.html +1 -1
- package/public/modules/selectors.html +1 -1
- package/public/types/observables.Observable.html +1 -1
- package/public/types/selectors.selection.html +1 -1
- package/public/types/selectors.singleSelector.html +1 -1
- package/src/MC.ts +65 -0
- package/src/elements.ts +10 -10
- package/src/formValues.ts +205 -203
- package/src/helpers.ts +9 -0
- package/src/observables.ts +64 -31
- package/src/test/Child.ts +21 -0
- package/src/test/Grandchild.ts +21 -0
- package/src/test/Grandparent.css +11 -0
- package/src/test/Grandparent.ts +21 -0
- package/src/test/Greatgrandchild.ts +42 -0
- package/src/test/Parent.ts +21 -0
- package/src/test/Testform.css +47 -0
- package/src/test/Testform.html +56 -0
- package/src/test/Testform.json +27 -0
- package/src/test/Testform.ts +159 -0
- package/src/test.ts +8 -5
- package/webpack.config.js +135 -61
- package/public/3d.html +0 -42
- package/public/MC.js +0 -271
- package/public/MC.js.map +0 -1
- package/public/MC.min.js +0 -407
- package/public/McEvent.js +0 -71
- package/public/McEvent.js.map +0 -1
- package/public/MicroComponents.svg +0 -15
- package/public/autoloader.js +0 -53
- package/public/autoloader.js.map +0 -1
- package/public/documentation/MC.md +0 -256
- package/public/documentation/ajaxian-1.png +0 -0
- package/public/documentation/ajaxian-2.png +0 -0
- package/public/documentation/ajaxian-3.png +0 -0
- package/public/documentation/comparision.md +0 -0
- package/public/documentation/elements.md +0 -276
- package/public/documentation/functional_requirements.md +0 -8
- package/public/documentation/history.md +0 -436
- package/public/documentation/how_tos.md +0 -0
- package/public/documentation/introduction.md +0 -0
- package/public/documentation/magic.md +0 -29
- package/public/documentation/proposal_draft.md +0 -37
- package/public/documentation/rnd0023-1.jpg +0 -0
- package/public/documentation/rnd0023-2.jpg +0 -0
- package/public/documentation/rnd0023-3.jpg +0 -0
- package/public/documentation/tB_react.md +0 -207
- package/public/documentation/usage.md +0 -142
- package/public/documentation/why.md +0 -25
- package/public/elements.js +0 -48
- package/public/elements.js.map +0 -1
- package/public/elements.min.js +0 -1
- package/public/fiddle/MicroComponents.svg +0 -15
- package/public/fiddle/fiddle.css +0 -0
- package/public/fiddle/fiddle.js +0 -0
- package/public/fiddle/index.html +0 -0
- package/public/formValues.js +0 -196
- package/public/formValues.js.map +0 -1
- package/public/formvValues.min.js +0 -1
- package/public/helpers.js +0 -105
- package/public/helpers.js.map +0 -1
- package/public/microcomponents.es3.js +0 -303
- package/public/microcomponents.es3.js.map +0 -1
- package/public/microcomponents.js +0 -9
- package/public/microcomponents.js.map +0 -1
- package/public/microcomponents.min.js +0 -1
- package/public/my-tag.css +0 -19
- package/public/observables.js +0 -239
- package/public/observables.js.map +0 -1
- package/public/observables.min.js +0 -1
- package/public/selectors.js +0 -971
- package/public/selectors.js.map +0 -1
- package/public/selectors.min.js +0 -1
- package/public/src/MC.ts +0 -413
- package/public/src/McEvent.ts +0 -96
- package/public/src/autoloader.ts +0 -98
- package/public/src/elements.ts +0 -76
- package/public/src/formValues.ts +0 -275
- package/public/src/helpers.ts +0 -140
- package/public/src/microcomponents.ts +0 -8
- package/public/src/observables.ts +0 -372
- package/public/src/selectors.ts +0 -1634
- package/public/src/test.ts +0 -174
- package/public/test.html +0 -23
- package/public/test.js +0 -79
- package/public/test.js.map +0 -1
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
# Micro Components <span style="float:right; height:1em;"><img height="24px" src="./MicroComponents.svg" /></span>
|
|
2
|
-
|
|
3
|
-
This system allows you to to define **Micro Components** and attach them to DOM elements or other Micro Components.
|
|
4
|
-
|
|
5
|
-
The module exports 2 classes:
|
|
6
|
-
- **MC**: Micro Component ( not DOM related, works on the server also )
|
|
7
|
-
- **DC**: Dom Component ( extends MC and adds DOM related methods, client only )
|
|
8
|
-
|
|
9
|
-
# Usage
|
|
10
|
-
|
|
11
|
-
- **If your functionality needs to access the DOM**, e.g. to find other DC instances, **your class extends DC**.
|
|
12
|
-
- **Otherwise** - and always on the server - **your class extends MC**.
|
|
13
|
-
|
|
14
|
-
In any case, your class instances will contain a property **"_mc"** where you can (but don't have to) put in other MCs.
|
|
15
|
-
|
|
16
|
-
Also your class instances will have a readonly **"target"** property that is back reference to the instance they are contained in.
|
|
17
|
-
|
|
18
|
-
- if your class extends **DC: "target"** will refer the **DOM HTMLElement your instance resides in**.
|
|
19
|
-
- if your class extends **MC: "target"** will refer the **parent MC your instance resides in**.
|
|
20
|
-
|
|
21
|
-
As an application programmer you should understand **MC** and **DC** methods and the structure they provide:
|
|
22
|
-
- **MC** encapsule functionality in a reusable way. Think DRY, and SOLID because we talk about objects.
|
|
23
|
-
- **MC** can use observables to provide data driven workflow between multiple MCs.
|
|
24
|
-
- **DC** use observables and formValues to provide 1-way and 2-way binding to the DOM.
|
|
25
|
-
- **DC** have selectors to find other DCs in the DOM.
|
|
26
|
-
|
|
27
|
-
# Rules
|
|
28
|
-
|
|
29
|
-
- **Your own application code MUST NOT overwrite the properties and methods in the classes below.**
|
|
30
|
-
- If you do it anyway, you do it on your own risk.
|
|
31
|
-
|
|
32
|
-
# Not Exported
|
|
33
|
-
|
|
34
|
-
## **McBase** Superclass
|
|
35
|
-
|
|
36
|
-
**Instance Properties**
|
|
37
|
-
- `.target`: readonly, a getter returning the DOM element or parent MC instance your instance resides in
|
|
38
|
-
|
|
39
|
-
**Constructor:**
|
|
40
|
-
- calls `init()`: triggers the 'init' lifecycle event, also recursively into the inner '._mc' structure.
|
|
41
|
-
- calls `autoAttachListeners()`: methods in your class code that start with "on[A-Z]" or "one[A-Z]" and describe native DOM events (e.g. "onClick") will be added to the DOM Element as listeners.
|
|
42
|
-
|
|
43
|
-
- **Instance Methods:**
|
|
44
|
-
- `.trigger( eventName, data )`: runs callback methods.
|
|
45
|
-
|
|
46
|
-
# Exported:
|
|
47
|
-
|
|
48
|
-
## **MC** Class, extends McBase
|
|
49
|
-
|
|
50
|
-
**Instance Properties**
|
|
51
|
-
- `.target`: (inherited from McBase)
|
|
52
|
-
- `._mc`: a hash object that contains inner micro components
|
|
53
|
-
|
|
54
|
-
- **Instance Methods:**
|
|
55
|
-
- `.trigger( eventName, data )`: (inherited from McBase)
|
|
56
|
-
|
|
57
|
-
## **DC** Class, extends McBase
|
|
58
|
-
|
|
59
|
-
- **Static Methods:**
|
|
60
|
-
- `DC.add( DomElement, key, mc)`: Add a DC to an HTMLElement.
|
|
61
|
-
- `DC.remove( DomElement, key )`: Remove a DC from an HTMLElement.
|
|
62
|
-
- `DC.trigger( eventName, DomElement, data, bubble )`: trigger an event, handles directional traversal in the DOM.
|
|
63
|
-
|
|
64
|
-
**Instance Properties**
|
|
65
|
-
- `.target`: (inherited from McBase)
|
|
66
|
-
- `._mc`: a hash object that contains inner micro components
|
|
67
|
-
|
|
68
|
-
- **Instance Methods:**
|
|
69
|
-
- `.parent(search?)`: Get parent DCs.
|
|
70
|
-
- `.ancestors(search?)`: Get ancestor DCs.
|
|
71
|
-
- `.children(search?)`: Get child DCs.
|
|
72
|
-
- `.descendants(search?)`: Get descendant DCs.
|
|
73
|
-
- `.trigger( eventName, data, bubble )`: trigger an event on "this", handles directional bubbling in the DOM.
|
|
74
|
-
|
|
75
|
-
# Examples
|
|
76
|
-
|
|
77
|
-
## Typical Workflow
|
|
78
|
-
|
|
79
|
-
1. **Attach DCs** to HTMLElements using `DC.add()`.
|
|
80
|
-
2. **Access** other DCs by their property key or navigating through DOM (e.g., `ancestors()`, `children()`, etc.) **and use their inner functionality**.
|
|
81
|
-
|
|
82
|
-
### Attach Micro Components to a DOM element
|
|
83
|
-
|
|
84
|
-
To attach a micro component to an HTML element, use the `DC.add()` static function.
|
|
85
|
-
|
|
86
|
-
```javascript
|
|
87
|
-
const myElement = document.getElementById('myDiv');
|
|
88
|
-
|
|
89
|
-
/*
|
|
90
|
-
usage with classes, both PUBLIC and PRIVATE
|
|
91
|
-
*/
|
|
92
|
-
|
|
93
|
-
// internal MC class
|
|
94
|
-
MyInnerClass = class extends MC{
|
|
95
|
-
constructor(target){
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// external DC class
|
|
100
|
-
MyPublicClass = class extends DC{
|
|
101
|
-
constructor(target){
|
|
102
|
-
// add a internal inner MC
|
|
103
|
-
this._mc.myInnerComponent = new MyInnerClass( this );
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// attach a DC
|
|
108
|
-
DC.add( myElement, 'myDcComponent', new MyPublicClass( myElement ) );
|
|
109
|
-
```
|
|
110
|
-
|
|
111
|
-
#### **Hint**
|
|
112
|
-
- After you added a micro component using this function, the **HTMLElement will contain a property `_mc`** which contains the MC instances visible in the DOM.
|
|
113
|
-
- Also the **HTMLElement then has an attribute '_mc'** to indicate that at least one micro component is attached to this dom element.
|
|
114
|
-
|
|
115
|
-
### Remove a top level Micro Component from a DOM element
|
|
116
|
-
|
|
117
|
-
Micro components can be removed using the `DC.remove()` static method.
|
|
118
|
-
|
|
119
|
-
```javascript
|
|
120
|
-
DC.remove( myDomElement, 'myComponent');
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
**When all micro components are removed** (the _mc object has no more properties), the **`_mc` attribute is also removed** from the HTML element.
|
|
124
|
-
|
|
125
|
-
### Retrieve Micro Components from a DOM element
|
|
126
|
-
|
|
127
|
-
```javascript
|
|
128
|
-
const target = document.querySelector( 'div' );
|
|
129
|
-
console.log(target._mc);
|
|
130
|
-
```
|
|
131
|
-
|
|
132
|
-
To retrieve a specific micro component by its key:
|
|
133
|
-
```javascript
|
|
134
|
-
const target = document.querySelector( 'div' );
|
|
135
|
-
const specificInstance = target._mc?.myPublicComponent;
|
|
136
|
-
console.log(specificInstance);
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
### Trigger an Event
|
|
140
|
-
|
|
141
|
-
```javascript
|
|
142
|
-
const target = document.querySelector( 'div' );
|
|
143
|
-
DC.trigger( target, '<eventName>', data | null, bubble );
|
|
144
|
-
|
|
145
|
-
// bubble is any combination of 'lud', meaning [local|up|down]
|
|
146
|
-
// local is only "this", "up" traverses towards root, "down" traverses the inner DOM.
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
# Methods you can use in your Micro Component Instances
|
|
150
|
-
|
|
151
|
-
## Retrieve Inner Micro Components
|
|
152
|
-
|
|
153
|
-
You can retrieve an inner micro component by using the ._mc property.
|
|
154
|
-
|
|
155
|
-
```javascript
|
|
156
|
-
const innerComponent = this._mc.myInnerComponent;
|
|
157
|
-
console.log(innerComponent);
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
**The following methods hop over HTMLElements that DO NOT contain micro components.**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
## Navigate Parent and Ancestor Micro Components
|
|
164
|
-
|
|
165
|
-
The `.parent()` and `.ancestors()` methods allow you to retrieve micro components attached to parent elements.
|
|
166
|
-
|
|
167
|
-
```javascript
|
|
168
|
-
const parentMcs = this.parent();
|
|
169
|
-
console.log(parentMcs); // all micro components of the closest ancestor that has an _mc attribute
|
|
170
|
-
|
|
171
|
-
const allAncestorsMcs = this.ancestors();
|
|
172
|
-
console.log(allAncestorsMcs); // all ancestor micro components up to document root.
|
|
173
|
-
```
|
|
174
|
-
|
|
175
|
-
To retrieve specific micro component(s) attached to ancestors:
|
|
176
|
-
```javascript
|
|
177
|
-
const specificAncestorMc = this.ancestors('myComponent');
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
## Retrieve Child and Descendant Micro Components
|
|
181
|
-
|
|
182
|
-
The `.children()` and `.descendants()` methods retrieve micro components attached to child elements or any descendants.
|
|
183
|
-
```javascript
|
|
184
|
-
const childComponents = this.children();
|
|
185
|
-
console.log(childComponents); // an array of nearest micro components in the inner DOM
|
|
186
|
-
|
|
187
|
-
const descendantComponents = this.descendants();
|
|
188
|
-
console.log(descendantComponents); // an array of all descendant micro components in the inner DOM
|
|
189
|
-
|
|
190
|
-
//To search for a specific micro component by key:
|
|
191
|
-
const specificMcs = this.descenants('specificMc');
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
## Trigger an Event
|
|
195
|
-
```javascript
|
|
196
|
-
// trigger an event on yourself
|
|
197
|
-
this
|
|
198
|
-
.trigger('myEvent', data); // calls the "onMyEvent" callback
|
|
199
|
-
|
|
200
|
-
// trigger an event on your external siblings
|
|
201
|
-
DC
|
|
202
|
-
.trigger( this.target, 'myEvent', data); // calls the "onMyEvent" callback on all external MCs that listen to it
|
|
203
|
-
|
|
204
|
-
// trigger an event on some remote DOM instance(s)
|
|
205
|
-
DC
|
|
206
|
-
.trigger(
|
|
207
|
-
this.target, // the DOM node your are in
|
|
208
|
-
'myEvent', // event name
|
|
209
|
-
null, // no data
|
|
210
|
-
'd' // into descendants
|
|
211
|
-
); // calls the "onMyEvent" callback
|
|
212
|
-
|
|
213
|
-
// trigger an event on some remote DC instance that you know the name of
|
|
214
|
-
this
|
|
215
|
-
.descendants('myRemoteInstance') // into my inner HTML
|
|
216
|
-
?.[0] // hopefully we found one
|
|
217
|
-
.trigger(
|
|
218
|
-
'myEvent', // event name
|
|
219
|
-
null, // no data
|
|
220
|
-
);
|
|
221
|
-
```
|
|
222
|
-
|
|
223
|
-
## Automatically attach Listener Methods to the DOM
|
|
224
|
-
|
|
225
|
-
On any **MC** and **DC** instance, the Superclass McBase calls **autoAttachListeners()** in the constructor.
|
|
226
|
-
|
|
227
|
-
Any methods you define in your code that start with **"on"** or **"one"**, and also match DOM event names, will be called accordingly:
|
|
228
|
-
|
|
229
|
-
```javascript
|
|
230
|
-
class MyEventTarget extends MC {
|
|
231
|
-
constructor(target) {
|
|
232
|
-
super(target);
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
onClick(ev) {
|
|
236
|
-
console.log("onClick", ev);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
let myFirstDiv = document.querySelector('div');
|
|
241
|
-
DC.add( myFirstDiv, 'myEventTarget', new MyEventTarget( myFirstDiv ) );
|
|
242
|
-
// now wait for somebody to click on that <div>
|
|
243
|
-
```
|
|
244
|
-
# Lifecycle Events on **MC** and **DC**
|
|
245
|
-
|
|
246
|
-
## Init
|
|
247
|
-
- "this" first
|
|
248
|
-
- traverses recursively into the _mc structure.
|
|
249
|
-
- callbacks will be executed **asynchronous**.
|
|
250
|
-
- requires **method .oneInit()** to be present in your class definition.
|
|
251
|
-
|
|
252
|
-
## Destroy ( TBD )
|
|
253
|
-
- deepest nested elements first.
|
|
254
|
-
- callbacks will be executed **synchronous**.
|
|
255
|
-
- then "this"
|
|
256
|
-
- requires **method .oneDestroy()** to be present in your class definition.
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
File without changes
|
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
# Custom Elements <span style="float:right; height:1em;"><img height="24px" src="https://gitlab.com/twoBirds/microcomponents/-/raw/main/MicroComponents.svg" /></span>
|
|
2
|
-
|
|
3
|
-
Custom Elements provides two funktions:
|
|
4
|
-
- **defineCE()** lets you define a custom tag and add functionality to it. Internally it uses [Web Components](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define).
|
|
5
|
-
- **getCE()** retrieves a custom element constructor from the web component registry via [CustomElementRegistry.get()](https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/get).
|
|
6
|
-
|
|
7
|
-
# Prerequisites
|
|
8
|
-
|
|
9
|
-
You should at least know Micro Components **[DC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MicroComponents.md?ref_type=heads#dc-class-extends-mcbase)** superclass usage, since your own class code extends from it and uses its static functions. It is recommended that you have read and understood the **[Micro Component documentation](https://gitlab.com/twoBirds/microcomponents/-/blob/main/assets/documentation/MicroComponents.md?ref_type=heads)**, since custom elements is just an extension that internally uses micro components.
|
|
10
|
-
|
|
11
|
-
# Usage
|
|
12
|
-
|
|
13
|
-
## Create a Custom Element
|
|
14
|
-
|
|
15
|
-
Before:
|
|
16
|
-
|
|
17
|
-
```html
|
|
18
|
-
<body></body>
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
```javascript
|
|
22
|
-
defineCE(
|
|
23
|
-
// equivalent to and internally using customElementRegistry.define()
|
|
24
|
-
"my-tag",
|
|
25
|
-
class MyTag extends DC {
|
|
26
|
-
|
|
27
|
-
constructor(target, data = {} ) {
|
|
28
|
-
super(target);
|
|
29
|
-
this.data = data;
|
|
30
|
-
target.innerHTML = "Hello World";
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
let MyTag = getCE("my-tag"),
|
|
36
|
-
data = { a: 1 },
|
|
37
|
-
myTag = new MyTag( data );
|
|
38
|
-
|
|
39
|
-
/*
|
|
40
|
-
HINT:
|
|
41
|
-
At this point the web component is not yet attached to the DOM.
|
|
42
|
-
But you can perform any operation on it, e.g. calling methods on its inner micro components.
|
|
43
|
-
*/
|
|
44
|
-
|
|
45
|
-
document.body.append( myTag );
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
After:
|
|
49
|
-
```html
|
|
50
|
-
<body>
|
|
51
|
-
<my-tag _mc>Hello World</my-tag>
|
|
52
|
-
</body>
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### **Hints**
|
|
56
|
-
- 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.
|
|
57
|
-
- it is essential to understand that your web component code is only one of the numerous micro components that can reside inside `<my-tag>`
|
|
58
|
-
- After you created a custom element, the custom element will contain a **property `<my-tag>._mc`** which contains your class instance.
|
|
59
|
-
- Also the custom element then has an **attribute '_mc'** which will indicate that at least one micro component is attached to it.
|
|
60
|
-
- Note that you can hand over data natively via parameters, when handling custom elements in above way.
|
|
61
|
-
- 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.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
## Web Component Lifecycle Events
|
|
65
|
-
|
|
66
|
-
```javascript
|
|
67
|
-
defineCE(
|
|
68
|
-
"my-tag",
|
|
69
|
-
class MyTag extends DC {
|
|
70
|
-
|
|
71
|
-
constructor(target) {
|
|
72
|
-
super(target);
|
|
73
|
-
target.setAttribute('my-attribute', 'change me');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/*
|
|
77
|
-
web component specific event handlers
|
|
78
|
-
*/
|
|
79
|
-
|
|
80
|
-
onConnected(ev) {
|
|
81
|
-
console.info("MyTag .onConnected() handler", ev);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
onAttributeChanged(ev) {
|
|
85
|
-
console.info("MyTag .onAttributeChanged() handler", ev);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
onAdopted(ev) {
|
|
89
|
-
console.info("MyTag .onAdopted() handler", ev);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
onDisconnected(ev) {
|
|
93
|
-
console.info("MyTag .onDisconnected() handler", ev);
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
['my-attribute'] // DOM node attributes to observe
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
let MyTag = getCE("my-tag"),
|
|
100
|
-
myTag = new MyTag();
|
|
101
|
-
|
|
102
|
-
document.body.append( myTag );
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
You can use your Web Component constructor to add other modules to the `<my-tag>` DOM node:
|
|
106
|
-
|
|
107
|
-
```javascript
|
|
108
|
-
|
|
109
|
-
class OtherModule extends DC {
|
|
110
|
-
|
|
111
|
-
constructor(target) {
|
|
112
|
-
super(target);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
onConnected(ev) {
|
|
116
|
-
console.info("[OtherModule] .onConnected() handler", ev);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
defineCE(
|
|
121
|
-
"my-tag",
|
|
122
|
-
class MyTag extends DC {
|
|
123
|
-
|
|
124
|
-
constructor(target) {
|
|
125
|
-
super(target);
|
|
126
|
-
DC.add( target, 'otherModule', new OtherModule(target) );
|
|
127
|
-
// equivalent:
|
|
128
|
-
// target._mc.otherModule = new OtherModule(target);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
onConnected(ev) {
|
|
132
|
-
console.info("[MyTag] .onConnected() handler", ev);
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
let MyTag = getCE("my-tag");
|
|
139
|
-
|
|
140
|
-
document.body.append( new MyTag() );
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
### **Hints**
|
|
144
|
-
- You can add as many other micro components to the DOM node as you like.
|
|
145
|
-
- The native web component lifecycle events will be triggered on **all** micro components.
|
|
146
|
-
- The event parameter handed over is an internal event instance, that contains the original native event in its `data` property.
|
|
147
|
-
|
|
148
|
-
## Native Event Handlers
|
|
149
|
-
|
|
150
|
-
Native event handlers will be auto-attached, when the method name follows one of these two schemes:
|
|
151
|
-
|
|
152
|
-
- **on[A-Z]...**: e.g. **onClick(){ ... }** will be called when the native event is triggered
|
|
153
|
-
- **one[A-Z]...**: e.g. **oneClick(){ ... }** will be called **once** only when the native event is triggered. Subsequent events will be ignored.
|
|
154
|
-
|
|
155
|
-
```javascript
|
|
156
|
-
defineCE(
|
|
157
|
-
"my-tag",
|
|
158
|
-
class MyTag extends DC {
|
|
159
|
-
|
|
160
|
-
constructor(target) {
|
|
161
|
-
super(target);
|
|
162
|
-
target.innerHTML = "Click Me";
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/*
|
|
166
|
-
native event handlers
|
|
167
|
-
*/
|
|
168
|
-
onClick(ev) {
|
|
169
|
-
console.info("MyTag .onClick() handler", ev);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
);
|
|
173
|
-
|
|
174
|
-
let MyTag = getCE("my-tag");
|
|
175
|
-
|
|
176
|
-
document.body.append( new MyTag() );
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
### **Hints**
|
|
180
|
-
- 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.
|
|
181
|
-
- The event parameter handed over is an internal event instance, that contains the original native event in its .data property.
|
|
182
|
-
|
|
183
|
-
## Lifecycle Event Handlers
|
|
184
|
-
|
|
185
|
-
Currently there is only one lifecycle event handler: the **oneInit(){ ... }** method you provide with your class.
|
|
186
|
-
|
|
187
|
-
This method is triggered asynchronous via a timeout after you made a new instance of your class.
|
|
188
|
-
|
|
189
|
-
```javascript
|
|
190
|
-
defineCE(
|
|
191
|
-
"my-tag",
|
|
192
|
-
class MyTag extends DC {
|
|
193
|
-
|
|
194
|
-
constructor(target) {
|
|
195
|
-
super(target);
|
|
196
|
-
target.innerHTML = "Hello World";
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/*
|
|
200
|
-
lifecycle event handlers
|
|
201
|
-
*/
|
|
202
|
-
oneInit(ev) {
|
|
203
|
-
console.info("MyTag .onInit() handler", ev);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
);
|
|
207
|
-
|
|
208
|
-
let MyTag = getCE("my-tag");
|
|
209
|
-
|
|
210
|
-
document.body.append( new MyTag() );
|
|
211
|
-
```
|
|
212
|
-
|
|
213
|
-
### **Hints**
|
|
214
|
-
- 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.
|
|
215
|
-
- The event parameter handed over is an internal event instance that contains no data.
|
|
216
|
-
|
|
217
|
-
## Custom Events
|
|
218
|
-
|
|
219
|
-
```javascript
|
|
220
|
-
defineCE(
|
|
221
|
-
"my-tag",
|
|
222
|
-
class MyTag extends DC {
|
|
223
|
-
|
|
224
|
-
constructor(target) {
|
|
225
|
-
super(target);
|
|
226
|
-
target.innerHTML = "Hello World";
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/*
|
|
230
|
-
custom event handlers
|
|
231
|
-
*/
|
|
232
|
-
onSpecialEvent(ev) {
|
|
233
|
-
console.log('MyTag .onSpecialEvent() handler', ev);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
);
|
|
237
|
-
|
|
238
|
-
let MyTag = getCE("my-tag");
|
|
239
|
-
|
|
240
|
-
document.body.append( new MyTag() );
|
|
241
|
-
|
|
242
|
-
document
|
|
243
|
-
.querySelector( 'my-tag' )
|
|
244
|
-
?._mc
|
|
245
|
-
.MyTag
|
|
246
|
-
.trigger('specialEvent');
|
|
247
|
-
|
|
248
|
-
/*
|
|
249
|
-
Alternatives:
|
|
250
|
-
*/
|
|
251
|
-
|
|
252
|
-
document
|
|
253
|
-
.querySelector( 'my-tag' )
|
|
254
|
-
?._mc
|
|
255
|
-
.trigger('specialEvent'); // ...on all micro components contained in <my-tag>
|
|
256
|
-
|
|
257
|
-
/*
|
|
258
|
-
traversing the DOM:
|
|
259
|
-
<my-tag>
|
|
260
|
-
<my-inner-tag />
|
|
261
|
-
</my-tag>
|
|
262
|
-
*/
|
|
263
|
-
|
|
264
|
-
// <my-tag> method:
|
|
265
|
-
onSpecialEvent(ev){
|
|
266
|
-
console.log( ev.data );
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
// <my-inner-tag> method:
|
|
270
|
-
sendSpecialEvent(){
|
|
271
|
-
this.trigger( 'specialEvent', "Hello World", 'u' ); // 'u' = bubble up.
|
|
272
|
-
}
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
### **Hints**
|
|
276
|
-
- The event parameter handed over is an internal event instance, that contains the payload in its .data property.
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
## Functional Requirements
|
|
2
|
-
|
|
3
|
-
- I want to **enrich a standard HTMLElement instance or any object with encapsuled and modular javascript**, without having to resort to web components or big frameworks.
|
|
4
|
-
- I want to have an **interface** into this functionality that I can both tight and loosely couple.
|
|
5
|
-
- I want to **import and use Micro Components in any framework** that allows for direct DOM access.
|
|
6
|
-
- I want **a single location** in an HTMLElement instance where all this functionality is **stored and sufficiently organized**.
|
|
7
|
-
- I want to be able to use **selectors** to find those instances in the DOM.
|
|
8
|
-
- All this must stricly follow **web standards**.
|