Files

369 lines
12 KiB
JavaScript

jQuery(document).ready(function($) {
// View suggestion details modal
window.pcPreviewSuggestion = function(suggestionId) {
$.ajax({
url: pcCommunitySuggestionsAdmin.ajax_url,
type: 'POST',
data: {
action: 'pc_get_suggestion',
suggestion_id: suggestionId,
nonce: pcCommunitySuggestionsAdmin.nonce
},
success: function(response) {
if (response.success) {
pcShowSuggestionModal(response.data);
} else {
alert(pcCommunitySuggestionsAdmin.i18n.action_failed + ': ' + response.data.message);
}
},
error: function() {
alert(pcCommunitySuggestionsAdmin.i18n.action_failed);
}
});
};
function pcShowSuggestionModal(suggestion) {
const modalHtml = `
<div class="pc-modal" style="display: none;">
<div class="pc-modal-overlay"></div>
<div class="pc-modal-content">
<div class="pc-modal-header">
<h3>${suggestion.title}</h3>
<button class="pc-modal-close" onclick="pcCloseModal()">&times;</button>
</div>
<div class="pc-modal-body">
<div class="pc-suggestion-content">
${suggestion.content}
</div>
<div class="pc-suggestion-meta">
<p><strong>Author:</strong> ${suggestion.author}</p>
<p><strong>Submitted:</strong> ${suggestion.date}</p>
<p><strong>Upvotes:</strong> ${suggestion.upvotes}</p>
</div>
</div>
<div class="pc-modal-footer">
<button class="button button-primary" onclick="pcCloseModal()">Close</button>
</div>
</div>
</div>
`;
$('body').append(modalHtml);
$('.pc-modal').fadeIn(200);
// Prevent background scrolling
$('body').css('overflow', 'hidden');
}
window.pcCloseModal = function() {
$('.pc-modal').fadeOut(200, function() {
$(this).remove();
});
$('body').css('overflow', '');
};
// Add comment functionality
window.pcAddComment = function(suggestionId) {
var $commentsRow = $('#pc-comments-' + suggestionId);
$commentsRow.slideToggle();
};
window.pcSubmitComment = function(suggestionId) {
var $textarea = $('#pc-comment-' + suggestionId);
var comment = $textarea.val().trim();
if (!comment) {
alert('Please enter a comment.');
return;
}
var $button = $textarea.siblings('button');
var originalText = $button.text();
$button.prop('disabled', true).text('Adding...');
$.ajax({
url: pcCommunitySuggestionsAdmin.ajax_url,
type: 'POST',
data: {
action: 'pc_add_comment',
suggestion_id: suggestionId,
comment: comment,
nonce: pcCommunitySuggestionsAdmin.nonce
},
success: function(response) {
if (response.success) {
var commentHtml = '<div class="pc-comment-item" data-comment-id="' + response.data.comment.id + '">' +
'<div class="pc-comment-header">' +
'<strong>' + response.data.comment.author + '</strong> ' +
'<span class="pc-comment-date">' + response.data.comment.date + '</span>' +
'</div>' +
'<div class="pc-comment-content">' + response.data.comment.content + '</div>' +
'<div class="pc-comment-actions">' +
'<button class="button button-small button-link-delete" onclick="pcDeleteComment(' + response.data.comment.id + ')">Delete</button>' +
'</div>' +
'</div>';
var $commentsList = $('#pc-comments-list-' + suggestionId);
$commentsList.find('.pc-no-comments').remove();
$commentsList.append(commentHtml);
$textarea.val('');
alert(response.data.message);
} else {
alert(response.data.message || pcCommunitySuggestionsAdmin.i18n.action_failed);
}
},
error: function() {
alert(pcCommunitySuggestionsAdmin.i18n.action_failed);
},
complete: function() {
$button.prop('disabled', false).text(originalText);
}
});
};
window.pcDeleteComment = function(commentId) {
if (!confirm('Are you sure you want to delete this comment?')) {
return;
}
$.ajax({
url: pcCommunitySuggestionsAdmin.ajax_url,
type: 'POST',
data: {
action: 'pc_delete_comment',
comment_id: commentId,
nonce: pcCommunitySuggestionsAdmin.nonce
},
success: function(response) {
if (response.success) {
$('.pc-comment-item[data-comment-id="' + commentId + '"]').fadeOut(300, function() {
$(this).remove();
});
alert(response.data.message);
} else {
alert(response.data.message || pcCommunitySuggestionsAdmin.i18n.action_failed);
}
},
error: function() {
alert(pcCommunitySuggestionsAdmin.i18n.action_failed);
}
});
};
// Close modal on overlay click
$(document).on('click', '.pc-modal-overlay', function(e) {
if (e.target === this) {
pcCloseModal();
}
});
// Close modal on ESC key
$(document).on('keydown', function(e) {
if (e.key === 'Escape' && $('.pc-modal').is(':visible')) {
pcCloseModal();
}
});
// Add modal styles
$('<style>')
.text(`
.pc-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 99999;
display: flex;
align-items: center;
justify-content: center;
}
.pc-modal-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
}
.pc-modal-content {
background: #fff;
border-radius: 8px;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
max-width: 600px;
width: 90%;
max-height: 80vh;
overflow: hidden;
display: flex;
flex-direction: column;
z-index: 100000;
position: relative;
}
.pc-modal-header {
padding: 20px 25px;
border-bottom: 1px solid #dcdcde;
display: flex;
align-items: center;
justify-content: space-between;
}
.pc-modal-header h3 {
margin: 0;
font-size: 1.3em;
color: #1d2327;
flex: 1;
margin-right: 20px;
}
.pc-modal-close {
background: none;
border: none;
font-size: 24px;
cursor: pointer;
padding: 0;
width: 30px;
height: 30px;
line-height: 1;
color: #646970;
}
.pc-modal-close:hover {
color: #2271b1;
}
.pc-modal-body {
padding: 25px;
flex: 1;
overflow-y: auto;
}
.pc-suggestion-content {
margin-bottom: 20px;
line-height: 1.6;
color: #2c3338;
}
.pc-suggestion-content p {
margin: 0 0 15px 0;
}
.pc-suggestion-content p:last-child {
margin-bottom: 0;
}
.pc-suggestion-meta {
border-top: 1px solid #dcdcde;
padding-top: 20px;
font-size: 14px;
}
.pc-suggestion-meta p {
margin: 0 0 8px 0;
}
.pc-suggestion-meta p:last-child {
margin-bottom: 0;
}
.pc-modal-footer {
padding: 20px 25px;
border-top: 1px solid #dcdcde;
text-align: right;
}
@media screen and (max-width: 782px) {
.pc-modal-content {
width: 95%;
margin: 20px;
}
.pc-modal-header {
padding: 15px 20px;
}
.pc-modal-body {
padding: 20px;
}
.pc-modal-footer {
padding: 15px 20px;
}
}
/* Dark mode support */
@media (prefers-color-scheme: dark) {
.pc-modal-content {
background: #2c3338;
border: 1px solid #3c434a;
}
.pc-modal-header,
.pc-modal-footer {
border-color: #3c434a;
}
.pc-modal-header h3 {
color: #dcdcde;
}
.pc-modal-close {
color: #9ca1a7;
}
.pc-modal-close:hover {
color: #2271b1;
}
.pc-suggestion-content {
color: #c3c4c7;
}
.pc-suggestion-meta {
border-color: #3c434a;
}
}
`)
.appendTo('head');
// Handle bulk actions
$('.pc-bulk-action').on('change', function() {
const action = $(this).val();
if (action) {
const checked = $('.pc-bulk-checkbox:checked');
if (checked.length === 0) {
alert(pcCommunitySuggestionsAdmin.i18n.no_items_selected);
$(this).val('');
return;
}
const suggestionIds = checked.map(function() {
return $(this).val();
}).get();
if (confirm(pcCommunitySuggestionsAdmin.i18n.confirm_bulk_action.replace('{count}', checked.length).replace('{action}', action))) {
$.ajax({
url: pcCommunitySuggestionsAdmin.ajax_url,
type: 'POST',
data: {
action: 'pc_get_stats',
nonce: pcCommunitySuggestionsAdmin.nonce
},
success: function(response) {
if (response.success) {
$('#pc-stat-total').text(response.data.total);
}
}
});
}
// Update stats every 30 seconds if on stats page
if ($('.pc-settings-stats').length) {
setInterval(pcUpdateStats, 30000);
}
});