-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
260 lines (228 loc) · 9.68 KB
/
Copy pathscript.js
File metadata and controls
260 lines (228 loc) · 9.68 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Mobile Navigation Toggle
document.addEventListener('DOMContentLoaded', function() {
const hamburger = document.getElementById('hamburger');
const mobileNav = document.getElementById('mobileNav');
if (hamburger && mobileNav) {
hamburger.addEventListener('click', function() {
hamburger.classList.toggle('active');
mobileNav.classList.toggle('active');
});
// Close mobile menu when clicking a link
const mobileLinks = mobileNav.querySelectorAll('.nav-link');
mobileLinks.forEach(link => {
link.addEventListener('click', function() {
hamburger.classList.remove('active');
mobileNav.classList.remove('active');
});
});
// Close mobile menu when clicking outside
document.addEventListener('click', function(event) {
const isClickInsideNav = mobileNav.contains(event.target);
const isClickOnHamburger = hamburger.contains(event.target);
if (!isClickInsideNav && !isClickOnHamburger && mobileNav.classList.contains('active')) {
hamburger.classList.remove('active');
mobileNav.classList.remove('active');
}
});
}
});
// Contact Form Handling (Formspree + Fetch)
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', async function(e) {
e.preventDefault();
const formNote = document.getElementById('formNote');
const submitBtn = contactForm.querySelector('button[type="submit"]');
// Basic client-side validation
const name = contactForm.querySelector('#name');
const email = contactForm.querySelector('#email');
const message = contactForm.querySelector('#message');
if (!name.value.trim() || !email.value.trim() || !message.value.trim()) {
formNote.style.display = 'block';
formNote.textContent = 'Please fill out the required fields.';
return;
}
// Disable button while submitting
submitBtn.disabled = true;
const originalText = submitBtn.textContent;
submitBtn.textContent = 'Sending...';
// Prepare form data
const data = new FormData(contactForm);
try {
const action = contactForm.getAttribute('action');
const resp = await fetch(action, {
method: 'POST',
body: data,
headers: {
'Accept': 'application/json'
}
});
if (resp.ok) {
formNote.style.display = 'block';
formNote.textContent = 'Thanks -- I\'ll get back to you soon!';
contactForm.reset();
} else {
const result = await resp.json().catch(() => ({}));
formNote.style.display = 'block';
formNote.textContent = result.error || 'There was a problem sending your message. Please try again later.';
}
} catch (err) {
formNote.style.display = 'block';
formNote.textContent = 'Network error. Please check your connection and try again.';
} finally {
submitBtn.disabled = false;
submitBtn.textContent = originalText;
}
});
}
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
// Use the element's `hash` property to avoid invalid selector issues
const hash = this.hash; // returns string like "#section" or ""
if (!hash || hash === '#') return; // nothing to scroll to
// Only prevent default when we have a valid target to scroll to
const target = document.querySelector(hash);
if (target) {
e.preventDefault();
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// Generic Modal Handler
function openModal(modalId) {
const modal = document.getElementById(modalId);
if (modal) {
modal.classList.add('active');
document.body.style.overflow = 'hidden';
}
}
function closeModal(modalId) {
const modal = document.getElementById(modalId);
if (modal) {
modal.classList.remove('active');
document.body.style.overflow = '';
}
}
// Clear embedded PDF content when closing the PDF modal
const originalCloseModal = closeModal;
closeModal = function(modalId) {
if (modalId === 'pdfModal') {
const pdfContainer = document.getElementById('pdfViewerContainer');
if (pdfContainer) pdfContainer.innerHTML = '';
}
originalCloseModal(modalId);
};
// Handle all data-modal buttons (open modal)
document.querySelectorAll('[data-modal]').forEach(button => {
button.addEventListener('click', function(e) {
e.preventDefault();
const modalId = this.getAttribute('data-modal');
// Support both direct modal IDs and legacy format (e.g., "script-request" -> "scriptModal")
const resolvedModalId = modalId === 'script-request' ? 'scriptModal' : (modalId + '-modal');
openModal(resolvedModalId);
});
});
// Handle all modal close buttons (via data-close-modal)
document.querySelectorAll('[data-close-modal]').forEach(closeBtn => {
closeBtn.addEventListener('click', function() {
const modalId = this.getAttribute('data-close-modal');
closeModal(modalId);
});
});
// Legacy support: handle old closeModal ID buttons
const legacyCloseModal = document.getElementById('closeModal');
if (legacyCloseModal) {
legacyCloseModal.addEventListener('click', function() {
closeModal('scriptModal');
});
}
// PDF.js loader + renderer
const PDFJS_CDN = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.min.js';
const PDFJS_WORKER_CDN = 'https://cdnjs.cloudflare.com/ajax/libs/pdf.js/2.16.105/pdf.worker.min.js';
function loadPdfJsOnce() {
return new Promise((resolve, reject) => {
if (window.pdfjsLib) return resolve(window.pdfjsLib);
const s = document.createElement('script');
s.src = PDFJS_CDN;
s.onload = () => {
if (!window.pdfjsLib) return reject(new Error('pdfjs failed to load'));
window.pdfjsLib.GlobalWorkerOptions.workerSrc = PDFJS_WORKER_CDN;
resolve(window.pdfjsLib);
};
s.onerror = () => reject(new Error('Failed to load pdfjs script'));
document.head.appendChild(s);
});
}
async function renderPdfToContainer(pdfUrl, containerEl) {
containerEl.innerHTML = '';
const loading = document.createElement('div');
loading.textContent = 'Loading document...';
loading.style.padding = '1rem';
containerEl.appendChild(loading);
try {
const pdfjsLib = await loadPdfJsOnce();
const loadingTask = pdfjsLib.getDocument(pdfUrl);
const pdf = await loadingTask.promise;
containerEl.innerHTML = '';
for (let pageNum = 1; pageNum <= pdf.numPages; pageNum++) {
// eslint-disable-next-line no-await-in-loop
const page = await pdf.getPage(pageNum);
const viewport = page.getViewport({ scale: 1 });
const containerWidth = containerEl.clientWidth || (window.innerWidth * 0.9);
const scale = (containerWidth / viewport.width) * (window.devicePixelRatio || 1);
const scaledViewport = page.getViewport({ scale });
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = Math.floor(scaledViewport.width);
canvas.height = Math.floor(scaledViewport.height);
canvas.style.width = '100%';
canvas.style.height = 'auto';
containerEl.appendChild(canvas);
const renderContext = { canvasContext: context, viewport: scaledViewport };
// eslint-disable-next-line no-await-in-loop
await page.render(renderContext).promise;
}
} catch (err) {
containerEl.innerHTML = '<p style="padding:1rem;color:var(--rust);">Failed to load document.</p>';
console.error('PDF render error', err);
}
}
// Handle PDF preview buttons using PDF.js renderer
document.querySelectorAll('[data-pdf]').forEach(button => {
button.addEventListener('click', async function(e) {
e.preventDefault();
const pdfUrl = this.getAttribute('data-pdf');
const parent = this.closest('.writing-item') || this.closest('.content-card') || document.body;
const titleEl = parent.querySelector('.writing-title, .card-title');
const title = titleEl ? titleEl.textContent.trim() : 'Preview';
const pdfContainer = document.getElementById('pdfViewerContainer');
const pdfTitle = document.getElementById('pdfTitle');
const pdfDownload = document.getElementById('pdfDownload');
const pdfOpenNew = document.getElementById('pdfOpenNew');
if (pdfTitle) pdfTitle.textContent = title;
if (pdfDownload) pdfDownload.href = pdfUrl;
if (pdfOpenNew) pdfOpenNew.href = pdfUrl;
openModal('pdfModal');
if (pdfContainer) await renderPdfToContainer(pdfUrl, pdfContainer);
});
});
// Close modal when clicking outside
document.querySelectorAll('.modal-overlay').forEach(modal => {
modal.addEventListener('click', function(e) {
if (e.target === this) {
closeModal(this.id);
}
});
});
// Close modal with Escape key
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
document.querySelectorAll('.modal-overlay.active').forEach(modal => {
closeModal(modal.id);
});
}
});