@twobirds/microcomponents 0.0.4 → 0.0.6
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/MicroComponentProposal_draft.md +1 -1
- package/README.md +44 -26
- package/dist/microcomponents.d.ts +218 -2
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.js +237 -43
- package/dist/microcomponents.min.js +7 -6
- package/dist/microcomponents.ts +391 -201
- package/jsdoc.conf.json +19 -0
- package/microcomponents_usage.md +56 -24
- package/package.json +13 -2
- package/public/docs/assets/highlight.css +14 -14
- package/public/docs/assets/search.js +1 -1
- package/public/docs/classes/microcomponents.MC.html +179 -41
- package/public/docs/classes/microcomponents_es3.MC.html +12 -12
- package/public/docs/functions/microcomponents_es3.__isEmpty.html +1 -1
- package/public/docs/index.html +18 -16
- package/public/docs/interfaces/microcomponents.HTMLElementMc.html +2 -2
- package/public/docs/modules/microcomponents.html +1 -1
- package/public/jsdoc/es3/files.html +216 -0
- package/public/jsdoc/es3/index.html +210 -0
- package/public/jsdoc/es3/symbols/_global_.html +368 -0
- package/public/jsdoc/es3/symbols/src/build_microcomponents.es3.js.html +104 -0
- package/public/jsdoc/es5+/files.html +218 -0
- package/public/jsdoc/es5+/index.html +218 -0
- package/public/jsdoc/es5+/symbols/MC.html +621 -0
- package/public/jsdoc/es5+/symbols/_global_.html +720 -0
- package/public/jsdoc/es5+/symbols/microcomponents.MC.html +323 -0
- package/public/jsdoc/es5+/symbols/src/build_microcomponents.js.html +375 -0
- package/src/microcomponents.ts +391 -201
package/dist/microcomponents.ts
CHANGED
|
@@ -1,212 +1,402 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
export class MC {
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
3
|
+
/**
|
|
4
|
+
* @module microcomponenents
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* A static flag to enable or disable debugging mode.
|
|
9
|
+
* @name debug
|
|
10
|
+
* @memberOf MC
|
|
11
|
+
* @static
|
|
12
|
+
* @type {boolean} A static flag to enable or disable debugging mode.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves all top-level microcomponents from a target HTMLElement.
|
|
17
|
+
* @name siblings
|
|
18
|
+
* @memberOf MC
|
|
19
|
+
* @static
|
|
20
|
+
* @function
|
|
21
|
+
* @param {HTMLElement} target - The target DOM element.
|
|
22
|
+
* @param {string} [search] - The key of the specific microcomponent to find.
|
|
23
|
+
* @returns {Array<any>} An array of microcomponents or an empty array if none found.
|
|
24
|
+
* @example
|
|
25
|
+
* const siblings = MC.siblings(
|
|
26
|
+
* document.getElementById('myElement'),
|
|
27
|
+
* 'myComponent'
|
|
28
|
+
* );
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Adds a top level microcomponent to a target DOM node.
|
|
33
|
+
* @name add
|
|
34
|
+
* @memberOf MC
|
|
35
|
+
* @static
|
|
36
|
+
* @function
|
|
37
|
+
* @param {HTMLElement} target - The target DOM element.
|
|
38
|
+
* @param {string} key - The key to identify the microcomponent.
|
|
39
|
+
* @param {any} [element={}] - The microcomponent to add.
|
|
40
|
+
* @returns {any} The added microcomponent.
|
|
41
|
+
* @example
|
|
42
|
+
* const component = MC.add(
|
|
43
|
+
* document.getElementById('myElement'),
|
|
44
|
+
* 'myComponent',
|
|
45
|
+
* {}
|
|
46
|
+
* );
|
|
47
|
+
*/
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Removes a top level microcomponent from the target DOM node.
|
|
51
|
+
* @name remove
|
|
52
|
+
* @memberOf MC
|
|
53
|
+
* @static
|
|
54
|
+
* @function
|
|
55
|
+
* @param {HTMLElement} target - The target DOM element.
|
|
56
|
+
* @param {string} key - The key identifying the microcomponent.
|
|
57
|
+
* @example
|
|
58
|
+
* MC.remove(
|
|
59
|
+
* document.getElementById('myElement'),
|
|
60
|
+
* 'myComponent'
|
|
61
|
+
* );
|
|
62
|
+
*/
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Manages microcomponents (MCs) attached to DOM elements, allowing for the
|
|
66
|
+
* addition, removal, and traversal of microcomponents across siblings, parents,
|
|
67
|
+
* children, and descendants.
|
|
68
|
+
*
|
|
69
|
+
* This class provides an API for interacting with microcomponents on DOM elements.
|
|
70
|
+
* Public microcomponents are stored on the `_mc` attribute of the DOM element, while
|
|
71
|
+
* private microcomponents are managed internally.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```javascript
|
|
75
|
+
*
|
|
76
|
+
* // create the micro component class
|
|
77
|
+
* class MyMC extends MC{
|
|
78
|
+
*
|
|
79
|
+
* constructor( target ){
|
|
80
|
+
* super( target );
|
|
81
|
+
* this.target = target;
|
|
82
|
+
* }
|
|
83
|
+
* }
|
|
84
|
+
*
|
|
85
|
+
* const element = document.querySelector('div')
|
|
86
|
+
*
|
|
87
|
+
* // Add the top level microcomponent to the target element
|
|
88
|
+
* let mcc = MC.add(
|
|
89
|
+
* element,
|
|
90
|
+
* 'myMC',
|
|
91
|
+
* new MyMC( element )
|
|
92
|
+
* );
|
|
93
|
+
*
|
|
94
|
+
* // Add an inner (private) microcomponent to mcc
|
|
95
|
+
* let mcc.add(
|
|
96
|
+
* 'myInnerMC',
|
|
97
|
+
* new MyMC( element )
|
|
98
|
+
* );
|
|
99
|
+
*
|
|
100
|
+
* console.log( element._mc );
|
|
101
|
+
* // { 'myMC': { target: (element) } }
|
|
102
|
+
*
|
|
103
|
+
* console.log( element._mc.myMC );
|
|
104
|
+
* // { target: (element) }
|
|
105
|
+
*
|
|
106
|
+
* console.log( mcc.siblings() );
|
|
107
|
+
* // [ { target: (element) } ]
|
|
108
|
+
*
|
|
109
|
+
* ```
|
|
110
|
+
* @class
|
|
111
|
+
* @name MC
|
|
112
|
+
*/
|
|
113
|
+
class MC {
|
|
114
|
+
|
|
115
|
+
private target: HTMLElement | { [key: string]: any } | undefined;
|
|
116
|
+
|
|
117
|
+
private privateMCs: { [key: string]: any } = {};
|
|
118
|
+
|
|
119
|
+
public _mc?: { [key: string]: any } = {};
|
|
120
|
+
|
|
121
|
+
static debug: boolean = false;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Constructs an MC instance with a target DOM element.
|
|
125
|
+
* @constructor
|
|
126
|
+
* @param {HTMLElement | { [key: string]: any }} - The target DOM element or any object.
|
|
127
|
+
*/
|
|
128
|
+
constructor( target: HTMLElement | { [key: string]: any } | undefined = undefined ) {
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* target is a DOM element or a parent MC
|
|
132
|
+
* @type {object}
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
13
135
|
this.target = target;
|
|
14
|
-
this.privateMCs = {};
|
|
15
|
-
if ( MC?.debug ) {
|
|
16
|
-
this._mc = this.privateMCs;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
// return all top-level micro components from an HTMLElement
|
|
21
|
-
static siblings( target: HTMLElement, search?: string ): any[] {
|
|
22
|
-
let t = target as HTMLElementMc;
|
|
23
|
-
if ( !t?._mc ) return [];
|
|
24
|
-
let mc: { [key: string]: any } = t._mc;
|
|
25
|
-
return search ? mc[search] ? [ mc[search] ] : [] : [ ...Object.entries( t._mc ) ];
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
// add a microcomponent to a target DOM node
|
|
29
|
-
static add( target: HTMLElement, key: string, element: any = {} ): any[] {
|
|
30
|
-
|
|
31
|
-
const mc = target as any;
|
|
32
|
-
|
|
33
|
-
// create the _mc property if necessary
|
|
34
|
-
if ( !mc._mc ){
|
|
35
|
-
mc._mc = new MC();
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// add the element with the given property key to the _mc property object
|
|
39
|
-
if ( key && !mc._mc[ key ] ){
|
|
40
|
-
mc._mc[ key ] = element;
|
|
41
|
-
if ( target ) {
|
|
42
|
-
target.setAttribute('_mc', '' );
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return element;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
// remove a micro component from the target DOM node
|
|
50
|
-
static remove( target: HTMLElement, key: string ): void {
|
|
51
|
-
|
|
52
|
-
if ( //silent fail
|
|
53
|
-
!key
|
|
54
|
-
|| typeof key !== 'string'
|
|
55
|
-
|| !target
|
|
56
|
-
|| target instanceof HTMLElement === false ) {
|
|
57
|
-
return;
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const mc = target as any;
|
|
61
|
-
|
|
62
|
-
// delete the property from the HTMLElement _mc collection
|
|
63
|
-
if (mc._mc && mc._mc[key]) {
|
|
64
|
-
delete mc._mc[key];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// remove the _mc attribute if the DOM collection is empty
|
|
68
|
-
if ( !Object.keys(mc._mc).length && target.hasAttribute('_mc') ) {
|
|
69
|
-
target.removeAttribute('_mc');
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/*
|
|
76
|
-
helper methods
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
// get all microcomponents from a list of HTMLElements
|
|
80
|
-
private getMicroComponents(elements: any[], search?: string): any[] {
|
|
81
|
-
|
|
82
|
-
let mcValues: any[] = [];
|
|
83
|
-
|
|
84
|
-
elements.forEach(e => {
|
|
85
|
-
if ( e ){
|
|
86
|
-
if ( e instanceof HTMLElement ){
|
|
87
|
-
if (!search && (e as HTMLElementMc)._mc) {
|
|
88
|
-
mcValues = mcValues.concat(Object.values((e as any)._mc));
|
|
89
|
-
} else if (search && (e as HTMLElementMc)._mc && (e as any)._mc[search]) {
|
|
90
|
-
mcValues.push((e as any)._mc[search]);
|
|
91
|
-
}
|
|
92
|
-
} else if (e instanceof MC ){
|
|
93
|
-
if ( !search ) {
|
|
94
|
-
mcValues = mcValues.concat(Object.values( this.privateMCs ));
|
|
95
|
-
} else if (search && this.privateMCs[search]) {
|
|
96
|
-
mcValues.push((e as any)._mc[search]);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
return mcValues;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// get all ancestor DOM nodes up to the document
|
|
106
|
-
private traverseNodes(node: Node | null, search?: string, firstonly: boolean = true): any[] {
|
|
107
|
-
|
|
108
|
-
let mcValues: any[] = [];
|
|
109
|
-
|
|
110
|
-
while (node) {
|
|
111
|
-
if ((node as HTMLElementMc)._mc) {
|
|
112
|
-
if (!search) {
|
|
113
|
-
mcValues = mcValues.concat(Object.values((node as any)._mc));
|
|
114
|
-
} else if (search && (node as any)._mc[search]) {
|
|
115
|
-
mcValues.push((node as any)._mc[search]);
|
|
116
|
-
}
|
|
117
|
-
if ( firstonly ) return mcValues;
|
|
118
|
-
}
|
|
119
|
-
node = node.parentNode;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
return mcValues;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
private isDOM( element: any ): boolean {
|
|
126
|
-
return element && element instanceof HTMLElement;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/*
|
|
130
|
-
Methods to handle private MCs
|
|
131
|
-
*/
|
|
132
|
-
|
|
133
|
-
// add a microcomponent to the private microcomponent collection
|
|
134
|
-
add( key?: string, element: any = {} ): any {
|
|
135
|
-
|
|
136
|
-
// add the element
|
|
137
|
-
if ( key ){
|
|
138
|
-
this.privateMCs[key] = element; // also the first element in the private MC collection
|
|
139
|
-
}
|
|
140
|
-
return element;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
// remove a micro component from the private microcomponent collection
|
|
144
|
-
remove(key: string): void {
|
|
145
|
-
|
|
146
|
-
// remove the component from the private collection
|
|
147
|
-
if (key in this.privateMCs) {
|
|
148
|
-
delete this.privateMCs[key];
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
/*
|
|
154
|
-
selector methods, non-chainable
|
|
155
|
-
*/
|
|
156
|
-
|
|
157
|
-
// all private collection members
|
|
158
|
-
siblings(search?: string): any[] {
|
|
159
|
-
if ( this.target ){
|
|
160
|
-
if ( this.target instanceof HTMLElement === true ){
|
|
161
|
-
return this.getMicroComponents( [ this.target ], search );
|
|
162
|
-
} else {
|
|
163
|
-
const result = this.privateMCs[search || ''];
|
|
164
|
-
return search && result ? [ result ] : Object.entries( this.privateMCs );
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return []
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
// first anchestor DOM node, all public MCs
|
|
172
|
-
parent(search?: string): any[] {
|
|
173
|
-
|
|
174
|
-
if ( !this.isDOM( this.target) ) return [];
|
|
175
|
-
|
|
176
|
-
return this.traverseNodes( ( this.target as HTMLElement).parentNode, search, true);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// all anchestor DOM nodes, beginning with the closest, all public MCs
|
|
180
|
-
parents(search?: string): any[] {
|
|
181
|
-
|
|
182
|
-
if ( !this.isDOM( this.target) ) return [];
|
|
183
|
-
|
|
184
|
-
return this.traverseNodes( (this.target as HTMLElement ).parentNode, search, false);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// nearest child DOM nodes, all puclic MCs
|
|
188
|
-
children(search?: string): any[] {
|
|
189
|
-
|
|
190
|
-
if ( !this.isDOM( this.target) ) return [];
|
|
191
|
-
|
|
192
|
-
const id = crypto.randomUUID();
|
|
193
|
-
(this.target as HTMLElement).setAttribute('temp-id', id);
|
|
194
|
-
const myDomElements = (this.target as HTMLElement).querySelectorAll(`*[_mc]:not([temp-id="${id}"] [_mc] [_mc])`);
|
|
195
|
-
const mcValues = this.getMicroComponents( [ ...myDomElements ], search);
|
|
196
|
-
(this.target as HTMLElement).removeAttribute('temp-id');
|
|
197
|
-
return mcValues;
|
|
198
|
-
}
|
|
199
136
|
|
|
200
|
-
|
|
201
|
-
|
|
137
|
+
/**
|
|
138
|
+
* The collection of private microcomponents.
|
|
139
|
+
* @private
|
|
140
|
+
* @type {Object.<string, any>}
|
|
141
|
+
*/
|
|
142
|
+
this.privateMCs = {};
|
|
202
143
|
|
|
203
|
-
|
|
144
|
+
if (MC.debug) {
|
|
145
|
+
/**
|
|
146
|
+
* exposes private microcomponents in debug mode.
|
|
147
|
+
* @public
|
|
148
|
+
* @type {Object.<string, any>}
|
|
149
|
+
*/
|
|
150
|
+
this._mc = this.privateMCs;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
static siblings(target: HTMLElement, search?: string): any[] {
|
|
155
|
+
let t = target as HTMLElementMc;
|
|
156
|
+
if (!t?._mc) return [];
|
|
157
|
+
let mc: { [key: string]: any } = t._mc;
|
|
158
|
+
return search ? mc[search] ? [mc[search]] : [] : [...Object.entries(t._mc)];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
static add(target: HTMLElement, key: string, element: any = {}): any[] {
|
|
162
|
+
const mc = target as any;
|
|
163
|
+
|
|
164
|
+
if (!mc._mc) {
|
|
165
|
+
mc._mc = new MC();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (key && !mc._mc[key]) {
|
|
169
|
+
mc._mc[key] = element;
|
|
170
|
+
if (target) {
|
|
171
|
+
target.setAttribute('_mc', '');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return element;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static remove(target: HTMLElement, key: string): void {
|
|
179
|
+
if (!key || typeof key !== 'string' || !target || !(target instanceof HTMLElement)) return;
|
|
180
|
+
|
|
181
|
+
const mc = target as any;
|
|
182
|
+
|
|
183
|
+
if (mc._mc && mc._mc[key]) {
|
|
184
|
+
delete mc._mc[key];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (!Object.keys(mc._mc).length && target.hasAttribute('_mc')) {
|
|
188
|
+
target.removeAttribute('_mc');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* returns top level microcomponents from a list of HTMLElements.
|
|
194
|
+
* @name getMicroComponents
|
|
195
|
+
* @instance
|
|
196
|
+
* @function
|
|
197
|
+
* @private
|
|
198
|
+
* @param {Array<any>} elements - The elements to search for microcomponents.
|
|
199
|
+
* @param {string} [search] - The key of the specific microcomponent.
|
|
200
|
+
* @returns {Array<any>} An array of microcomponents.
|
|
201
|
+
*/
|
|
202
|
+
private getMicroComponents(elements: any[], search?: string): any[] {
|
|
203
|
+
let mcValues: any[] = [];
|
|
204
|
+
|
|
205
|
+
elements.forEach(e => {
|
|
206
|
+
if (e) {
|
|
207
|
+
if (e instanceof HTMLElement) {
|
|
208
|
+
if (!search && (e as HTMLElementMc)._mc) {
|
|
209
|
+
mcValues = mcValues.concat(Object.values((e as any)._mc));
|
|
210
|
+
} else if (search && (e as HTMLElementMc)._mc && (e as any)._mc[search]) {
|
|
211
|
+
mcValues.push((e as any)._mc[search]);
|
|
212
|
+
}
|
|
213
|
+
} else if (e instanceof MC) {
|
|
214
|
+
if (!search) {
|
|
215
|
+
mcValues = mcValues.concat(Object.values(this.privateMCs));
|
|
216
|
+
} else if (search && this.privateMCs[search]) {
|
|
217
|
+
mcValues.push((e as any)._mc[search]);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
return mcValues;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* returns ancestor MCs
|
|
228
|
+
* @name traverseNodes
|
|
229
|
+
* @instance
|
|
230
|
+
* @function
|
|
231
|
+
* @private
|
|
232
|
+
* @param {Node | null} node - The starting DOM node.
|
|
233
|
+
* @param {string} [search] - The key of the specific microcomponent.
|
|
234
|
+
* @param {boolean} [firstonly=true] - If true, returns the first match only.
|
|
235
|
+
* @returns {Array<any>} An array of matching microcomponents.
|
|
236
|
+
*/
|
|
237
|
+
private traverseNodes(node: Node | null, search?: string, firstonly: boolean = true): any[] {
|
|
238
|
+
let mcValues: any[] = [];
|
|
239
|
+
|
|
240
|
+
while (node) {
|
|
241
|
+
if ((node as HTMLElementMc)._mc) {
|
|
242
|
+
if (!search) {
|
|
243
|
+
mcValues = mcValues.concat(Object.values((node as any)._mc));
|
|
244
|
+
} else if (search && (node as any)._mc[search]) {
|
|
245
|
+
mcValues.push((node as any)._mc[search]);
|
|
246
|
+
}
|
|
247
|
+
if (firstonly) return mcValues;
|
|
248
|
+
}
|
|
249
|
+
node = node.parentNode;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return mcValues;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* returns true if the element is an instance of HTMLElement.
|
|
257
|
+
* @instance
|
|
258
|
+
* @function
|
|
259
|
+
* @name isDOM
|
|
260
|
+
* @private
|
|
261
|
+
* @type {Function}
|
|
262
|
+
* @param {any} element - The element to check.
|
|
263
|
+
* @returns {boolean}
|
|
264
|
+
*/
|
|
265
|
+
private isDOM(element: any): boolean {
|
|
266
|
+
return element && element instanceof HTMLElement;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* adds a microcomponent to the private collection.
|
|
271
|
+
* @name add
|
|
272
|
+
* @instance
|
|
273
|
+
* @function
|
|
274
|
+
* @param {string} [key] - The key to identify the microcomponent.
|
|
275
|
+
* @param {any} [element={}] - The microcomponent to add.
|
|
276
|
+
* @returns {any} The added microcomponent.
|
|
277
|
+
* @example
|
|
278
|
+
* const privateComp = this.add('privateComponent', {});
|
|
279
|
+
*/
|
|
280
|
+
add(key?: string, element: any = {}): any {
|
|
281
|
+
if (key) {
|
|
282
|
+
this.privateMCs[key] = element;
|
|
283
|
+
}
|
|
284
|
+
return element;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* removes a microcomponent from the private collection.
|
|
289
|
+
* @name remove
|
|
290
|
+
* @instance
|
|
291
|
+
* @function
|
|
292
|
+
* @param {string} [key] - The key to identify the microcomponent.
|
|
293
|
+
* @example
|
|
294
|
+
* this.remove('privateComponent');
|
|
295
|
+
*/
|
|
296
|
+
remove(key: string): void {
|
|
297
|
+
if (key in this.privateMCs) {
|
|
298
|
+
delete this.privateMCs[key];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* removes a microcomponent from the private collection.
|
|
304
|
+
* @name siblings
|
|
305
|
+
* @instance
|
|
306
|
+
* @function
|
|
307
|
+
* @param {string} [search] - The key of the specific microcomponent.
|
|
308
|
+
* @returns {Array<any>} An array of matching sibling microcomponents.
|
|
309
|
+
* @example
|
|
310
|
+
* const siblings = this.siblings('siblingComponent');
|
|
311
|
+
*/
|
|
312
|
+
siblings(search?: string): any[] {
|
|
313
|
+
if (this.target) {
|
|
314
|
+
if (this.target instanceof HTMLElement === true) {
|
|
315
|
+
return this.getMicroComponents([this.target], search);
|
|
316
|
+
} else {
|
|
317
|
+
const result = this.privateMCs[search || ''];
|
|
318
|
+
return search && result ? [result] : Object.entries(this.privateMCs);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
return [];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* retrieves the first ancestor DOM node with a matching microcomponent.
|
|
326
|
+
* @name parent
|
|
327
|
+
* @instance
|
|
328
|
+
* @function
|
|
329
|
+
* @param {string} [search] - The key of the specific microcomponent.
|
|
330
|
+
* @returns {Array<any>} An array with the matching microcomponent.
|
|
331
|
+
* @example
|
|
332
|
+
* const parent = mcInstance.parent('parentComponent');
|
|
333
|
+
*/
|
|
334
|
+
/**
|
|
335
|
+
*/
|
|
336
|
+
parent(search?: string): any[] {
|
|
337
|
+
if (!this.isDOM(this.target)) return [];
|
|
338
|
+
return this.traverseNodes((this.target as HTMLElement).parentNode, search, true);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* retrieves the first ancestor DOM node with a matching microcomponent.
|
|
343
|
+
* @name parents
|
|
344
|
+
* @instance
|
|
345
|
+
* @function
|
|
346
|
+
* @param {string} [search] - The key of the specific microcomponent.
|
|
347
|
+
* @returns {Array<any>} An array of matching ancestor microcomponents.
|
|
348
|
+
* @example
|
|
349
|
+
* const parents = mcInstance.parents('ancestorComponent');
|
|
350
|
+
*/
|
|
351
|
+
parents(search?: string): any[] {
|
|
352
|
+
if (!this.isDOM(this.target)) return [];
|
|
353
|
+
return this.traverseNodes((this.target as HTMLElement).parentNode, search, false);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* retrieves nearest child _mc DOM nodes, gets their matching microcomponents, and stops there.
|
|
358
|
+
* @name children
|
|
359
|
+
* @instance
|
|
360
|
+
* @function
|
|
361
|
+
* @param {string} [search] - The key of the specific microcomponent.
|
|
362
|
+
* @returns {Array<any>} An array of matching child microcomponents.
|
|
363
|
+
* @example
|
|
364
|
+
* const children = mcInstance.children('childComponent');
|
|
365
|
+
*/
|
|
366
|
+
children(search?: string): any[] {
|
|
367
|
+
if (!this.isDOM(this.target)) return [];
|
|
368
|
+
|
|
369
|
+
const id = crypto.randomUUID();
|
|
370
|
+
(this.target as HTMLElement).setAttribute('temp-id', id);
|
|
371
|
+
const myDomElements = (this.target as HTMLElement).querySelectorAll(`*[_mc]:not([temp-id="${id}"] [_mc] [_mc])`);
|
|
372
|
+
const mcValues = this.getMicroComponents(Array.from(myDomElements), search);
|
|
373
|
+
|
|
374
|
+
return mcValues;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* retrieves all decendant _mc DOM nodes and gets their matching microcomponents.
|
|
379
|
+
* @name children
|
|
380
|
+
* @instance
|
|
381
|
+
* @function
|
|
382
|
+
* @param {string} [search] - The key of the specific microcomponent.
|
|
383
|
+
* @returns {Array<any>} An array of matching descendant microcomponents.
|
|
384
|
+
* @example
|
|
385
|
+
* const descendants = mcInstance.descendants('descendantComponent');
|
|
386
|
+
*/
|
|
387
|
+
descendants(search?: string): any[] {
|
|
388
|
+
if (!this.isDOM(this.target)) return [];
|
|
389
|
+
|
|
390
|
+
const myDomElements = (this.target as HTMLElement).querySelectorAll('*[_mc]');
|
|
391
|
+
const mcValues = this.getMicroComponents(Array.from(myDomElements), search);
|
|
392
|
+
|
|
393
|
+
return mcValues;
|
|
394
|
+
}
|
|
204
395
|
|
|
205
|
-
const myDomElements = (this.target as HTMLElement).querySelectorAll('*[_mc]');
|
|
206
|
-
return this.getMicroComponents( [ ...myDomElements ], search);
|
|
207
|
-
}
|
|
208
396
|
}
|
|
209
397
|
|
|
210
398
|
export interface HTMLElementMc extends HTMLElement {
|
|
211
399
|
_mc: MC;
|
|
212
400
|
}
|
|
401
|
+
|
|
402
|
+
export { MC };
|
package/jsdoc.conf.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"source": {
|
|
3
|
+
"include": ["src/microcomponents.js"],
|
|
4
|
+
"includePattern": ".+\\.js(doc|x)?$",
|
|
5
|
+
"excludePattern": "(^|\\/|\\\\)_"
|
|
6
|
+
},
|
|
7
|
+
"opts": {
|
|
8
|
+
"destination": "./public/jsdoc/es5+",
|
|
9
|
+
"recurse": false,
|
|
10
|
+
"template": "default"
|
|
11
|
+
},
|
|
12
|
+
"plugins": [
|
|
13
|
+
"plugins/markdown"
|
|
14
|
+
],
|
|
15
|
+
"templates": {
|
|
16
|
+
"cleverLinks": true,
|
|
17
|
+
"monospaceLinks": true
|
|
18
|
+
}
|
|
19
|
+
}
|