/* SHARE A STORY. * * Taufik, 14 October 2026: "Can we make a share here to share via whatapps * of any channel", with a screenshot of Google's own share sheet — the link, * Copy link, then Facebook, Gmail, X, Reddit, WhatsApp. * * On a phone the phone already has the best version of that: the system * share sheet, with whatever the person actually uses on it. So a touch * device that offers navigator.share gets exactly that, and nothing else. * Everywhere else — a laptop, a browser without it, the office LAN on plain * http where it does not exist — a short list opens under the button: the * channels by name, the address, and Copy link. If the browser CAN share * natively there too, the list ends with "More…", which hands over to it. * * Every button on the site works through this one file: [data-sharebox] * holds the address (relative, /news/ — made absolute here from this * site's own address, so neuon.site shares neuon.site and neuon.ai shares * neuon.ai), the title and a line of text. Popups fill those in per story * through window.NEUShare.set(). * * Nothing is handed to innerHTML: every name and address is a text node or an * attribute, and every outside link opens in a tab of its own. * * It also answers every [data-mcopy] copy button that is NOT inside the * timeline — the timeline has its own, in milestones.js, which stops the * click before it gets here — so the chapter popup and the story page copy * an address the same way the timeline always has. */ (function () { 'use strict'; var open = null; // the [data-sharebox] whose list is showing function abs(u) { try { return new URL(u || location.pathname, location.origin).href; } catch (e) { return location.href; } } function data(box) { return { url: abs(box.getAttribute('data-url')), title: box.getAttribute('data-title') || document.title, text: box.getAttribute('data-text') || '', }; } /* the channels, in the order the picture had them, WhatsApp first because it is the one asked for by name */ function channels(d) { var e = encodeURIComponent; var line = d.title + (d.text ? ' — ' + d.text : ''); return [ { key: 'wa', name: 'WhatsApp', href: 'https://wa.me/?text=' + e(d.title + '\n' + d.url) }, { key: 'tg', name: 'Telegram', href: 'https://t.me/share/url?url=' + e(d.url) + '&text=' + e(d.title) }, { key: 'fb', name: 'Facebook', href: 'https://www.facebook.com/sharer/sharer.php?u=' + e(d.url) }, { key: 'x', name: 'X', href: 'https://twitter.com/intent/tweet?url=' + e(d.url) + '&text=' + e(d.title) }, { key: 'in', name: 'LinkedIn', href: 'https://www.linkedin.com/sharing/share-offsite/?url=' + e(d.url) }, { key: 'mail', name: 'Email', href: 'mailto:?subject=' + e(d.title) + '&body=' + e(line + '\n\n' + d.url) }, ]; } function build(box) { var list = box.querySelector('[data-sharelist]'); if (!list) return null; var d = data(box); list.textContent = ''; /* the address itself first, the way the picture has it: somebody about to forward a link wants to see what it is */ var row = document.createElement('div'); row.className = 'share__row'; var code = document.createElement('code'); code.className = 'share__url'; code.textContent = d.url; var copy = document.createElement('button'); copy.type = 'button'; copy.className = 'share__copy'; copy.setAttribute('data-sharecopy', d.url); copy.textContent = 'Copy link'; row.appendChild(code); row.appendChild(copy); list.appendChild(row); var grid = document.createElement('div'); grid.className = 'share__grid'; channels(d).forEach(function (c) { var a = document.createElement('a'); a.className = 'share__ch share__ch--' + c.key; a.href = c.href; /* a mail link opens the mail program, not a tab; everything else is somewhere else and opens in a tab of its own */ if (c.key !== 'mail') { a.target = '_blank'; a.rel = 'noopener noreferrer'; } var dot = document.createElement('i'); dot.setAttribute('aria-hidden', 'true'); a.appendChild(dot); a.appendChild(document.createTextNode(c.name)); a.addEventListener('click', function () { setTimeout(function () { shut(); }, 0); }); grid.appendChild(a); }); if (navigator.share) { var more = document.createElement('button'); more.type = 'button'; more.className = 'share__ch share__ch--more'; more.setAttribute('data-sharenative', ''); var dot2 = document.createElement('i'); dot2.setAttribute('aria-hidden', 'true'); more.appendChild(dot2); more.appendChild(document.createTextNode('More…')); grid.appendChild(more); } list.appendChild(grid); return list; } function shut() { if (!open) return; var list = open.querySelector('[data-sharelist]'); var btn = open.querySelector('[data-share]'); if (list) list.hidden = true; if (btn) btn.setAttribute('aria-expanded', 'false'); open.classList.remove('is-open'); open = null; } function show(box) { if (open && open !== box) shut(); var list = build(box); if (!list) return; list.hidden = false; box.classList.add('is-open'); var btn = box.querySelector('[data-share]'); if (btn) btn.setAttribute('aria-expanded', 'true'); open = box; var first = list.querySelector('.share__ch'); if (first) first.focus({ preventScroll: true }); } function native(box) { var d = data(box); /* called straight from the press — a share sheet needs the tap that asked for it, and anything awaited first would lose it. Cancelling the sheet rejects with AbortError, which is a person changing their mind, not a fault: nothing is shown for it. Any other refusal falls back to the list. */ return navigator.share({ title: d.title, text: d.text || d.title, url: d.url }).catch(function (err) { if (!err || err.name !== 'AbortError') show(box); }); } var coarse = window.matchMedia && matchMedia('(pointer: coarse)').matches; document.addEventListener('click', function (e) { var t = e.target; var btn = t.closest && t.closest('[data-share]'); if (btn) { e.preventDefault(); e.stopPropagation(); var box = btn.closest('[data-sharebox]'); if (!box) return; if (open === box) { shut(); return; } if (coarse && navigator.share) native(box); else show(box); return; } var nat = t.closest && t.closest('[data-sharenative]'); if (nat) { e.preventDefault(); e.stopPropagation(); var b2 = nat.closest('[data-sharebox]'); shut(); if (b2 && navigator.share) native(b2); return; } var cp = t.closest && (t.closest('[data-sharecopy]') || t.closest('[data-mcopy]')); if (cp) { e.preventDefault(); e.stopPropagation(); copyIt(cp, cp.getAttribute('data-sharecopy') || cp.getAttribute('data-mcopy') || ''); return; } /* a press anywhere else closes an open list, and nothing more */ if (open && !open.contains(t)) shut(); }); /* …and closes it in the CAPTURE phase as well, before anything can stop the press on its way here: the timeline's own copy buttons stop their clicks at the panel, and a press on one of those is still a press elsewhere */ document.addEventListener('click', function (e) { if (open && !open.contains(e.target)) shut(); }, true); /* ESCAPE CLOSES THE LIST, AND ONLY THE LIST. The popups underneath close on Escape too — the timeline's box listens in the capture phase on the document — so this listens on the WINDOW in the capture phase, which runs before both, and stops there only when it had something to close. */ window.addEventListener('keydown', function (e) { if (e.key !== 'Escape' || !open) return; var btn = open.querySelector('[data-share]'); shut(); e.stopPropagation(); e.preventDefault(); if (btn) btn.focus({ preventScroll: true }); }, true); /* ── COPY ── The same two ways the timeline's buttons copy (milestones.js): the clipboard where the page is allowed it, and the old way where it is not — writeText needs a secure context and quietly refuses without one, which is exactly the office LAN. And it SAYS whether it worked. */ function copyIt(b, url) { var said = function (ok) { if (b.__was == null) b.__was = b.textContent; b.textContent = ok ? (b.classList.contains('share__copy') ? 'Copied ✓' : '✓') : '✕'; b.classList.add(ok ? 'is-done' : 'is-bad'); clearTimeout(b.__t); b.__t = setTimeout(function () { b.textContent = b.__was; b.__was = null; b.classList.remove('is-done', 'is-bad'); }, 1400); }; if (navigator.clipboard && navigator.clipboard.writeText) { navigator.clipboard.writeText(url).then(function () { said(true); }, function () { said(fallback(url)); }); } else { said(fallback(url)); } } function fallback(url) { try { var t = document.createElement('textarea'); t.value = url; t.setAttribute('readonly', ''); t.style.position = 'fixed'; t.style.opacity = '0'; document.body.appendChild(t); t.select(); var ok = document.execCommand('copy'); document.body.removeChild(t); return ok; } catch (e) { return false; } } /* for the popups: point a box at a story, or hide it for a card that has no page of its own */ window.NEUShare = { set: function (box, d) { if (!box) return; if (open === box) shut(); if (!d || !d.url) { box.hidden = true; return; } box.setAttribute('data-url', d.url); box.setAttribute('data-title', d.title || ''); box.setAttribute('data-text', d.text || ''); box.hidden = false; }, close: shut, }; })();