@twobirds/microcomponents 0.0.21 → 0.0.23
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/.gitlab-ci.yml +1 -0
- package/README.md +31 -12
- package/assets/documentation/MicroComponents.md +228 -0
- package/assets/my-tag.css +4 -0
- package/assets/test.html +5 -1
- package/dist/MC.d.ts +32 -0
- package/dist/MC.d.ts.map +1 -0
- package/dist/MC.min.js +414 -0
- package/dist/MC.ts +350 -0
- package/dist/McEvent.d.ts +15 -0
- package/dist/McEvent.d.ts.map +1 -0
- package/dist/McEvent.ts +96 -0
- package/dist/autoloader.d.ts +2 -0
- package/dist/autoloader.d.ts.map +1 -0
- package/dist/autoloader.ts +98 -0
- package/dist/customElements.d.ts +5 -0
- package/dist/customElements.d.ts.map +1 -0
- package/dist/customElements.min.js +1 -0
- package/dist/customElements.ts +67 -0
- package/dist/customObservables.d.ts +3 -0
- package/dist/customObservables.d.ts.map +1 -0
- package/dist/customObservables.min.js +1 -0
- package/dist/customObservables.ts +340 -0
- package/dist/customSelectors.d.ts +108 -0
- package/dist/customSelectors.d.ts.map +1 -0
- package/dist/customSelectors.min.js +1 -0
- package/dist/customSelectors.ts +1634 -0
- package/dist/formValues.d.ts +4 -0
- package/dist/formValues.d.ts.map +1 -0
- package/dist/formValues.ts +275 -0
- package/dist/formvValues.min.js +1 -0
- package/dist/helpers.d.ts +11 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.ts +173 -0
- package/dist/microcomponents.d.ts +7 -25
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.es3.d.ts +1 -20
- package/dist/microcomponents.es3.d.ts.map +1 -1
- package/dist/microcomponents.min.js +1 -38
- package/dist/microcomponents.ts +8 -216
- package/dist/test.d.ts +2 -0
- package/dist/test.d.ts.map +1 -0
- package/package.json +11 -7
- package/public/MC.js +273 -0
- package/public/MC.min.js +414 -0
- package/public/MCbundle.js +8 -0
- package/public/MCbundle.min.js +1 -0
- package/public/McEvent.js +74 -0
- package/public/MicroComponents.svg +15 -0
- package/public/assets/search.js +1 -1
- package/public/autoloader.js +57 -0
- package/public/classes/MC.DC.html +247 -0
- package/public/classes/MC.MC.html +150 -0
- package/public/classes/MC.McBase.html +123 -0
- package/public/classes/MCbundle.McEvent.html +184 -0
- package/public/customElements.js +48 -0
- package/public/customElements.min.js +1 -0
- package/public/customObservables.js +255 -0
- package/public/customObservables.min.js +1 -0
- package/public/customSelectors.js +1088 -0
- package/public/customSelectors.min.js +1 -0
- package/public/documentation/MicroComponents.md +228 -0
- package/public/formValues.js +211 -0
- package/public/formvValues.min.js +1 -0
- package/public/functions/MC.addListener.html +73 -0
- package/public/functions/MC.removeListener.html +69 -0
- package/public/functions/MCbundle._d.html +85 -0
- package/public/functions/MCbundle._t.html +85 -0
- package/public/functions/MCbundle.debounce.html +85 -0
- package/public/functions/MCbundle.deepEqual.html +85 -0
- package/public/functions/MCbundle.defineCE.html +98 -0
- package/public/functions/MCbundle.formValues.html +89 -0
- package/public/functions/MCbundle.getCE.html +83 -0
- package/public/functions/MCbundle.htmlToElements.html +83 -0
- package/public/functions/MCbundle.isObject.html +83 -0
- package/public/functions/MCbundle.kebabToPascal.html +83 -0
- package/public/functions/MCbundle.makeCO.html +85 -0
- package/public/functions/MCbundle.nameSpace.html +89 -0
- package/public/functions/MCbundle.parse.html +85 -0
- package/public/helpers.js +128 -0
- package/public/index.html +22 -13
- package/public/interfaces/MC.HTMLMcElement.html +3605 -0
- package/public/interfaces/MCbundle.DSelector.html +1010 -0
- package/public/interfaces/MCbundle.LooseObject.html +80 -0
- package/public/interfaces/MCbundle.TSelector.html +709 -0
- package/public/microcomponents.es3.js +348 -0
- package/public/microcomponents.js +8 -176
- package/public/microcomponents.min.js +1 -0
- package/public/modules/MC.html +84 -0
- package/public/modules/MCbundle.html +133 -0
- package/public/modules.html +4 -4
- package/public/my-tag.css +4 -0
- package/public/test.html +5 -1
- package/public/test.js +100 -202
- package/public/types/MCbundle.selection.html +74 -0
- package/public/types/MCbundle.singleSelector.html +74 -0
- package/src/MC.ts +350 -0
- package/src/McEvent.ts +96 -0
- package/src/autoloader.ts +98 -0
- package/src/customElements.ts +67 -0
- package/src/customObservables.ts +340 -0
- package/src/customSelectors.ts +1634 -0
- package/src/formValues.ts +275 -0
- package/src/helpers.ts +173 -0
- package/src/microcomponents.es3.js +353 -82
- package/src/microcomponents.ts +8 -216
- package/src/test.js +137 -218
- package/tsconfig.json +2 -2
- package/typedoc.json +1 -1
- package/webpack.config.js +233 -0
- package/dist/microcomponents.es3.js +0 -96
- package/dist/microcomponents.es3.min.js +0 -15
- package/dist/microcomponents.js +0 -176
- package/public/classes/microcomponents.MC.html +0 -313
- package/public/classes/microcomponents_es3.MC.html +0 -336
- package/public/functions/microcomponents_es3.__isEmpty.html +0 -60
- package/public/modules/microcomponents.html +0 -57
- package/public/modules/microcomponents_es3.html +0 -60
package/dist/microcomponents.ts
CHANGED
|
@@ -1,216 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
let mcValues: any[] = [];
|
|
10
|
-
|
|
11
|
-
elements.forEach( e => {
|
|
12
|
-
if (e) {
|
|
13
|
-
if (e instanceof HTMLElement) {
|
|
14
|
-
const _mc = ( e as any )?._mc;
|
|
15
|
-
if ( !search && _mc ) {
|
|
16
|
-
mcValues = mcValues.concat(Object.values(( e as any )._mc));
|
|
17
|
-
}
|
|
18
|
-
else if ( search && _mc && _mc[search] ) {
|
|
19
|
-
mcValues.push( _mc[search] );
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
});
|
|
24
|
-
return mcValues;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// get all ancestor DOM nodes up to the document
|
|
28
|
-
function traverseNodes(node: { [key:string]: any }, search: string, firstonly = true): any[] {
|
|
29
|
-
let mcValues: any[] = [];
|
|
30
|
-
while (node) {
|
|
31
|
-
if (node._mc) {
|
|
32
|
-
if (!search) {
|
|
33
|
-
mcValues = mcValues.concat(Object.values(node._mc));
|
|
34
|
-
}
|
|
35
|
-
else if (search && node._mc[search]) {
|
|
36
|
-
mcValues.push(node._mc[search]);
|
|
37
|
-
}
|
|
38
|
-
if (firstonly)
|
|
39
|
-
return mcValues;
|
|
40
|
-
}
|
|
41
|
-
node = node.parentNode;
|
|
42
|
-
}
|
|
43
|
-
return mcValues;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function isDOM(element: { [key:string]: any } ) {
|
|
47
|
-
return element && element instanceof HTMLElement;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
type HTMLElementMC = HTMLElement & { _mc: { [key:string]: any } }
|
|
51
|
-
|
|
52
|
-
export class MC {
|
|
53
|
-
|
|
54
|
-
#target: { [key:string]: any } = {};
|
|
55
|
-
|
|
56
|
-
#_mc: { [key:string]: any } = {};
|
|
57
|
-
|
|
58
|
-
static debug: boolean = false;
|
|
59
|
-
|
|
60
|
-
constructor( target: { [key:string]: any } ) {
|
|
61
|
-
this.#target = target;
|
|
62
|
-
this.#_mc = {};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
get _mc(): undefined | any {
|
|
66
|
-
if ( !MC.debug ) return undefined;
|
|
67
|
-
return this.#_mc;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
get target(): any {
|
|
71
|
-
return this.#target;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// return all top-level micro components from an HTMLElement, or the searched one
|
|
75
|
-
static siblings( target: HTMLElementMC, search: string = '' ): any[] {
|
|
76
|
-
const mc = target as any;
|
|
77
|
-
|
|
78
|
-
if (!mc._mc) {
|
|
79
|
-
return [];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const _mc = mc._mc;
|
|
83
|
-
|
|
84
|
-
if ( search.length ){
|
|
85
|
-
return _mc?.[search] || [];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return [ ...Object.values(_mc) ];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// add a microcomponent to a target DOM node
|
|
92
|
-
static add( target: HTMLElement, key: string, element = {} ): any {
|
|
93
|
-
|
|
94
|
-
let node = target as HTMLElementMC;
|
|
95
|
-
|
|
96
|
-
// create the _mc property if necessary
|
|
97
|
-
if (!node._mc) {
|
|
98
|
-
node._mc = new MC( target );
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const _mc = node._mc;
|
|
102
|
-
|
|
103
|
-
// add the element with the given property key to the _mc property
|
|
104
|
-
if ( key.length && !_mc[key]) {
|
|
105
|
-
_mc[key] = element;
|
|
106
|
-
// add attribute if HTMLElement
|
|
107
|
-
if ( target instanceof HTMLElement ) {
|
|
108
|
-
target.setAttribute('_mc', '');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return element;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// remove a micro component from the DOM
|
|
115
|
-
static remove( target: HTMLElementMC, key: string) {
|
|
116
|
-
if ( !key ) return; // silent fail
|
|
117
|
-
|
|
118
|
-
const _mc = target?._mc || {};
|
|
119
|
-
|
|
120
|
-
// delete the property from the HTMLElement _mc collection
|
|
121
|
-
if ( _mc && _mc[key] ) {
|
|
122
|
-
delete _mc[key];
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// remove the _mc attribute if the DOM collection is empty
|
|
126
|
-
if ( !Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('_mc') ) {
|
|
127
|
-
target.removeAttribute('_mc');
|
|
128
|
-
}
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// add a microcomponent to the collection
|
|
133
|
-
add( key: string, element: any, target?: HTMLElement ): any {
|
|
134
|
-
if ( this.#_mc[key] ) return;
|
|
135
|
-
|
|
136
|
-
// if target parameter exists ( a DOM node )
|
|
137
|
-
// you want a top level (public) MC
|
|
138
|
-
if ( target ){
|
|
139
|
-
// ... add the new micro component to the DOM node _mc
|
|
140
|
-
MC.add( target, key, element );
|
|
141
|
-
}
|
|
142
|
-
// add the new micro component to the private collection anyway
|
|
143
|
-
this.#_mc[key] = element;
|
|
144
|
-
|
|
145
|
-
return element;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// remove a micro component from the collection
|
|
149
|
-
remove( key: string, target?: HTMLElement ): void {
|
|
150
|
-
const _mc = this.#_mc;
|
|
151
|
-
const t = target as HTMLElementMC;
|
|
152
|
-
|
|
153
|
-
// if target parameter exists ( a DOM node )
|
|
154
|
-
// you want to remove a top level (public) MC
|
|
155
|
-
if ( target ){
|
|
156
|
-
// ... remove the new micro component from the DOM node _mc
|
|
157
|
-
MC.remove( t, key );
|
|
158
|
-
}
|
|
159
|
-
// remove the new micro component from the private collection anyway
|
|
160
|
-
delete _mc?.[key];
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// return all micro components from the collection, or the searched one
|
|
164
|
-
// in a top level DOM MC, this refers to the inner collection always
|
|
165
|
-
// use MC.siblings() to get the public MCs
|
|
166
|
-
siblings( search: string = '' ): any[] {
|
|
167
|
-
const _mc = this.#_mc;
|
|
168
|
-
|
|
169
|
-
if ( !_mc ) return [];
|
|
170
|
-
|
|
171
|
-
const result = _mc[search || ''];
|
|
172
|
-
|
|
173
|
-
return search && result ? [result] : [ ...Object.values(_mc) ];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/*
|
|
177
|
-
the following selectors will only work for the DOM top level MCs and return [] otherwise
|
|
178
|
-
*/
|
|
179
|
-
|
|
180
|
-
// first anchestor DOM node, all public MCs
|
|
181
|
-
parent( search: string = '' ): any[] {
|
|
182
|
-
if ( !isDOM(this.#target) ) return [];
|
|
183
|
-
|
|
184
|
-
return traverseNodes( (this as any).#target.parentNode, search, true);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// all anchestor DOM nodes, beginning with the closest, all public MCs
|
|
188
|
-
parents( search: string = '' ): any[] {
|
|
189
|
-
if ( !isDOM(this.#target) ) return [];
|
|
190
|
-
|
|
191
|
-
return traverseNodes( (this as any).#target.parentNode, search, false);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// nearest child DOM nodes, all puclic MCs
|
|
195
|
-
children( search: string = '' ): any[] {
|
|
196
|
-
if ( !isDOM(this.#target) ) return [];
|
|
197
|
-
|
|
198
|
-
const id = Math.random().toString().replace('.','');
|
|
199
|
-
const selector = '[_mc]:not([temp_id="' + id +'"] [_mc] [_mc])'; // HOTFIX: minifier or tsc compiler error, dont use backtick string, cuts out blanks
|
|
200
|
-
this.#target.setAttribute('temp_id', id);
|
|
201
|
-
const myDomElements = this.#target.querySelectorAll(selector);
|
|
202
|
-
const mcValues = getMicroComponents([...myDomElements], search);
|
|
203
|
-
this.#target.removeAttribute('temp_id');
|
|
204
|
-
|
|
205
|
-
return mcValues;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// all descendant DOM nodes, all puclic MCs
|
|
209
|
-
descendants( search: string = '' ): any[] {
|
|
210
|
-
if ( !isDOM(this.#target) ) return [];
|
|
211
|
-
|
|
212
|
-
const myDomElements = this.#target.querySelectorAll('*[_mc]');
|
|
213
|
-
|
|
214
|
-
return getMicroComponents([...myDomElements], search);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
1
|
+
"use strict";
|
|
2
|
+
export * from './helpers.js';
|
|
3
|
+
export * from './MC.js';
|
|
4
|
+
export * from './McEvent.js';
|
|
5
|
+
export * from './customElements.js';
|
|
6
|
+
export * from './customObservables.js';
|
|
7
|
+
export * from './customSelectors.js';
|
|
8
|
+
export * from './formValues.js';
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../src/test.js"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,23 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twobirds/microcomponents",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.23",
|
|
4
4
|
"description": "Micro Components Organization Class",
|
|
5
|
-
"main": "microcomponents.
|
|
5
|
+
"main": "./dist/microcomponents.ts",
|
|
6
|
+
"types": "./dist/microcomponents.d.ts",
|
|
6
7
|
"sideEffects": false,
|
|
7
8
|
"scripts": {
|
|
8
9
|
"typedoc": "typedoc",
|
|
10
|
+
"webpack": "webpack",
|
|
9
11
|
"clean": "rm -rf build/*; rm -rf dist/*;",
|
|
10
12
|
"prep": "cp -R assets/* public || true; cp src/* build || true;",
|
|
11
|
-
"build": "npm run clean; npm run prep; tsc; npm run
|
|
12
|
-
"
|
|
13
|
-
"copyToDist": "cp -R assets/* public || true; cp build/micro* dist; cp build/microcomponents.js public; cp build/test.js public; cp MicroComponents.svg dist; cp MicroComponents.svg public/fiddle;"
|
|
13
|
+
"build": "npm run clean; npm run prep; tsc; npm run copyToDist; npm run webpack; cp dist/*.js public; cp build/test.js public; cp build/*.js public;",
|
|
14
|
+
"copyToDist": "cp -R assets/* public || true; cp build/*.ts* dist; cp MicroComponents.svg dist; cp MicroComponents.svg public/fiddle; cp dist/MicroComponents.svg public;"
|
|
14
15
|
},
|
|
15
16
|
"author": "",
|
|
16
17
|
"license": "ISC",
|
|
17
18
|
"devDependencies": {
|
|
18
19
|
"jsmin": "^1.0.1",
|
|
19
|
-
"
|
|
20
|
-
"
|
|
20
|
+
"ts-loader": "^9.5.2",
|
|
21
|
+
"typedoc": "^0.26.11",
|
|
22
|
+
"typescript": "^5.5.4",
|
|
23
|
+
"webpack": "^5.97.1",
|
|
24
|
+
"webpack-cli": "^6.0.1"
|
|
21
25
|
},
|
|
22
26
|
"keywords": [
|
|
23
27
|
"javascript",
|
package/public/MC.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { McEvent } from './McEvent.js';
|
|
3
|
+
var isBrowser = new Function("try {return this===window;}catch(e){ return false;}");
|
|
4
|
+
let nativeEventNames = new Set();
|
|
5
|
+
// HTMLElement native event names
|
|
6
|
+
if (isBrowser()) {
|
|
7
|
+
Object.keys(HTMLElement.prototype).filter(key => /^on/.test(key)).forEach(eventName => nativeEventNames.add(eventName));
|
|
8
|
+
}
|
|
9
|
+
// get all microcomponents from a list of HTMLElements
|
|
10
|
+
function getMicroComponents(elements, search) {
|
|
11
|
+
let mcValues = [];
|
|
12
|
+
elements.forEach(e => {
|
|
13
|
+
if (e) {
|
|
14
|
+
if (e instanceof HTMLElement) {
|
|
15
|
+
const _mc = e?._mc;
|
|
16
|
+
if (!search && _mc) {
|
|
17
|
+
mcValues = mcValues.concat(Object.values(e._mc));
|
|
18
|
+
}
|
|
19
|
+
else if (search && _mc && _mc[search]) {
|
|
20
|
+
mcValues.push(_mc[search]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return mcValues;
|
|
26
|
+
}
|
|
27
|
+
// get all ancestor DOM nodes up to the document root
|
|
28
|
+
function traverseNodes(node, search, firstonly = true) {
|
|
29
|
+
let mcValues = [];
|
|
30
|
+
while (node) {
|
|
31
|
+
if (node._mc) {
|
|
32
|
+
if (!search) {
|
|
33
|
+
mcValues = mcValues.concat(Object.values(node._mc));
|
|
34
|
+
}
|
|
35
|
+
else if (search && node._mc[search]) {
|
|
36
|
+
mcValues.push(node._mc[search]);
|
|
37
|
+
}
|
|
38
|
+
if (firstonly)
|
|
39
|
+
return mcValues;
|
|
40
|
+
}
|
|
41
|
+
node = node.parentNode;
|
|
42
|
+
}
|
|
43
|
+
return mcValues;
|
|
44
|
+
}
|
|
45
|
+
function isDOM(element) {
|
|
46
|
+
return element && element instanceof HTMLElement;
|
|
47
|
+
}
|
|
48
|
+
function addListener(domNode, eventName, handler, capture = false, once = false) {
|
|
49
|
+
let options = { once: false };
|
|
50
|
+
if (capture) {
|
|
51
|
+
options.capture = capture;
|
|
52
|
+
}
|
|
53
|
+
if (once) {
|
|
54
|
+
options.once = once;
|
|
55
|
+
}
|
|
56
|
+
//console.log( 'addEventListener', eventName, ( handler as Function ).name, options );
|
|
57
|
+
domNode.addEventListener(eventName, handler, options);
|
|
58
|
+
}
|
|
59
|
+
function removeListener(domNode, eventName, handler) {
|
|
60
|
+
domNode.removeEventListener(eventName, handler);
|
|
61
|
+
}
|
|
62
|
+
// add a microcomponent to a target DOM node
|
|
63
|
+
function add(target, key, element = {}) {
|
|
64
|
+
let node = target;
|
|
65
|
+
// create the _mc property if necessary
|
|
66
|
+
if (!node._mc) {
|
|
67
|
+
let _MC = MC, debug = MC?.debug;
|
|
68
|
+
MC.debug = false;
|
|
69
|
+
node._mc = new MC(target);
|
|
70
|
+
MC.debug = debug;
|
|
71
|
+
}
|
|
72
|
+
const _mc = node._mc;
|
|
73
|
+
// add the element with the given property key to the _mc property
|
|
74
|
+
if (key.length && !_mc[key]) {
|
|
75
|
+
_mc[key] = element;
|
|
76
|
+
// add attribute if HTMLElement
|
|
77
|
+
if (target instanceof HTMLElement) {
|
|
78
|
+
target.setAttribute('_mc', '');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return element;
|
|
82
|
+
}
|
|
83
|
+
// remove a micro component from the DOM
|
|
84
|
+
function remove(target, key) {
|
|
85
|
+
if (!key)
|
|
86
|
+
return; // silent fail
|
|
87
|
+
const _mc = target?._mc || {};
|
|
88
|
+
// delete the property from the HTMLElement _mc collection
|
|
89
|
+
if (_mc && _mc[key]) {
|
|
90
|
+
delete _mc[key];
|
|
91
|
+
}
|
|
92
|
+
// remove the _mc attribute if the DOM collection is empty
|
|
93
|
+
if (!Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('_mc')) {
|
|
94
|
+
target.removeAttribute('_mc');
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// trigger an event
|
|
99
|
+
function trigger(target, ev, data = {}) {
|
|
100
|
+
let _mc = target?._mc;
|
|
101
|
+
if (!_mc)
|
|
102
|
+
return;
|
|
103
|
+
[...Object.values(_mc)].forEach((mc) => {
|
|
104
|
+
if (mc?.trigger && typeof mc?.trigger === 'function') {
|
|
105
|
+
mc?.trigger(ev, data);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
class McBase {
|
|
111
|
+
#target; // avoid circular reference
|
|
112
|
+
#_mc = {};
|
|
113
|
+
constructor(target) {
|
|
114
|
+
let that = this;
|
|
115
|
+
this.#target = new WeakRef(target);
|
|
116
|
+
if (this.constructor !== MC && this.constructor !== DC) {
|
|
117
|
+
Object.defineProperty(this, "_mc", {
|
|
118
|
+
get() {
|
|
119
|
+
return this.#_mc;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
get target() {
|
|
125
|
+
return this.#target.deref();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
class MC extends McBase {
|
|
129
|
+
constructor(target) {
|
|
130
|
+
super(target);
|
|
131
|
+
let that = this;
|
|
132
|
+
if (that.constructor === MC || that.constructor === DC)
|
|
133
|
+
return;
|
|
134
|
+
setTimeout(() => {
|
|
135
|
+
that.trigger('init');
|
|
136
|
+
}, 0);
|
|
137
|
+
}
|
|
138
|
+
trigger(ev, data = {}, bubble) {
|
|
139
|
+
let that = this;
|
|
140
|
+
bubble = bubble || 'l';
|
|
141
|
+
// run all triggers
|
|
142
|
+
if (that.constructor === MC || that.constructor === DC) {
|
|
143
|
+
[...Object.getOwnPropertyNames(that)].forEach((mc) => {
|
|
144
|
+
if (that[mc]?.trigger && typeof that[mc]?.trigger === 'function') {
|
|
145
|
+
that[mc]?.trigger(ev, data, bubble);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
return; // must return here!
|
|
149
|
+
}
|
|
150
|
+
// only this instance
|
|
151
|
+
if (that instanceof MC || that instanceof DC) {
|
|
152
|
+
const that = this, mcEvent = typeof ev !== 'string' ? ev : new McEvent(ev, data, bubble), handlerName = 'on' + mcEvent.type[0].toUpperCase() + mcEvent.type.slice(1);
|
|
153
|
+
;
|
|
154
|
+
// console.log('trigger', handlerName, mcEvent, data, bubble );
|
|
155
|
+
if (mcEvent.bubble.indexOf('l') > -1) {
|
|
156
|
+
if (!mcEvent.immediateStopped
|
|
157
|
+
&& typeof that[handlerName] === 'function') {
|
|
158
|
+
// console.log( 'execute handler:', handlerName );
|
|
159
|
+
that[handlerName](mcEvent);
|
|
160
|
+
if (that.hasOwnProperty(handlerName)) { // it is a once handler
|
|
161
|
+
delete that[handlerName]; // remove after first execution
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// if event stopped or local only, handling is cancelled
|
|
166
|
+
if (mcEvent.stopped || mcEvent.bubble === 'l') {
|
|
167
|
+
return that;
|
|
168
|
+
}
|
|
169
|
+
// at this point, either 'u' or 'd' bubbling should be indicated
|
|
170
|
+
// mcEvent bubbling is asynchronous, and the order of target MC execution in case of multiple matches is not guaranteed!
|
|
171
|
+
if (isBrowser() && /[ud]/.test(mcEvent.bubble) && that instanceof MC) {
|
|
172
|
+
setTimeout(() => {
|
|
173
|
+
// bubbling must include 'l' for local, or else no handlers are executed'
|
|
174
|
+
if (mcEvent.bubble.indexOf('l') === -1) {
|
|
175
|
+
mcEvent.bubble += 'l';
|
|
176
|
+
}
|
|
177
|
+
// bubble up towards root
|
|
178
|
+
if (mcEvent.bubble.indexOf('u') > -1) {
|
|
179
|
+
that
|
|
180
|
+
.parent()
|
|
181
|
+
.forEach((mc) => {
|
|
182
|
+
mc instanceof MC ? mc.trigger(mcEvent) : null;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
// bubble down
|
|
186
|
+
if (mcEvent.bubble.indexOf('d') > -1) {
|
|
187
|
+
that
|
|
188
|
+
.children()
|
|
189
|
+
.forEach((mc) => {
|
|
190
|
+
mc instanceof MC ? mc.trigger(mcEvent) : null;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}, 0);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
;
|
|
199
|
+
class DC extends MC {
|
|
200
|
+
constructor(target) {
|
|
201
|
+
super(target);
|
|
202
|
+
}
|
|
203
|
+
static debug = false;
|
|
204
|
+
static add = add;
|
|
205
|
+
static remove = remove;
|
|
206
|
+
static trigger = trigger;
|
|
207
|
+
parent(search = '') {
|
|
208
|
+
let target = this.target;
|
|
209
|
+
if (!isDOM(target))
|
|
210
|
+
return [];
|
|
211
|
+
return traverseNodes(target.parentNode, search, true);
|
|
212
|
+
}
|
|
213
|
+
ancestors(search = '') {
|
|
214
|
+
let target = this?.target;
|
|
215
|
+
if (!isDOM(target))
|
|
216
|
+
return [];
|
|
217
|
+
return traverseNodes(this.target.parentNode, search, false);
|
|
218
|
+
}
|
|
219
|
+
children(search = '') {
|
|
220
|
+
let target = this?.target;
|
|
221
|
+
if (!isDOM(target))
|
|
222
|
+
return [];
|
|
223
|
+
const id = Math.random().toString().replace('.', '');
|
|
224
|
+
const selector = '[_mc]:not([temp_id="' + id + '"] [_mc] [_mc])';
|
|
225
|
+
target.setAttribute('temp_id', id);
|
|
226
|
+
const myDomElements = target.querySelectorAll(selector);
|
|
227
|
+
const mcValues = getMicroComponents([...myDomElements], search);
|
|
228
|
+
target.removeAttribute('temp_id');
|
|
229
|
+
return mcValues;
|
|
230
|
+
}
|
|
231
|
+
descendants(search = '') {
|
|
232
|
+
let target = this?.target;
|
|
233
|
+
if (!isDOM(target))
|
|
234
|
+
return [];
|
|
235
|
+
const myDomElements = target.querySelectorAll('*[_mc]');
|
|
236
|
+
return getMicroComponents([...myDomElements], search);
|
|
237
|
+
}
|
|
238
|
+
autoAttachListeners() {
|
|
239
|
+
if (!isBrowser())
|
|
240
|
+
return;
|
|
241
|
+
const target = this.target;
|
|
242
|
+
if (target instanceof HTMLElement) {
|
|
243
|
+
let that = this;
|
|
244
|
+
Object
|
|
245
|
+
.getOwnPropertyNames(that.constructor.prototype)
|
|
246
|
+
.filter(key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function')
|
|
247
|
+
.forEach(key => {
|
|
248
|
+
let once = /^one/.test(key), withoutOnOne = key.replace(/^on[e]{0,1}/, ''), // preserves upper/lowercase for non-native events
|
|
249
|
+
nativeEventName = withoutOnOne.toLowerCase(), listener = (ev) => { that.trigger(withoutOnOne, ev); };
|
|
250
|
+
;
|
|
251
|
+
// console.log('autoAttach', nativeEventName, withoutOnOne, once, listener);
|
|
252
|
+
if (once) {
|
|
253
|
+
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
254
|
+
Object.defineProperty(this, 'on' + withoutOnOne, {
|
|
255
|
+
configurable: true,
|
|
256
|
+
enumerable: false,
|
|
257
|
+
writable: true,
|
|
258
|
+
value: (ev) => {
|
|
259
|
+
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
260
|
+
(that.constructor.prototype[key]).bind(that)(ev);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
if (nativeEventNames.has('on' + nativeEventName)) { // native event defined in HTMLElement
|
|
265
|
+
// console.log('added as native listener', target, nativeEventName );
|
|
266
|
+
addListener(target, nativeEventName, listener, false, once);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
;
|
|
273
|
+
export { addListener, removeListener, McBase, MC, DC, McEvent };
|