AgPerry's picture
Add files using upload-large-folder tool
e4718b1 verified
Raw History Blame Contribute Delete
16.3 kB
document.addEventListener('DOMContentLoaded', () => {
document.querySelectorAll('[data-dismiss-consent]').forEach((button) => {
button.addEventListener('click', () => {
document.querySelector('.consent-backdrop')?.remove();
document.querySelector('.consent')?.remove();
});
});
const count = document.querySelector('[name="players"]');
const rows = [...document.querySelectorAll('[data-player-row]')];
function syncPlayers() {
if (!count) return;
const n = Math.max(1, Math.min(6, Number(count.value) || 1));
rows.forEach((row, index) => {
row.style.display = index < n ? 'grid' : 'none';
row.querySelectorAll('input').forEach((input) => { input.disabled = index >= n; });
});
}
if (count) {
count.addEventListener('input', syncPlayers);
syncPlayers();
}
document.querySelectorAll('[data-confirm]').forEach((form) => {
form.addEventListener('submit', (event) => {
if (!window.confirm(form.dataset.confirm)) event.preventDefault();
});
});
const menuTriggers = [...document.querySelectorAll('.menu-trigger')];
const siteSelectorTrigger = document.querySelector('.site-selector-trigger');
const siteSelectorPanel = document.querySelector('#geek-sites-panel');
const profileTrigger = document.querySelector('.profile-trigger');
const profilePanel = document.querySelector('#profile-panel');
function closeHeaderMenus(except = null, restoreFocus = false) {
menuTriggers.forEach((trigger) => {
if (trigger === except) return;
const panel = document.querySelector(`#${trigger.getAttribute('aria-controls')}`);
const wasOpen = trigger.getAttribute('aria-expanded') === 'true';
trigger.setAttribute('aria-expanded', 'false');
if (panel) panel.hidden = true;
if (restoreFocus && wasOpen) trigger.focus();
});
if (siteSelectorTrigger !== except && siteSelectorTrigger && siteSelectorPanel) {
const wasOpen = siteSelectorTrigger.getAttribute('aria-expanded') === 'true';
siteSelectorTrigger.setAttribute('aria-expanded', 'false');
siteSelectorPanel.hidden = true;
if (restoreFocus && wasOpen) siteSelectorTrigger.focus();
}
if (profileTrigger !== except && profileTrigger && profilePanel) {
const wasOpen = profileTrigger.getAttribute('aria-expanded') === 'true';
profileTrigger.setAttribute('aria-expanded', 'false');
profilePanel.hidden = true;
if (restoreFocus && wasOpen) profileTrigger.focus();
}
}
menuTriggers.forEach((trigger) => {
const panel = document.querySelector(`#${trigger.getAttribute('aria-controls')}`);
if (!panel) return;
trigger.addEventListener('click', () => {
const open = trigger.getAttribute('aria-expanded') !== 'true';
closeHeaderMenus(trigger);
trigger.setAttribute('aria-expanded', String(open));
panel.hidden = !open;
});
trigger.addEventListener('keydown', (event) => {
if (event.key !== 'ArrowDown') return;
event.preventDefault();
closeHeaderMenus(trigger);
trigger.setAttribute('aria-expanded', 'true');
panel.hidden = false;
panel.querySelector('a')?.focus();
});
});
if (siteSelectorTrigger && siteSelectorPanel) {
siteSelectorTrigger.addEventListener('click', () => {
const open = siteSelectorTrigger.getAttribute('aria-expanded') !== 'true';
closeHeaderMenus(siteSelectorTrigger);
siteSelectorTrigger.setAttribute('aria-expanded', String(open));
siteSelectorPanel.hidden = !open;
});
}
if (profileTrigger && profilePanel) {
profileTrigger.addEventListener('click', () => {
const open = profileTrigger.getAttribute('aria-expanded') !== 'true';
closeHeaderMenus(profileTrigger);
profileTrigger.setAttribute('aria-expanded', String(open));
profilePanel.hidden = !open;
});
}
document.addEventListener('click', (event) => {
if (!(event.target instanceof Element) || event.target.closest('.menu, .brand, .profile-menu')) return;
closeHeaderMenus();
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape') closeHeaderMenus(null, true);
});
document.querySelector('[data-skip-main]')?.addEventListener('click', () => {
const main = document.querySelector('main');
if (!main) return;
main.tabIndex = -1;
main.focus();
});
document.querySelectorAll('[data-nav-href]').forEach((button) => {
button.addEventListener('click', () => window.location.assign(button.dataset.navHref));
});
const cookieButton = document.querySelector('.footer-cookie-button');
const cookieNotice = document.querySelector('#footer-cookie-notice');
if (cookieButton && cookieNotice) {
cookieButton.addEventListener('click', () => {
const open = cookieButton.getAttribute('aria-expanded') !== 'true';
cookieButton.setAttribute('aria-expanded', String(open));
cookieNotice.hidden = !open;
});
}
const sidebar = document.querySelector('#site-sidebar');
const sidebarToggle = document.querySelector('.hamburger[aria-controls="site-sidebar"]');
let sidebarReturnFocus = null;
function setSidebar(open, restoreFocus = false) {
if (!sidebar || !sidebarToggle) return;
sidebar.hidden = !open;
sidebarToggle.setAttribute('aria-expanded', String(open));
if (open) {
sidebarReturnFocus = document.activeElement;
} else if (restoreFocus && sidebarReturnFocus instanceof HTMLElement) {
sidebarReturnFocus.focus();
}
}
if (sidebar && sidebarToggle) {
sidebarToggle.addEventListener('click', () => {
setSidebar(sidebarToggle.getAttribute('aria-expanded') !== 'true');
});
sidebar.querySelectorAll('[data-close-sidebar]').forEach((button) => {
button.addEventListener('click', () => setSidebar(false, true));
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && !sidebar.hidden) setSidebar(false, true);
});
}
document.querySelectorAll('.member-sidebar-heading').forEach((button) => {
const body = document.querySelector(`#${button.getAttribute('aria-controls')}`);
if (!body) return;
button.addEventListener('click', () => {
const open = button.getAttribute('aria-expanded') !== 'true';
button.setAttribute('aria-expanded', String(open));
body.hidden = !open;
});
});
document.querySelector('.quickbar-add')?.addEventListener('click', (event) => {
const button = event.currentTarget;
button.textContent = 'Current Page Added';
button.disabled = true;
});
document.querySelectorAll('.home-source-overflow').forEach((rail) => {
const list = rail.querySelector('.home-source-list, .creator-video-list');
if (!list) return;
let offset = 0;
const update = (direction) => {
const card = list.querySelector('li');
if (!card) return;
const step = card.getBoundingClientRect().width + 15;
const max = Math.max(0, list.scrollWidth - rail.clientWidth + 38);
offset = Math.max(0, Math.min(max, offset + direction * step));
list.style.transform = `translateX(${-offset}px)`;
};
rail.querySelector('.rail-previous')?.addEventListener('click', () => update(-1));
rail.querySelector('.rail-next')?.addEventListener('click', () => update(1));
});
document.querySelectorAll('.home-media-overflow').forEach((rail) => {
const list = rail.querySelector('.home-media-rail');
if (!list) return;
let offset = 0;
const update = (direction) => {
const card = list.querySelector('li');
if (!card) return;
const step = card.getBoundingClientRect().width + 15;
const max = Math.max(0, list.scrollWidth - rail.clientWidth);
offset = Math.max(0, Math.min(max, offset + direction * step));
list.style.transform = `translateX(${-offset}px)`;
};
rail.querySelector('[data-rail-previous]')?.addEventListener('click', () => update(-1));
rail.querySelector('[data-rail-next]')?.addEventListener('click', () => update(1));
});
const gamePopover = document.querySelector('[data-game-popover]');
const gamePopoverContent = gamePopover?.querySelector('[data-game-popover-content]');
const gamePanelTriggers = [...document.querySelectorAll('[data-game-panel]')];
if (gamePopover) document.body.appendChild(gamePopover);
let activeGamePanelTrigger = null;
function closeGamePopover(restoreFocus = false) {
if (!gamePopover) return;
gamePopover.hidden = true;
delete gamePopover.dataset.panel;
gamePanelTriggers.forEach((trigger) => trigger.setAttribute('aria-expanded', 'false'));
if (restoreFocus && activeGamePanelTrigger instanceof HTMLElement) activeGamePanelTrigger.focus();
activeGamePanelTrigger = null;
}
function gamePanelMarkup(panel) {
const panels = {
settings: '<h2>Game settings</h2><ul><li><a href="/boardgame/30549/pandemic/edit">Edit game information</a></li><li><a href="/collection/user/local-member">Open your collection</a></li></ul>',
share: '<h2>Share Pandemic</h2><p>Copy the address for this local game page.</p><button type="button" data-local-choice="copy-link">Copy local page link</button><p role="status" hidden></p>',
collection: '<h2>Add To Collection</h2><p>What best matches your copy?</p><ul><li><button type="button" data-local-choice="Own">Own</button></li><li><button type="button" data-local-choice="Wishlist">Wishlist</button></li></ul><p role="status" hidden></p>',
subscribe: '<h2>Subscription updated</h2><p role="status">Pandemic notifications are enabled for this local profile.</p>',
'subscribe-options': '<h2>Subscription options</h2><ul><li><button type="button" data-local-choice="All activity">All activity</button></li><li><button type="button" data-local-choice="New files and videos">New files and videos</button></li><li><button type="button" data-local-choice="Community discussions">Community discussions</button></li></ul><p role="status" hidden></p>',
more: '<h2>More game sections</h2><ul><li><a href="/boardgame/30549/pandemic/credits">Credits</a></li><li><a href="/boardgame/30549/pandemic/community">Community</a></li><li><a href="/boardgame/30549/pandemic/reimplementations">Reimplementations</a></li></ul>',
rating: '<h2>Rate Pandemic</h2><p>Choose a rating from 1 to 10.</p><div class="source-rating-options">' + Array.from({ length: 10 }, (_, index) => `<button type="button" data-local-rating="${index + 1}">${index + 1}</button>`).join('') + '</div><p role="status" hidden></p>',
};
return panels[panel] || '<h2>Game option</h2><p>This local option is available.</p>';
}
function setGamePopover(panel, trigger) {
if (!gamePopover || !gamePopoverContent) return;
const wasSameOpen = !gamePopover.hidden && gamePopover.dataset.panel === panel;
closeGamePopover();
if (wasSameOpen) return;
activeGamePanelTrigger = trigger;
trigger.setAttribute('aria-expanded', 'true');
gamePopover.dataset.panel = panel;
gamePopoverContent.innerHTML = gamePanelMarkup(panel);
gamePopover.hidden = false;
const triggerRect = trigger.getBoundingClientRect();
const panelRect = gamePopover.getBoundingClientRect();
const opensAbove = Boolean(trigger.closest('.source-game-actions'));
const top = opensAbove
? Math.max(8, triggerRect.top - panelRect.height - 7)
: Math.min(window.innerHeight - panelRect.height - 8, triggerRect.bottom + 7);
const left = Math.max(8, Math.min(window.innerWidth - panelRect.width - 8, triggerRect.left));
gamePopover.style.top = `${top}px`;
gamePopover.style.left = `${left}px`;
}
gamePanelTriggers.forEach((trigger) => {
trigger.setAttribute('aria-haspopup', 'dialog');
trigger.setAttribute('aria-expanded', 'false');
trigger.addEventListener('click', () => {
const panel = trigger.dataset.gamePanel;
if (panel === 'subscribe') {
const subscribed = trigger.getAttribute('aria-pressed') !== 'true';
trigger.setAttribute('aria-pressed', String(subscribed));
trigger.textContent = subscribed ? 'Subscribed' : 'Subscribe';
setGamePopover(panel, trigger);
const status = gamePopoverContent?.querySelector('[role="status"]');
if (status && !subscribed) status.textContent = 'Pandemic notifications are disabled for this local profile.';
return;
}
setGamePopover(panel, trigger);
});
});
gamePopover?.querySelector('[data-close-game-popover]')?.addEventListener('click', () => closeGamePopover(true));
gamePopoverContent?.addEventListener('click', async (event) => {
const choice = event.target instanceof Element ? event.target.closest('[data-local-choice]') : null;
const rating = event.target instanceof Element ? event.target.closest('[data-local-rating]') : null;
const status = gamePopoverContent.querySelector('[role="status"]');
if (choice instanceof HTMLButtonElement) {
const value = choice.dataset.localChoice || 'Selection';
if (value === 'copy-link') {
try {
await navigator.clipboard.writeText(window.location.href);
if (status) status.textContent = 'Local page link copied.';
} catch (_error) {
if (status) status.textContent = 'Local page link is ready in the address bar.';
}
} else if (gamePopover.dataset.panel === 'collection') {
try {
const response = await fetch('/collection/status/30549', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' },
body: new URLSearchParams({ choice: value }),
});
if (!response.ok) throw new Error('Collection status could not be saved.');
const collectionButton = document.querySelector('.source-collection');
const labelNode = collectionButton ? [...collectionButton.childNodes].find((node) => node.nodeType === Node.TEXT_NODE) : null;
if (labelNode) labelNode.nodeValue = 'In Collection';
collectionButton?.setAttribute('data-in-collection', 'true');
if (status) status.textContent = `${value} saved for this local profile.`;
} catch (error) {
if (status) status.textContent = error instanceof Error ? error.message : 'Collection status could not be saved.';
}
} else {
if (status) status.textContent = `${value} saved for this local profile.`;
}
if (status) status.hidden = false;
}
if (rating instanceof HTMLButtonElement) {
const value = Number(rating.dataset.localRating);
const ratingButton = document.querySelector('[data-game-panel="rating"]');
try {
const response = await fetch('/collection/rating/30549', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'X-Requested-With': 'fetch',
},
body: new URLSearchParams({ rating: String(value) }),
});
if (!response.ok) throw new Error('Rating could not be saved.');
ratingButton?.querySelectorAll('.source-icon-star').forEach((star, index) => star.classList.toggle('is-rated', index < value));
ratingButton?.setAttribute('aria-label', `Your rating: ${value} out of 10`);
ratingButton?.setAttribute('data-current-rating', String(value));
const summary = document.querySelector('[data-rating-summary]');
if (summary) {
summary.textContent = `Your rating: ${value}/10`;
summary.hidden = false;
}
if (status) status.textContent = `Your local rating is ${value}/10.`;
} catch (error) {
if (status) status.textContent = error instanceof Error ? error.message : 'Rating could not be saved.';
}
if (status) status.hidden = false;
}
});
document.addEventListener('click', (event) => {
if (!gamePopover || gamePopover.hidden || !(event.target instanceof Element)) return;
if (event.target.closest('[data-game-popover], [data-game-panel]')) return;
closeGamePopover();
});
document.addEventListener('keydown', (event) => {
if (event.key === 'Escape' && gamePopover && !gamePopover.hidden) closeGamePopover(true);
});
});