@twobirds/microcomponents 0.0.46 → 0.0.50
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 +206 -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 +72 -33
- package/dist/selectors.min.js +1 -1
- package/dist/test.ts +14 -14
- package/package.json +1 -1
- package/public/Grandparent.css +13 -0
- package/public/Grandparent.js +338 -0
- package/public/MC.js +43 -0
- package/public/MC.js.map +1 -1
- package/public/MC.min.js +44 -0
- package/public/TestForm.css +47 -0
- package/public/TestForm.html +56 -0
- package/public/TestForm.js +56 -0
- package/public/TestForm.json +27 -0
- package/public/Testform.js +149 -0
- 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/documentation/MC.md +380 -166
- package/public/documentation/observe.md +317 -0
- package/public/elements.js +9 -9
- package/public/elements.js.map +1 -1
- package/public/elements.min.js +1 -1
- package/public/formValues.js +142 -133
- package/public/formValues.js.map +1 -1
- package/public/formvValues.min.js +1 -1
- package/public/functions/MC.addListener.html +1 -1
- package/public/functions/MC.removeListener.html +1 -1
- package/public/functions/elements.defineCE.html +1 -1
- package/public/functions/elements.getCE.html +1 -1
- package/public/functions/formValues.formValues.html +1 -1
- package/public/functions/observables.observe.html +1 -1
- package/public/functions/selectors._d.html +1 -1
- package/public/functions/selectors._t.html +1 -1
- package/public/helpers.js +8 -0
- package/public/helpers.js.map +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/microcomponents.min.js +1 -1
- package/public/modules/MC.html +1 -1
- package/public/modules/elements.html +1 -1
- package/public/modules/formValues.html +1 -1
- package/public/modules/observables.html +1 -1
- package/public/modules/selectors.html +1 -1
- package/public/observables.js +39 -21
- package/public/observables.js.map +1 -1
- package/public/observables.min.js +1 -1
- package/public/selectors.min.js +1 -1
- package/public/src/MC.ts +65 -0
- package/public/src/elements.ts +10 -10
- package/public/src/formValues.ts +206 -203
- package/public/src/helpers.ts +9 -0
- package/public/src/observables.ts +72 -33
- package/public/src/test.ts +14 -14
- package/public/test/Grandparent.js +1 -0
- package/public/test/Testform.js +1 -0
- package/public/test.html +5 -1
- package/public/test.js +9 -11
- package/public/test.js.map +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 +206 -203
- package/src/helpers.ts +9 -0
- package/src/observables.ts +72 -33
- 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 +14 -14
- package/webpack.config.js +135 -61
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
Okay, I understand. You want me to modify the code examples within the Markdown documentation so that they demonstrate how to use the observe() functionality inside a microcomponent's constructor (specifically, an MC constructor). I'll also keep the output as a JavaScript variable assignment (let md = "...";).
|
|
2
|
+
|
|
3
|
+
Here's the updated version of the code, with the examples adapted to be used within an MC constructor:
|
|
4
|
+
JavaScript
|
|
5
|
+
|
|
6
|
+
let md = `# Observable Properties Functionality (`observe()`)
|
|
7
|
+
|
|
8
|
+
This document explains the `observe()` functionality, a utility for making properties of JavaScript objects observable. When an observable property's value changes, registered callback functions are automatically notified, enabling reactive data binding and UI updates.
|
|
9
|
+
|
|
10
|
+
## Table of Contents
|
|
11
|
+
|
|
12
|
+
- [Core Concept](#core-concept)
|
|
13
|
+
- [Function Signature](#function-signature)
|
|
14
|
+
- [How it Works](#how-it-works)
|
|
15
|
+
- [Key Features](#key-features)
|
|
16
|
+
- [Usage](#usage)
|
|
17
|
+
- [Making Properties Observable (within MC Constructor)](#making-properties-observable-within-mc-constructor)
|
|
18
|
+
- [Registering Observers (`observe()` method on Observable) (within MC Constructor)](#registering-observers-observe-method-on-observable-within-mc-constructor)
|
|
19
|
+
- [Binding Observable Objects to the DOM (`bind()` method on Observable Object) (within MC Constructor)](#binding-observable-objects-to-the-dom-bind-method-on-observable-object-within-mc-constructor)
|
|
20
|
+
- [Manually Notifying Observers (`notify()` method on Observable) (within MC Constructor)](#manually-notifying-observers-notify-method-on-observable-within-mc-constructor)
|
|
21
|
+
- [Examples of Useful Usage (within MC Constructor)](#examples-of-useful-usage-within-mc-constructor)
|
|
22
|
+
- [Simple Data Binding (within MC Constructor)](#simple-data-binding-within-mc-constructor)
|
|
23
|
+
- [Automatic UI Updates in Web Components (within MC Constructor)](#automatic-ui-updates-in-web-components-within-mc-constructor)
|
|
24
|
+
- [Centralized State Management (within MC Constructor)](#centralized-state-management-within-mc-constructor)
|
|
25
|
+
- [Form Input Synchronization (within MC Constructor)](#form-input-synchronization-within-mc-constructor)
|
|
26
|
+
- [Considerations](#considerations)
|
|
27
|
+
- [Performance](#performance)
|
|
28
|
+
- [Deep Object Observation](#deep-object-observation)
|
|
29
|
+
- [Type Changes](#type-changes)
|
|
30
|
+
- [Related Concepts](#related-concepts)
|
|
31
|
+
|
|
32
|
+
## <a name="core-concept"></a>Core Concept
|
|
33
|
+
|
|
34
|
+
The `observe()` function transforms specified properties of a JavaScript object into "observable" properties. This means that changes to these properties can be intercepted, and registered listeners (callbacks) can be automatically invoked to react to these changes. This pattern is fundamental for implementing reactive programming paradigms, where UI or other parts of an application automatically update in response to data modifications.
|
|
35
|
+
|
|
36
|
+
## <a name="function-signature"></a>Function Signature
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
observe(target: LooseObject, propertyName?: string): LooseObject
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
- `target: LooseObject`: The JavaScript object whose properties you want to make observable. `LooseObject` is likely a type definition for a generic object (e.g., `{ [key: string]: any }`).
|
|
43
|
+
- `propertyName?: string`: An optional string specifying a single property of the `target` object to make observable. If this argument is omitted, the function attempts to make all own properties of the `target` observable.
|
|
44
|
+
- `returns: LooseObject`: The original `target` object, now with the specified (or all) properties made observable.
|
|
45
|
+
|
|
46
|
+
## <a name="how-it-works"></a>How it Works
|
|
47
|
+
|
|
48
|
+
The `observe()` function achieves observability by:
|
|
49
|
+
|
|
50
|
+
1. **Redefining Properties:** It uses `Object.defineProperty` to redefine the getter and setter of the `target` property (or properties).
|
|
51
|
+
2. **Creating an `Observable` Wrapper:** For each observable property, it creates an instance of a dynamically generated `Observable` class that extends the original constructor of the property's value (e.g., `String`, `Number`, `Object`, `Array`). This wrapper holds the actual value and manages a list of observer callbacks.
|
|
52
|
+
3. **Intercepting Access (Getter):** When an observable property is accessed, the getter returns the `Observable` wrapper instance. For observable objects, it also sets up a debounced function to check for deep changes within the object at a later point.
|
|
53
|
+
4. **Intercepting Modification (Setter):** When an observable property is assigned a new value, the setter:
|
|
54
|
+
- Checks if the type of the new value is compatible with the old value.
|
|
55
|
+
- Creates a new `Observable` wrapper with the new value.
|
|
56
|
+
- Notifies all registered observer callbacks associated with that property (or the entire observable object, depending on how observation is set up).
|
|
57
|
+
5. **Managing Callbacks:** Each `Observable` wrapper maintains a list of callback functions registered through its `observe()` method. These callbacks are invoked when the observable's value changes.
|
|
58
|
+
6. **DOM Binding (`bind()`):** For observable objects, the `bind()` method facilitates automatic synchronization with placeholders in the DOM using the `Placeholders` DOM Component.
|
|
59
|
+
|
|
60
|
+
## <a name="key-features"></a>Key Features
|
|
61
|
+
|
|
62
|
+
- Selective Observability: You can choose to make specific properties observable or observe all properties of an object.
|
|
63
|
+
- Type Checking: The setter attempts to prevent changing the data type of an observable property.
|
|
64
|
+
- Callback Mechanism: Allows registering multiple functions to be notified of changes.
|
|
65
|
+
- DOM Binding Integration: Provides a `bind()` method for observable objects to automatically update DOM elements containing placeholders.
|
|
66
|
+
- Debounced Deep Change Detection: For observable objects, changes within the object are detected using a debounced deep equality check to optimize performance.
|
|
67
|
+
|
|
68
|
+
## <a name="usage"></a>Usage
|
|
69
|
+
|
|
70
|
+
### <a name="making-properties-observable-within-mc-constructor"></a>Making Properties Observable (within MC Constructor)
|
|
71
|
+
|
|
72
|
+
To make properties of an object observable within an MC constructor, call the `observe()` function with the object as the `target`. You can optionally specify a `propertyName` to observe only a single property.
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
import { observe } from './observables.js'; // Assuming your observe() function is in this file
|
|
76
|
+
import { MC } from './MC.js'; // Import your MC class
|
|
77
|
+
|
|
78
|
+
class MyComponent extends MC {
|
|
79
|
+
constructor(target) {
|
|
80
|
+
super(target);
|
|
81
|
+
|
|
82
|
+
this.myData = {
|
|
83
|
+
name: 'Initial Name',
|
|
84
|
+
count: 0,
|
|
85
|
+
settings: {
|
|
86
|
+
theme: 'light',
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
observe(this.myData); // Make all properties of this.myData observable
|
|
91
|
+
|
|
92
|
+
this.myData.name = 'Updated Name'; // Observers of this.myData.name will be notified
|
|
93
|
+
this.myData.count++; // Observers of this.myData.count will be notified
|
|
94
|
+
this.myData.settings.theme = 'dark'; // Observers of this.myData.settings will be notified
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```javascript
|
|
100
|
+
import { observe } from './observables.js';
|
|
101
|
+
import { MC } from './MC.js';
|
|
102
|
+
|
|
103
|
+
class MyConfigComponent extends MC {
|
|
104
|
+
constructor(target) {
|
|
105
|
+
super(target);
|
|
106
|
+
|
|
107
|
+
this.config = {
|
|
108
|
+
apiKey: 'your_api_key',
|
|
109
|
+
isEnabled: true,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
observe(this.config, 'apiKey'); // Only make the apiKey property observable
|
|
113
|
+
|
|
114
|
+
this.config.apiKey = 'new_api_key'; // Observers of this.config.apiKey will be notified
|
|
115
|
+
this.config.isEnabled = false; // Observers of this.config.isEnabled will NOT be directly notified by observe()
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### <a name="registering-observers-observe-method-on-observable-within-mc-constructor"></a>Registering Observers (`observe()` method on Observable) (within MC Constructor)
|
|
121
|
+
|
|
122
|
+
When a property becomes observable, its getter will return an `Observable` wrapper. This wrapper has an `observe()` method to register callback functions that will be executed when the observable's value changes.
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
import { observe } from './observables.js';
|
|
126
|
+
import { MC } from './MC.js';
|
|
127
|
+
|
|
128
|
+
class MyStateComponent extends MC {
|
|
129
|
+
constructor(target) {
|
|
130
|
+
super(target);
|
|
131
|
+
|
|
132
|
+
this.state = {
|
|
133
|
+
message: 'Hello',
|
|
134
|
+
};
|
|
135
|
+
observe(this.state);
|
|
136
|
+
|
|
137
|
+
this.state.message.observe((newValue) => {
|
|
138
|
+
console.log('Message changed to:', newValue);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
this.state.message = 'World'; // Output: Message changed to: World
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### <a name="binding-observable-objects-to-the-dom-bind-method-on-observable-object-within-mc-constructor"></a>Binding Observable Objects to the DOM (`bind()` method on Observable Object) (within MC Constructor)
|
|
147
|
+
|
|
148
|
+
If an observable property's value is an object, it will have a `bind()` method. This method allows you to automatically connect the observable object's properties to placeholders (`{propertyName}`) within a specified DOM element (or its descendants). This relies on the `Placeholders` DOM Component.
|
|
149
|
+
|
|
150
|
+
```javascript
|
|
151
|
+
import { observe } from './observables.js';
|
|
152
|
+
import { MC } from './MC.js';
|
|
153
|
+
|
|
154
|
+
class MyUserDataComponent extends MC {
|
|
155
|
+
constructor(target) {
|
|
156
|
+
super(target);
|
|
157
|
+
|
|
158
|
+
this.userData = {
|
|
159
|
+
firstName: 'John',
|
|
160
|
+
lastName: 'Doe',
|
|
161
|
+
};
|
|
162
|
+
observe(this.userData);
|
|
163
|
+
|
|
164
|
+
this.outputElement = document.getElementById('output'); // Assuming you have an element with id="output"
|
|
165
|
+
this.userData.bind(this.outputElement);
|
|
166
|
+
|
|
167
|
+
this.outputElement.innerHTML = `
|
|
168
|
+
<p>First Name: {firstName}</p>
|
|
169
|
+
<p>Last Name: {lastName}</p>
|
|
170
|
+
`;
|
|
171
|
+
|
|
172
|
+
this.userData.firstName = 'Jane'; // The text content of the first <p> will automatically update
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### <a name="manually-notifying-observers-notify-method-on-observable-within-mc-constructor"></a>Manually Notifying Observers (`notify()` method on Observable) (within MC Constructor)
|
|
178
|
+
|
|
179
|
+
The `Observable` wrapper also has a `notify()` method that you can call manually to force the registered observers to be executed. This can be useful in specific scenarios where changes might not be directly detected by the setter (though the debounced deep check for objects aims to handle this).
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
import { observe } from './observables.js';
|
|
183
|
+
import { MC } from './MC.js';
|
|
184
|
+
|
|
185
|
+
class MyDataItemsComponent extends MC {
|
|
186
|
+
constructor(target) {
|
|
187
|
+
super(target);
|
|
188
|
+
|
|
189
|
+
this.data = {
|
|
190
|
+
items: [],
|
|
191
|
+
};
|
|
192
|
+
observe(this.data);
|
|
193
|
+
|
|
194
|
+
this.data.items.observe(() => {
|
|
195
|
+
console.log('Items array changed:', this.data.items);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
// Directly modifying the array might not always trigger the setter
|
|
199
|
+
this.data.items.push('new item');
|
|
200
|
+
this.data.items.notify(); // Manually trigger the observers
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
## <a name="examples-of-useful-usage-within-mc-constructor"></a>Examples of Useful Usage (within MC Constructor)
|
|
206
|
+
|
|
207
|
+
### <a name="simple-data-binding-within-mc-constructor"></a>Simple Data Binding (within MC Constructor)
|
|
208
|
+
|
|
209
|
+
Observables are excellent for simple data binding scenarios where you want to keep different parts of your application synchronized with a piece of data.
|
|
210
|
+
|
|
211
|
+
```javascript
|
|
212
|
+
import { observe } from './observables.js';
|
|
213
|
+
import { MC } from './MC.js';
|
|
214
|
+
|
|
215
|
+
class MyInputComponent extends MC {
|
|
216
|
+
constructor(target) {
|
|
217
|
+
super(target);
|
|
218
|
+
|
|
219
|
+
this.viewModel = {
|
|
220
|
+
inputValue: '',
|
|
221
|
+
};
|
|
222
|
+
observe(this.viewModel);
|
|
223
|
+
|
|
224
|
+
this.inputElement = document.getElementById('myInput'); // Assuming you have an input with id="myInput"
|
|
225
|
+
this.outputSpan = document.getElementById('outputValue'); // Assuming you have a span with id="outputValue"
|
|
226
|
+
|
|
227
|
+
this.inputElement.addEventListener('input', (event) => {
|
|
228
|
+
this.viewModel.inputValue = event.target.value;
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
this.viewModel.inputValue.observe((newValue) => {
|
|
232
|
+
this.outputSpan.textContent = newValue;
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### <a name="automatic-ui-updates-in-web-components-within-mc-constructor"></a>Automatic UI Updates in Web Components (within MC Constructor)
|
|
239
|
+
|
|
240
|
+
As demonstrated in the `my-tag` example, observables can be used to automatically update the template of a web component when its internal data changes, leading to reactive and dynamic UIs.
|
|
241
|
+
|
|
242
|
+
### <a name="centralized-state-management-within-mc-constructor"></a>Centralized State Management (within MC Constructor)
|
|
243
|
+
|
|
244
|
+
Observables can form the basis of a simple state management system. By making the application's state an observable object, different components can observe relevant parts of the state and automatically update when those parts change.
|
|
245
|
+
|
|
246
|
+
```javascript
|
|
247
|
+
import { observe } from './observables.js';
|
|
248
|
+
import { MC } from './MC.js';
|
|
249
|
+
|
|
250
|
+
class MyAppStateComponent extends MC {
|
|
251
|
+
constructor(target) {
|
|
252
|
+
super(target);
|
|
253
|
+
|
|
254
|
+
this.appState = {
|
|
255
|
+
user: null,
|
|
256
|
+
isLoading: false,
|
|
257
|
+
errorMessage: null,
|
|
258
|
+
};
|
|
259
|
+
observe(this.appState);
|
|
260
|
+
|
|
261
|
+
// Component A observes the user
|
|
262
|
+
this.appState.user.observe((user) => {
|
|
263
|
+
if (user) {
|
|
264
|
+
console.log('User logged in:', user.name);
|
|
265
|
+
// Update UI to show user info
|
|
266
|
+
} else {
|
|
267
|
+
console.log('User logged out');
|
|
268
|
+
// Update UI to reflect logged out state
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
// Component B observes the loading state
|
|
273
|
+
this.appState.isLoading.observe((loading) => {
|
|
274
|
+
if (loading) {
|
|
275
|
+
console.log('Loading...');
|
|
276
|
+
// Show loading indicator
|
|
277
|
+
} else {
|
|
278
|
+
console.log('Loading complete.');
|
|
279
|
+
// Hide loading indicator
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// Actions can update the state
|
|
284
|
+
this.loginUser = (username, password) => {
|
|
285
|
+
this.appState.isLoading = true;
|
|
286
|
+
// ... perform login logic ...
|
|
287
|
+
setTimeout(() => {
|
|
288
|
+
this.appState.user = { name: username };
|
|
289
|
+
this.appState.isLoading = false;
|
|
290
|
+
}, 1000);
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
this.loginUser('john.doe', 'password');
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## <a name="considerations"></a>Considerations
|
|
299
|
+
|
|
300
|
+
### <a name="performance"></a>Performance
|
|
301
|
+
|
|
302
|
+
While observables provide a convenient way to handle data changes, be mindful of performance, especially with deeply nested objects or a large number of observers. Excessive notifications and deep equality checks can potentially impact performance.
|
|
303
|
+
|
|
304
|
+
### <a name="deep-object-observation"></a>Deep Object Observation
|
|
305
|
+
|
|
306
|
+
The provided `observe()` implementation includes a debounced deep equality check for object properties. However, for very complex or frequently changing nested objects, you might consider more optimized deep observation techniques if performance becomes an issue.
|
|
307
|
+
|
|
308
|
+
### <a name="type-changes"></a>Type Changes
|
|
309
|
+
|
|
310
|
+
The setter in the `observe()` function attempts to prevent changing the data type of an observable property. Be aware of this limitation when working with observable data.
|
|
311
|
+
|
|
312
|
+
## <a name="related-concepts"></a>Related Concepts
|
|
313
|
+
|
|
314
|
+
- Reactive Programming: A programming paradigm centered around the propagation of change.
|
|
315
|
+
- Data Binding: The process of synchronizing data between different parts of an application, often between the model and the view.
|
|
316
|
+
- DOM Components: Reusable units of behavior associated with DOM elements, as provided by the Microcomponent Framework.
|
|
317
|
+
- Placeholders: Dynamic markers within HTML that can be automatically updated with data.
|
package/public/elements.js
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
import { DC } from
|
|
3
|
-
import { kebabToPascal } from
|
|
1
|
+
'use strict';
|
|
2
|
+
import { DC } from './MC.js';
|
|
3
|
+
import { kebabToPascal } from './helpers.js';
|
|
4
4
|
class CeBaseClass extends HTMLElement {
|
|
5
5
|
constructor() {
|
|
6
6
|
super();
|
|
7
7
|
}
|
|
8
8
|
connectedCallback() {
|
|
9
|
-
DC.trigger(this,
|
|
9
|
+
DC.trigger(this, 'Connected');
|
|
10
10
|
}
|
|
11
11
|
disconnectedCallback() {
|
|
12
|
-
DC.trigger(this,
|
|
12
|
+
DC.trigger(this, 'Disconnected');
|
|
13
13
|
}
|
|
14
14
|
adoptedCallback() {
|
|
15
|
-
DC.trigger(this,
|
|
15
|
+
DC.trigger(this, 'Adopted');
|
|
16
16
|
}
|
|
17
17
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
18
|
-
DC.trigger(this,
|
|
18
|
+
DC.trigger(this, 'AttributeChanged', { name, oldValue, newValue });
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
function createNamedClass(name, ClassBody) {
|
|
22
|
-
return Object.defineProperty(ClassBody,
|
|
22
|
+
return Object.defineProperty(ClassBody, 'name', { value: name });
|
|
23
23
|
}
|
|
24
24
|
export function defineCE(tagName, DefinitionClass, observedAttributes = []) {
|
|
25
25
|
if (customElements.get(tagName)) {
|
|
26
26
|
console.warn(`IGNORED: Custom element "${tagName}" is already defined!`);
|
|
27
27
|
return;
|
|
28
28
|
}
|
|
29
|
-
if (!tagName.includes(
|
|
29
|
+
if (!tagName.includes('-')) {
|
|
30
30
|
throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');
|
|
31
31
|
}
|
|
32
32
|
let name = kebabToPascal(tagName);
|
package/public/elements.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"elements.js","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAC7B,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,oB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AQ,CAAM;QAEd,YAAY,IAAU;YACrB,KAAK,EAAE,CAAC;YACR,IAAI,QAAQ,GAAG,IAAI,eAAe,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;YAC/C,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"}
|
package/public/elements.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.elements=t():e.elements=t()}(this,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{defineCE:()=>y,getCE:()=>w});var r=new Function("try {return this===window;}catch(e){ return false;}");let n=new Set;r()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>n.add(e)));class o{#e;#t=!1;#r=!1;constructor(e,t={},r="l"){this.#e=e,this.data=t,this.bubble=r}get type(){return this.#e}get stopped(){return this.#t}get immediateStopped(){return this.#r}stopPropagation(){this.#t=!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(e,t){let r=[];return e.forEach((e=>{if(e&&e instanceof HTMLElement){const n=e?._mc;!t&&n?r=r.concat(Object.values(e._mc)):t&&n&&n[t]&&r.push(n[t])}})),r}function a(e,t,r=!0){let n=[];for(;e;){if(e._mc&&(t?t&&e._mc[t]&&n.push(e._mc[t]):n=n.concat(Object.values(e._mc)),r))return n;e=e.parentNode}return n}function u(e){return e&&e instanceof HTMLElement}function l(e,t,r={}){let n=e;if(!n._mc){let t=b?.debug;b.debug=!1,n._mc=new b(e),b.debug=t,delete n._mc._mc}const o=n._mc;return t.length&&!o[t]&&(o[t]=r,e instanceof HTMLElement&&e.setAttribute("_mc","")),r}function d(e,t){if(!t)return;const r=e?._mc||{};r&&r[t]&&delete r[t],!Object.keys(r).length&&e instanceof HTMLElement&&e.hasAttribute("_mc")&&e.removeAttribute("_mc")}function p(e,t,r={},n="l"){let i=e?._mc;if(!i)return;let c=t instanceof o?new o(t.type,t.data,"l"):new o(t,r,"l"),s="string"!=typeof t?t:new o(t,r,n);[...Object.values(i)].forEach((e=>{e?.trigger&&"function"==typeof e?.trigger&&e?.trigger(c)})),/[ud]/.test(s.bubble)&&f(e,s)}function f(e,t){/[ud]/.test(t.bubble)&&setTimeout((()=>{if(-1===t.bubble.indexOf("l")&&(t.bubble+="l"),t.bubble.indexOf("u")>-1){let r=a(e,"",!0)?.[0].target;r&&b.trigger(r,t)}t.bubble.indexOf("d")>-1&&new Set(e._mc.children().map((e=>e?.target))).forEach((e=>{e&&b.trigger(e,t)}))}),0)}i()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>c.add(e)));class g{#n;constructor(e,t=!1){var r;this.#n=new WeakRef(e||{}),(r=this).constructor!==b&&setTimeout((()=>{r.trigger("Init")}),0),function(e){if(!i()||e.constructor===b)return;const t=e.target;if(t instanceof HTMLElement){let r=e;Object.getOwnPropertyNames(r.constructor.prototype).filter((e=>/^on[A-Z]|one[A-Z]/.test(e)&&"function"==typeof r.constructor.prototype[e])).forEach((n=>{let o=/^one/.test(n),i=n.replace(/^on[e]{0,1}/,""),s=i.toLowerCase();o&&Object.defineProperty(e,"on"+i,{configurable:!0,enumerable:!1,writable:!0,value:e=>{r.constructor.prototype[n].bind(r)(e)}}),c.has("on"+s)&&function(e,t,r,n=!1,o=!1){let i={once:!1};n&&(i.capture=n),o&&(i.once=o),e.addEventListener(t,r,i)}(t,s,(e=>{r.trigger(i,e)}),!1,o)}))}}(this)}get target(){return this.#n.deref()}}class m extends g{_mc={};constructor(e){super(e)}static trigger=p;trigger(e,t={},r="l"){let n=this;if(n.constructor===m||n.constructor===b){let i=e instanceof o?new o(e.type,e.data,"l"):new o(e,t,"l"),c="string"!=typeof e?e:new o(e,t,r);return[...Object.getOwnPropertyNames(n)].forEach((e=>{n[e]?.trigger&&"function"==typeof n[e]?.trigger&&n[e]?.trigger(i)})),void(/[ud]/.test(c.bubble)&&f(n.target,c))}if(n instanceof m||n instanceof b){const n=this,i="string"!=typeof e?e:new o(e,t,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{_mc={};constructor(e,t=!1){super(e)}static add=l;static remove=d;static trigger=p;trigger(e,t={},r){m.prototype.trigger.call(this,e,t,r)}parent(e=""){let t=this.target;return u(t)?a(t.parentNode,e,!0):[]}ancestors(e=""){let t=this?.target;return u(t)?a(this.target.parentNode,e,!1):[]}children(e=""){let t=this?.target;if(!u(t))return[];const r=Math.random().toString().replace(".",""),n='[_mc]:not([temp_id="'+r+'"] [_mc] [_mc])';t.setAttribute("temp_id",r);const o=s([...t.querySelectorAll(n)],e);return t.removeAttribute("temp_id"),o}descendants(e=""){let t=this?.target;return u(t)?s([...t.querySelectorAll("*[_mc]")],e):[]}}class h extends HTMLElement{constructor(){super()}connectedCallback(){b.trigger(this,"Connected")}disconnectedCallback(){b.trigger(this,"Disconnected")}adoptedCallback(){b.trigger(this,"Adopted")}attributeChangedCallback(e,t,r){b.trigger(this,"AttributeChanged",{name:e,oldValue:t,newValue:r})}}function y(e,t,r=[]){if(customElements.get(e))return void console.warn(`IGNORED: Custom element "${e}" is already defined!`);if(!e.includes("-"))throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');let n=e.split("-").map((e=>e.charAt(0).toUpperCase()+e.slice(1))).join("");customElements.define(e,function(e,t){return Object.defineProperty(t,"name",{value:e})}(n,class extends h{instance;constructor(e){super();let r=new t(this,e);b.add(this,n,r)}static get observedAttributes(){return r}}))}function w(e){return customElements.get(e)}return t})()));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.elements=t():e.elements=t()}(this,(()=>(()=>{"use strict";var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{defineCE:()=>y,getCE:()=>w});var r=new Function("try {return this===window;}catch(e){ return false;}");let n=new Set;r()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>n.add(e)));class o{#e;#t=!1;#r=!1;constructor(e,t={},r="l"){this.#e=e,this.data=t,this.bubble=r}get type(){return this.#e}get stopped(){return this.#t}get immediateStopped(){return this.#r}stopPropagation(){this.#t=!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(e,t){let r=[];return e.forEach((e=>{if(e&&e instanceof HTMLElement){const n=e?._mc;!t&&n?r=r.concat(Object.values(e._mc)):t&&n&&n[t]&&r.push(n[t])}})),r}function a(e,t,r=!0){let n=[];for(;e;){if(e._mc&&(t?t&&e._mc[t]&&n.push(e._mc[t]):n=n.concat(Object.values(e._mc)),r))return n;e=e.parentNode}return n}function u(e){return e&&e instanceof HTMLElement}function l(e,t,r={}){let n=e;if(!n._mc){let t=b?.debug;b.debug=!1,n._mc=new b(e),b.debug=t,delete n._mc._mc}const o=n._mc;return t.length&&!o[t]&&(o[t]=r,e instanceof HTMLElement&&e.setAttribute("_mc","")),r}function d(e,t){if(!t)return;const r=e?._mc||{};r&&r[t]&&delete r[t],!Object.keys(r).length&&e instanceof HTMLElement&&e.hasAttribute("_mc")&&e.removeAttribute("_mc")}function p(e,t,r={},n="l"){let i=e?._mc;if(!i)return;let c=t instanceof o?new o(t.type,t.data,"l"):new o(t,r,"l"),s="string"!=typeof t?t:new o(t,r,n);[...Object.values(i)].forEach((e=>{e?.trigger&&"function"==typeof e?.trigger&&e?.trigger(c)})),/[ud]/.test(s.bubble)&&f(e,s)}function f(e,t){/[ud]/.test(t.bubble)&&setTimeout((()=>{if(-1===t.bubble.indexOf("l")&&(t.bubble+="l"),t.bubble.indexOf("u")>-1){let r=a(e,"",!0)?.[0].target;r&&b.trigger(r,t)}t.bubble.indexOf("d")>-1&&new Set(e._mc.children().map((e=>e?.target))).forEach((e=>{e&&b.trigger(e,t)}))}),0)}i()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>c.add(e)));class g{#n;constructor(e,t=!1){var r;this.#n=new WeakRef(e||{}),(r=this).constructor!==b&&setTimeout((()=>{r.trigger("Init")}),0),function(e){if(!i()||e.constructor===b)return;const t=e.target;if(t instanceof HTMLElement){let r=e;Object.getOwnPropertyNames(r.constructor.prototype).filter((e=>/^on[A-Z]|one[A-Z]/.test(e)&&"function"==typeof r.constructor.prototype[e])).forEach((n=>{let o=/^one/.test(n),i=n.replace(/^on[e]{0,1}/,""),s=i.toLowerCase();o&&Object.defineProperty(e,"on"+i,{configurable:!0,enumerable:!1,writable:!0,value:e=>{r.constructor.prototype[n].bind(r)(e)}}),c.has("on"+s)&&function(e,t,r,n=!1,o=!1){let i={once:!1};n&&(i.capture=n),o&&(i.once=o),e.addEventListener(t,r,i)}(t,s,(e=>{r.trigger(i,e)}),!1,o)}))}}(this)}get target(){return this.#n.deref()}}class m extends g{_mc={};constructor(e){super(e)}static trigger=p;trigger(e,t={},r="l"){let n=this;if(n.constructor===m||n.constructor===b){let i=e instanceof o?new o(e.type,e.data,"l"):new o(e,t,"l"),c="string"!=typeof e?e:new o(e,t,r);return[...Object.getOwnPropertyNames(n)].forEach((e=>{n[e]?.trigger&&"function"==typeof n[e]?.trigger&&n[e]?.trigger(i)})),void(/[ud]/.test(c.bubble)&&f(n.target,c))}if(n instanceof m||n instanceof b){const n=this,i="string"!=typeof e?e:new o(e,t,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{_mc={};constructor(e,t=!1){super(e)}static add=l;static remove=d;static trigger=p;trigger(e,t={},r){m.prototype.trigger.call(this,e,t,r)}parent(e=""){let t=this.target;return u(t)?a(t.parentNode,e,!0):[]}ancestors(e=""){let t=this?.target;return u(t)?a(this.target.parentNode,e,!1):[]}children(e=""){let t=this?.target;if(!u(t))return[];const r=Math.random().toString().replace(".",""),n='[_mc]:not([temp_id="'+r+'"] [_mc] [_mc])';t.setAttribute("temp_id",r);const o=s([...t.querySelectorAll(n)],e);return t.removeAttribute("temp_id"),o}descendants(e=""){let t=this?.target;return u(t)?s([...t.querySelectorAll("*[_mc]")],e):[]}}new Map;class h extends HTMLElement{constructor(){super()}connectedCallback(){b.trigger(this,"Connected")}disconnectedCallback(){b.trigger(this,"Disconnected")}adoptedCallback(){b.trigger(this,"Adopted")}attributeChangedCallback(e,t,r){b.trigger(this,"AttributeChanged",{name:e,oldValue:t,newValue:r})}}function y(e,t,r=[]){if(customElements.get(e))return void console.warn(`IGNORED: Custom element "${e}" is already defined!`);if(!e.includes("-"))throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');let n=e.split("-").map((e=>e.charAt(0).toUpperCase()+e.slice(1))).join("");customElements.define(e,function(e,t){return Object.defineProperty(t,"name",{value:e})}(n,class extends h{instance;constructor(e){super();let r=new t(this,e);b.add(this,n,r)}static get observedAttributes(){return r}}))}function w(e){return customElements.get(e)}return t})()));
|