Repository navigation
Expand file tree
/
Copy pathworkshop-helpers.js
More file actions
160 lines (160 loc) · 8.16 KB
/
Copy pathworkshop-helpers.js
File metadata and controls
160 lines (160 loc) · 8.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* Company inputs stay in this browser. No keys, uploads or network requests. */
(() => {
const tokens = {
website: '[YOUR_COMPANY_WEBSITE]', buyer: '[BUYER_SEGMENT]', offer: '[COLD_OFFER]',
targetOne: '[TARGET_1_DOMAIN]', targetTwo: '[TARGET_2_DOMAIN]'
};
let fields = {};
try { fields = JSON.parse(localStorage.getItem('gtm-part1-company') || '{}'); } catch {}
const originals = new WeakMap();
function domain(value) {
const text = String(value || '').trim();
if (!text || /[\s@]/.test(text)) return '';
try {
const url = new URL(/^https?:\/\//i.test(text) ? text : 'https://' + text);
const host = url.hostname.toLowerCase().replace(/^www\./, '');
return /^([a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z][a-z0-9-]*$/.test(host) && !url.username && !url.password ? host : '';
} catch { return ''; }
}
const render = text => Object.entries(tokens).reduce((out, [key, token]) => {
const value = key.startsWith('target') ? domain(fields[key]) : String(fields[key] || '').trim().replace(/[\r\n]+/g, ' ');
return value ? out.split(token).join(value) : out;
}, text);
function attachPrompt(el, text) {
originals.set(el, text);
el.dataset.customPrompt = 'true';
update();
}
function update() {
document.querySelectorAll('[data-input-error]').forEach(el => { el.hidden = true; el.textContent = ''; });
document.querySelectorAll('[data-company-field]').forEach(el => {
if (document.activeElement !== el) el.value = fields[el.dataset.companyField] || '';
});
document.querySelectorAll('[data-custom-prompt]').forEach(el => {
const text = render(originals.get(el) || '');
if (el.tagName === 'TEXTAREA') el.value = text; else el.textContent = text;
});
}
document.querySelectorAll('[data-company-field]').forEach(el => {
el.value = fields[el.dataset.companyField] || '';
el.addEventListener('input', () => {
fields[el.dataset.companyField] = el.value;
try { localStorage.setItem('gtm-part1-company', JSON.stringify(fields)); } catch {}
update();
});
el.addEventListener('blur', () => {
const key = el.dataset.companyField;
if (!key.startsWith('target')) return;
const normalized = domain(el.value);
if (!normalized) return;
fields[key] = normalized;
el.value = normalized;
try { localStorage.setItem('gtm-part1-company', JSON.stringify(fields)); } catch {}
update();
});
});
document.querySelectorAll('[data-personalize]').forEach(el => attachPrompt(el,
el.tagName === 'TEXTAREA' ? el.value : el.textContent));
function validatePrompt(text, scope) {
const missing = ['targetOne', 'targetTwo'].filter(key => text.includes(tokens[key]));
let panel = scope?.querySelector('.target-inputs:not([hidden])');
if (!panel && scope?.previousElementSibling?.matches('.target-inputs')) panel = scope.previousElementSibling;
if (!missing.length || !panel) return true;
const error = panel.querySelector('[data-input-error]');
error.textContent = 'Enter a company domain for ' + missing.map(key => key === 'targetOne' ? 'Target 1' : 'Target 2').join(' and ') + ' above, then copy. For example: zuora.com.';
error.hidden = false;
panel.querySelector('[data-company-field="' + missing[0] + '"]')?.focus();
return false;
}
window.addEventListener('storage', event => {
if (event.key !== 'gtm-part1-company') return;
try { fields = JSON.parse(event.newValue || '{}'); update(); } catch {}
});
function celebrate(label) {
document.getElementById('workshop-celebration')?.remove();
const layer = document.createElement('div');
layer.id = 'workshop-celebration'; layer.className = 'workshop-confetti';
layer.setAttribute('aria-hidden', 'true');
if (!matchMedia('(prefers-reduced-motion: reduce)').matches) {
for (let n = 0; n < 64; n++) {
const piece = document.createElement('i');
piece.style.setProperty('--x', `${Math.random() * 100}vw`);
piece.style.setProperty('--drift', `${Math.random() * 150 - 75}px`);
piece.style.setProperty('--delay', `${Math.random() * .25}s`);
piece.style.setProperty('--turn', `${Math.random() * 720 - 360}deg`);
piece.style.background = ['#ed5c2b', '#327d68', '#e4b858', '#9fc9b4'][n % 4];
layer.append(piece);
}
}
document.body.append(layer);
const status = document.getElementById('workshop-success');
if (status) { status.textContent = label; status.classList.add('visible'); }
setTimeout(() => { layer.remove(); status?.classList.remove('visible'); }, 2300);
}
// Participants confirm their local results. Copying a prompt never confirms a result.
const resultButtons = [...document.querySelectorAll('[data-exercise-complete]')];
const checkpoints = [...document.querySelectorAll('[data-chapter-checkpoint]')];
const savedState = new Map();
const checkedKey = id => 'gtm-part1-checked-' + id;
const finishedKey = chapter => 'gtm-part1-finished-chapter-' + chapter;
function isSaved(key) {
try { return localStorage.getItem(key) === 'true'; }
catch { return savedState.get(key) === true; }
}
function save(key, value) {
savedState.set(key, value);
try { if (value) localStorage.setItem(key, 'true'); else localStorage.removeItem(key); } catch {}
}
function requiredResults(panel) { return JSON.parse(panel.dataset.requiredExercises); }
function updateCompletion() {
resultButtons.forEach(button => {
const checked = isSaved(checkedKey(button.dataset.exerciseComplete));
button.setAttribute('aria-pressed', String(checked));
button.textContent = checked ? '✓ I checked this result' : 'My result matches these checks ✓';
});
checkpoints.forEach(panel => {
const required = requiredResults(panel);
const count = required.filter(id => isSaved(checkedKey(id))).length;
const ready = count === required.length;
const chapter = panel.dataset.chapterCheckpoint;
if (!ready) save(finishedKey(chapter), false);
const finished = ready && isSaved(finishedKey(chapter));
const button = panel.querySelector('[data-finish-chapter]');
button.disabled = !ready || finished;
button.textContent = finished ? `Chapter ${chapter} complete ✓` : `Finish chapter ${chapter}`;
panel.querySelector('[data-chapter-status]').textContent = finished
? 'All exercise results confirmed. Chapter complete.'
: `${count} of ${required.length} exercise results confirmed. Check each result before finishing.`;
});
}
resultButtons.forEach(button => button.addEventListener('click', () => {
const key = checkedKey(button.dataset.exerciseComplete);
save(key, !isSaved(key));
updateCompletion();
}));
checkpoints.forEach(panel => panel.querySelector('[data-finish-chapter]').addEventListener('click', () => {
const chapter = panel.dataset.chapterCheckpoint;
const ready = requiredResults(panel).every(id => isSaved(checkedKey(id)));
if (!ready || isSaved(finishedKey(chapter))) { updateCompletion(); return; }
save(finishedKey(chapter), true);
updateCompletion();
celebrate(`Chapter ${chapter} complete. All exercise results confirmed.`);
}));
window.addEventListener('storage', event => {
if (event.key?.startsWith('gtm-part1-checked-') || event.key?.startsWith('gtm-part1-finished-chapter-') || event.key === null) updateCompletion();
});
updateCompletion();
// Native dialogs keep keyboard/Escape behavior. A click on the backdrop closes them.
document.querySelectorAll('dialog').forEach(dialog => dialog.addEventListener('click', event => {
const box = dialog.getBoundingClientRect();
if (event.target === dialog && (event.clientX < box.left || event.clientX > box.right ||
event.clientY < box.top || event.clientY > box.bottom)) dialog.close();
}));
document.querySelectorAll('[data-open-glossary]').forEach(button => button.addEventListener('click', () => {
document.getElementById('glossary-dialog').showModal();
}));
document.querySelectorAll('[data-close-glossary]').forEach(button => button.addEventListener('click', () => {
document.getElementById('glossary-dialog').close();
}));
window.GTMWorkshop = { attachPrompt, render, validatePrompt };
})();