204 lines
7.3 KiB
JavaScript
204 lines
7.3 KiB
JavaScript
jQuery(document).ready(function($) {
|
||
// 显示消息函数
|
||
function flm_show_message(message, type) {
|
||
var className = type === 'success' ? 'notice-success' : 'notice-error';
|
||
var $notice = $('<div class="notice ' + className + ' is-dismissible"><p>' + message + '</p></div>');
|
||
|
||
$('.wrap h1').after($notice);
|
||
|
||
// 3秒后自动隐藏
|
||
setTimeout(function() {
|
||
$notice.fadeOut(300, function() {
|
||
$(this).remove();
|
||
});
|
||
}, 3000);
|
||
}
|
||
|
||
// 使链接可排序
|
||
$('#flm-sortable-links').sortable({
|
||
handle: '.flm-link-preview',
|
||
placeholder: 'flm-link-item-placeholder',
|
||
axis: 'y',
|
||
update: function(event, ui) {
|
||
// 获取排序后的ID顺序
|
||
var linkIds = [];
|
||
$('#flm-sortable-links .flm-link-item').each(function(index) {
|
||
var linkId = $(this).find('input[name="link_ids[]"]').val();
|
||
linkIds.push(linkId);
|
||
});
|
||
|
||
// AJAX保存排序
|
||
$.ajax({
|
||
url: flm_vars.ajax_url,
|
||
type: 'POST',
|
||
data: {
|
||
action: 'flm_update_sort_order',
|
||
link_ids: linkIds,
|
||
nonce: flm_vars.nonce
|
||
},
|
||
success: function(response) {
|
||
if (response.success) {
|
||
// 显示成功消息
|
||
if (typeof flm_show_message === 'function') {
|
||
flm_show_message('排序已保存', 'success');
|
||
}
|
||
} else {
|
||
if (typeof flm_show_message === 'function') {
|
||
flm_show_message('排序保存失败', 'error');
|
||
}
|
||
}
|
||
},
|
||
error: function() {
|
||
if (typeof flm_show_message === 'function') {
|
||
flm_show_message('排序保存失败', 'error');
|
||
}
|
||
}
|
||
});
|
||
},
|
||
stop: function(event, ui) {
|
||
// 拖拽结束后,重新索引所有字段以确保数据一致性
|
||
$('#flm-sortable-links .flm-link-item').each(function(index) {
|
||
// 更新link_ids字段的值顺序,确保与DOM顺序一致
|
||
// 这里不需要改变,因为link_ids的值本身就是ID
|
||
// 但需要确保其他字段在提交时能正确对应
|
||
});
|
||
}
|
||
});
|
||
|
||
// 自动获取头像切换
|
||
$('#auto_get_icon').on('change', function() {
|
||
if ($(this).is(':checked')) {
|
||
$('#icon_field').hide();
|
||
} else {
|
||
$('#icon_field').show();
|
||
}
|
||
});
|
||
|
||
// 多选删除功能
|
||
function updateBatchDeleteButton() {
|
||
var selectedCount = $('.flm-select-link:checked').length;
|
||
var $batchButton = $('.flm-batch-delete');
|
||
|
||
if (selectedCount > 0) {
|
||
$batchButton.prop('disabled', false).text('批量删除 (' + selectedCount + ')');
|
||
} else {
|
||
$batchButton.prop('disabled', true).text('批量删除');
|
||
}
|
||
}
|
||
|
||
// 复选框变化时更新批量删除按钮
|
||
$(document).on('change', '.flm-select-link', function() {
|
||
updateBatchDeleteButton();
|
||
});
|
||
|
||
// 表单提交前确保数据顺序正确
|
||
$('#flm-links-form').on('submit', function(e) {
|
||
// PHP代码现在使用 array_values() 来处理数组,所以不需要重新索引
|
||
// 让表单正常提交,PHP会按顺序处理数组
|
||
});
|
||
|
||
// 批量删除
|
||
$('.flm-batch-delete').on('click', function(e) {
|
||
e.preventDefault();
|
||
|
||
var selectedIds = [];
|
||
$('.flm-select-link:checked').each(function() {
|
||
selectedIds.push($(this).val());
|
||
});
|
||
|
||
if (selectedIds.length === 0) {
|
||
alert('请先选择要删除的链接');
|
||
return;
|
||
}
|
||
|
||
if (!confirm('确定要删除选中的 ' + selectedIds.length + ' 个链接吗?此操作不可撤销!')) {
|
||
return;
|
||
}
|
||
|
||
var $button = $(this);
|
||
|
||
$.ajax({
|
||
url: flm_vars.ajax_url,
|
||
type: 'POST',
|
||
data: {
|
||
action: 'flm_batch_delete_links',
|
||
link_ids: selectedIds,
|
||
nonce: flm_vars.nonce
|
||
},
|
||
beforeSend: function() {
|
||
$button.prop('disabled', true).text('删除中...');
|
||
},
|
||
success: function(response) {
|
||
if (response.success) {
|
||
// 移除已删除的链接项
|
||
var $itemsToRemove = $('.flm-select-link:checked').closest('.flm-link-item');
|
||
var totalItems = $itemsToRemove.length;
|
||
var removedItems = 0;
|
||
|
||
$itemsToRemove.fadeOut(300, function() {
|
||
$(this).remove();
|
||
removedItems++;
|
||
|
||
// 只有当所有项目都被移除后才更新按钮状态
|
||
if (removedItems === totalItems) {
|
||
updateBatchDeleteButton();
|
||
}
|
||
});
|
||
|
||
var message = '成功删除 ' + response.data.deleted_count + ' 个链接';
|
||
if (response.data.error_count > 0) {
|
||
message += ',' + response.data.error_count + ' 个删除失败';
|
||
}
|
||
alert(message);
|
||
} else {
|
||
alert('批量删除失败:' + (response.data || '请重试'));
|
||
$button.prop('disabled', false).text('批量删除');
|
||
}
|
||
},
|
||
error: function() {
|
||
alert('批量删除失败,请重试');
|
||
$button.prop('disabled', false).text('批量删除');
|
||
}
|
||
});
|
||
});
|
||
|
||
// 删除单个链接
|
||
$('.flm-delete-link').on('click', function(e) {
|
||
e.preventDefault();
|
||
|
||
if (!confirm('确定要删除这个链接吗?')) {
|
||
return;
|
||
}
|
||
|
||
var $button = $(this);
|
||
var linkId = $button.data('link-id');
|
||
|
||
$.ajax({
|
||
url: flm_vars.ajax_url,
|
||
type: 'POST',
|
||
data: {
|
||
action: 'flm_delete_link',
|
||
link_id: linkId,
|
||
nonce: flm_vars.nonce
|
||
},
|
||
beforeSend: function() {
|
||
$button.prop('disabled', true).text('删除中...');
|
||
},
|
||
success: function(response) {
|
||
if (response.success) {
|
||
$button.closest('.flm-link-item').fadeOut(300, function() {
|
||
$(this).remove();
|
||
updateBatchDeleteButton();
|
||
});
|
||
} else {
|
||
alert('删除失败,请重试');
|
||
$button.prop('disabled', false).text('删除');
|
||
}
|
||
},
|
||
error: function() {
|
||
alert('删除失败,请重试');
|
||
$button.prop('disabled', false).text('删除');
|
||
}
|
||
});
|
||
});
|
||
}); |