diff --git a/js/cards.js b/js/cards.js index 5005c420..82503b9f 100644 --- a/js/cards.js +++ b/js/cards.js @@ -145,8 +145,32 @@ window.CactiCards = window.CactiCards || (function() { return rowMatch || belowMatch; } - /* Swap a card's markup in place (after refresh/add) and re-bind it. */ - function replaceCard(grid, adapter, card, html) { + /* Decide, mirroring normalizeState(), whether a fresh card will be + * expandable, so a preserved toggle is never forced onto a card that cannot + * expand (which would leave normalizeState() marking it ccCardExpanded and + * persisting expanded:true for a non-expandable card). */ + function willExpand(card) { + var expandable = card.getAttribute('data-expandable'); + if (expandable === '1' || expandable === 'true') { + return true; + } + if (expandable != null) { + return false; + } + return card.classList.contains('ccCardExpandable'); + } + + /* Swap a card's markup in place (after refresh/add) and re-bind it. + * + * capturedExpanded (optional) is the card's expanded state when an internal + * refresh/resize request started. When it is supplied and the live card has + * since been toggled by the user, the incoming markup reflects the stale + * captured state, so the live state is carried onto the fresh markup to avoid + * reverting that toggle - but only when the fresh card is still expandable. + * When it is omitted (direct callers) or the state is unchanged, the host + * markup's own data-expanded stays authoritative (the normalizeState + * contract, so a host may still drive expansion through returned markup). */ + function replaceCard(grid, adapter, card, html, capturedExpanded) { var tmp = document.createElement('div'); tmp.innerHTML = (html || '').trim(); var fresh = tmp.querySelector(cardSel(adapter)) || tmp.firstElementChild; @@ -165,6 +189,12 @@ window.CactiCards = window.CactiCards || (function() { if (!target || !target.parentNode) { return null; } + if (capturedExpanded != null) { + var liveExpanded = target.classList.contains('ccCardExpanded'); + if (liveExpanded !== capturedExpanded && willExpand(fresh)) { + fresh.setAttribute('data-expanded', liveExpanded ? '1' : '0'); + } + } target.parentNode.replaceChild(fresh, target); bindCard(grid, adapter, fresh); if (typeof adapter.applyTheme === 'function') { @@ -173,6 +203,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,28 +234,50 @@ 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) { - replaceCard(grid, adapter, card, html); + // 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, state.expanded); } }).catch(function() { /* fire-and-forget: the host surfaces adapter errors */ }); } 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); + var capturedExpanded = card.classList.contains('ccCardExpanded'); 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); + replaceCard(grid, adapter, card, html, capturedExpanded); } saveLayout(grid, adapter); }).catch(function() { /* fire-and-forget: the host surfaces adapter errors */ }); @@ -218,6 +286,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 +429,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;