mosaic 1.0.3 → 1.0.6

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 (88) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +63 -8
  3. package/cjs/attribute.js +90 -0
  4. package/cjs/constants.js +15 -0
  5. package/cjs/context.js +66 -0
  6. package/cjs/define.js +117 -0
  7. package/cjs/formControl.js +42 -0
  8. package/cjs/helpers.js +140 -0
  9. package/cjs/index.js +9 -0
  10. package/cjs/list.js +109 -0
  11. package/cjs/mergeSlots.js +37 -0
  12. package/cjs/proxy.js +42 -0
  13. package/cjs/render.js +384 -0
  14. package/cjs/token.js +63 -0
  15. package/cjs/update.js +128 -0
  16. package/dist/mosaic.js +1096 -0
  17. package/dist/mosaic.min.js +1 -0
  18. package/package.json +42 -25
  19. package/src/attribute.js +84 -0
  20. package/src/constants.js +5 -0
  21. package/src/context.js +60 -0
  22. package/src/define.js +116 -0
  23. package/src/formControl.js +36 -0
  24. package/src/helpers.js +123 -0
  25. package/src/index.js +1 -0
  26. package/src/list.js +100 -0
  27. package/src/mergeSlots.js +31 -0
  28. package/src/proxy.js +36 -0
  29. package/src/render.js +386 -0
  30. package/src/token.js +55 -0
  31. package/src/update.js +122 -0
  32. package/SCRATCH.md +0 -31
  33. package/cli.js +0 -17
  34. package/conf.json +0 -0
  35. package/docs/assets/css/main.css +0 -2638
  36. package/docs/assets/images/icons.png +0 -0
  37. package/docs/assets/images/icons@2x.png +0 -0
  38. package/docs/assets/images/widgets.png +0 -0
  39. package/docs/assets/images/widgets@2x.png +0 -0
  40. package/docs/assets/js/main.js +0 -248
  41. package/docs/assets/js/search.js +0 -1
  42. package/docs/index.html +0 -111
  43. package/docs/modules.html +0 -131
  44. package/example/components/x-drawer.html +0 -41
  45. package/example/mosaic/templates/mocha/index.html +0 -39
  46. package/example/mosaic/tests.d.ts +0 -13
  47. package/example/mosaic/tests.js +0 -28
  48. package/example/mosaic/website.d.ts +0 -31
  49. package/example/mosaic/website.js +0 -64
  50. package/example/mosaic.config.d.ts +0 -7
  51. package/example/mosaic.config.js +0 -8
  52. package/example/package-lock.json +0 -899
  53. package/example/package.json +0 -20
  54. package/example/pages/index.html +0 -12
  55. package/example/test/x-drawer.d.ts +0 -0
  56. package/example/test/x-drawer.js +0 -94
  57. package/index.d.ts +0 -85
  58. package/index.js +0 -125
  59. package/index.wdocs.js +0 -37
  60. package/out/fonts/OpenSans-Bold-webfont.eot +0 -0
  61. package/out/fonts/OpenSans-Bold-webfont.svg +0 -1830
  62. package/out/fonts/OpenSans-Bold-webfont.woff +0 -0
  63. package/out/fonts/OpenSans-BoldItalic-webfont.eot +0 -0
  64. package/out/fonts/OpenSans-BoldItalic-webfont.svg +0 -1830
  65. package/out/fonts/OpenSans-BoldItalic-webfont.woff +0 -0
  66. package/out/fonts/OpenSans-Italic-webfont.eot +0 -0
  67. package/out/fonts/OpenSans-Italic-webfont.svg +0 -1830
  68. package/out/fonts/OpenSans-Italic-webfont.woff +0 -0
  69. package/out/fonts/OpenSans-Light-webfont.eot +0 -0
  70. package/out/fonts/OpenSans-Light-webfont.svg +0 -1831
  71. package/out/fonts/OpenSans-Light-webfont.woff +0 -0
  72. package/out/fonts/OpenSans-LightItalic-webfont.eot +0 -0
  73. package/out/fonts/OpenSans-LightItalic-webfont.svg +0 -1835
  74. package/out/fonts/OpenSans-LightItalic-webfont.woff +0 -0
  75. package/out/fonts/OpenSans-Regular-webfont.eot +0 -0
  76. package/out/fonts/OpenSans-Regular-webfont.svg +0 -1831
  77. package/out/fonts/OpenSans-Regular-webfont.woff +0 -0
  78. package/out/global.html +0 -912
  79. package/out/index.html +0 -65
  80. package/out/index.js.html +0 -239
  81. package/out/scripts/linenumber.js +0 -25
  82. package/out/scripts/prettify/Apache-License-2.0.txt +0 -202
  83. package/out/scripts/prettify/lang-css.js +0 -2
  84. package/out/scripts/prettify/prettify.js +0 -28
  85. package/out/styles/jsdoc-default.css +0 -358
  86. package/out/styles/prettify-jsdoc.css +0 -111
  87. package/out/styles/prettify-tomorrow.css +0 -132
  88. package/tsconfig.json +0 -11
package/dist/mosaic.js ADDED
@@ -0,0 +1,1096 @@
1
+ function systemReducer(state, action) {
2
+ switch (action.type) {
3
+ case "SET": {
4
+ const { name, value } = action.payload;
5
+ return {
6
+ ...state,
7
+ [name]: value,
8
+ }
9
+ }
10
+ case "MERGE": {
11
+ return {
12
+ ...state,
13
+ ...action.payload,
14
+ }
15
+ }
16
+ }
17
+ }
18
+
19
+ /*
20
+
21
+ Notes
22
+
23
+ - any call to dispatch will immediately invoke the reducer
24
+ - any call to dispatch within middleware is also immediately processed
25
+ - SET and MERGE are not interceptable by middleware
26
+ - middleware functions are assumed to call next synchronously
27
+ - the render function (subscriber) is invoked once for every call to dispatch
28
+ - the render function is debounced
29
+
30
+ */
31
+
32
+ function configure(
33
+ { update = {}, middleware = [], derivations = {}, initialState = {} },
34
+ node
35
+ ) {
36
+ let subscribers = [];
37
+ let state;
38
+ let updatedCallback = () => {};
39
+
40
+ function updateState(o) {
41
+ state = { ...o };
42
+ for (let k in derivations) {
43
+ state[k] = derivations[k](o);
44
+ }
45
+ }
46
+
47
+ updateState(initialState);
48
+
49
+ function getState() {
50
+ return { ...state }
51
+ }
52
+
53
+ function subscribe(fn) {
54
+ subscribers.push(fn);
55
+ }
56
+
57
+ function flush() {
58
+ subscribers.forEach((fn) => fn());
59
+ subscribers = [];
60
+ }
61
+
62
+ function onUpdate(fn) {
63
+ updatedCallback = fn;
64
+ }
65
+
66
+ function dispatch(action) {
67
+ const { type } = action;
68
+ if (type === "SET" || type === "MERGE") {
69
+ updateState(systemReducer(getState(), action));
70
+ } else {
71
+ if (middleware.length) {
72
+ let mw = middleware.slice();
73
+
74
+ let next = (action) => {
75
+ if (mw.length) {
76
+ let x = mw.shift();
77
+
78
+ if (action.type in x) {
79
+ x[action.type](action, next, {
80
+ getState,
81
+ dispatch,
82
+ afterNextRender: subscribe,
83
+ });
84
+ } else {
85
+ next(action);
86
+ }
87
+ } else if (action.type in update) {
88
+ updateState(update[action.type](getState(), action));
89
+ }
90
+ };
91
+
92
+ let x = mw.shift();
93
+
94
+ if (type in x) {
95
+ x[type](action, next, {
96
+ getState,
97
+ dispatch,
98
+ afterNextRender: subscribe,
99
+ });
100
+ } else {
101
+ next(action);
102
+ }
103
+ } else if (type in update) {
104
+ updateState(update[type](getState(), action));
105
+ }
106
+ }
107
+ updatedCallback();
108
+ }
109
+
110
+ for (let actionName in update) {
111
+ if (actionName.startsWith("$")) {
112
+ node.addEventListener(actionName, ({ detail }) => dispatch(detail));
113
+ }
114
+ }
115
+
116
+ return {
117
+ dispatch,
118
+ getState,
119
+ onUpdate,
120
+ flush,
121
+ }
122
+ }
123
+
124
+ const TEXT = 1;
125
+ const ATTRIBUTE = 2;
126
+ const INPUT = 3;
127
+ const EVENT = 4;
128
+ const REPEAT = 5;
129
+
130
+ const updateFormControl = (node, value) => {
131
+ if (node.nodeName === "SELECT") {
132
+ Array.from(node.querySelectorAll("option")).forEach((option) => {
133
+ option.selected = value.includes(option.value);
134
+ });
135
+ return
136
+ }
137
+
138
+ let checked;
139
+
140
+ switch (node.getAttribute("type")) {
141
+ case "checkbox":
142
+ checked = value;
143
+ if (node.checked === checked) break
144
+ if (checked) {
145
+ node.setAttribute("checked", "");
146
+ } else {
147
+ node.removeAttribute("checked");
148
+ }
149
+ break
150
+ case "radio":
151
+ checked = value === node.getAttribute("value");
152
+ if (node.checked === checked) break
153
+ node.checked = checked;
154
+ if (checked) {
155
+ node.setAttribute("checked", "");
156
+ } else {
157
+ node.removeAttribute("checked");
158
+ }
159
+ break
160
+ default:
161
+ if (node.value === value) break
162
+ node.setAttribute("value", (node.value = value || ""));
163
+ break
164
+ }
165
+ };
166
+
167
+ const last = (v = []) => v[v.length - 1];
168
+
169
+ const isWhitespace = (node) => {
170
+ return node.nodeType === node.TEXT_NODE && node.nodeValue.trim() === ""
171
+ };
172
+
173
+ const walk = (node, callback, deep = true) => {
174
+ if (!node) return
175
+ if (!isWhitespace(node)) {
176
+ let v = callback(node);
177
+ if (v === false) return
178
+ if (v?.nodeName) return walk(v, callback, deep)
179
+ }
180
+ if (deep) walk(node.firstChild, callback, deep);
181
+ walk(node.nextSibling, callback, deep);
182
+ };
183
+
184
+ const transformBrackets = (str = "") => {
185
+ let parts = str.split(/(\[[^\]]+\])/).filter((v) => v);
186
+ return parts.reduce((a, part) => {
187
+ let v = part.charAt(0) === "[" ? "." + part.replace(/\./g, ":") : part;
188
+ return a + v
189
+ }, "")
190
+ };
191
+
192
+ const getTarget = (path, target) => {
193
+ let parts = transformBrackets(path)
194
+ .split(".")
195
+ .map((k) => {
196
+ if (k.charAt(0) === "[") {
197
+ let p = k.slice(1, -1).replace(/:/g, ".");
198
+ return getValueAtPath(p, target)
199
+ } else {
200
+ return k
201
+ }
202
+ });
203
+
204
+ let t =
205
+ parts.slice(0, -1).reduce((o, k) => {
206
+ return o && o[k]
207
+ }, target) || target;
208
+ return [t, last(parts)]
209
+ };
210
+
211
+ const getValueAtPath = (path, target) => {
212
+ let [a, b] = getTarget(path, target);
213
+ let v = a?.[b];
214
+ if (typeof v === "function") return v.bind(a)
215
+ return v
216
+ };
217
+
218
+ const fragmentFromTemplate = (v) => {
219
+ if (typeof v === "string") {
220
+ let tpl = document.createElement("template");
221
+ tpl.innerHTML = v.trim();
222
+ return tpl.content
223
+ }
224
+ if (v.nodeName === "TEMPLATE") return v.cloneNode(true).content
225
+ if (v.nodeName === "defs") return v.firstElementChild.cloneNode(true)
226
+ };
227
+
228
+ const debounce = (fn) => {
229
+ let wait = false;
230
+ let invoke = false;
231
+ return () => {
232
+ if (wait) {
233
+ invoke = true;
234
+ } else {
235
+ wait = true;
236
+ fn();
237
+ requestAnimationFrame(() => {
238
+ if (invoke) fn();
239
+ wait = false;
240
+ });
241
+ }
242
+ }
243
+ };
244
+
245
+ const isPrimitive = (v) => v === null || typeof v !== "object";
246
+
247
+ const typeOf = (v) =>
248
+ Object.prototype.toString.call(v).match(/\s(.+[^\]])/)[1];
249
+
250
+ const pascalToKebab = (string) =>
251
+ string.replace(/[\w]([A-Z])/g, function (m) {
252
+ return m[0] + "-" + m[1].toLowerCase()
253
+ });
254
+
255
+ const kebabToPascal = (string) =>
256
+ string.replace(/[\w]-([\w])/g, function (m) {
257
+ return m[0] + m[2].toUpperCase()
258
+ });
259
+
260
+ const applyAttribute = (node, name, value) => {
261
+ name = pascalToKebab(name);
262
+
263
+ if (typeof value === "boolean") {
264
+ if (name.startsWith("aria-")) {
265
+ value = "" + value;
266
+ } else if (value) {
267
+ value = "";
268
+ }
269
+ }
270
+
271
+ if (typeof value === "string" || typeof value === "number") {
272
+ node.setAttribute(name, value);
273
+ } else {
274
+ node.removeAttribute(name);
275
+ }
276
+ };
277
+
278
+ const attributeToProp = (k, v) => {
279
+ let name = kebabToPascal(k);
280
+ if (v === "") v = true;
281
+ if (k.startsWith("aria-")) {
282
+ if (v === "true") v = true;
283
+ if (v === "false") v = false;
284
+ }
285
+ return {
286
+ name,
287
+ value: v,
288
+ }
289
+ };
290
+
291
+ function parseEach(node) {
292
+ let each = node.getAttribute("each");
293
+ let m = each?.match(/(.+)\s+in\s+(.+)/);
294
+ if (!m) {
295
+ if (!each) return m
296
+ return {
297
+ path: each.trim(),
298
+ key: node.getAttribute("key"),
299
+ }
300
+ }
301
+ let [_, left, right] = m;
302
+ let parts = left.match(/\(([^\)]+)\)/);
303
+ let [a, b] = (parts ? parts[1].split(",") : [left]).map((v) => v.trim());
304
+
305
+ return {
306
+ path: right.trim(),
307
+ identifier: b ? b : a,
308
+ index: b ? a : b,
309
+ key: node.getAttribute("key"),
310
+ }
311
+ }
312
+
313
+ const getBlockSize = (template) => {
314
+ let i = 0;
315
+ walk(template.content?.firstChild || template.firstChild, () => i++, false);
316
+ return i
317
+ };
318
+
319
+ const nextNonWhitespaceSibling = (node) => {
320
+ return isWhitespace(node.nextSibling)
321
+ ? nextNonWhitespaceSibling(node.nextSibling)
322
+ : node.nextSibling
323
+ };
324
+
325
+ const getBlockFragments = (template, numBlocks) => {
326
+ let blockSize = getBlockSize(template);
327
+
328
+ let r = [];
329
+ if (numBlocks) {
330
+ while (numBlocks--) {
331
+ let f = document.createDocumentFragment();
332
+ let n = blockSize;
333
+ while (n--) {
334
+ f.appendChild(nextNonWhitespaceSibling(template));
335
+ }
336
+ r.push(f);
337
+ }
338
+ }
339
+ return r
340
+ };
341
+
342
+ const getBlocks = (template) => {
343
+ let numBlocks = template.getAttribute("length");
344
+ let blockSize = getBlockSize(template);
345
+ let r = [];
346
+ let node = template;
347
+ if (numBlocks) {
348
+ while (numBlocks--) {
349
+ let f = [];
350
+ let n = blockSize;
351
+ while (n--) {
352
+ node = nextNonWhitespaceSibling(node);
353
+ f.push(node);
354
+ }
355
+ r.push(f);
356
+ }
357
+ }
358
+ return r
359
+ };
360
+
361
+ const compareKeyedLists = (key, a = [], b = []) => {
362
+ let delta = b.map(([k, item]) =>
363
+ !key ? (k in a ? k : -1) : a.findIndex(([_, v]) => v[key] === item[key])
364
+ );
365
+ if (a.length !== b.length || !delta.every((a, b) => a === b)) return delta
366
+ };
367
+
368
+ function lastChild(v) {
369
+ return (v.nodeType === v.DOCUMENT_FRAGMENT_NODE && v.lastChild) || v
370
+ }
371
+
372
+ const updateList = (template, delta, entries, createListItem) => {
373
+ let n = template.getAttribute("length") || 0;
374
+ let blocks = getBlockFragments(template, n);
375
+ let t = template;
376
+
377
+ delta.forEach((i, newIndex) => {
378
+ let frag =
379
+ i === -1
380
+ ? createListItem(entries[newIndex][1], entries[newIndex][0])
381
+ : blocks[i];
382
+ let x = lastChild(frag);
383
+ t.after(frag);
384
+ t = x;
385
+ });
386
+
387
+ template.setAttribute("length", delta.length);
388
+ };
389
+
390
+ const VALUE = 1;
391
+ const KEY = 2;
392
+ const FUNCTION = 3;
393
+
394
+ const hasMustache = (v) => v.match(/({{[^{}]+}})/);
395
+
396
+ const getParts = (value) =>
397
+ value
398
+ .trim()
399
+ .split(/({{[^{}]+}})/)
400
+ .filter((v) => v)
401
+ .map((value) => {
402
+ let match = value.match(/{{([^{}]+)}}/);
403
+
404
+ if (!match)
405
+ return {
406
+ type: VALUE,
407
+ value,
408
+ negated: false,
409
+ }
410
+
411
+ value = match[1].trim();
412
+ let negated = value.charAt(0) === "!";
413
+ if (negated) value = value.slice(1);
414
+
415
+ return {
416
+ type: KEY,
417
+ value,
418
+ negated,
419
+ }
420
+ });
421
+
422
+ const getValueFromParts = (target, parts) => {
423
+ return parts.reduce((a, part) => {
424
+ let { type, value, negated } = part;
425
+
426
+ let v;
427
+
428
+ if (type === VALUE) v = value;
429
+ if (type === KEY) {
430
+ v = getValueAtPath(value, target);
431
+ }
432
+ if (type === FUNCTION) {
433
+ let args = part.args.map((value) => getValueAtPath(value, target));
434
+
435
+ v = getValueAtPath(part.method, target)?.(...args);
436
+ }
437
+
438
+ if (negated) v = !v;
439
+
440
+ return a || a === 0 ? a + v : v
441
+ }, "")
442
+ };
443
+
444
+ const pascalToKebab$1 = (string) =>
445
+ string.replace(/[\w]([A-Z])/g, function (m) {
446
+ return m[0] + "-" + m[1].toLowerCase()
447
+ });
448
+
449
+ const kebabToPascal$1 = (string) =>
450
+ string.replace(/[\w]-([\w])/g, function (m) {
451
+ return m[0] + m[2].toUpperCase()
452
+ });
453
+
454
+ const parseStyles = (value) => {
455
+ let type = typeof value;
456
+
457
+ if (type === "string")
458
+ return value.split(";").reduce((o, value) => {
459
+ const [k, v] = value.split(":").map((v) => v.trim());
460
+ if (k) o[k] = v;
461
+ return o
462
+ }, {})
463
+
464
+ if (type === "object") return value
465
+
466
+ return {}
467
+ };
468
+
469
+ const joinStyles = (value) =>
470
+ Object.entries(value)
471
+ .map(([k, v]) => `${k}: ${v};`)
472
+ .join(" ");
473
+
474
+ const convertStyles = (o) =>
475
+ Object.keys(o).reduce((a, k) => {
476
+ a[pascalToKebab$1(k)] = o[k];
477
+ return a
478
+ }, {});
479
+
480
+ const applyAttribute$1 = (node, name, value) => {
481
+ if (name === "style") {
482
+ value = joinStyles(
483
+ convertStyles({
484
+ ...parseStyles(node.getAttribute("style")),
485
+ ...parseStyles(value),
486
+ })
487
+ );
488
+ } else if (name === "class") {
489
+ switch (typeOf(value)) {
490
+ case "Array":
491
+ value = value.join(" ");
492
+ break
493
+ case "Object":
494
+ value = Object.keys(value)
495
+ .reduce((a, k) => {
496
+ if (value[k]) a.push(k);
497
+ return a
498
+ }, [])
499
+ .join(" ");
500
+ break
501
+ }
502
+ } else if (!isPrimitive(value)) {
503
+ return (node[kebabToPascal$1(name)] = value)
504
+ }
505
+
506
+ name = pascalToKebab$1(name);
507
+
508
+ if (typeof value === "boolean") {
509
+ if (name.startsWith("aria-")) {
510
+ value = "" + value;
511
+ } else if (value) {
512
+ value = "";
513
+ }
514
+ }
515
+
516
+ let current = node.getAttribute(name);
517
+
518
+ if (value === current) return
519
+
520
+ if (typeof value === "string" || typeof value === "number") {
521
+ node.setAttribute(name, value);
522
+ } else {
523
+ node.removeAttribute(name);
524
+ }
525
+ };
526
+
527
+ const handler = ({ path, identifier, key, index, i, k }) => ({
528
+ get(target, property) {
529
+ let x = getValueAtPath(path, target);
530
+
531
+ // x === the collection
532
+
533
+ if (property === identifier) {
534
+ for (let n in x) {
535
+ let v = x[n];
536
+ if (key) {
537
+ if (v[key] === k) return v
538
+ } else {
539
+ if (n == i) return v
540
+ }
541
+ }
542
+ }
543
+
544
+ if (property === index) {
545
+ for (let n in x) {
546
+ let v = x[n];
547
+ if (key) {
548
+ if (v[key] === k) return n
549
+ } else {
550
+ if (n == i) return n
551
+ }
552
+ }
553
+ }
554
+
555
+ let t = key ? x.find((v) => v[key] === k) : x[i];
556
+ if (t?.hasOwnProperty?.(property)) return t[property]
557
+
558
+ return Reflect.get(...arguments)
559
+ },
560
+ set(target, property, value) {
561
+ let x = getValueAtPath(path, target);
562
+ let t = key ? x.find((v) => v[key] === k) : x[i];
563
+ if (t && !isPrimitive(t)) {
564
+ t[property] = value;
565
+ return true
566
+ }
567
+
568
+ return Reflect.set(...arguments)
569
+ },
570
+ });
571
+
572
+ const createContext = (v = []) => {
573
+ let context = v;
574
+ return {
575
+ get: () => context,
576
+ push: (v) => context.push(v),
577
+ wrap: (state) => {
578
+ return context.reduce(
579
+ (target, ctx) => new Proxy(target, handler(ctx)),
580
+ state
581
+ )
582
+ },
583
+ }
584
+ };
585
+
586
+ const render = (
587
+ target,
588
+ { getState, dispatch },
589
+ template,
590
+ updatedCallback,
591
+ beforeMountCallback
592
+ ) => {
593
+ let observer = () => {
594
+ let subscribers = new Set();
595
+ return {
596
+ publish: (cb) => {
597
+ for (let fn of subscribers) {
598
+ fn();
599
+ }
600
+ cb?.();
601
+ },
602
+ subscribe(fn) {
603
+ subscribers.add(fn);
604
+ },
605
+ }
606
+ };
607
+
608
+ const createSubscription = {
609
+ [TEXT]: ({ value, node, context }, { getState }) => {
610
+ return {
611
+ handler: () => {
612
+ let state = context ? context.wrap(getState()) : getState();
613
+ let a = node.textContent;
614
+ let b = getValueFromParts(state, getParts(value));
615
+ if (a !== b) node.textContent = b;
616
+ },
617
+ }
618
+ },
619
+ [ATTRIBUTE]: ({ value, node, name, context }, { getState }) => {
620
+ return {
621
+ handler: () => {
622
+ let state = context ? context.wrap(getState()) : getState();
623
+ let b = getValueFromParts(state, getParts(value));
624
+
625
+ applyAttribute$1(node, name, b);
626
+
627
+ if (node.nodeName === "OPTION") {
628
+ let path = node.parentNode.getAttribute("name");
629
+ let selected = getValueAtPath(path, state);
630
+ node.selected = selected === b;
631
+ }
632
+ },
633
+ }
634
+ },
635
+ [INPUT]: ({ node, path, context }, { getState, dispatch }) => {
636
+ node.addEventListener("input", () => {
637
+ let value =
638
+ node.getAttribute("type") === "checkbox" ? node.checked : node.value;
639
+
640
+ if (value.trim?.().length && !isNaN(value)) value = +value;
641
+
642
+ if (context) {
643
+ let state = context.wrap(getState());
644
+ state[path] = value;
645
+ dispatch({
646
+ type: "MERGE",
647
+ payload: state,
648
+ });
649
+ } else {
650
+ dispatch({
651
+ type: "SET",
652
+ payload: {
653
+ name: path,
654
+ value,
655
+ context,
656
+ },
657
+ });
658
+ }
659
+ });
660
+
661
+ return {
662
+ handler: () => {
663
+ let state = context ? context.wrap(getState()) : getState();
664
+ updateFormControl(node, getValueAtPath(path, state));
665
+ },
666
+ }
667
+ },
668
+ [EVENT]: (
669
+ { node, eventType, actionType, context },
670
+ { dispatch, getState }
671
+ ) => {
672
+ /*
673
+
674
+ NB that context is only passed for local actions not prefixed with "$"
675
+
676
+ */
677
+
678
+ node.addEventListener(eventType, (event) => {
679
+ let action = {
680
+ type: actionType,
681
+ event,
682
+ context: context ? context.wrap(getState()) : getState(),
683
+ };
684
+
685
+ dispatch(action);
686
+
687
+ if (actionType.startsWith("$")) {
688
+ node.dispatchEvent(
689
+ new CustomEvent(actionType, {
690
+ detail: action,
691
+ bubbles: true,
692
+ })
693
+ );
694
+ }
695
+ });
696
+ return {
697
+ handler: () => {},
698
+ }
699
+ },
700
+ [REPEAT]: (
701
+ {
702
+ node,
703
+ context,
704
+ map,
705
+ path,
706
+ identifier,
707
+ index,
708
+ key,
709
+ blockIndex,
710
+ hydrate,
711
+ pickupNode,
712
+ },
713
+ { getState }
714
+ ) => {
715
+ let oldValue;
716
+ node.$t = blockIndex - 1;
717
+
718
+ const initialiseBlock = (rootNode, i, k, exitNode) => {
719
+ walk(
720
+ rootNode,
721
+ multi(
722
+ (node) => {
723
+ if (node === exitNode) return false
724
+ },
725
+ bindAll(
726
+ map,
727
+ hydrate,
728
+ createContext(
729
+ (context?.get() || []).concat({
730
+ path,
731
+ identifier,
732
+ key,
733
+ index,
734
+ i,
735
+ k,
736
+ })
737
+ )
738
+ ),
739
+ (child) => (child.$t = blockIndex)
740
+ )
741
+ );
742
+ };
743
+
744
+ function firstChild(v) {
745
+ return (v.nodeType === v.DOCUMENT_FRAGMENT_NODE && v.firstChild) || v
746
+ }
747
+
748
+ const createListItem = (datum, i) => {
749
+ let k = datum[key];
750
+ let frag = fragmentFromTemplate(node);
751
+ initialiseBlock(firstChild(frag), i, k);
752
+ return frag
753
+ };
754
+
755
+ if (hydrate) {
756
+ let x = getValueAtPath(path, getState());
757
+ let blocks = getBlocks(node);
758
+
759
+ blocks.forEach((block, i) => {
760
+ let datum = x[i];
761
+ let k = datum?.[key];
762
+ initialiseBlock(block[0], i, k, last(block).nextSibling);
763
+ });
764
+
765
+ pickupNode = last(last(blocks)).nextSibling;
766
+ }
767
+
768
+ return {
769
+ handler: () => {
770
+ let state = context ? context.wrap(getState()) : getState();
771
+
772
+ const newValue = Object.entries(getValueAtPath(path, state) || []);
773
+ const delta = compareKeyedLists(key, oldValue, newValue);
774
+
775
+ if (delta) {
776
+ updateList(node, delta, newValue, createListItem);
777
+ }
778
+ oldValue = newValue.slice(0);
779
+ },
780
+ pickupNode,
781
+ }
782
+ },
783
+ };
784
+
785
+ const mediator = () => {
786
+ const o = observer();
787
+ return {
788
+ bind(v) {
789
+ let s = createSubscription[v.type](v, { getState, dispatch });
790
+ o.subscribe(s.handler);
791
+ return s
792
+ },
793
+ // scheduleUpdate: debounce((state, cb) => {
794
+ // return o.publish(state, cb)
795
+ // }),
796
+ update(cb) {
797
+ return o.publish(cb)
798
+ },
799
+ }
800
+ };
801
+
802
+ const { bind, update } = mediator();
803
+
804
+ let blockCount = 0;
805
+
806
+ const parse = (frag) => {
807
+ let index = 0;
808
+ let map = {};
809
+
810
+ walk(frag, (node) => {
811
+ let x = [];
812
+ let pickupNode;
813
+ switch (node.nodeType) {
814
+ case node.TEXT_NODE: {
815
+ let value = node.textContent;
816
+ if (hasMustache(value)) {
817
+ x.push({
818
+ type: TEXT,
819
+ value,
820
+ });
821
+ }
822
+ break
823
+ }
824
+ case node.ELEMENT_NODE: {
825
+ let each = parseEach(node);
826
+
827
+ if (each) {
828
+ let ns = node.namespaceURI;
829
+ let m;
830
+
831
+ if (ns.endsWith("/svg")) {
832
+ node.removeAttribute("each");
833
+ let tpl = document.createElementNS(ns, "defs");
834
+ tpl.innerHTML = node.outerHTML;
835
+ node.parentNode.replaceChild(tpl, node);
836
+ node = tpl;
837
+ m = parse(node.firstChild);
838
+ } else {
839
+ if (node.nodeName !== "TEMPLATE") {
840
+ node.removeAttribute("each");
841
+ let tpl = document.createElement("template");
842
+
843
+ tpl.innerHTML = node.outerHTML;
844
+ node.parentNode.replaceChild(tpl, node);
845
+ node = tpl;
846
+ }
847
+ m = parse(node.content.firstChild);
848
+ }
849
+
850
+ pickupNode = node.nextSibling;
851
+
852
+ x.push({
853
+ type: REPEAT,
854
+ map: m,
855
+ blockIndex: blockCount++,
856
+ ...each,
857
+ pickupNode,
858
+ });
859
+
860
+ break
861
+ }
862
+
863
+ let attrs = node.attributes;
864
+ let i = attrs.length;
865
+ while (i--) {
866
+ let { name, value } = attrs[i];
867
+
868
+ if (
869
+ name === ":name" &&
870
+ value &&
871
+ (node.nodeName === "INPUT" ||
872
+ node.nodeName === "SELECT" ||
873
+ node.nodeName === "TEXTAREA")
874
+ ) {
875
+ x.push({
876
+ type: INPUT,
877
+ path: value,
878
+ });
879
+
880
+ node.removeAttribute(name);
881
+ node.setAttribute("name", value);
882
+ } else if (name.startsWith(":on")) {
883
+ node.removeAttribute(name);
884
+ let eventType = name.split(":on")[1];
885
+ x.push({
886
+ type: EVENT,
887
+ eventType,
888
+ actionType: value,
889
+ });
890
+ } else if (name.startsWith(":")) {
891
+ let prop = name.slice(1);
892
+
893
+ let v = value || prop;
894
+
895
+ if (!v.includes("{{")) v = `{{${v}}}`;
896
+
897
+ x.push({
898
+ type: ATTRIBUTE,
899
+ name: prop,
900
+ value: v,
901
+ });
902
+ node.removeAttribute(name);
903
+ }
904
+ }
905
+ }
906
+ }
907
+ if (x.length) map[index] = x;
908
+ index++;
909
+ return pickupNode
910
+ });
911
+
912
+ return map
913
+ };
914
+
915
+ const multi =
916
+ (...fns) =>
917
+ (...args) => {
918
+ for (let fn of fns) {
919
+ let v = fn(...args);
920
+ if (v === false) return false
921
+ }
922
+ };
923
+
924
+ const bindAll = (bMap, hydrate = 0, context) => {
925
+ let index = 0;
926
+ return (node) => {
927
+ let k = index;
928
+ let p;
929
+ if (k in bMap) {
930
+ bMap[k].forEach((v) => {
931
+ let x = bind({
932
+ ...v,
933
+ node,
934
+ context,
935
+ hydrate,
936
+ });
937
+ p = x.pickupNode;
938
+ });
939
+ node.$i = index;
940
+ }
941
+ index++;
942
+ return p
943
+ }
944
+ };
945
+
946
+ let frag = fragmentFromTemplate(template);
947
+ let map = parse(frag);
948
+
949
+ walk(frag, bindAll(map));
950
+ beforeMountCallback?.(frag);
951
+ target.prepend(frag);
952
+ update();
953
+
954
+ return debounce(() => update(updatedCallback))
955
+ };
956
+
957
+ const childNodes = (node) => {
958
+ let frag = document.createDocumentFragment();
959
+ while (node.firstChild) {
960
+ frag.appendChild(node.firstChild);
961
+ }
962
+ return frag
963
+ };
964
+
965
+ const mergeSlots = (targetNode, sourceNode) => {
966
+ let namedSlots = sourceNode.querySelectorAll("slot[name]");
967
+
968
+ namedSlots.forEach((slot) => {
969
+ let name = slot.attributes.name.value;
970
+ let node = targetNode.querySelector(`[slot="${name}"]`);
971
+ if (!node) {
972
+ slot.parentNode.replaceChild(childNodes(slot), slot);
973
+ return
974
+ }
975
+ node.removeAttribute("slot");
976
+ slot.parentNode.replaceChild(node, slot);
977
+ });
978
+
979
+ let defaultSlot = sourceNode.querySelector("slot:not([name])");
980
+
981
+ if (defaultSlot) {
982
+ defaultSlot.parentNode.replaceChild(
983
+ childNodes(targetNode.innerHTML.trim() ? targetNode : defaultSlot),
984
+ defaultSlot
985
+ );
986
+ }
987
+ };
988
+
989
+ const define = (name, factory, template) =>
990
+ customElements.define(
991
+ name,
992
+ class extends HTMLElement {
993
+ async connectedCallback() {
994
+ if (!this.initialised) {
995
+ let config = factory(this);
996
+
997
+ if (config instanceof Promise) config = await config;
998
+
999
+ let { subscribe, shadow, initialState = {} } = config;
1000
+
1001
+ this.connectedCallback = config.connectedCallback;
1002
+ this.disconnectedCallback = config.disconnectedCallback;
1003
+
1004
+ const { dispatch, getState, onUpdate, flush } = configure(
1005
+ config,
1006
+ this
1007
+ );
1008
+
1009
+ initialState = getState();
1010
+
1011
+ let observedProps = Object.keys(initialState).filter(
1012
+ (v) => v.charAt(0) === "$"
1013
+ );
1014
+
1015
+ let observedAttributes = observedProps
1016
+ .map((v) => v.slice(1))
1017
+ .map(pascalToKebab);
1018
+
1019
+ let sa = this.setAttribute;
1020
+ this.setAttribute = (k, v) => {
1021
+ if (observedAttributes.includes(k)) {
1022
+ let { name, value } = attributeToProp(k, v);
1023
+ dispatch({
1024
+ type: "SET",
1025
+ payload: { name: "$" + name, value },
1026
+ });
1027
+ }
1028
+ sa.apply(this, [k, v]);
1029
+ };
1030
+
1031
+ observedAttributes.forEach((name) => {
1032
+ let property = attributeToProp(name).name;
1033
+
1034
+ let value;
1035
+
1036
+ if (this.hasAttribute(name)) {
1037
+ value = this.getAttribute(name);
1038
+ } else {
1039
+ value = this[property] || initialState["$" + property];
1040
+ }
1041
+
1042
+ Object.defineProperty(this, property, {
1043
+ get() {
1044
+ return getState()["$" + property]
1045
+ },
1046
+ set(v) {
1047
+ dispatch({
1048
+ type: "SET",
1049
+ payload: { name: "$" + property, value: v },
1050
+ });
1051
+ if (isPrimitive(v)) {
1052
+ applyAttribute(this, property, v);
1053
+ }
1054
+ },
1055
+ });
1056
+
1057
+ this[property] = value;
1058
+ });
1059
+
1060
+ let beforeMountCallback;
1061
+
1062
+ if (shadow) {
1063
+ this.attachShadow({
1064
+ mode: shadow,
1065
+ });
1066
+ } else {
1067
+ beforeMountCallback = (frag) => mergeSlots(this, frag);
1068
+ }
1069
+
1070
+ onUpdate(
1071
+ render(
1072
+ this.shadowRoot || this,
1073
+ { getState, dispatch },
1074
+ template,
1075
+ () => {
1076
+ observedProps.forEach((k) => {
1077
+ let v = getState()[k];
1078
+ if (isPrimitive(v)) applyAttribute(this, k.slice(1), v);
1079
+ });
1080
+ subscribe?.(getState());
1081
+ flush();
1082
+ },
1083
+ beforeMountCallback
1084
+ )
1085
+ );
1086
+ this.initialised = true;
1087
+ }
1088
+ this.connectedCallback?.();
1089
+ }
1090
+ disconnectedCallback() {
1091
+ this.disconnectedCallback?.();
1092
+ }
1093
+ }
1094
+ );
1095
+
1096
+ export { define };