jQuery(document).ready(function($) {
'use strict';
console.log('DigitsGraph: Admin Scripts Loaded');
// ============================================
// 1. مقداردهی اولیه رنگها
// ============================================
if ($.fn.wpColorPicker) {
$('.color-picker').wpColorPicker();
}
// ============================================
// 2. مدیریت نمایش فیلدهای پسزمینه
// ============================================
$('#background_type').on('change', function() {
var type = $(this).val();
$('#color_field').toggle(type === 'color' || type === 'normal');
$('#image_field').toggle(type === 'image');
$('#glass_field').toggle(type === 'glass');
}).trigger('change');
// ============================================
// 3. جمعشدن/بازشدن بخشها
// ============================================
$('.digitsgraph-section-title').on('click', function() {
var content = $(this).next('.digitsgraph-section-content');
var toggle = $(this).find('.section-toggle');
if (content.is(':visible')) {
content.slideUp(300);
toggle.removeClass('open');
toggle.text('▼');
} else {
content.slideDown(300);
toggle.addClass('open');
toggle.text('▲');
}
});
// ============================================
// 4. آپلود تصویر
// ============================================
$('.digitsgraph-upload-btn').on('click', function(e) {
e.preventDefault();
var button = $(this);
var input = button.closest('.digitsgraph-image-upload').find('.digitsgraph-image-url');
var key = input.attr('name').replace('digitsgraph_settings[', '').replace(']', '');
if (typeof wp !== 'undefined' && wp.media) {
var frame = wp.media({
title: 'انتخاب تصویر',
button: {
text: 'انتخاب تصویر'
},
multiple: false
});
frame.on('select', function() {
var attachment = frame.state().get('selection').first().toJSON();
input.val(attachment.url);
var preview = button.closest('.digitsgraph-image-upload').find('.digitsgraph-image-preview');
if (preview.length) {
preview.attr('src', attachment.url).show();
} else {
button.closest('.digitsgraph-image-upload').append(
'
'
);
}
// ارسال به سرور
$.ajax({
url: digitsgraphAjax.ajaxurl,
type: 'POST',
data: {
action: 'digitsgraph_update_setting',
key: key,
value: attachment.url,
nonce: digitsgraphAjax.nonce
},
success: function(response) {
if (response.success) {
showNotification('✅ تصویر با موفقیت ذخیره شد', 'success');
}
}
});
});
frame.open();
} else {
alert('لطفاً کتابخانه رسانه وردپرس را بارگذاری کنید.');
}
});
// ============================================
// 5. پیشنمایش
// ============================================
$('.digitsgraph-preview-btn').on('click', function(e) {
e.preventDefault();
var modal = $('#previewModal');
var button = $(this);
// نمایش مودال
modal.addClass('active');
$('body').css('overflow', 'hidden');
var container = $('#previewContainer');
container.html('
در حال بارگذاری پیشنمایش...
');
// ارسال درخواست AJAX
$.ajax({
url: digitsgraphAjax.ajaxurl,
type: 'POST',
data: {
action: 'digitsgraph_get_preview',
nonce: digitsgraphAjax.nonce
},
timeout: 30000,
success: function(response) {
console.log('Preview Response:', response);
if (response.success) {
container.html(response.data.html);
// اعمال تنظیمات روی پیشنمایش
applyPreviewSettings(response.data.settings);
} else {
container.html('❌ خطا در بارگذاری پیشنمایش: ' + (response.data?.message || 'خطای ناشناخته') + '
');
}
},
error: function(xhr, status, error) {
console.error('Preview Error:', error);
container.html('❌ خطا در ارتباط با سرور: ' + error + '
');
}
});
});
// ============================================
// 6. بستن مودال
// ============================================
$('.digitsgraph-modal-close').on('click', function() {
$('#previewModal').removeClass('active');
$('body').css('overflow', '');
});
$('#previewModal').on('click', function(e) {
if ($(e.target).is(this)) {
$(this).removeClass('active');
$('body').css('overflow', '');
}
});
$(document).on('keydown', function(e) {
if (e.key === 'Escape') {
$('#previewModal').removeClass('active');
$('body').css('overflow', '');
}
});
// ============================================
// 7. ریست تنظیمات
// ============================================
$('.digitsgraph-reset-btn').on('click', function() {
if (!confirm('⚠️ آیا مطمئن هستید که میخواهید تمام تنظیمات را به حالت اولیه بازگردانید؟')) {
return;
}
var button = $(this);
button.prop('disabled', true).html(' در حال ریست...');
$.ajax({
url: digitsgraphAjax.ajaxurl,
type: 'POST',
data: {
action: 'digitsgraph_reset_settings',
nonce: digitsgraphAjax.nonce
},
success: function(response) {
if (response.success) {
showNotification('✅ تنظیمات با موفقیت ریست شد', 'success');
setTimeout(function() {
location.reload();
}, 1500);
} else {
showNotification('❌ خطا در ریست تنظیمات', 'error');
button.prop('disabled', false).text('ریست تنظیمات');
}
},
error: function() {
showNotification('❌ خطا در ارتباط با سرور', 'error');
button.prop('disabled', false).text('ریست تنظیمات');
}
});
});
// ============================================
// 8. اعمال پیشنمایش
// ============================================
$('.digitsgraph-apply-preview').on('click', function() {
$('.digitsgraph-form').submit();
$('#previewModal').removeClass('active');
$('body').css('overflow', '');
showNotification('✅ تغییرات با موفقیت ذخیره شد', 'success');
});
// ============================================
// 9. نمایش نوتیفیکیشن
// ============================================
function showNotification(message, type) {
var $notification = $('' + message + '
');
$('body').append($notification);
setTimeout(function() {
$notification.fadeOut(500, function() {
$(this).remove();
});
}, 4000);
}
// ============================================
// 10. ذخیره خودکار تغییرات
// ============================================
var saveTimeout;
$('.digitsgraph-form select, .digitsgraph-form input[type="checkbox"], .digitsgraph-form input[type="number"], .digitsgraph-form input[type="text"]').on('change keyup', function() {
if ($(this).attr('name') === 'digitsgraph_settings[enabled]') {
return;
}
clearTimeout(saveTimeout);
saveTimeout = setTimeout(function() {
var $form = $('.digitsgraph-form');
var formData = $form.serialize();
$.ajax({
url: $form.attr('action') || window.location.href,
type: 'POST',
data: formData,
success: function() {
// بهروزرسانی پیشنمایش
if ($('#previewModal').hasClass('active')) {
$('.digitsgraph-preview-btn').click();
}
}
});
}, 1500);
});
// ============================================
// 11. اعمال تنظیمات روی پیشنمایش
// ============================================
function applyPreviewSettings(settings) {
if (!settings) return;
var preview = $('.digits-preview-form');
// اعمال رنگ پسزمینه
if (settings.background_color) {
preview.css('background-color', settings.background_color);
}
// اعمال تصویر پسزمینه
if (settings.background_image) {
preview.css({
'background-image': 'url(' + settings.background_image + ')',
'background-size': 'cover',
'background-position': 'center'
});
}
// اعمال گردی گوشهها
if (settings.border_radius) {
preview.css('border-radius', settings.border_radius + 'px');
preview.find('input, button').css('border-radius', settings.border_radius + 'px');
}
// اعمال سایه
if (settings.shadow && settings.shadow !== 'none') {
var shadows = {
'small': '0 2px 8px rgba(0,0,0,0.1)',
'medium': '0 4px 16px rgba(0,0,0,0.15)',
'large': '0 8px 32px rgba(0,0,0,0.2)'
};
if (shadows[settings.shadow]) {
preview.css('box-shadow', shadows[settings.shadow]);
}
}
console.log('✅ Preview settings applied:', settings);
}
// ============================================
// 12. نمایش خطاهای AJAX
// ============================================
$(document).ajaxError(function(event, jqxhr, settings, thrownError) {
console.error('AJAX Error:', thrownError);
});
console.log('✅ DigitsGraph: Admin ready');
});
Warning: Cannot modify header information - headers already sent by (output started at /home/itivir/agahyraygan.ir/wp-content/plugins/digitsgraph/includes/class-ajax.php:1) in /home/itivir/agahyraygan.ir/wp-includes/pluggable.php on line 1535
Warning: Cannot modify header information - headers already sent by (output started at /home/itivir/agahyraygan.ir/wp-content/plugins/digitsgraph/includes/class-ajax.php:1) in /home/itivir/agahyraygan.ir/wp-includes/pluggable.php on line 1538