create-pmos 0.443.0 → 0.445.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 (110) hide show
  1. package/package.json +25 -25
  2. package/payload/KIT-VERSION +2 -2
  3. package/payload/planning/evals/eval-calibration.md +1 -1
  4. package/payload/planning/okrs/health-budgets.md +1 -1
  5. package/payload/state/discoveries.md +1 -1
  6. package/payload/state/log.md +1 -1
  7. package/workspace/adler-moments-test.js +8 -0
  8. package/workspace/adler-seasons-art-test.js +3 -0
  9. package/workspace/board-columns-test.js +168 -0
  10. package/workspace/board-programs-test.js +1 -1
  11. package/workspace/briefing-premium-board-test.js +91 -8
  12. package/workspace/briefing-premium-insights-test.js +83 -38
  13. package/workspace/briefing-premium-test.js +2 -1
  14. package/workspace/calm-timeline-test.js +3 -1
  15. package/workspace/changelog.json +24 -0
  16. package/workspace/goals-clear-ui-test.js +34 -20
  17. package/workspace/guide/home.md +5 -0
  18. package/workspace/guide/jira.md +13 -1
  19. package/workspace/guide/ready-made-dashboards.md +2 -2
  20. package/workspace/intel-report-ui-test.js +1 -1
  21. package/workspace/lib/board-fold.js +33 -5
  22. package/workspace/lib/board-premium.js +1 -1
  23. package/workspace/lib/console-state.js +5 -1
  24. package/workspace/lib/engine/boards.js +9 -2
  25. package/workspace/lib/factory-join.js +46 -14
  26. package/workspace/lib/freshness.js +14 -1
  27. package/workspace/lib/initiative-rows.js +38 -4
  28. package/workspace/lib/issues.js +8 -1
  29. package/workspace/lib/jira-cli.js +18 -2
  30. package/workspace/lib/jira-moves.js +119 -0
  31. package/workspace/lib/jira-tickets.js +23 -4
  32. package/workspace/lib/notes.js +4 -2
  33. package/workspace/lib/offloop.js +75 -29
  34. package/workspace/lib/page-build.js +97 -0
  35. package/workspace/lib/plan-items.js +24 -3
  36. package/workspace/lib/plan-tree.js +62 -9
  37. package/workspace/lib/ready-reports.js +22 -8
  38. package/workspace/lib/refresh.js +14 -2
  39. package/workspace/lib/routes-board-premium.js +9 -1
  40. package/workspace/lib/routes-boards.js +114 -16
  41. package/workspace/lib/routes-brief-page.js +3 -1
  42. package/workspace/lib/routes-calendar-import.js +13 -1
  43. package/workspace/lib/routes-ready-reports.js +28 -5
  44. package/workspace/lib/task-links.js +12 -1
  45. package/workspace/lib/workflows-store.js +14 -3
  46. package/workspace/live-pill-round-test.js +178 -0
  47. package/workspace/milestone-pins-test.js +1 -1
  48. package/workspace/okr-goals-ui-test.js +21 -19
  49. package/workspace/package-lock.json +2 -2
  50. package/workspace/package.json +1 -1
  51. package/workspace/panels-test.js +375 -0
  52. package/workspace/premium-rec-tab-test.js +2 -1
  53. package/workspace/premium-vw-item-test.js +10 -2
  54. package/workspace/premium-vw-jira-test.js +3 -0
  55. package/workspace/program-panel-test.js +6 -6
  56. package/workspace/public/pm/bd-cols.js +149 -0
  57. package/workspace/public/pm/bd-ini-panel.js +3 -2
  58. package/workspace/public/pm/bd-pane.js +41 -0
  59. package/workspace/public/pm/bd-span.js +29 -11
  60. package/workspace/public/pm/cozy-adler.js +21 -0
  61. package/workspace/public/pm/cozy-board.css +45 -0
  62. package/workspace/public/pm/cozy-divider.css +32 -0
  63. package/workspace/public/pm/cozy-divider.js +45 -0
  64. package/workspace/public/pm/cozy-insights.css +22 -0
  65. package/workspace/public/pm/cozy-live-pill.css +17 -22
  66. package/workspace/public/pm/cozy-live-pill.js +38 -18
  67. package/workspace/public/pm/cozy-panels.css +80 -0
  68. package/workspace/public/pm/cozy-panels.js +691 -0
  69. package/workspace/public/pm/cozy-viewer.js +148 -70
  70. package/workspace/public/pm/dash-view.js +5 -2
  71. package/workspace/public/pm/goals-clear.js +9 -2
  72. package/workspace/public/pm/home-app.js +30 -19
  73. package/workspace/public/pm/home-ini.js +9 -3
  74. package/workspace/public/pm/home-today.js +4 -2
  75. package/workspace/public/pm/ini-edit.js +405 -0
  76. package/workspace/public/pm/ini-tasks.js +175 -46
  77. package/workspace/public/pm/ini-versions.js +18 -4
  78. package/workspace/public/pm/ini-view-model.js +12 -7
  79. package/workspace/public/pm/ini-view.css +162 -26
  80. package/workspace/public/pm/ini-view.js +44 -13
  81. package/workspace/public/pm/ini-wf-model.js +9 -0
  82. package/workspace/public/pm/ini-wf.js +14 -9
  83. package/workspace/public/pm/ins-pane.js +55 -0
  84. package/workspace/public/pm/jira-move.js +50 -0
  85. package/workspace/public/pm/live-model.js +3 -2
  86. package/workspace/public/pm/pn-narrow.js +76 -0
  87. package/workspace/public/pm/premium-board.js +85 -32
  88. package/workspace/public/pm/premium-insights.js +211 -43
  89. package/workspace/public/pm/task-card.css +2 -0
  90. package/workspace/public/pm/task-card.js +4 -1
  91. package/workspace/public/premium-board.html +2 -0
  92. package/workspace/ready-dash-ui-test.js +4 -4
  93. package/workspace/server.js +71 -20
  94. package/workspace/skills/jira-move.skill +35 -0
  95. package/workspace/stage-card-tasks-test.js +397 -0
  96. package/workspace/stage-card-test.js +1 -1
  97. package/workspace/task-delete-test.js +9 -1
  98. package/workspace/task-links-test.js +1 -0
  99. package/workspace/task-milestones-test.js +3 -3
  100. package/workspace/task-plans-test.js +1 -1
  101. package/workspace/task-press-test.js +12 -4
  102. package/workspace/task-views-test.js +166 -13
  103. package/workspace/test-lists/batch18-live-pill-round.json +10 -0
  104. package/workspace/test-lists/batch18-panels.json +9 -0
  105. package/workspace/test-lists/board-columns.json +5 -0
  106. package/workspace/test-lists/jira-moves.json +11 -0
  107. package/workspace/test-lists/page-speed-fold.json +5 -0
  108. package/workspace/test-lists/page-speed-reads.json +5 -0
  109. package/workspace/test-lists/stage-card-tasks.json +6 -0
  110. package/workspace/timeline-holds-test.js +1 -1
@@ -0,0 +1,691 @@
1
+ /* pm/cozy-panels.js — panels side by side, like a tablet (feedback batch 18 item 15, build 15-A; canvas In development TbT1Split to TbT9Divider,
2
+ * sticky "What to build"). Board and Intelligence too (panels on Board and Intelligence, build P-A; canvas In development BiB1–BiB9,
3
+ * BiI1–BiI4, brief feedback-batch/panels-board-intel/build-brief.md).
4
+ *
5
+ * Home is already a split: the chat, a divider, the Viewer. On Board and Intelligence the page itself is the first panel: opening a Viewer
6
+ * splits the screen into the page, the divider and the Viewer, and nothing floats over the page unless it is placed as slide over.
7
+ * Side by side up to 2 panels on a laptop and up to 3 at 1600 px and wider, in one row. A phone keeps today's one panel (the engine is off).
8
+ * Divider between two panels (role separator). A drag snaps at ⅓, ½ and ⅔ of the room; a drag to an edge closes that side (a Viewer
9
+ * closes, the chat slides away; the page never closes: a drag to its edge makes the Viewer full screen). Double-click resets
10
+ * it to ½. ← and → move it 24 px, Home resets it.
11
+ * The first panel and one Viewer (today's split): the divider drives the Viewer's own width (cozy-viewer.js setWidth: on
12
+ * Home the list folds to a rail first, the width is kept per kind). Anything else is laid out here with shares of the row.
13
+ * Open beside drag a source row (Home: a list row, a Live pill row, a file or link chip in the chat; a page: its own rows) onto the
14
+ * panels. Over a Viewer, that Viewer turns blue and the drop replaces it; at the far edge (or, with no Viewer, the right
15
+ * half) a blue zone shows and the row opens in a panel of its own. ⌘/Ctrl-click or a middle click on a source does the same
16
+ * as a drop at the far edge (openBeside). When a new panel would go over the limit, it replaces the panel used least
17
+ * recently (a Viewer closes, the chat slides over); on a page with no room the new Viewer slides over instead. A plain
18
+ * click still replaces the active Viewer (the one used last). Task rows in a stage card keep their own drag (item 16).
19
+ * Active with 2 or more Viewers open, the one a plain click replaces (the one clicked, focused or scrolled in last) has a thin
20
+ * blue edge (.cz-pn-active, BiB8Active). A link inside a Viewer changes that same Viewer (cozy-viewer.js).
21
+ * ⧉ on every Viewer's header (and the chat's, icon-only, before ×): Full screen, Split, Slide over (TbT7Place).
22
+ * Slide over one panel at a time floats over the right side (the chat 400 px, a Viewer 560 px), with a grab bar. A drag or a swipe
23
+ * off the right edge puts it away; the Live pill brings it back (slideOver). Nothing pops open by itself. The page panel
24
+ * never slides over and is never put away.
25
+ * Narrow a page gets the class cz-pn-narrow while its panel is narrower than fit() (the width its current view needs whole);
26
+ * the page's narrow form keys on it. A phone keeps the page's own media queries (no class).
27
+ * Remembered Home: each initiative reopens with its panels and shares (localStorage 'cz-pn:<initiative id>', like Android app pairs).
28
+ * A page: only the divider share (localStorage 'cz-pn:<key>'); the open Viewers are not restored. A private window
29
+ * forgets it, and the page falls back to today's split.
30
+ * Present Home: while a recording presents (BroadcastChannel 'pmos-present': present or show), the chat slides away by itself; on
31
+ * Stop (off, or the recording ends) it comes back.
32
+ *
33
+ * mountPanels(o) once, on the page.
34
+ * Home: { desk?, chat?, go?(item) → true, reopen?(item) }. go: the page opens a Live pill row in its chat (Home's own Live pill
35
+ * handler). reopen: the page opens a remembered Viewer item ({ id, kind, title }) again, or says false.
36
+ * A page: { desk?, page, sources, clicks?, fit?() → px, key? }. page: the page's main element, the first panel (never put away, never
37
+ * over). sources: the selector of the rows that can open beside (drag and ⌘-click). clicks: rows that open beside on ⌘-click
38
+ * only (a By stage card, whose drag moves the stage). fit(): the least width at which the page's current view is whole (about
39
+ * 880 for the Board's Track or Timeline and the Intelligence table, 360 for a list); the Viewer opens at the width its kind keeps
40
+ * (560 px at first) but never so wide that the page goes under fit(). key: the name the share is kept under ('board', 'intel').
41
+ * desk defaults to the page's parent; a Viewer host (data-cz-viewer-host) is added to it when it has none.
42
+ * window.czPanels = { list(), place(id, 'full'|'split'|'over'), slideOver(item?), openBeside(row|item), putAway(), away(), onChange(fn),
43
+ * scope(id) }
44
+ * panel ids: 'chat' or 'page' (the first panel), 'v0', 'v1', 'v2' (v0 is #czViewer). list() → [{ id, kind: 'chat'|'page'|'viewer',
45
+ * mode: 'split'|'over'|'away', full, active, title }]. slideOver(item): a Live pill row (item.sid, a session) opens in the chat, and the
46
+ * chat floats over when it is not in the row; a Viewer item ({ kind, title, render | html }) opens in a Viewer that floats over; no
47
+ * item brings back the panel put away. It returns false when it did nothing, so the caller falls back to its own open.
48
+ * openBeside(row): a source element opens beside (as a drop at the far edge); a Viewer item opens in a Viewer of its own. away() → the
49
+ * panel put away last, or null. scope(id) (Home): the page shows that initiative now (null: none); its panels are restored.
50
+ * The document gets the event 'cz-panels' with { panels: list(), over, away } after each change.
51
+ * The divider's look (Adler's sprout, the day's charm) is cozy-divider.js's (build 15-B); this module owns the element, its drag and keys. */
52
+ import { panels as P, open as openViewer, widthOf, PHONE } from './cozy-viewer.js';
53
+ import { dividerHtml } from './cozy-divider.js';
54
+ import { onRecording } from './rec-state.js';
55
+
56
+ const SHEET = new URL('./cozy-panels.css', import.meta.url).href;
57
+ const SNAP = [1 / 3, 1 / 2, 2 / 3], SNAPPX = 20, EDGE = 120, STEP = 24, WIDE = 1600, PANEL = 440, AWAYPX = 96, START = 560, MINV = 420, GAP = 22;
58
+ const HOME_SRC = '#hmList button[data-hm-sel], #czLiveList a[data-lv], #hmChat [data-file], #hmChat [data-ctx], #hmChat [data-att]';
59
+ const IC = {
60
+ full: 'M3.5 5h17v14h-17z', split: 'M3.5 5h17v14h-17zM12 5v14', over: 'M3.5 5h17v14h-17zM14 8h4v8h-4z',
61
+ place: 'M3.5 5h17v14h-17zM12 5v14', close: 'M6 6l12 12M18 6L6 18',
62
+ };
63
+ const NAME = { full: 'Full screen', split: 'Split', over: 'Slide over' };
64
+ const svg = (d, w = 18) => `<svg aria-hidden="true" width="${w}" height="${w}" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="${d}"/></svg>`;
65
+ const store = {
66
+ get(k) { try { return JSON.parse(localStorage.getItem(k) || 'null'); } catch { return null; } },
67
+ set(k, v) { try { if (v == null) localStorage.removeItem(k); else localStorage.setItem(k, JSON.stringify(v)); } catch { /* a private window */ } },
68
+ };
69
+ const isPhone = () => innerWidth <= PHONE;
70
+ const maxRow = () => (innerWidth >= WIDE ? 3 : 2);
71
+ /* C is the first panel's id: 'chat' on Home, 'page' on a page (S.page). S.chat is its element either way. */
72
+ let C = 'chat';
73
+ const S = { on: false, page: false, desk: null, chat: null, src: HOME_SRC, clicks: '', fitFn: null, key: '', row: ['chat'], over: null, away: [], fr: [], used: {}, subs: [], go: null, reopen: null, pend: null, scope: null, auto: false, restoring: false, keepList: false, noClick: 0, divs: [] };
74
+
75
+ /* ── the panels and their elements ── */
76
+ const isV = (id) => /^v\d$/.test(id || '');
77
+ const vs = () => P.list().filter((p) => p.open);
78
+ const elOf = (id) => (id === C ? S.chat : (P.list().find((p) => p.id === id) || {}).el || null);
79
+ const visible = (id) => S.row.includes(id) || S.over === id;
80
+ /** The page panel stays: it never gives way, slides over or is put away (only a Viewer full screen hides it, until Split or a close). */
81
+ const pinned = (id) => S.page && id === C;
82
+ const drop = (id) => { S.row = S.row.filter((x) => x !== id); if (S.over === id) S.over = null; S.away = S.away.filter((x) => x !== id); };
83
+ const mark = (id) => { if (id) S.used[id] = performance.now(); };
84
+ const lru = (ids) => ids.slice().sort((a, b) => (S.used[a] || 0) - (S.used[b] || 0))[0];
85
+ /** Today's split, laid out by cozy-viewer.js: the first panel alone, or it then one Viewer, nothing over and nothing put away. */
86
+ const simple = () => (S.page || (!S.over && !S.away.length)) && S.row[0] === C && S.row.length <= 2; // a page: a Viewer over leaves the split as it is
87
+ const managed = () => S.on && !isPhone() && !simple();
88
+ const pageW = () => (S.fitFn ? Math.max(0, +S.fitFn() || 0) : 0);
89
+
90
+ /** The row and the open Viewers agree: a closed Viewer leaves, the row is never empty, and never over the limit. */
91
+ function sync() {
92
+ const open = vs().map((p) => p.id);
93
+ [...S.row, S.over, ...S.away].filter((id) => isV(id) && !open.includes(id)).forEach(drop);
94
+ if (S.page && S.over === C) S.over = null;
95
+ if (!S.row.length) {
96
+ if (S.over) { S.row = [S.over]; S.over = null; }
97
+ else { const v = [...S.away].reverse().find(isV); if (v) { drop(v); S.row = [v]; } else { drop(C); S.row = [C]; } }
98
+ }
99
+ if (!open.length && !S.row.includes(C)) { drop(C); S.row.unshift(C); } // no Viewer: the chat (or the page) is the page, as today
100
+ if (S.page && S.row.includes(C) && S.row[0] !== C) { S.row = [C, ...S.row.filter((x) => x !== C)]; S.fr = []; } // the page is first
101
+ fit();
102
+ }
103
+ /** Over the limit (a new panel, a narrower window): the panel used least recently gives way. The chat slides over (or away when a panel
104
+ * is over already); a Viewer is put away when soft (a narrower window), else it closes. The page never gives way. */
105
+ function fit(keep, soft) {
106
+ while (S.row.length > maxRow()) {
107
+ const can = S.row.filter((x) => x !== keep && !pinned(x));
108
+ if (!can.length) break;
109
+ const id = lru(can);
110
+ S.row = S.row.filter((x) => x !== id);
111
+ if (id === C) { if (!S.over) S.over = C; else S.away.push(C); }
112
+ else if (soft) S.away.push(id);
113
+ else { S.away.push(id); setTimeout(() => P.close(id), 0); } // hidden until it closes
114
+ }
115
+ }
116
+ function shares() {
117
+ if (S.fr.length !== S.row.length) S.fr = S.row.map(() => 1 / S.row.length);
118
+ return S.fr;
119
+ }
120
+
121
+ /* ── the layout ── */
122
+ function layout() {
123
+ if (!S.on || !S.desk) return;
124
+ sync();
125
+ const B = document.body, phone = isPhone(), man = managed(), ids = [C, ...vs().map((p) => p.id)];
126
+ const back = S.man && !man; // back to today's split: cozy-viewer.js decides the width and the rail again
127
+ if (back) { S.keepList = false; P.rail(false); }
128
+ S.man = man;
129
+ B.classList.toggle('cz-pn', !phone);
130
+ B.classList.toggle('cz-pn-man', man);
131
+ const fr = shares();
132
+ ids.forEach((id) => {
133
+ const el = elOf(id); if (!el) return;
134
+ const at = S.row.indexOf(id);
135
+ el.classList.toggle('cz-pn-row', !phone && at >= 0);
136
+ el.classList.toggle('cz-pn-over', !phone && S.over === id);
137
+ el.classList.toggle('cz-pn-away', !phone && S.away.includes(id));
138
+ el.style.order = !phone && man && at >= 0 ? String(at * 2) : '';
139
+ el.style.flex = !phone && man && at >= 0 ? `${fr[at]} 1 0px` : '';
140
+ if (S.page && id !== C) el.style.marginLeft = !phone && man && at > 0 ? GAP + 'px' : ''; // a page's row has no flex gap (stray body children)
141
+ grab(el, !phone && S.over === id);
142
+ });
143
+ const host = S.desk.querySelector('[data-cz-viewer-host]');
144
+ if (host) host.style.order = !phone && !man ? '2' : '';
145
+ if (host && S.page) host.style.marginLeft = !phone && !man ? GAP + 'px' : '';
146
+ if (S.chat) S.chat.style.order = !phone && !man ? '0' : S.chat.style.order;
147
+ divs(phone ? 0 : S.row.length - 1, man);
148
+ if (man && !phone && !S.page) {
149
+ const room = S.desk.clientWidth - 64 - (S.row.length - 1) * 22; // the desk less its padding and the gaps
150
+ const list = document.getElementById('hmList'), lw = list ? list.offsetWidth || 316 : 0;
151
+ const fold = !S.keepList && (!S.row.includes(C) || room - lw < S.row.length * PANEL);
152
+ if (fold !== P.railOn()) P.rail(fold);
153
+ }
154
+ if (back) P.rest(); else P.relayout();
155
+ if (S.page && !man && !phone) S.row.filter(isV).forEach((id) => P.size(id, startW(id), false)); // the page keeps its view whole (fit)
156
+ tools();
157
+ active();
158
+ narrow();
159
+ emit();
160
+ if (S.scope && !S.restoring && !S.page) store.set('cz-pn:' + S.scope, snapshot());
161
+ }
162
+ function emit() {
163
+ const d = { panels: list(), over: S.over, away: S.away.slice() };
164
+ S.subs.forEach((f) => { try { f(d); } catch { /* the listener's own */ } });
165
+ document.dispatchEvent(new CustomEvent('cz-panels', { detail: d }));
166
+ }
167
+ function list() {
168
+ const one = S.row.length === 1, act = activeId();
169
+ return [C, ...vs().map((p) => p.id)].map((id) => {
170
+ const v = P.list().find((p) => p.id === id);
171
+ const title = id !== C ? (v && v.item && v.item.title) || '' : S.page ? (document.title || '').replace(/^PMOS · /, '') || 'Page' : ((document.getElementById('hmCh') || {}).textContent || '').trim() || 'Chat';
172
+ return { id, kind: id === C ? C : 'viewer', mode: S.over === id ? 'over' : S.away.includes(id) ? 'away' : 'split', full: one && S.row[0] === id, active: id === act, title };
173
+ });
174
+ }
175
+
176
+ /* ── the start width on a page (rule 0): the Viewer's kind keeps its width, but the page stays whole; a moved divider's share wins ── */
177
+ function room() {
178
+ const cs = getComputedStyle(S.desk);
179
+ return S.desk.clientWidth - (parseFloat(cs.paddingLeft) || 0) - (parseFloat(cs.paddingRight) || 0);
180
+ }
181
+ function startW(id) {
182
+ const v = P.list().find((p) => p.id === id), r = room() - GAP, m = store.get('cz-pn:' + S.key);
183
+ if (m && m.v === 2 && m.share > 0 && m.share < 1) return Math.round(r * m.share);
184
+ const want = (v && v.item && widthOf(v.item.kind)) || START;
185
+ return Math.max(MINV, Math.min(want, r - pageW()));
186
+ }
187
+ /** The page panel narrower than its view needs: cz-pn-narrow, its narrow form (P-B). Not on a phone: the page's media queries keep it. */
188
+ function narrow() {
189
+ if (!S.page || !S.chat) return;
190
+ const w = S.chat.getBoundingClientRect().width;
191
+ S.chat.classList.toggle('cz-pn-narrow', !isPhone() && w > 0 && w < pageW());
192
+ }
193
+
194
+ /* ── the active Viewer (BiB8Active): the one a plain click replaces, marked only while 2 or more Viewers show ── */
195
+ function activeId() {
196
+ const open = vs().filter((p) => visible(p.id));
197
+ if (open.length < 2) return null;
198
+ return open.slice().sort((a, b) => b.used - a.used)[0].id;
199
+ }
200
+ function active() {
201
+ const a = activeId();
202
+ P.list().forEach((p) => { if (p.el) p.el.classList.toggle('cz-pn-active', !isPhone() && p.id === a); });
203
+ }
204
+ /** A press, a focus or a scroll in a Viewer makes it the one a plain click replaces. */
205
+ function used(id) {
206
+ if (!isV(id)) return;
207
+ const was = activeId();
208
+ P.use(id);
209
+ if (activeId() !== was) { active(); emit(); }
210
+ }
211
+
212
+ /* ── the dividers (TbT2Divider, TbT9Divider) ── */
213
+ function divs(n, man) {
214
+ while (S.divs.length > n) S.divs.pop().remove();
215
+ while (S.divs.length < n) S.divs.push(divider());
216
+ S.divs.forEach((d, k) => {
217
+ d.dataset.k = String(k);
218
+ d.style.order = man ? String(k * 2 + 1) : '1';
219
+ if (d.__first !== (k === 0)) { d.__first = k === 0; d.querySelector('.cz-pn-in').innerHTML = dividerHtml({ first: k === 0 }); }
220
+ if (d.parentNode !== S.desk) S.desk.appendChild(d);
221
+ });
222
+ }
223
+ function divider() {
224
+ const d = document.createElement('div');
225
+ d.className = 'cz-pn-div'; d.tabIndex = 0;
226
+ d.setAttribute('role', 'separator'); d.setAttribute('aria-orientation', 'vertical');
227
+ d.setAttribute('aria-label', 'Drag to resize the panels. Double-click to reset to half.'); d.title = 'Drag to resize. Double-click to reset.';
228
+ d.innerHTML = '<span class="cz-pn-in"></span>';
229
+ d.addEventListener('pointerenter', () => d.classList.add('hov'));
230
+ d.addEventListener('pointerleave', () => d.classList.remove('hov'));
231
+ d.addEventListener('pointerdown', (e) => dragDiv(d, e));
232
+ d.addEventListener('dblclick', () => resetDiv(+d.dataset.k));
233
+ d.addEventListener('keydown', (e) => {
234
+ if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') { e.preventDefault(); nudge(+d.dataset.k, e.key === 'ArrowLeft' ? -STEP : STEP); }
235
+ else if (e.key === 'Home') { e.preventDefault(); resetDiv(+d.dataset.k); }
236
+ });
237
+ return d;
238
+ }
239
+ const rect = (id) => { const el = elOf(id); return el ? el.getBoundingClientRect() : null; };
240
+ /** Where the divider k may sit: the pair it parts, the row's left and right, the gap. */
241
+ function geo(k) {
242
+ const a = S.row[k], b = S.row[k + 1], ra = rect(a), rb = rect(b), r0 = rect(S.row[0]), r1 = rect(S.row[S.row.length - 1]);
243
+ if (!ra || !rb || !r0 || !r1) return null;
244
+ return { a, b, ra, rb, L: r0.left, R: r1.right, gap: Math.max(0, rb.left - ra.right) };
245
+ }
246
+ /** x snapped at ⅓, ½ or ⅔ of the row when it is near one. */
247
+ function snap(x, g) {
248
+ const total = g.R - g.L;
249
+ for (const q of SNAP) { const at = g.L + q * total; if (Math.abs(x - at) < SNAPPX) return { x: at, q }; }
250
+ return { x, q: 0 };
251
+ }
252
+ /** The divider k at x: the two panels share their room again (laid out here), or the Viewer takes the width (today's split). On a page
253
+ * the share is what is kept (rule 7), not the kind's width. */
254
+ function setAt(k, x, g, keep) {
255
+ if (!managed()) {
256
+ const w = g.R - x - g.gap / 2;
257
+ if (S.page) { if (keep) store.set('cz-pn:' + S.key, { v: 2, share: Math.min(0.95, Math.max(0.05, w / (g.R - g.L - g.gap))) }); P.size(g.b, w, false); if (keep) layout(); return; }
258
+ P.size(g.b, w, keep); return;
259
+ }
260
+ const wa = Math.max(60, x - g.ra.left - g.gap / 2), wb = Math.max(60, g.rb.right - x - g.gap / 2), fr = shares(), s = fr[k] + fr[k + 1];
261
+ fr[k] = s * wa / (wa + wb); fr[k + 1] = s - fr[k];
262
+ [g.a, g.b].forEach((id, i) => { const el = elOf(id); if (el) el.style.flex = `${fr[k + i]} 1 0px`; });
263
+ if (keep) layout();
264
+ }
265
+ function dragDiv(d, e) {
266
+ if (e.button !== 0) return;
267
+ const k = +d.dataset.k, g = geo(k); if (!g) return;
268
+ e.preventDefault(); d.setPointerCapture(e.pointerId);
269
+ d.classList.add('drag'); document.body.classList.add('cz-pn-drag', 'cz-vw-drag');
270
+ mark(g.a); mark(g.b);
271
+ let last = null;
272
+ const mv = (ev) => {
273
+ const s = snap(ev.clientX, g);
274
+ const shut = ev.clientX - g.L < EDGE ? g.a : g.R - ev.clientX < EDGE ? g.b : null; // to an edge: that side closes on the drop
275
+ d.classList.toggle('snap', !!s.q); d.dataset.snap = s.q ? String(Math.round(s.q * 6)) : '';
276
+ [g.a, g.b].forEach((id) => { const el = elOf(id); if (el) el.classList.toggle('cz-pn-shut', id === shut); });
277
+ last = { x: s.x, shut };
278
+ if (!shut) setAt(k, s.x, g, false);
279
+ };
280
+ const up = () => {
281
+ d.removeEventListener('pointermove', mv); d.removeEventListener('pointerup', up); d.removeEventListener('pointercancel', up);
282
+ d.classList.remove('drag', 'snap'); document.body.classList.remove('cz-pn-drag', 'cz-vw-drag');
283
+ [g.a, g.b].forEach((id) => { const el = elOf(id); if (el) el.classList.remove('cz-pn-shut'); });
284
+ if (!last) return;
285
+ if (last.shut && pinned(last.shut)) place(g.b, 'full'); // the page never closes: the Viewer takes the screen
286
+ else if (last.shut) shut(last.shut); else setAt(k, last.x, g, true);
287
+ };
288
+ d.addEventListener('pointermove', mv); d.addEventListener('pointerup', up); d.addEventListener('pointercancel', up);
289
+ }
290
+ function nudge(k, dx) {
291
+ const g = geo(k); if (!g) return;
292
+ setAt(k, g.ra.right + g.gap / 2 + dx, g, true);
293
+ }
294
+ function resetDiv(k) {
295
+ const g = geo(k); if (!g) return;
296
+ if (!managed()) {
297
+ if (S.page) { store.set('cz-pn:' + S.key, { v: 2, share: 0.5 }); layout(); return; }
298
+ P.reset(g.b); layout(); return;
299
+ }
300
+ S.fr = S.row.map(() => 1 / S.row.length); layout();
301
+ }
302
+ /** A panel leaves the row: a Viewer closes, the chat slides away (the Live pill brings it back). The page never leaves. */
303
+ function shut(id) {
304
+ if (isV(id)) { P.close(id); return; }
305
+ if (pinned(id)) return;
306
+ drop(C); S.away.push(C); S.fr = []; layout();
307
+ }
308
+
309
+ /* ── the ⧉ menu (TbT7Place) ── */
310
+ let menu = null;
311
+ function closeMenu(focus) {
312
+ if (!menu) return;
313
+ const a = menu.__anchor; menu.remove(); menu = null;
314
+ document.removeEventListener('pointerdown', away, true);
315
+ if (a) { a.setAttribute('aria-expanded', 'false'); if (focus && a.isConnected) a.focus(); }
316
+ }
317
+ function away(e) { if (menu && !menu.contains(e.target) && !(menu.__anchor && menu.__anchor.contains(e.target))) closeMenu(false); }
318
+ function modeOf(id) { return S.over === id ? 'over' : S.row.length === 1 && S.row[0] === id ? 'full' : S.row.includes(id) ? 'split' : 'away'; }
319
+ function openMenu(id, anchor) {
320
+ if (menu) { const same = menu.__anchor === anchor; closeMenu(false); if (same) return; }
321
+ mark(id);
322
+ const now = modeOf(id);
323
+ const m = document.createElement('div');
324
+ m.className = 'cz-pn-menu'; m.setAttribute('role', 'menu'); m.setAttribute('aria-label', 'Arrange'); m.dataset.escLayer = '1';
325
+ m.innerHTML = ['full', 'split', 'over'].map((k) => `<button type="button" role="menuitemradio" aria-checked="${now === k}" data-cz-pn-place="${k}" aria-label="${NAME[k]}" title="${NAME[k]}">${svg(IC[k], 20)}</button>`).join('');
326
+ m.addEventListener('click', (e) => { const b = e.target.closest('[data-cz-pn-place]'); if (!b) return; closeMenu(false); place(id, b.dataset.czPnPlace); });
327
+ m.addEventListener('keydown', (e) => {
328
+ const bs = [...m.querySelectorAll('button')], at = bs.indexOf(document.activeElement);
329
+ if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); closeMenu(true); }
330
+ else if (/^Arrow(Left|Right|Up|Down)$/.test(e.key)) { e.preventDefault(); const f = /Right|Down/.test(e.key) ? 1 : -1; bs[(at + f + bs.length) % bs.length].focus(); }
331
+ });
332
+ m.__anchor = anchor; menu = m;
333
+ document.body.appendChild(m);
334
+ const r = anchor.getBoundingClientRect();
335
+ m.style.top = Math.round(r.bottom + 6) + 'px';
336
+ m.style.left = Math.round(Math.max(8, Math.min(innerWidth - m.offsetWidth - 8, r.right - m.offsetWidth / 2 - r.width / 2))) + 'px';
337
+ anchor.setAttribute('aria-expanded', 'true');
338
+ (m.querySelector('[aria-checked="true"]') || m.querySelector('button')).focus();
339
+ document.addEventListener('pointerdown', away, true);
340
+ }
341
+
342
+ /* ── placing a panel ── */
343
+ function known(id) { return id === C || vs().some((p) => p.id === id); }
344
+ function place(id, mode) {
345
+ if (!S.on || isPhone() || !known(id) || !['full', 'split', 'over'].includes(mode)) return false;
346
+ if (pinned(id) && mode !== 'split') return false; // the page changes only through its own controls: it never slides over
347
+ mark(id);
348
+ const was = S.row.slice();
349
+ drop(id);
350
+ if (mode === 'full') {
351
+ // the others give way: the chat (or the page) steps away (the Live pill, Split or a close brings it back), the other Viewers close
352
+ // (the row is set first: a Viewer that closes lays the panels out again, and must find this one already full)
353
+ const others = [...S.row, S.over, ...S.away].filter((x) => x && x !== id);
354
+ S.row = [id]; S.over = null; S.away = others.includes(C) ? [C] : []; S.fr = [];
355
+ layout();
356
+ others.filter(isV).forEach((x) => P.close(x));
357
+ return true;
358
+ } else if (mode === 'split') {
359
+ if (!S.row.length) { // it was full: the panel put away comes back beside it
360
+ const back = (S.over && !pinned(S.over) ? S.over : null) || S.away.pop() || null; if (back) drop(back);
361
+ S.row = !back ? [id] : back === C ? [C, id] : [id, back];
362
+ } else if (id === C) S.row.unshift(id); else S.row.push(id);
363
+ fit(id);
364
+ } else {
365
+ if (S.over) { const o = S.over; S.over = null; S.away.push(o); }
366
+ if (!S.row.length) { const back = [...S.away].reverse().find((x) => x !== id); if (back) { drop(back); S.row = [back]; } }
367
+ if (!S.row.length) { S.row = was.length ? was : [id]; layout(); return false; } // nothing to slide over
368
+ S.over = id;
369
+ }
370
+ S.fr = [];
371
+ layout();
372
+ return true;
373
+ }
374
+ function putAway() {
375
+ if (!S.over || pinned(S.over)) return false;
376
+ const id = S.over; S.over = null; S.away.push(id);
377
+ layout();
378
+ return true;
379
+ }
380
+ /** The panel put away last that the Live pill can bring back (never the page hidden behind a full screen Viewer). */
381
+ const lastAway = () => [...S.away].reverse().find((x) => !pinned(x)) || null;
382
+ function slideOver(item) {
383
+ if (!S.on || isPhone()) return false;
384
+ const pend = S.pend && Date.now() - S.pend.t < 6000 ? S.pend : null;
385
+ if (item && (item.sid || item.kind === 'rec' || item.kind === 'session')) {
386
+ // a Live pill row: the page opens the chat (Home's own handler); the chat comes where it can be seen
387
+ if (!S.go || !S.go(item)) return false;
388
+ if (item.kind === 'rec') return true;
389
+ mark(C);
390
+ if (pend) { S.pend = null; P.unbeside(); drop(C); if (pend.side === 'left') S.row.unshift(C); else S.row.push(C); S.fr = []; fit(C); layout(); }
391
+ else if (!visible(C)) place(C, 'over');
392
+ return true;
393
+ }
394
+ if (item && (typeof item.render === 'function' || item.html)) { // a Viewer of its own, floating over
395
+ S.pend = { mode: 'over', t: Date.now() }; P.beside();
396
+ openViewer({ ...item, fresh: true });
397
+ return true;
398
+ }
399
+ const back = lastAway();
400
+ if (!back) return false;
401
+ S.away = S.away.filter((x) => x !== back);
402
+ if (S.over) S.away.push(S.over);
403
+ S.over = back; mark(back); P.use(back);
404
+ layout();
405
+ return true;
406
+ }
407
+ /** Where a new panel beside goes: the right of the row. On a laptop page that already shows a Viewer, it slides over (rule 5, "on a
408
+ * laptop the second item slides over"); at 1600 px and wider the Viewer used least recently gives way (fit). */
409
+ function besideTo() { return S.page && maxRow() === 2 && S.row.length >= 2 ? { mode: 'over', t: Date.now() } : { side: 'right', t: Date.now() }; }
410
+ /** ⌘/Ctrl-click, a middle click or the API: a source row opens beside (as a drop at the far edge); a Viewer item opens in a Viewer of its own. */
411
+ function openBeside(x) {
412
+ if (!S.on || isPhone() || !x) return false;
413
+ S.pend = besideTo(); P.beside();
414
+ if (x.nodeType === 1) { S.noMod = Date.now(); x.click(); }
415
+ else if (typeof x.render === 'function' || x.html) openViewer({ ...x, fresh: true });
416
+ else { S.pend = null; P.unbeside(); return false; }
417
+ setTimeout(() => { if (S.pend && Date.now() - S.pend.t > 5500) { S.pend = null; P.unbeside(); } }, 6000);
418
+ return true;
419
+ }
420
+
421
+ /* ── a new or closed Viewer ── */
422
+ function onViewer(d) {
423
+ if (!S.on || !d || !d.panel) return;
424
+ if (d.shut) { drop(d.panel); S.fr = []; layout(); return; }
425
+ mark(d.panel);
426
+ if (S.row.includes(d.panel) || S.over === d.panel || S.away.includes(d.panel)) {
427
+ if (S.away.includes(d.panel)) { drop(d.panel); if (S.over) S.away.push(S.over); S.over = d.panel; } // a plain click into the Viewer put away: it slides back over
428
+ layout(); return;
429
+ }
430
+ const pend = S.pend && Date.now() - S.pend.t < 6000 ? S.pend : null; S.pend = null;
431
+ if (pend && pend.mode === 'over') { if (S.over) S.away.push(S.over); S.over = d.panel; }
432
+ else if (pend && pend.side === 'left' && !S.page) S.row.unshift(d.panel);
433
+ else S.row.push(d.panel);
434
+ S.fr = [];
435
+ fit(d.panel);
436
+ layout();
437
+ }
438
+
439
+ /* ── the chat's own ⧉ and × (TbT1Split): only while a Viewer is open, when there is something to arrange. A page has its own controls. ── */
440
+ function tools() {
441
+ if (!S.chat || S.page) return;
442
+ let t = S.chat.querySelector(':scope > .cz-pn-ct');
443
+ const want = S.on && !isPhone() && vs().length > 0;
444
+ document.body.classList.toggle('cz-pn-ctl', want);
445
+ if (!want) { if (t) t.remove(); return; }
446
+ if (!t) {
447
+ t = document.createElement('div'); t.className = 'cz-pn-ct';
448
+ t.innerHTML = `<button type="button" class="cz-vw-ib" data-cz-pn-chat="place" aria-label="Arrange the chat" title="Arrange" aria-haspopup="menu" aria-expanded="false">${svg(IC.place)}</button>`
449
+ + `<button type="button" class="cz-vw-ib" data-cz-pn-chat="away" aria-label="Put the chat away" title="Put the chat away. The Live pill brings it back.">${svg(IC.close)}</button>`;
450
+ t.addEventListener('click', (e) => {
451
+ const b = e.target.closest('[data-cz-pn-chat]'); if (!b) return;
452
+ if (b.dataset.czPnChat === 'place') openMenu(C, b);
453
+ else if (S.over === C) putAway(); else shut(C);
454
+ });
455
+ S.chat.appendChild(t);
456
+ }
457
+ }
458
+ /** The grab bar on the panel that slides over: a drag or swipe off the right edge puts it away. */
459
+ function grab(el, want) {
460
+ let g = el.querySelector(':scope > .cz-pn-grab');
461
+ if (!want) { if (g) g.remove(); el.style.transform = ''; return; }
462
+ if (g) return;
463
+ g = document.createElement('div'); g.className = 'cz-pn-grab'; g.setAttribute('aria-hidden', 'true'); g.title = 'Drag to the right to put it away';
464
+ g.innerHTML = '<i></i>';
465
+ g.addEventListener('pointerdown', (e) => {
466
+ if (e.button !== 0) return;
467
+ e.preventDefault(); g.setPointerCapture(e.pointerId);
468
+ const x0 = e.clientX; let dx = 0;
469
+ el.classList.add('cz-pn-moving');
470
+ const mv = (ev) => { dx = Math.max(0, ev.clientX - x0); el.style.transform = `translateX(${dx}px)`; };
471
+ const up = () => {
472
+ g.removeEventListener('pointermove', mv); g.removeEventListener('pointerup', up); g.removeEventListener('pointercancel', up);
473
+ el.classList.remove('cz-pn-moving'); el.style.transform = '';
474
+ if (dx > AWAYPX) putAway();
475
+ };
476
+ g.addEventListener('pointermove', mv); g.addEventListener('pointerup', up); g.addEventListener('pointercancel', up);
477
+ });
478
+ el.prepend(g);
479
+ }
480
+
481
+ /* ── open beside: drag a row onto the panels (TbT5DragRow, BiB4DragRow, BiB9DropOn) ── */
482
+ let D = null;
483
+ function area() {
484
+ const r0 = rect(S.row[0]), r1 = rect(S.row[S.row.length - 1]);
485
+ if (!r0 || !r1) return null;
486
+ return { left: r0.left, right: r1.right, top: Math.min(r0.top, r1.top), bottom: Math.max(r0.bottom, r1.bottom) };
487
+ }
488
+ function dragStart() {
489
+ const label = (D.src.getAttribute('aria-label') || D.src.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 60);
490
+ D.ghost = document.createElement('div'); D.ghost.className = 'cz-pn-ghost'; D.ghost.textContent = label || 'Open beside';
491
+ D.zone = document.createElement('div'); D.zone.className = 'cz-pn-drop'; D.zone.hidden = true;
492
+ document.body.append(D.zone, D.ghost);
493
+ document.body.classList.add('cz-pn-dragrow');
494
+ D.on = true;
495
+ }
496
+ /** What a drop at (x, y) does: { on: <Viewer id> } replaces that Viewer; { side } opens beside on that side; null does nothing. With the
497
+ * box the blue zone covers. A Viewer that slides over is on top, so it is asked first. */
498
+ function target(x, y) {
499
+ const a = area(); if (!a) return null;
500
+ const vh = Math.min(a.bottom, innerHeight), top = Math.max(a.top, 0);
501
+ const strip = (side) => ({ side, box: { left: side === 'left' ? a.left : a.right - EDGE, top, width: EDGE, height: vh - top } });
502
+ const inBox = (r) => r && x >= r.left && x <= r.right && y >= r.top && y <= r.bottom;
503
+ const ro = S.over && isV(S.over) ? rect(S.over) : null;
504
+ if (inBox(ro)) return { on: S.over, box: ro };
505
+ const inside = x >= a.left && x <= a.right && y >= top && y <= vh;
506
+ if (!inside) return null;
507
+ if (x > a.right - EDGE && S.row.some(isV)) return strip('right');
508
+ if (x < a.left + EDGE && !S.page && S.row.some(isV)) return strip('left');
509
+ const v = S.row.filter(isV).find((id) => inBox(rect(id)));
510
+ if (v) return { on: v, box: rect(v) };
511
+ if (S.page) { // over the page: with no Viewer the right half opens one; the page itself changes only through its own controls
512
+ if (S.row.some(isV)) return null;
513
+ const mid = (a.left + a.right) / 2;
514
+ return x >= mid ? { side: 'right', box: { left: mid, top, width: a.right - mid, height: vh - top } } : null;
515
+ }
516
+ const mid = (a.left + a.right) / 2, side = x < mid ? 'left' : 'right';
517
+ return { side, box: { left: side === 'left' ? a.left : mid, top, width: side === 'left' ? mid - a.left : a.right - mid, height: vh - top } };
518
+ }
519
+ function dragMove(e) {
520
+ D.ghost.style.left = (e.clientX + 14) + 'px'; D.ghost.style.top = (e.clientY + 10) + 'px';
521
+ const t = target(e.clientX, e.clientY);
522
+ D.t = t;
523
+ D.zone.hidden = !t;
524
+ if (t) {
525
+ Object.assign(D.zone.style, { top: t.box.top + 'px', height: t.box.height + 'px', left: t.box.left + 'px', width: t.box.width + 'px' });
526
+ D.zone.dataset.side = t.side || '';
527
+ D.zone.dataset.on = t.on || '';
528
+ }
529
+ }
530
+ /** A selector that finds the row again: the list may draw itself anew while the row is dragged. */
531
+ function again(src) {
532
+ if (S.page) {
533
+ const a = [...src.attributes].find((x) => /^data-/.test(x.name) && x.value);
534
+ return a ? `[${a.name}="${CSS.escape(a.value)}"]` : null;
535
+ }
536
+ for (const k of ['hmSel', 'lv', 'file', 'ctx', 'att']) {
537
+ if (src.dataset[k] == null) continue;
538
+ const attr = 'data-' + k.replace(/[A-Z]/g, (c) => '-' + c.toLowerCase());
539
+ return `${src.closest('#hmList') ? '#hmList ' : src.closest('#czLiveList') ? '#czLiveList ' : '#hmChat '}[${attr}="${CSS.escape(src.dataset[k])}"]`;
540
+ }
541
+ return null;
542
+ }
543
+ function dragEnd(d) {
544
+ d.ghost.remove(); d.zone.remove(); document.body.classList.remove('cz-pn-dragrow');
545
+ const t = d.t; if (!t) return;
546
+ S.noClick = Date.now(); // the press that ends a drag is not also a click
547
+ const click = () => { S.noClick = 0; const src = d.src.isConnected ? d.src : d.sel && (S.page ? S.chat.querySelector(d.sel) || document.querySelector(d.sel) : document.querySelector(d.sel)); if (src) { S.noMod = Date.now(); src.click(); } };
548
+ if (t.on) { // onto a Viewer: that Viewer shows the row (BiB9DropOn)
549
+ S.pend = null; P.unbeside(); mark(t.on); P.use(t.on);
550
+ setTimeout(click, 0);
551
+ return;
552
+ }
553
+ S.pend = t.side === 'right' ? besideTo() : { side: t.side, t: Date.now() };
554
+ if (!d.src.matches('#czLiveList a[data-lv]')) P.beside();
555
+ setTimeout(() => { click(); setTimeout(() => { if (S.pend && Date.now() - S.pend.t > 5500) { S.pend = null; P.unbeside(); } }, 6000); }, 0);
556
+ }
557
+ function dragWire() {
558
+ document.addEventListener('pointerdown', (e) => {
559
+ if (!S.on || isPhone() || e.button !== 0 || e.pointerType === 'touch') return;
560
+ const src = e.target.closest && e.target.closest(S.src); if (!src) return;
561
+ D = { src, sel: again(src), x0: e.clientX, y0: e.clientY, pid: e.pointerId, on: false };
562
+ }, true);
563
+ document.addEventListener('pointermove', (e) => {
564
+ if (!D || e.pointerId !== D.pid) return;
565
+ if (!D.on) { if (Math.hypot(e.clientX - D.x0, e.clientY - D.y0) < 8) return; dragStart(); }
566
+ e.preventDefault(); dragMove(e);
567
+ }, true);
568
+ document.addEventListener('pointerup', (e) => { if (!D || e.pointerId !== D.pid) return; const d = D; D = null; if (d.on) dragEnd(d); }, true);
569
+ document.addEventListener('pointercancel', () => { if (D && D.on) { D.ghost.remove(); D.zone.remove(); document.body.classList.remove('cz-pn-dragrow'); } D = null; }, true);
570
+ document.addEventListener('dragstart', (e) => { if (S.on && e.target.closest && e.target.closest(S.src)) e.preventDefault(); }, true); // a link drags our way
571
+ document.addEventListener('click', (e) => {
572
+ if (e.isTrusted && S.noClick && Date.now() - S.noClick < 500) { e.preventDefault(); e.stopPropagation(); S.noClick = 0; return; }
573
+ // ⌘/Ctrl-click on a page's source row opens it beside (the row's own handler opens it; the engine says where)
574
+ if (!S.page || !S.on || isPhone() || !(e.metaKey || e.ctrlKey) || (S.noMod && Date.now() - S.noMod < 300)) return;
575
+ const src = e.target.closest && e.target.closest(besideSel()); if (!src) return;
576
+ S.pend = besideTo(); P.beside();
577
+ if (src.matches('a[href]')) e.preventDefault(); // not a new browser tab
578
+ }, true);
579
+ // a middle click on a source row opens it beside too
580
+ document.addEventListener('auxclick', (e) => {
581
+ if (!S.page || !S.on || isPhone() || e.button !== 1) return;
582
+ const src = e.target.closest && e.target.closest(besideSel()); if (!src) return;
583
+ e.preventDefault(); openBeside(src);
584
+ }, true);
585
+ document.addEventListener('mousedown', (e) => { if (S.page && e.button === 1 && e.target.closest && e.target.closest(besideSel())) e.preventDefault(); }, true); // no autoscroll
586
+ }
587
+ const besideSel = () => [S.src, S.clicks].filter(Boolean).join(', ');
588
+
589
+ /* ── remembered per initiative on Home (rule 7) ── */
590
+ function snapshot() {
591
+ return {
592
+ v: 1, n: S.row.length, fr: S.fr.slice(), chat: S.over === C ? 'over' : S.away.includes(C) ? 'away' : 'split', chatAt: Math.max(0, S.row.indexOf(C)),
593
+ items: vs().map((p) => ({ ...(p.item || {}), at: S.row.indexOf(p.id), over: S.over === p.id })).filter((x) => x.id),
594
+ };
595
+ }
596
+ const nextOpen = (ms) => new Promise((res) => { const f = (e) => { if (e.detail && !e.detail.shut) { document.removeEventListener('cz-viewer', f); res(true); } }; document.addEventListener('cz-viewer', f); setTimeout(() => { document.removeEventListener('cz-viewer', f); res(false); }, ms); });
597
+ async function restore(id) {
598
+ const m = store.get('cz-pn:' + id);
599
+ if (!m || m.v !== 1 || isPhone()) return;
600
+ S.restoring = true;
601
+ try {
602
+ if (!vs().length) await nextOpen(2500); // the page opens the initiative's own Viewer first
603
+ if (S.scope !== id) return;
604
+ const open = vs().map((p) => p.item && p.item.id);
605
+ for (const it of (m.items || []).filter((x) => !open.includes(x.id)).slice(0, maxRow() - 1)) {
606
+ if (!S.reopen) break;
607
+ S.pend = it.over ? { mode: 'over', t: Date.now() } : { side: it.at === 0 ? 'left' : 'right', t: Date.now() };
608
+ P.beside();
609
+ const wait = nextOpen(2500);
610
+ let ok = false; try { ok = await S.reopen(it); } catch { ok = false; }
611
+ if (ok === false) { S.pend = null; P.unbeside(); continue; }
612
+ await wait;
613
+ if (S.scope !== id) return;
614
+ }
615
+ if (vs().length) {
616
+ if (m.chat === 'away' && visible(C)) { drop(C); S.away.push(C); }
617
+ else if (m.chat === 'over' && S.over !== C) { drop(C); if (S.over) S.away.push(S.over); S.over = C; }
618
+ if (S.row.length === m.n && Array.isArray(m.fr) && m.fr.length === m.n) S.fr = m.fr.slice();
619
+ }
620
+ } finally {
621
+ S.restoring = false;
622
+ layout();
623
+ }
624
+ }
625
+ function scope(id) {
626
+ id = id || null;
627
+ if (S.page || id === S.scope) return;
628
+ S.scope = id;
629
+ if (id) restore(id);
630
+ }
631
+
632
+ /* ── Present (Home): the chat slides away while a recording presents, and comes back on Stop (rule 8) ── */
633
+ function presentOn() {
634
+ if (S.page || S.auto || isPhone() || !vs().length || !visible(C)) return;
635
+ drop(C); S.away.push(C); S.auto = true; layout();
636
+ }
637
+ function presentOff() {
638
+ if (!S.auto) return;
639
+ S.auto = false;
640
+ if (S.away.includes(C)) { drop(C); S.row.unshift(C); S.fr = []; fit(C, true); }
641
+ layout();
642
+ }
643
+
644
+ export function mountPanels(o = {}) {
645
+ if (S.on || typeof document === 'undefined') return window.czPanels;
646
+ if (o.page) {
647
+ S.page = true; C = 'page'; S.row = [C];
648
+ S.chat = o.page; S.desk = o.desk || o.page.parentNode;
649
+ S.src = o.sources || ''; S.clicks = o.clicks || ''; S.fitFn = typeof o.fit === 'function' ? o.fit : null;
650
+ S.key = o.key || o.page.id || 'page';
651
+ if (!S.desk) return null;
652
+ if (!S.desk.querySelector('[data-cz-viewer-host]')) { const h = document.createElement('div'); h.className = 'cz-pn-host'; h.setAttribute('data-cz-viewer-host', ''); S.desk.appendChild(h); }
653
+ document.body.classList.add('cz-pn-page');
654
+ document.querySelectorAll('.cz-vw.cz-vw-float').forEach((el) => { document.querySelector('[data-cz-viewer-host]').appendChild(el); el.classList.remove('cz-vw-float'); }); // opened before the mount
655
+ } else {
656
+ S.desk = o.desk || document.querySelector('[data-cz-viewer-host]') && document.querySelector('[data-cz-viewer-host]').parentNode;
657
+ S.chat = o.chat || document.getElementById('hmChat');
658
+ if (!S.desk || !S.chat) return null;
659
+ S.go = typeof o.go === 'function' ? o.go : null;
660
+ S.reopen = typeof o.reopen === 'function' ? o.reopen : null;
661
+ }
662
+ // the sheet sets the desk's padding: a Viewer opened before it lands (?focus= on load) is sized again once it has, so the page stays whole
663
+ if (!document.querySelector(`link[href="${SHEET}"]`)) { const l = document.createElement('link'); l.rel = 'stylesheet'; l.href = SHEET; l.onload = () => layout(); document.head.appendChild(l); }
664
+ S.on = true;
665
+ P.config({ limit: 3, place: (id, b) => openMenu(id, b), managed: (id) => managed() && (S.row.includes(id) || S.over === id || S.away.includes(id)) });
666
+ document.addEventListener('cz-viewer', (e) => onViewer(e.detail));
667
+ document.addEventListener('cz:vw-rail', (e) => { if (managed() && e.detail && !e.detail.on) S.keepList = true; }); // the rail's list button: the list stays
668
+ ['pointerdown', 'focusin', 'wheel'].forEach((k) => document.addEventListener(k, (e) => {
669
+ const t = e.target; if (!t || !t.closest) return;
670
+ const p = t.closest('[data-cz-panel]');
671
+ if (p) { mark(p.dataset.czPanel); used(p.dataset.czPanel); } else if (S.chat.contains(t)) mark(C);
672
+ }, { capture: true, passive: true }));
673
+ dragWire();
674
+ let rz = 0;
675
+ addEventListener('resize', () => { cancelAnimationFrame(rz); rz = requestAnimationFrame(() => { fit(null, true); S.keepList = false; layout(); }); });
676
+ if (S.page && typeof ResizeObserver !== 'undefined') new ResizeObserver(() => narrow()).observe(S.chat);
677
+ if (!S.page) {
678
+ if (typeof BroadcastChannel === 'function') {
679
+ const ch = new BroadcastChannel('pmos-present');
680
+ ch.addEventListener('message', (e) => { const t = e.data && e.data.t; if (t === 'present' || t === 'show') presentOn(); else if (t === 'off') presentOff(); });
681
+ }
682
+ onRecording((s) => { if (!s || !s.on) presentOff(); });
683
+ }
684
+ window.czPanels = {
685
+ list, place, slideOver, putAway, scope, openBeside,
686
+ away: lastAway,
687
+ onChange(fn) { if (typeof fn === 'function') S.subs.push(fn); },
688
+ };
689
+ layout();
690
+ return window.czPanels;
691
+ }