';
}
document.querySelectorAll('.cno-pop').forEach(function (el) { build(el.id.replace('pop-', ''), el); });
var toast = document.querySelector('.cno-toast'), tt;
function say(m) { toast.textContent = m; toast.classList.add('show'); clearTimeout(tt); tt = setTimeout(function () { toast.classList.remove('show'); }, 1800); }
var hoverable = matchMedia('(hover:hover) and (pointer:fine)').matches;
var phone = matchMedia('(max-width:639px)');
function close(btn) {
btn.setAttribute('aria-expanded', 'false'); delete btn.dataset.pinned;
document.getElementById(btn.getAttribute('aria-controls')).hidden = true;
}
function closeAll(except) { document.querySelectorAll('[data-pop][aria-expanded=true]').forEach(function (b) { if (b !== except) close(b); }); }
function open(btn) {
closeAll(btn);
var pop = document.getElementById(btn.getAttribute('aria-controls'));
pop.hidden = false; btn.setAttribute('aria-expanded', 'true');
// keep the popover inside the viewport; move the caret to stay under the button
pop.style.left = '0px';
var r = pop.getBoundingClientRect(), shift = Math.min(0, document.documentElement.clientWidth - 12 - r.right);
pop.style.left = shift + 'px';
var item = btn.closest('.cno-share__item'), bw = btn.getBoundingClientRect().width;
pop.style.setProperty('--caret', Math.max(12, bw / 2 - 6 - shift) + 'px');
}
document.addEventListener('click', function (e) {
var cp = e.target.closest('[data-copy]');
if (cp) {
var u = cp.getAttribute('data-copy');
(navigator.clipboard ? navigator.clipboard.writeText(u) : Promise.reject()).catch(function () {
var a = document.createElement('textarea'); a.value = u; document.body.appendChild(a); a.select(); try { document.execCommand('copy'); } catch (x) {} a.remove();
}).then(function () { say('Link copied'); });
return;
}
var b = e.target.closest('[data-pop]');
if (b) {
if (b.getAttribute('data-pop') === 'share' && phone.matches && navigator.share) { // phones: the device's own share sheet
navigator.share({ title: title, url: canonical }).catch(function () {});
return;
}
if (b.getAttribute('aria-expanded') !== 'true') { open(b); b.dataset.pinned = '1'; }
else if (!b.dataset.pinned) b.dataset.pinned = '1'; // opened by hover: a click keeps it open
else close(b);
return;
}
if (!e.target.closest('.cno-pop')) closeAll();
});
document.addEventListener('keydown', function (e) {
if (e.key === 'Escape') { var o = document.querySelector('[data-pop][aria-expanded=true]'); if (o) { close(o); o.focus(); } }
});
// desktop: open on hover (click still pins it open for touch and keyboard)
if (hoverable) document.querySelectorAll('.cno-share__item').forEach(function (item) {
var btn = item.querySelector('[data-pop]'), t;
if (!btn) return;
item.addEventListener('mouseenter', function () { clearTimeout(t); t = setTimeout(function () { if (btn.getAttribute('aria-expanded') !== 'true') open(btn); }, 120); });
item.addEventListener('mouseleave', function () { clearTimeout(t); t = setTimeout(function () { if (!btn.dataset.pinned) close(btn); }, 250); });
});
// mockup controls (collapsed on phones so they do not cover the popovers)
if (phone.matches) { var d = document.querySelector('.mock-controls details'); if (d) d.open = false; }
var root = document.querySelector('.cno-share');
[['mc-paywalled', 'data-paywalled'], ['mc-subscriber', 'data-subscriber']].forEach(function (c) {
document.getElementById(c[0]).addEventListener('change', function (e) { root.setAttribute(c[1], String(e.target.checked)); closeAll(); });
});
})();