@twobirds/microcomponents 0.0.66 → 0.0.67

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.
Files changed (93) hide show
  1. package/README.md +10 -6
  2. package/assets/{test.html → test/test.html} +3 -3
  3. package/package.json +4 -4
  4. package/public/documentation/MC.md +588 -0
  5. package/public/documentation/ajaxian-1.png +0 -0
  6. package/public/documentation/ajaxian-2.png +0 -0
  7. package/public/documentation/ajaxian-3.png +0 -0
  8. package/public/documentation/elements.md +278 -0
  9. package/public/documentation/functional_requirements.md +8 -0
  10. package/public/documentation/history.md +436 -0
  11. package/public/documentation/magic.md +29 -0
  12. package/public/documentation/observables.md +107 -0
  13. package/public/documentation/rnd0023-1.jpg +0 -0
  14. package/public/documentation/rnd0023-2.jpg +0 -0
  15. package/public/documentation/rnd0023-3.jpg +0 -0
  16. package/public/documentation/tB_react.md +207 -0
  17. package/public/documentation/usage.md +142 -0
  18. package/public/documentation/why.md +25 -0
  19. package/public/fiddle/MicroComponents.svg +15 -0
  20. package/public/fiddle/fiddle.css +0 -0
  21. package/public/fiddle/fiddle.js +0 -0
  22. package/public/fiddle/index.html +0 -0
  23. package/public/src/MC.ts +605 -0
  24. package/public/src/McEvent.ts +96 -0
  25. package/public/src/elements.ts +76 -0
  26. package/public/src/formValues.ts +233 -0
  27. package/public/src/helpers.ts +149 -0
  28. package/public/src/microcomponents.ts +8 -0
  29. package/public/src/observables.ts +401 -0
  30. package/public/src/selectors.ts +1634 -0
  31. package/public/src/test.ts +193 -0
  32. package/public/test/child.js +15 -0
  33. package/public/test/child.js.map +1 -0
  34. package/public/test/grandchild.js +15 -0
  35. package/public/test/grandchild.js.map +1 -0
  36. package/public/test/grandparent.css +13 -0
  37. package/public/test/grandparent.js +1 -0
  38. package/public/test/grandparent.js.map +1 -0
  39. package/public/test/greatgrandchild.js +29 -0
  40. package/public/test/greatgrandchild.js.map +1 -0
  41. package/public/test/my-tag.css +19 -0
  42. package/public/test/parent.js +15 -0
  43. package/public/test/parent.js.map +1 -0
  44. package/public/test/test.html +27 -0
  45. package/public/test/testform.js +1 -0
  46. package/public/test/testform.js.map +1 -0
  47. package/public/test/testmc.js +10 -0
  48. package/public/test/testmc.js.map +1 -0
  49. package/public/typedoc/.nojekyll +1 -0
  50. package/public/{classes → typedoc/classes}/MC.DC.html +2 -2
  51. package/public/{classes → typedoc/classes}/MC.MC.html +2 -2
  52. package/public/{classes → typedoc/classes}/MC.McBase.html +2 -2
  53. package/public/{classes → typedoc/classes}/MC.McEvent.html +2 -2
  54. package/public/{functions → typedoc/functions}/MC.addListener.html +1 -1
  55. package/public/{functions → typedoc/functions}/MC.autoload.html +1 -1
  56. package/public/{functions → typedoc/functions}/MC.removeListener.html +1 -1
  57. package/public/{functions → typedoc/functions}/elements.defineCE.html +1 -1
  58. package/public/{functions → typedoc/functions}/elements.getCE.html +1 -1
  59. package/public/{functions → typedoc/functions}/formValues.formValues.html +1 -1
  60. package/public/{functions → typedoc/functions}/observables.observe.html +1 -1
  61. package/public/{functions → typedoc/functions}/selectors._d.html +1 -1
  62. package/public/{functions → typedoc/functions}/selectors._t.html +1 -1
  63. package/public/{index.html → typedoc/index.html} +8 -6
  64. package/public/{interfaces → typedoc/interfaces}/MC.HTMLMcElement.html +2 -2
  65. package/public/{interfaces → typedoc/interfaces}/selectors.DSelector.html +2 -2
  66. package/public/{interfaces → typedoc/interfaces}/selectors.TSelector.html +2 -2
  67. package/public/typedoc/media/MicroComponents.svg +15 -0
  68. package/public/{modules → typedoc/modules}/MC.html +1 -1
  69. package/public/{modules → typedoc/modules}/elements.html +1 -1
  70. package/public/{modules → typedoc/modules}/formValues.html +1 -1
  71. package/public/{modules → typedoc/modules}/observables.html +1 -1
  72. package/public/{modules → typedoc/modules}/selectors.html +1 -1
  73. package/public/{types → typedoc/types}/observables.Observable.html +1 -1
  74. package/public/{types → typedoc/types}/selectors.selection.html +1 -1
  75. package/public/{types → typedoc/types}/selectors.singleSelector.html +1 -1
  76. package/src/test/testmc.ts +0 -6
  77. package/typedoc.json +1 -1
  78. package/webpack.config.js +1 -1
  79. package/assets/3d.html +0 -42
  80. package/public/microcomponents.min.js +0 -1
  81. /package/assets/{grandparent.css → test/grandparent.css} +0 -0
  82. /package/assets/{my-tag.css → test/my-tag.css} +0 -0
  83. /package/dist/{microcomponents.min.js → microcomponents.js} +0 -0
  84. /package/public/{media/MicroComponents.svg → MicroComponents.svg} +0 -0
  85. /package/public/{assets → typedoc/assets}/highlight.css +0 -0
  86. /package/public/{assets → typedoc/assets}/icons.js +0 -0
  87. /package/public/{assets → typedoc/assets}/icons.svg +0 -0
  88. /package/public/{assets → typedoc/assets}/main.js +0 -0
  89. /package/public/{assets → typedoc/assets}/navigation.js +0 -0
  90. /package/public/{assets → typedoc/assets}/search.js +0 -0
  91. /package/public/{assets → typedoc/assets}/style.css +0 -0
  92. /package/public/{hierarchy.html → typedoc/hierarchy.html} +0 -0
  93. /package/public/{modules.html → typedoc/modules.html} +0 -0
@@ -0,0 +1,401 @@
1
+ 'use strict';
2
+
3
+ import {
4
+ LooseObject,
5
+ debounce,
6
+ deepEqual,
7
+ flattenObject,
8
+ copyGettersSetters,
9
+ } from './helpers.js';
10
+
11
+ import { DC, HTMLMcElement, McEvent } from './MC.js';
12
+
13
+ function isObservableObject(value: any): boolean {
14
+ return value != null && typeof value === 'object';
15
+ }
16
+
17
+ type Callback = (value: any) => void;
18
+
19
+ function getPlaceholders(target: HTMLElement): string[] {
20
+ let placeholders: Set<string> = new Set(),
21
+ regEx = /\{[^\{\}]*\}/g;
22
+
23
+ // scan attributes
24
+ target
25
+ .getAttributeNames()
26
+ .filter((attr) => target.getAttribute(attr)?.match(regEx)) // extract placeholder(s)
27
+ .forEach((attr) => {
28
+ let phs: Set<string> = new Set(target.getAttribute(attr)?.match(regEx));
29
+ phs.forEach((placeholder) =>
30
+ placeholders.add(placeholder.replace(/[\{\}]/g, ''))
31
+ );
32
+ });
33
+
34
+ // scan children text nodes
35
+ [...target.childNodes]
36
+ .filter((childNode) => {
37
+ return (
38
+ // is text node and has placeholders
39
+ childNode.nodeType === 3 &&
40
+ ((childNode as any).nodeValue || '').match(regEx)
41
+ );
42
+ })
43
+ .forEach((childNode) => {
44
+ ((childNode as any).nodeValue || '')
45
+ .match(regEx)
46
+ .forEach((placeholder: string) => {
47
+ placeholders.add(placeholder.replace(/[\{\}]/g, ''));
48
+ });
49
+ });
50
+
51
+ // extract {} and return result
52
+ return [...placeholders].map((placeholder) =>
53
+ placeholder.replace(/[\{\}]/g, '')
54
+ );
55
+ }
56
+
57
+ const inputs = [HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement];
58
+
59
+ class Placeholders extends DC {
60
+ data: LooseObject = {};
61
+ oldDisplay?: string = '';
62
+
63
+ constructor(target: HTMLElement) {
64
+ super(target);
65
+ let that = this;
66
+ getPlaceholders(target).forEach((property) => (that.data[property] = ''));
67
+ observe(that);
68
+ that.#attachCallbacks();
69
+ delete that._mc; // cleanup, not needed
70
+ }
71
+
72
+ #attachCallbacks() {
73
+ let that = this,
74
+ target = that.target,
75
+ hide: boolean = false;
76
+
77
+ //console.log('attachCallbacks', that.target);
78
+
79
+ // scan attributes
80
+ target
81
+ .getAttributeNames()
82
+ .filter((attr: string) =>
83
+ target.getAttribute(attr)?.match(/\{[^\{\}]*\}/g)
84
+ ) // extract placeholder(s)
85
+ .forEach((attr: string) => {
86
+ let phs: Set<string> = new Set(
87
+ target.getAttribute(attr)?.match(/\{[^\{\}]*\}/g)
88
+ );
89
+ // attach callback
90
+ that.data.observe(
91
+ ((template: string) => (data: LooseObject) => {
92
+ let result: string = template;
93
+ phs.forEach((placeholder) => {
94
+ result = result.replace(
95
+ placeholder,
96
+ data[placeholder.replace(/[\{\}]/g, '')]
97
+ );
98
+ });
99
+ target.setAttribute(attr, result);
100
+ //console.log('cb setAttribute()', data, template, attr, result);
101
+ })(target.getAttribute(attr))
102
+ );
103
+ });
104
+
105
+ // scan children text nodes
106
+ [...target.childNodes]
107
+ .filter((childNode) => {
108
+ return (
109
+ // is text node and has placeholders
110
+ childNode.nodeType === 3 &&
111
+ ((childNode as any).nodeValue || '').match(/\{[^\{\}]*\}/g)
112
+ );
113
+ })
114
+ .forEach((childNode) => {
115
+ hide = true;
116
+ let phs: Set<string> = new Set(
117
+ childNode.nodeValue.match(/\{[^\{\}]*\}/g)
118
+ );
119
+
120
+ // attach callback
121
+ that.data.observe(
122
+ ((template: string) => (data: LooseObject) => {
123
+ let result: string = template;
124
+ //console.log('text node "', template, '" callbach data:', data);
125
+ phs.forEach((placeholder) => {
126
+ result = result.replace(
127
+ placeholder,
128
+ data[placeholder.replace(/[\{\}]/g, '')]
129
+ );
130
+ });
131
+ if (
132
+ Object.keys(data).some((key) => {
133
+ return !!data[key];
134
+ })
135
+ ) {
136
+ if (!!that.oldDisplay) {
137
+ target.style = that.oldDisplay;
138
+ } else {
139
+ target.removeAttribute('style');
140
+ }
141
+ delete that.oldDisplay;
142
+ }
143
+ childNode.nodeValue = result;
144
+ })(childNode.nodeValue)
145
+ );
146
+
147
+ // hide for now
148
+ that.oldDisplay = target.getAttribute('style)') || '';
149
+ if (
150
+ hide &&
151
+ !inputs.includes((target as any).constructor) &&
152
+ target.tagName !== 'FIELDSET'
153
+ ) {
154
+ target.style.display = 'none';
155
+ }
156
+ });
157
+ }
158
+
159
+ onData(ev: McEvent) {
160
+ let that = this,
161
+ data: LooseObject = {};
162
+ Object.keys(ev.data).forEach((key) => {
163
+ if (that.data.hasOwnProperty(key)) data[key] = ev.data[key];
164
+ });
165
+ //console.log('onData()', ev, data, that.data);
166
+ if (JSON.stringify(data) !== JSON.stringify(that.data))
167
+ Object.assign(that.data, data); // will trigger callbacks
168
+ }
169
+ }
170
+
171
+ function getPlaceholderElements(
172
+ target: HTMLElement | DocumentFragment | ShadowRoot
173
+ ): HTMLElement[] {
174
+ let elements: Array<HTMLElement> = (
175
+ target instanceof HTMLElement ? [target] : []
176
+ )
177
+ .concat([...(target.querySelectorAll('*') as unknown as HTMLElement[])])
178
+ .filter((e) => !!getPlaceholders(e as HTMLElement).length)
179
+ .filter((e) => e.tagName !== 'STYLE');
180
+ //console.log('getPlaceholderElements', elements);
181
+ return elements as HTMLElement[];
182
+ }
183
+
184
+ function makePlaceholders(
185
+ element: HTMLElement | DocumentFragment | ShadowRoot
186
+ ): void {
187
+ //console.log('makePlaceholders', element);
188
+ getPlaceholderElements(element).forEach((e: HTMLElement) => {
189
+ if ((e as HTMLMcElement)?._mc?._placeholders) return;
190
+ DC.add(e, '_placeholders', new Placeholders(e));
191
+ });
192
+ }
193
+
194
+ export type Observable = {
195
+ observe: (cb: Callback) => void;
196
+ notify: () => void;
197
+ bind: (target: HTMLElement | DocumentFragment | ShadowRoot) => void;
198
+ };
199
+
200
+ export const observe = (
201
+ target: LooseObject,
202
+ propertyName?: string
203
+ ): LooseObject => {
204
+ let keys = !propertyName
205
+ ? [...Object.getOwnPropertyNames(target)]
206
+ : [propertyName];
207
+
208
+ keys.forEach((key) => {
209
+ // console.log( 'co propname', key, typeof target[key], target[key] );
210
+ if (key[0] === '_' || target?.[key] === undefined) return target;
211
+ let val: any = target[key],
212
+ Constructor: any;
213
+
214
+ if (!val.prototype) {
215
+ switch (typeof val) {
216
+ case 'string':
217
+ Constructor = String;
218
+ break;
219
+ case 'number':
220
+ Constructor = Number;
221
+ break;
222
+ case 'boolean':
223
+ Constructor = Boolean;
224
+ break;
225
+ case 'object':
226
+ if (val instanceof Array) {
227
+ Constructor = Array;
228
+ } else {
229
+ Constructor = Object;
230
+ }
231
+ break;
232
+ default:
233
+ return;
234
+ }
235
+ }
236
+
237
+ let Observable = class extends Constructor {
238
+ #notify: boolean = true;
239
+
240
+ constructor(val: any) {
241
+ super(val);
242
+ (this.constructor as LooseObject).callbacks = [];
243
+ if (isObservableObject(val)) {
244
+ // you cannot bind native values to the dom
245
+ Object.getOwnPropertyNames(val).forEach((key) => {
246
+ this[key] = val[key];
247
+ });
248
+ }
249
+ }
250
+
251
+ observe(f: Function) {
252
+ //console.log('observe');
253
+ (this.constructor as any).callbacks.push(f);
254
+ return this;
255
+ }
256
+
257
+ bind(target: HTMLElement | DocumentFragment | ShadowRoot) {
258
+ const that = this,
259
+ iso = isObservableObject(that.valueOf());
260
+ //console.log('bind', iso, target);
261
+
262
+ if (iso) {
263
+ makePlaceholders(target);
264
+ const placeholders: unknown = [
265
+ ...target.querySelectorAll('[_mc]'),
266
+ ].filter((e) => !!(e as LooseObject)?._mc?._placeholders);
267
+ //console.log('placeholders:', target, placeholders);
268
+ const func = function callback(data: LooseObject) {
269
+ // avoid bubbling for speed
270
+ let d = flattenObject(data);
271
+ //console.log('inside callback', target, d);
272
+ (placeholders as DC[]).forEach((e) => {
273
+ // console.log('trigger callback', e);
274
+ if (!e) return;
275
+ (e as any)._mc.trigger('data', d);
276
+ });
277
+ };
278
+ (that.constructor as any).callbacks.push(func);
279
+ that.notify();
280
+ } else {
281
+ console.warn(
282
+ 'IGNORED - cannot bind a non-Object to the dom, IS:',
283
+ typeof that.valueOf(),
284
+ that
285
+ );
286
+ }
287
+
288
+ return this;
289
+ }
290
+
291
+ notify(notify?: boolean) {
292
+ let that = this,
293
+ data =
294
+ isObservableObject(that.valueOf()) &&
295
+ that.valueOf() instanceof Array === false
296
+ ? structuredClone(Object.assign({}, that))
297
+ : that.valueOf();
298
+
299
+ if (notify !== undefined) {
300
+ that.#notify = notify;
301
+ // return;
302
+ }
303
+
304
+ //if (that.#notify) console.log('notify', this, data);
305
+ (that.constructor as any).callbacks.forEach((f: Function) => {
306
+ // console.log(
307
+ // (this.constructor as any).callbacks,
308
+ // 'notify: exec callback',
309
+ // f,
310
+ // data
311
+ // );
312
+ f(data);
313
+ });
314
+
315
+ return that;
316
+ }
317
+ };
318
+
319
+ let o = new Observable(val);
320
+
321
+ Object.defineProperty(target, key, {
322
+ enumerable: true,
323
+ get() {
324
+ if (isObservableObject(o.valueOf())) {
325
+ let check = structuredClone(Object.assign({}, o));
326
+ setTimeout(
327
+ debounce(function checkChanges() {
328
+ let state = structuredClone(Object.assign({}, o));
329
+ //console.log( 'check changes', state, check );
330
+ if (!deepEqual(state, check)) {
331
+ //console.log( '-> not equal!' );
332
+ o.notify();
333
+ }
334
+ }, 0),
335
+ 0
336
+ );
337
+ }
338
+ return o;
339
+ },
340
+ set(val: any) {
341
+ //console.log( 'SET:', isObservableObject( val.valueOf() ), 'set:', val, 'to', isObservableObject( o.valueOf() ), o, );
342
+ if (typeof val === typeof o.valueOf()) {
343
+ let iso =
344
+ isObservableObject(o.valueOf()) &&
345
+ o.valueOf() instanceof Array === false;
346
+ //console.log('target iso:', iso);
347
+ if (iso) {
348
+ let old = structuredClone(Object.assign({}, o));
349
+ //val = structuredClone(Object.assign({}, val));
350
+
351
+ //let callbacks = (o.constructor as LooseObject).callbacks;
352
+ if (o instanceof Observable === false) {
353
+ o = new Observable(structuredClone(Object.assign({}, val)));
354
+ copyGettersSetters(val, o);
355
+ o.seal();
356
+ }
357
+ Object.assign(o, val);
358
+
359
+ // console.log('after set o=', o, 'check', old);
360
+ // (o.constructor as LooseObject).callbacks = callbacks;
361
+
362
+ setTimeout(
363
+ // check for changes
364
+ debounce(function checkChanges() {
365
+ let state = structuredClone(Object.assign({}, o));
366
+ //console.log( 'check changes', state, check );
367
+ if (!deepEqual(state, old)) {
368
+ //console.log( '-> not equal!' );
369
+ o.notify();
370
+ }
371
+ }, 0),
372
+ 0
373
+ );
374
+ } else {
375
+ // simple native var
376
+ let old = o.valueOf(),
377
+ callbacks = (o.constructor as LooseObject).callbacks;
378
+ o = new Observable(val);
379
+ (o.constructor as LooseObject).callbacks = callbacks;
380
+ if (val !== old) {
381
+ o.notify();
382
+ }
383
+ }
384
+ } else {
385
+ console.warn(
386
+ 'IGNORED - cannot change observable types, IS:',
387
+ typeof o.valueOf(),
388
+ o,
389
+ '- CANNOT BECOME:',
390
+ typeof val
391
+ );
392
+ }
393
+ },
394
+ });
395
+
396
+ // replace old property with proxy (observable)
397
+ target[key] = val;
398
+ });
399
+
400
+ return target;
401
+ };