').text(comment.comment).html().replace(/\n/g, '
');
let deleteHtml = '';
if (canDelete) {
deleteHtml = `
`;
}
return `
${user.first_name + ' ' + user.last_name|| 'User'}
${createdAt}
${deleteHtml}
${commentHtml}
`;
}
$('.comment-btn').on('click', function() {
currentBlogId = $(this).data('id');
$('#comment-blog-id').val(currentBlogId);
$('#comments-list').html('
Loading comments...
');
// Show modal
$('#commentsModal').modal('show');
$.get("/blog-comments/" + currentBlogId, function(res) {
if (!res.comments || res.comments.length === 0) {
$('#comments-list').html(
'
No comments yet. Be the first to share your thoughts.
'
);
return;
}
let html = '';
res.comments.forEach(function(c) {
html += renderCommentItem(c);
});
$('#comments-list').html(html);
});
});
// Delete comment (only for comment owner)
$('#comments-list').on('click', '.delete-comment-btn', function() {
let commentId = $(this).data('id');
if (!confirm('Are you sure you want to delete this comment?')) {
return;
}
$.ajax({
url: "/delete-blog-comment/" + commentId,
type: "POST",
data: {
_method: 'DELETE',
_token: document.querySelector('meta[name=csrf-token]').content
},
success: function() {
let wrapper = $('[data-comment-id="' + commentId + '"]');
wrapper.remove();
if (currentBlogId) {
let countEl = $('#comments-count-' + currentBlogId);
if (countEl.length) {
let current = parseInt(countEl.text(), 10) || 0;
countEl.text(Math.max(0, current - 1));
}
}
if (!$('#comments-list').children().length) {
$('#comments-list').html(
'
No comments yet. Be the first to share your thoughts.
'
);
}
}
});
});
// Simple custom emoji picker for comment textarea (no external library)
document.addEventListener('DOMContentLoaded', function() {
const button = document.querySelector('#emoji-btn');
const textarea = document.querySelector('#comment-text');
if (!button || !textarea) {
return;
}
const emojis = [
'😀', '😁', '😂', '🤣', '😃', '😄', '😅', '😊', '😇', '🙂',
'🙃', '😉', '😍', '😘', '😜', '🤗', '🤔', '😐', '😑', '😶',
'😏', '😣', '😥', '😮', '🤐', '😯', '😪', '😴', '😌', '🤓',
'😎', '🤩', '🥳', '😕', '😟', '🙁', '😮💨', '😢', '😭', '😤',
'😡', '👍', '👎', '🙏', '👏', '🙌', '💪', '❤️', '💔', '✨'
];
// Create panel
const panel = document.createElement('div');
panel.id = 'emoji-panel';
panel.style.position = 'absolute';
panel.style.zIndex = '9999';
panel.style.display = 'none';
panel.style.background = '#fff';
panel.style.border = '1px solid #ddd';
panel.style.borderRadius = '6px';
panel.style.padding = '6px';
panel.style.boxShadow = '0 2px 8px rgba(0,0,0,0.15)';
panel.style.maxWidth = '260px';
panel.style.fontSize = '18px';
panel.style.lineHeight = '1.6';
panel.style.cursor = 'pointer';
emojis.forEach(e => {
const span = document.createElement('span');
span.textContent = e;
span.style.margin = '2px';
span.dataset.emoji = e;
panel.appendChild(span);
});
document.body.appendChild(panel);
function togglePanel() {
const rect = button.getBoundingClientRect();
panel.style.left = (rect.left + window.scrollX) + 'px';
panel.style.top = (rect.bottom + window.scrollY + 4) + 'px';
panel.style.display = (panel.style.display === 'none' || panel.style.display === '') ? 'block' :
'none';
}
button.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
togglePanel();
});
panel.addEventListener('click', function(e) {
const emoji = e.target.dataset.emoji;
if (!emoji) return;
const start = textarea.selectionStart || 0;
const end = textarea.selectionEnd || 0;
const text = textarea.value;
textarea.value = text.slice(0, start) + emoji + text.slice(end);
textarea.focus();
const cursorPos = start + emoji.length;
textarea.setSelectionRange(cursorPos, cursorPos);
panel.style.display = 'none';
});
// Hide panel when clicking outside
document.addEventListener('click', function() {
panel.style.display = 'none';
});
});
// Share
$('.share-btn').on('click', function() {
let url = $(this).data('url');
let heading = $(this).data('heading');
let shareText = heading + ' - ' + url;
// If Web Share API is available
if (navigator.share) {
navigator.share({
heading: heading,
text: heading,
url: url
}).catch(function() {
// ignore errors (user cancelled etc.)
});
return;
}
// WhatsApp
let whatsappUrl = 'https://api.whatsapp.com/send?text=' + encodeURIComponent(shareText);
window.open(whatsappUrl, '_blank');
// LinkedIn (share as well)
let linkedInUrl = 'https://www.linkedin.com/sharing/share-offsite/?url=' + encodeURIComponent(url);
window.open(linkedInUrl, '_blank');
// Copy link
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(url).then(function() {
// Optional: small feedback
});
} else {
// Fallback copy
let tempInput = document.createElement('input');
tempInput.value = url;
document.body.appendChild(tempInput);
tempInput.select();
document.execCommand('copy');
document.body.removeChild(tempInput);
}
});