@twobirds/microcomponents 0.0.1 → 0.0.2
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 +16 -0
- package/README.md +10 -6
- package/assets/fiddle/MicroComponents.svg +15 -0
- package/assets/fiddle/fiddle.css +0 -0
- package/assets/fiddle/fiddle.js +0 -0
- package/assets/fiddle/index.html +0 -0
- package/assets/index.html +11 -0
- package/dist/microcomponents.d.ts +2 -1
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.js +47 -22
- package/dist/microcomponents.min.js +18 -11
- package/dist/microcomponents.ts +196 -0
- package/package.json +27 -6
- package/public/docs/assets/search.js +1 -0
- package/{docs → public/docs}/classes/microcomponents.MC.html +38 -23
- package/{docs → public/docs}/classes/microcomponents_es3.MC.html +16 -16
- package/{docs → public/docs}/functions/microcomponents_es3.__isEmpty.html +5 -5
- package/{docs → public/docs}/index.html +14 -9
- package/{docs → public/docs}/interfaces/microcomponents.HTMLElementMc.html +6 -6
- package/{docs → public/docs}/modules/microcomponents.html +5 -5
- package/{docs → public/docs}/modules/microcomponents_es3.html +4 -4
- package/{docs → public/docs}/modules.html +5 -5
- package/public/fiddle/MicroComponents.svg +15 -0
- package/public/fiddle/fiddle.css +0 -0
- package/public/fiddle/fiddle.js +0 -0
- package/public/fiddle/index.html +0 -0
- package/public/index.html +11 -0
- package/src/microcomponents.ts +53 -26
- package/typedoc.json +3 -3
- package/docs/assets/search.js +0 -1
- /package/{docs → public/docs}/.nojekyll +0 -0
- /package/{docs → public/docs}/assets/highlight.css +0 -0
- /package/{docs → public/docs}/assets/main.js +0 -0
- /package/{docs → public/docs}/assets/style.css +0 -0
package/.gitlab-ci.yml
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# The Docker image that will be used to build your app
|
|
2
|
+
image: node:lts
|
|
3
|
+
# Functions that should be executed before the build script is run
|
|
4
|
+
before_script:
|
|
5
|
+
- npm i
|
|
6
|
+
pages:
|
|
7
|
+
script:
|
|
8
|
+
- npm run build
|
|
9
|
+
artifacts:
|
|
10
|
+
paths:
|
|
11
|
+
# The folder that contains the files to be exposed at the Page URL
|
|
12
|
+
- public
|
|
13
|
+
rules:
|
|
14
|
+
# This ensures that only pushes to the default branch will trigger
|
|
15
|
+
# a pages deploy
|
|
16
|
+
- if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
|
package/README.md
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
## MICRO COMPONENTS
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|
|
|
5
5
|
This code aims at **improving Javascript modularity**. It does not describe a specific Micro Component itself but rather describes they way they are **organized**. A typical example of a **Micro Component** can be found in "additional concepts" below.
|
|
6
6
|
|
|
7
|
-
Using micro components enforces **DRY, KISS and SOLID** principles in Javascript programming. Read this [RFC](
|
|
7
|
+
Using micro components enforces **DRY, KISS and SOLID** principles in Javascript programming. Read this [RFC](https://gitlab.com/twoBirds/microcomponents/-/blob/main/MicroComponentProposal_draft.md?ref_type=heads) for the shortest info.
|
|
8
8
|
|
|
9
|
-
This is a low-level approach. It consists of a single class with 2 static methods and 5 selector methods, and weights
|
|
9
|
+
This is a low-level approach. It consists of a single class with 2 static methods and 5 selector methods, and weights in at ~2 kB. Also there is an ES3 version to help e.g. [hbbtv](https://en.wikipedia.org/wiki/Hybrid_Broadcast_Broadband_TV) modular JS coding, or any other device that relies on ES3.
|
|
10
10
|
|
|
11
11
|
For an overview, continue reading.
|
|
12
12
|
|
|
13
|
-
For a detailed functional description goto [Micro Component Usage](https://gitlab.com/twoBirds/microcomponents/-/blob/main/microcomponents_usage.md?ref_type=heads).
|
|
13
|
+
For a detailed functional description goto [Micro Component Usage](https://gitlab.com/twoBirds/microcomponents/-/blob/main/microcomponents_usage.md?ref_type=heads) and check the [typedoc API documentation](http://twobirds.gitlab.io/microcomponents/docs/modules.html).
|
|
14
14
|
|
|
15
15
|
To see what can be done, go to [additional concepts](https://gitlab.com/twoBirds/microcomponents/-/blob/main/microcomponents_magic.md?ref_type=heads) which delve a little deeper into how to use it, and what it already has been used for.
|
|
16
16
|
|
|
@@ -59,7 +59,7 @@ before:
|
|
|
59
59
|
|
|
60
60
|
code: (assume "Button" is some class in a library)
|
|
61
61
|
```javascript
|
|
62
|
-
import 'MC' from 'twobirds/microcomponents';
|
|
62
|
+
import 'MC' from '@twobirds/microcomponents';
|
|
63
63
|
import 'Button' from 'tb/ui'; // made-up example library
|
|
64
64
|
|
|
65
65
|
// select first button element in DOM
|
|
@@ -93,7 +93,11 @@ This functionality has been used in very big and complicated data processing pro
|
|
|
93
93
|
- to represent business processes and their tasks
|
|
94
94
|
- and lately to compose very complex native web components from distinct modular functionality.
|
|
95
95
|
|
|
96
|
-
I decided extract this from the [twoBirds framework](https://www.npmjs.com/package/twobirds-core), since it is framework agnostic and relies solely on the standard DOM and native javascript or typescript.
|
|
96
|
+
I decided to extract this functionality from the [twoBirds framework](https://www.npmjs.com/package/twobirds-core), since it is framework agnostic, and relies solely on the standard DOM and native javascript or typescript, and is a useful tool in itself.
|
|
97
|
+
|
|
98
|
+
## Useful Links
|
|
99
|
+
|
|
100
|
+
[API documentation](http://twobirds.gitlab.io/microcomponents/docs/modules.html)
|
|
97
101
|
|
|
98
102
|
## Contact
|
|
99
103
|
Feel free to contact me for any questions or inquiries:
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<svg width="200" height="200" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
|
|
2
|
+
<!-- Created with SVG-edit - http://svg-edit.googlecode.com/ -->
|
|
3
|
+
<title>test</title>
|
|
4
|
+
<rect fill="transparent" x="0" y="0" width="200" height="200" fill-opacity="1"/>
|
|
5
|
+
<g style="fill:transparent">
|
|
6
|
+
<title>first bird</title>
|
|
7
|
+
<path fill="#18829c" d="
|
|
8
|
+
m15,160
|
|
9
|
+
a30,75 45 1 0 160,-150
|
|
10
|
+
a20,50 225 1 1 -110,100
|
|
11
|
+
a12,25 45 1 1 60,-50
|
|
12
|
+
a20,50 225 1 0 -110,100
|
|
13
|
+
" />
|
|
14
|
+
</g>
|
|
15
|
+
</svg>
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Micro Components</title>
|
|
7
|
+
</head>
|
|
8
|
+
<body>
|
|
9
|
+
<a href="http://twobirds.gitlab.io/microcomponents/docs/modules.html" target="_blank">API</a>
|
|
10
|
+
</body>
|
|
11
|
+
</html>
|
|
@@ -3,12 +3,13 @@ export declare class MC {
|
|
|
3
3
|
private privateMCs;
|
|
4
4
|
static debug: boolean;
|
|
5
5
|
constructor(target?: HTMLElement | null);
|
|
6
|
-
static add(element
|
|
6
|
+
static add(element: any, key: string, target: HTMLElement): any;
|
|
7
7
|
add(element?: any, key?: string, target?: HTMLElement): any;
|
|
8
8
|
static remove(target: HTMLElement, key: string): void;
|
|
9
9
|
remove(key: string): void;
|
|
10
10
|
private getMicroComponents;
|
|
11
11
|
private traverseNodes;
|
|
12
|
+
private isDOM;
|
|
12
13
|
siblings(search?: string): any[];
|
|
13
14
|
parent(search?: string): any[];
|
|
14
15
|
parents(search?: string): any[];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"microcomponents.d.ts","sourceRoot":"","sources":["../src/microcomponents.ts"],"names":[],"mappings":"AACA,qBAAa,EAAE;IAEd,OAAO,CAAC,MAAM,CAAqB;IAEnC,OAAO,CAAC,UAAU,CAA8B;IAEhD,MAAM,CAAC,KAAK,EAAE,OAAO,CAAS;gBAElB,MAAM,GAAE,WAAW,GAAG,IAAW;IAM7C,MAAM,CAAC,GAAG,CAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"microcomponents.d.ts","sourceRoot":"","sources":["../src/microcomponents.ts"],"names":[],"mappings":"AACA,qBAAa,EAAE;IAEd,OAAO,CAAC,MAAM,CAAqB;IAEnC,OAAO,CAAC,UAAU,CAA8B;IAEhD,MAAM,CAAC,KAAK,EAAE,OAAO,CAAS;gBAElB,MAAM,GAAE,WAAW,GAAG,IAAW;IAM7C,MAAM,CAAC,GAAG,CAAE,OAAO,EAAE,GAAQ,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,GAAI,GAAG;IAqBtE,GAAG,CAAE,OAAO,GAAE,GAAQ,EAAE,GAAG,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAI,GAAG;IAgBjE,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;IAkBrD,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI;IAiBzB,OAAO,CAAC,kBAAkB;IA0B1B,OAAO,CAAC,aAAa;IAmBrB,OAAO,CAAC,KAAK;IASb,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,EAAE;IAchC,MAAM,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,EAAE;IAQ9B,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,EAAE;IAQ/B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,EAAE;IAahC,WAAW,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,GAAG,EAAE;CAOnC;AAED,MAAM,WAAW,aAAc,SAAQ,WAAW;IACjD,GAAG,EAAE,EAAE,CAAC;CACR"}
|
package/dist/microcomponents.js
CHANGED
|
@@ -10,17 +10,15 @@ export class MC {
|
|
|
10
10
|
// add a microcomponent to a target DOM node
|
|
11
11
|
static add(element = {}, key, target) {
|
|
12
12
|
const mc = target;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
target.setAttribute('_mc', '');
|
|
23
|
-
}
|
|
13
|
+
// create the _mc property if necessary
|
|
14
|
+
if (!mc._mc) {
|
|
15
|
+
mc._mc = new MC();
|
|
16
|
+
}
|
|
17
|
+
// add the element with the given property key to the _mc property object
|
|
18
|
+
if (key && !mc._mc[key]) {
|
|
19
|
+
mc._mc[key] = element;
|
|
20
|
+
if (target) {
|
|
21
|
+
target.setAttribute('_mc', '');
|
|
24
22
|
}
|
|
25
23
|
}
|
|
26
24
|
return element;
|
|
@@ -30,9 +28,10 @@ export class MC {
|
|
|
30
28
|
// add the element
|
|
31
29
|
if (target && key) {
|
|
32
30
|
// ...to the DOM
|
|
33
|
-
|
|
31
|
+
this.privateMCs[key] = element; // also the first element in the private MC collection
|
|
32
|
+
return MC.add(element, key, target);
|
|
34
33
|
}
|
|
35
|
-
|
|
34
|
+
if (key && !element) {
|
|
36
35
|
// ... to the private element collection
|
|
37
36
|
this.privateMCs[key] = element;
|
|
38
37
|
}
|
|
@@ -69,11 +68,21 @@ export class MC {
|
|
|
69
68
|
let mcValues = [];
|
|
70
69
|
elements.forEach(e => {
|
|
71
70
|
if (e) {
|
|
72
|
-
if (
|
|
73
|
-
|
|
71
|
+
if (e instanceof HTMLElement) {
|
|
72
|
+
if (!search && e._mc) {
|
|
73
|
+
mcValues = mcValues.concat(Object.values(e._mc));
|
|
74
|
+
}
|
|
75
|
+
else if (search && e._mc && e._mc[search]) {
|
|
76
|
+
mcValues.push(e._mc[search]);
|
|
77
|
+
}
|
|
74
78
|
}
|
|
75
|
-
else if (
|
|
76
|
-
|
|
79
|
+
else if (e instanceof MC) {
|
|
80
|
+
if (!search) {
|
|
81
|
+
mcValues = mcValues.concat(Object.values(this.privateMCs));
|
|
82
|
+
}
|
|
83
|
+
else if (search && this.privateMCs[search]) {
|
|
84
|
+
mcValues.push(e._mc[search]);
|
|
85
|
+
}
|
|
77
86
|
}
|
|
78
87
|
}
|
|
79
88
|
});
|
|
@@ -97,27 +106,41 @@ export class MC {
|
|
|
97
106
|
}
|
|
98
107
|
return mcValues;
|
|
99
108
|
}
|
|
109
|
+
isDOM(element) {
|
|
110
|
+
return element && element instanceof HTMLElement;
|
|
111
|
+
}
|
|
100
112
|
/*
|
|
101
113
|
selector methods, non-chainable
|
|
102
114
|
*/
|
|
103
115
|
// all public _mc members and private collection members
|
|
104
116
|
siblings(search) {
|
|
105
|
-
|
|
117
|
+
if (this.target) {
|
|
118
|
+
if (this.target instanceof HTMLElement === true) {
|
|
119
|
+
return this.getMicroComponents([this.target], search);
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
const result = this.privateMCs[search || ''];
|
|
123
|
+
return search && result ? [result] : Object.entries(this.privateMCs);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return [];
|
|
106
127
|
}
|
|
107
|
-
// first anchestor DOM node, all
|
|
128
|
+
// first anchestor DOM node, all public MCs
|
|
108
129
|
parent(search) {
|
|
109
|
-
if (!this.target)
|
|
130
|
+
if (!this.isDOM(this.target))
|
|
110
131
|
return [];
|
|
111
132
|
return this.traverseNodes(this.target.parentNode, search, true);
|
|
112
133
|
}
|
|
113
|
-
// all anchestor DOM nodes, beginning with the closest, all
|
|
134
|
+
// all anchestor DOM nodes, beginning with the closest, all public MCs
|
|
114
135
|
parents(search) {
|
|
115
|
-
if (!this.target)
|
|
136
|
+
if (!this.isDOM(this.target))
|
|
116
137
|
return [];
|
|
117
138
|
return this.traverseNodes(this.target.parentNode, search, false);
|
|
118
139
|
}
|
|
119
140
|
// nearest child DOM nodes, all puclic MCs
|
|
120
141
|
children(search) {
|
|
142
|
+
if (!this.isDOM(this.target))
|
|
143
|
+
return [];
|
|
121
144
|
const id = crypto.randomUUID();
|
|
122
145
|
this.target.setAttribute('temp-id', id);
|
|
123
146
|
const myDomElements = this.target.querySelectorAll(`*[_mc]:not([temp-id="${id}"] [_mc] [_mc])`);
|
|
@@ -127,6 +150,8 @@ export class MC {
|
|
|
127
150
|
}
|
|
128
151
|
// all descendant DOM nodes, all puclic MCs
|
|
129
152
|
descendants(search) {
|
|
153
|
+
if (!this.isDOM(this.target))
|
|
154
|
+
return [];
|
|
130
155
|
const myDomElements = this.target.querySelectorAll('*[_mc]');
|
|
131
156
|
return this.getMicroComponents([...myDomElements], search);
|
|
132
157
|
}
|
|
@@ -1,27 +1,34 @@
|
|
|
1
1
|
'use strict';export class MC{target;privateMCs={};static debug=false;constructor(target=null){this.target=target;this.privateMCs={};}
|
|
2
|
-
static add(element={},key,target){const mc=target;if(
|
|
3
|
-
if(key&&!mc._mc[key]){mc._mc[key]=element;if(target){target.setAttribute('_mc','');}}
|
|
2
|
+
static add(element={},key,target){const mc=target;if(!mc._mc){mc._mc=new MC();}
|
|
3
|
+
if(key&&!mc._mc[key]){mc._mc[key]=element;if(target){target.setAttribute('_mc','');}}
|
|
4
4
|
return element;}
|
|
5
|
-
add(element={},key,target){if(target&&key){MC.add(element,key,target);}
|
|
6
|
-
|
|
5
|
+
add(element={},key,target){if(target&&key){this.privateMCs[key]=element;return MC.add(element,key,target);}
|
|
6
|
+
if(key&&!element){this.privateMCs[key]=element;}
|
|
7
7
|
return element;}
|
|
8
8
|
static remove(target,key){const mc=target;if(mc._mc&&mc._mc[key]){delete mc._mc[key];}
|
|
9
9
|
if(!Object.keys(mc._mc).length&&target.hasAttribute('_mc')){target.removeAttribute('_mc');}
|
|
10
10
|
return;}
|
|
11
11
|
remove(key){if(key in this.privateMCs){delete this.privateMCs[key];}
|
|
12
12
|
if(MC.debug&&this.target){MC.remove(this.target,key);}}
|
|
13
|
-
getMicroComponents(elements,search){let mcValues=[];elements.forEach(e=>{if(e){if(!search&&e._mc){mcValues=mcValues.concat(Object.values(e._mc));}
|
|
14
|
-
else if(search&&e._mc&&e._mc[search]){mcValues.push(e._mc[search]);}}
|
|
13
|
+
getMicroComponents(elements,search){let mcValues=[];elements.forEach(e=>{if(e){if(e instanceof HTMLElement){if(!search&&e._mc){mcValues=mcValues.concat(Object.values(e._mc));}
|
|
14
|
+
else if(search&&e._mc&&e._mc[search]){mcValues.push(e._mc[search]);}}
|
|
15
|
+
else if(e instanceof MC){if(!search){mcValues=mcValues.concat(Object.values(this.privateMCs));}
|
|
16
|
+
else if(search&&this.privateMCs[search]){mcValues.push(e._mc[search]);}}}});return mcValues;}
|
|
15
17
|
traverseNodes(node,search,firstonly=true){let mcValues=[];while(node){if(node._mc){if(!search){mcValues=mcValues.concat(Object.values(node._mc));}
|
|
16
18
|
else if(search&&node._mc[search]){mcValues.push(node._mc[search]);}
|
|
17
19
|
if(firstonly)
|
|
18
20
|
return mcValues;}
|
|
19
21
|
node=node.parentNode;}
|
|
20
22
|
return mcValues;}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
isDOM(element){return element&&element instanceof HTMLElement;}
|
|
24
|
+
siblings(search){if(this.target){if(this.target instanceof HTMLElement===true){return this.getMicroComponents([this.target],search);}
|
|
25
|
+
else{const result=this.privateMCs[search||''];return search&&result?[result]:Object.entries(this.privateMCs);}}
|
|
26
|
+
return[];}
|
|
27
|
+
parent(search){if(!this.isDOM(this.target))
|
|
23
28
|
return[];return this.traverseNodes(this.target.parentNode,search,true);}
|
|
24
|
-
parents(search){if(!this.target)
|
|
29
|
+
parents(search){if(!this.isDOM(this.target))
|
|
25
30
|
return[];return this.traverseNodes(this.target.parentNode,search,false);}
|
|
26
|
-
children(search){
|
|
27
|
-
|
|
31
|
+
children(search){if(!this.isDOM(this.target))
|
|
32
|
+
return[];const id=crypto.randomUUID();this.target.setAttribute('temp-id',id);const myDomElements=this.target.querySelectorAll(`*[_mc]:not([temp-id="${id}"][_mc][_mc])`);const mcValues=this.getMicroComponents([...myDomElements],search);this.target.removeAttribute('temp-id');return mcValues;}
|
|
33
|
+
descendants(search){if(!this.isDOM(this.target))
|
|
34
|
+
return[];const myDomElements=this.target.querySelectorAll('*[_mc]');return this.getMicroComponents([...myDomElements],search);}}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
export class MC {
|
|
3
|
+
|
|
4
|
+
private target: HTMLElement | null;
|
|
5
|
+
|
|
6
|
+
private privateMCs: { [key: string]: any } = {};
|
|
7
|
+
|
|
8
|
+
static debug: boolean = false;
|
|
9
|
+
|
|
10
|
+
constructor(target: HTMLElement | null = null) {
|
|
11
|
+
this.target = target;
|
|
12
|
+
this.privateMCs = {};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// add a microcomponent to a target DOM node
|
|
16
|
+
static add( element: any = {}, key: string, target: HTMLElement ): any {
|
|
17
|
+
|
|
18
|
+
const mc = target as any;
|
|
19
|
+
|
|
20
|
+
// create the _mc property if necessary
|
|
21
|
+
if ( !mc._mc ){
|
|
22
|
+
mc._mc = new MC();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// add the element with the given property key to the _mc property object
|
|
26
|
+
if ( key && !mc._mc[ key ] ){
|
|
27
|
+
mc._mc[ key ] = element;
|
|
28
|
+
if ( target ) {
|
|
29
|
+
target.setAttribute('_mc', '' );
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return element;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// add a microcomponent to the DOM or to the private microcomponent collection
|
|
37
|
+
add( element: any = {}, key?: string, target?: HTMLElement ): any {
|
|
38
|
+
|
|
39
|
+
// add the element
|
|
40
|
+
if ( target && key ){
|
|
41
|
+
// ...to the DOM
|
|
42
|
+
this.privateMCs[key] = element; // also the first element in the private MC collection
|
|
43
|
+
return MC.add( element, key, target );
|
|
44
|
+
}
|
|
45
|
+
if ( key && !element ){
|
|
46
|
+
// ... to the private element collection
|
|
47
|
+
this.privateMCs[key] = element;
|
|
48
|
+
}
|
|
49
|
+
return element;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// remove a micro component from the DOM
|
|
53
|
+
static remove(target: HTMLElement, key: string): void {
|
|
54
|
+
|
|
55
|
+
const mc = target as any;
|
|
56
|
+
|
|
57
|
+
// delete the property from the DOM
|
|
58
|
+
if (mc._mc && mc._mc[key]) {
|
|
59
|
+
delete mc._mc[key];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// remove the _mc attribute if the DOM collection is empty
|
|
63
|
+
if ( !Object.keys(mc._mc).length && target.hasAttribute('_mc') ) {
|
|
64
|
+
target.removeAttribute('_mc');
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// remove a micro component from the DOM or the private microcomponent collection
|
|
71
|
+
remove(key: string): void {
|
|
72
|
+
|
|
73
|
+
// remove the component from the private collection
|
|
74
|
+
if (key in this.privateMCs) {
|
|
75
|
+
delete this.privateMCs[key];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if ( MC.debug && this.target ){
|
|
79
|
+
MC.remove(this.target, key);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/*
|
|
84
|
+
helper methods
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
// get all microcomponents from a list of HTMLElements
|
|
88
|
+
private getMicroComponents(elements: any[], search?: string): any[] {
|
|
89
|
+
|
|
90
|
+
let mcValues: any[] = [];
|
|
91
|
+
|
|
92
|
+
elements.forEach(e => {
|
|
93
|
+
if ( e ){
|
|
94
|
+
if ( e instanceof HTMLElement ){
|
|
95
|
+
if (!search && (e as HTMLElementMc)._mc) {
|
|
96
|
+
mcValues = mcValues.concat(Object.values((e as any)._mc));
|
|
97
|
+
} else if (search && (e as HTMLElementMc)._mc && (e as any)._mc[search]) {
|
|
98
|
+
mcValues.push((e as any)._mc[search]);
|
|
99
|
+
}
|
|
100
|
+
} else if (e instanceof MC ){
|
|
101
|
+
if ( !search ) {
|
|
102
|
+
mcValues = mcValues.concat(Object.values( this.privateMCs ));
|
|
103
|
+
} else if (search && this.privateMCs[search]) {
|
|
104
|
+
mcValues.push((e as any)._mc[search]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
return mcValues;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// get all ancestor DOM nodes up to the document
|
|
114
|
+
private traverseNodes(node: Node | null, search?: string, firstonly: boolean = true): any[] {
|
|
115
|
+
|
|
116
|
+
let mcValues: any[] = [];
|
|
117
|
+
|
|
118
|
+
while (node) {
|
|
119
|
+
if ((node as HTMLElementMc)._mc) {
|
|
120
|
+
if (!search) {
|
|
121
|
+
mcValues = mcValues.concat(Object.values((node as any)._mc));
|
|
122
|
+
} else if (search && (node as any)._mc[search]) {
|
|
123
|
+
mcValues.push((node as any)._mc[search]);
|
|
124
|
+
}
|
|
125
|
+
if ( firstonly ) return mcValues;
|
|
126
|
+
}
|
|
127
|
+
node = node.parentNode;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return mcValues;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private isDOM( element: any ): boolean {
|
|
134
|
+
return element && element instanceof HTMLElement;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/*
|
|
138
|
+
selector methods, non-chainable
|
|
139
|
+
*/
|
|
140
|
+
|
|
141
|
+
// all public _mc members and private collection members
|
|
142
|
+
siblings(search?: string): any[] {
|
|
143
|
+
if ( this.target ){
|
|
144
|
+
if ( this.target instanceof HTMLElement === true ){
|
|
145
|
+
return this.getMicroComponents( [ this.target ], search );
|
|
146
|
+
} else {
|
|
147
|
+
const result = this.privateMCs[search || ''];
|
|
148
|
+
return search && result ? [ result ] : Object.entries( this.privateMCs );
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return []
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
// first anchestor DOM node, all public MCs
|
|
156
|
+
parent(search?: string): any[] {
|
|
157
|
+
|
|
158
|
+
if ( !this.isDOM( this.target) ) return [];
|
|
159
|
+
|
|
160
|
+
return this.traverseNodes( ( this.target as HTMLElement).parentNode, search, true);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// all anchestor DOM nodes, beginning with the closest, all public MCs
|
|
164
|
+
parents(search?: string): any[] {
|
|
165
|
+
|
|
166
|
+
if ( !this.isDOM( this.target) ) return [];
|
|
167
|
+
|
|
168
|
+
return this.traverseNodes( (this.target as HTMLElement ).parentNode, search, false);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// nearest child DOM nodes, all puclic MCs
|
|
172
|
+
children(search?: string): any[] {
|
|
173
|
+
|
|
174
|
+
if ( !this.isDOM( this.target) ) return [];
|
|
175
|
+
|
|
176
|
+
const id = crypto.randomUUID();
|
|
177
|
+
(this.target as HTMLElement).setAttribute('temp-id', id);
|
|
178
|
+
const myDomElements = (this.target as HTMLElement).querySelectorAll(`*[_mc]:not([temp-id="${id}"] [_mc] [_mc])`);
|
|
179
|
+
const mcValues = this.getMicroComponents( [ ...myDomElements ], search);
|
|
180
|
+
(this.target as HTMLElement).removeAttribute('temp-id');
|
|
181
|
+
return mcValues;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// all descendant DOM nodes, all puclic MCs
|
|
185
|
+
descendants(search?: string): any[] {
|
|
186
|
+
|
|
187
|
+
if ( !this.isDOM( this.target) ) return [];
|
|
188
|
+
|
|
189
|
+
const myDomElements = (this.target as HTMLElement).querySelectorAll('*[_mc]');
|
|
190
|
+
return this.getMicroComponents( [ ...myDomElements ], search);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export interface HTMLElementMc extends HTMLElement {
|
|
195
|
+
_mc: MC;
|
|
196
|
+
}
|
package/package.json
CHANGED
|
@@ -1,19 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twobirds/microcomponents",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.2",
|
|
4
4
|
"description": "Micro Components Organization Class",
|
|
5
5
|
"main": "microcomponents.js",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"scripts": {
|
|
8
|
-
"
|
|
8
|
+
"typedoc": "typedoc",
|
|
9
|
+
"clean": "rm -rf build/*; rm -rf docs/*; rm -rf dist/*;",
|
|
10
|
+
"prep": "cp -R assets/* public || true; cp src/* build || true;",
|
|
11
|
+
"build": "npm run clean; npm run prep; tsc; npm run minify; npm run copyToDist;",
|
|
9
12
|
"minify": "jsmin -o build/microcomponents.es3.min.js ./src/microcomponents.es3.js;jsmin -o build/microcomponents.min.js build/microcomponents.js;",
|
|
10
|
-
"copyToDist": "cp build/* dist; cp MicroComponents.svg dist"
|
|
13
|
+
"copyToDist": "cp build/* dist; cp MicroComponents.svg dist; cp MicroComponents.svg public/fiddle;"
|
|
11
14
|
},
|
|
12
15
|
"author": "",
|
|
13
16
|
"license": "ISC",
|
|
14
17
|
"devDependencies": {
|
|
15
18
|
"jsmin": "^1.0.1",
|
|
16
|
-
"typedoc": "^0.
|
|
17
|
-
"typescript": "^5.6.
|
|
18
|
-
}
|
|
19
|
+
"typedoc": "^0.26.10",
|
|
20
|
+
"typescript": "^5.6.3"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"javascript",
|
|
24
|
+
"library",
|
|
25
|
+
"native",
|
|
26
|
+
"DOM manipulation",
|
|
27
|
+
"lightweight",
|
|
28
|
+
"standalone",
|
|
29
|
+
"SPA",
|
|
30
|
+
"PWA",
|
|
31
|
+
"ES3",
|
|
32
|
+
"ES5",
|
|
33
|
+
"ES6",
|
|
34
|
+
"ES7",
|
|
35
|
+
"ES2015",
|
|
36
|
+
"ES2016",
|
|
37
|
+
"client",
|
|
38
|
+
"API"
|
|
39
|
+
]
|
|
19
40
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
window.searchData = JSON.parse("{\"kinds\":{\"2\":\"Module\",\"64\":\"Function\",\"128\":\"Class\",\"256\":\"Interface\",\"512\":\"Constructor\",\"1024\":\"Property\",\"2048\":\"Method\",\"65536\":\"Type literal\"},\"rows\":[{\"kind\":2,\"name\":\"microcomponents\",\"url\":\"modules/microcomponents.html\",\"classes\":\"tsd-kind-module\"},{\"kind\":128,\"name\":\"MC\",\"url\":\"classes/microcomponents.MC.html\",\"classes\":\"tsd-kind-class tsd-parent-kind-module\",\"parent\":\"microcomponents\"},{\"kind\":1024,\"name\":\"debug\",\"url\":\"classes/microcomponents.MC.html#debug\",\"classes\":\"tsd-kind-property tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"add\",\"url\":\"classes/microcomponents.MC.html#add-2\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"remove\",\"url\":\"classes/microcomponents.MC.html#remove-2\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":512,\"name\":\"constructor\",\"url\":\"classes/microcomponents.MC.html#constructor\",\"classes\":\"tsd-kind-constructor tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":1024,\"name\":\"target\",\"url\":\"classes/microcomponents.MC.html#target\",\"classes\":\"tsd-kind-property tsd-parent-kind-class tsd-is-private\",\"parent\":\"microcomponents.MC\"},{\"kind\":1024,\"name\":\"privateMCs\",\"url\":\"classes/microcomponents.MC.html#privateMCs\",\"classes\":\"tsd-kind-property tsd-parent-kind-class tsd-is-private\",\"parent\":\"microcomponents.MC\"},{\"kind\":65536,\"name\":\"__type\",\"url\":\"classes/microcomponents.MC.html#privateMCs.__type\",\"classes\":\"tsd-kind-type-literal tsd-parent-kind-property\",\"parent\":\"microcomponents.MC.privateMCs\"},{\"kind\":2048,\"name\":\"add\",\"url\":\"classes/microcomponents.MC.html#add\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"remove\",\"url\":\"classes/microcomponents.MC.html#remove\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"getMicroComponents\",\"url\":\"classes/microcomponents.MC.html#getMicroComponents\",\"classes\":\"tsd-kind-method tsd-parent-kind-class tsd-is-private\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"traverseNodes\",\"url\":\"classes/microcomponents.MC.html#traverseNodes\",\"classes\":\"tsd-kind-method tsd-parent-kind-class tsd-is-private\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"isDOM\",\"url\":\"classes/microcomponents.MC.html#isDOM\",\"classes\":\"tsd-kind-method tsd-parent-kind-class tsd-is-private\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"siblings\",\"url\":\"classes/microcomponents.MC.html#siblings\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"parent\",\"url\":\"classes/microcomponents.MC.html#parent\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"parents\",\"url\":\"classes/microcomponents.MC.html#parents\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"children\",\"url\":\"classes/microcomponents.MC.html#children\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":2048,\"name\":\"descendants\",\"url\":\"classes/microcomponents.MC.html#descendants\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.MC\"},{\"kind\":256,\"name\":\"HTMLElementMc\",\"url\":\"interfaces/microcomponents.HTMLElementMc.html\",\"classes\":\"tsd-kind-interface tsd-parent-kind-module\",\"parent\":\"microcomponents\"},{\"kind\":1024,\"name\":\"_mc\",\"url\":\"interfaces/microcomponents.HTMLElementMc.html#_mc\",\"classes\":\"tsd-kind-property tsd-parent-kind-interface\",\"parent\":\"microcomponents.HTMLElementMc\"},{\"kind\":2,\"name\":\"microcomponents.es3\",\"url\":\"modules/microcomponents_es3.html\",\"classes\":\"tsd-kind-module\"},{\"kind\":64,\"name\":\"__isEmpty\",\"url\":\"functions/microcomponents_es3.__isEmpty.html\",\"classes\":\"tsd-kind-function tsd-parent-kind-module\",\"parent\":\"microcomponents.es3\"},{\"kind\":128,\"name\":\"MC\",\"url\":\"classes/microcomponents_es3.MC.html\",\"classes\":\"tsd-kind-class tsd-parent-kind-module\",\"parent\":\"microcomponents.es3\"},{\"kind\":2048,\"name\":\"add\",\"url\":\"classes/microcomponents_es3.MC.html#add\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"},{\"kind\":2048,\"name\":\"remove\",\"url\":\"classes/microcomponents_es3.MC.html#remove\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"},{\"kind\":512,\"name\":\"constructor\",\"url\":\"classes/microcomponents_es3.MC.html#constructor\",\"classes\":\"tsd-kind-constructor tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"},{\"kind\":1024,\"name\":\"_target\",\"url\":\"classes/microcomponents_es3.MC.html#_target\",\"classes\":\"tsd-kind-property tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"},{\"kind\":2048,\"name\":\"siblings\",\"url\":\"classes/microcomponents_es3.MC.html#siblings\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"},{\"kind\":2048,\"name\":\"parent\",\"url\":\"classes/microcomponents_es3.MC.html#parent\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"},{\"kind\":2048,\"name\":\"parents\",\"url\":\"classes/microcomponents_es3.MC.html#parents\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"},{\"kind\":2048,\"name\":\"children\",\"url\":\"classes/microcomponents_es3.MC.html#children\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"},{\"kind\":2048,\"name\":\"descendants\",\"url\":\"classes/microcomponents_es3.MC.html#descendants\",\"classes\":\"tsd-kind-method tsd-parent-kind-class\",\"parent\":\"microcomponents.es3.MC\"}],\"index\":{\"version\":\"2.3.9\",\"fields\":[\"name\",\"comment\"],\"fieldVectors\":[[\"name/0\",[0,31.209]],[\"comment/0\",[]],[\"name/1\",[1,26.101]],[\"comment/1\",[]],[\"name/2\",[2,31.209]],[\"comment/2\",[]],[\"name/3\",[3,22.736]],[\"comment/3\",[]],[\"name/4\",[4,22.736]],[\"comment/4\",[]],[\"name/5\",[5,26.101]],[\"comment/5\",[]],[\"name/6\",[6,31.209]],[\"comment/6\",[]],[\"name/7\",[7,31.209]],[\"comment/7\",[]],[\"name/8\",[8,31.209]],[\"comment/8\",[]],[\"name/9\",[3,22.736]],[\"comment/9\",[]],[\"name/10\",[4,22.736]],[\"comment/10\",[]],[\"name/11\",[9,31.209]],[\"comment/11\",[]],[\"name/12\",[10,31.209]],[\"comment/12\",[]],[\"name/13\",[11,31.209]],[\"comment/13\",[]],[\"name/14\",[12,26.101]],[\"comment/14\",[]],[\"name/15\",[13,26.101]],[\"comment/15\",[]],[\"name/16\",[14,26.101]],[\"comment/16\",[]],[\"name/17\",[15,26.101]],[\"comment/17\",[]],[\"name/18\",[16,26.101]],[\"comment/18\",[]],[\"name/19\",[17,31.209]],[\"comment/19\",[]],[\"name/20\",[18,31.209]],[\"comment/20\",[]],[\"name/21\",[19,31.209]],[\"comment/21\",[]],[\"name/22\",[20,31.209]],[\"comment/22\",[]],[\"name/23\",[1,26.101]],[\"comment/23\",[]],[\"name/24\",[3,22.736]],[\"comment/24\",[]],[\"name/25\",[4,22.736]],[\"comment/25\",[]],[\"name/26\",[5,26.101]],[\"comment/26\",[]],[\"name/27\",[21,31.209]],[\"comment/27\",[]],[\"name/28\",[12,26.101]],[\"comment/28\",[]],[\"name/29\",[13,26.101]],[\"comment/29\",[]],[\"name/30\",[14,26.101]],[\"comment/30\",[]],[\"name/31\",[15,26.101]],[\"comment/31\",[]],[\"name/32\",[16,26.101]],[\"comment/32\",[]]],\"invertedIndex\":[[\"__isempty\",{\"_index\":20,\"name\":{\"22\":{}},\"comment\":{}}],[\"__type\",{\"_index\":8,\"name\":{\"8\":{}},\"comment\":{}}],[\"_mc\",{\"_index\":18,\"name\":{\"20\":{}},\"comment\":{}}],[\"_target\",{\"_index\":21,\"name\":{\"27\":{}},\"comment\":{}}],[\"add\",{\"_index\":3,\"name\":{\"3\":{},\"9\":{},\"24\":{}},\"comment\":{}}],[\"children\",{\"_index\":15,\"name\":{\"17\":{},\"31\":{}},\"comment\":{}}],[\"constructor\",{\"_index\":5,\"name\":{\"5\":{},\"26\":{}},\"comment\":{}}],[\"debug\",{\"_index\":2,\"name\":{\"2\":{}},\"comment\":{}}],[\"descendants\",{\"_index\":16,\"name\":{\"18\":{},\"32\":{}},\"comment\":{}}],[\"getmicrocomponents\",{\"_index\":9,\"name\":{\"11\":{}},\"comment\":{}}],[\"htmlelementmc\",{\"_index\":17,\"name\":{\"19\":{}},\"comment\":{}}],[\"isdom\",{\"_index\":11,\"name\":{\"13\":{}},\"comment\":{}}],[\"mc\",{\"_index\":1,\"name\":{\"1\":{},\"23\":{}},\"comment\":{}}],[\"microcomponents\",{\"_index\":0,\"name\":{\"0\":{}},\"comment\":{}}],[\"microcomponents.es3\",{\"_index\":19,\"name\":{\"21\":{}},\"comment\":{}}],[\"parent\",{\"_index\":13,\"name\":{\"15\":{},\"29\":{}},\"comment\":{}}],[\"parents\",{\"_index\":14,\"name\":{\"16\":{},\"30\":{}},\"comment\":{}}],[\"privatemcs\",{\"_index\":7,\"name\":{\"7\":{}},\"comment\":{}}],[\"remove\",{\"_index\":4,\"name\":{\"4\":{},\"10\":{},\"25\":{}},\"comment\":{}}],[\"siblings\",{\"_index\":12,\"name\":{\"14\":{},\"28\":{}},\"comment\":{}}],[\"target\",{\"_index\":6,\"name\":{\"6\":{}},\"comment\":{}}],[\"traversenodes\",{\"_index\":10,\"name\":{\"12\":{}},\"comment\":{}}]],\"pipeline\":[]}}");
|