@twobirds/microcomponents 0.0.20 → 0.0.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitlab-ci.yml +1 -0
- package/README.md +33 -14
- package/assets/documentation/MicroComponents.md +228 -0
- package/assets/my-tag.css +4 -0
- package/assets/test.html +5 -1
- package/dist/MC.d.ts +32 -0
- package/dist/MC.d.ts.map +1 -0
- package/dist/MC.min.js +414 -0
- package/dist/MC.ts +350 -0
- package/dist/MCbundle.d.ts +8 -0
- package/dist/MCbundle.d.ts.map +1 -0
- package/dist/MCbundle.min.js +1 -0
- package/dist/MCbundle.ts +8 -0
- package/dist/McEvent.d.ts +15 -0
- package/dist/McEvent.d.ts.map +1 -0
- package/dist/McEvent.ts +96 -0
- package/dist/autoloader.d.ts +2 -0
- package/dist/autoloader.d.ts.map +1 -0
- package/dist/autoloader.ts +98 -0
- package/dist/customElements.d.ts +5 -0
- package/dist/customElements.d.ts.map +1 -0
- package/dist/customElements.min.js +1 -0
- package/dist/customElements.ts +67 -0
- package/dist/customObservables.d.ts +3 -0
- package/dist/customObservables.d.ts.map +1 -0
- package/dist/customObservables.min.js +1 -0
- package/dist/customObservables.ts +340 -0
- package/dist/customSelectors.d.ts +108 -0
- package/dist/customSelectors.d.ts.map +1 -0
- package/dist/customSelectors.min.js +1 -0
- package/dist/customSelectors.ts +1634 -0
- package/dist/formValues.d.ts +4 -0
- package/dist/formValues.d.ts.map +1 -0
- package/dist/formValues.ts +275 -0
- package/dist/formvValues.min.js +1 -0
- package/dist/helpers.d.ts +11 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.ts +173 -0
- package/dist/microcomponents.es3.d.ts +1 -20
- package/dist/microcomponents.es3.d.ts.map +1 -1
- package/dist/test.d.ts +2 -0
- package/dist/test.d.ts.map +1 -0
- package/package.json +11 -7
- package/public/MC.js +273 -0
- package/public/MC.min.js +414 -0
- package/public/MCbundle.js +8 -0
- package/public/MCbundle.min.js +1 -0
- package/public/McEvent.js +74 -0
- package/public/MicroComponents.svg +15 -0
- package/public/assets/search.js +1 -1
- package/public/autoloader.js +57 -0
- package/public/classes/MC.DC.html +247 -0
- package/public/classes/MC.MC.html +150 -0
- package/public/classes/MC.McBase.html +123 -0
- package/public/classes/MCbundle.McEvent.html +184 -0
- package/public/customElements.js +48 -0
- package/public/customElements.min.js +1 -0
- package/public/customObservables.js +255 -0
- package/public/customObservables.min.js +1 -0
- package/public/customSelectors.js +1088 -0
- package/public/customSelectors.min.js +1 -0
- package/public/documentation/MicroComponents.md +228 -0
- package/public/formValues.js +211 -0
- package/public/formvValues.min.js +1 -0
- package/public/functions/MC.addListener.html +73 -0
- package/public/functions/MC.removeListener.html +69 -0
- package/public/functions/MCbundle._d.html +85 -0
- package/public/functions/MCbundle._t.html +85 -0
- package/public/functions/MCbundle.debounce.html +85 -0
- package/public/functions/MCbundle.deepEqual.html +85 -0
- package/public/functions/MCbundle.defineCE.html +98 -0
- package/public/functions/MCbundle.formValues.html +89 -0
- package/public/functions/MCbundle.getCE.html +83 -0
- package/public/functions/MCbundle.htmlToElements.html +83 -0
- package/public/functions/MCbundle.isObject.html +83 -0
- package/public/functions/MCbundle.kebabToPascal.html +83 -0
- package/public/functions/MCbundle.makeCO.html +85 -0
- package/public/functions/MCbundle.nameSpace.html +89 -0
- package/public/functions/MCbundle.parse.html +85 -0
- package/public/helpers.js +128 -0
- package/public/index.html +22 -13
- package/public/interfaces/MC.HTMLMcElement.html +3605 -0
- package/public/interfaces/MCbundle.DSelector.html +1010 -0
- package/public/interfaces/MCbundle.LooseObject.html +80 -0
- package/public/interfaces/MCbundle.TSelector.html +709 -0
- package/public/microcomponents.es3.js +348 -0
- package/public/modules/MC.html +84 -0
- package/public/modules/MCbundle.html +133 -0
- package/public/modules.html +4 -4
- package/public/my-tag.css +4 -0
- package/public/test.html +5 -1
- package/public/test.js +100 -202
- package/public/types/MCbundle.selection.html +74 -0
- package/public/types/MCbundle.singleSelector.html +74 -0
- package/src/MC.ts +350 -0
- package/src/MCbundle.ts +8 -0
- package/src/McEvent.ts +96 -0
- package/src/autoloader.ts +98 -0
- package/src/customElements.ts +67 -0
- package/src/customObservables.ts +340 -0
- package/src/customSelectors.ts +1634 -0
- package/src/formValues.ts +275 -0
- package/src/helpers.ts +173 -0
- package/src/microcomponents.es3.js +353 -82
- package/src/test.js +137 -218
- package/tsconfig.json +2 -2
- package/typedoc.json +1 -1
- package/webpack.config.js +233 -0
- package/dist/microcomponents.d.ts +0 -26
- package/dist/microcomponents.d.ts.map +0 -1
- package/dist/microcomponents.es3.js +0 -96
- package/dist/microcomponents.es3.min.js +0 -15
- package/dist/microcomponents.js +0 -176
- package/dist/microcomponents.min.js +0 -38
- package/dist/microcomponents.ts +0 -216
- package/public/classes/microcomponents.MC.html +0 -313
- package/public/classes/microcomponents_es3.MC.html +0 -336
- package/public/functions/microcomponents_es3.__isEmpty.html +0 -60
- package/public/microcomponents.js +0 -176
- package/public/modules/microcomponents.html +0 -57
- package/public/modules/microcomponents_es3.html +0 -60
- package/src/microcomponents.ts +0 -216
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
function __isEmpty(obj) {
|
|
3
|
-
if (!obj)
|
|
4
|
-
return false;
|
|
5
|
-
for (var key in obj) {
|
|
6
|
-
if (obj.hasOwnProperty(key) && key !== "_target") {
|
|
7
|
-
return false;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
return true;
|
|
11
|
-
}
|
|
12
|
-
function MC(target) {
|
|
13
|
-
this._target = target;
|
|
14
|
-
}
|
|
15
|
-
// static functions, chainable
|
|
16
|
-
MC.add = function (domElement, key, element) {
|
|
17
|
-
if (key && typeof key === "string" && domElement && !domElement._mc[key]) {
|
|
18
|
-
domElement.mc[key] = element;
|
|
19
|
-
domElement.setAttribute("_mc", "");
|
|
20
|
-
}
|
|
21
|
-
return this;
|
|
22
|
-
};
|
|
23
|
-
MC.remove = function (domElement, key) {
|
|
24
|
-
delete domElement._mc[key];
|
|
25
|
-
if (isEmpty(this)) {
|
|
26
|
-
this._target.removeAttribute("_mc");
|
|
27
|
-
}
|
|
28
|
-
return this;
|
|
29
|
-
};
|
|
30
|
-
// other methods, not chainable
|
|
31
|
-
MC.prototype.siblings = function (search) {
|
|
32
|
-
var mcValues = [];
|
|
33
|
-
if (this._target._mc) {
|
|
34
|
-
for (var key in this._target._mc) {
|
|
35
|
-
if (key !== "_target" &&
|
|
36
|
-
this._target._mc.hasOwnProperty(key) &&
|
|
37
|
-
(!search || key === search)) {
|
|
38
|
-
mcValues.push(this._target._mc[key]);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
return mcValues;
|
|
43
|
-
};
|
|
44
|
-
MC.prototype.parent = function (search) {
|
|
45
|
-
var mcValues = [];
|
|
46
|
-
var current = this._target.parentNode;
|
|
47
|
-
while (current) {
|
|
48
|
-
if (current._mc) {
|
|
49
|
-
return current._mc.siblings(search);
|
|
50
|
-
}
|
|
51
|
-
current = current.parentNode;
|
|
52
|
-
}
|
|
53
|
-
return mcValues;
|
|
54
|
-
};
|
|
55
|
-
MC.prototype.parents = function (search) {
|
|
56
|
-
var mcValues = [];
|
|
57
|
-
var current = this._target.parentNode;
|
|
58
|
-
while (current) {
|
|
59
|
-
if (current._mc) {
|
|
60
|
-
mcValues = mcValues.concat(current._mc.siblings(search));
|
|
61
|
-
}
|
|
62
|
-
current = current.parentNode;
|
|
63
|
-
}
|
|
64
|
-
return mcValues;
|
|
65
|
-
};
|
|
66
|
-
MC.prototype.children = function (search) {
|
|
67
|
-
var mcValues = [];
|
|
68
|
-
function traverseTree(element) {
|
|
69
|
-
var children = element.children;
|
|
70
|
-
for (var i = 0; i < children.length; i++) {
|
|
71
|
-
var child = children[i];
|
|
72
|
-
if (child._mc) {
|
|
73
|
-
mcValues = mcValues.concat(child._mc.siblings(search));
|
|
74
|
-
continue;
|
|
75
|
-
}
|
|
76
|
-
traverseTree(child);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
traverseTree(this._target);
|
|
80
|
-
return mcValues;
|
|
81
|
-
};
|
|
82
|
-
MC.prototype.descendants = function (search) {
|
|
83
|
-
var mcValues = [];
|
|
84
|
-
function traverseTree(element) {
|
|
85
|
-
var children = element.children;
|
|
86
|
-
for (var i = 0; i < children.length; i++) {
|
|
87
|
-
var child = children[i];
|
|
88
|
-
if (child._mc) {
|
|
89
|
-
mcValues = mcValues.concat(child._mc.siblings(search));
|
|
90
|
-
}
|
|
91
|
-
traverseTree(child);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
traverseTree(this._target);
|
|
95
|
-
return mcValues;
|
|
96
|
-
};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";function __isEmpty(obj){if(!obj)return false;for(var key in obj){if(obj.hasOwnProperty(key)&&key!=="_target"){return false;}}
|
|
2
|
-
return true;}
|
|
3
|
-
function MC(target){this._target=target;}
|
|
4
|
-
MC.add=function(domElement,key,element){if(key&&typeof key==="string"&&domElement&&!domElement._mc[key]){domElement.mc[key]=element;domElement.setAttribute("_mc","");}
|
|
5
|
-
return this;};MC.remove=function(domElement,key){delete domElement._mc[key];if(isEmpty(this)){this._target.removeAttribute("_mc");}
|
|
6
|
-
return this;};MC.prototype.siblings=function(search){var mcValues=[];if(this._target._mc){for(var key in this._target._mc){if(key!=="_target"&&this._target._mc.hasOwnProperty(key)&&(!search||key===search)){mcValues.push(this._target._mc[key]);}}}
|
|
7
|
-
return mcValues;};MC.prototype.parent=function(search){var mcValues=[];var current=this._target.parentNode;while(current){if(current._mc){return current._mc.siblings(search);}
|
|
8
|
-
current=current.parentNode;}
|
|
9
|
-
return mcValues;};MC.prototype.parents=function(search){var mcValues=[];var current=this._target.parentNode;while(current){if(current._mc){mcValues=mcValues.concat(current._mc.siblings(search));}
|
|
10
|
-
current=current.parentNode;}
|
|
11
|
-
return mcValues;};MC.prototype.children=function(search){var mcValues=[];function traverseTree(element){var children=element.children;for(var i=0;i<children.length;i++){var child=children[i];if(child._mc){mcValues=mcValues.concat(child._mc.siblings(search));continue;}
|
|
12
|
-
traverseTree(child);}}
|
|
13
|
-
traverseTree(this._target);return mcValues;};MC.prototype.descendants=function(search){var mcValues=[];function traverseTree(element){var children=element.children;for(var i=0;i<children.length;i++){var child=children[i];if(child._mc){mcValues=mcValues.concat(child._mc.siblings(search));}
|
|
14
|
-
traverseTree(child);}}
|
|
15
|
-
traverseTree(this._target);return mcValues;};
|
package/dist/microcomponents.js
DELETED
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
/*
|
|
3
|
-
helper methods
|
|
4
|
-
*/
|
|
5
|
-
// get all microcomponents from a list of HTMLElements
|
|
6
|
-
function getMicroComponents(elements, search) {
|
|
7
|
-
let mcValues = [];
|
|
8
|
-
elements.forEach(e => {
|
|
9
|
-
if (e) {
|
|
10
|
-
if (e instanceof HTMLElement) {
|
|
11
|
-
const _mc = e?._mc;
|
|
12
|
-
if (!search && _mc) {
|
|
13
|
-
mcValues = mcValues.concat(Object.values(e._mc));
|
|
14
|
-
}
|
|
15
|
-
else if (search && _mc && _mc[search]) {
|
|
16
|
-
mcValues.push(_mc[search]);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
});
|
|
21
|
-
return mcValues;
|
|
22
|
-
}
|
|
23
|
-
// get all ancestor DOM nodes up to the document
|
|
24
|
-
function traverseNodes(node, search, firstonly = true) {
|
|
25
|
-
let mcValues = [];
|
|
26
|
-
while (node) {
|
|
27
|
-
if (node._mc) {
|
|
28
|
-
if (!search) {
|
|
29
|
-
mcValues = mcValues.concat(Object.values(node._mc));
|
|
30
|
-
}
|
|
31
|
-
else if (search && node._mc[search]) {
|
|
32
|
-
mcValues.push(node._mc[search]);
|
|
33
|
-
}
|
|
34
|
-
if (firstonly)
|
|
35
|
-
return mcValues;
|
|
36
|
-
}
|
|
37
|
-
node = node.parentNode;
|
|
38
|
-
}
|
|
39
|
-
return mcValues;
|
|
40
|
-
}
|
|
41
|
-
function isDOM(element) {
|
|
42
|
-
return element && element instanceof HTMLElement;
|
|
43
|
-
}
|
|
44
|
-
export class MC {
|
|
45
|
-
#target = {};
|
|
46
|
-
#_mc = {};
|
|
47
|
-
static debug = false;
|
|
48
|
-
constructor(target) {
|
|
49
|
-
this.#target = target;
|
|
50
|
-
this.#_mc = {};
|
|
51
|
-
}
|
|
52
|
-
get _mc() {
|
|
53
|
-
if (!MC.debug)
|
|
54
|
-
return undefined;
|
|
55
|
-
return this.#_mc;
|
|
56
|
-
}
|
|
57
|
-
get target() {
|
|
58
|
-
return this.#target;
|
|
59
|
-
}
|
|
60
|
-
// return all top-level micro components from an HTMLElement, or the searched one
|
|
61
|
-
static siblings(target, search = '') {
|
|
62
|
-
const mc = target;
|
|
63
|
-
if (!mc._mc) {
|
|
64
|
-
return [];
|
|
65
|
-
}
|
|
66
|
-
const _mc = mc._mc;
|
|
67
|
-
if (search.length) {
|
|
68
|
-
return _mc?.[search] || [];
|
|
69
|
-
}
|
|
70
|
-
return [...Object.values(_mc)];
|
|
71
|
-
}
|
|
72
|
-
// add a microcomponent to a target DOM node
|
|
73
|
-
static add(target, key, element = {}) {
|
|
74
|
-
let node = target;
|
|
75
|
-
// create the _mc property if necessary
|
|
76
|
-
if (!node._mc) {
|
|
77
|
-
node._mc = new MC(target);
|
|
78
|
-
}
|
|
79
|
-
const _mc = node._mc;
|
|
80
|
-
// add the element with the given property key to the _mc property
|
|
81
|
-
if (key.length && !_mc[key]) {
|
|
82
|
-
_mc[key] = element;
|
|
83
|
-
// add attribute if HTMLElement
|
|
84
|
-
if (target instanceof HTMLElement) {
|
|
85
|
-
target.setAttribute('_mc', '');
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return element;
|
|
89
|
-
}
|
|
90
|
-
// remove a micro component from the DOM
|
|
91
|
-
static remove(target, key) {
|
|
92
|
-
if (!key)
|
|
93
|
-
return; // silent fail
|
|
94
|
-
const _mc = target?._mc || {};
|
|
95
|
-
// delete the property from the HTMLElement _mc collection
|
|
96
|
-
if (_mc && _mc[key]) {
|
|
97
|
-
delete _mc[key];
|
|
98
|
-
}
|
|
99
|
-
// remove the _mc attribute if the DOM collection is empty
|
|
100
|
-
if (!Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('_mc')) {
|
|
101
|
-
target.removeAttribute('_mc');
|
|
102
|
-
}
|
|
103
|
-
return;
|
|
104
|
-
}
|
|
105
|
-
// add a microcomponent to the collection
|
|
106
|
-
add(key, element, target) {
|
|
107
|
-
if (this.#_mc[key])
|
|
108
|
-
return;
|
|
109
|
-
// if target parameter exists ( a DOM node )
|
|
110
|
-
// you want a top level (public) MC
|
|
111
|
-
if (target) {
|
|
112
|
-
// ... add the new micro component to the DOM node _mc
|
|
113
|
-
MC.add(target, key, element);
|
|
114
|
-
}
|
|
115
|
-
// add the new micro component to the private collection anyway
|
|
116
|
-
this.#_mc[key] = element;
|
|
117
|
-
return element;
|
|
118
|
-
}
|
|
119
|
-
// remove a micro component from the collection
|
|
120
|
-
remove(key, target) {
|
|
121
|
-
const _mc = this.#_mc;
|
|
122
|
-
const t = target;
|
|
123
|
-
// if target parameter exists ( a DOM node )
|
|
124
|
-
// you want to remove a top level (public) MC
|
|
125
|
-
if (target) {
|
|
126
|
-
// ... remove the new micro component from the DOM node _mc
|
|
127
|
-
MC.remove(t, key);
|
|
128
|
-
}
|
|
129
|
-
// remove the new micro component from the private collection anyway
|
|
130
|
-
delete _mc?.[key];
|
|
131
|
-
}
|
|
132
|
-
// return all micro components from the collection, or the searched one
|
|
133
|
-
// in a top level DOM MC, this refers to the inner collection always
|
|
134
|
-
// use MC.siblings() to get the public MCs
|
|
135
|
-
siblings(search = '') {
|
|
136
|
-
const _mc = this.#_mc;
|
|
137
|
-
if (!_mc)
|
|
138
|
-
return [];
|
|
139
|
-
const result = _mc[search || ''];
|
|
140
|
-
return search && result ? [result] : [...Object.values(_mc)];
|
|
141
|
-
}
|
|
142
|
-
/*
|
|
143
|
-
the following selectors will only work for the DOM top level MCs and return [] otherwise
|
|
144
|
-
*/
|
|
145
|
-
// first anchestor DOM node, all public MCs
|
|
146
|
-
parent(search = '') {
|
|
147
|
-
if (!isDOM(this.#target))
|
|
148
|
-
return [];
|
|
149
|
-
return traverseNodes(this.#target.parentNode, search, true);
|
|
150
|
-
}
|
|
151
|
-
// all anchestor DOM nodes, beginning with the closest, all public MCs
|
|
152
|
-
parents(search = '') {
|
|
153
|
-
if (!isDOM(this.#target))
|
|
154
|
-
return [];
|
|
155
|
-
return traverseNodes(this.#target.parentNode, search, false);
|
|
156
|
-
}
|
|
157
|
-
// nearest child DOM nodes, all puclic MCs
|
|
158
|
-
children(search = '') {
|
|
159
|
-
if (!isDOM(this.#target))
|
|
160
|
-
return [];
|
|
161
|
-
const id = Math.random().toString().replace('.', '');
|
|
162
|
-
const selector = '[_mc]:not([temp_id="' + id + '"] [_mc] [_mc])'; // HOTFIX: minifier or tsc compiler error, dont use backtick string, cuts out blanks
|
|
163
|
-
this.#target.setAttribute('temp_id', id);
|
|
164
|
-
const myDomElements = this.#target.querySelectorAll(selector);
|
|
165
|
-
const mcValues = getMicroComponents([...myDomElements], search);
|
|
166
|
-
this.#target.removeAttribute('temp_id');
|
|
167
|
-
return mcValues;
|
|
168
|
-
}
|
|
169
|
-
// all descendant DOM nodes, all puclic MCs
|
|
170
|
-
descendants(search = '') {
|
|
171
|
-
if (!isDOM(this.#target))
|
|
172
|
-
return [];
|
|
173
|
-
const myDomElements = this.#target.querySelectorAll('*[_mc]');
|
|
174
|
-
return getMicroComponents([...myDomElements], search);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
'use strict';function getMicroComponents(elements,search){let mcValues=[];elements.forEach(e=>{if(e){if(e instanceof HTMLElement){const _mc=e?._mc;if(!search&&_mc){mcValues=mcValues.concat(Object.values(e._mc));}
|
|
2
|
-
else if(search&&_mc&&_mc[search]){mcValues.push(_mc[search]);}}}});return mcValues;}
|
|
3
|
-
function traverseNodes(node,search,firstonly=true){let mcValues=[];while(node){if(node._mc){if(!search){mcValues=mcValues.concat(Object.values(node._mc));}
|
|
4
|
-
else if(search&&node._mc[search]){mcValues.push(node._mc[search]);}
|
|
5
|
-
if(firstonly)
|
|
6
|
-
return mcValues;}
|
|
7
|
-
node=node.parentNode;}
|
|
8
|
-
return mcValues;}
|
|
9
|
-
function isDOM(element){return element&&element instanceof HTMLElement;}
|
|
10
|
-
export class MC{#target={};#_mc={};static debug=false;constructor(target){this.#target=target;this.#_mc={};}
|
|
11
|
-
get _mc(){if(!MC.debug)
|
|
12
|
-
return undefined;return this.#_mc;}
|
|
13
|
-
get target(){return this.#target;}
|
|
14
|
-
static siblings(target,search=''){const mc=target;if(!mc._mc){return[];}
|
|
15
|
-
const _mc=mc._mc;if(search.length){return _mc?.[search]||[];}
|
|
16
|
-
return[...Object.values(_mc)];}
|
|
17
|
-
static add(target,key,element={}){let node=target;if(!node._mc){node._mc=new MC(target);}
|
|
18
|
-
const _mc=node._mc;if(key.length&&!_mc[key]){_mc[key]=element;if(target instanceof HTMLElement){target.setAttribute('_mc','');}}
|
|
19
|
-
return element;}
|
|
20
|
-
static remove(target,key){if(!key)
|
|
21
|
-
return;const _mc=target?._mc||{};if(_mc&&_mc[key]){delete _mc[key];}
|
|
22
|
-
if(!Object.keys(_mc).length&&target instanceof HTMLElement&&target.hasAttribute('_mc')){target.removeAttribute('_mc');}
|
|
23
|
-
return;}
|
|
24
|
-
add(key,element,target){if(this.#_mc[key])
|
|
25
|
-
return;if(target){MC.add(target,key,element);}
|
|
26
|
-
this.#_mc[key]=element;return element;}
|
|
27
|
-
remove(key,target){const _mc=this.#_mc;const t=target;if(target){MC.remove(t,key);}
|
|
28
|
-
delete _mc?.[key];}
|
|
29
|
-
siblings(search=''){const _mc=this.#_mc;if(!_mc)
|
|
30
|
-
return[];const result=_mc[search||''];return search&&result?[result]:[...Object.values(_mc)];}
|
|
31
|
-
parent(search=''){if(!isDOM(this.#target))
|
|
32
|
-
return[];return traverseNodes(this.#target.parentNode,search,true);}
|
|
33
|
-
parents(search=''){if(!isDOM(this.#target))
|
|
34
|
-
return[];return traverseNodes(this.#target.parentNode,search,false);}
|
|
35
|
-
children(search=''){if(!isDOM(this.#target))
|
|
36
|
-
return[];const id=Math.random().toString().replace('.','');const selector='[_mc]:not([temp_id="'+id+'"] [_mc] [_mc])';this.#target.setAttribute('temp_id',id);const myDomElements=this.#target.querySelectorAll(selector);const mcValues=getMicroComponents([...myDomElements],search);this.#target.removeAttribute('temp_id');return mcValues;}
|
|
37
|
-
descendants(search=''){if(!isDOM(this.#target))
|
|
38
|
-
return[];const myDomElements=this.#target.querySelectorAll('*[_mc]');return getMicroComponents([...myDomElements],search);}}
|
package/dist/microcomponents.ts
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
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
|
-
}
|
|
26
|
-
|
|
27
|
-
// get all ancestor DOM nodes up to the document
|
|
28
|
-
function traverseNodes(node: { [key:string]: any }, search: string, firstonly = true): any[] {
|
|
29
|
-
let mcValues: any[] = [];
|
|
30
|
-
while (node) {
|
|
31
|
-
if (node._mc) {
|
|
32
|
-
if (!search) {
|
|
33
|
-
mcValues = mcValues.concat(Object.values(node._mc));
|
|
34
|
-
}
|
|
35
|
-
else if (search && node._mc[search]) {
|
|
36
|
-
mcValues.push(node._mc[search]);
|
|
37
|
-
}
|
|
38
|
-
if (firstonly)
|
|
39
|
-
return mcValues;
|
|
40
|
-
}
|
|
41
|
-
node = node.parentNode;
|
|
42
|
-
}
|
|
43
|
-
return mcValues;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function isDOM(element: { [key:string]: any } ) {
|
|
47
|
-
return element && element instanceof HTMLElement;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
type HTMLElementMC = HTMLElement & { _mc: { [key:string]: any } }
|
|
51
|
-
|
|
52
|
-
export class MC {
|
|
53
|
-
|
|
54
|
-
#target: { [key:string]: any } = {};
|
|
55
|
-
|
|
56
|
-
#_mc: { [key:string]: any } = {};
|
|
57
|
-
|
|
58
|
-
static debug: boolean = false;
|
|
59
|
-
|
|
60
|
-
constructor( target: { [key:string]: any } ) {
|
|
61
|
-
this.#target = target;
|
|
62
|
-
this.#_mc = {};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
get _mc(): undefined | any {
|
|
66
|
-
if ( !MC.debug ) return undefined;
|
|
67
|
-
return this.#_mc;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
get target(): any {
|
|
71
|
-
return this.#target;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// return all top-level micro components from an HTMLElement, or the searched one
|
|
75
|
-
static siblings( target: HTMLElementMC, search: string = '' ): any[] {
|
|
76
|
-
const mc = target as any;
|
|
77
|
-
|
|
78
|
-
if (!mc._mc) {
|
|
79
|
-
return [];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const _mc = mc._mc;
|
|
83
|
-
|
|
84
|
-
if ( search.length ){
|
|
85
|
-
return _mc?.[search] || [];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return [ ...Object.values(_mc) ];
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// add a microcomponent to a target DOM node
|
|
92
|
-
static add( target: HTMLElement, key: string, element = {} ): any {
|
|
93
|
-
|
|
94
|
-
let node = target as HTMLElementMC;
|
|
95
|
-
|
|
96
|
-
// create the _mc property if necessary
|
|
97
|
-
if (!node._mc) {
|
|
98
|
-
node._mc = new MC( target );
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const _mc = node._mc;
|
|
102
|
-
|
|
103
|
-
// add the element with the given property key to the _mc property
|
|
104
|
-
if ( key.length && !_mc[key]) {
|
|
105
|
-
_mc[key] = element;
|
|
106
|
-
// add attribute if HTMLElement
|
|
107
|
-
if ( target instanceof HTMLElement ) {
|
|
108
|
-
target.setAttribute('_mc', '');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
return element;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// remove a micro component from the DOM
|
|
115
|
-
static remove( target: HTMLElementMC, key: string) {
|
|
116
|
-
if ( !key ) return; // silent fail
|
|
117
|
-
|
|
118
|
-
const _mc = target?._mc || {};
|
|
119
|
-
|
|
120
|
-
// delete the property from the HTMLElement _mc collection
|
|
121
|
-
if ( _mc && _mc[key] ) {
|
|
122
|
-
delete _mc[key];
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// remove the _mc attribute if the DOM collection is empty
|
|
126
|
-
if ( !Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('_mc') ) {
|
|
127
|
-
target.removeAttribute('_mc');
|
|
128
|
-
}
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// add a microcomponent to the collection
|
|
133
|
-
add( key: string, element: any, target?: HTMLElement ): any {
|
|
134
|
-
if ( this.#_mc[key] ) return;
|
|
135
|
-
|
|
136
|
-
// if target parameter exists ( a DOM node )
|
|
137
|
-
// you want a top level (public) MC
|
|
138
|
-
if ( target ){
|
|
139
|
-
// ... add the new micro component to the DOM node _mc
|
|
140
|
-
MC.add( target, key, element );
|
|
141
|
-
}
|
|
142
|
-
// add the new micro component to the private collection anyway
|
|
143
|
-
this.#_mc[key] = element;
|
|
144
|
-
|
|
145
|
-
return element;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// remove a micro component from the collection
|
|
149
|
-
remove( key: string, target?: HTMLElement ): void {
|
|
150
|
-
const _mc = this.#_mc;
|
|
151
|
-
const t = target as HTMLElementMC;
|
|
152
|
-
|
|
153
|
-
// if target parameter exists ( a DOM node )
|
|
154
|
-
// you want to remove a top level (public) MC
|
|
155
|
-
if ( target ){
|
|
156
|
-
// ... remove the new micro component from the DOM node _mc
|
|
157
|
-
MC.remove( t, key );
|
|
158
|
-
}
|
|
159
|
-
// remove the new micro component from the private collection anyway
|
|
160
|
-
delete _mc?.[key];
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// return all micro components from the collection, or the searched one
|
|
164
|
-
// in a top level DOM MC, this refers to the inner collection always
|
|
165
|
-
// use MC.siblings() to get the public MCs
|
|
166
|
-
siblings( search: string = '' ): any[] {
|
|
167
|
-
const _mc = this.#_mc;
|
|
168
|
-
|
|
169
|
-
if ( !_mc ) return [];
|
|
170
|
-
|
|
171
|
-
const result = _mc[search || ''];
|
|
172
|
-
|
|
173
|
-
return search && result ? [result] : [ ...Object.values(_mc) ];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/*
|
|
177
|
-
the following selectors will only work for the DOM top level MCs and return [] otherwise
|
|
178
|
-
*/
|
|
179
|
-
|
|
180
|
-
// first anchestor DOM node, all public MCs
|
|
181
|
-
parent( search: string = '' ): any[] {
|
|
182
|
-
if ( !isDOM(this.#target) ) return [];
|
|
183
|
-
|
|
184
|
-
return traverseNodes( (this as any).#target.parentNode, search, true);
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
// all anchestor DOM nodes, beginning with the closest, all public MCs
|
|
188
|
-
parents( search: string = '' ): any[] {
|
|
189
|
-
if ( !isDOM(this.#target) ) return [];
|
|
190
|
-
|
|
191
|
-
return traverseNodes( (this as any).#target.parentNode, search, false);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// nearest child DOM nodes, all puclic MCs
|
|
195
|
-
children( search: string = '' ): any[] {
|
|
196
|
-
if ( !isDOM(this.#target) ) return [];
|
|
197
|
-
|
|
198
|
-
const id = Math.random().toString().replace('.','');
|
|
199
|
-
const selector = '[_mc]:not([temp_id="' + id +'"] [_mc] [_mc])'; // HOTFIX: minifier or tsc compiler error, dont use backtick string, cuts out blanks
|
|
200
|
-
this.#target.setAttribute('temp_id', id);
|
|
201
|
-
const myDomElements = this.#target.querySelectorAll(selector);
|
|
202
|
-
const mcValues = getMicroComponents([...myDomElements], search);
|
|
203
|
-
this.#target.removeAttribute('temp_id');
|
|
204
|
-
|
|
205
|
-
return mcValues;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
// all descendant DOM nodes, all puclic MCs
|
|
209
|
-
descendants( search: string = '' ): any[] {
|
|
210
|
-
if ( !isDOM(this.#target) ) return [];
|
|
211
|
-
|
|
212
|
-
const myDomElements = this.#target.querySelectorAll('*[_mc]');
|
|
213
|
-
|
|
214
|
-
return getMicroComponents([...myDomElements], search);
|
|
215
|
-
}
|
|
216
|
-
}
|