mosaic 1.0.8 → 1.0.11

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