Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 56 additions & 4 deletions js/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Comment on lines +176 to +179
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
Expand All @@ -188,26 +204,47 @@ 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 */ });
}

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);
}
Expand All @@ -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. */
Expand Down Expand Up @@ -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;
Expand Down
Loading