@johnmorrisdotca/fuda-cards 0.9.2 → 0.11.0

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 (98) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE.md +14 -0
  3. package/README.md +466 -60
  4. package/dist/binder.d.ts +4 -6
  5. package/dist/binder.js +32 -23
  6. package/dist/card-markup.d.ts +2 -0
  7. package/dist/card-markup.js +59 -0
  8. package/dist/card-options.d.ts +131 -0
  9. package/dist/card-options.js +48 -0
  10. package/dist/card-styles.d.ts +2 -0
  11. package/dist/card-styles.js +314 -0
  12. package/dist/choice.d.ts +1 -1
  13. package/dist/choice.js +27 -23
  14. package/dist/collection-address.d.ts +8 -0
  15. package/dist/collection-address.js +52 -0
  16. package/dist/collection-binder-view.d.ts +9 -0
  17. package/dist/collection-binder-view.js +123 -0
  18. package/dist/collection-controls.d.ts +2 -0
  19. package/dist/collection-controls.js +143 -0
  20. package/dist/collection-deck.d.ts +11 -0
  21. package/dist/collection-deck.js +165 -0
  22. package/dist/collection-dialog.d.ts +2 -0
  23. package/dist/collection-dialog.js +28 -0
  24. package/dist/collection-element.d.ts +41 -63
  25. package/dist/collection-element.js +288 -472
  26. package/dist/collection-host.d.ts +5 -0
  27. package/dist/collection-host.js +1 -0
  28. package/dist/collection-icons.d.ts +3 -0
  29. package/dist/collection-icons.js +19 -0
  30. package/dist/collection-motion.d.ts +11 -0
  31. package/dist/collection-motion.js +72 -0
  32. package/dist/collection-styles.d.ts +4 -0
  33. package/dist/collection-styles.js +336 -0
  34. package/dist/collection-views.d.ts +8 -0
  35. package/dist/collection-views.js +8 -0
  36. package/dist/collection.d.ts +140 -11
  37. package/dist/collection.js +139 -33
  38. package/dist/element-define.d.ts +2 -0
  39. package/dist/element-define.js +3 -0
  40. package/dist/element.d.ts +20 -1
  41. package/dist/element.js +289 -98
  42. package/dist/index.d.ts +9 -52
  43. package/dist/index.js +5 -5
  44. package/dist/live-3d.d.ts +126 -0
  45. package/dist/live-3d.js +229 -0
  46. package/dist/materials.d.ts +117 -6
  47. package/dist/materials.js +191 -28
  48. package/dist/motion.d.ts +2 -0
  49. package/dist/motion.js +4 -0
  50. package/dist/pile-card.js +138 -91
  51. package/dist/pile.d.ts +61 -8
  52. package/dist/pile.js +89 -29
  53. package/dist/stack.d.ts +42 -6
  54. package/dist/stack.js +28 -3
  55. package/dist/version.d.ts +11 -0
  56. package/dist/version.js +11 -0
  57. package/dist/wear.d.ts +147 -9
  58. package/dist/wear.js +273 -89
  59. package/dist/zoom.d.ts +1 -1
  60. package/dist/zoom.js +73 -42
  61. package/docs/API.md +783 -0
  62. package/docs/collection-displays.md +1 -1
  63. package/docs/guides/accessibility-and-theming.md +62 -0
  64. package/docs/guides/architecture.md +53 -0
  65. package/docs/guides/binder-and-piles.md +83 -0
  66. package/docs/guides/bundlers.md +60 -0
  67. package/docs/guides/card-element.md +65 -0
  68. package/docs/guides/collections.md +86 -0
  69. package/docs/guides/embed-in-an-iframe.md +39 -0
  70. package/docs/guides/frameworks.md +132 -0
  71. package/docs/guides/getting-started.md +96 -0
  72. package/docs/guides/html-and-cdn.md +69 -0
  73. package/docs/guides/ja/accessibility-and-theming.md +62 -0
  74. package/docs/guides/ja/architecture.md +53 -0
  75. package/docs/guides/ja/binder-and-piles.md +83 -0
  76. package/docs/guides/ja/bundlers.md +60 -0
  77. package/docs/guides/ja/card-element.md +65 -0
  78. package/docs/guides/ja/collections.md +86 -0
  79. package/docs/guides/ja/embed-in-an-iframe.md +39 -0
  80. package/docs/guides/ja/frameworks.md +132 -0
  81. package/docs/guides/ja/getting-started.md +96 -0
  82. package/docs/guides/ja/html-and-cdn.md +69 -0
  83. package/docs/guides/ja/live-3d.md +75 -0
  84. package/docs/guides/ja/materials-and-stacks.md +73 -0
  85. package/docs/guides/ja/motion-and-spin.md +73 -0
  86. package/docs/guides/ja/troubleshooting.md +60 -0
  87. package/docs/guides/ja/typescript-and-node.md +45 -0
  88. package/docs/guides/ja/upgrading.md +56 -0
  89. package/docs/guides/ja/wear.md +63 -0
  90. package/docs/guides/live-3d.md +75 -0
  91. package/docs/guides/materials-and-stacks.md +73 -0
  92. package/docs/guides/motion-and-spin.md +73 -0
  93. package/docs/guides/troubleshooting.md +60 -0
  94. package/docs/guides/typescript-and-node.md +45 -0
  95. package/docs/guides/upgrading.md +56 -0
  96. package/docs/guides/wear.md +63 -0
  97. package/docs/live-3d.md +59 -0
  98. package/package.json +81 -11
@@ -1,146 +1,197 @@
1
- import { collectionViewAliases, collectionViews, isCard, parseCollection, randomDrawIndex, movePage } from './collection.js';
2
- import { BINDER_CSS, binderCovers, binderLayout, buildLeaf, buildSide, sideCount, spreadCount, spreadOf, spreadSides } from './binder.js';
3
- import { deckLandings, fanBounds, fanLayout, fanShift, flight, normalizeLanding, timing } from './pile.js';
4
- import { zoomView } from './zoom.js';
5
- import { createChoice, CHOICE_CSS, CHOICE_VARS } from './choice.js';
6
- import { stackLayout } from './stack.js';
7
- const STYLE = `<style>
8
- :host{display:block;${CHOICE_VARS.dark};color:var(--fc-text);font:16px system-ui}@media(prefers-color-scheme:light){:host(:not([theme="dark"])){${CHOICE_VARS.light}}}:host([theme="light"]){${CHOICE_VARS.light}}:host([theme="dark"]){${CHOICE_VARS.dark}}
9
- *{box-sizing:border-box}.shell{background:var(--fc-surface);padding:1rem;border-radius:1rem;display:grid;gap:.9rem}.nav{display:flex;gap:.6rem;align-items:center;justify-content:center;flex-wrap:wrap}.opts{display:grid;grid-template-columns:repeat(auto-fit,minmax(min(100%,13.5rem),1fr));gap:.9rem}.opts:not(:has(.choice:not([hidden]))){display:none}.opts .choice{--choice-min:4.6rem}
10
- button,input{font:inherit;color:inherit}.nav button,dialog button{min-height:48px;min-width:48px;background:var(--fc-btn);border:2px solid var(--fc-btn-border);border-radius:.7rem;padding:.5rem 1rem;cursor:pointer}.nav button:disabled{opacity:.45;cursor:default}.nav input{min-height:48px;background:var(--fc-btn);border:2px solid var(--fc-btn-border);border-radius:.7rem;padding:.5rem .8rem}
11
- button:focus-visible,input:focus-visible,.card:focus-visible{outline:3px solid var(--fc-focus);outline-offset:3px}${CHOICE_CSS}${BINDER_CSS}
12
- .displays .choice-list{grid-template-columns:repeat(3,minmax(0,1fr))}@media(min-width:340px){.displays .choice-list{grid-template-columns:repeat(4,minmax(0,1fr))}.displays .choice-list button{min-height:50px;padding:.3rem .25rem;font-size:.7rem}.displays .choice-list svg{width:28px;height:18px}}@media(min-width:800px){.displays .choice-list{grid-template-columns:repeat(6,minmax(0,1fr))}.displays .choice-list button{min-height:56px;padding:.45rem .7rem;font-size:.8rem}.displays .choice-list svg{width:34px;height:22px}}
13
- .stage{position:relative;height:var(--fuda-stage-height,clamp(300px,calc(100svh - 440px),620px));overflow:hidden;border-radius:1rem;background:radial-gradient(ellipse,var(--fc-stage-1),var(--fc-stage-2) 68%)}.stage:focus-visible{outline:3px solid var(--fc-focus);outline-offset:2px}.v{position:absolute;inset:0}kazoku-view-controls{display:block}
14
- .card{padding:0;border:0;background:transparent;line-height:0}.card img{width:100%;height:100%;object-fit:contain;border-radius:5%;box-shadow:0 6px 16px #0009}
15
- .stack .card{position:absolute;width:min(64vw,300px);height:86%;top:7%;left:50%;transition:transform .25s}
16
- .grid{display:grid;grid-template-columns:repeat(var(--columns,3),minmax(0,1fr));gap:1rem;padding:1rem;align-content:start}.grid .card{height:clamp(130px,24vw,220px)}
17
- .v.case{perspective:800px}.stage.case{background:linear-gradient(#4a3f33,#1d1914);border:10px solid #6b5436;box-shadow:inset 0 0 26px #000a}.stage.case::after{content:'';position:absolute;inset:0;pointer-events:none;z-index:2;background:linear-gradient(115deg,#ffffff30 0,#ffffff00 28%,#ffffff12 55%,#ffffff00 78%,#ffffff22 100%)}.case .card{transform:perspective(600px) rotateX(-15deg);box-shadow:0 12px 0 -3px #2c251d,0 18px 8px -4px #000a}.case .card img{box-shadow:0 4px 10px #0009}
18
- .wall{display:flex;gap:1rem;align-items:center;padding:1rem}.wall .card{height:90%;flex:0 0 190px}
19
- .story{display:flex;flex-direction:column;align-items:center;gap:1rem;padding:1rem}.story .card{width:min(75%,300px);flex:0 0 90%;min-height:0}
20
- .plane{position:absolute;left:0;top:0;display:grid;gap:18px;grid-template-columns:repeat(var(--atlas-cols,5),140px);padding:30px}.plane .card{height:195px}
21
- .stage.spotlight{background:radial-gradient(ellipse at center,#e5ce99,#454054 40%,#171822 78%)}
22
- .stage:is(.deck,.random,.fan){perspective:1800px}.v.deck{--w:200px;--gap:56px;--fl:0;--fr:0;--ft:0;--fb:0;--ease:cubic-bezier(.2,.8,.2,1);transform-style:preserve-3d}
23
- .v.deck .pile,.v.deck .slot{position:absolute;left:50%;top:50%;width:var(--w);aspect-ratio:2.5/3.5;margin:calc(var(--w) * -.7) 0 0 calc(var(--w) * -.5);transform-style:preserve-3d}.slot{border:2px dashed var(--fc-btn-border);border-radius:5%;opacity:.55}
24
- .v.deck .deck-pile{translate:calc(((var(--fl) - var(--fr) - 1) * var(--w) - var(--gap)) / 2) 0}.v.deck .discard-pile,.v.deck .slot{translate:calc(((var(--fl) + var(--fr) + 1) * var(--w) + var(--gap)) / 2) 0}.v.deck.fanonly .deck-pile{translate:calc((var(--fl) - var(--fr)) * var(--w) / 2) 0}
25
- .v.deck .deck-pile,.v.deck .discard-pile,.v.deck .slot{transition:translate var(--t,0s) var(--ease)}
26
- .extent{position:absolute;left:50%;top:50%;width:calc((var(--fl) + var(--fr) + 2) * var(--w) + var(--gap));height:calc((1.4 + var(--ft) + var(--fb)) * var(--w));translate:-50% calc(-50% + (var(--fb) - var(--ft)) * var(--w) / 2);visibility:hidden;pointer-events:none}.fanonly .extent{width:calc((var(--fl) + var(--fr) + 1) * var(--w))}
27
- .pile .card{position:absolute;inset:0;display:block;transform-style:preserve-3d;transform-origin:50% 50%;transform:translate3d(calc(var(--x,0) * var(--w)),calc(var(--y,0) * var(--w)),calc(var(--k,0) * .9px)) rotate(calc(var(--r,0) * 1deg));transition:transform var(--t,0s) var(--ease),transform-origin var(--t,0s) var(--ease);transition-delay:calc(var(--j,0) * var(--stag,22ms))}.pile .card:not(.top){pointer-events:none}.pile .card.top{cursor:pointer}
28
- .fan-open .deck-pile .card{transform-origin:50% 165%;transform:translate3d(calc(var(--fx,0) * var(--w)),0,calc(var(--k,0) * .9px)) rotate(calc(var(--fa,0) * 1deg));transition-delay:calc(var(--i,0) * var(--stag,28ms));pointer-events:auto;cursor:pointer}.fan-open .deck-pile .card:hover,.fan-open .deck-pile .card:focus-visible{translate:0 -14px}
29
- .fly,.turn{position:absolute;inset:0;transform-style:preserve-3d}.face{position:absolute;inset:0;backface-visibility:hidden;-webkit-backface-visibility:hidden;border-radius:5%;overflow:hidden}.face img{display:block;width:100%;height:100%;object-fit:contain;border-radius:3%;box-shadow:0 4px 10px #0008}.face.back{transform:rotateY(180deg)}
30
- .generic-back{display:grid;place-items:center;background:radial-gradient(ellipse at center,#b78745 0 12%,#152d46 13% 27%,#386988 28% 31%,#10233b 32% 62%,#d2b074 63% 66%,#182c45 67%);border:6px solid #b39158;color:#f7e9bf;font:bold 18px Georgia;letter-spacing:.12em;box-shadow:0 4px 10px #0008}
31
- .pile.discard[data-land=face-down] .card:not(.flat) .turn{transform:rotateY(180deg)}.card.flat .turn{transform:rotateY(calc(var(--fuda-yaw,0) * -1deg)) rotateX(calc(var(--fuda-tilt,0) * -1deg))}.ghost{pointer-events:none}
32
- dialog{color:var(--fc-text);background:var(--fc-dialog);border:1px solid var(--fc-btn-border);border-radius:1rem;text-align:center;max-width:95vw;max-height:95vh}dialog::backdrop{background:#000c}.detail{display:block;height:min(70vh,600px);max-width:80vw;object-fit:contain;margin:1rem auto}.empty{text-align:center;padding:3rem}
33
- @media(max-width:600px){.stage{height:var(--fuda-stage-height,clamp(220px,calc(100svh - 570px),620px))}}@media(max-width:500px){.opts{grid-template-columns:repeat(2,minmax(0,1fr))}.opts .speed{grid-column:1/-1}.v.deck{--gap:28px}.grid{grid-template-columns:repeat(min(2,var(--columns,3)),minmax(0,1fr))}.shell{padding:.7rem}.v.deck{--w:150px}}@media(prefers-reduced-motion:reduce){*{transition:none!important}}
34
- </style>`;
35
- const SHELL = `<section class="shell"><div class="displays"></div><div class="stage" tabindex="0" role="group" aria-label="Collection"><div class="v"></div></div><div class="nav"><button class="prev">← Previous</button><span class="count" aria-live="polite"></span><button class="next">Next →</button><label class="draw-filter" hidden>Filter cards <input type="search" aria-label="Filter cards for random draw"></label><button class="draw" hidden>Draw a card</button></div><div class="opts"></div><dialog aria-label="Card detail"></dialog></section>`;
36
- const pages = ['binder', 'case'];
37
- const scrolling = ['wall', 'story', 'atlas'];
38
- /** The displays that draw a pile of cards: they turn in 3D, fan open and closed, and a card comes off them onto a pile at their side. The Fan of Cards is the same pile with only the fan. */
39
- const pileViews = ['deck', 'random'];
40
- /** The displays whose cards sit on top of each other: moving to another card glides the cards that stay and lets the one that leaves go. */
41
- const gliding = ['rolodex', 'spotlight', 'fan'];
42
- const rects = (cols, rows, w, h, gx, gy, x0 = 1, y0 = 1) => Array.from({ length: cols * rows }, (_, k) => `<rect x="${x0 + (k % cols) * gx}" y="${y0 + Math.floor(k / cols) * gy}" width="${w}" height="${h}" rx="1" opacity="${0.55 + ((k % cols + Math.floor(k / cols)) % 2) * 0.4}"/>`).join('');
43
- const ICONS = {
44
- deck: '<rect x="9" y="4" width="14" height="14" rx="2" opacity=".45"/><rect x="6" y="2" width="14" height="14" rx="2" opacity=".7"/><rect x="3" y="0" width="14" height="14" rx="2"/>',
45
- rolodex: '<rect x="2" y="9" width="12" height="10" rx="1.5" opacity=".4"/><rect x="8" y="5" width="12" height="10" rx="1.5" opacity=".7"/><rect x="14" y="1" width="12" height="10" rx="1.5"/>',
46
- binder: rects(3, 2, 9, 9, 11, 10, 2, 1),
47
- case: '<path d="M3 4h28l-3 6H6z" opacity=".5"/><path d="M6 12h22l-3 8H9z"/>',
48
- jukebox: '<path d="M5 20V9a12 9 0 0 1 24 0v11z" opacity=".5"/><rect x="11" y="9" width="12" height="9" rx="1.5"/>',
49
- random: '<rect x="9" y="1" width="16" height="18" rx="3"/><circle cx="14" cy="6" r="1.6" fill="#0008"/><circle cx="20" cy="14" r="1.6" fill="#0008"/><circle cx="17" cy="10" r="1.6" fill="#0008"/>',
50
- story: '<rect x="11" y="0" width="12" height="7" rx="1.5"/><rect x="11" y="8" width="12" height="7" rx="1.5" opacity=".7"/><rect x="11" y="16" width="12" height="6" rx="1.5" opacity=".4"/>',
51
- wall: '<rect x="0" y="3" width="9" height="14" rx="1.5" opacity=".5"/><rect x="12" y="3" width="10" height="14" rx="1.5"/><rect x="25" y="3" width="9" height="14" rx="1.5" opacity=".5"/>',
52
- tower: '<rect x="9" y="1" width="16" height="4" rx="1"/><rect x="9" y="7" width="16" height="4" rx="1" opacity=".8"/><rect x="9" y="13" width="16" height="4" rx="1" opacity=".6"/><rect x="9" y="19" width="16" height="3" rx="1" opacity=".4"/>',
53
- spotlight: '<rect x="11" y="0" width="12" height="18" rx="2"/><rect x="1" y="5" width="8" height="12" rx="1.5" opacity=".4"/><rect x="25" y="5" width="8" height="12" rx="1.5" opacity=".4"/>',
54
- atlas: rects(4, 3, 6, 5.5, 8, 7, 2, 1),
55
- fan: [-30, -15, 0, 15, 30].map((a, i) => `<rect x="14" y="1" width="7" height="13" rx="1.2" transform="rotate(${a} 17.5 21)" opacity="${0.45 + i * 0.1}"/>`).join('')
56
- };
57
- const icon = (view) => `<svg viewBox="0 0 34 22" aria-hidden="true">${ICONS[view]}</svg>`;
58
- const ADDRESS_KEYS = ['view', 'fan', 'land', 'animate', 'speed', 'binder', 'cover', 'sheet', 'pockets'];
59
- const NAMES = { land: 'deck-land' };
1
+ import { binderLayout, sideCount, spreadCount, spreadOf } from "./binder.js";
2
+ import { collectionViewAliases, collectionViews, isCard, movePage, parseCollection, } from "./collection.js";
3
+ import { readAddress, writeAddress } from "./collection-address.js";
4
+ import { binderPageCount, renderBinder, syncBinderChoices } from "./collection-binder-view.js";
5
+ import { buildControls } from "./collection-controls.js";
6
+ import { applyFan, playFlip, renderDeck } from "./collection-deck.js";
7
+ import { renderDetail } from "./collection-dialog.js";
8
+ import { playMove, snapshotRects } from "./collection-motion.js";
9
+ import { gliding, pages, pileViews, scrolling } from "./collection-views.js";
10
+ import { normalizeLanding, timing } from "./pile.js";
11
+ /**
12
+ * `<fuda-collection>`: a whole collection in one of ten displays (a deck, a rolodex, binder pages, a display case, a random draw, a story scroll, a gallery wall,
13
+ * a spotlight, a card table, a fan), with zoom, Fit and Centre, the choices kept in the page address and a card detail on a press. Give it
14
+ * `cards` (see {@link parseCollection}) and a `view`.
15
+ *
16
+ * @example
17
+ * ```ts no-run
18
+ * import "@johnmorrisdotca/fuda-cards/collection-element";
19
+ * import type { FudaCollection } from "@johnmorrisdotca/fuda-cards/collection-element";
20
+ *
21
+ * const collection = document.createElement("fuda-collection") as FudaCollection;
22
+ * collection.setAttribute("view", "binder");
23
+ * collection.cards = [{ id: "1", name: "Explorer One", image: "one.jpg" }];
24
+ * document.body.append(collection);
25
+ * ```
26
+ */
60
27
  export class FudaCollection extends HTMLElement {
61
- root = this.attachShadow({ mode: 'open' });
28
+ /** @internal */
29
+ root = this.attachShadow({ mode: "open" });
30
+ /** @internal */
62
31
  data = [];
32
+ /** @internal */
63
33
  index = 0;
34
+ /** @internal */
64
35
  pageSize = 9;
36
+ /** @internal */
65
37
  selected = null;
38
+ /** @internal */
66
39
  back = false;
40
+ /** @internal */
67
41
  drawFilter = "";
42
+ /** @internal */
68
43
  zoom = null;
44
+ /** @internal */
69
45
  tiles;
46
+ /** @internal */
70
47
  landing;
48
+ /** @internal */
71
49
  fanChoice;
50
+ /** @internal */
72
51
  animateChoice;
52
+ /** @internal */
73
53
  speedChoice;
54
+ /** @internal */
74
55
  binderChoices = {};
56
+ /** @internal */
75
57
  turn = null;
58
+ /** @internal */
76
59
  flip = null;
60
+ /** @internal */
77
61
  addressTimer = 0;
62
+ /** @internal */
78
63
  fanPlaces = [];
79
- static get observedAttributes() { return ['view', 'page-size', 'columns', 'fan', 'fan-size', 'deck-land', 'deck-messiness', 'deck-seed', 'theme', 'animate', 'speed', 'binder', 'cover', 'sheet', 'pockets']; }
80
- set cards(value) { this.data = parseCollection(value); this.index = Math.min(this.index, Math.max(0, this.data.length - 1)); this.flip = null; this.render(); }
81
- get cards() { return this.data.map(c => ({ ...c, ...(c.metadata ? { metadata: { ...c.metadata } } : {}) })); }
64
+ liveSetup = null;
65
+ liveOff = [];
66
+ static get observedAttributes() {
67
+ return [
68
+ "view",
69
+ "page-size",
70
+ "columns",
71
+ "fan",
72
+ "fan-size",
73
+ "deck-land",
74
+ "deck-messiness",
75
+ "deck-seed",
76
+ "theme",
77
+ "animate",
78
+ "speed",
79
+ "binder",
80
+ "cover",
81
+ "sheet",
82
+ "pockets",
83
+ ];
84
+ }
85
+ set cards(value) {
86
+ this.data = parseCollection(value);
87
+ this.index = Math.min(this.index, Math.max(0, this.data.length - 1));
88
+ this.flip = null;
89
+ this.render();
90
+ }
91
+ get cards() {
92
+ return this.data.map((c) => ({ ...c, ...(c.metadata ? { metadata: { ...c.metadata } } : {}) }));
93
+ }
94
+ /** Live 3D: the card in focus turns in 3D on the page's one WebGL context, and every other card stays its picture. Nothing 3D loads until a card is focused. */
95
+ set live(value) {
96
+ this.liveSetup = value;
97
+ this.render();
98
+ }
99
+ get live() {
100
+ return this.liveSetup;
101
+ }
102
+ /** The cards are drawn again on every move, so their live views are made again too: the pictures stay in the shared cache. */
103
+ attachLive(v) {
104
+ for (const off of this.liveOff)
105
+ off();
106
+ this.liveOff = [];
107
+ const setup = this.liveSetup;
108
+ if (!setup)
109
+ return;
110
+ for (const el of Array.from(v.querySelectorAll(".card[data-i]:not(.ghost)"))) {
111
+ const c = this.data[Number(el.dataset.i)];
112
+ const item = c && setup.item(c);
113
+ if (!item)
114
+ continue;
115
+ const still = el.querySelector("img") ?? undefined;
116
+ this.liveOff.push(setup.cards.add(el, item, still ? { still } : {}));
117
+ }
118
+ }
82
119
  /** The card on top of the deck, or the first card of the page or fan (counting from 0). */
83
- get position() { return this.index; }
84
- connectedCallback() { if (this.hasAttribute('address'))
85
- this.readAddress(); this.render(); }
86
- disconnectedCallback() { clearTimeout(this.addressTimer); }
120
+ get position() {
121
+ return this.index;
122
+ }
123
+ connectedCallback() {
124
+ if (this.hasAttribute("address"))
125
+ readAddress(this);
126
+ this.render();
127
+ }
128
+ disconnectedCallback() {
129
+ clearTimeout(this.addressTimer);
130
+ for (const off of this.liveOff)
131
+ off();
132
+ this.liveOff = [];
133
+ }
87
134
  attributeChangedCallback(name) {
88
- this.pageSize = Math.min(100, Math.max(1, Math.floor(Number(this.getAttribute('page-size')) || 9)));
89
- if (name === 'fan' && this.setFan())
135
+ this.pageSize = Math.min(100, Math.max(1, Math.floor(Number(this.getAttribute("page-size")) || 9)));
136
+ if (name === "fan" && this.setFan())
90
137
  return;
91
- if (name === 'animate' || name === 'speed') {
138
+ if (name === "animate" || name === "speed") {
92
139
  this.timeVars();
93
140
  this.syncChoices();
94
141
  return;
95
142
  }
96
- if (name === 'view' || name === 'page-size' || name === 'columns') {
143
+ if (name === "view" || name === "page-size" || name === "columns") {
97
144
  this.index = 0;
98
145
  this.flip = null;
99
146
  }
100
- this.render(name === 'view', name === 'view' ? { kind: 'view', dir: 1 } : undefined);
147
+ this.render(name === "view", name === "view" ? { kind: "view", dir: 1 } : undefined);
148
+ }
149
+ /** @internal */
150
+ get currentView() {
151
+ let v = this.getAttribute("view") ?? "";
152
+ v = collectionViewAliases[v] ?? v;
153
+ return collectionViews.some((x) => x.id === v) ? v : "deck";
101
154
  }
102
- get currentView() { let v = this.getAttribute('view') ?? ''; v = collectionViewAliases[v] ?? v; return collectionViews.some(x => x.id === v) ? v : 'deck'; }
103
155
  /** How a card taken off lands: still facing up, or turned over sideways. The 0.6.0 value `upside-down` is still accepted and means turned over. */
104
- get land() { return normalizeLanding(this.getAttribute('deck-land')); }
105
- get fanOpen() { return this.getAttribute('fan') === 'open'; }
106
- get stacked() { return ![...pages, ...scrolling].includes(this.currentView); }
107
- get piled() { return pileViews.includes(this.currentView); }
156
+ /** @internal */
157
+ get land() {
158
+ return normalizeLanding(this.getAttribute("deck-land"));
159
+ }
160
+ /** @internal */
161
+ get fanOpen() {
162
+ return this.getAttribute("fan") === "open";
163
+ }
164
+ /** @internal */
165
+ get stacked() {
166
+ return ![...pages, ...scrolling].includes(this.currentView);
167
+ }
168
+ /** @internal */
169
+ get piled() {
170
+ return pileViews.includes(this.currentView);
171
+ }
108
172
  /** Only a card turns: a collection of objects (no card in it) never turns, and in a mixed one the objects stay square to the reader while the cards turn. */
109
- get hasCards() { return this.data.some(isCard); }
110
- get turnable() { return (this.piled || this.currentView === 'fan') && this.hasCards; }
111
- q(selector) { return this.root.querySelector(selector); }
112
- /** With `address`, the display, the fan, the landing, the animation and its speed are kept in the page's address (`?view=fan&fan=open&land=face-down&animate=off`), and a link to it opens the same. */
113
- readAddress() { try {
114
- const params = new URLSearchParams(location.search);
115
- for (const key of ADDRESS_KEYS) {
116
- const value = params.get(key);
117
- if (value === null)
118
- continue;
119
- const attr = NAMES[key] ?? key;
120
- if (this.getAttribute(attr) !== value)
121
- this.setAttribute(attr, value);
122
- }
173
+ /** @internal */
174
+ get hasCards() {
175
+ return this.data.some(isCard);
123
176
  }
124
- catch { } }
125
- writeAddress() { if (!this.hasAttribute('address'))
126
- return; clearTimeout(this.addressTimer); this.addressTimer = window.setTimeout(() => { try {
127
- const url = new URL(location.href);
128
- const want = { view: this.currentView === 'deck' ? null : this.currentView, fan: this.fanOpen ? 'open' : null, land: this.land === 'face-up' ? null : this.land, animate: this.getAttribute('animate') === 'off' ? 'off' : null, speed: ['slow', 'quick'].includes(this.getAttribute('speed') ?? '') ? this.getAttribute('speed') : null, binder: this.getAttribute('binder') === 'zip' ? 'zip' : null, cover: binderCovers.some(c => c.id === this.getAttribute('cover')) && this.getAttribute('cover') !== 'green' ? this.getAttribute('cover') : null, sheet: this.getAttribute('sheet') === 'black' ? 'black' : null, pockets: ['4', '12'].includes(this.getAttribute('pockets') ?? '') ? this.getAttribute('pockets') : null };
129
- for (const [k, v] of Object.entries(want)) {
130
- if (v === null)
131
- url.searchParams.delete(k);
132
- else
133
- url.searchParams.set(k, v);
134
- }
135
- history.replaceState(history.state, '', url);
177
+ /** @internal */
178
+ get turnable() {
179
+ return (this.piled || this.currentView === "fan") && this.hasCards;
180
+ }
181
+ /** @internal */
182
+ q(selector) {
183
+ return this.root.querySelector(selector);
136
184
  }
137
- catch { } }, 250); }
185
+ /** @internal */
138
186
  move(n) {
139
187
  if (!this.data.length)
140
188
  return;
141
189
  const view = this.currentView;
142
- if (view === 'binder') {
143
- const per = this.binderLay().per, spreads = spreadCount(sideCount(this.data.length, per)), from = spreadOf(this.index, per), to = (from + n + spreads * Math.abs(n)) % spreads;
190
+ if (view === "binder") {
191
+ const per = this.binderLay().per;
192
+ const spreads = spreadCount(sideCount(this.data.length, per));
193
+ const from = spreadOf(this.index, per);
194
+ const to = (from + n + spreads * Math.abs(n)) % spreads;
144
195
  this.turn = { from, to };
145
196
  this.index = this.spreadFirst(to);
146
197
  }
@@ -149,419 +200,184 @@ export class FudaCollection extends HTMLElement {
149
200
  }
150
201
  else {
151
202
  const next = (this.index + n + this.data.length) % this.data.length;
152
- this.flip = this.piled && Math.abs(n) === 1 && next === this.index + n ? (n > 0 ? 'off' : 'back') : null;
203
+ this.flip = this.piled && Math.abs(n) === 1 && next === this.index + n ? (n > 0 ? "off" : "back") : null;
153
204
  this.index = next;
154
205
  }
155
- this.render(false, { kind: 'move', dir: n > 0 ? 1 : -1 });
206
+ this.render(false, { kind: "move", dir: n > 0 ? 1 : -1 });
207
+ }
208
+ /** @internal */
209
+ binderLay() {
210
+ return binderLayout(this.getAttribute("pockets"), this.getAttribute("columns"), this.getAttribute("page-size"));
156
211
  }
157
- binderLay() { return binderLayout(this.getAttribute('pockets'), this.getAttribute('columns'), this.getAttribute('page-size')); }
158
212
  /** The first card of an opening of the binder: the right-hand page's first card, or the left-hand page's when the right-hand page is past the end. */
159
- spreadFirst(spread) { const per = this.binderLay().per, sides = sideCount(this.data.length, per); return Math.min(this.data.length - 1, (2 * spread < sides ? 2 * spread : 2 * spread - 1) * per); }
160
- image(c, at) { let button = document.createElement('button'); button.className = 'card'; button.dataset.i = String(at); button.setAttribute('aria-label', `Inspect ${c.name}`); let img = document.createElement('img'); img.src = c.image; img.alt = c.name; img.loading = 'lazy'; img.onerror = () => { img.replaceWith(document.createTextNode(`Image unavailable: ${c.name}`)); }; button.append(img); button.onclick = () => this.open(c); return button; }
161
- open(c) { this.selected = c; this.back = false; this.renderDialog(); }
162
- renderDialog() { let dlg = this.q('dialog'); dlg.replaceChildren(); let close = document.createElement('button'); close.textContent = 'Close'; close.onclick = () => dlg.close(); dlg.append(close); if (this.selected) {
163
- let img = document.createElement('img');
164
- img.className = 'detail';
165
- img.src = this.back && this.selected.backImage ? this.selected.backImage : this.selected.image;
166
- img.alt = `${this.selected.name}, ${this.back ? 'back' : 'front'}`;
167
- let label = document.createElement('p');
168
- label.textContent = this.selected.name;
169
- dlg.append(img, label);
170
- if (this.selected.backImage) {
171
- let flip = document.createElement('button');
172
- flip.textContent = this.back ? 'Show front' : 'Show back';
173
- flip.onclick = () => { this.back = !this.back; this.renderDialog(); };
174
- dlg.append(flip);
175
- }
176
- } if (!dlg.open)
177
- dlg.showModal(); }
178
- canDraw() { const query = this.drawFilter.trim().toLocaleLowerCase(); return this.data.some(c => c.name.toLocaleLowerCase().includes(query)); }
213
+ spreadFirst(spread) {
214
+ const per = this.binderLay().per;
215
+ const sides = sideCount(this.data.length, per);
216
+ return Math.min(this.data.length - 1, (2 * spread < sides ? 2 * spread : 2 * spread - 1) * per);
217
+ }
218
+ /** @internal */
219
+ image(c, at) {
220
+ const button = document.createElement("button");
221
+ button.className = "card";
222
+ button.dataset.i = String(at);
223
+ button.setAttribute("aria-label", `Inspect ${c.name}`);
224
+ const img = document.createElement("img");
225
+ img.src = c.image;
226
+ img.alt = c.name;
227
+ img.loading = "lazy";
228
+ img.onerror = () => {
229
+ img.replaceWith(document.createTextNode(`Image unavailable: ${c.name}`));
230
+ };
231
+ button.append(img);
232
+ button.onclick = () => this.open(c);
233
+ return button;
234
+ }
235
+ /** @internal */
236
+ open(c) {
237
+ this.selected = c;
238
+ this.back = false;
239
+ renderDetail(this);
240
+ }
241
+ /** @internal */
242
+ canDraw() {
243
+ const query = this.drawFilter.trim().toLocaleLowerCase();
244
+ return this.data.some((c) => c.name.toLocaleLowerCase().includes(query));
245
+ }
179
246
  /** The one length every move takes, in a custom property the stylesheet reads: `animate="off"` and reduced motion make it 0, `speed` stretches it. */
180
- timeVars() { const t = timing(this, true), shell = this.root.querySelector('.shell'); if (!shell)
181
- return t; shell.style.setProperty('--t', t.on ? `${t.ms}ms` : '0s'); if (t.on)
182
- shell.style.removeProperty('--stag');
183
- else
184
- shell.style.setProperty('--stag', '0ms'); return t; }
185
- syncChoices() { this.animateChoice?.set(this.getAttribute('animate') === 'off' ? 'off' : 'on'); this.speedChoice?.set(['slow', 'quick'].includes(this.getAttribute('speed') ?? '') ? this.getAttribute('speed') : 'normal'); }
186
- build() {
187
- if (this.zoom)
188
- return;
189
- this.root.innerHTML = STYLE + SHELL;
190
- const keep = (attr, value) => { if (value === null)
191
- this.removeAttribute(attr);
247
+ /** @internal */
248
+ timeVars() {
249
+ const t = timing(this, true);
250
+ const shell = this.root.querySelector(".shell");
251
+ if (!shell)
252
+ return t;
253
+ shell.style.setProperty("--t", t.on ? `${t.ms}ms` : "0s");
254
+ if (t.on)
255
+ shell.style.removeProperty("--stag");
192
256
  else
193
- this.setAttribute(attr, value); this.writeAddress(); };
194
- this.tiles = createChoice({ label: 'Display', layout: 'tiles', value: this.currentView, options: collectionViews.map(v => ({ value: v.id, label: v.label, icon: icon(v.id) })), onChange: value => { this.setAttribute('view', value); this.writeAddress(); } });
195
- this.q('.displays').append(this.tiles.element);
196
- this.landing = createChoice({ label: 'A card taken off lands', value: this.land, options: deckLandings.map(l => ({ value: l.id, label: l.label })), onChange: value => { this.setAttribute('deck-land', value); this.writeAddress(); } });
197
- this.fanChoice = createChoice({ label: 'Fan', value: this.fanOpen ? 'open' : 'closed', options: [{ value: 'closed', label: 'Closed' }, { value: 'open', label: 'Open' }], onChange: value => { if (value === 'open')
198
- this.setAttribute('fan', 'open');
199
- else
200
- this.removeAttribute('fan'); this.writeAddress(); } });
201
- this.animateChoice = createChoice({ label: 'Animate', value: 'on', options: [{ value: 'on', label: 'On' }, { value: 'off', label: 'Off' }], onChange: value => keep('animate', value === 'off' ? 'off' : 'on') });
202
- this.speedChoice = createChoice({ label: 'Speed', value: 'normal', options: [{ value: 'slow', label: 'Slow' }, { value: 'normal', label: 'Normal' }, { value: 'quick', label: 'Quick' }], onChange: value => keep('speed', value) });
203
- const bin = (attr, label, value, options) => { const h = createChoice({ label, value, options, onChange: v => { this.setAttribute(attr, v); this.writeAddress(); } }); this.binderChoices[attr] = h; return h.element; };
204
- this.q('.opts').append(this.landing.element, this.fanChoice.element, bin('binder', 'Binder', 'ring', [{ value: 'ring', label: 'Ring' }, { value: 'zip', label: 'Zip' }]), bin('cover', 'Cover', 'green', binderCovers.map(c => ({ value: c.id, label: c.label }))), bin('sheet', 'Sheet', 'clear', [{ value: 'clear', label: 'Clear' }, { value: 'black', label: 'Black' }]), bin('pockets', 'Pockets', '9', [{ value: '4', label: '4' }, { value: '9', label: '9' }, { value: '12', label: '12' }]), this.animateChoice.element, this.speedChoice.element);
205
- this.speedChoice.element.classList.add('speed');
206
- this.q('.prev').onclick = () => this.move(-1);
207
- this.q('.next').onclick = () => this.move(1);
208
- const input = this.q('.draw-filter input'), draw = this.q('.draw');
209
- input.oninput = () => { this.drawFilter = input.value; draw.disabled = !this.canDraw(); };
210
- draw.onclick = () => { const next = randomDrawIndex(this.data, this.index, this.drawFilter); if (next === null)
211
- return; this.index = next; this.render(false, { kind: 'move', dir: 1 }); };
212
- const stage = this.q('.stage');
213
- this.zoom = zoomView(stage, this.q('.v'), { address: this.hasAttribute('address'), items: '.card,.slot', theme: this.getAttribute('theme') ?? undefined });
214
- let startX = 0;
215
- stage.addEventListener('touchstart', e => { if (e.touches.length === 1)
216
- startX = e.touches[0].clientX; }, { passive: true });
217
- stage.addEventListener('touchend', e => { if (!this.stacked || this.piled || this.zoom.zoomed() || e.changedTouches.length !== 1)
218
- return; const dx = e.changedTouches[0].clientX - startX; if (Math.abs(dx) > 45) {
219
- if (e.cancelable)
220
- e.preventDefault();
221
- this.move(dx < 0 ? 1 : -1);
222
- } }, { passive: false });
223
- stage.addEventListener('keydown', (e) => { if (!this.stacked || this.piled || (e.key !== 'ArrowRight' && e.key !== 'ArrowLeft'))
224
- return; e.preventDefault(); this.move(e.key === 'ArrowRight' ? 1 : -1); this.q('.stack .card:last-child')?.focus(); });
257
+ shell.style.setProperty("--stag", "0ms");
258
+ return t;
225
259
  }
226
- /** One pile of the deck. `positions` are data indexes from the bottom of the pile to its top; the cards under the top one lie nudged and turned from the seed and the top one lies square. A deck's cards also carry where they lie when the fan is open. */
227
- pile(kind, positions, fan = []) {
228
- const pile = document.createElement('div');
229
- pile.className = `pile ${kind}-pile${kind === 'discard' ? ' discard' : ''}`;
230
- pile.dataset.land = this.land;
231
- const messy = Math.min(1, Math.max(0, Number(this.getAttribute('deck-messiness') ?? 0.3)));
232
- const seed = Number(this.getAttribute('deck-seed') ?? 1) || 1;
233
- const depth = Math.min(positions.length - 1, fan.length ? fan.length - 1 : 9);
234
- const shown = positions.slice(positions.length - 1 - depth);
235
- const places = stackLayout(positions.length, { messiness: messy, seed, depth });
236
- shown.forEach((at, j) => {
237
- const top = j === shown.length - 1, c = this.data[at], place = places[j];
238
- const el = document.createElement('div');
239
- const flat = !isCard(c);
240
- el.className = `card${top ? ' top' : ''}${flat ? ' flat' : ''}`;
241
- el.dataset.i = String(at);
242
- if (place && !top) {
243
- el.style.setProperty('--x', String(place.x));
244
- el.style.setProperty('--y', String(place.y));
245
- el.style.setProperty('--r', String(place.rotate));
246
- }
247
- el.style.setProperty('--k', String(j));
248
- el.style.setProperty('--j', String(shown.length - 1 - j));
249
- const f = fan[shown.length - 1 - j];
250
- if (f) {
251
- el.style.setProperty('--fx', String(f.x));
252
- el.style.setProperty('--fa', String(f.angle));
253
- el.style.setProperty('--i', String(shown.length - 1 - j));
254
- }
255
- const fly = document.createElement('div');
256
- fly.className = 'fly';
257
- const turn = document.createElement('div');
258
- turn.className = 'turn';
259
- const front = document.createElement('div');
260
- front.className = 'face front';
261
- const img = document.createElement('img');
262
- img.src = c.image;
263
- img.alt = top ? c.name : '';
264
- img.loading = 'lazy';
265
- img.onerror = () => { img.replaceWith(document.createTextNode(`Image unavailable: ${c.name}`)); };
266
- front.append(img);
267
- turn.append(front);
268
- // An object has no back to show: only a card gets the picture of its back (or the package's own).
269
- if (!flat) {
270
- const back = document.createElement('div');
271
- back.className = 'face back';
272
- if (c.backImage) {
273
- const b = document.createElement('img');
274
- b.src = c.backImage;
275
- b.alt = '';
276
- b.loading = 'lazy';
277
- back.append(b);
278
- }
279
- else {
280
- back.classList.add('generic-back');
281
- back.textContent = 'FUDA';
282
- }
283
- turn.append(back);
284
- }
285
- fly.append(turn);
286
- el.append(fly);
287
- el.onclick = () => this.open(c);
288
- if (top) {
289
- el.setAttribute('role', 'button');
290
- el.setAttribute('aria-label', `Inspect ${c.name}`);
291
- el.tabIndex = 0;
292
- el.onkeydown = e => { if (e.key === 'Enter' || e.key === ' ') {
293
- e.preventDefault();
294
- this.open(c);
295
- } };
296
- }
297
- pile.append(el);
298
- });
299
- return pile;
300
- }
301
- /** Where the fan's cards lie for a pile of this many cards. A phone's fan lies closer across and leans on its angle. */
302
- fanFor(count) { const narrow = (this.clientWidth || innerWidth) < 520; return fanLayout(count, Number(this.getAttribute('deck-seed') ?? 1) || 1, narrow ? 0.3 : 0.34); }
303
- /** Open the fan (or close it) on the pile that is on screen, without drawing it again, so every card glides: the open fan's reach is put in custom properties that move the piles apart and the Fit box. */
304
- fanVars(v, open) {
305
- const b = open && this.fanPlaces.length > 1 ? fanBounds(this.fanPlaces) : { left: 0, right: 0, top: 0, bottom: 0 };
306
- v.classList.toggle('fan-open', open && this.fanPlaces.length > 1);
307
- for (const [k, x] of [['--fl', b.left], ['--fr', b.right], ['--ft', b.top], ['--fb', b.bottom]])
308
- v.style.setProperty(k, String(x));
260
+ /** @internal */
261
+ syncChoices() {
262
+ this.animateChoice?.set(this.getAttribute("animate") === "off" ? "off" : "on");
263
+ this.speedChoice?.set(["slow", "quick"].includes(this.getAttribute("speed") ?? "") ? this.getAttribute("speed") : "normal");
309
264
  }
310
265
  /** `fan` changed: when a pile is on screen, move its cards to or from the fan in place (animated) and fit the view to it. Returns false when there is no pile to move. */
311
266
  setFan() {
312
- if (!this.zoom || !(this.piled || this.currentView === 'fan'))
267
+ if (!this.zoom || !(this.piled || this.currentView === "fan"))
313
268
  return false;
314
- const v = this.q('.v');
315
- if (!v.classList.contains('deck') || !this.data.length)
269
+ const v = this.q(".v");
270
+ if (!v.classList.contains("deck") || !this.data.length)
316
271
  return false;
317
272
  const t = this.timeVars();
318
- this.zoom.animated(() => { this.fanVars(v, this.fanOpen); this.zoom.refresh(); }, t.on ? t.ms : 0);
273
+ this.zoom.animated(() => {
274
+ applyFan(this, v, this.fanOpen);
275
+ this.zoom.refresh();
276
+ }, t.on ? t.ms : 0);
319
277
  this.syncChoices();
320
- this.fanChoice.set(this.fanOpen ? 'open' : 'closed');
321
- this.writeAddress();
278
+ this.fanChoice.set(this.fanOpen ? "open" : "closed");
279
+ writeAddress(this);
322
280
  return true;
323
281
  }
324
- renderDeck(v, fanOnly = false) {
325
- const n = this.data.length, deckPositions = [], discardPositions = [];
326
- v.classList.add('deck');
327
- if (fanOnly) {
328
- const size = Math.min(10, Math.max(2, Math.floor(Number(this.getAttribute('fan-size')) || 10))), count = Math.min(size, n);
329
- // The fan window: `count` cards from the first one, wrapping round; the top card is the first and lies on the left when open.
330
- for (let p = count - 1; p >= 0; p--)
331
- deckPositions.push((this.index + p) % n);
332
- v.classList.add('fanonly');
333
- this.fanPlaces = this.fanFor(count);
334
- v.append(this.pile('deck', deckPositions, this.fanPlaces));
335
- }
336
- else {
337
- // The deck is the cards from the top card down (bottom first); the pile is the cards already flipped off, the last one on top.
338
- for (let p = 0; p < n - this.index; p++)
339
- deckPositions.push(n - 1 - p);
340
- for (let p = 0; p < this.index; p++)
341
- discardPositions.push(p);
342
- this.fanPlaces = this.fanFor(Math.min(10, deckPositions.length));
343
- const slot = () => { const s = document.createElement('div'); s.className = 'slot'; return s; };
344
- // The deck is on the left and the pile on its other side; an empty pile is a dashed slot, so Fit does not jump when the first card lands.
345
- v.append(deckPositions.length ? this.pile('deck', deckPositions, this.fanPlaces) : slot(), discardPositions.length ? this.pile('discard', discardPositions) : slot());
346
- }
347
- const extent = document.createElement('div');
348
- extent.className = 'extent';
349
- v.append(extent);
350
- this.fanVars(v, this.fanOpen);
351
- }
352
- /** The card that just moved flies between the two piles, turning over sideways on the way if it lands face down. The same element, so the card is never drawn twice. An open fan sends the card from where the fan holds it. */
353
- playFlip(direction, v, ms) {
354
- // Either pile may be an empty dashed slot (the last card put back leaves the pile empty): the card still flies the same distance, from where the pile was.
355
- const deck = v.querySelector('.deck-pile'), discard = v.querySelector('.discard-pile');
356
- const moved = direction === 'off' ? discard?.querySelector('.card.top') : deck?.querySelector('.card.top');
357
- if (!moved)
358
- return;
359
- const style = getComputedStyle(v), w = parseFloat(style.getPropertyValue('--w')) || 200, gap = parseFloat(style.getPropertyValue('--gap')) || 56, fr = parseFloat(v.style.getPropertyValue('--fr')) || 0;
360
- const dx = (1 + fr) * w + gap, dy = 0;
361
- const fly = moved.querySelector('.fly'), turn = moved.querySelector('.turn');
362
- const lead = this.fanOpen && this.fanPlaces.length > 1 ? this.fanPlaces[0] : null, shift = lead ? fanShift(lead) : { x: 0, y: 0 };
363
- const away = { x: shift.x * w, y: shift.y * w }, spin = lead ? lead.angle : 0;
364
- // Taking a card off an open fan starts it where the fan holds it; bringing it back ends there.
365
- // The piles' cards lie 0.9 px apart in depth (ten at most), so the flying card starts just above the higher of them and ends at its own place.
366
- const z = 9.6 - (Number.parseFloat(moved.style.getPropertyValue('--k')) || 0) * 0.9;
367
- const from = direction === 'off' ? { x: -dx + away.x, y: -dy + away.y, z } : { x: dx - away.x, y: dy - away.y, z };
368
- flight(fly, turn, from, moved.classList.contains('flat') ? 'face-up' : this.land, direction, ms, direction === 'off' ? spin : -spin);
369
- }
370
- renderFan(v) { this.renderDeck(v, true); }
371
- /** Where every card is on screen now, by data index. */
372
- snapshot(v) { const m = new Map(); v.querySelectorAll('[data-i]:not(.ghost)').forEach(el => m.set(el.dataset.i, el.getBoundingClientRect())); return m; }
373
- /** Cards that arrive grow and fade in, a little to the side the reader is heading. Only an opening keyframe: they end on the card's own style. */
374
- enter(els, ms, dir = 0, stagger = 18) { let k = 0; for (const el of els)
375
- el.animate([{ opacity: 0, translate: `${dir * 36}px 0`, scale: .94, offset: 0 }], { duration: ms, delay: Math.min(k++, 12) * stagger, easing: 'cubic-bezier(.2,.8,.2,1)', fill: 'backwards' }); }
376
- /** After a card or page change or a switch of display: glide what stays, let what goes leave, bring in what comes. Nothing runs afterwards: each is one animation that ends by itself. */
377
- play(how, before, left, view, v, ms) {
378
- if (how.kind === 'view') {
379
- this.enter(Array.from(v.querySelectorAll('.card,.slot')), ms * 0.7);
380
- return;
381
- }
382
- if (pages.includes(view)) {
383
- this.enter(Array.from(v.querySelectorAll('.card')), ms * 0.7, how.dir);
384
- return;
385
- }
386
- if (scrolling.includes(view)) {
387
- const el = v.querySelector(`[data-i="${this.index}"]`);
388
- if (el)
389
- this.zoom.reveal(el, ms);
390
- return;
391
- }
392
- if (!gliding.includes(view) || this.zoom.turned())
393
- return;
394
- const scale = v.offsetWidth ? v.getBoundingClientRect().width / v.offsetWidth : 1;
395
- const kept = new Set();
396
- for (const el of Array.from(v.querySelectorAll('.card[data-i]'))) {
397
- const was = before.get(el.dataset.i), now = el.getBoundingClientRect();
398
- if (!was || !now.width) {
399
- this.enter([el], ms * 0.7, how.dir);
400
- continue;
401
- }
402
- kept.add(el.dataset.i);
403
- const dx = (was.left + was.width / 2 - now.left - now.width / 2) / scale, dy = (was.top + was.height / 2 - now.top - now.height / 2) / scale;
404
- el.animate([{ translate: `${dx}px ${dy}px`, scale: was.width / now.width, offset: 0 }], { duration: ms, easing: 'cubic-bezier(.2,.8,.2,1)' });
405
- }
406
- const host = v.querySelector('.deck-pile') ?? v;
407
- for (const old of left) {
408
- if (kept.has(old.dataset.i))
409
- continue;
410
- old.classList.add('ghost');
411
- old.removeAttribute('role');
412
- old.tabIndex = -1;
413
- host.append(old);
414
- const done = () => old.remove();
415
- old.animate([{ opacity: 1, translate: '0 0', offset: 0 }, { opacity: 0, translate: `${-how.dir * 60}px 0`, offset: 1 }], { duration: ms * 0.8, easing: 'ease-in' }).finished.then(done, done);
416
- }
417
- }
418
- syncBinder(view) {
419
- const show = view === 'binder', cur = (attr, dflt, ok) => { const v = this.getAttribute(attr); return v && ok.includes(v) ? v : dflt; };
420
- const vals = { binder: cur('binder', 'ring', ['ring', 'zip']), cover: cur('cover', 'green', binderCovers.map(c => c.id)), sheet: cur('sheet', 'clear', ['clear', 'black']), pockets: cur('pockets', '9', ['4', '9', '12']) };
421
- for (const [k, h] of Object.entries(this.binderChoices)) {
422
- h.hidden = !show;
423
- h.set(vals[k]);
424
- }
425
- }
426
- binderCount() { const per = this.binderLay().per, sides = sideCount(this.data.length, per), [l, r] = spreadSides(spreadOf(this.index, per), sides); const a = (l < 0 ? r : l) + 1, b = (r < 0 ? l : r) + 1; return a === b ? `Page ${a} / ${sides}` : `Pages ${a}–${b} / ${sides}`; }
427
- /** The binder: a ring or zip binder open on two facing sheets of pockets. A page turn swings the leaf about the spine, bending as it goes, and shows the pockets on its back. */
428
- renderBinder(v, t) {
429
- const layout = this.binderLay(), n = this.data.length, sides = sideCount(n, layout.per), spread = spreadOf(this.index, layout.per);
430
- const sheet = this.getAttribute('sheet') === 'black' ? 'black' : 'clear', zip = this.getAttribute('binder') === 'zip', cover = binderCovers.find(c => c.id === this.getAttribute('cover')) ?? binderCovers[0];
431
- v.classList.add('binder');
432
- v.style.setProperty('--cover', cover.color);
433
- const pocket = (index, live) => { const c = this.data[index], el = document.createElement('div'); el.className = 'pocket'; if (live)
434
- el.append(this.image(c, index));
435
- else {
436
- const img = document.createElement('img');
437
- img.src = c.image;
438
- img.alt = '';
439
- img.style.cssText = 'width:86%;height:92%;object-fit:contain;border-radius:3%;box-shadow:0 2px 5px #0007';
440
- el.append(img);
441
- } return el; };
442
- const [L, R] = spreadSides(spread, sides), turn = this.turn;
443
- this.turn = null;
444
- const flat = turn && t.on && Math.abs(turn.to - turn.from) === 1 ? turn : null;
445
- const [oldL, oldR] = flat ? spreadSides(flat.from, sides) : [L, R], [newL, newR] = flat ? spreadSides(flat.to, sides) : [L, R];
446
- const forward = !!flat && flat.to > flat.from;
447
- const book = document.createElement('div');
448
- book.className = `book ${zip ? 'b-zip' : 'b-ring'}`;
449
- const inside = document.createElement('div');
450
- inside.className = 'inside';
451
- const left = document.createElement('div'), right = document.createElement('div');
452
- left.className = 'half';
453
- right.className = 'half';
454
- const put = (half, side, where) => { half.replaceChildren(); if (side >= 0)
455
- half.append(buildSide(side, where, sheet, layout, n, pocket)); };
456
- // While a leaf swings the half it has not reached yet shows the page it is leaving; the other half already shows the page it uncovers.
457
- put(left, flat ? (forward ? oldL : newL) : L, 'l');
458
- put(right, flat ? (forward ? newR : oldR) : R, 'r');
459
- const spine = document.createElement('div');
460
- spine.className = 'spine';
461
- for (const y of [18, 50, 82]) {
462
- const ring = document.createElement('i');
463
- ring.className = 'ring';
464
- ring.style.top = `${y}%`;
465
- spine.append(ring);
466
- }
467
- inside.append(left, right, spine);
468
- const tab = document.createElement('i');
469
- tab.className = 'tab';
470
- book.append(inside, tab);
471
- v.append(book);
472
- if (flat) {
473
- const w = 300, strip = 34, { leaf, slices } = buildLeaf(forward ? oldR : newR, forward ? newL : oldL, sheet, layout, n, pocket, w, strip);
474
- inside.append(leaf);
475
- const ms = Math.round(t.ms * 1.4), ease = 'cubic-bezier(.45,.05,.3,1)', a = forward ? [0, -180] : [-180, 0];
476
- leaf.style.transform = `rotateY(${a[1]}deg)`;
477
- const swing = leaf.animate([{ transform: `rotateY(${a[0]}deg)` }, { transform: `rotateY(${a[1]}deg)` }], { duration: ms, easing: ease, fill: 'forwards' });
478
- // Each slice leans on the one before it, so the sheet bends as it swings (the free edge trails) and straightens at the end.
479
- const bend = forward ? 8 : -8;
480
- slices.forEach((s, k) => { if (k)
481
- s.animate([{ transform: 'rotateY(0deg)' }, { transform: `rotateY(${bend}deg)`, offset: .5 }, { transform: 'rotateY(0deg)' }], { duration: ms, easing: ease }); });
482
- const finish = () => { if (!leaf.isConnected)
483
- return; put(left, newL, 'l'); put(right, newR, 'r'); leaf.remove(); };
484
- swing.finished.then(finish, finish);
485
- }
486
- }
282
+ /** @internal */
487
283
  render(reset = false, how) {
488
284
  if (!this.isConnected)
489
285
  return;
490
- this.build();
491
- let view = this.currentView, stage = this.q('.stage'), v = this.q('.v');
492
- const t = this.timeVars(), playing = t.on && how && this.data.length > 0;
493
- const before = playing ? this.snapshot(v) : new Map(), left = playing && how.kind === 'move' && gliding.includes(view) ? Array.from(v.querySelectorAll('.card:not(.ghost)')) : [];
494
- this.syncBinder(view);
286
+ buildControls(this);
287
+ const view = this.currentView;
288
+ const stage = this.q(".stage");
289
+ const v = this.q(".v");
290
+ const t = this.timeVars();
291
+ const playing = t.on && how && this.data.length > 0;
292
+ const before = playing ? snapshotRects(v) : new Map();
293
+ const left = playing && how.kind === "move" && gliding.includes(view)
294
+ ? Array.from(v.querySelectorAll(".card:not(.ghost)"))
295
+ : [];
296
+ syncBinderChoices(this, view);
495
297
  this.tiles.set(view);
496
298
  this.landing.set(this.land);
497
- this.fanChoice.set(this.fanOpen ? 'open' : 'closed');
299
+ this.fanChoice.set(this.fanOpen ? "open" : "closed");
498
300
  this.syncChoices();
499
301
  this.landing.hidden = !this.piled || !this.hasCards;
500
- this.fanChoice.hidden = !(this.piled || view === 'fan');
501
- this.q('.count').textContent = this.data.length ? (view === 'binder' ? this.binderCount() : pages.includes(view) ? `Page ${Math.floor(this.index / this.pageSize) + 1} / ${Math.ceil(this.data.length / this.pageSize)}` : `${this.index + 1} / ${this.data.length}`) : 'No cards';
502
- for (const selector of ['.prev', '.next'])
302
+ this.fanChoice.hidden = !(this.piled || view === "fan");
303
+ this.q(".count").textContent = this.data.length
304
+ ? view === "binder"
305
+ ? binderPageCount(this)
306
+ : pages.includes(view)
307
+ ? `Page ${Math.floor(this.index / this.pageSize) + 1} / ${Math.ceil(this.data.length / this.pageSize)}`
308
+ : `${this.index + 1} / ${this.data.length}`
309
+ : "No cards";
310
+ for (const selector of [".prev", ".next"])
503
311
  this.q(selector).disabled = !this.data.length;
504
- const random = view === 'random';
505
- this.q('.draw-filter').hidden = !random;
506
- this.q('.draw').hidden = !random;
507
- this.q('.draw-filter input').value = this.drawFilter;
508
- this.q('.draw').disabled = !this.canDraw();
312
+ const random = view === "random";
313
+ this.q(".draw-filter").hidden = !random;
314
+ this.q(".draw").hidden = !random;
315
+ this.q(".draw-filter input").value = this.drawFilter;
316
+ this.q(".draw").disabled = !this.canDraw();
509
317
  stage.className = `stage ${view}`;
510
- v.className = 'v';
318
+ v.className = "v";
511
319
  v.replaceChildren();
512
- v.style.removeProperty('--columns');
513
- for (const k of ['--fl', '--fr', '--ft', '--fb'])
320
+ v.style.removeProperty("--columns");
321
+ for (const k of ["--fl", "--fr", "--ft", "--fb"])
514
322
  v.style.removeProperty(k);
515
- this.zoom.minOpen = scrolling.includes(view) ? .5 : 0;
323
+ this.zoom.minOpen = scrolling.includes(view) ? 0.5 : 0;
516
324
  this.zoom.setTurnable(this.turnable && this.data.length > 0, this.data.length > 0 && !this.hasCards);
517
- this.zoom.items = this.piled || view === 'fan' ? '.extent' : view === 'binder' ? '.book' : '.card,.slot';
325
+ this.zoom.items = this.piled || view === "fan" ? ".extent" : view === "binder" ? ".book" : ".card,.slot";
518
326
  if (!this.data.length) {
519
327
  v.innerHTML = '<p class="empty">No cards in this collection.</p>';
520
328
  }
521
329
  else if (this.piled)
522
- this.renderDeck(v);
523
- else if (view === 'fan')
524
- this.renderFan(v);
525
- else if (view === 'binder')
526
- this.renderBinder(v, t);
330
+ renderDeck(this, v);
331
+ else if (view === "fan")
332
+ renderDeck(this, v, true);
333
+ else if (view === "binder")
334
+ renderBinder(this, v, t);
527
335
  else if (pages.includes(view)) {
528
- v.classList.add('grid', view);
529
- const columns = Math.min(10, Math.max(1, Math.floor(Number(this.getAttribute('columns')) || 3)));
530
- v.style.setProperty('--columns', String(columns));
531
- let start = Math.floor(this.index / this.pageSize) * this.pageSize;
532
- this.data.slice(start, start + this.pageSize).forEach((c, k) => v.append(this.image(c, start + k)));
336
+ v.classList.add("grid", view);
337
+ const columns = Math.min(10, Math.max(1, Math.floor(Number(this.getAttribute("columns")) || 3)));
338
+ v.style.setProperty("--columns", String(columns));
339
+ const start = Math.floor(this.index / this.pageSize) * this.pageSize;
340
+ this.data.slice(start, start + this.pageSize).forEach((c, k) => {
341
+ v.append(this.image(c, start + k));
342
+ });
533
343
  }
534
- else if (view === 'wall' || view === 'story') {
344
+ else if (view === "wall" || view === "story") {
535
345
  v.classList.add(view);
536
- this.data.forEach((c, k) => v.append(this.image(c, k)));
346
+ this.data.forEach((c, k) => {
347
+ v.append(this.image(c, k));
348
+ });
537
349
  }
538
- else if (view === 'atlas') {
539
- v.classList.add('atlas');
540
- let plane = document.createElement('div');
541
- plane.className = 'plane';
542
- this.data.forEach((c, k) => plane.append(this.image(c, k)));
350
+ else if (view === "atlas") {
351
+ v.classList.add("atlas");
352
+ const plane = document.createElement("div");
353
+ plane.className = "plane";
354
+ this.data.forEach((c, k) => {
355
+ plane.append(this.image(c, k));
356
+ });
543
357
  v.append(plane);
544
358
  }
545
359
  else {
546
- v.classList.add('stack', view);
547
- let count = Math.min(3, this.data.length);
360
+ v.classList.add("stack", view);
361
+ const count = Math.min(3, this.data.length);
548
362
  for (let d = count - 1; d >= 0; d--) {
549
363
  const at = (this.index + d) % this.data.length;
550
- let c = this.data[at], el = this.image(c, at);
551
- let offset = view === 'rolodex' ? d * 42 : view === 'spotlight' ? d * 70 : d * 14;
552
- let rotation = view === 'rolodex' ? d * 12 : d * 5;
553
- el.style.transform = `translateX(calc(-50% + ${offset}px)) rotate(${rotation}deg) scale(${1 - d * .08})`;
554
- el.style.opacity = String(1 - d * .2);
364
+ const c = this.data[at];
365
+ const el = this.image(c, at);
366
+ const offset = view === "rolodex" ? d * 42 : view === "spotlight" ? d * 70 : d * 14;
367
+ const rotation = view === "rolodex" ? d * 12 : d * 5;
368
+ el.style.transform = `translateX(calc(-50% + ${offset}px)) rotate(${rotation}deg) scale(${1 - d * 0.08})`;
369
+ el.style.opacity = String(1 - d * 0.2);
555
370
  v.append(el);
556
371
  }
557
372
  }
373
+ this.attachLive(v);
558
374
  this.zoom.refresh(reset);
559
375
  if (this.flip && this.piled && t.on && this.data.length > 1)
560
- this.playFlip(this.flip, v, Math.round(t.ms * 1.15));
376
+ playFlip(this, this.flip, v, Math.round(t.ms * 1.15));
561
377
  this.flip = null;
562
378
  if (playing)
563
- this.play(how, before, left, view, v, t.ms);
379
+ playMove(this, how, before, left, view, v, t.ms);
564
380
  }
565
381
  }
566
- if (!customElements.get('fuda-collection'))
567
- customElements.define('fuda-collection', FudaCollection);
382
+ if (!customElements.get("fuda-collection"))
383
+ customElements.define("fuda-collection", FudaCollection);