archaeopteryx 3.22.0 → 3.24.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.
package/archaeopteryx.js CHANGED
@@ -20,7 +20,7 @@
20
20
  *
21
21
  */
22
22
 
23
- // v 3.22.0
23
+ // v 3.24.0
24
24
  // 2026-09-17
25
25
  //
26
26
  // Archaeopteryx.js is a software tool for the visualization and
@@ -103,7 +103,7 @@ function (root, d3, forester, phyloXml) {
103
103
  // IIFE's own function name -- a plain object says what it is.)
104
104
  let archaeopteryx = {};
105
105
 
106
- const VERSION = '3.22.0';
106
+ const VERSION = '3.24.0';
107
107
  const WEBSITE = 'https://cmzmasek.github.io/archaeopteryx-js/';
108
108
  const DESKTOP_WEBSITE = 'https://cmzmasek.github.io/archaeopteryx/';
109
109
  const SOURCE_WEBSITE = 'https://github.com/cmzmasek/archaeopteryx-js';
@@ -436,6 +436,8 @@ function (root, d3, forester, phyloXml) {
436
436
  const BRANCH_SCALE_CONTROLGROUP = 'branch_scale_g';
437
437
  const BRANCH_SCALE_TIME_BUTTON = 'branch_scale_time_b';
438
438
  const BRANCH_SCALE_DIV_BUTTON = 'branch_scale_div_b';
439
+ const CLOCK_PLOT_BUTTON = 'clock_plot_b';
440
+ const CLOCK_PLOT_DIALOG = 'aptx_clock_plot';
439
441
  const ABOUT_DIALOG = 'aptx_about';
440
442
  const TREE_PROPERTIES_DIALOG = 'aptx_tree_props';
441
443
  const PROG_NAME = 'progname';
@@ -681,6 +683,7 @@ function (root, d3, forester, phyloXml) {
681
683
  let _timeInfo = null; // forester.timeAxisInfo, recomputed per render
682
684
  let _timeTree = false; // forester.isTimeTree: never re-rooted
683
685
  let _branchScaleAvailable = false; // the tree states time AND divergence, so the switch is offered
686
+ let _clockPlotKind = null; // forester.clockPlotKind: which clock plot the tree has, if any
684
687
  let _clusterH = 0; // the cluster layout's vertical extent, set per render
685
688
  let _docListenersBound = false; // page-level key/wheel handlers bind once, not per launch
686
689
  let _docListeners = []; // ...and destroy() can take every one of them down again
@@ -1483,7 +1486,24 @@ function (root, d3, forester, phyloXml) {
1483
1486
  return hsvToHex({h: hsv.h, s: Math.max(hsv.s, HOVER_GLOW_SAT_FLOOR), v: Math.max(hsv.v, HOVER_GLOW_VAL_FLOOR)});
1484
1487
  }
1485
1488
 
1486
- function showHoverGlow(d) {
1489
+ // The glow's three discs, in `g`, in the colour of node d.
1490
+ function paintHoverGlow(g, d) {
1491
+ let dia = Math.max(HOVER_GLOW_MIN_DIA, (_state.nodeSizeDefault || 0) * 3);
1492
+ let fill = hoverGlowColor(d);
1493
+ HOVER_GLOW_RADII.forEach(function (radius, i) {
1494
+ g.append('circle')
1495
+ .attr('fill-opacity', HOVER_GLOW_ALPHAS[i])
1496
+ .attr('stroke', 'none')
1497
+ .attr('fill', fill)
1498
+ .attr('r', (dia * radius) / 2);
1499
+ });
1500
+ }
1501
+
1502
+ // Lights node d. `also` lights more nodes with it: one point of the clock
1503
+ // plot can stand for several tips. They are drawn INSIDE the one glow
1504
+ // group, placed from d, so whatever hides, moves or strips the glow
1505
+ // (hideHoverGlow, an export) takes all of them.
1506
+ function showHoverGlow(d, also) {
1487
1507
  if (!_svgGroup || !d || d.x === undefined) {
1488
1508
  return;
1489
1509
  }
@@ -1491,23 +1511,21 @@ function (root, d3, forester, phyloXml) {
1491
1511
  if (g.empty()) {
1492
1512
  g = _svgGroup.append('g').attr('class', 'aptx-hoverglow')
1493
1513
  .style('pointer-events', 'none');
1494
- HOVER_GLOW_RADII.forEach(function (unused, i) {
1495
- g.append('circle')
1496
- .attr('fill-opacity', HOVER_GLOW_ALPHAS[i])
1497
- .attr('stroke', 'none');
1498
- });
1499
1514
  }
1500
- let dia = Math.max(HOVER_GLOW_MIN_DIA, (_state.nodeSizeDefault || 0) * 3);
1515
+ g.selectAll('*').remove();
1501
1516
  let p = layoutPointXY(d);
1502
- let fill = hoverGlowColor(d);
1503
1517
  g.attr('transform', 'translate(' + p[0] + ',' + p[1] + ')')
1504
1518
  .style('display', null)
1505
1519
  .raise();
1506
- g.selectAll('circle')
1507
- .attr('fill', fill)
1508
- .attr('r', function (unused, i) {
1509
- return (dia * HOVER_GLOW_RADII[i]) / 2;
1510
- });
1520
+ paintHoverGlow(g, d);
1521
+ (also || []).forEach(function (n) {
1522
+ if (!n || n === d || n.x === undefined) {
1523
+ return;
1524
+ }
1525
+ let q = layoutPointXY(n);
1526
+ paintHoverGlow(g.append('g').attr('class', 'aptx-hoverglow-also')
1527
+ .attr('transform', 'translate(' + (q[0] - p[0]) + ',' + (q[1] - p[1]) + ')'), n);
1528
+ });
1511
1529
  }
1512
1530
 
1513
1531
  function hideHoverGlow() {
@@ -1518,6 +1536,7 @@ function (root, d3, forester, phyloXml) {
1518
1536
 
1519
1537
  function mouseover(event, d) {
1520
1538
  showHoverGlow(d);
1539
+ markClockPlot(d);
1521
1540
  // Start empty so the previous node's text cannot flash while this one
1522
1541
  // fades in; mousemove fills it in immediately after. Clearing here
1523
1542
  // rather than on the way out is what keeps the tooltip from collapsing
@@ -1831,6 +1850,7 @@ function (root, d3, forester, phyloXml) {
1831
1850
 
1832
1851
  function mouseout() {
1833
1852
  hideHoverGlow();
1853
+ markClockPlot(null);
1834
1854
  // Fade only. Emptying the tooltip here collapsed it to nothing but its
1835
1855
  // padding and border -- a small rounded pill -- and THAT is what sat
1836
1856
  // there fading out afterwards. The content is cleared on the next
@@ -3514,6 +3534,7 @@ function (root, d3, forester, phyloXml) {
3514
3534
  drawTimeOverlays();
3515
3535
  rebuildOverview(); // measured AFTER the overlays, so the bbox is this frame's
3516
3536
  updateSearchHitNavigation();
3537
+ renderClockPlot(false); // an open clock plot follows the view, the colours and the selection
3517
3538
  noteViewChange();
3518
3539
  }
3519
3540
 
@@ -6036,6 +6057,7 @@ function (root, d3, forester, phyloXml) {
6036
6057
  // it arrived showing. An Auspice build arrives in the time view; a
6037
6058
  // BEAST or Newick file arrives stating its own branch lengths.
6038
6059
  _branchScaleAvailable = _treeData ? forester.hasTimeAndDivergence(_treeData) : false;
6060
+ _clockPlotKind = _treeData ? forester.clockPlotKind(_treeData) : null;
6039
6061
  _state.branchScale = _treeData ? forester.branchLengthScale(_treeData) : 'divergence';
6040
6062
  if (_state.showScaleAxis === undefined) {
6041
6063
  // on for a tree SHOWN in time; a dated tree that opens in
@@ -6479,6 +6501,8 @@ function (root, d3, forester, phyloXml) {
6479
6501
  document.querySelectorAll('dialog.aptx-dialog').forEach(function (dlg) {
6480
6502
  dlg.remove();
6481
6503
  });
6504
+ _clockPlot = null;
6505
+ _clockPlotKind = null;
6482
6506
  if (_msaNav) {
6483
6507
  _msaNav.remove();
6484
6508
  _msaNav = null;
@@ -10605,6 +10629,7 @@ function (root, d3, forester, phyloXml) {
10605
10629
  if (scaleGroup) {
10606
10630
  scaleGroup.style.display = _branchScaleAvailable ? '' : 'none';
10607
10631
  }
10632
+ syncClockPlotControl();
10608
10633
  }
10609
10634
 
10610
10635
  // The tree was EDITED (a node deleted). Whether it has two layouts is asked
@@ -10623,6 +10648,7 @@ function (root, d3, forester, phyloXml) {
10623
10648
  forester.captureDivergence(_treeData);
10624
10649
  }
10625
10650
  _branchScaleAvailable = forester.hasTimeAndDivergence(_treeData);
10651
+ _clockPlotKind = forester.clockPlotKind(_treeData);
10626
10652
  if (was && _branchScaleAvailable && _state.branchScale === 'divergence') {
10627
10653
  forester.applyDivergenceBranchLengths(_treeData);
10628
10654
  } else if (was) {
@@ -11000,6 +11026,847 @@ function (root, d3, forester, phyloXml) {
11000
11026
  shell.dialog.showModal();
11001
11027
  }
11002
11028
 
11029
+ // ===================== Clock plot =====================
11030
+ // The tips of the tree on view as points, each one's date against its
11031
+ // divergence from the root, with a least-squares line through them (and
11032
+ // the ancestors' points too, when asked for): the picture TempEst,
11033
+ // TreeTime and Auspice's clock view draw, here as a
11034
+ // panel beside the tree rather than a layout of it, so an outlier is seen
11035
+ // on the plot and in the tree at once (Christian, 2026-10-02). What is
11036
+ // plotted, which trees have a plot and what the line is fitted to are
11037
+ // forester.clockPlotData's; this is the drawing and the link:
11038
+ //
11039
+ // - a point wears its node's colour in the tree (found, selected, the
11040
+ // Color visualization, the file's own styles);
11041
+ // - pointing at one lights its node in the tree, and pointing at a node
11042
+ // in the tree rings its point;
11043
+ // - a click selects or deselects the node, a drag selects the tips in
11044
+ // the box -- the tree's own selection, so everything that reads it
11045
+ // (the labels, the downloads, the host page) sees it;
11046
+ // - tips drawn on one spot are one dot, and the dot answers for all of
11047
+ // them: named together, lit together, selected together.
11048
+ //
11049
+ // The plot follows the view: in a subtree it is the clade's, with the
11050
+ // clade's own line. Modeless, and it can be dragged by its title: the
11051
+ // point is to work in the tree with it open.
11052
+ const CLOCK_PLOT_WHAT = 'clock plot: each tip\'s date against its divergence from the root, with a'
11053
+ + ' line through the tips (its slope is the rate); a panel linked to the tree';
11054
+ const CLOCK_PLOT_W = 440;
11055
+ const CLOCK_PLOT_H = 300;
11056
+ const CLOCK_PLOT_PAD = {top: 10, right: 14, bottom: 38, left: 58};
11057
+ const CLOCK_PLOT_HIT_PX = 7; // how near the pointer has to be to a point
11058
+ const CLOCK_PLOT_SAME_PX = 1; // points nearer each other than this are one dot on the screen
11059
+ const CLOCK_PLOT_NAMES_MAX = 5; // how many of a dot's tips the readout names
11060
+ const CLOCK_PLOT_DRAG_PX = 4; // a press that moves further than this is a box, not a click
11061
+ const CLOCK_PLOT_MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
11062
+ const SVG_NS = 'http://www.w3.org/2000/svg';
11063
+ let _clockPlot = null; // the open panel, or null
11064
+ // What the two checkboxes said last, kept while the page lives. Tips only
11065
+ // to begin with (Christian, 2026-10-02): the line is theirs, and a deep
11066
+ // root crowds them into a corner (dengue: a root in 1319, tips from 1944).
11067
+ let _clockPlotOptions = {line: true, internal: false};
11068
+
11069
+ // The button is shown where the tree has a plot, and lit while the panel
11070
+ // is open. (A tree that loses its plot -- the tip that made it deleted --
11071
+ // loses the panel at its next render: see renderClockPlot.)
11072
+ function syncClockPlotControl() {
11073
+ let button = byId(CLOCK_PLOT_BUTTON);
11074
+ if (button) {
11075
+ button.style.display = _clockPlotKind ? '' : 'none';
11076
+ button.classList.toggle('aptx-lit', !!_clockPlot);
11077
+ }
11078
+ }
11079
+
11080
+ function toggleClockPlot() {
11081
+ if (_clockPlot) {
11082
+ closeClockPlot();
11083
+ } else {
11084
+ showClockPlot();
11085
+ }
11086
+ }
11087
+
11088
+ // Closes the panel NOW. A dialog's own `close` event arrives in a later
11089
+ // task -- much later in a tab the browser is throttling -- and while the
11090
+ // viewer still believed the panel open, a press of the button "closed"
11091
+ // the closed panel again and did nothing (seen on the live demo,
11092
+ // 2026-10-02). So nothing here waits for the event.
11093
+ function closeClockPlot() {
11094
+ let cp = _clockPlot;
11095
+ if (!cp) {
11096
+ return;
11097
+ }
11098
+ _clockPlot = null;
11099
+ hideHoverGlow();
11100
+ if (cp.dialog.open) {
11101
+ cp.dialog.close();
11102
+ }
11103
+ cp.dialog.remove();
11104
+ syncClockPlotControl();
11105
+ }
11106
+
11107
+ function clockSvg(parent, tag, cls) {
11108
+ let el = document.createElementNS(SVG_NS, tag);
11109
+ if (cls) {
11110
+ el.setAttribute('class', cls);
11111
+ }
11112
+ parent.appendChild(el);
11113
+ return el;
11114
+ }
11115
+
11116
+ function showClockPlot() {
11117
+ if (!_clockPlotKind || !_treeData) {
11118
+ return;
11119
+ }
11120
+ let shell = makeDialogShell(CLOCK_PLOT_DIALOG, 'Clock plot', CLOCK_PLOT_W + 26);
11121
+ let body = shell.body;
11122
+ body.classList.add('aptx-clock');
11123
+
11124
+ let svg = clockSvg(body, 'svg', 'aptx-clock-svg');
11125
+ svg.setAttribute('width', CLOCK_PLOT_W);
11126
+ svg.setAttribute('height', CLOCK_PLOT_H);
11127
+ svg.setAttribute('viewBox', '0 0 ' + CLOCK_PLOT_W + ' ' + CLOCK_PLOT_H);
11128
+ svg.setAttribute('role', 'img');
11129
+ svg.setAttribute('aria-label', 'Clock plot: each tip\'s date against its divergence from the root');
11130
+ let clip = clockSvg(clockSvg(svg, 'defs'), 'clipPath');
11131
+ clip.id = CLOCK_PLOT_DIALOG + '_clip';
11132
+ let clipRect = clockSvg(clip, 'rect');
11133
+ clipRect.setAttribute('x', CLOCK_PLOT_PAD.left);
11134
+ clipRect.setAttribute('y', CLOCK_PLOT_PAD.top);
11135
+ clipRect.setAttribute('width', CLOCK_PLOT_W - CLOCK_PLOT_PAD.left - CLOCK_PLOT_PAD.right);
11136
+ clipRect.setAttribute('height', CLOCK_PLOT_H - CLOCK_PLOT_PAD.top - CLOCK_PLOT_PAD.bottom);
11137
+ // an opaque ground under the points: the dialog is frosted glass, and
11138
+ // the tree's colours showing through would read as data
11139
+ let ground = clockSvg(svg, 'rect', 'aptx-clock-ground');
11140
+ ['x', 'y', 'width', 'height'].forEach(function (a) {
11141
+ ground.setAttribute(a, clipRect.getAttribute(a));
11142
+ });
11143
+ let axes = clockSvg(svg, 'g', 'aptx-clock-axes');
11144
+ let inside = clockSvg(svg, 'g');
11145
+ inside.setAttribute('clip-path', 'url(#' + clip.id + ')');
11146
+ let internal = clockSvg(inside, 'g', 'aptx-clock-internal');
11147
+ let line = clockSvg(inside, 'line', 'aptx-clock-line');
11148
+ let tips = clockSvg(inside, 'g', 'aptx-clock-tips');
11149
+ let ring = clockSvg(svg, 'circle', 'aptx-clock-ring');
11150
+ ring.setAttribute('r', 6.5);
11151
+ ring.style.display = 'none';
11152
+ let box = clockSvg(svg, 'rect', 'aptx-clock-box');
11153
+ box.style.display = 'none';
11154
+
11155
+ let options = document.createElement('div');
11156
+ options.className = 'aptx-clock-options';
11157
+ body.appendChild(options);
11158
+ let check = function (label, key, title) {
11159
+ let row = document.createElement('label');
11160
+ row.className = 'aptx-reps-choice';
11161
+ row.title = title;
11162
+ let input = document.createElement('input');
11163
+ input.type = 'checkbox';
11164
+ input.checked = _clockPlotOptions[key];
11165
+ input.addEventListener('change', function () {
11166
+ _clockPlotOptions[key] = input.checked;
11167
+ renderClockPlot(true);
11168
+ });
11169
+ let span = document.createElement('span');
11170
+ span.textContent = label;
11171
+ row.appendChild(input);
11172
+ row.appendChild(span);
11173
+ options.appendChild(row);
11174
+ return row;
11175
+ };
11176
+ check('Regression line', 'line', 'to show/hide the least-squares line through the tips');
11177
+ // on a root-to-tip tree no ancestor is dated: nothing to show, so the
11178
+ // choice is not offered (layClockPlot hides it by the data's kind)
11179
+ let internalRow = check('Internal nodes', 'internal', 'to show/hide the internal nodes: their dates were inferred,'
11180
+ + ' and they take no part in the line');
11181
+ let deselect = document.createElement('button');
11182
+ deselect.type = 'button';
11183
+ deselect.className = 'aptx-reps-button aptx-clock-deselect';
11184
+ deselect.textContent = 'Deselect all';
11185
+ deselect.title = 'deselect every selected node of the tree';
11186
+ deselect.addEventListener('click', function () {
11187
+ if (_selectedNodes.size > 0) {
11188
+ _selectedNodes = new Set();
11189
+ selectionChangedByClockPlot();
11190
+ }
11191
+ });
11192
+ options.appendChild(deselect);
11193
+
11194
+ let stats = document.createElement('div');
11195
+ stats.className = 'aptx-clock-stats';
11196
+ body.appendChild(stats);
11197
+ let note = document.createElement('p');
11198
+ note.className = 'aptx-shortcuts-note aptx-clock-note';
11199
+ body.appendChild(note);
11200
+
11201
+ _clockPlot = {dialog: shell.dialog, svg: svg, axes: axes, internal: internal, line: line, tips: tips,
11202
+ ring: ring, box: box, stats: stats, note: note, deselect: deselect, internalRow: internalRow,
11203
+ data: null, marks: [], seen: null, hover: null, lays: 0, x: null, y: null};
11204
+ // the shell's close control, and anything else that closes the dialog
11205
+ let closed = function () {
11206
+ if (_clockPlot && _clockPlot.dialog === shell.dialog) {
11207
+ closeClockPlot();
11208
+ }
11209
+ };
11210
+ shell.dialog.querySelector('.aptx-dialog-close').addEventListener('click', closed);
11211
+ shell.dialog.addEventListener('close', closed);
11212
+ bindClockPlotPointer(_clockPlot);
11213
+ // modeless, as the cheat sheet is: the tree stays in reach
11214
+ shell.dialog.show();
11215
+ renderClockPlot(true);
11216
+ placeClockPlot(shell.dialog);
11217
+ makeDialogDraggable(shell.dialog);
11218
+ syncClockPlotControl();
11219
+ }
11220
+
11221
+ // In the tree area's top right corner, clear of the control panel where
11222
+ // there is room, and inside the window.
11223
+ //
11224
+ // The page around the viewer may keep something of its own in that corner,
11225
+ // above everything -- the demo page's tree chooser sits there -- and the
11226
+ // panel opened under it, its close control out of reach (seen on the live
11227
+ // demo, 2026-10-02; no harness has a host page). So the panel's top edge is
11228
+ // asked what is on it, and the panel goes below whatever is not ours.
11229
+ function placeClockPlot(dialog) {
11230
+ let area = _container ? _container.getBoundingClientRect() : null;
11231
+ let w = dialog.offsetWidth;
11232
+ let h = dialog.offsetHeight;
11233
+ let right = area ? Math.min(area.right, window.innerWidth) : window.innerWidth;
11234
+ let top = area ? Math.max(area.top, 0) : 0;
11235
+ let lowest = Math.max(6, window.innerHeight - h - 6);
11236
+ let put = function (y) {
11237
+ setStyles(dialog, {
11238
+ 'position': 'fixed',
11239
+ 'margin': '0',
11240
+ 'left': Math.max(6, right - w - 14) + 'px',
11241
+ 'top': Math.max(6, Math.min(lowest, y)) + 'px'
11242
+ });
11243
+ };
11244
+ put(top + 14);
11245
+ let box = dialog.getBoundingClientRect();
11246
+ let under = 0;
11247
+ [box.left + 12, (box.left + box.right) / 2, box.right - 14].forEach(function (px) {
11248
+ let over = document.elementFromPoint(px, box.top + 14);
11249
+ if (!over || dialog.contains(over) || (_container && _container.contains(over))) {
11250
+ return;
11251
+ }
11252
+ // the whole of what is in the way, not the link or label inside it
11253
+ while (over.parentElement && over.parentElement !== document.body
11254
+ && !over.parentElement.contains(dialog)) {
11255
+ over = over.parentElement;
11256
+ }
11257
+ under = Math.max(under, over.getBoundingClientRect().bottom);
11258
+ });
11259
+ if (under > 0) {
11260
+ put(under + 8);
11261
+ }
11262
+ }
11263
+
11264
+ // A modeless dialog moved by its title bar, kept inside the window.
11265
+ function makeDialogDraggable(dialog) {
11266
+ let bar = dialog.querySelector('.aptx-dialog-title');
11267
+ if (!bar) {
11268
+ return;
11269
+ }
11270
+ bar.classList.add('aptx-dialog-drag');
11271
+ bar.addEventListener('pointerdown', function (e) {
11272
+ if (e.button !== 0 || (e.target.closest && e.target.closest('button'))) {
11273
+ return;
11274
+ }
11275
+ let start = dialog.getBoundingClientRect();
11276
+ let dx = e.clientX - start.left;
11277
+ let dy = e.clientY - start.top;
11278
+ let move = function (ev) {
11279
+ setStyles(dialog, {
11280
+ 'position': 'fixed',
11281
+ 'margin': '0',
11282
+ 'left': Math.max(0, Math.min(window.innerWidth - 40, ev.clientX - dx)) + 'px',
11283
+ 'top': Math.max(0, Math.min(window.innerHeight - 30, ev.clientY - dy)) + 'px'
11284
+ });
11285
+ };
11286
+ let up = function () {
11287
+ document.removeEventListener('pointermove', move);
11288
+ document.removeEventListener('pointerup', up);
11289
+ document.removeEventListener('pointercancel', up);
11290
+ };
11291
+ document.addEventListener('pointermove', move);
11292
+ document.addEventListener('pointerup', up);
11293
+ document.addEventListener('pointercancel', up);
11294
+ e.preventDefault();
11295
+ });
11296
+ }
11297
+
11298
+ // A number of the plot at three significant digits; very small or very
11299
+ // large in exponent form, which is how a rate is usually written.
11300
+ function clockNumber(v, digits) {
11301
+ if (typeof v !== 'number' || !isFinite(v)) {
11302
+ return '';
11303
+ }
11304
+ let sig = digits || 3;
11305
+ let a = Math.abs(v);
11306
+ if (a !== 0 && (a < 1e-4 || a >= 1e7)) {
11307
+ return v.toExponential(sig - 1);
11308
+ }
11309
+ return String(Number(v.toPrecision(sig)));
11310
+ }
11311
+
11312
+ // A date of the plot: a calendar year to two decimals (about four days),
11313
+ // anything else as a number.
11314
+ function clockDate(v, calendar) {
11315
+ return calendar ? String(forester.roundNumber(v, 2)) : clockNumber(v, 5);
11316
+ }
11317
+
11318
+ function clockTicksX(from, to, calendar) {
11319
+ if (calendar) {
11320
+ if ((to - from) >= 3) {
11321
+ return forester.calendarTickYears(from, to).map(function (y) {
11322
+ return {value: y, label: String(y)};
11323
+ });
11324
+ }
11325
+ let months = forester.calendarTickMonths(from, to);
11326
+ if (months.length >= 2) {
11327
+ return months.map(function (m) {
11328
+ return {value: m.value, label: CLOCK_PLOT_MONTHS[m.month - 1] + ' ' + m.year};
11329
+ });
11330
+ }
11331
+ }
11332
+ return d3.ticks(from, to, 6).map(function (v) {
11333
+ return {value: v, label: String(Number(v.toPrecision(12)))};
11334
+ });
11335
+ }
11336
+
11337
+ // The colour a node wears in the tree, for its point; null where it wears
11338
+ // none, and the point takes the panel's ink.
11339
+ function clockPointColor(node, tip) {
11340
+ let found = getFoundColor(node);
11341
+ if (found || !tip) {
11342
+ return found;
11343
+ }
11344
+ let vis = _state.showVisualizations ? visualizationColorFor(node) : null;
11345
+ if (vis) {
11346
+ return vis;
11347
+ }
11348
+ let style = nodeStyle(node);
11349
+ if (style && (style.nodeColor || style.fontColor)) {
11350
+ return style.nodeColor || style.fontColor;
11351
+ }
11352
+ if (_state.useVisualStyles && node.color) {
11353
+ return 'rgb(' + node.color.red + ',' + node.color.green + ',' + node.color.blue + ')';
11354
+ }
11355
+ return null;
11356
+ }
11357
+
11358
+ /**
11359
+ * Draws the open clock plot for the tree on view. Called at the end of
11360
+ * every render, so it has to cost next to nothing when what it shows has
11361
+ * not changed. The points are worked out and laid down again only for
11362
+ * another tree, another clade, a tree on view with a different number of
11363
+ * nodes (one deleted) or a checkbox; any other render -- a zoom step, a
11364
+ * selection -- costs a count of the nodes and one pass over the points
11365
+ * for their colours, with a DOM write only where one changed.
11366
+ *
11367
+ * Until review (2026-10-02) the points were worked out afresh on every
11368
+ * render and only the DRAWING was skipped, while this comment claimed
11369
+ * the opposite: 5 ms a render on a 7,199-node build, where the count
11370
+ * takes 0.2.
11371
+ *
11372
+ * @param force work everything out and lay it down again
11373
+ */
11374
+ function renderClockPlot(force) {
11375
+ let cp = _clockPlot;
11376
+ if (!cp) {
11377
+ return;
11378
+ }
11379
+ if (!cp.dialog.isConnected) { // the viewer was torn down under it
11380
+ _clockPlot = null;
11381
+ return;
11382
+ }
11383
+ let top = _treeData ? (topNode() || forester.getTreeRoot(_treeData)) : null;
11384
+ let count = 0;
11385
+ if (top) {
11386
+ forester.preOrderTraversalAll(top, function () {
11387
+ ++count;
11388
+ });
11389
+ }
11390
+ let seen = cp.seen; // (another tree is another top: no need to ask for both)
11391
+ if (force || !seen || seen.top !== top || seen.count !== count) {
11392
+ let data = top ? forester.clockPlotData(_treeData, top) : null;
11393
+ if (!data) {
11394
+ closeClockPlot(); // the tree no longer has a plot (a tip deleted)
11395
+ return;
11396
+ }
11397
+ cp.seen = {top: top, count: count};
11398
+ cp.data = data;
11399
+ layClockPlot(cp, data);
11400
+ }
11401
+ colorClockPlot(cp);
11402
+ // The render that brought us here put the tree's glow out (the node
11403
+ // may have moved). The pointer is still on its point, so the node is
11404
+ // lit again where it now is: a click on a point redraws the tree, and
11405
+ // left the point ringed and its node dark until the pointer moved to
11406
+ // another point and back.
11407
+ if (cp.hover) {
11408
+ lightClockNodes(cp.hover);
11409
+ }
11410
+ }
11411
+
11412
+ function layClockPlot(cp, data) {
11413
+ let calendar = data.calendar;
11414
+ let rootToTip = data.kind === 'root-to-tip';
11415
+ cp.internalRow.style.display = rootToTip ? 'none' : '';
11416
+ let shown = data.points.filter(function (p) {
11417
+ return p.tip || _clockPlotOptions.internal;
11418
+ });
11419
+ let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
11420
+ shown.forEach(function (p) {
11421
+ x0 = Math.min(x0, p.date);
11422
+ x1 = Math.max(x1, p.date);
11423
+ y0 = Math.min(y0, p.div);
11424
+ y1 = Math.max(y1, p.div);
11425
+ });
11426
+ let padX = ((x1 - x0) * 0.04) || 0.5;
11427
+ let padY = ((y1 - y0) * 0.06) || (Math.abs(y1) * 0.06) || 1e-6;
11428
+ let left = CLOCK_PLOT_PAD.left;
11429
+ let right = CLOCK_PLOT_W - CLOCK_PLOT_PAD.right;
11430
+ let top = CLOCK_PLOT_PAD.top;
11431
+ let bottom = CLOCK_PLOT_H - CLOCK_PLOT_PAD.bottom;
11432
+ // time runs left to right, whichever way its numbers do
11433
+ let x = d3.scaleLinear().domain([x0 - padX, x1 + padX]).range(data.forward ? [left, right] : [right, left]);
11434
+ let y = d3.scaleLinear().domain([y0 - padY, y1 + padY]).range([bottom, top]);
11435
+ cp.x = x;
11436
+ cp.y = y;
11437
+
11438
+ // --- the frame, the ticks and what each axis measures
11439
+ while (cp.axes.firstChild) {
11440
+ cp.axes.removeChild(cp.axes.firstChild);
11441
+ }
11442
+ let seg = function (cls, xa, ya, xb, yb) {
11443
+ let l = clockSvg(cp.axes, 'line', cls);
11444
+ l.setAttribute('x1', xa);
11445
+ l.setAttribute('y1', ya);
11446
+ l.setAttribute('x2', xb);
11447
+ l.setAttribute('y2', yb);
11448
+ };
11449
+ let text = function (cls, s, tx, ty, anchor) {
11450
+ let t = clockSvg(cp.axes, 'text', cls);
11451
+ t.setAttribute('x', tx);
11452
+ t.setAttribute('y', ty);
11453
+ t.setAttribute('text-anchor', anchor);
11454
+ t.textContent = s;
11455
+ return t;
11456
+ };
11457
+ clockTicksX(x.domain()[0], x.domain()[1], calendar).forEach(function (tick) {
11458
+ let px = x(tick.value);
11459
+ seg('aptx-clock-grid', px, top, px, bottom);
11460
+ text('aptx-clock-tick', tick.label, px, bottom + 13, 'middle');
11461
+ });
11462
+ d3.ticks(y.domain()[0], y.domain()[1], 5).forEach(function (v) {
11463
+ let py = y(v);
11464
+ seg('aptx-clock-grid', left, py, right, py);
11465
+ text('aptx-clock-tick', String(Number(v.toPrecision(12))), left - 6, py + 3.5, 'end');
11466
+ });
11467
+ seg('aptx-clock-frame', left, bottom, right, bottom);
11468
+ seg('aptx-clock-frame', left, top, left, bottom);
11469
+ let unit = data.unit && !calendar ? ' (' + data.unit + ')' : '';
11470
+ text('aptx-clock-title', (data.forward ? 'Date' : 'Age') + unit, (left + right) / 2, CLOCK_PLOT_H - 6, 'middle');
11471
+ let yt = text('aptx-clock-title', 'Divergence' + (data.divUnit ? ' (' + data.divUnit + ')' : ''), 0, 0, 'middle');
11472
+ yt.setAttribute('transform', 'translate(12,' + ((top + bottom) / 2) + ') rotate(-90)');
11473
+
11474
+ // --- the points: ancestors under the line, tips over it
11475
+ cp.marks = [];
11476
+ [cp.internal, cp.tips].forEach(function (g) {
11477
+ while (g.firstChild) {
11478
+ g.removeChild(g.firstChild);
11479
+ }
11480
+ });
11481
+ shown.forEach(function (p) {
11482
+ let c = clockSvg(p.tip ? cp.tips : cp.internal, 'circle');
11483
+ c.setAttribute('cx', x(p.date));
11484
+ c.setAttribute('cy', y(p.div));
11485
+ c.setAttribute('r', p.tip ? 3 : 2);
11486
+ cp.marks.push({p: p, el: c, px: x(p.date), py: y(p.div), key: ''});
11487
+ });
11488
+
11489
+ // --- the line, across the frame (clipped to it)
11490
+ let fit = data.fit;
11491
+ if (fit && _clockPlotOptions.line) {
11492
+ let d = x.domain();
11493
+ cp.line.setAttribute('x1', x(d[0]));
11494
+ cp.line.setAttribute('y1', y(fit.intercept + (fit.slope * d[0])));
11495
+ cp.line.setAttribute('x2', x(d[1]));
11496
+ cp.line.setAttribute('y2', y(fit.intercept + (fit.slope * d[1])));
11497
+ cp.line.style.display = '';
11498
+ } else {
11499
+ cp.line.style.display = 'none';
11500
+ }
11501
+ // every point may have moved from under the pointer: what it is on
11502
+ // now is known at its next move
11503
+ cp.hover = null;
11504
+ clockRing(cp, null);
11505
+
11506
+ // --- what the line says
11507
+ while (cp.stats.firstChild) {
11508
+ cp.stats.removeChild(cp.stats.firstChild);
11509
+ }
11510
+ let tipCount = data.points.reduce(function (n, p) {
11511
+ return n + (p.tip ? 1 : 0);
11512
+ }, 0);
11513
+ let per = (data.divUnit ? ' ' + data.divUnit : '') + ' per ' + (calendar ? 'year' : (data.unit || 'unit of time'));
11514
+ if (fit) {
11515
+ treePropRow(cp.stats, 'Rate', clockNumber(fit.rate) + per);
11516
+ treePropRow(cp.stats, 'Root date, by the line', fit.rootDate === null
11517
+ ? 'none: divergence does not rise with time' : clockDate(fit.rootDate, calendar));
11518
+ treePropRow(cp.stats, 'Root date, in the tree', data.root.date === null
11519
+ ? 'none: only the tips are dated' : clockDate(data.root.date, calendar));
11520
+ treePropRow(cp.stats, 'R²', fit.r2 === null ? 'none: every tip has one divergence' : clockNumber(fit.r2));
11521
+ } else {
11522
+ treePropRow(cp.stats, 'Line', 'none: it takes three tips, not all on one date');
11523
+ }
11524
+ treePropRow(cp.stats, 'Tips', countNumber(tipCount) + (_in_subtree ? ' (the clade on view)' : ''));
11525
+ cp.note.textContent = (data.fromRates
11526
+ ? 'Divergence here is each branch’s time × its clock rate: the points show the model’s rates, not a measurement of their own. '
11527
+ : '')
11528
+ + (rootToTip
11529
+ ? 'Divergence here is each tip’s distance from the root along the branch lengths the file states; only the tips are dated. '
11530
+ : '')
11531
+ + (data.fromNames > 0
11532
+ ? (data.fromNames === 1 ? 'One tip is dated by its name' : countNumber(data.fromNames) + ' tips are dated by their names')
11533
+ + ' (a year alone is taken as mid-year'
11534
+ + (data.ambiguousNames > 0 ? '; ' + countNumber(data.ambiguousNames) + ' with day and month ambiguous, read day first' : '')
11535
+ + '). '
11536
+ : '')
11537
+ + 'The line is fitted to the tips only. Tips share ancestry, so R² describes the fit and is no test.'
11538
+ + ' Click a point to select its node; drag to select the tips in a box.';
11539
+ // what was laid down, for the browser harness: read here, where it is
11540
+ // computed, not off the DOM
11541
+ cp.dialog._aptxClockPlot = {data: data, shown: shown.length, calendar: calendar, lays: ++cp.lays};
11542
+ }
11543
+
11544
+ // One pass: a point whose colour or highlight differs from what it wears
11545
+ // is repainted, and a highlighted one is lifted over its neighbours.
11546
+ function colorClockPlot(cp) {
11547
+ for (let i = 0; i < cp.marks.length; ++i) {
11548
+ let m = cp.marks[i];
11549
+ let hit = getFoundColor(m.p.node) !== null;
11550
+ let color = clockPointColor(m.p.node, m.p.tip);
11551
+ let key = (color || '') + (hit ? '*' : '');
11552
+ if (key === m.key) {
11553
+ continue;
11554
+ }
11555
+ m.key = key;
11556
+ m.el.style.fill = color || '';
11557
+ m.el.style.stroke = hit ? makeFoundOutlineColor(m.p.node) : '';
11558
+ m.el.setAttribute('r', hit ? 4 : (m.p.tip ? 3 : 2));
11559
+ m.el.setAttribute('class', hit ? 'aptx-clock-hit' : '');
11560
+ if (hit) {
11561
+ m.el.parentNode.appendChild(m.el);
11562
+ }
11563
+ }
11564
+ cp.deselect.disabled = _selectedNodes.size === 0;
11565
+ }
11566
+
11567
+ // The node a point's node is seen AS in the tree: itself, or the
11568
+ // collapsed clade that holds it (the highest one, which is the one drawn).
11569
+ function clockTreeNode(node) {
11570
+ let seen = node;
11571
+ let top = topNode();
11572
+ for (let n = node.parent; n && n !== _root; n = n.parent) {
11573
+ if (isCollapsed(n)) {
11574
+ seen = n;
11575
+ }
11576
+ if (n === top) {
11577
+ break;
11578
+ }
11579
+ }
11580
+ return seen;
11581
+ }
11582
+
11583
+ /**
11584
+ * The points under the pointer: the nearest one within reach, and every
11585
+ * other of its kind drawn on the same spot. Tips with one date and one
11586
+ * divergence -- identical sequences sampled on one day -- are one dot on
11587
+ * the screen (778 of the 2,985 tips of a measles build share theirs with
11588
+ * another), and a dot that answered for only the first of them left the
11589
+ * rest beyond pointing, naming or deselecting (code review; Christian,
11590
+ * 2026-10-02: the dot stands for all of them). The SAME SPOT, not
11591
+ * everything within reach: in a dense cloud that is hundreds of tips,
11592
+ * and a click meant for one dot would take them all.
11593
+ *
11594
+ * @returns {Array} the marks, in the tree's own order, top to bottom;
11595
+ * empty where the pointer is on none
11596
+ */
11597
+ function clockMarksAt(cp, px, py) {
11598
+ let best = null;
11599
+ let bestD = CLOCK_PLOT_HIT_PX * CLOCK_PLOT_HIT_PX;
11600
+ for (let i = 0; i < cp.marks.length; ++i) {
11601
+ let m = cp.marks[i];
11602
+ let dx = m.px - px;
11603
+ let dy = m.py - py;
11604
+ let d = (dx * dx) + (dy * dy);
11605
+ // a tip wins over an ancestor at the same distance: it is the datum
11606
+ if (d < bestD || (d === bestD && best && m.p.tip && !best.p.tip)) {
11607
+ best = m;
11608
+ bestD = d;
11609
+ }
11610
+ }
11611
+ if (!best) {
11612
+ return [];
11613
+ }
11614
+ // (the points were laid down from a walk that takes the last child
11615
+ // first, so the tree's order is theirs backwards)
11616
+ return cp.marks.filter(function (m) {
11617
+ return m.p.tip === best.p.tip && Math.abs(m.px - best.px) < CLOCK_PLOT_SAME_PX
11618
+ && Math.abs(m.py - best.py) < CLOCK_PLOT_SAME_PX;
11619
+ }).reverse();
11620
+ }
11621
+
11622
+ function sameClockMarks(a, b) {
11623
+ if (!a || !b || a.length !== b.length) {
11624
+ return a === b;
11625
+ }
11626
+ return a.every(function (m, i) {
11627
+ return m === b[i];
11628
+ });
11629
+ }
11630
+
11631
+ // Lights, in the tree, the nodes of the points under the pointer: each as
11632
+ // the tree shows it (clockTreeNode), each once.
11633
+ function lightClockNodes(marks) {
11634
+ let nodes = [];
11635
+ marks.forEach(function (m) {
11636
+ let n = clockTreeNode(m.p.node);
11637
+ if (nodes.indexOf(n) < 0) {
11638
+ nodes.push(n);
11639
+ }
11640
+ });
11641
+ showHoverGlow(nodes[0], nodes.slice(1));
11642
+ }
11643
+
11644
+ function clockRing(cp, mark) {
11645
+ if (!mark) {
11646
+ cp.ring.style.display = 'none';
11647
+ return;
11648
+ }
11649
+ cp.ring.setAttribute('cx', mark.px);
11650
+ cp.ring.setAttribute('cy', mark.py);
11651
+ cp.ring.style.display = '';
11652
+ }
11653
+
11654
+ // A node pointed at IN THE TREE rings its point, where it has one.
11655
+ function markClockPlot(node) {
11656
+ let cp = _clockPlot;
11657
+ if (!cp) {
11658
+ return;
11659
+ }
11660
+ let mark = null;
11661
+ if (node) {
11662
+ for (let i = 0; i < cp.marks.length; ++i) {
11663
+ if (cp.marks[i].p.node === node) {
11664
+ mark = cp.marks[i];
11665
+ break;
11666
+ }
11667
+ }
11668
+ }
11669
+ clockRing(cp, mark);
11670
+ }
11671
+
11672
+ function clockSigned(v) {
11673
+ return (v > 0 ? '+' : '') + clockNumber(v, 3);
11674
+ }
11675
+
11676
+ // One value where the points agree, "least - greatest" where they do not
11677
+ // (points a fraction of a pixel apart are one dot and may differ a hair).
11678
+ function clockSpan(values, format) {
11679
+ let lo = Math.min.apply(null, values);
11680
+ let hi = Math.max.apply(null, values);
11681
+ return lo === hi ? format(lo) : (format(lo) + ' \u2013 ' + format(hi));
11682
+ }
11683
+
11684
+ // What the readout says of the points under the pointer: of one, what it
11685
+ // is; of several on one spot, how many, the names of the first few, and
11686
+ // what they share.
11687
+ function clockPointText(marks) {
11688
+ let p = marks[0].p;
11689
+ if (marks.length === 1) {
11690
+ let text = (p.node.name ? 'Name: ' + p.node.name : (p.tip ? 'Tip' : 'Internal node')) + '<br>';
11691
+ let date = dateText(p.node.date);
11692
+ if (date) {
11693
+ text += 'Date: ' + date + '<br>';
11694
+ }
11695
+ text += 'Divergence: ' + clockNumber(p.div, 4) + '<br>';
11696
+ if (typeof p.residual === 'number') {
11697
+ text += 'Off the line: ' + clockSigned(p.residual) + '<br>';
11698
+ }
11699
+ if (!p.tip) {
11700
+ text += 'Tips below: ' + forester.getAllExternalNodes(p.node).length + '<br>';
11701
+ }
11702
+ return text;
11703
+ }
11704
+ let text = marks.length + (p.tip ? ' tips here' : ' internal nodes here') + '<br>';
11705
+ let named = marks.filter(function (m) {
11706
+ return !!m.p.node.name;
11707
+ });
11708
+ named.slice(0, CLOCK_PLOT_NAMES_MAX).forEach(function (m) {
11709
+ text += 'Name: ' + m.p.node.name + '<br>';
11710
+ });
11711
+ if (named.length > CLOCK_PLOT_NAMES_MAX) {
11712
+ text += 'Name: \u2026 and ' + (named.length - CLOCK_PLOT_NAMES_MAX) + ' more<br>';
11713
+ }
11714
+ let unit = (p.node.date && p.node.date.unit) ? ' ' + p.node.date.unit : '';
11715
+ text += 'Date: ' + clockSpan(marks.map(function (m) { return m.p.date; }), String) + unit + '<br>';
11716
+ text += 'Divergence: ' + clockSpan(marks.map(function (m) { return m.p.div; }), function (v) {
11717
+ return clockNumber(v, 4);
11718
+ }) + '<br>';
11719
+ if (marks.every(function (m) { return typeof m.p.residual === 'number'; })) {
11720
+ text += 'Off the line: ' + clockSpan(marks.map(function (m) { return m.p.residual; }), clockSigned) + '<br>';
11721
+ }
11722
+ return text;
11723
+ }
11724
+
11725
+ function selectionChangedByClockPlot() {
11726
+ update(null, 0, true);
11727
+ document.dispatchEvent(new Event('selected_nodes_changed_event'));
11728
+ }
11729
+
11730
+ function bindClockPlotPointer(cp) {
11731
+ let svg = cp.svg;
11732
+ let press = null; // {x, y, boxed} while the button is down
11733
+ let at = function (e) {
11734
+ let r = svg.getBoundingClientRect();
11735
+ return [(e.clientX - r.left) * (CLOCK_PLOT_W / (r.width || CLOCK_PLOT_W)),
11736
+ (e.clientY - r.top) * (CLOCK_PLOT_H / (r.height || CLOCK_PLOT_H))];
11737
+ };
11738
+ let readout = function (e, marks) {
11739
+ if (!_node_mouseover_div) {
11740
+ return;
11741
+ }
11742
+ if (!marks) {
11743
+ _node_mouseover_div.interrupt().style('opacity', 1e-6);
11744
+ return;
11745
+ }
11746
+ let tip = _node_mouseover_div.node();
11747
+ tip.classList.remove('aptx-light', 'aptx-dark');
11748
+ if (_panelTheme) {
11749
+ tip.classList.add('aptx-' + _panelTheme);
11750
+ }
11751
+ _node_mouseover_div.html(markUpDataLabels(escapeHtmlKeepBreaks(clockPointText(marks))));
11752
+ _node_mouseover_div.interrupt().style('opacity', 0.95);
11753
+ placeHoverReadout(tip, e);
11754
+ };
11755
+ // cp.hover: the points the pointer is on (null for none), kept for the
11756
+ // renders in between
11757
+ let point = function (e) {
11758
+ let xy = at(e);
11759
+ let found = clockMarksAt(cp, xy[0], xy[1]);
11760
+ let marks = found.length > 0 ? found : null;
11761
+ if (!sameClockMarks(marks, cp.hover)) {
11762
+ cp.hover = marks;
11763
+ clockRing(cp, marks ? marks[0] : null);
11764
+ if (marks) {
11765
+ lightClockNodes(marks);
11766
+ } else {
11767
+ hideHoverGlow();
11768
+ }
11769
+ svg.style.cursor = marks ? 'pointer' : '';
11770
+ }
11771
+ readout(e, marks);
11772
+ };
11773
+ svg.addEventListener('pointerdown', function (e) {
11774
+ if (e.button !== 0) {
11775
+ return;
11776
+ }
11777
+ let xy = at(e);
11778
+ press = {x: xy[0], y: xy[1], boxed: false};
11779
+ try {
11780
+ svg.setPointerCapture(e.pointerId); // the box follows the pointer out of the drawing
11781
+ } catch {
11782
+ // no pointer capture available; a release outside is then missed
11783
+ }
11784
+ e.preventDefault();
11785
+ });
11786
+ svg.addEventListener('pointermove', function (e) {
11787
+ if (!press) {
11788
+ point(e);
11789
+ return;
11790
+ }
11791
+ let xy = at(e);
11792
+ if (!press.boxed && Math.hypot(xy[0] - press.x, xy[1] - press.y) > CLOCK_PLOT_DRAG_PX) {
11793
+ press.boxed = true;
11794
+ readout(e, null);
11795
+ clockRing(cp, null);
11796
+ hideHoverGlow();
11797
+ cp.hover = null;
11798
+ }
11799
+ if (press.boxed) {
11800
+ cp.box.setAttribute('x', Math.min(press.x, xy[0]));
11801
+ cp.box.setAttribute('y', Math.min(press.y, xy[1]));
11802
+ cp.box.setAttribute('width', Math.abs(xy[0] - press.x));
11803
+ cp.box.setAttribute('height', Math.abs(xy[1] - press.y));
11804
+ cp.box.style.display = '';
11805
+ }
11806
+ });
11807
+ let release = function (e, cancelled) {
11808
+ if (!press) {
11809
+ return;
11810
+ }
11811
+ let was = press;
11812
+ press = null;
11813
+ cp.box.style.display = 'none';
11814
+ if (cancelled) {
11815
+ return;
11816
+ }
11817
+ let xy = at(e);
11818
+ if (was.boxed) {
11819
+ // the tips in the box join the selection; an ancestor is
11820
+ // selected by a click, where it is one node and meant
11821
+ let xa = Math.min(was.x, xy[0]), xb = Math.max(was.x, xy[0]);
11822
+ let ya = Math.min(was.y, xy[1]), yb = Math.max(was.y, xy[1]);
11823
+ let added = 0;
11824
+ cp.marks.forEach(function (m) {
11825
+ if (m.p.tip && m.px >= xa && m.px <= xb && m.py >= ya && m.py <= yb
11826
+ && !_selectedNodes.has(m.p.node)) {
11827
+ _selectedNodes.add(m.p.node);
11828
+ ++added;
11829
+ }
11830
+ });
11831
+ if (added > 0) {
11832
+ selectionChangedByClockPlot();
11833
+ }
11834
+ return;
11835
+ }
11836
+ // a click selects every node of the dot; where all of them are
11837
+ // selected already, it deselects them
11838
+ let marks = clockMarksAt(cp, xy[0], xy[1]);
11839
+ if (marks.length > 0) {
11840
+ let all = marks.every(function (m) {
11841
+ return _selectedNodes.has(m.p.node);
11842
+ });
11843
+ marks.forEach(function (m) {
11844
+ if (all) {
11845
+ _selectedNodes.delete(m.p.node);
11846
+ } else {
11847
+ _selectedNodes.add(m.p.node);
11848
+ }
11849
+ });
11850
+ selectionChangedByClockPlot();
11851
+ }
11852
+ };
11853
+ svg.addEventListener('pointerup', function (e) {
11854
+ release(e, false);
11855
+ });
11856
+ svg.addEventListener('pointercancel', function (e) {
11857
+ release(e, true);
11858
+ });
11859
+ svg.addEventListener('pointerleave', function (e) {
11860
+ if (!press) {
11861
+ cp.hover = null;
11862
+ clockRing(cp, null);
11863
+ hideHoverGlow();
11864
+ readout(e, null);
11865
+ svg.style.cursor = '';
11866
+ }
11867
+ });
11868
+ }
11869
+
11003
11870
  // ===================== Protein domain architectures =====================
11004
11871
  // The desktop's domain display, ported from its RenderableDomainArchitecture
11005
11872
  // and TreePanel (the spec is section D1 of the repo's TODO.md). Each tip's
@@ -15256,6 +16123,14 @@ function (root, d3, forester, phyloXml) {
15256
16123
  for (let i = 0; i < btns.length; ++i) {
15257
16124
  btns[i].innerHTML = panelThemeIcon(); // a drawn glyph, not a text character
15258
16125
  }
16126
+ // a modeless dialog (the cheat sheet, the clock plot) stays open across
16127
+ // the switch, and took the theme only when it was made
16128
+ document.querySelectorAll('dialog.aptx-dialog').forEach(function (dlg) {
16129
+ dlg.classList.remove('aptx-light', 'aptx-dark');
16130
+ if (_panelTheme === 'light' || _panelTheme === 'dark') {
16131
+ dlg.classList.add('aptx-' + _panelTheme);
16132
+ }
16133
+ });
15259
16134
  applyTreeTheme();
15260
16135
  }
15261
16136
 
@@ -16002,6 +16877,29 @@ function (root, d3, forester, phyloXml) {
16002
16877
  + ' color:var(--p-accent-ink); }'
16003
16878
  + '.aptx-reps-primary { font-weight:600; }'
16004
16879
  + '.aptx-reps-button:focus-visible { outline:none; box-shadow:0 0 0 3px var(--p-accent-weak); }'
16880
+ + '.aptx-reps-button:disabled { opacity:0.4; cursor:default; pointer-events:none; }'
16881
+ // the clock plot: a drawing in the panel's own inks, so it turns
16882
+ // with the theme; a point's colour, where it has one, is the
16883
+ // node's in the tree and is set on the point itself
16884
+ + '.aptx-dialog-drag { cursor:move; touch-action:none; user-select:none; -webkit-user-select:none; }'
16885
+ + '.aptx-clock.aptx-dialog-body { overflow:visible; }'
16886
+ + '.aptx-clock-svg { display:block; touch-action:none; user-select:none; -webkit-user-select:none; }'
16887
+ + '.aptx-clock-svg text { font-size:10px; fill:var(--p-muted); font-variant-numeric:tabular-nums; }'
16888
+ + '.aptx-clock-svg text.aptx-clock-title { font-size:10.5px; fill:var(--p-ink); }'
16889
+ + '.aptx-clock-ground { fill:var(--p-surface2); }'
16890
+ + '.aptx-clock-grid { stroke:var(--p-line); stroke-width:1; shape-rendering:crispEdges; }'
16891
+ + '.aptx-clock-frame { stroke:var(--p-line-strong); stroke-width:1; shape-rendering:crispEdges; }'
16892
+ + '.aptx-clock-line { stroke:var(--p-accent); stroke-width:1.6; }'
16893
+ + '.aptx-clock-internal circle { fill:var(--p-faint); fill-opacity:0.55; }'
16894
+ + '.aptx-clock-tips circle { fill:var(--p-ink); fill-opacity:0.8; }'
16895
+ + '.aptx-clock-svg circle.aptx-clock-hit { fill-opacity:1; stroke-width:1.2; }'
16896
+ + '.aptx-clock-ring { fill:none; stroke:var(--p-accent); stroke-width:1.8; pointer-events:none; }'
16897
+ + '.aptx-clock-box { fill:var(--p-accent-weak); stroke:var(--p-accent); stroke-width:1;'
16898
+ + ' shape-rendering:crispEdges; pointer-events:none; }'
16899
+ + '.aptx-clock-options { display:flex; align-items:center; gap:14px; margin:6px 0 8px; }'
16900
+ + '.aptx-clock-options .aptx-clock-deselect { margin-left:auto; height:22px; padding:0 9px; }'
16901
+ + '.aptx-clock-stats .aptx-dialog-key { flex-basis:38%; }'
16902
+ + '.aptx-clock-note { margin-top:8px; }'
16005
16903
  + '.aptx-dialog-mono { font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,"Courier New",monospace;'
16006
16904
  + ' font-size:11px; white-space:pre-wrap; overflow-wrap:anywhere; }'
16007
16905
  // The hover tooltip, on the same palette.
@@ -16111,6 +17009,12 @@ function (root, d3, forester, phyloXml) {
16111
17009
  + '.aptx-panel .aptx-seg:hover { color:var(--p-accent-ink); background:var(--p-accent-weak); }'
16112
17010
  + '.aptx-panel .aptx-seg:has(> input:checked) { background:var(--p-accent); color:#fff; }'
16113
17011
  + '.aptx-panel .aptx-seg:has(> input:disabled) { opacity:0.4; cursor:default; }'
17012
+ // the Clock plot button sits in the mode bar beside the segments
17013
+ // and matches their height; lit while its panel is open
17014
+ + '.aptx-panel .aptx-modebar input[type=button].aptx-clockbtn { height:auto; margin:0; padding:3px 8px;'
17015
+ + ' font-weight:600; color:var(--p-muted); border-radius:7px; }'
17016
+ + '.aptx-panel .aptx-modebar input[type=button].aptx-clockbtn.aptx-lit { background:var(--p-accent);'
17017
+ + ' border-color:var(--p-accent); color:#fff; }'
16114
17018
  + '.aptx-panel .aptx-actions { margin-left:auto; display:flex; align-items:center; gap:5px; }'
16115
17019
  + '.aptx-panel .aptx-theme-btn, .aptx-panel .aptx-info-btn { flex:none; width:20px; height:20px; display:grid; place-items:center; padding:0; border:1px solid var(--p-line-strong); border-radius:6px; background:var(--p-surface2); color:var(--p-muted); cursor:pointer; font-size:12px; line-height:1; }'
16116
17020
  + '.aptx-panel .aptx-theme-btn:hover { background:var(--p-accent-weak); color:var(--p-accent-ink); border-color:var(--p-accent); }'
@@ -17335,9 +18239,12 @@ function (root, d3, forester, phyloXml) {
17335
18239
  // live. Injecting twice is a no-op.
17336
18240
  injectPanelStyles();
17337
18241
 
18242
+ // REMOVED (2026-10-07): the 2.x class "node_mouseover_tooltip" beside
18243
+ // "aptx-tip". A host's old stylesheet outranked .aptx-tip through it
18244
+ // (div.X beats .Y) and restyled the popup; see CHANGELOG, Unreleased.
17338
18245
  if (!_node_mouseover_div) {
17339
18246
  _node_mouseover_div = d3.select("body").append("div")
17340
- .attr("class", "node_mouseover_tooltip aptx-tip")
18247
+ .attr("class", "aptx-tip")
17341
18248
  .style("opacity", 1e-6);
17342
18249
  }
17343
18250
 
@@ -17517,6 +18424,7 @@ function (root, d3, forester, phyloXml) {
17517
18424
  on(LAYOUT_RECT_BUTTON, 'click', layoutButtonClicked);
17518
18425
  on(BRANCH_SCALE_TIME_BUTTON, 'click', branchScaleButtonClicked);
17519
18426
  on(BRANCH_SCALE_DIV_BUTTON, 'click', branchScaleButtonClicked);
18427
+ on(CLOCK_PLOT_BUTTON, 'click', toggleClockPlot);
17520
18428
 
17521
18429
  on(LAYOUT_CIRC_BUTTON, 'click', layoutButtonClicked);
17522
18430
  on(LAYOUT_UNROOTED_BUTTON, 'click', layoutButtonClicked);
@@ -17960,6 +18868,12 @@ function (root, d3, forester, phyloXml) {
17960
18868
  h = h.concat(makeSegment('Div', BRANCH_SCALE_DIV_BUTTON, 'branch_scale_radio',
17961
18869
  'lay the tree out by DIVERGENCE (substitutions per site: as the tree records it, or on a BEAST tree each branch\'s time x its clock rate)'));
17962
18870
  h = h.concat('</div>');
18871
+ // The clock plot, where the tree has one (forester.clockPlotKind).
18872
+ // Beside the switch because it draws the same two quantities
18873
+ // against each other; a button of its own because it opens a
18874
+ // panel and leaves the layout alone.
18875
+ h = h.concat('<input type="button" class="aptx-clockbtn" value="Clock plot" name="' + CLOCK_PLOT_BUTTON
18876
+ + '" id="' + CLOCK_PLOT_BUTTON + '" title="' + CLOCK_PLOT_WHAT + '">');
17963
18877
  h = h.concat('</div>');
17964
18878
  h = h.concat('</fieldset>');
17965
18879
  return h;