@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.
@@ -1,212 +1,402 @@
1
1
  'use strict';
2
- export class MC {
3
2
 
4
- private target: HTMLElement | null;
5
-
6
- private privateMCs: { [key: string]: any } = {};
7
-
8
- _mc?: { [key: string]: any } = {};
9
-
10
- static debug: boolean = false;
11
-
12
- constructor(target: HTMLElement | null = null) {
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
+ * &nbsp;document.getElementById('myElement'),
27
+ * &nbsp;'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
+ * &nbsp;document.getElementById('myElement'),
44
+ * &nbsp;'myComponent',
45
+ * &nbsp;{}
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
+ * &nbsp;document.getElementById('myElement'),
60
+ * &nbsp;'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
+ * &nbsp;constructor( target ){
80
+ * &nbsp;&nbsp;super( target );
81
+ * &nbsp;&nbsp;this.target = target;
82
+ * &nbsp;}
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
+ * &nbsp;element,
90
+ * &nbsp;'myMC',
91
+ * &nbsp;new MyMC( element )
92
+ * );
93
+ *
94
+ * // Add an inner (private) microcomponent to mcc
95
+ * let mcc.add(
96
+ * &nbsp;'myInnerMC',
97
+ * &nbsp;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
- // all descendant DOM nodes, all puclic MCs
201
- descendants(search?: string): any[] {
137
+ /**
138
+ * The collection of private microcomponents.
139
+ * @private
140
+ * @type {Object.<string, any>}
141
+ */
142
+ this.privateMCs = {};
202
143
 
203
- if ( !this.isDOM( this.target) ) return [];
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 };
@@ -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
+ }