@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
package/public/MC.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { McEvent } from './McEvent.js';
|
|
3
|
+
var isBrowser = new Function("try {return this===window;}catch(e){ return false;}");
|
|
4
|
+
let nativeEventNames = new Set();
|
|
5
|
+
// HTMLElement native event names
|
|
6
|
+
if (isBrowser()) {
|
|
7
|
+
Object.keys(HTMLElement.prototype).filter(key => /^on/.test(key)).forEach(eventName => nativeEventNames.add(eventName));
|
|
8
|
+
}
|
|
9
|
+
// get all microcomponents from a list of HTMLElements
|
|
10
|
+
function getMicroComponents(elements, search) {
|
|
11
|
+
let mcValues = [];
|
|
12
|
+
elements.forEach(e => {
|
|
13
|
+
if (e) {
|
|
14
|
+
if (e instanceof HTMLElement) {
|
|
15
|
+
const _mc = e?._mc;
|
|
16
|
+
if (!search && _mc) {
|
|
17
|
+
mcValues = mcValues.concat(Object.values(e._mc));
|
|
18
|
+
}
|
|
19
|
+
else if (search && _mc && _mc[search]) {
|
|
20
|
+
mcValues.push(_mc[search]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
return mcValues;
|
|
26
|
+
}
|
|
27
|
+
// get all ancestor DOM nodes up to the document root
|
|
28
|
+
function traverseNodes(node, search, firstonly = true) {
|
|
29
|
+
let mcValues = [];
|
|
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
|
+
function isDOM(element) {
|
|
46
|
+
return element && element instanceof HTMLElement;
|
|
47
|
+
}
|
|
48
|
+
function addListener(domNode, eventName, handler, capture = false, once = false) {
|
|
49
|
+
let options = { once: false };
|
|
50
|
+
if (capture) {
|
|
51
|
+
options.capture = capture;
|
|
52
|
+
}
|
|
53
|
+
if (once) {
|
|
54
|
+
options.once = once;
|
|
55
|
+
}
|
|
56
|
+
//console.log( 'addEventListener', eventName, ( handler as Function ).name, options );
|
|
57
|
+
domNode.addEventListener(eventName, handler, options);
|
|
58
|
+
}
|
|
59
|
+
function removeListener(domNode, eventName, handler) {
|
|
60
|
+
domNode.removeEventListener(eventName, handler);
|
|
61
|
+
}
|
|
62
|
+
// add a microcomponent to a target DOM node
|
|
63
|
+
function add(target, key, element = {}) {
|
|
64
|
+
let node = target;
|
|
65
|
+
// create the _mc property if necessary
|
|
66
|
+
if (!node._mc) {
|
|
67
|
+
let _MC = MC, debug = MC?.debug;
|
|
68
|
+
MC.debug = false;
|
|
69
|
+
node._mc = new MC(target);
|
|
70
|
+
MC.debug = debug;
|
|
71
|
+
}
|
|
72
|
+
const _mc = node._mc;
|
|
73
|
+
// add the element with the given property key to the _mc property
|
|
74
|
+
if (key.length && !_mc[key]) {
|
|
75
|
+
_mc[key] = element;
|
|
76
|
+
// add attribute if HTMLElement
|
|
77
|
+
if (target instanceof HTMLElement) {
|
|
78
|
+
target.setAttribute('_mc', '');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return element;
|
|
82
|
+
}
|
|
83
|
+
// remove a micro component from the DOM
|
|
84
|
+
function remove(target, key) {
|
|
85
|
+
if (!key)
|
|
86
|
+
return; // silent fail
|
|
87
|
+
const _mc = target?._mc || {};
|
|
88
|
+
// delete the property from the HTMLElement _mc collection
|
|
89
|
+
if (_mc && _mc[key]) {
|
|
90
|
+
delete _mc[key];
|
|
91
|
+
}
|
|
92
|
+
// remove the _mc attribute if the DOM collection is empty
|
|
93
|
+
if (!Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('_mc')) {
|
|
94
|
+
target.removeAttribute('_mc');
|
|
95
|
+
}
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
// trigger an event
|
|
99
|
+
function trigger(target, ev, data = {}) {
|
|
100
|
+
let _mc = target?._mc;
|
|
101
|
+
if (!_mc)
|
|
102
|
+
return;
|
|
103
|
+
[...Object.values(_mc)].forEach((mc) => {
|
|
104
|
+
if (mc?.trigger && typeof mc?.trigger === 'function') {
|
|
105
|
+
mc?.trigger(ev, data);
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
class McBase {
|
|
111
|
+
#target; // avoid circular reference
|
|
112
|
+
#_mc = {};
|
|
113
|
+
constructor(target) {
|
|
114
|
+
let that = this;
|
|
115
|
+
this.#target = new WeakRef(target);
|
|
116
|
+
if (this.constructor !== MC && this.constructor !== DC) {
|
|
117
|
+
Object.defineProperty(this, "_mc", {
|
|
118
|
+
get() {
|
|
119
|
+
return this.#_mc;
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
get target() {
|
|
125
|
+
return this.#target.deref();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
class MC extends McBase {
|
|
129
|
+
constructor(target) {
|
|
130
|
+
super(target);
|
|
131
|
+
let that = this;
|
|
132
|
+
if (that.constructor === MC || that.constructor === DC)
|
|
133
|
+
return;
|
|
134
|
+
setTimeout(() => {
|
|
135
|
+
that.trigger('init');
|
|
136
|
+
}, 0);
|
|
137
|
+
}
|
|
138
|
+
trigger(ev, data = {}, bubble) {
|
|
139
|
+
let that = this;
|
|
140
|
+
bubble = bubble || 'l';
|
|
141
|
+
// run all triggers
|
|
142
|
+
if (that.constructor === MC || that.constructor === DC) {
|
|
143
|
+
[...Object.getOwnPropertyNames(that)].forEach((mc) => {
|
|
144
|
+
if (that[mc]?.trigger && typeof that[mc]?.trigger === 'function') {
|
|
145
|
+
that[mc]?.trigger(ev, data, bubble);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
return; // must return here!
|
|
149
|
+
}
|
|
150
|
+
// only this instance
|
|
151
|
+
if (that instanceof MC || that instanceof DC) {
|
|
152
|
+
const that = this, mcEvent = typeof ev !== 'string' ? ev : new McEvent(ev, data, bubble), handlerName = 'on' + mcEvent.type[0].toUpperCase() + mcEvent.type.slice(1);
|
|
153
|
+
;
|
|
154
|
+
// console.log('trigger', handlerName, mcEvent, data, bubble );
|
|
155
|
+
if (mcEvent.bubble.indexOf('l') > -1) {
|
|
156
|
+
if (!mcEvent.immediateStopped
|
|
157
|
+
&& typeof that[handlerName] === 'function') {
|
|
158
|
+
// console.log( 'execute handler:', handlerName );
|
|
159
|
+
that[handlerName](mcEvent);
|
|
160
|
+
if (that.hasOwnProperty(handlerName)) { // it is a once handler
|
|
161
|
+
delete that[handlerName]; // remove after first execution
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// if event stopped or local only, handling is cancelled
|
|
166
|
+
if (mcEvent.stopped || mcEvent.bubble === 'l') {
|
|
167
|
+
return that;
|
|
168
|
+
}
|
|
169
|
+
// at this point, either 'u' or 'd' bubbling should be indicated
|
|
170
|
+
// mcEvent bubbling is asynchronous, and the order of target MC execution in case of multiple matches is not guaranteed!
|
|
171
|
+
if (isBrowser() && /[ud]/.test(mcEvent.bubble) && that instanceof MC) {
|
|
172
|
+
setTimeout(() => {
|
|
173
|
+
// bubbling must include 'l' for local, or else no handlers are executed'
|
|
174
|
+
if (mcEvent.bubble.indexOf('l') === -1) {
|
|
175
|
+
mcEvent.bubble += 'l';
|
|
176
|
+
}
|
|
177
|
+
// bubble up towards root
|
|
178
|
+
if (mcEvent.bubble.indexOf('u') > -1) {
|
|
179
|
+
that
|
|
180
|
+
.parent()
|
|
181
|
+
.forEach((mc) => {
|
|
182
|
+
mc instanceof MC ? mc.trigger(mcEvent) : null;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
// bubble down
|
|
186
|
+
if (mcEvent.bubble.indexOf('d') > -1) {
|
|
187
|
+
that
|
|
188
|
+
.children()
|
|
189
|
+
.forEach((mc) => {
|
|
190
|
+
mc instanceof MC ? mc.trigger(mcEvent) : null;
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}, 0);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
;
|
|
199
|
+
class DC extends MC {
|
|
200
|
+
constructor(target) {
|
|
201
|
+
super(target);
|
|
202
|
+
}
|
|
203
|
+
static debug = false;
|
|
204
|
+
static add = add;
|
|
205
|
+
static remove = remove;
|
|
206
|
+
static trigger = trigger;
|
|
207
|
+
parent(search = '') {
|
|
208
|
+
let target = this.target;
|
|
209
|
+
if (!isDOM(target))
|
|
210
|
+
return [];
|
|
211
|
+
return traverseNodes(target.parentNode, search, true);
|
|
212
|
+
}
|
|
213
|
+
ancestors(search = '') {
|
|
214
|
+
let target = this?.target;
|
|
215
|
+
if (!isDOM(target))
|
|
216
|
+
return [];
|
|
217
|
+
return traverseNodes(this.target.parentNode, search, false);
|
|
218
|
+
}
|
|
219
|
+
children(search = '') {
|
|
220
|
+
let target = this?.target;
|
|
221
|
+
if (!isDOM(target))
|
|
222
|
+
return [];
|
|
223
|
+
const id = Math.random().toString().replace('.', '');
|
|
224
|
+
const selector = '[_mc]:not([temp_id="' + id + '"] [_mc] [_mc])';
|
|
225
|
+
target.setAttribute('temp_id', id);
|
|
226
|
+
const myDomElements = target.querySelectorAll(selector);
|
|
227
|
+
const mcValues = getMicroComponents([...myDomElements], search);
|
|
228
|
+
target.removeAttribute('temp_id');
|
|
229
|
+
return mcValues;
|
|
230
|
+
}
|
|
231
|
+
descendants(search = '') {
|
|
232
|
+
let target = this?.target;
|
|
233
|
+
if (!isDOM(target))
|
|
234
|
+
return [];
|
|
235
|
+
const myDomElements = target.querySelectorAll('*[_mc]');
|
|
236
|
+
return getMicroComponents([...myDomElements], search);
|
|
237
|
+
}
|
|
238
|
+
autoAttachListeners() {
|
|
239
|
+
if (!isBrowser())
|
|
240
|
+
return;
|
|
241
|
+
const target = this.target;
|
|
242
|
+
if (target instanceof HTMLElement) {
|
|
243
|
+
let that = this;
|
|
244
|
+
Object
|
|
245
|
+
.getOwnPropertyNames(that.constructor.prototype)
|
|
246
|
+
.filter(key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function')
|
|
247
|
+
.forEach(key => {
|
|
248
|
+
let once = /^one/.test(key), withoutOnOne = key.replace(/^on[e]{0,1}/, ''), // preserves upper/lowercase for non-native events
|
|
249
|
+
nativeEventName = withoutOnOne.toLowerCase(), listener = (ev) => { that.trigger(withoutOnOne, ev); };
|
|
250
|
+
;
|
|
251
|
+
// console.log('autoAttach', nativeEventName, withoutOnOne, once, listener);
|
|
252
|
+
if (once) {
|
|
253
|
+
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
254
|
+
Object.defineProperty(this, 'on' + withoutOnOne, {
|
|
255
|
+
configurable: true,
|
|
256
|
+
enumerable: false,
|
|
257
|
+
writable: true,
|
|
258
|
+
value: (ev) => {
|
|
259
|
+
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
260
|
+
(that.constructor.prototype[key]).bind(that)(ev);
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
if (nativeEventNames.has('on' + nativeEventName)) { // native event defined in HTMLElement
|
|
265
|
+
// console.log('added as native listener', target, nativeEventName );
|
|
266
|
+
addListener(target, nativeEventName, listener, false, once);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
;
|
|
273
|
+
export { addListener, removeListener, McBase, MC, DC, McEvent };
|
package/public/MC.min.js
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
(function webpackUniversalModuleDefinition(root, factory) {
|
|
2
|
+
if(typeof exports === 'object' && typeof module === 'object')
|
|
3
|
+
module.exports = factory();
|
|
4
|
+
else if(typeof define === 'function' && define.amd)
|
|
5
|
+
define([], factory);
|
|
6
|
+
else if(typeof exports === 'object')
|
|
7
|
+
exports["MC"] = factory();
|
|
8
|
+
else
|
|
9
|
+
root["MC"] = factory();
|
|
10
|
+
})(this, () => {
|
|
11
|
+
return /******/ (() => { // webpackBootstrap
|
|
12
|
+
/******/ "use strict";
|
|
13
|
+
/******/ // The require scope
|
|
14
|
+
/******/ var __webpack_require__ = {};
|
|
15
|
+
/******/
|
|
16
|
+
/************************************************************************/
|
|
17
|
+
/******/ /* webpack/runtime/define property getters */
|
|
18
|
+
/******/ (() => {
|
|
19
|
+
/******/ // define getter functions for harmony exports
|
|
20
|
+
/******/ __webpack_require__.d = (exports, definition) => {
|
|
21
|
+
/******/ for(var key in definition) {
|
|
22
|
+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
|
|
23
|
+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
|
|
24
|
+
/******/ }
|
|
25
|
+
/******/ }
|
|
26
|
+
/******/ };
|
|
27
|
+
/******/ })();
|
|
28
|
+
/******/
|
|
29
|
+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
|
|
30
|
+
/******/ (() => {
|
|
31
|
+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
|
|
32
|
+
/******/ })();
|
|
33
|
+
/******/
|
|
34
|
+
/******/ /* webpack/runtime/make namespace object */
|
|
35
|
+
/******/ (() => {
|
|
36
|
+
/******/ // define __esModule on exports
|
|
37
|
+
/******/ __webpack_require__.r = (exports) => {
|
|
38
|
+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
|
|
39
|
+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
40
|
+
/******/ }
|
|
41
|
+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
|
|
42
|
+
/******/ };
|
|
43
|
+
/******/ })();
|
|
44
|
+
/******/
|
|
45
|
+
/************************************************************************/
|
|
46
|
+
var __webpack_exports__ = {};
|
|
47
|
+
// ESM COMPAT FLAG
|
|
48
|
+
__webpack_require__.r(__webpack_exports__);
|
|
49
|
+
|
|
50
|
+
// EXPORTS
|
|
51
|
+
__webpack_require__.d(__webpack_exports__, {
|
|
52
|
+
DC: () => (/* binding */ DC),
|
|
53
|
+
MC: () => (/* binding */ MC),
|
|
54
|
+
McBase: () => (/* binding */ McBase),
|
|
55
|
+
McEvent: () => (/* reexport */ McEvent),
|
|
56
|
+
addListener: () => (/* binding */ addListener),
|
|
57
|
+
removeListener: () => (/* binding */ removeListener)
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
;// ./build/McEvent.js
|
|
61
|
+
|
|
62
|
+
var isBrowser = new Function("try {return this===window;}catch(e){ return false;}");
|
|
63
|
+
let nativeEventNames = new Set();
|
|
64
|
+
// HTMLElement native event names
|
|
65
|
+
if (isBrowser()) {
|
|
66
|
+
Object.keys(HTMLElement.prototype).filter(key => /^on/.test(key)).forEach(eventName => nativeEventNames.add(eventName));
|
|
67
|
+
}
|
|
68
|
+
function isDOM(element) {
|
|
69
|
+
return element && element instanceof HTMLElement;
|
|
70
|
+
}
|
|
71
|
+
class McEvent {
|
|
72
|
+
#type;
|
|
73
|
+
#stopped = false;
|
|
74
|
+
#immediateStopped = false;
|
|
75
|
+
constructor(type, data = {}, bubble = 'l') {
|
|
76
|
+
// bubble standard = local only, options = any combination of'uld' characters: up to root, local, down
|
|
77
|
+
this.#type = type;
|
|
78
|
+
this.data = data;
|
|
79
|
+
this.bubble = bubble;
|
|
80
|
+
}
|
|
81
|
+
get type() {
|
|
82
|
+
return this.#type;
|
|
83
|
+
}
|
|
84
|
+
get stopped() {
|
|
85
|
+
return this.#stopped;
|
|
86
|
+
}
|
|
87
|
+
get immediateStopped() {
|
|
88
|
+
return this.#immediateStopped;
|
|
89
|
+
}
|
|
90
|
+
stopPropagation() {
|
|
91
|
+
this.#stopped = true;
|
|
92
|
+
}
|
|
93
|
+
stopImmediatePropagation() {
|
|
94
|
+
this.stopPropagation();
|
|
95
|
+
this.#immediateStopped = true;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// get all microcomponents from a list of HTMLElements
|
|
99
|
+
function getMicroComponents(elements, search) {
|
|
100
|
+
let mcValues = [];
|
|
101
|
+
elements.forEach(e => {
|
|
102
|
+
if (e) {
|
|
103
|
+
if (e instanceof HTMLElement) {
|
|
104
|
+
const _mc = e?._mc;
|
|
105
|
+
if (!search && _mc) {
|
|
106
|
+
mcValues = mcValues.concat(Object.values(e._mc));
|
|
107
|
+
}
|
|
108
|
+
else if (search && _mc && _mc[search]) {
|
|
109
|
+
mcValues.push(_mc[search]);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
return mcValues;
|
|
115
|
+
}
|
|
116
|
+
// get all ancestor DOM nodes up to the document root
|
|
117
|
+
function traverseNodes(node, search, firstonly = true) {
|
|
118
|
+
let mcValues = [];
|
|
119
|
+
while (node) {
|
|
120
|
+
if (node._mc) {
|
|
121
|
+
if (!search) {
|
|
122
|
+
mcValues = mcValues.concat(Object.values(node._mc));
|
|
123
|
+
}
|
|
124
|
+
else if (search && node._mc[search]) {
|
|
125
|
+
mcValues.push(node._mc[search]);
|
|
126
|
+
}
|
|
127
|
+
if (firstonly)
|
|
128
|
+
return mcValues;
|
|
129
|
+
}
|
|
130
|
+
node = node.parentNode;
|
|
131
|
+
}
|
|
132
|
+
return mcValues;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
;// ./build/MC.js
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
var MC_isBrowser = new Function("try {return this===window;}catch(e){ return false;}");
|
|
140
|
+
let MC_nativeEventNames = new Set();
|
|
141
|
+
// HTMLElement native event names
|
|
142
|
+
if (MC_isBrowser()) {
|
|
143
|
+
Object.keys(HTMLElement.prototype).filter(key => /^on/.test(key)).forEach(eventName => MC_nativeEventNames.add(eventName));
|
|
144
|
+
}
|
|
145
|
+
// get all microcomponents from a list of HTMLElements
|
|
146
|
+
function MC_getMicroComponents(elements, search) {
|
|
147
|
+
let mcValues = [];
|
|
148
|
+
elements.forEach(e => {
|
|
149
|
+
if (e) {
|
|
150
|
+
if (e instanceof HTMLElement) {
|
|
151
|
+
const _mc = e?._mc;
|
|
152
|
+
if (!search && _mc) {
|
|
153
|
+
mcValues = mcValues.concat(Object.values(e._mc));
|
|
154
|
+
}
|
|
155
|
+
else if (search && _mc && _mc[search]) {
|
|
156
|
+
mcValues.push(_mc[search]);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return mcValues;
|
|
162
|
+
}
|
|
163
|
+
// get all ancestor DOM nodes up to the document root
|
|
164
|
+
function MC_traverseNodes(node, search, firstonly = true) {
|
|
165
|
+
let mcValues = [];
|
|
166
|
+
while (node) {
|
|
167
|
+
if (node._mc) {
|
|
168
|
+
if (!search) {
|
|
169
|
+
mcValues = mcValues.concat(Object.values(node._mc));
|
|
170
|
+
}
|
|
171
|
+
else if (search && node._mc[search]) {
|
|
172
|
+
mcValues.push(node._mc[search]);
|
|
173
|
+
}
|
|
174
|
+
if (firstonly)
|
|
175
|
+
return mcValues;
|
|
176
|
+
}
|
|
177
|
+
node = node.parentNode;
|
|
178
|
+
}
|
|
179
|
+
return mcValues;
|
|
180
|
+
}
|
|
181
|
+
function MC_isDOM(element) {
|
|
182
|
+
return element && element instanceof HTMLElement;
|
|
183
|
+
}
|
|
184
|
+
function addListener(domNode, eventName, handler, capture = false, once = false) {
|
|
185
|
+
let options = { once: false };
|
|
186
|
+
if (capture) {
|
|
187
|
+
options.capture = capture;
|
|
188
|
+
}
|
|
189
|
+
if (once) {
|
|
190
|
+
options.once = once;
|
|
191
|
+
}
|
|
192
|
+
//console.log( 'addEventListener', eventName, ( handler as Function ).name, options );
|
|
193
|
+
domNode.addEventListener(eventName, handler, options);
|
|
194
|
+
}
|
|
195
|
+
function removeListener(domNode, eventName, handler) {
|
|
196
|
+
domNode.removeEventListener(eventName, handler);
|
|
197
|
+
}
|
|
198
|
+
// add a microcomponent to a target DOM node
|
|
199
|
+
function add(target, key, element = {}) {
|
|
200
|
+
let node = target;
|
|
201
|
+
// create the _mc property if necessary
|
|
202
|
+
if (!node._mc) {
|
|
203
|
+
let _MC = MC, debug = MC?.debug;
|
|
204
|
+
MC.debug = false;
|
|
205
|
+
node._mc = new MC(target);
|
|
206
|
+
MC.debug = debug;
|
|
207
|
+
}
|
|
208
|
+
const _mc = node._mc;
|
|
209
|
+
// add the element with the given property key to the _mc property
|
|
210
|
+
if (key.length && !_mc[key]) {
|
|
211
|
+
_mc[key] = element;
|
|
212
|
+
// add attribute if HTMLElement
|
|
213
|
+
if (target instanceof HTMLElement) {
|
|
214
|
+
target.setAttribute('_mc', '');
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return element;
|
|
218
|
+
}
|
|
219
|
+
// remove a micro component from the DOM
|
|
220
|
+
function remove(target, key) {
|
|
221
|
+
if (!key)
|
|
222
|
+
return; // silent fail
|
|
223
|
+
const _mc = target?._mc || {};
|
|
224
|
+
// delete the property from the HTMLElement _mc collection
|
|
225
|
+
if (_mc && _mc[key]) {
|
|
226
|
+
delete _mc[key];
|
|
227
|
+
}
|
|
228
|
+
// remove the _mc attribute if the DOM collection is empty
|
|
229
|
+
if (!Object.keys(_mc).length && target instanceof HTMLElement && target.hasAttribute('_mc')) {
|
|
230
|
+
target.removeAttribute('_mc');
|
|
231
|
+
}
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
// trigger an event
|
|
235
|
+
function trigger(target, ev, data = {}) {
|
|
236
|
+
let _mc = target?._mc;
|
|
237
|
+
if (!_mc)
|
|
238
|
+
return;
|
|
239
|
+
[...Object.values(_mc)].forEach((mc) => {
|
|
240
|
+
if (mc?.trigger && typeof mc?.trigger === 'function') {
|
|
241
|
+
mc?.trigger(ev, data);
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
class McBase {
|
|
247
|
+
#target; // avoid circular reference
|
|
248
|
+
#_mc = {};
|
|
249
|
+
constructor(target) {
|
|
250
|
+
let that = this;
|
|
251
|
+
this.#target = new WeakRef(target);
|
|
252
|
+
if (this.constructor !== MC && this.constructor !== DC) {
|
|
253
|
+
Object.defineProperty(this, "_mc", {
|
|
254
|
+
get() {
|
|
255
|
+
return this.#_mc;
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
get target() {
|
|
261
|
+
return this.#target.deref();
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
class MC extends McBase {
|
|
265
|
+
constructor(target) {
|
|
266
|
+
super(target);
|
|
267
|
+
let that = this;
|
|
268
|
+
if (that.constructor === MC || that.constructor === DC)
|
|
269
|
+
return;
|
|
270
|
+
setTimeout(() => {
|
|
271
|
+
that.trigger('init');
|
|
272
|
+
}, 0);
|
|
273
|
+
}
|
|
274
|
+
trigger(ev, data = {}, bubble) {
|
|
275
|
+
let that = this;
|
|
276
|
+
bubble = bubble || 'l';
|
|
277
|
+
// run all triggers
|
|
278
|
+
if (that.constructor === MC || that.constructor === DC) {
|
|
279
|
+
[...Object.getOwnPropertyNames(that)].forEach((mc) => {
|
|
280
|
+
if (that[mc]?.trigger && typeof that[mc]?.trigger === 'function') {
|
|
281
|
+
that[mc]?.trigger(ev, data, bubble);
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
return; // must return here!
|
|
285
|
+
}
|
|
286
|
+
// only this instance
|
|
287
|
+
if (that instanceof MC || that instanceof DC) {
|
|
288
|
+
const that = this, mcEvent = typeof ev !== 'string' ? ev : new McEvent(ev, data, bubble), handlerName = 'on' + mcEvent.type[0].toUpperCase() + mcEvent.type.slice(1);
|
|
289
|
+
;
|
|
290
|
+
// console.log('trigger', handlerName, mcEvent, data, bubble );
|
|
291
|
+
if (mcEvent.bubble.indexOf('l') > -1) {
|
|
292
|
+
if (!mcEvent.immediateStopped
|
|
293
|
+
&& typeof that[handlerName] === 'function') {
|
|
294
|
+
// console.log( 'execute handler:', handlerName );
|
|
295
|
+
that[handlerName](mcEvent);
|
|
296
|
+
if (that.hasOwnProperty(handlerName)) { // it is a once handler
|
|
297
|
+
delete that[handlerName]; // remove after first execution
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// if event stopped or local only, handling is cancelled
|
|
302
|
+
if (mcEvent.stopped || mcEvent.bubble === 'l') {
|
|
303
|
+
return that;
|
|
304
|
+
}
|
|
305
|
+
// at this point, either 'u' or 'd' bubbling should be indicated
|
|
306
|
+
// mcEvent bubbling is asynchronous, and the order of target MC execution in case of multiple matches is not guaranteed!
|
|
307
|
+
if (MC_isBrowser() && /[ud]/.test(mcEvent.bubble) && that instanceof MC) {
|
|
308
|
+
setTimeout(() => {
|
|
309
|
+
// bubbling must include 'l' for local, or else no handlers are executed'
|
|
310
|
+
if (mcEvent.bubble.indexOf('l') === -1) {
|
|
311
|
+
mcEvent.bubble += 'l';
|
|
312
|
+
}
|
|
313
|
+
// bubble up towards root
|
|
314
|
+
if (mcEvent.bubble.indexOf('u') > -1) {
|
|
315
|
+
that
|
|
316
|
+
.parent()
|
|
317
|
+
.forEach((mc) => {
|
|
318
|
+
mc instanceof MC ? mc.trigger(mcEvent) : null;
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
// bubble down
|
|
322
|
+
if (mcEvent.bubble.indexOf('d') > -1) {
|
|
323
|
+
that
|
|
324
|
+
.children()
|
|
325
|
+
.forEach((mc) => {
|
|
326
|
+
mc instanceof MC ? mc.trigger(mcEvent) : null;
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
}, 0);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
;
|
|
335
|
+
class DC extends MC {
|
|
336
|
+
constructor(target) {
|
|
337
|
+
super(target);
|
|
338
|
+
}
|
|
339
|
+
static debug = false;
|
|
340
|
+
static add = add;
|
|
341
|
+
static remove = remove;
|
|
342
|
+
static trigger = trigger;
|
|
343
|
+
parent(search = '') {
|
|
344
|
+
let target = this.target;
|
|
345
|
+
if (!MC_isDOM(target))
|
|
346
|
+
return [];
|
|
347
|
+
return MC_traverseNodes(target.parentNode, search, true);
|
|
348
|
+
}
|
|
349
|
+
ancestors(search = '') {
|
|
350
|
+
let target = this?.target;
|
|
351
|
+
if (!MC_isDOM(target))
|
|
352
|
+
return [];
|
|
353
|
+
return MC_traverseNodes(this.target.parentNode, search, false);
|
|
354
|
+
}
|
|
355
|
+
children(search = '') {
|
|
356
|
+
let target = this?.target;
|
|
357
|
+
if (!MC_isDOM(target))
|
|
358
|
+
return [];
|
|
359
|
+
const id = Math.random().toString().replace('.', '');
|
|
360
|
+
const selector = '[_mc]:not([temp_id="' + id + '"] [_mc] [_mc])';
|
|
361
|
+
target.setAttribute('temp_id', id);
|
|
362
|
+
const myDomElements = target.querySelectorAll(selector);
|
|
363
|
+
const mcValues = MC_getMicroComponents([...myDomElements], search);
|
|
364
|
+
target.removeAttribute('temp_id');
|
|
365
|
+
return mcValues;
|
|
366
|
+
}
|
|
367
|
+
descendants(search = '') {
|
|
368
|
+
let target = this?.target;
|
|
369
|
+
if (!MC_isDOM(target))
|
|
370
|
+
return [];
|
|
371
|
+
const myDomElements = target.querySelectorAll('*[_mc]');
|
|
372
|
+
return MC_getMicroComponents([...myDomElements], search);
|
|
373
|
+
}
|
|
374
|
+
autoAttachListeners() {
|
|
375
|
+
if (!MC_isBrowser())
|
|
376
|
+
return;
|
|
377
|
+
const target = this.target;
|
|
378
|
+
if (target instanceof HTMLElement) {
|
|
379
|
+
let that = this;
|
|
380
|
+
Object
|
|
381
|
+
.getOwnPropertyNames(that.constructor.prototype)
|
|
382
|
+
.filter(key => /^on[A-Z]|one[A-Z]/.test(key) && typeof that.constructor.prototype[key] === 'function')
|
|
383
|
+
.forEach(key => {
|
|
384
|
+
let once = /^one/.test(key), withoutOnOne = key.replace(/^on[e]{0,1}/, ''), // preserves upper/lowercase for non-native events
|
|
385
|
+
nativeEventName = withoutOnOne.toLowerCase(), listener = (ev) => { that.trigger(withoutOnOne, ev); };
|
|
386
|
+
;
|
|
387
|
+
// console.log('autoAttach', nativeEventName, withoutOnOne, once, listener);
|
|
388
|
+
if (once) {
|
|
389
|
+
// console.log( '...is only called once', that.constructor.prototype[key] );
|
|
390
|
+
Object.defineProperty(this, 'on' + withoutOnOne, {
|
|
391
|
+
configurable: true,
|
|
392
|
+
enumerable: false,
|
|
393
|
+
writable: true,
|
|
394
|
+
value: (ev) => {
|
|
395
|
+
// console.log( 'onceHandler execute:', that.constructor.prototype[key] );
|
|
396
|
+
(that.constructor.prototype[key]).bind(that)(ev);
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
if (MC_nativeEventNames.has('on' + nativeEventName)) { // native event defined in HTMLElement
|
|
401
|
+
// console.log('added as native listener', target, nativeEventName );
|
|
402
|
+
addListener(target, nativeEventName, listener, false, once);
|
|
403
|
+
}
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
;
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
/******/ return __webpack_exports__;
|
|
412
|
+
/******/ })()
|
|
413
|
+
;
|
|
414
|
+
});
|