@twobirds/microcomponents 0.0.33 → 0.0.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -27
- package/assets/documentation/{MicroComponents.md → MC.md} +17 -17
- package/assets/documentation/comparision.md +0 -0
- package/assets/documentation/elements.md +276 -0
- package/assets/documentation/magic.md +10 -68
- package/dist/MC.d.ts +2 -3
- package/dist/MC.d.ts.map +1 -1
- package/dist/MC.min.js +45 -43
- package/dist/MC.ts +173 -175
- package/dist/autoloader.ts +1 -1
- package/dist/{customElements.d.ts → elements.d.ts} +1 -1
- package/dist/elements.d.ts.map +1 -0
- package/dist/elements.min.js +1 -0
- package/dist/{customElements.ts → elements.ts} +23 -14
- package/dist/helpers.d.ts +6 -4
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.ts +95 -128
- package/dist/microcomponents.d.ts +3 -3
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.min.js +1 -1
- package/dist/microcomponents.ts +3 -3
- package/dist/{customObservables.d.ts → observables.d.ts} +3 -3
- package/dist/observables.d.ts.map +1 -0
- package/dist/observables.min.js +1 -0
- package/dist/observables.ts +354 -0
- package/dist/{customSelectors.d.ts → selectors.d.ts} +1 -1
- package/dist/selectors.d.ts.map +1 -0
- package/dist/selectors.min.js +1 -0
- package/dist/test.ts +133 -157
- package/klemmstein.png +0 -0
- package/package.json +52 -52
- package/public/MC.js +46 -44
- package/public/MC.js.map +1 -1
- package/public/MC.min.js +45 -43
- package/public/assets/search.js +1 -1
- package/public/autoloader.js +1 -1
- package/public/autoloader.js.map +1 -1
- package/public/classes/{DC.html → MC.DC.html} +60 -59
- package/public/classes/{MC.html → MC.MC.html} +41 -35
- package/public/classes/{McBase.html → MC.McBase.html} +28 -20
- package/public/classes/{McEvent.html → MC.McEvent.html} +48 -42
- package/public/documentation/{MicroComponents.md → MC.md} +17 -17
- package/public/documentation/comparision.md +0 -0
- package/public/documentation/elements.md +276 -0
- package/public/documentation/magic.md +10 -68
- package/public/{customElements.js → elements.js} +9 -10
- package/public/elements.js.map +1 -0
- package/public/elements.min.js +1 -0
- package/public/functions/{addListener.html → MC.addListener.html} +18 -12
- package/public/functions/{removeListener.html → MC.removeListener.html} +18 -12
- package/public/functions/elements.defineCE.html +78 -0
- package/public/functions/elements.getCE.html +63 -0
- package/public/functions/formValues.formValues.html +68 -0
- package/public/functions/observables.observe.html +65 -0
- package/public/functions/selectors._d.html +69 -0
- package/public/functions/selectors._t.html +69 -0
- package/public/helpers.js +66 -89
- package/public/helpers.js.map +1 -1
- package/public/index.html +48 -33
- package/public/interfaces/{HTMLMcElement.html → MC.HTMLMcElement.html} +601 -595
- package/public/interfaces/selectors.DSelector.html +1013 -0
- package/public/interfaces/selectors.TSelector.html +712 -0
- package/public/microcomponents.js +3 -3
- package/public/microcomponents.js.map +1 -1
- package/public/microcomponents.min.js +1 -1
- package/public/modules/MC.html +78 -0
- package/public/modules/elements.html +62 -0
- package/public/modules/formValues.html +60 -0
- package/public/modules/observables.html +65 -0
- package/public/modules/selectors.html +76 -0
- package/public/modules.html +11 -23
- package/public/observables.js +255 -0
- package/public/observables.js.map +1 -0
- package/public/observables.min.js +1 -0
- package/public/{customSelectors.js → selectors.js} +1 -1
- package/public/selectors.js.map +1 -0
- package/public/selectors.min.js +1 -0
- package/public/src/MC.ts +376 -0
- package/public/src/McEvent.ts +96 -0
- package/public/src/autoloader.ts +98 -0
- package/{src/customElements.ts → public/src/elements.ts} +23 -14
- package/public/src/formValues.ts +275 -0
- package/public/src/helpers.ts +140 -0
- package/public/src/microcomponents.ts +8 -0
- package/public/src/observables.ts +354 -0
- package/public/src/test.ts +187 -0
- package/public/test.js +99 -115
- package/public/test.js.map +1 -1
- package/public/types/observables.Observable.html +91 -0
- package/public/types/selectors.selection.html +58 -0
- package/public/types/selectors.singleSelector.html +58 -0
- package/src/MC.ts +173 -175
- package/src/autoloader.ts +1 -1
- package/src/elements.ts +76 -0
- package/src/helpers.ts +95 -128
- package/src/microcomponents.ts +3 -3
- package/src/observables.ts +354 -0
- package/src/selectors.ts +1634 -0
- package/src/test.ts +133 -157
- package/typedoc.json +1 -1
- package/webpack.config.js +9 -9
- package/dist/customElements.d.ts.map +0 -1
- package/dist/customElements.min.js +0 -1
- package/dist/customObservables.d.ts.map +0 -1
- package/dist/customObservables.min.js +0 -1
- package/dist/customObservables.ts +0 -348
- package/dist/customSelectors.d.ts.map +0 -1
- package/dist/customSelectors.min.js +0 -1
- package/public/customElements.js.map +0 -1
- package/public/customElements.min.js +0 -1
- package/public/customObservables.js +0 -256
- package/public/customObservables.js.map +0 -1
- package/public/customObservables.min.js +0 -1
- package/public/customSelectors.js.map +0 -1
- package/public/customSelectors.min.js +0 -1
- package/src/customObservables.ts +0 -348
- /package/dist/{customSelectors.ts → selectors.ts} +0 -0
- /package/{src/customSelectors.ts → public/src/selectors.ts} +0 -0
package/dist/MC.ts
CHANGED
|
@@ -1,34 +1,37 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
3
|
import { LooseObject } from './helpers.js';
|
|
4
4
|
import { McEvent } from './McEvent.js';
|
|
5
5
|
|
|
6
|
-
var isBrowser=new Function(
|
|
6
|
+
var isBrowser = new Function(
|
|
7
|
+
'try {return this===window;}catch(e){ return false;}'
|
|
8
|
+
);
|
|
7
9
|
|
|
8
10
|
let nativeEventNames: Set<string> = new Set();
|
|
9
11
|
|
|
10
12
|
// HTMLElement native event names
|
|
11
|
-
if (
|
|
12
|
-
Object.keys(
|
|
13
|
+
if (isBrowser()) {
|
|
14
|
+
Object.keys(HTMLElement.prototype)
|
|
15
|
+
.filter((key) => /^on/.test(key))
|
|
16
|
+
.forEach((eventName) => nativeEventNames.add(eventName));
|
|
13
17
|
}
|
|
14
18
|
|
|
15
|
-
export interface HTMLMcElement extends HTMLElement{
|
|
19
|
+
export interface HTMLMcElement extends HTMLElement {
|
|
16
20
|
_mc: LooseObject;
|
|
17
21
|
}
|
|
18
22
|
|
|
19
23
|
// get all microcomponents from a list of HTMLElements
|
|
20
|
-
function getMicroComponents(
|
|
24
|
+
function getMicroComponents(elements: any[], search: string): any[] {
|
|
21
25
|
let mcValues: any[] = [];
|
|
22
26
|
|
|
23
|
-
elements.forEach(
|
|
27
|
+
elements.forEach((e) => {
|
|
24
28
|
if (e) {
|
|
25
29
|
if (e instanceof HTMLElement) {
|
|
26
|
-
const _mc = (
|
|
27
|
-
if (
|
|
28
|
-
mcValues = mcValues.concat(Object.values((
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
mcValues.push( _mc[search] );
|
|
30
|
+
const _mc = (e as any)?._mc;
|
|
31
|
+
if (!search && _mc) {
|
|
32
|
+
mcValues = mcValues.concat(Object.values((e as any)._mc));
|
|
33
|
+
} else if (search && _mc && _mc[search]) {
|
|
34
|
+
mcValues.push(_mc[search]);
|
|
32
35
|
}
|
|
33
36
|
}
|
|
34
37
|
}
|
|
@@ -37,70 +40,78 @@ function getMicroComponents( elements: any[], search: string): any[] {
|
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
// get all ancestor DOM nodes up to the document root
|
|
40
|
-
function traverseNodes(
|
|
43
|
+
function traverseNodes(
|
|
44
|
+
node: { [key: string]: any },
|
|
45
|
+
search: string,
|
|
46
|
+
firstonly = true
|
|
47
|
+
): any[] {
|
|
41
48
|
let mcValues: any[] = [];
|
|
42
49
|
while (node) {
|
|
43
50
|
if (node._mc) {
|
|
44
51
|
if (!search) {
|
|
45
52
|
mcValues = mcValues.concat(Object.values(node._mc));
|
|
46
|
-
}
|
|
47
|
-
else if (search && node._mc[search]) {
|
|
53
|
+
} else if (search && node._mc[search]) {
|
|
48
54
|
mcValues.push(node._mc[search]);
|
|
49
55
|
}
|
|
50
|
-
if (firstonly)
|
|
51
|
-
return mcValues;
|
|
56
|
+
if (firstonly) return mcValues;
|
|
52
57
|
}
|
|
53
58
|
node = node.parentNode;
|
|
54
59
|
}
|
|
55
60
|
return mcValues;
|
|
56
61
|
}
|
|
57
62
|
|
|
58
|
-
function isDOM(element: { [key:string]: any }
|
|
63
|
+
function isDOM(element: { [key: string]: any }) {
|
|
59
64
|
return element && element instanceof HTMLElement;
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
function addListener(
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
function addListener(
|
|
68
|
+
domNode: Node,
|
|
69
|
+
eventName: string,
|
|
70
|
+
handler: EventListenerOrEventListenerObject | null,
|
|
71
|
+
capture: boolean = false,
|
|
72
|
+
once: boolean = false
|
|
73
|
+
) {
|
|
74
|
+
let options: EventListenerOptions & { once: boolean } = { once: false };
|
|
75
|
+
if (capture) {
|
|
68
76
|
options.capture = capture;
|
|
69
77
|
}
|
|
70
78
|
|
|
71
|
-
if (
|
|
79
|
+
if (once) {
|
|
72
80
|
options.once = once;
|
|
73
81
|
}
|
|
74
82
|
|
|
75
83
|
//console.log( 'addEventListener', eventName, ( handler as Function ).name, options );
|
|
76
|
-
domNode.addEventListener(
|
|
84
|
+
domNode.addEventListener(eventName, handler, options);
|
|
77
85
|
}
|
|
78
86
|
|
|
79
|
-
function removeListener(
|
|
80
|
-
domNode
|
|
87
|
+
function removeListener(
|
|
88
|
+
domNode: Node,
|
|
89
|
+
eventName: string,
|
|
90
|
+
handler: EventListenerOrEventListenerObject | null
|
|
91
|
+
) {
|
|
92
|
+
domNode.removeEventListener(eventName, handler);
|
|
81
93
|
}
|
|
82
94
|
|
|
83
95
|
// add a microcomponent to a target DOM node
|
|
84
|
-
function add(
|
|
85
|
-
|
|
96
|
+
function add(target: HTMLElement, key: string, element = {}): any {
|
|
86
97
|
let node = target as HTMLMcElement;
|
|
87
98
|
|
|
88
99
|
// create the _mc property if necessary
|
|
89
100
|
if (!node._mc) {
|
|
90
101
|
let debug = (DC as any)?.debug;
|
|
91
|
-
(
|
|
92
|
-
node._mc = new (
|
|
93
|
-
(
|
|
102
|
+
(DC as any).debug = false;
|
|
103
|
+
node._mc = new (DC as any)(target);
|
|
104
|
+
(DC as any).debug = debug;
|
|
94
105
|
delete node._mc._mc; // NOT NEEDED on the top level _mc property of a DOM HTMLElement
|
|
95
106
|
}
|
|
96
107
|
|
|
97
108
|
const _mc = node._mc;
|
|
98
109
|
|
|
99
110
|
// add the element with the given property key to the _mc property
|
|
100
|
-
if (
|
|
111
|
+
if (key.length && !_mc[key]) {
|
|
101
112
|
_mc[key] = element;
|
|
102
113
|
// add attribute if HTMLElement
|
|
103
|
-
if (
|
|
114
|
+
if (target instanceof HTMLElement) {
|
|
104
115
|
target.setAttribute('_mc', '');
|
|
105
116
|
}
|
|
106
117
|
}
|
|
@@ -108,239 +119,227 @@ function add( target: HTMLElement, key: string, element = {} ): any {
|
|
|
108
119
|
}
|
|
109
120
|
|
|
110
121
|
// remove a micro component from the DOM
|
|
111
|
-
function remove(
|
|
112
|
-
if (
|
|
122
|
+
function remove(target: HTMLMcElement, key: string) {
|
|
123
|
+
if (!key) return; // silent fail
|
|
113
124
|
|
|
114
125
|
const _mc = target?._mc || {};
|
|
115
126
|
|
|
116
127
|
// delete the property from the HTMLElement _mc collection
|
|
117
|
-
if (
|
|
128
|
+
if (_mc && _mc[key]) {
|
|
118
129
|
delete _mc[key];
|
|
119
130
|
}
|
|
120
131
|
|
|
121
132
|
// remove the _mc attribute if the DOM collection is empty
|
|
122
|
-
if (
|
|
133
|
+
if (
|
|
134
|
+
!Object.keys(_mc).length &&
|
|
135
|
+
target instanceof HTMLElement &&
|
|
136
|
+
target.hasAttribute('_mc')
|
|
137
|
+
) {
|
|
123
138
|
target.removeAttribute('_mc');
|
|
124
139
|
}
|
|
125
140
|
return;
|
|
126
141
|
}
|
|
127
142
|
|
|
128
143
|
// trigger an event
|
|
129
|
-
function trigger(
|
|
144
|
+
function trigger(target: unknown, ev: McEvent | string, data: any = {}) {
|
|
130
145
|
let _mc = (target as any)?._mc;
|
|
131
|
-
if (
|
|
132
|
-
[
|
|
146
|
+
if (!_mc) return;
|
|
147
|
+
[...Object.values(_mc)].forEach((mc: any) => {
|
|
133
148
|
//console.log( 'mc [', mc.constructor.name, ']', mc.constructor.prototype?.oneInit );
|
|
134
|
-
if (
|
|
135
|
-
(mc as any)?.trigger(
|
|
149
|
+
if ((mc as any)?.trigger && typeof (mc as any)?.trigger === 'function') {
|
|
150
|
+
(mc as any)?.trigger(ev, data);
|
|
136
151
|
}
|
|
137
152
|
});
|
|
138
153
|
return;
|
|
139
154
|
}
|
|
140
155
|
|
|
141
|
-
// run the "init" lifecycle event on the
|
|
142
|
-
function init(
|
|
143
|
-
if (
|
|
144
|
-
|
|
145
|
-
setTimeout(
|
|
146
|
-
()
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
, 0
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
, 0
|
|
166
|
-
);
|
|
167
|
-
|
|
156
|
+
// run the "init" lifecycle event on the complete _mc structure
|
|
157
|
+
function init(element: MC | DC) {
|
|
158
|
+
if (element.constructor === DC) return;
|
|
159
|
+
|
|
160
|
+
setTimeout(() => {
|
|
161
|
+
element.trigger('Init');
|
|
162
|
+
/*
|
|
163
|
+
let keys = Object.keys( ( element as any)?._mc );
|
|
164
|
+
if ( keys.length ){
|
|
165
|
+
//console.log( 'inner Inits on [', mc.constructor.name, ']', keys );
|
|
166
|
+
keys?.forEach( (key: string) => {
|
|
167
|
+
let mc = ( element as any)?._mc?.[key];
|
|
168
|
+
//console.log( key, typeof mc?.oneInit, mc );
|
|
169
|
+
if ( ( mc as any )['oneInit'] && typeof ( mc as any ).oneInit == 'function' ){
|
|
170
|
+
( mc as any ).oneInit( new McEvent('Init') );
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
*/
|
|
175
|
+
}, 0);
|
|
168
176
|
}
|
|
169
177
|
|
|
170
|
-
function autoAttachListeners(
|
|
171
|
-
if (
|
|
178
|
+
function autoAttachListeners(element: MC | DC) {
|
|
179
|
+
if (!isBrowser() || element.constructor === DC) return;
|
|
172
180
|
|
|
173
181
|
const target = element.target;
|
|
174
182
|
|
|
175
|
-
if (
|
|
183
|
+
if (target instanceof HTMLElement) {
|
|
176
184
|
let that: LooseObject = element;
|
|
177
|
-
Object
|
|
178
|
-
.
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
185
|
+
Object.getOwnPropertyNames(that.constructor.prototype)
|
|
186
|
+
.filter(
|
|
187
|
+
(key) =>
|
|
188
|
+
/^on[A-Z]|one[A-Z]/.test(key) &&
|
|
189
|
+
typeof that.constructor.prototype[key] === 'function'
|
|
190
|
+
)
|
|
191
|
+
.forEach((key) => {
|
|
192
|
+
let once = /^one/.test(key),
|
|
193
|
+
withoutOnOne = key.replace(/^on[e]{0,1}/, ''), // preserves upper/lowercase for non-native events
|
|
184
194
|
nativeEventName = withoutOnOne.toLowerCase(),
|
|
185
|
-
listener = (
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (
|
|
195
|
+
listener = (ev: McEvent | Event | CustomEvent) => {
|
|
196
|
+
that.trigger(withoutOnOne, ev);
|
|
197
|
+
};
|
|
198
|
+
if (once) {
|
|
189
199
|
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
190
|
-
Object.defineProperty(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
( that.constructor.prototype[key] ).bind(that)(ev);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
);
|
|
200
|
+
Object.defineProperty(element, 'on' + withoutOnOne, {
|
|
201
|
+
configurable: true,
|
|
202
|
+
enumerable: false,
|
|
203
|
+
writable: true,
|
|
204
|
+
value: (ev: any) => {
|
|
205
|
+
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
206
|
+
that.constructor.prototype[key].bind(that)(ev);
|
|
207
|
+
},
|
|
208
|
+
});
|
|
203
209
|
}
|
|
204
210
|
|
|
205
|
-
if (
|
|
211
|
+
if (nativeEventNames.has('on' + nativeEventName)) {
|
|
212
|
+
// native event defined in HTMLElement
|
|
206
213
|
// console.log('added as native listener', target, nativeEventName );
|
|
207
|
-
addListener(
|
|
214
|
+
addListener(target, nativeEventName, listener, false, once);
|
|
208
215
|
}
|
|
209
216
|
});
|
|
210
217
|
}
|
|
211
218
|
}
|
|
212
219
|
|
|
213
220
|
class McBase {
|
|
214
|
-
|
|
215
221
|
#target: WeakRef<any>; // avoid circular reference
|
|
216
222
|
|
|
217
|
-
constructor(
|
|
223
|
+
constructor(target?: { [key: string]: any }, debug: boolean = false) {
|
|
218
224
|
let that: unknown = this,
|
|
219
|
-
element
|
|
225
|
+
element = that as MC | DC;
|
|
220
226
|
|
|
221
|
-
this.#target = new WeakRef(
|
|
227
|
+
this.#target = new WeakRef(target || {});
|
|
222
228
|
|
|
223
|
-
init(
|
|
229
|
+
init(element); // async lifecycle event
|
|
224
230
|
|
|
225
|
-
autoAttachListeners(
|
|
231
|
+
autoAttachListeners(element);
|
|
226
232
|
}
|
|
227
233
|
|
|
228
234
|
get target(): any {
|
|
229
235
|
return this.#target.deref();
|
|
230
236
|
}
|
|
231
|
-
|
|
232
237
|
}
|
|
233
238
|
|
|
234
239
|
class MC extends McBase {
|
|
235
|
-
|
|
236
|
-
_mc: { [key:string]: any } = {};
|
|
240
|
+
_mc: { [key: string]: any } = {};
|
|
237
241
|
|
|
238
242
|
constructor(target?: any) {
|
|
239
243
|
super(target);
|
|
240
244
|
}
|
|
241
245
|
|
|
242
|
-
|
|
246
|
+
static trigger = trigger;
|
|
243
247
|
|
|
244
|
-
trigger(
|
|
248
|
+
trigger(ev: McEvent | string, data: any = {}, bubble?: string) {
|
|
245
249
|
let that = this;
|
|
246
250
|
|
|
247
251
|
//console.log( that, '.trigger(', ev, data, bubble, ')' );
|
|
248
252
|
bubble = bubble || 'l';
|
|
249
253
|
|
|
250
254
|
// run all triggers
|
|
251
|
-
if (
|
|
252
|
-
[
|
|
253
|
-
if (
|
|
254
|
-
(that as any)[mc]?.trigger
|
|
255
|
+
if (that.constructor === MC || that.constructor === DC) {
|
|
256
|
+
[...Object.getOwnPropertyNames(that)].forEach((mc) => {
|
|
257
|
+
if (
|
|
258
|
+
(that as any)[mc]?.trigger &&
|
|
259
|
+
typeof (that as any)[mc]?.trigger === 'function'
|
|
260
|
+
) {
|
|
261
|
+
(that as any)[mc]?.trigger(ev, data, bubble);
|
|
255
262
|
}
|
|
256
263
|
});
|
|
257
264
|
return; // must return here!
|
|
258
265
|
}
|
|
259
266
|
|
|
260
267
|
// only this instance
|
|
261
|
-
if (
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
268
|
+
if ((that as any) instanceof MC || (that as any) instanceof DC) {
|
|
269
|
+
const that = this,
|
|
270
|
+
mcEvent: McEvent =
|
|
271
|
+
typeof ev !== 'string'
|
|
272
|
+
? (ev as McEvent)
|
|
273
|
+
: new McEvent(ev, data, bubble),
|
|
274
|
+
handlerName =
|
|
275
|
+
'on' + mcEvent.type[0].toUpperCase() + mcEvent.type.slice(1);
|
|
269
276
|
// console.log('trigger', handlerName, mcEvent, data, bubble );
|
|
270
277
|
|
|
271
|
-
if (
|
|
272
|
-
if (
|
|
273
|
-
&&
|
|
274
|
-
|
|
278
|
+
if (mcEvent.bubble.indexOf('l') > -1) {
|
|
279
|
+
if (
|
|
280
|
+
!mcEvent.immediateStopped &&
|
|
281
|
+
typeof (that as any)[handlerName] === 'function'
|
|
282
|
+
) {
|
|
275
283
|
// console.log( 'execute handler:', handlerName );
|
|
276
|
-
(
|
|
277
|
-
if (
|
|
278
|
-
|
|
284
|
+
(that as any)[handlerName](mcEvent);
|
|
285
|
+
if ((that as any).hasOwnProperty(handlerName)) {
|
|
286
|
+
// it is a once handler
|
|
287
|
+
delete (that as any)[handlerName]; // remove after first execution
|
|
279
288
|
}
|
|
280
289
|
}
|
|
281
290
|
}
|
|
282
291
|
|
|
283
292
|
// if event stopped or local only, handling is cancelled
|
|
284
|
-
if (
|
|
293
|
+
if (mcEvent.stopped || mcEvent.bubble === 'l') {
|
|
285
294
|
return that;
|
|
286
295
|
}
|
|
287
296
|
|
|
288
297
|
// at this point, either 'u' or 'd' bubbling should be indicated
|
|
289
298
|
// mcEvent bubbling is asynchronous, and the order of target MC execution in case of multiple matches is not guaranteed!
|
|
290
|
-
if (
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
(that as any)
|
|
301
|
-
.parent()
|
|
302
|
-
.forEach( (mc: any) => {
|
|
303
|
-
mc instanceof MC ? ( mc as any ).trigger(mcEvent) : null;
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// bubble down
|
|
308
|
-
if ( mcEvent.bubble.indexOf('d') > -1 ){
|
|
309
|
-
(that as any)
|
|
310
|
-
.children()
|
|
311
|
-
.forEach( (mc: any) => {
|
|
312
|
-
mc instanceof MC ? ( mc as any ).trigger(mcEvent) : null;
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
},
|
|
317
|
-
0
|
|
318
|
-
);
|
|
319
|
-
}
|
|
299
|
+
if (
|
|
300
|
+
isBrowser() &&
|
|
301
|
+
/[ud]/.test(mcEvent.bubble) &&
|
|
302
|
+
(that as any) instanceof MC
|
|
303
|
+
) {
|
|
304
|
+
setTimeout(() => {
|
|
305
|
+
// bubbling must include 'l' for local, or else no handlers are executed'
|
|
306
|
+
if (mcEvent.bubble.indexOf('l') === -1) {
|
|
307
|
+
mcEvent.bubble += 'l';
|
|
308
|
+
}
|
|
320
309
|
|
|
321
|
-
|
|
310
|
+
// bubble up towards root
|
|
311
|
+
if (mcEvent.bubble.indexOf('u') > -1) {
|
|
312
|
+
(that as any).parent().forEach((mc: any) => {
|
|
313
|
+
mc instanceof MC ? (mc as any).trigger(mcEvent) : null;
|
|
314
|
+
});
|
|
315
|
+
}
|
|
322
316
|
|
|
317
|
+
// bubble down
|
|
318
|
+
if (mcEvent.bubble.indexOf('d') > -1) {
|
|
319
|
+
(that as any).children().forEach((mc: any) => {
|
|
320
|
+
mc instanceof MC ? (mc as any).trigger(mcEvent) : null;
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}, 0);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
323
326
|
}
|
|
324
|
-
|
|
325
|
-
};
|
|
327
|
+
}
|
|
326
328
|
|
|
327
329
|
class DC extends McBase {
|
|
330
|
+
_mc: { [key: string]: any } = {};
|
|
328
331
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
constructor(target: HTMLElement ) {
|
|
332
|
+
constructor(target: HTMLElement, debug: boolean = false) {
|
|
332
333
|
super(target);
|
|
333
334
|
}
|
|
334
335
|
|
|
335
|
-
static debug: boolean = false;
|
|
336
|
-
|
|
337
336
|
static add: Function = add;
|
|
338
337
|
static remove: Function = remove;
|
|
339
338
|
static trigger: Function = trigger;
|
|
340
339
|
|
|
341
|
-
trigger(
|
|
342
|
-
MC.prototype.trigger.call(
|
|
343
|
-
}
|
|
340
|
+
trigger(ev: McEvent | string, data: any = {}, bubble?: string) {
|
|
341
|
+
MC.prototype.trigger.call(this, ev, data, bubble);
|
|
342
|
+
}
|
|
344
343
|
|
|
345
344
|
parent(search: string = ''): any[] {
|
|
346
345
|
let target: HTMLElement = (this as any).target;
|
|
@@ -372,7 +371,6 @@ class DC extends McBase {
|
|
|
372
371
|
const myDomElements = target.querySelectorAll('*[_mc]');
|
|
373
372
|
return getMicroComponents([...myDomElements], search);
|
|
374
373
|
}
|
|
375
|
-
|
|
376
|
-
};
|
|
374
|
+
}
|
|
377
375
|
|
|
378
376
|
export { addListener, removeListener, McBase, MC, DC, McEvent };
|
package/dist/autoloader.ts
CHANGED
|
@@ -2,4 +2,4 @@ export declare function defineCE(tagName: string, DefinitionClass: {
|
|
|
2
2
|
new (...args: any[]): any;
|
|
3
3
|
}, observedAttributes?: string[]): void;
|
|
4
4
|
export declare function getCE(tagName: string): CustomElementConstructor | undefined;
|
|
5
|
-
//# sourceMappingURL=
|
|
5
|
+
//# sourceMappingURL=elements.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elements.d.ts","sourceRoot":"","sources":["../src/elements.ts"],"names":[],"mappings":"AAkCA,wBAAgB,QAAQ,CACvB,OAAO,EAAE,MAAM,EACf,eAAe,EAAE;IAAE,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,GAAG,CAAA;CAAE,EAC9C,kBAAkB,GAAE,MAAM,EAAO,QAkCjC;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,wBAAwB,GAAG,SAAS,CAE3E"}
|
|
@@ -0,0 +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:()=>h,getCE:()=>y});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=g?.debug;g.debug=!1,n._mc=new g(e),g.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={}){let n=e?._mc;n&&[...Object.values(n)].forEach((e=>{e?.trigger&&"function"==typeof e?.trigger&&e?.trigger(t,r)}))}i()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>c.add(e)));class f{#n;constructor(e,t=!1){var r;this.#n=new WeakRef(e||{}),(r=this).constructor!==g&&setTimeout((()=>{r.trigger("Init")}),0),function(e){if(!i()||e.constructor===g)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 f{_mc={};constructor(e){super(e)}static trigger=p;trigger(e,t={},r){let n=this;if(r=r||"l",n.constructor!==m&&n.constructor!==g){if(n instanceof m||n instanceof g){const n=this,c="string"!=typeof e?e:new o(e,t,r),s="on"+c.type[0].toUpperCase()+c.type.slice(1);if(c.bubble.indexOf("l")>-1&&(c.immediateStopped||"function"!=typeof n[s]||(n[s](c),n.hasOwnProperty(s)&&delete n[s])),c.stopped||"l"===c.bubble)return n;i()&&/[ud]/.test(c.bubble)&&n instanceof m&&setTimeout((()=>{-1===c.bubble.indexOf("l")&&(c.bubble+="l"),c.bubble.indexOf("u")>-1&&n.parent().forEach((e=>{e instanceof m&&e.trigger(c)})),c.bubble.indexOf("d")>-1&&n.children().forEach((e=>{e instanceof m&&e.trigger(c)}))}),0)}}else[...Object.getOwnPropertyNames(n)].forEach((o=>{n[o]?.trigger&&"function"==typeof n[o]?.trigger&&n[o]?.trigger(e,t,r)}))}}class g extends f{_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 b extends HTMLElement{constructor(){super()}connectedCallback(){g.trigger(this,"Connected")}disconnectedCallback(){g.trigger(this,"Disconnected")}adoptedCallback(){g.trigger(this,"Adopted")}attributeChangedCallback(e,t,r){g.trigger(this,"AttributeChanged",{name:e,oldValue:t,newValue:r})}}function h(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 b{instance;constructor(e){super();let r=new t(this,e);g.add(this,n,r)}static get observedAttributes(){return r}}))}function y(e){return customElements.get(e)}return t})()));
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
import { DC } from
|
|
4
|
-
import { kebabToPascal } from
|
|
3
|
+
import { DC } from "./MC.js";
|
|
4
|
+
import { kebabToPascal } from "./helpers.js";
|
|
5
5
|
|
|
6
6
|
class CeBaseClass extends HTMLElement {
|
|
7
7
|
constructor() {
|
|
@@ -9,34 +9,43 @@ class CeBaseClass extends HTMLElement {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
connectedCallback() {
|
|
12
|
-
(DC as any).trigger(this,
|
|
12
|
+
(DC as any).trigger(this, "Connected");
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
disconnectedCallback() {
|
|
16
|
-
(DC as any).trigger(this,
|
|
16
|
+
(DC as any).trigger(this, "Disconnected");
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
adoptedCallback() {
|
|
20
|
-
(DC as any).trigger(this,
|
|
20
|
+
(DC as any).trigger(this, "Adopted");
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
attributeChangedCallback(name: string, oldValue: any, newValue: any) {
|
|
24
|
-
(DC as any).trigger(this,
|
|
24
|
+
(DC as any).trigger(this, "AttributeChanged", { name, oldValue, newValue });
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
function createNamedClass(
|
|
29
|
-
|
|
28
|
+
function createNamedClass(
|
|
29
|
+
name: string,
|
|
30
|
+
ClassBody: { new (...args: any[]): any }
|
|
31
|
+
) {
|
|
32
|
+
return Object.defineProperty(ClassBody, "name", { value: name });
|
|
30
33
|
}
|
|
31
34
|
|
|
32
|
-
export function defineCE(
|
|
35
|
+
export function defineCE(
|
|
36
|
+
tagName: string,
|
|
37
|
+
DefinitionClass: { new (...args: any[]): any },
|
|
38
|
+
observedAttributes: string[] = []
|
|
39
|
+
) {
|
|
33
40
|
if (customElements.get(tagName)) {
|
|
34
41
|
console.warn(`IGNORED: Custom element "${tagName}" is already defined!`);
|
|
35
42
|
return;
|
|
36
43
|
}
|
|
37
44
|
|
|
38
|
-
if (!tagName.includes(
|
|
39
|
-
throw new Error(
|
|
45
|
+
if (!tagName.includes("-")) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
'createCustomElement: tagName must contain at least one hyphen ("-").'
|
|
48
|
+
);
|
|
40
49
|
}
|
|
41
50
|
|
|
42
51
|
let name = kebabToPascal(tagName);
|
|
@@ -48,10 +57,10 @@ export function defineCE( tagName: string, DefinitionClass: { new (...args: any[
|
|
|
48
57
|
class extends CeBaseClass {
|
|
49
58
|
instance: any;
|
|
50
59
|
|
|
51
|
-
constructor(
|
|
60
|
+
constructor(data?: any) {
|
|
52
61
|
super();
|
|
53
62
|
let instance = new DefinitionClass(this, data);
|
|
54
|
-
(
|
|
63
|
+
(DC as any).add(this, name, instance);
|
|
55
64
|
}
|
|
56
65
|
|
|
57
66
|
static get observedAttributes() {
|
|
@@ -60,7 +69,7 @@ export function defineCE( tagName: string, DefinitionClass: { new (...args: any[
|
|
|
60
69
|
}
|
|
61
70
|
)
|
|
62
71
|
);
|
|
63
|
-
}
|
|
72
|
+
}
|
|
64
73
|
|
|
65
74
|
export function getCE(tagName: string): CustomElementConstructor | undefined {
|
|
66
75
|
return customElements.get(tagName);
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
export declare function isObject(value: any): boolean;
|
|
2
1
|
export interface LooseObject {
|
|
3
2
|
[key: string | symbol]: any;
|
|
4
3
|
}
|
|
4
|
+
export declare function isNativeObject(value: any): boolean;
|
|
5
|
+
export declare function sortObject(o: LooseObject): LooseObject;
|
|
6
|
+
export declare function flattenObject(o: LooseObject, doSort?: boolean): LooseObject;
|
|
7
|
+
export declare function deepEqual(o1: LooseObject, o2: LooseObject): boolean;
|
|
8
|
+
export declare function nameSpace(ns: string | string[], obj?: LooseObject): any;
|
|
9
|
+
export declare function parse(what: string | Object | any[], parseThis: Object): any;
|
|
5
10
|
export declare function kebabToPascal(str: string): string;
|
|
6
11
|
export declare function debounce(func: Function, milliseconds: number): Function;
|
|
7
|
-
export declare function deepEqual(o1: any, o2: any): boolean;
|
|
8
|
-
export declare function nameSpace(ns: string, obj: LooseObject, val?: any, orig?: any): any;
|
|
9
|
-
export declare function parse(what: string | Object | any[], parseThis: Object): any;
|
|
10
12
|
export declare function htmlToElements(html: string): HTMLElement[];
|
|
11
13
|
//# sourceMappingURL=helpers.d.ts.map
|