@twobirds/microcomponents 0.0.51 → 0.0.52

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