// LinkSpin Pages screen: gallery + wizard (scan -> type -> angle -> write) + editor + leads. (function () { const $ = id => document.getElementById(id); const esc = CR.esc, fmt = CR.fmt, api = CR.api, toast = CR.toast, dialog = CR.dialog; let PAGES = [], SEL = null, SCAN = null, TYPE = 'bridge', HOSTS = []; const PAL = { graphite: '#3B9DFF', ink: '#B8410E', ember: '#FF8A3D' }; CR.mount({ active: 'pages', title: 'Pages', sub: 'loading', actions: '' }); const pageUrl = p => 'https://' + (HOSTS[0] || location.host) + '/p/' + p.slug; async function load(keepSel) { let r; try { r = await CR.get('/api/my/pages'); } catch (e) { CR.sub('could not load'); return; } PAGES = r.pages || []; HOSTS = r.hosts || []; const pubd = PAGES.filter(p => p.status === 'published').length; CR.sub(PAGES.length + ' page' + (PAGES.length === 1 ? '' : 's') + ' · ' + pubd + ' live'); const views = PAGES.reduce((n, p) => n + p.views, 0), leads = PAGES.reduce((n, p) => n + p.leads, 0), clicks = PAGES.reduce((n, p) => n + p.clicks, 0); $('tiles').innerHTML = [['Pages', fmt(PAGES.length)], ['Live', fmt(pubd)], ['Views', fmt(views)], ['Clicks through', fmt(clicks)], ['Leads on your list', fmt(leads)]].map(([k, v], i) => '
' + k + '' + v + '
').join(''); $('gal').innerHTML = PAGES.length ? PAGES.map(p => '
' + esc(p.title) + '' + p.type + ' · ' + (p.status === 'published' ? 'live' : 'draft') + ' · ' + fmt(p.views) + ' views' + (p.leads ? ' · ' + fmt(p.leads) + ' leads' : '') + '
').join('') : '
No pages yet. Scan the page you promote and write your first bridge page in a minute.
'; $('gal').querySelectorAll('.card').forEach(c => { const go = () => openEditor(c.dataset.id); c.addEventListener('click', go); c.addEventListener('keydown', e => { if (e.key === 'Enter') go(); }); }); try { const L = await CR.get('/api/my/pages/leads'); const rows = L.leads || []; $('leadsBox').hidden = !rows.length; $('leadRows').innerHTML = rows.slice(0, 50).map(l => '
' + esc(l.email) + '' + (l.name ? ' ' + esc(l.name) + '' : '') + '' + esc(l.page) + '' + new Date(l.ts).toLocaleString() + '
').join(''); $('leadFoot').textContent = rows.length + ' lead' + (rows.length === 1 ? '' : 's') + ' on your list · yours to export'; } catch (e) {} if (keepSel && SEL) openEditor(SEL, true); } // wizard $('types').querySelectorAll('button').forEach(b => b.addEventListener('click', () => { TYPE = b.dataset.t; $('types').querySelectorAll('button').forEach(x => x.classList.toggle('on', x === b)); })); $('scanBtn').addEventListener('click', async () => { const url = $('scanUrl').value.trim(); if (!url) return $('scanUrl').focus(); $('scanBtn').disabled = true; $('scanOut').hidden = false; $('scanOut').innerHTML = 'Scanning…'; try { const r = await api('/api/my/pages/scan', { url }); SCAN = r.scan; $('scanOut').innerHTML = 'SCANNED · HTTP ' + SCAN.status + '
' + esc(SCAN.title || SCAN.h1 || '(no title)') + '
' + esc((SCAN.description || '').slice(0, 160)) + '
' + (SCAN.image ? '
image found
' : '') + (SCAN.bullets.length ? '
' + SCAN.bullets.length + ' points found
' : ''); } catch (e) { SCAN = null; $('scanOut').innerHTML = '' + esc(e.message) + ''; } $('scanBtn').disabled = false; }); async function createPage(writeIt) { const url = $('scanUrl').value.trim(); const angle = $('angle').value.trim(); if (!SCAN && url && writeIt) { $('scanBtn').click(); await new Promise(r => setTimeout(r, 2500)); } try { const r = await api('/api/my/pages', { type: TYPE, title: (SCAN && (SCAN.title || SCAN.h1)) || angle || 'New ' + TYPE + ' page', angle, scan: SCAN || (url ? { url } : null), content: { cta: { url: url, text: TYPE === 'squeeze' ? 'Send me the details' : 'Take a look' }, headline: angle || '' } }); SEL = r.page.id; if (writeIt) { toast('Writing…'); await api('/api/my/pages/' + SEL + '/write', { angle, cta: url }); } await load(); openEditor(SEL); } catch (e) { toast(e.message, true); } } $('writeBtn').addEventListener('click', () => createPage(true)); $('blankBtn').addEventListener('click', () => createPage(false)); $('newBtn').addEventListener('click', () => { $('editor').hidden = true; $('wizard').hidden = false; $('scanUrl').focus(); }); // editor const F = { bridge: [['eyebrow', 'Eyebrow'], ['headline', 'Headline'], ['sub', 'Subhead'], ['body', 'Body, blank line between paragraphs', 'ta'], ['bullets', 'Bullets, one per line', 'ta'], ['bonusTitle', 'Bonus title'], ['bonus', 'Bonus text', 'ta'], ['ctaText', 'Button text'], ['ctaUrl', 'Button link'], ['image', 'Hero image URL'], ['disclaimer', 'Footer line']], review: [['eyebrow', 'Eyebrow'], ['headline', 'Headline'], ['rating', 'Rating 1 to 5'], ['sub', 'Subhead'], ['body', 'Review body, blank line between paragraphs', 'ta'], ['pros', 'What works, one per line', 'ta'], ['cons', 'What to know, one per line', 'ta'], ['verdict', 'Verdict'], ['ctaText', 'Button text'], ['ctaUrl', 'Button link'], ['image', 'Hero image URL'], ['disclaimer', 'Footer line']], squeeze: [['eyebrow', 'Eyebrow'], ['headline', 'Headline'], ['sub', 'Subhead'], ['bullets', 'What they get, one per line', 'ta'], ['ctaText', 'Button text'], ['ctaUrl', 'Where they go after the form'], ['image', 'Image URL'], ['privacy', 'Privacy line'], ['formEmbed', 'Your autoresponder form (optional, plain HTML form only, no JavaScript). Leave empty to capture leads on your LinkSpin list.', 'ta'], ['disclaimer', 'Footer line']] }; function openEditor(id, quiet) { const p = PAGES.find(x => x.id === id); if (!p) return; SEL = id; $('gal').querySelectorAll('.card').forEach(c => c.classList.toggle('on', c.dataset.id === id)); $('wizard').hidden = true; $('editor').hidden = false; $('edTitle').textContent = p.title; $('edStatus').innerHTML = p.status === 'published' ? 'LIVE' : 'DRAFT'; $('edPublish').textContent = p.status === 'published' ? 'Unpublish' : 'Publish'; $('edPreview').href = '/p/' + p.slug + '?preview=1'; $('edUrl').hidden = p.status !== 'published'; $('edUrl').textContent = pageUrl(p); const c = p.content || {}; const v = k => k === 'ctaText' ? (c.cta && c.cta.text) || '' : k === 'ctaUrl' ? (c.cta && c.cta.url) || '' : Array.isArray(c[k]) ? c[k].join('\n') : (c[k] == null ? '' : c[k]); $('edFields').innerHTML = F[p.type].map(([k, label, ta]) => '').join(''); $('pal').innerHTML = Object.entries(PAL).map(([k, col]) => '').join(''); $('pal').querySelectorAll('button').forEach(b => b.addEventListener('click', async () => { try { await api('/api/my/pages/' + p.id, { palette: b.dataset.p }); load(true); } catch (e) { toast(e.message, true); } })); if (!quiet && window.matchMedia('(max-width:900px)').matches) $('side').scrollIntoView({ behavior: 'smooth' }); } function collect(p) { const g = k => { const el = $('f-' + k); return el ? el.value : undefined; }; const c = {}; for (const [k] of F[p.type]) { if (k === 'ctaText' || k === 'ctaUrl') continue; c[k] = g(k); } c.cta = { text: g('ctaText'), url: g('ctaUrl') }; for (const k of ['bullets', 'pros', 'cons']) if (c[k] != null) c[k] = c[k].split('\n'); return c; } $('edSave').addEventListener('click', async () => { const p = PAGES.find(x => x.id === SEL); if (!p) return; try { await api('/api/my/pages/' + p.id, { content: collect(p), title: ($('f-headline').value || p.title).slice(0, 100) }); toast('Saved'); load(true); } catch (e) { toast(e.message, true); } }); $('edPublish').addEventListener('click', async () => { const p = PAGES.find(x => x.id === SEL); if (!p) return; try { await api('/api/my/pages/' + p.id, { content: collect(p) }); const r = await api('/api/my/pages/' + p.id, { status: p.status === 'published' ? 'draft' : 'published' }); toast(r.page.status === 'published' ? 'Live at ' + pageUrl(r.page) : 'Unpublished'); load(true); } catch (e) { toast(e.message, true); } }); $('edRewrite').addEventListener('click', async () => { const p = PAGES.find(x => x.id === SEL); if (!p) return; const v = await dialog({ title: 'Write again', text: 'A fresh draft from the scan and your angle. Your current text is replaced.', fields: [{ k: 'angle', label: 'Angle', value: p.angle || '' }], ok: 'Write' }); if (!v) return; try { toast('Writing…'); await api('/api/my/pages/' + p.id + '/write', { angle: v.angle }); load(true); } catch (e) { toast(e.message, true); } }); $('edDelete').addEventListener('click', async () => { const p = PAGES.find(x => x.id === SEL); if (!p) return; if (!await dialog({ title: 'Delete ' + p.title + '?', text: 'The page stops answering. Leads already on your list stay.', ok: 'Delete', danger: true })) return; try { await api('/api/my/pages/' + p.id, { remove: true }); SEL = null; $('editor').hidden = true; $('wizard').hidden = false; load(); } catch (e) { toast(e.message, true); } }); $('edClose').addEventListener('click', () => { $('editor').hidden = true; $('wizard').hidden = false; }); $('edSpin').addEventListener('click', async () => { const p = PAGES.find(x => x.id === SEL); if (!p) return; if (p.status !== 'published') return toast('Publish the page first.', true); try { const r = await CR.get('/api/my/rotations'); const rots = r.rotations || []; if (!rots.length) { const c = await api('/api/my/rotations', { name: p.title }); await api('/api/my/rotations/' + c.rotation.id + '/dest', { url: pageUrl(p), label: p.title, weight: 10 }); toast('New spin made with this page'); location.href = '/links#' + c.rotation.id; return; } const v = await dialog({ title: 'Which spin?', html: '', ok: 'Add page' }); if (!v) return; const id = document.getElementById('dlg-rot') ? document.getElementById('dlg-rot').value : rots[0].id; await api('/api/my/rotations/' + id + '/dest', { url: pageUrl(p), label: p.title, weight: 10 }); toast('Added to the spin'); location.href = '/links#' + id; } catch (e) { toast(e.message, true); } }); CR.requireSession(() => load()); })();