No announcements yet. Post one to get started!
';
return;
}
this.state.posts
.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp))
.forEach(post => {
const author = this.state.authors.find(a => a.id === post.authorId);
const postEl = document.createElement('div');
postEl.className = 'announcement-post';
postEl.innerHTML = `
${post.content.replace(/\n/g, '
')}
`;
feed.appendChild(postEl);
});
},
renderAuthorSelect() {
const select = this.elements.authorSelect;
if (!select) return;
select.innerHTML = '
';
this.state.authors.forEach(author => {
select.innerHTML += `
`;
});
},
renderAuthorsTable() {
const tableBody = this.elements.authorsTableBody;
if (!tableBody) return;
tableBody.innerHTML = '';
this.state.authors.forEach(author => {
tableBody.innerHTML += `
| ${author.name} |
${author.title} |
|
`;
});
},
// --- EVENT HANDLERS & ACTIONS ---
changeTab(tabIndex) {
if (tabIndex < 0 || tabIndex >= this.elements.tabButtons.length) return;
this.state.currentTab = tabIndex;
this.elements.tabButtons.forEach((btn, i) => btn.classList.toggle('active', i === tabIndex));
this.elements.tabContents.forEach((content, i) => content.classList.toggle('active', i === tabIndex));
this.updateNavButtons();
// Show/hide PDF button based on tab
this.elements.pdfButtonContainer.style.display = tabIndex === 0 ? 'block' : 'none';
},
updateNavButtons() {
if (!this.elements.prevTabBtn || !this.elements.nextTabBtn) return;
this.elements.prevTabBtn.style.visibility = this.state.currentTab === 0 ? 'hidden' : 'visible';
this.elements.nextTabBtn.style.visibility = this.state.currentTab === this.elements.tabButtons.length - 1 ? 'hidden' : 'visible';
},
handlePostSubmit(e) {
e.preventDefault();
const form = this.elements.postForm;
const newPost = {
id: this.getNextId(this.state.posts),
title: form.querySelector('#post-title').value,
authorId: parseInt(form.querySelector('#post-author').value),
content: form.querySelector('#post-content').value,
timestamp: new Date().toISOString()
};
if (!newPost.authorId) {
this.showNotification('Please select an author.', 'error');
return;
}
this.state.posts.push(newPost);
this.showNotification('Announcement published!', 'success');
form.reset();
this.renderAnnouncements();
},
handleAuthorSubmit(e) {
e.preventDefault();
const form = this.elements.authorForm;
const id = parseInt(form.querySelector('#author-edit-id').value);
const authorData = {
name: form.querySelector('#author-name').value,
title: form.querySelector('#author-title').value,
};
if (id) {
const author = this.state.authors.find(a => a.id === id);
if (author) { Object.assign(author, authorData); }
} else {
authorData.id = this.getNextId(this.state.authors);
this.state.authors.push(authorData);
}
form.reset();
form.querySelector('#author-edit-id').value = '';
this.renderAll();
this.showNotification('Author saved.', 'success');
},
handleAuthorTableActions(e) {
const target = e.target.closest('button');
if (!target) return;
const id = parseInt(target.dataset.id);
const action = target.dataset.action;
if (action === 'edit') {
const author = this.state.authors.find(a => a.id === id);
if (author) {
const form = this.elements.authorForm;
form.querySelector('#author-edit-id').value = author.id;
form.querySelector('#author-name').value = author.name;
form.querySelector('#author-title').value = author.title;
}
} else if (action === 'delete') {
if (confirm('Are you sure? This may affect past posts.')) {
this.state.authors = this.state.authors.filter(a => a.id !== id);
this.renderAll();
}
}
},
downloadFeedPDF() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF();
doc.setFontSize(18);
doc.text('Company Announcements Feed', 105, 22, { align: 'center' });
let yPos = 35;
this.state.posts.forEach(post => {
if (yPos > 260) { // Add new page if content overflows
doc.addPage();
yPos = 20;
}
const author = this.state.authors.find(a => a.id === post.authorId);
doc.setFontSize(14);
doc.setFont(undefined, 'bold');
doc.text(post.title, 14, yPos);
doc.setFontSize(10);
doc.setFont(undefined, 'normal');
doc.setTextColor(100);
doc.text(`By ${author ? author.name : 'Unknown'} on ${new Date(post.timestamp).toLocaleDateString()}`, 14, yPos + 5);
doc.setTextColor(0);
const contentLines = doc.splitTextToSize(post.content, 180);
doc.text(contentLines, 14, yPos + 12);
yPos += contentLines.length * 5 + 20;
});
doc.save('Announcements_Feed.pdf');
},
// --- UTILITY FUNCTIONS ---
getNextId(array) {
return array.length > 0 ? Math.max(...array.map(item => item.id)) + 1 : 1;
},
showNotification(message, type = 'success') {
const box = this.elements.notificationBox;
if (!box) return;
box.textContent = message;
box.className = `sicp-notification ${type}`;
box.classList.add('show');
setTimeout(() => { box.classList.remove('show'); }, 3000);
}
};
// Make SICP object globally accessible
window.SICP = SICP;
// Start the application
SICP.init();
});