@twobirds/microcomponents 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -4
- package/assets/test.html +11 -0
- package/dist/microcomponents.d.ts +15 -231
- package/dist/microcomponents.d.ts.map +1 -1
- package/dist/microcomponents.es3.d.ts +5 -0
- package/dist/microcomponents.es3.d.ts.map +1 -1
- package/dist/microcomponents.js +143 -334
- package/dist/microcomponents.min.js +32 -29
- package/dist/microcomponents.ts +209 -395
- package/dist/test.d.ts +36 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +214 -0
- package/package.json +3 -4
- package/public/{docs/assets → assets}/highlight.css +29 -22
- package/public/assets/search.js +1 -0
- package/public/{docs/classes → classes}/microcomponents.MC.html +78 -241
- package/public/{docs/classes → classes}/microcomponents_es3.MC.html +12 -12
- package/public/{docs/functions → functions}/microcomponents_es3.__isEmpty.html +1 -1
- package/public/index.html +101 -11
- package/public/microcomponents.js +176 -0
- package/public/{docs/modules → modules}/microcomponents.html +2 -7
- package/public/test.html +11 -0
- package/public/test.js +214 -0
- package/src/microcomponents.ts +209 -395
- package/src/test.js +245 -0
- package/typedoc.json +1 -1
- package/assets/index.html +0 -11
- package/jsdoc.conf.json +0 -19
- package/public/docs/assets/search.js +0 -1
- package/public/docs/index.html +0 -97
- package/public/docs/interfaces/microcomponents.HTMLElementMc.html +0 -3605
- package/public/jsdoc/es3/files.html +0 -216
- package/public/jsdoc/es3/index.html +0 -210
- package/public/jsdoc/es3/symbols/_global_.html +0 -368
- package/public/jsdoc/es3/symbols/src/build_microcomponents.es3.js.html +0 -104
- package/public/jsdoc/es5+/files.html +0 -218
- package/public/jsdoc/es5+/index.html +0 -218
- package/public/jsdoc/es5+/symbols/MC.html +0 -621
- package/public/jsdoc/es5+/symbols/_global_.html +0 -720
- package/public/jsdoc/es5+/symbols/microcomponents.MC.html +0 -323
- package/public/jsdoc/es5+/symbols/src/build_microcomponents.js.html +0 -375
- /package/public/{docs/.nojekyll → .nojekyll} +0 -0
- /package/public/{docs/assets → assets}/main.js +0 -0
- /package/public/{docs/assets → assets}/style.css +0 -0
- /package/public/{docs/modules → modules}/microcomponents_es3.html +0 -0
- /package/public/{docs/modules.html → modules.html} +0 -0
package/dist/microcomponents.ts
CHANGED
|
@@ -1,402 +1,216 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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
|
-
*/
|
|
135
|
-
this.target = target;
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* The collection of private microcomponents.
|
|
139
|
-
* @private
|
|
140
|
-
* @type {Object.<string, any>}
|
|
141
|
-
*/
|
|
142
|
-
this.privateMCs = {};
|
|
143
|
-
|
|
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
|
-
}
|
|
3
|
+
/*
|
|
4
|
+
helper methods
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// get all microcomponents from a list of HTMLElements
|
|
8
|
+
function getMicroComponents( elements: any[], search: string): any[] {
|
|
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
|
+
}
|
|
395
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;
|
|
396
44
|
}
|
|
397
45
|
|
|
398
|
-
|
|
399
|
-
|
|
46
|
+
function isDOM(element: { [key:string]: any } ) {
|
|
47
|
+
return element && element instanceof HTMLElement;
|
|
400
48
|
}
|
|
401
49
|
|
|
402
|
-
|
|
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
|
+
}
|
package/dist/test.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
declare function getMicroComponents(elements: any, search: any): any[];
|
|
2
|
+
declare function traverseNodes(node: any, search: any, firstonly?: boolean): any[];
|
|
3
|
+
declare function isDOM(element: any): any;
|
|
4
|
+
declare class MC {
|
|
5
|
+
static add(target: any, key: any, element?: {}): {};
|
|
6
|
+
static remove(target: any, key: any): void;
|
|
7
|
+
static debug: boolean;
|
|
8
|
+
static siblings(target: any, search?: string): any;
|
|
9
|
+
constructor(target: any);
|
|
10
|
+
constructor(target: any);
|
|
11
|
+
get _mc(): {} | undefined;
|
|
12
|
+
get target(): {};
|
|
13
|
+
add(key: any, element: any, target: any): any;
|
|
14
|
+
remove(key: any, target: any): void;
|
|
15
|
+
siblings(search?: string): any[];
|
|
16
|
+
parent(search?: string): any[];
|
|
17
|
+
parents(search?: string): any[];
|
|
18
|
+
children(search?: string): any[];
|
|
19
|
+
descendants(search?: string): any[];
|
|
20
|
+
#private;
|
|
21
|
+
}
|
|
22
|
+
declare let elements: HTMLDivElement[];
|
|
23
|
+
declare class MyInnerMC extends MC {
|
|
24
|
+
constructor(target: any);
|
|
25
|
+
doSomething(): void;
|
|
26
|
+
}
|
|
27
|
+
declare class MyMC extends MC {
|
|
28
|
+
constructor(target: any);
|
|
29
|
+
doSomething(): void;
|
|
30
|
+
#private;
|
|
31
|
+
}
|
|
32
|
+
declare const element: HTMLDivElement | null;
|
|
33
|
+
declare let topLevelMC: {};
|
|
34
|
+
declare let c: NodeListOf<Element>;
|
|
35
|
+
declare let sc: any;
|
|
36
|
+
//# sourceMappingURL=test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../src/test.js"],"names":[],"mappings":"AAAA,uEAeC;AACD,mFAcC;AACD,0CAEC;AACD;IA0BC,oDAaC;IACD,2CAcC;IAnDD,sBAAqB;IAYrB,mDAUC;IArBD,yBAGC;;IACD,0BAGC;IACD,iBAEC;IAyCD,8CAOC;IACD,oCAOC;IACD,iCAKC;IACD,+BAGC;IACD,gCAGC;IACD,iCASC;IACD,oCAIC;;CACD;AAKD,uCAA+D;AAS/D;IACC,yBAEC;IAED,oBAGC;CACD;AAGD;IAGC,yBAQC;IAED,oBAGC;;CACD;AAID,6CAA8C;AAG9C,2BAAiE;AAUjE,mCAA2C;AA8B3C,oBAA8B"}
|