From 30eaf8815e73aa915ac19a1f7095c6fddbc44fbc Mon Sep 17 00:00:00 2001 From: TheWitness Date: Mon, 9 Nov 2026 17:04:07 -0500 Subject: [PATCH] cards: sync bundled cards.js to the converged controller baseline plugin_mactrack #412 merged with an earlier cards.js, leaving it byte-divergent from the canonical CactiCards controller. Re-sync it to the converged baseline (md5 84876af1) that Cacti core (#8684 for 1.2.x, #8683 for develop) and every Card API plugin now carry: the refresh/resize supersede guard, async remove-commit, the duplicate-add guard and expand/collapse keyboard focus. mactrack uses only the card chrome (no chart primitives), so no card-widgets.css is bundled; css/cards.css is already byte-identical to canonical. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- js/cards.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/js/cards.js b/js/cards.js index 5005c420..4e5e480c 100644 --- a/js/cards.js +++ b/js/cards.js @@ -173,6 +173,22 @@ window.CactiCards = window.CactiCards || (function() { return fresh; } + /* A per-card-id request generation shared by refresh and resize so a slow + * response cannot overwrite the markup of a newer request for the same card + * (for example a refresh that lands after a later grow). The most recently + * started request for an id wins; earlier in-flight responses are discarded. */ + function startRequest(grid, id) { + if (!grid.__ccReq) { + grid.__ccReq = {}; + } + grid.__ccReq[id] = (grid.__ccReq[id] || 0) + 1; + return grid.__ccReq[id]; + } + + function requestSuperseded(grid, id, token) { + return !!grid.__ccReq && grid.__ccReq[id] !== token; + } + function refreshCard(grid, adapter, card) { var id = cardId(card, adapter); // Hosts that inject into an inner body (rather than replacing the whole @@ -188,8 +204,11 @@ window.CactiCards = window.CactiCards || (function() { expanded: card.classList.contains('ccCardExpanded'), height: Number.parseInt(card.getAttribute('data-height') || '1', 10) }; + var token = startRequest(grid, id); Promise.resolve(adapter.fetchCard(id, state)).then(function(html) { - if (html != null) { + // A newer refresh/resize for this card superseded us; discard the + // stale markup instead of reverting the card to an older state. + if (html != null && !requestSuperseded(grid, id, token)) { replaceCard(grid, adapter, card, html); } }).catch(function() { /* fire-and-forget: the host surfaces adapter errors */ }); @@ -197,17 +216,35 @@ window.CactiCards = window.CactiCards || (function() { function removeCard(grid, adapter, card) { var id = cardId(card, adapter); + // Only drop the live card and persist the new layout once the host has + // confirmed the deletion. Re-resolve by id so an overlapping refresh that + // swapped the node does not leave a detached reference behind. + var commit = function() { + var target = grid.contains(card) ? card : findCard(grid, adapter, id); + if (target && target.parentNode) { + target.parentNode.removeChild(target); + saveLayout(grid, adapter); + } + }; if (typeof adapter.onRemoveCard === 'function') { - adapter.onRemoveCard(id, card); + Promise.resolve(adapter.onRemoveCard(id, card)).then(commit).catch(function() { + /* rejected: leave the card and its saved layout untouched */ + }); + } else { + commit(); } - card.parentNode.removeChild(card); - saveLayout(grid, adapter); } function resizeCard(grid, adapter, card, dir) { var id = cardId(card, adapter); if (typeof adapter.onResize === 'function') { + var token = startRequest(grid, id); Promise.resolve(adapter.onResize(id, dir, card)).then(function(html) { + // A newer refresh/resize for this card has superseded us; leave + // its markup and layout in place rather than reverting them. + if (requestSuperseded(grid, id, token)) { + return; + } if (typeof html === 'string') { replaceCard(grid, adapter, card, html); } @@ -218,6 +255,19 @@ window.CactiCards = window.CactiCards || (function() { } } + /* After expand/collapse hides the button that was activated (see cards.css), + * move keyboard focus to the now-visible counterpart so the control is not + * lost when the card is toggled from the keyboard. */ + function refocusToolCounterpart(card, tool, counterpart) { + if (document.activeElement !== tool) { + return; + } + var next = card.querySelector('.ccTool[data-tool="' + counterpart + '"]'); + if (next) { + next.focus(); + } + } + /* Wire a single card: its drag handle (mouse + touch + keyboard) and drag * events. Handle interactions are DELEGATED from the persistent card element * so they survive hosts that replace the header markup on refresh/reload. */ @@ -348,11 +398,13 @@ window.CactiCards = window.CactiCards || (function() { case 'expand': card.classList.add('ccCardExpanded'); card.setAttribute('data-expanded', '1'); + refocusToolCounterpart(card, tool, 'collapse'); saveLayout(grid, adapter); break; case 'collapse': card.classList.remove('ccCardExpanded'); card.setAttribute('data-expanded', '0'); + refocusToolCounterpart(card, tool, 'expand'); saveLayout(grid, adapter); break; case 'grow': resizeCard(grid, adapter, card, 'grow'); break;