@twobirds/microcomponents 0.0.46 → 0.0.50
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/assets/Grandparent.css +13 -0
- package/assets/documentation/MC.md +380 -166
- package/assets/test.html +5 -1
- package/dist/MC.d.ts +1 -0
- package/dist/MC.d.ts.map +1 -1
- package/dist/MC.min.js +44 -0
- package/dist/MC.ts +65 -0
- package/dist/elements.min.js +1 -1
- package/dist/elements.ts +10 -10
- package/dist/formValues.d.ts +3 -2
- package/dist/formValues.d.ts.map +1 -1
- package/dist/formValues.ts +206 -203
- package/dist/formvValues.min.js +1 -1
- package/dist/helpers.d.ts +1 -0
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.ts +9 -0
- package/dist/microcomponents.min.js +1 -1
- package/dist/observables.d.ts +1 -1
- package/dist/observables.d.ts.map +1 -1
- package/dist/observables.min.js +1 -1
- package/dist/observables.ts +72 -33
- package/dist/selectors.min.js +1 -1
- package/dist/test.ts +14 -14
- package/package.json +1 -1
- package/public/Grandparent.css +13 -0
- package/public/Grandparent.js +338 -0
- package/public/MC.js +43 -0
- package/public/MC.js.map +1 -1
- package/public/MC.min.js +44 -0
- package/public/TestForm.css +47 -0
- package/public/TestForm.html +56 -0
- package/public/TestForm.js +56 -0
- package/public/TestForm.json +27 -0
- package/public/Testform.js +149 -0
- package/public/classes/MC.DC.html +13 -13
- package/public/classes/MC.MC.html +7 -7
- package/public/classes/MC.McBase.html +4 -4
- package/public/classes/MC.McEvent.html +13 -13
- package/public/documentation/MC.md +380 -166
- package/public/documentation/observe.md +317 -0
- package/public/elements.js +9 -9
- package/public/elements.js.map +1 -1
- package/public/elements.min.js +1 -1
- package/public/formValues.js +142 -133
- package/public/formValues.js.map +1 -1
- package/public/formvValues.min.js +1 -1
- package/public/functions/MC.addListener.html +1 -1
- package/public/functions/MC.removeListener.html +1 -1
- package/public/functions/elements.defineCE.html +1 -1
- package/public/functions/elements.getCE.html +1 -1
- package/public/functions/formValues.formValues.html +1 -1
- package/public/functions/observables.observe.html +1 -1
- package/public/functions/selectors._d.html +1 -1
- package/public/functions/selectors._t.html +1 -1
- package/public/helpers.js +8 -0
- package/public/helpers.js.map +1 -1
- package/public/interfaces/MC.HTMLMcElement.html +2 -2
- package/public/interfaces/selectors.DSelector.html +55 -55
- package/public/interfaces/selectors.TSelector.html +37 -37
- package/public/microcomponents.min.js +1 -1
- package/public/modules/MC.html +1 -1
- package/public/modules/elements.html +1 -1
- package/public/modules/formValues.html +1 -1
- package/public/modules/observables.html +1 -1
- package/public/modules/selectors.html +1 -1
- package/public/observables.js +39 -21
- package/public/observables.js.map +1 -1
- package/public/observables.min.js +1 -1
- package/public/selectors.min.js +1 -1
- package/public/src/MC.ts +65 -0
- package/public/src/elements.ts +10 -10
- package/public/src/formValues.ts +206 -203
- package/public/src/helpers.ts +9 -0
- package/public/src/observables.ts +72 -33
- package/public/src/test.ts +14 -14
- package/public/test/Grandparent.js +1 -0
- package/public/test/Testform.js +1 -0
- package/public/test.html +5 -1
- package/public/test.js +9 -11
- package/public/test.js.map +1 -1
- package/public/types/observables.Observable.html +1 -1
- package/public/types/selectors.selection.html +1 -1
- package/public/types/selectors.singleSelector.html +1 -1
- package/src/MC.ts +65 -0
- package/src/elements.ts +10 -10
- package/src/formValues.ts +206 -203
- package/src/helpers.ts +9 -0
- package/src/observables.ts +72 -33
- package/src/test/Child.ts +21 -0
- package/src/test/Grandchild.ts +21 -0
- package/src/test/Grandparent.css +11 -0
- package/src/test/Grandparent.ts +21 -0
- package/src/test/Greatgrandchild.ts +42 -0
- package/src/test/Parent.ts +21 -0
- package/src/test/Testform.css +47 -0
- package/src/test/Testform.html +56 -0
- package/src/test/Testform.json +27 -0
- package/src/test/Testform.ts +159 -0
- package/src/test.ts +14 -14
- package/webpack.config.js +135 -61
|
@@ -2,11 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
LooseObject,
|
|
5
|
-
parse,
|
|
6
5
|
debounce,
|
|
7
6
|
deepEqual,
|
|
8
|
-
nameSpace,
|
|
9
7
|
flattenObject,
|
|
8
|
+
copyGettersSetters,
|
|
10
9
|
} from './helpers.js';
|
|
11
10
|
|
|
12
11
|
import { DC, HTMLMcElement, McEvent } from './MC.js';
|
|
@@ -76,9 +75,10 @@ class Placeholders extends DC {
|
|
|
76
75
|
|
|
77
76
|
#attachCallbacks() {
|
|
78
77
|
let that = this,
|
|
79
|
-
target = that.target
|
|
78
|
+
target = that.target,
|
|
79
|
+
hide: boolean = false;
|
|
80
80
|
|
|
81
|
-
//
|
|
81
|
+
//console.log('attachCallbacks', that.target);
|
|
82
82
|
|
|
83
83
|
// scan attributes
|
|
84
84
|
target
|
|
@@ -116,6 +116,7 @@ class Placeholders extends DC {
|
|
|
116
116
|
);
|
|
117
117
|
})
|
|
118
118
|
.forEach((childNode) => {
|
|
119
|
+
hide = true;
|
|
119
120
|
let phs: Set<string> = new Set(
|
|
120
121
|
childNode.nodeValue.match(/\{[^\{\}]*\}/g)
|
|
121
122
|
);
|
|
@@ -124,6 +125,7 @@ class Placeholders extends DC {
|
|
|
124
125
|
that.data.observe(
|
|
125
126
|
((template: string) => (data: LooseObject) => {
|
|
126
127
|
let result: string = template;
|
|
128
|
+
//console.log('text node "', template, '" callbach data:', data);
|
|
127
129
|
phs.forEach((placeholder) => {
|
|
128
130
|
result = result.replace(
|
|
129
131
|
placeholder,
|
|
@@ -135,7 +137,11 @@ class Placeholders extends DC {
|
|
|
135
137
|
return !!data[key];
|
|
136
138
|
})
|
|
137
139
|
) {
|
|
138
|
-
|
|
140
|
+
if (!!that.oldDisplay) {
|
|
141
|
+
target.style = that.oldDisplay;
|
|
142
|
+
} else {
|
|
143
|
+
target.removeAttribute('style');
|
|
144
|
+
}
|
|
139
145
|
delete that.oldDisplay;
|
|
140
146
|
}
|
|
141
147
|
childNode.nodeValue = result;
|
|
@@ -144,7 +150,11 @@ class Placeholders extends DC {
|
|
|
144
150
|
|
|
145
151
|
// hide for now
|
|
146
152
|
that.oldDisplay = target.getAttribute('style)') || '';
|
|
147
|
-
if (
|
|
153
|
+
if (
|
|
154
|
+
hide &&
|
|
155
|
+
!inputs.includes((target as any).constructor) &&
|
|
156
|
+
target.tagName !== 'FIELDSET'
|
|
157
|
+
) {
|
|
148
158
|
target.style.display = 'none';
|
|
149
159
|
}
|
|
150
160
|
});
|
|
@@ -156,7 +166,7 @@ class Placeholders extends DC {
|
|
|
156
166
|
Object.keys(ev.data).forEach((key) => {
|
|
157
167
|
if (that.data.hasOwnProperty(key)) data[key] = ev.data[key];
|
|
158
168
|
});
|
|
159
|
-
//
|
|
169
|
+
//console.log('onData()', ev, data, that.data);
|
|
160
170
|
if (JSON.stringify(data) !== JSON.stringify(that.data))
|
|
161
171
|
Object.assign(that.data, data); // will trigger callbacks
|
|
162
172
|
}
|
|
@@ -169,13 +179,16 @@ function getPlaceholderElements(
|
|
|
169
179
|
target instanceof HTMLElement ? [target] : []
|
|
170
180
|
)
|
|
171
181
|
.concat([...(target.querySelectorAll('*') as unknown as HTMLElement[])])
|
|
172
|
-
.filter((e) => !!getPlaceholders(e as HTMLElement).length)
|
|
182
|
+
.filter((e) => !!getPlaceholders(e as HTMLElement).length)
|
|
183
|
+
.filter((e) => e.tagName !== 'STYLE');
|
|
184
|
+
//console.log('getPlaceholderElements', elements);
|
|
173
185
|
return elements as HTMLElement[];
|
|
174
186
|
}
|
|
175
187
|
|
|
176
|
-
function
|
|
188
|
+
function makePlaceholders(
|
|
177
189
|
element: HTMLElement | DocumentFragment | ShadowRoot
|
|
178
190
|
): void {
|
|
191
|
+
//console.log('makePlaceholders', element);
|
|
179
192
|
getPlaceholderElements(element).forEach((e: HTMLElement) => {
|
|
180
193
|
if ((e as HTMLMcElement)?._mc?._placeholders) return;
|
|
181
194
|
DC.add(e, '_placeholders', new Placeholders(e));
|
|
@@ -188,14 +201,17 @@ export type Observable = {
|
|
|
188
201
|
bind: (target: HTMLElement | DocumentFragment | ShadowRoot) => void;
|
|
189
202
|
};
|
|
190
203
|
|
|
191
|
-
export const observe = (
|
|
204
|
+
export const observe = (
|
|
205
|
+
target: LooseObject,
|
|
206
|
+
propertyName?: string
|
|
207
|
+
): LooseObject => {
|
|
192
208
|
let keys = !propertyName
|
|
193
209
|
? [...Object.getOwnPropertyNames(target)]
|
|
194
210
|
: [propertyName];
|
|
195
211
|
|
|
196
212
|
keys.forEach((key) => {
|
|
197
213
|
// console.log( 'co propname', key, typeof target[key], target[key] );
|
|
198
|
-
if (key[0] === '_' || target?.[key] === undefined) return;
|
|
214
|
+
if (key[0] === '_' || target?.[key] === undefined) return target;
|
|
199
215
|
let val: any = target[key],
|
|
200
216
|
Constructor: any;
|
|
201
217
|
|
|
@@ -223,6 +239,8 @@ export const observe = (target: LooseObject, propertyName?: string) => {
|
|
|
223
239
|
}
|
|
224
240
|
|
|
225
241
|
let Observable = class extends Constructor {
|
|
242
|
+
#notify: boolean = true;
|
|
243
|
+
|
|
226
244
|
constructor(val: any) {
|
|
227
245
|
super(val);
|
|
228
246
|
(this.constructor as LooseObject).callbacks = [];
|
|
@@ -246,16 +264,22 @@ export const observe = (target: LooseObject, propertyName?: string) => {
|
|
|
246
264
|
//console.log('bind', iso, target);
|
|
247
265
|
|
|
248
266
|
if (iso) {
|
|
249
|
-
|
|
250
|
-
|
|
267
|
+
makePlaceholders(target);
|
|
268
|
+
const placeholders: unknown = [
|
|
269
|
+
...target.querySelectorAll('[_mc]'),
|
|
270
|
+
].filter((e) => !!(e as LooseObject)?._mc?._placeholders);
|
|
271
|
+
//console.log('placeholders:', target, placeholders);
|
|
272
|
+
const func = function callback(data: LooseObject) {
|
|
251
273
|
// avoid bubbling for speed
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
274
|
+
let d = flattenObject(data);
|
|
275
|
+
//console.log('inside callback', target, d);
|
|
276
|
+
(placeholders as DC[]).forEach((e) => {
|
|
277
|
+
// console.log('trigger callback', e);
|
|
278
|
+
if (!e) return;
|
|
279
|
+
(e as any)._mc.trigger('data', d);
|
|
280
|
+
});
|
|
281
|
+
};
|
|
282
|
+
(that.constructor as any).callbacks.push(func);
|
|
259
283
|
that.notify();
|
|
260
284
|
} else {
|
|
261
285
|
console.warn(
|
|
@@ -268,22 +292,34 @@ export const observe = (target: LooseObject, propertyName?: string) => {
|
|
|
268
292
|
return this;
|
|
269
293
|
}
|
|
270
294
|
|
|
271
|
-
notify() {
|
|
295
|
+
notify(notify?: boolean) {
|
|
272
296
|
let that = this,
|
|
273
297
|
data =
|
|
274
298
|
isObservableObject(that.valueOf()) &&
|
|
275
299
|
that.valueOf() instanceof Array === false
|
|
276
300
|
? structuredClone(Object.assign({}, that))
|
|
277
301
|
: that.valueOf();
|
|
302
|
+
|
|
303
|
+
if (notify !== undefined) {
|
|
304
|
+
that.#notify = notify;
|
|
305
|
+
// return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
//if (that.#notify) console.log('notify', this, data);
|
|
278
309
|
(that.constructor as any).callbacks.forEach((f: Function) => {
|
|
279
|
-
// console.log(
|
|
310
|
+
// console.log(
|
|
311
|
+
// (this.constructor as any).callbacks,
|
|
312
|
+
// 'notify: exec callback',
|
|
313
|
+
// f,
|
|
314
|
+
// data
|
|
315
|
+
// );
|
|
280
316
|
f(data);
|
|
281
317
|
});
|
|
282
|
-
|
|
318
|
+
|
|
319
|
+
return that;
|
|
283
320
|
}
|
|
284
321
|
};
|
|
285
322
|
|
|
286
|
-
//(Observable.constructor as LooseObject).callbacks = [];
|
|
287
323
|
let o = new Observable(val);
|
|
288
324
|
|
|
289
325
|
Object.defineProperty(target, key, {
|
|
@@ -311,20 +347,21 @@ export const observe = (target: LooseObject, propertyName?: string) => {
|
|
|
311
347
|
let iso =
|
|
312
348
|
isObservableObject(o.valueOf()) &&
|
|
313
349
|
o.valueOf() instanceof Array === false;
|
|
314
|
-
//let iso = o.constructor.prototype.__proto__.constructor === Object;
|
|
315
350
|
//console.log('target iso:', iso);
|
|
316
351
|
if (iso) {
|
|
317
352
|
let old = structuredClone(Object.assign({}, o));
|
|
318
|
-
val = structuredClone(Object.assign({}, val));
|
|
353
|
+
//val = structuredClone(Object.assign({}, val));
|
|
319
354
|
|
|
320
|
-
let
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
355
|
+
//let callbacks = (o.constructor as LooseObject).callbacks;
|
|
356
|
+
if (o instanceof Observable === false) {
|
|
357
|
+
o = new Observable(structuredClone(Object.assign({}, val)));
|
|
358
|
+
copyGettersSetters(val, o);
|
|
359
|
+
o.seal();
|
|
360
|
+
}
|
|
361
|
+
Object.assign(o, val);
|
|
324
362
|
|
|
325
|
-
//console.log('after set o=', o, 'check', old);
|
|
326
|
-
(o.constructor as LooseObject).callbacks =
|
|
327
|
-
(o.constructor as LooseObject).callbacks = callbacks;
|
|
363
|
+
// console.log('after set o=', o, 'check', old);
|
|
364
|
+
// (o.constructor as LooseObject).callbacks = callbacks;
|
|
328
365
|
|
|
329
366
|
setTimeout(
|
|
330
367
|
// check for changes
|
|
@@ -363,4 +400,6 @@ export const observe = (target: LooseObject, propertyName?: string) => {
|
|
|
363
400
|
// replace old property with proxy (observable)
|
|
364
401
|
target[key] = val;
|
|
365
402
|
});
|
|
403
|
+
|
|
404
|
+
return target;
|
|
366
405
|
};
|
package/public/src/test.ts
CHANGED
|
@@ -42,21 +42,18 @@ class Translator extends DC {
|
|
|
42
42
|
|
|
43
43
|
(that.Translation as Observable).bind(translationTargetObject);
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
that.target.setAttribute('language', 'en');
|
|
46
46
|
|
|
47
|
-
(
|
|
47
|
+
(that.Language as Observable).notify();
|
|
48
48
|
|
|
49
49
|
delete that._mc; // cleanup, not needed
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
onAttributeChanged(ev: McEvent) {
|
|
53
53
|
if (!ev.data.newValue) return;
|
|
54
|
-
|
|
55
|
-
let that = this;
|
|
56
|
-
|
|
57
54
|
switch (ev.data.name) {
|
|
58
55
|
case 'language':
|
|
59
|
-
|
|
56
|
+
this.Language = ev.data.newValue;
|
|
60
57
|
break;
|
|
61
58
|
}
|
|
62
59
|
}
|
|
@@ -112,10 +109,10 @@ defineCE(
|
|
|
112
109
|
|
|
113
110
|
onClick(ev: McEvent) {
|
|
114
111
|
const that = this,
|
|
115
|
-
c =
|
|
112
|
+
c = that.Dom.counter++;
|
|
116
113
|
(ev.data as PointerEvent).stopPropagation();
|
|
117
|
-
(
|
|
118
|
-
(
|
|
114
|
+
(that.target as HTMLElement).classList.remove('clicked');
|
|
115
|
+
(that.target as HTMLElement).classList.add(
|
|
119
116
|
that.Dom.counter > 9 ? 'overClicked' : 'clicked'
|
|
120
117
|
);
|
|
121
118
|
}
|
|
@@ -130,10 +127,11 @@ let MyTag: CustomElementConstructor = getCE('my-tag')!,
|
|
|
130
127
|
stress test
|
|
131
128
|
*/
|
|
132
129
|
|
|
133
|
-
let start = new Date().getTime()
|
|
130
|
+
let start = new Date().getTime(),
|
|
131
|
+
max = 100;
|
|
134
132
|
|
|
135
|
-
// create
|
|
136
|
-
for (let x = 0; x <
|
|
133
|
+
// create 1000 elements
|
|
134
|
+
for (let x = 0; x < max; x++) {
|
|
137
135
|
fragment.append(new MyTag());
|
|
138
136
|
}
|
|
139
137
|
|
|
@@ -152,14 +150,16 @@ setTimeout(() => {
|
|
|
152
150
|
// console.clear();
|
|
153
151
|
|
|
154
152
|
console.log(
|
|
155
|
-
'%c \n\nStress Test:\n\n %cInstancing
|
|
153
|
+
'%c \n\nStress Test:\n\n %cInstancing %s web components (%s classes): %c %s ms.',
|
|
156
154
|
'font-weight: bold; color: black;',
|
|
157
155
|
'font-weight: normal;',
|
|
156
|
+
max,
|
|
157
|
+
max * 3,
|
|
158
158
|
'font-weight: bold; color: blue;',
|
|
159
159
|
end - start
|
|
160
160
|
);
|
|
161
161
|
|
|
162
|
-
document.body.appendChild(new MyTag());
|
|
162
|
+
//document.body.appendChild(new MyTag());
|
|
163
163
|
|
|
164
164
|
let element = document.querySelector('my-tag');
|
|
165
165
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.TestGrandparent=e():t.TestGrandparent=e()}(self,(()=>(()=>{"use strict";var t={d:(e,r)=>{for(var n in r)t.o(r,n)&&!t.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:r[n]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{TestGrandparent:()=>S});var r=new Function("try {return this===window;}catch(e){ return false;}");let n=new Set;r()&&Object.keys(HTMLElement.prototype).filter((t=>/^on/.test(t))).forEach((t=>n.add(t)));class o{#t;#e=!1;#r=!1;constructor(t,e={},r="l"){this.#t=t,this.data=e,this.bubble=r}get type(){return this.#t}get stopped(){return this.#e}get immediateStopped(){return this.#r}stopPropagation(){this.#e=!0}stopImmediatePropagation(){this.stopPropagation(),this.#r=!0}}var s=new Function("try {return this===window;}catch(e){ return false;}");let c=new Set;function i(t,e){let r=[];return t.forEach((t=>{if(t&&t instanceof HTMLElement){const n=t?._mc;!e&&n?r=r.concat(Object.values(t._mc)):e&&n&&n[e]&&r.push(n[e])}})),r}function a(t,e,r=!0){let n=[];for(;t;){if(t._mc&&(e?e&&t._mc[e]&&n.push(t._mc[e]):n=n.concat(Object.values(t._mc)),r))return n;t=t.parentNode}return n}function l(t){return t&&t instanceof HTMLElement}function u(t,e,r={}){let n=t;if(!n._mc){let e=b?.debug;b.debug=!1,n._mc=new b(t),b.debug=e,delete n._mc._mc}const o=n._mc;return e.length&&!o[e]&&(o[e]=r,t instanceof HTMLElement&&t.setAttribute("_mc","")),r}function d(t,e){if(!e)return;const r=t?._mc||{};r&&r[e]&&delete r[e],!Object.keys(r).length&&t instanceof HTMLElement&&t.hasAttribute("_mc")&&t.removeAttribute("_mc")}function p(t,e,r={},n="l"){let s=t?._mc;if(!s)return;let c=e instanceof o?new o(e.type,e.data,"l"):new o(e,r,"l"),i="string"!=typeof e?e:new o(e,r,n);[...Object.values(s)].forEach((t=>{t?.trigger&&"function"==typeof t?.trigger&&t?.trigger(c)})),/[ud]/.test(i.bubble)&&g(t,i)}function g(t,e){/[ud]/.test(e.bubble)&&setTimeout((()=>{if(-1===e.bubble.indexOf("l")&&(e.bubble+="l"),e.bubble.indexOf("u")>-1){let r=a(t,"",!0)?.[0].target;r&&b.trigger(r,e)}e.bubble.indexOf("d")>-1&&new Set(t._mc.children().map((t=>t?.target))).forEach((t=>{t&&b.trigger(t,e)}))}),0)}s()&&Object.keys(HTMLElement.prototype).filter((t=>/^on/.test(t))).forEach((t=>c.add(t)));class f{#n;constructor(t,e=!1){var r;this.#n=new WeakRef(t||{}),(r=this).constructor!==b&&setTimeout((()=>{r.trigger("Init")}),0),function(t){if(!s()||t.constructor===b)return;const e=t.target;if(e instanceof HTMLElement){let r=t;Object.getOwnPropertyNames(r.constructor.prototype).filter((t=>/^on[A-Z]|one[A-Z]/.test(t)&&"function"==typeof r.constructor.prototype[t])).forEach((n=>{let o=/^one/.test(n),s=n.replace(/^on[e]{0,1}/,""),i=s.toLowerCase();o&&Object.defineProperty(t,"on"+s,{configurable:!0,enumerable:!1,writable:!0,value:t=>{r.constructor.prototype[n].bind(r)(t)}}),c.has("on"+i)&&function(t,e,r,n=!1,o=!1){let s={once:!1};n&&(s.capture=n),o&&(s.once=o),t.addEventListener(e,r,s)}(e,i,(t=>{r.trigger(s,t)}),!1,o)}))}}(this)}get target(){return this.#n.deref()}}class m extends f{_mc={};constructor(t){super(t)}static trigger=p;trigger(t,e={},r="l"){let n=this;if(n.constructor===m||n.constructor===b){let s=t instanceof o?new o(t.type,t.data,"l"):new o(t,e,"l"),c="string"!=typeof t?t:new o(t,e,r);return[...Object.getOwnPropertyNames(n)].forEach((t=>{n[t]?.trigger&&"function"==typeof n[t]?.trigger&&n[t]?.trigger(s)})),void(/[ud]/.test(c.bubble)&&g(n.target,c))}if(n instanceof m||n instanceof b){const n=this,s="string"!=typeof t?t:new o(t,e,r),c="on"+s.type[0].toUpperCase()+s.type.slice(1);if(s.bubble.indexOf("l")>-1&&(s.immediateStopped||"function"!=typeof n[c]||(n[c](s),n.hasOwnProperty(c)&&delete n[c])),s.stopped||"l"===s.bubble)return n;/[ud]/.test(s.bubble)&&g(n.target,s)}}}class b extends f{_mc={};constructor(t,e=!1){super(t)}static add=u;static remove=d;static trigger=p;trigger(t,e={},r){m.prototype.trigger.call(this,t,e,r)}parent(t=""){let e=this.target;return l(e)?a(e.parentNode,t,!0):[]}ancestors(t=""){let e=this?.target;return l(e)?a(this.target.parentNode,t,!1):[]}children(t=""){let e=this?.target;if(!l(e))return[];const r=Math.random().toString().replace(".",""),n='[_mc]:not([temp_id="'+r+'"] [_mc] [_mc])';e.setAttribute("temp_id",r);const o=i([...e.querySelectorAll(n)],t);return e.removeAttribute("temp_id"),o}descendants(t=""){let e=this?.target;return l(e)?i([...e.querySelectorAll("*[_mc]")],t):[]}}new Map;class h extends HTMLElement{constructor(){super()}connectedCallback(){b.trigger(this,"Connected")}disconnectedCallback(){b.trigger(this,"Disconnected")}adoptedCallback(){b.trigger(this,"Adopted")}attributeChangedCallback(t,e,r){b.trigger(this,"AttributeChanged",{name:t,oldValue:e,newValue:r})}}function y(t,e,r=[]){if(customElements.get(t))return void console.warn(`IGNORED: Custom element "${t}" is already defined!`);if(!t.includes("-"))throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');let n=t.split("-").map((t=>t.charAt(0).toUpperCase()+t.slice(1))).join("");customElements.define(t,function(t,e){return Object.defineProperty(e,"name",{value:t})}(n,class extends h{instance;constructor(t){super();let r=new e(this,t);b.add(this,n,r)}static get observedAttributes(){return r}}))}function w(t){return customElements.get(t)}function _(t,e){return Math.floor(Math.random()*(e-t+1)+t)}y("test-greatgrandchild",class extends b{constructor(t){super(t)}onConnected(){this.updateStyle()}updateStyle(){this.target.setAttribute("style","border-color:rgb("+_(0,255)+","+_(0,255)+","+_(0,255)+")")}});const O=w("test-greatgrandchild");y("test-grandchild",class extends b{constructor(t){super(t)}onConnected(){let t=this;for(let e=0;e<2;e++)t.target.append(new O)}});const E=w("test-grandchild");y("test-child",class extends b{constructor(t){super(t)}onConnected(){let t=this;for(let e=0;e<10;e++)t.target.append(new E)}});const v=w("test-child");y("test-parent",class extends b{constructor(t){super(t)}onConnected(){let t=this;for(let e=0;e<5;e++)t.target.append(new v)}});const j=w("test-parent");y("test-grandparent",class extends b{constructor(t){super(t)}onConnected(){let t=this;for(let e=0;e<5;e++)t.target.append(new j)}});const S=w("test-grandparent");return e})()));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.TestTestform=t():e.TestTestform=t()}(self,(()=>(()=>{"use strict";var e=new Function("try {return this===window;}catch(e){ return false;}");let t=new Set;e()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>t.add(e)));class n{#e;#t=!1;#n=!1;constructor(e,t={},n="l"){this.#e=e,this.data=t,this.bubble=n}get type(){return this.#e}get stopped(){return this.#t}get immediateStopped(){return this.#n}stopPropagation(){this.#t=!0}stopImmediatePropagation(){this.stopPropagation(),this.#n=!0}}var a=new Function("try {return this===window;}catch(e){ return false;}");let r=new Set;function l(e,t){let n=[];return e.forEach((e=>{if(e&&e instanceof HTMLElement){const a=e?._mc;!t&&a?n=n.concat(Object.values(e._mc)):t&&a&&a[t]&&n.push(a[t])}})),n}function o(e,t,n=!0){let a=[];for(;e;){if(e._mc&&(t?t&&e._mc[t]&&a.push(e._mc[t]):a=a.concat(Object.values(e._mc)),n))return a;e=e.parentNode}return a}function i(e){return e&&e instanceof HTMLElement}function s(e,t,n,a=!1,r=!1){let l={once:!1};a&&(l.capture=a),r&&(l.once=r),e.addEventListener(t,n,l)}function c(e,t,n={}){let a=e;if(!a._mc){let t=m?.debug;m.debug=!1,a._mc=new m(e),m.debug=t,delete a._mc._mc}const r=a._mc;return t.length&&!r[t]&&(r[t]=n,e instanceof HTMLElement&&e.setAttribute("_mc","")),n}function u(e,t){if(!t)return;const n=e?._mc||{};n&&n[t]&&delete n[t],!Object.keys(n).length&&e instanceof HTMLElement&&e.hasAttribute("_mc")&&e.removeAttribute("_mc")}function p(e,t,a={},r="l"){let l=e?._mc;if(!l)return;let o=t instanceof n?new n(t.type,t.data,"l"):new n(t,a,"l"),i="string"!=typeof t?t:new n(t,a,r);[...Object.values(l)].forEach((e=>{e?.trigger&&"function"==typeof e?.trigger&&e?.trigger(o)})),/[ud]/.test(i.bubble)&&d(e,i)}function d(e,t){/[ud]/.test(t.bubble)&&setTimeout((()=>{if(-1===t.bubble.indexOf("l")&&(t.bubble+="l"),t.bubble.indexOf("u")>-1){let n=o(e,"",!0)?.[0].target;n&&m.trigger(n,t)}t.bubble.indexOf("d")>-1&&new Set(e._mc.children().map((e=>e?.target))).forEach((e=>{e&&m.trigger(e,t)}))}),0)}a()&&Object.keys(HTMLElement.prototype).filter((e=>/^on/.test(e))).forEach((e=>r.add(e)));class b{#a;constructor(e,t=!1){var n;this.#a=new WeakRef(e||{}),(n=this).constructor!==m&&setTimeout((()=>{n.trigger("Init")}),0),function(e){if(!a()||e.constructor===m)return;const t=e.target;if(t instanceof HTMLElement){let n=e;Object.getOwnPropertyNames(n.constructor.prototype).filter((e=>/^on[A-Z]|one[A-Z]/.test(e)&&"function"==typeof n.constructor.prototype[e])).forEach((a=>{let l=/^one/.test(a),o=a.replace(/^on[e]{0,1}/,""),i=o.toLowerCase();l&&Object.defineProperty(e,"on"+o,{configurable:!0,enumerable:!1,writable:!0,value:e=>{n.constructor.prototype[a].bind(n)(e)}}),r.has("on"+i)&&s(t,i,(e=>{n.trigger(o,e)}),!1,l)}))}}(this)}get target(){return this.#a.deref()}}class f extends b{_mc={};constructor(e){super(e)}static trigger=p;trigger(e,t={},a="l"){let r=this;if(r.constructor===f||r.constructor===m){let l=e instanceof n?new n(e.type,e.data,"l"):new n(e,t,"l"),o="string"!=typeof e?e:new n(e,t,a);return[...Object.getOwnPropertyNames(r)].forEach((e=>{r[e]?.trigger&&"function"==typeof r[e]?.trigger&&r[e]?.trigger(l)})),void(/[ud]/.test(o.bubble)&&d(r.target,o))}if(r instanceof f||r instanceof m){const r=this,l="string"!=typeof e?e:new n(e,t,a),o="on"+l.type[0].toUpperCase()+l.type.slice(1);if(l.bubble.indexOf("l")>-1&&(l.immediateStopped||"function"!=typeof r[o]||(r[o](l),r.hasOwnProperty(o)&&delete r[o])),l.stopped||"l"===l.bubble)return r;/[ud]/.test(l.bubble)&&d(r.target,l)}}}class m extends b{_mc={};constructor(e,t=!1){super(e)}static add=c;static remove=u;static trigger=p;trigger(e,t={},n){f.prototype.trigger.call(this,e,t,n)}parent(e=""){let t=this.target;return i(t)?o(t.parentNode,e,!0):[]}ancestors(e=""){let t=this?.target;return i(t)?o(this.target.parentNode,e,!1):[]}children(e=""){let t=this?.target;if(!i(t))return[];const n=Math.random().toString().replace(".",""),a='[_mc]:not([temp_id="'+n+'"] [_mc] [_mc])';t.setAttribute("temp_id",n);const r=l([...t.querySelectorAll(a)],e);return t.removeAttribute("temp_id"),r}descendants(e=""){let t=this?.target;return i(t)?l([...t.querySelectorAll("*[_mc]")],e):[]}}function g(e){let t={};return[...Object.keys(e)].sort().forEach((n=>{var a;(a=e[n])&&a.constructor&&a.constructor===Object?t[n]=g(e[n]):t[n]=e[n]})),t}function h(e,t=!0){let n={};return[...Object.getOwnPropertyNames(e)].forEach((t=>{if(e[t]&&e[t].constructor&&e[t].constructor===Object){const a=h(e[t],!1);for(const e in a)n[t+"."+e]=a[e]}else n[t]=e[t]})),t?g(n):n}function y(e,t){return JSON.stringify(h(e))===JSON.stringify(h(t))}function v(e,t){let n;return()=>{clearTimeout(n),n=setTimeout((function(){e(...arguments)}),t)}}function O(e,t){const n=Object.getOwnPropertyDescriptors(e);for(const[e,a]of Object.entries(n))("get"in a||"set"in a)&&Object.defineProperty(t,e,a)}new Map;class w extends HTMLElement{constructor(){super()}connectedCallback(){m.trigger(this,"Connected")}disconnectedCallback(){m.trigger(this,"Disconnected")}adoptedCallback(){m.trigger(this,"Adopted")}attributeChangedCallback(e,t,n){m.trigger(this,"AttributeChanged",{name:e,oldValue:t,newValue:n})}}function x(e){return null!=e&&"object"==typeof e}function E(e){let t=new Set,n=/\{[^\{\}]*\}/g;return e.getAttributeNames().filter((t=>e.getAttribute(t)?.match(n))).forEach((a=>{new Set(e.getAttribute(a)?.match(n)).forEach((e=>t.add(e.replace(/[\{\}]/g,""))))})),[...e.childNodes].filter((e=>3===e.nodeType&&(e.nodeValue||"").match(n))).forEach((e=>{(e.nodeValue||"").match(n).forEach((e=>{t.add(e.replace(/[\{\}]/g,""))}))})),[...t].map((e=>e.replace(/[\{\}]/g,""))),[...t]}const k=[HTMLInputElement,HTMLSelectElement,HTMLTextAreaElement];class A extends m{data={};oldDisplay="";constructor(e){super(e);let t=this;E(e).forEach((e=>t.data[e]="")),j(t),t.#r(),delete t._mc}#r(){let e=this,t=e.target,n=!1;t.getAttributeNames().filter((e=>t.getAttribute(e)?.match(/\{[^\{\}]*\}/g))).forEach((n=>{let a=new Set(t.getAttribute(n)?.match(/\{[^\{\}]*\}/g));var r;e.data.observe((r=t.getAttribute(n),e=>{let l=r;a.forEach((t=>{l=l.replace(t,e[t.replace(/[\{\}]/g,"")])})),t.setAttribute(n,l)}))})),[...t.childNodes].filter((e=>3===e.nodeType&&(e.nodeValue||"").match(/\{[^\{\}]*\}/g))).forEach((a=>{n=!0;let r=new Set(a.nodeValue.match(/\{[^\{\}]*\}/g));var l;e.data.observe((l=a.nodeValue,n=>{let o=l;r.forEach((e=>{o=o.replace(e,n[e.replace(/[\{\}]/g,"")])})),Object.keys(n).some((e=>!!n[e]))&&(e.oldDisplay?t.style=e.oldDisplay:t.removeAttribute("style"),delete e.oldDisplay),a.nodeValue=o})),e.oldDisplay=t.getAttribute("style)")||"",n&&!k.includes(t.constructor)&&"FIELDSET"!==t.tagName&&(t.style.display="none")}))}onData(e){let t=this,n={};Object.keys(e.data).forEach((a=>{t.data.hasOwnProperty(a)&&(n[a]=e.data[a])})),JSON.stringify(n)!==JSON.stringify(t.data)&&Object.assign(t.data,n)}}const j=(e,t)=>((t?[t]:[...Object.getOwnPropertyNames(e)]).forEach((t=>{if("_"===t[0]||void 0===e?.[t])return e;let n,a=e[t];if(!a.prototype)switch(typeof a){case"string":n=String;break;case"number":n=Number;break;case"boolean":n=Boolean;break;case"object":n=a instanceof Array?Array:Object;break;default:return}let r=class extends n{#l=!0;constructor(e){super(e),this.constructor.callbacks=[],x(e)&&Object.getOwnPropertyNames(e).forEach((t=>{this[t]=e[t]}))}observe(e){return this.constructor.callbacks.push(e),this}bind(e){const t=this;if(x(t.valueOf())){!function(e){var t;(t=e,(t instanceof HTMLElement?[t]:[]).concat([...t.querySelectorAll("*")]).filter((e=>!!E(e).length)).filter((e=>"STYLE"!==e.tagName))).forEach((e=>{e?._mc?._placeholders||m.add(e,"_placeholders",new A(e))}))}(e);const n=[...e.querySelectorAll("[_mc]")].filter((e=>!!e?._mc?._placeholders)),a=function(e){let t=h(e);n.forEach((e=>{e&&e._mc.trigger("data",t)}))};t.constructor.callbacks.push(a),t.notify()}else console.warn("IGNORED - cannot bind a non-Object to the dom, IS:",typeof t.valueOf(),t);return this}notify(e){let t=this,n=x(t.valueOf())&&t.valueOf()instanceof Array==0?structuredClone(Object.assign({},t)):t.valueOf();return void 0!==e&&(t.#l=e),t.constructor.callbacks.forEach((e=>{e(n)})),t}},l=new r(a);Object.defineProperty(e,t,{enumerable:!0,get(){if(x(l.valueOf())){let e=structuredClone(Object.assign({},l));setTimeout(v((function(){y(structuredClone(Object.assign({},l)),e)||l.notify()}),0),0)}return l},set(e){if(typeof e==typeof l.valueOf())if(x(l.valueOf())&&l.valueOf()instanceof Array==0){let t=structuredClone(Object.assign({},l));l instanceof r==0&&(l=new r(structuredClone(Object.assign({},e))),O(e,l),l.seal()),Object.assign(l,e),setTimeout(v((function(){y(structuredClone(Object.assign({},l)),t)||l.notify()}),0),0)}else{let t=l.valueOf(),n=l.constructor.callbacks;l=new r(e),l.constructor.callbacks=n,e!==t&&l.notify()}else console.warn("IGNORED - cannot change observable types, IS:",typeof l.valueOf(),l,"- CANNOT BECOME:",typeof e)}}),e[t]=a})),e);class S{constructor(e){}}class _ extends S{#o;#i;constructor(e,t){super(e),this.#o=e,this.#i=t}get val(){return new FormData(this.#i).get(this.#o.name)}set val(e){[...this.#i.querySelectorAll(`input[type="radio][name="${this.#o.name}"]`)].filter((t=>t.getAttribute("value")===e)).shift().checked=!0}}class T extends S{#o;constructor(e){super(e),this.#o=e}get val(){return this.#o.checked}set val(e){this.#o.checked=e}}class N extends S{#o;constructor(e){super(e),this.#o=e}get val(){return this.#o.value}set val(e){this.#o.value=e}}class C extends S{#o;#s=!1;constructor(e){super(e),this.#o=e,this.#s=e.multiple}get val(){const e=[];return[...this.#o.querySelectorAll("option")].filter((e=>e.selected)).forEach((t=>{t.selected&&e.push(t.value)})),this.#s?e:e.length?e[0]:""}set val(e){e="string"==typeof e?[e]:e,[...this.#o.querySelectorAll("option")].forEach((t=>{e.indexOf(t.value)>-1?(t.setAttribute("selected","selected"),t.selected=!0):(t.removeAttribute("selected"),t.selected=!1)}))}}class I extends S{#o;constructor(e){super(e),this.#o=e}get val(){return this.#o.value||""}set val(e){this.#o.value=e}}class L{constructor(e,t,n){let a=(e=>{let t=[...(e||document).querySelectorAll("input,select,textarea")],n={};return t.forEach((t=>{const a=t.getAttribute("name")||"undefined";if(a){let r,l,o=t.type;switch((e=>e.tagName.toLowerCase())(t)){case"input":switch(o){case"radio":r=new _(t,e.constructor===HTMLFormElement?e:e.querySelector("form")),l=m.add(t,a,r);break;case"checkbox":r=new T(t),l=m.add(t,a,r);break;default:r=new N(t),l=m.add(t,a,r)}Object.defineProperty(n,a,{enumerable:!0,configurable:!0,get:()=>l.val,set:e=>{l.val!==e&&(l.val=e)}});break;case"select":r=new C(t),l=m.add(t,a,r),Object.defineProperty(n,a,{enumerable:!0,configurable:!0,get:()=>l.val,set:e=>{l.val!==e&&(l.val=e)}});break;case"textarea":r=new I(t),l=m.add(t,a,r),Object.defineProperty(n,a,{enumerable:!0,configurable:!0,get:()=>l.val,set:e=>{l.val!==e&&(l.val=e)}});break;default:console.warn("handler missing:",a,"for",t.tagName.toLowerCase(),t)}}})),n})(n);e[t]=Object.assign({},a),j(e,t),O(a,e[t]),[...n.querySelectorAll("input, textarea, select")].forEach((n=>{s(n,"keyup",(n=>{e[t].notify()})),s(n,"change",(n=>{e[t].notify()})),s(n,"select",(n=>{e[t].notify()}))}))}}return function(e,t,n=[]){if(customElements.get(e))return void console.warn(`IGNORED: Custom element "${e}" is already defined!`);if(!e.includes("-"))throw new Error('createCustomElement: tagName must contain at least one hyphen ("-").');let a=e.split("-").map((e=>e.charAt(0).toUpperCase()+e.slice(1))).join("");customElements.define(e,function(e,t){return Object.defineProperty(t,"name",{value:e})}(a,class extends w{instance;constructor(e){super();let n=new t(this,e);m.add(this,a,n)}static get observedAttributes(){return n}}))}("test-testform",class extends m{data;constructor(e){super(e);const t=this;delete this._mc;let n=t.target.attachShadow({mode:"open"});n.innerHTML=`<style>${this.css}</style>${this.template}`,new L(t,"data",n),t.data.bind(n)}onConnected(){}oneInit(){}get template(){return'\n <form>\n \x3c!-- sample form to start with --\x3e\n <fieldset>\n <legend>Inputs</legend>\n <label>text</label><input type="text" name="textInput" value="Hallo" /> <strong>2wB: </strong>{textInput}\n <label>color</label><input type="color" name="colorInput" value="" />\n <label>date</label><input type="date" name="dateInput" value="" />\n <label>datetime-local</label><input type="datetime-local" name="dtlInput" value="" />\n <label>email</label><input type="email" name="email" value="" />\n <label>file</label><input type="file" name="fileInput" value="" />\n <label>hidden</label><input type="hidden" name="hiddenInput" value="hiddenVal" />\n <label>month</label><input type="month" name="month" value="" />\n <label>number</label><input type="number" name="number" value="" />\n <label>password</label><input type="password" name="passInput" value="" />\n <label>range</label><input type="range" name="rangeInput" value="" />\n <label>search</label><input type="search" name="searchInput" value="" />\n <label>tel</label><input type="tel" name="telInput" value="" />\n <label>time</label><input type="time" name="timeInput" value="" />\n <label>url</label><input type="url" name="urlInput" value="" />\n <label>week</label><input type="week" name="weekInput" value="" />\n </fieldset>\n\n <fieldset>\n <legend>Selects</legend>\n\n <label>singleSelect</label>\n <select name="singleSelect">\n <option disabled selected value> -- select -- </option>\n <option value="1">Option 1</option>\n <option value="2">Option 2</option>\n <option value="3">Option 3</option>\n <option value="4">Option 4</option>\n </select>\n\n <label>multiSelect</label>\n <select name="multiSelect" multiple="multiple" type="select-multiple">\n <option value="1">Option 1</option>\n <option value="2" selected=true>Option 2</option>\n <option value="3">Option 3</option>\n <option value="4" selected=true>Option 4</option>\n </select>\n\n </fieldset>\n\n <fieldset>\n <legend>Radiobuttons <strong>2wB: </strong><span>{myRadio}</span></legend>\n\n <label>radio1</label><input type="radio" name="myRadio" value="radio1">\n <label>radio2</label><input type="radio" name="myRadio" value="radio2">\n <label>radio3</label><input type="radio" name="myRadio" value="radio3">\n\n </fieldset>\n\n <fieldset>\n <legend>Checkboxes</legend>\n <label>cb1</label><input type="checkbox" name="cb1">\n <label>cb2</label><input type="checkbox" name="cb2">\n <label>cb3</label><input type="checkbox" name="cb3">\n <label>cb4</label><input type="checkbox" name="cb4">\n <label>cb5</label><input type="checkbox" name="cb5">\n\n </fieldset>\n\n <fieldset>\n <legend>TextArea</legend>\n <label>textArea</label>\n <textarea name="textArea">abc</textarea>\n </fieldset>\n\n </form>\n '}get css(){return"\nlegend {\n display: inherit;\n margin: inherit;\n vertical-align: inherit;\n}\n\nlabel {\n display: inline;\n margin-top: 5px;\n margin-left: 5px;\n vertical-align: top;\n width: 10em;\n text-align: right;\n}\n\nlabel::before {\n content: ' ';\n display: block;\n}\n\nform input,\nform select,\nform textarea {\n margin-left: 5px;\n margin-top: 0px;\n display: inline;\n}\n\nform label + textarea {\n display: inline-block;\n margin-top: 3px;\n}\n\nform input + label {\n width: inherit;\n margin-top: 3px;\n text-align: left;\n}\n\nbody > form {\n display: block;\n}\n\nform fieldset {\n border: 1px solid darkslategrey;\n border-radius: 3px;\n margin: 0.5em 0;\n}\n "}}),{}})()));
|
package/public/test.html
CHANGED
|
@@ -11,13 +11,17 @@
|
|
|
11
11
|
href="MicroComponents.svg"
|
|
12
12
|
/>
|
|
13
13
|
<link href="./my-tag.css" rel="stylesheet" />
|
|
14
|
+
<link href="./Grandparent.css" rel="stylesheet" />
|
|
14
15
|
<script src="./microcomponents.min.js"></script>
|
|
15
16
|
<script>
|
|
16
|
-
Object.assign(window, microcomponents); // make global
|
|
17
|
+
Object.assign(window, microcomponents); // make system global, for debugging
|
|
18
|
+
autoload(true); // automatically load web components that are undefined
|
|
17
19
|
</script>
|
|
18
20
|
<script type="module" src="./test.js"></script>
|
|
19
21
|
</head>
|
|
20
22
|
<body>
|
|
23
|
+
<test-grandparent></test-grandparent>
|
|
24
|
+
<test-testform></test-testform>
|
|
21
25
|
Open the debugger, read console, then:<br />
|
|
22
26
|
</body>
|
|
23
27
|
</html>
|
package/public/test.js
CHANGED
|
@@ -17,17 +17,16 @@ class Translator extends DC {
|
|
|
17
17
|
}
|
|
18
18
|
});
|
|
19
19
|
that.Translation.bind(translationTargetObject);
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
that.target.setAttribute('language', 'en');
|
|
21
|
+
that.Language.notify();
|
|
22
22
|
delete that._mc;
|
|
23
23
|
}
|
|
24
24
|
onAttributeChanged(ev) {
|
|
25
25
|
if (!ev.data.newValue)
|
|
26
26
|
return;
|
|
27
|
-
let that = this;
|
|
28
27
|
switch (ev.data.name) {
|
|
29
28
|
case 'language':
|
|
30
|
-
|
|
29
|
+
this.Language = ev.data.newValue;
|
|
31
30
|
break;
|
|
32
31
|
}
|
|
33
32
|
}
|
|
@@ -57,22 +56,21 @@ defineCE('my-tag', class MyTag extends DC {
|
|
|
57
56
|
that.Dom.bind(that.target);
|
|
58
57
|
}
|
|
59
58
|
onClick(ev) {
|
|
60
|
-
const that = this, c =
|
|
59
|
+
const that = this, c = that.Dom.counter++;
|
|
61
60
|
ev.data.stopPropagation();
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
that.target.classList.remove('clicked');
|
|
62
|
+
that.target.classList.add(that.Dom.counter > 9 ? 'overClicked' : 'clicked');
|
|
64
63
|
}
|
|
65
64
|
}, ['language']);
|
|
66
65
|
let MyTag = getCE('my-tag'), fragment = new DocumentFragment();
|
|
67
|
-
let start = new Date().getTime();
|
|
68
|
-
for (let x = 0; x <
|
|
66
|
+
let start = new Date().getTime(), max = 100;
|
|
67
|
+
for (let x = 0; x < max; x++) {
|
|
69
68
|
fragment.append(new MyTag());
|
|
70
69
|
}
|
|
71
70
|
let end = new Date().getTime();
|
|
72
71
|
document.body.appendChild(fragment);
|
|
73
72
|
setTimeout(() => {
|
|
74
|
-
console.log('%c \n\nStress Test:\n\n %cInstancing
|
|
75
|
-
document.body.appendChild(new MyTag());
|
|
73
|
+
console.log('%c \n\nStress Test:\n\n %cInstancing %s web components (%s classes): %c %s ms.', 'font-weight: bold; color: black;', 'font-weight: normal;', max, max * 3, 'font-weight: bold; color: blue;', end - start);
|
|
76
74
|
let element = document.querySelector('my-tag');
|
|
77
75
|
console.log("\n\n%cInspect this tag:%c \n( ...to see the inner structure in the '._mc' property )\n\n %O %c %s", 'font-weight: bold;', 'font-weight: normal;', element, 'font-weight: bold;', '\n\nLook up code in the debugger: test.js\n\n');
|
|
78
76
|
DC.trigger(element, 'doSomething');
|
package/public/test.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test.js","sourceRoot":"","sources":["../src/test.ts"],"names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CAAC,SAAS,CAChD,CAAC;IACH,CAAC;CACD,EACD,CAAC,UAAU,CAAC,CACZ,CAAC;AAEF,IAAI,KAAK,GAA6B,KAAK,CAAC,QAAQ,CAAE,EACrD,QAAQ,GAAG,IAAI,gBAAgB,EAAE,CAAC;AAMnC,IAAI,KAAK,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,EAC/B,GAAG,GAAG,GAAG,CAAC;AAGX,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;IAC9B,QAAQ,CAAC,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC,CAAC;AAC9B,CAAC;AAED,IAAI,GAAG,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAC;AAG/B,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;AAQpC,UAAU,CAAC,GAAG,EAAE;IAGf,OAAO,CAAC,GAAG,CACV,gFAAgF,EAChF,kCAAkC,EAClC,sBAAsB,EACtB,GAAG,EACH,GAAG,GAAG,CAAC,EACP,iCAAiC,EACjC,GAAG,GAAG,KAAK,CACX,CAAC;IAIF,IAAI,OAAO,GAAG,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IAE/C,OAAO,CAAC,GAAG,CACV,mGAAmG,EACnG,oBAAoB,EACpB,sBAAsB,EACtB,OAAO,EACP,oBAAoB,EACpB,+CAA+C,CAC/C,CAAC;IAGF,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;AACpC,CAAC,EAAE,GAAG,CAAC,CAAC"}
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
<h5>cb: <span class="tsd-signature-type">Callback</span></h5></li></ul></div>
|
|
56
56
|
<h4 class="tsd-returns-title">Returns <span class="tsd-signature-type">void</span></h4></li></ul></li></ul></li></ul></div><aside class="tsd-sources">
|
|
57
57
|
<ul>
|
|
58
|
-
<li>Defined in <a href="https://gitlab.com/twoBirds/microcomponents/-/blob/
|
|
58
|
+
<li>Defined in <a href="https://gitlab.com/twoBirds/microcomponents/-/blob/404f13d/src/observables.ts#L191">src/observables.ts:191</a></li></ul></aside></div>
|
|
59
59
|
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
|
|
60
60
|
<div class="tsd-navigation settings">
|
|
61
61
|
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<h1>Type alias selection</h1></div>
|
|
19
19
|
<div class="tsd-signature">selection<span class="tsd-signature-symbol">:</span> <a href="selectors.singleSelector.html" class="tsd-signature-type" data-tsd-kind="Type alias">singleSelector</a><span class="tsd-signature-symbol"> | </span><a href="selectors.singleSelector.html" class="tsd-signature-type" data-tsd-kind="Type alias">singleSelector</a><span class="tsd-signature-symbol">[]</span></div><aside class="tsd-sources">
|
|
20
20
|
<ul>
|
|
21
|
-
<li>Defined in <a href="https://gitlab.com/twoBirds/microcomponents/-/blob/
|
|
21
|
+
<li>Defined in <a href="https://gitlab.com/twoBirds/microcomponents/-/blob/404f13d/src/selectors.ts#L1634">src/selectors.ts:1634</a></li></ul></aside></div>
|
|
22
22
|
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
|
|
23
23
|
<div class="tsd-navigation settings">
|
|
24
24
|
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
<h1>Type alias singleSelector</h1></div>
|
|
19
19
|
<div class="tsd-signature">single<wbr/>Selector<span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">ShadowRoot</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">HTMLElement</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">HTMLElement</span><span class="tsd-signature-symbol">[]</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">HTMLCollection</span><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">NodeList</span><span class="tsd-signature-symbol"> | </span><a href="../interfaces/selectors.DSelector.html" class="tsd-signature-type" data-tsd-kind="Interface">DSelector</a><span class="tsd-signature-symbol"> | </span><a href="../classes/MC.MC.html" class="tsd-signature-type" data-tsd-kind="Class">MC</a><span class="tsd-signature-symbol"> | </span><a href="../classes/MC.DC.html" class="tsd-signature-type" data-tsd-kind="Class">DC</a><span class="tsd-signature-symbol"> | </span><a href="../interfaces/selectors.TSelector.html" class="tsd-signature-type" data-tsd-kind="Interface">TSelector</a><span class="tsd-signature-symbol"> | </span><span class="tsd-signature-type">undefined</span></div><aside class="tsd-sources">
|
|
20
20
|
<ul>
|
|
21
|
-
<li>Defined in <a href="https://gitlab.com/twoBirds/microcomponents/-/blob/
|
|
21
|
+
<li>Defined in <a href="https://gitlab.com/twoBirds/microcomponents/-/blob/404f13d/src/selectors.ts#L1632">src/selectors.ts:1632</a></li></ul></aside></div>
|
|
22
22
|
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
|
|
23
23
|
<div class="tsd-navigation settings">
|
|
24
24
|
<details class="tsd-index-accordion"><summary class="tsd-accordion-summary">
|
package/src/MC.ts
CHANGED
|
@@ -411,3 +411,68 @@ class DC extends McBase {
|
|
|
411
411
|
}
|
|
412
412
|
|
|
413
413
|
export { addListener, removeListener, McBase, MC, DC, McEvent };
|
|
414
|
+
|
|
415
|
+
let observer: MutationObserver | null = null;
|
|
416
|
+
const unresolved = new Map();
|
|
417
|
+
|
|
418
|
+
function loadit() {
|
|
419
|
+
document
|
|
420
|
+
.querySelectorAll(':not(:defined),[_mc]:not([_mc=""])')
|
|
421
|
+
.forEach((el) => {
|
|
422
|
+
const load: string[] = el.tagName.includes('-')
|
|
423
|
+
? [el.tagName.toLowerCase()]
|
|
424
|
+
: el.getAttribute('_mc')!.trim().split(' ');
|
|
425
|
+
|
|
426
|
+
load.forEach((elementName) => {
|
|
427
|
+
let fileName: any = elementName.toLowerCase().split('-'), // intentional any
|
|
428
|
+
lastElement = fileName[fileName.length - 1],
|
|
429
|
+
renameLastElement =
|
|
430
|
+
lastElement[0].toUpperCase() + lastElement.slice(1);
|
|
431
|
+
fileName[fileName.length - 1] = renameLastElement;
|
|
432
|
+
fileName = fileName.join('/') + '.js';
|
|
433
|
+
|
|
434
|
+
// autoload web components
|
|
435
|
+
// TBD: _mc components
|
|
436
|
+
if (
|
|
437
|
+
el.tagName.includes('-') &&
|
|
438
|
+
!unresolved.has(el.tagName.toLowerCase())
|
|
439
|
+
) {
|
|
440
|
+
const se: HTMLScriptElement = document.createElement('script');
|
|
441
|
+
|
|
442
|
+
unresolved.set(elementName, fileName);
|
|
443
|
+
|
|
444
|
+
se.setAttribute('src', fileName);
|
|
445
|
+
se.setAttribute('blocking', 'render');
|
|
446
|
+
se.setAttribute('type', 'module');
|
|
447
|
+
se.onload = () => {
|
|
448
|
+
// console.info('loaded', s);
|
|
449
|
+
unresolved.delete(elementName);
|
|
450
|
+
//console.log(unresolved);
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
document.head.append(se);
|
|
454
|
+
}
|
|
455
|
+
});
|
|
456
|
+
//console.log(unresolved);
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
let autoloadEnabled: boolean = false;
|
|
461
|
+
|
|
462
|
+
export function autoload(enable: boolean = true): boolean {
|
|
463
|
+
if (!enable) {
|
|
464
|
+
observer?.disconnect();
|
|
465
|
+
observer = null;
|
|
466
|
+
return autoloadEnabled;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
autoloadEnabled = enable;
|
|
470
|
+
observer = new MutationObserver(loadit);
|
|
471
|
+
|
|
472
|
+
setTimeout(() => {
|
|
473
|
+
observer!.observe(document.body, { childList: true, subtree: true });
|
|
474
|
+
loadit();
|
|
475
|
+
}, 5);
|
|
476
|
+
|
|
477
|
+
return autoloadEnabled;
|
|
478
|
+
}
|
package/src/elements.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
|
|
3
|
-
import { DC } from
|
|
4
|
-
import { kebabToPascal } from
|
|
3
|
+
import { DC } from './MC.js';
|
|
4
|
+
import { kebabToPascal } from './helpers.js';
|
|
5
5
|
|
|
6
6
|
class CeBaseClass extends HTMLElement {
|
|
7
7
|
constructor() {
|
|
@@ -9,19 +9,19 @@ class CeBaseClass extends HTMLElement {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
connectedCallback() {
|
|
12
|
-
(DC as any).trigger(this,
|
|
12
|
+
(DC as any).trigger(this, 'Connected');
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
disconnectedCallback() {
|
|
16
|
-
(DC as any).trigger(this,
|
|
16
|
+
(DC as any).trigger(this, 'Disconnected');
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
adoptedCallback() {
|
|
20
|
-
(DC as any).trigger(this,
|
|
20
|
+
(DC as any).trigger(this, 'Adopted');
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
attributeChangedCallback(name: string, oldValue: any, newValue: any) {
|
|
24
|
-
(DC as any).trigger(this,
|
|
24
|
+
(DC as any).trigger(this, 'AttributeChanged', { name, oldValue, newValue });
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
|
|
@@ -29,7 +29,7 @@ function createNamedClass(
|
|
|
29
29
|
name: string,
|
|
30
30
|
ClassBody: { new (...args: any[]): any }
|
|
31
31
|
) {
|
|
32
|
-
return Object.defineProperty(ClassBody,
|
|
32
|
+
return Object.defineProperty(ClassBody, 'name', { value: name });
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
export function defineCE(
|
|
@@ -42,7 +42,7 @@ export function defineCE(
|
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
if (!tagName.includes(
|
|
45
|
+
if (!tagName.includes('-')) {
|
|
46
46
|
throw new Error(
|
|
47
47
|
'createCustomElement: tagName must contain at least one hyphen ("-").'
|
|
48
48
|
);
|
|
@@ -60,7 +60,7 @@ export function defineCE(
|
|
|
60
60
|
constructor(data?: any) {
|
|
61
61
|
super();
|
|
62
62
|
let instance = new DefinitionClass(this, data);
|
|
63
|
-
|
|
63
|
+
DC.add(this, name, instance);
|
|
64
64
|
}
|
|
65
65
|
|
|
66
66
|
static get observedAttributes() {
|