@twobirds/microcomponents 0.0.48 → 0.0.51

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