281 行
12 KiB
JavaScript
281 行
12 KiB
JavaScript
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'backend/format'], function ($, undefined, Backend, Table, Form, Format) {
|
|
|
|
/**
|
|
* 解析分类图标地址
|
|
* @param {string} iconKey 标识或路径
|
|
* @param {number} id 分类ID
|
|
* @returns {string} 完整的静态图片URL
|
|
*/
|
|
function resolveIconUrl(iconKey, id) {
|
|
if (!iconKey) {
|
|
iconKey = 'cat-job';
|
|
}
|
|
if (/^(https?:\/\/|\/uploads\/|\/static\/)/i.test(iconKey)) {
|
|
return iconKey;
|
|
}
|
|
var file = (iconKey.indexOf('.') !== -1) ? iconKey : iconKey + '.png';
|
|
return '/static/icons/' + file + '?v=2';
|
|
}
|
|
|
|
var Controller = {
|
|
index: function () {
|
|
// 初始化表格参数配置
|
|
Table.api.init({
|
|
extend: {
|
|
index_url: 'fc/category/index' + location.search,
|
|
add_url: 'fc/category/add',
|
|
edit_url: 'fc/category/edit',
|
|
del_url: 'fc/category/del',
|
|
multi_url: 'fc/category/multi',
|
|
import_url: 'fc/category/import',
|
|
table: 'fc_category',
|
|
}
|
|
});
|
|
|
|
var table = $("#table");
|
|
|
|
// 初始化表格
|
|
table.bootstrapTable({
|
|
url: $.fn.bootstrapTable.defaults.extend.index_url,
|
|
pk: 'id',
|
|
sortName: 'sort',
|
|
sortOrder: 'asc',
|
|
escape: false,
|
|
columns: [
|
|
[
|
|
{checkbox: true, width: '40px'},
|
|
{
|
|
field: 'id',
|
|
title: __('SerialNo'),
|
|
operate: false,
|
|
sortable: false,
|
|
align: 'center',
|
|
width: '60px',
|
|
class: 'fc-serial',
|
|
table: table,
|
|
formatter: Format.serial
|
|
},
|
|
|
|
// 1. 图标预览(微质感微卡片)
|
|
{
|
|
field: 'icon_preview',
|
|
title: __('Icon'),
|
|
operate: false,
|
|
align: 'center',
|
|
width: '80px',
|
|
formatter: function (value, row, index) {
|
|
var url = resolveIconUrl(row.icon_key, row.id);
|
|
var color = row.color || '#00E070';
|
|
return '<div style="display:inline-flex;align-items:center;justify-content:center;width:40px;height:40px;border-radius:10px;background:' + color + '18;border:1px solid ' + color + '40;padding:2px;">' +
|
|
'<img src="' + url + '" style="width:28px;height:28px;object-fit:contain;" onerror="this.src=\'/static/icons/cat-job.png\'" />' +
|
|
'</div>';
|
|
}
|
|
},
|
|
|
|
// 2. 分类名称(留足宽度,带主题色小圆点)
|
|
{
|
|
field: 'name',
|
|
title: __('Name'),
|
|
operate: 'LIKE',
|
|
align: 'left',
|
|
width: '200px',
|
|
formatter: function (value, row, index) {
|
|
var color = row.color || '#00E070';
|
|
var dot = '<span style="display:inline-block;width:10px;height:10px;border-radius:50%;background:' + color + ';margin-right:10px;vertical-align:middle;box-shadow:0 0 6px ' + color + '99;"></span>';
|
|
return '<div style="display:flex;align-items:center;">' + dot + '<span style="font-weight:600;font-size:14px;color:#2c3e50;">' + value + '</span></div>';
|
|
}
|
|
},
|
|
|
|
// 3. 主题色
|
|
{
|
|
field: 'color',
|
|
title: __('Color'),
|
|
operate: 'LIKE',
|
|
align: 'center',
|
|
width: '130px',
|
|
formatter: function (value, row, index) {
|
|
var color = value || '#00E070';
|
|
return '<span class="badge" style="background:' + color + ';color:#ffffff;font-size:12px;font-family:monospace;padding:4px 10px;border-radius:6px;text-shadow:0 1px 2px rgba(0,0,0,0.3);">' + color + '</span>';
|
|
}
|
|
},
|
|
|
|
// 4. 排序权重
|
|
{
|
|
field: 'sort',
|
|
title: __('Sort'),
|
|
align: 'center',
|
|
width: '110px',
|
|
sortable: true,
|
|
formatter: function (value, row, index) {
|
|
return '<span class="badge" style="background:#eef7f2;color:#04874a;font-weight:700;font-size:13px;padding:4px 10px;border-radius:8px;border:1px solid #bcead1;">' + value + '</span>';
|
|
}
|
|
},
|
|
|
|
// 5. 发布帖子数
|
|
{
|
|
field: 'post_count',
|
|
title: __('Post_count'),
|
|
align: 'center',
|
|
width: '120px',
|
|
sortable: true,
|
|
formatter: function (value, row, index) {
|
|
var count = Number(value || 0);
|
|
var bg = count > 0 ? '#f0fdf4' : '#f8f9fa';
|
|
var text = count > 0 ? '#15803d' : '#888';
|
|
return '<span class="label" style="background:' + bg + ';color:' + text + ';border-radius:8px;padding:4px 10px;font-size:12px;font-weight:600;display:inline-block;">' + count + ' 篇</span>';
|
|
}
|
|
},
|
|
|
|
// 6. 启用状态(原生点击切换开关)
|
|
{
|
|
field: 'enabled',
|
|
title: __('Enabled'),
|
|
align: 'center',
|
|
width: '100px',
|
|
searchList: {0: '已禁用', 1: '已启用'},
|
|
formatter: Table.api.formatter.toggle
|
|
},
|
|
|
|
// 7. 更新时间
|
|
{
|
|
field: 'updatetime',
|
|
title: __('Updatetime'),
|
|
operate: 'RANGE',
|
|
addclass: 'datetimerange',
|
|
autocomplete: false,
|
|
align: 'center',
|
|
width: '180px',
|
|
formatter: Table.api.formatter.datetime
|
|
},
|
|
|
|
// 8. 操作栏
|
|
{
|
|
field: 'operate',
|
|
title: __('Operate'),
|
|
table: table,
|
|
align: 'center',
|
|
width: '120px',
|
|
events: Table.api.events.operate,
|
|
formatter: Table.api.formatter.operate
|
|
}
|
|
]
|
|
]
|
|
});
|
|
|
|
// 为表格绑定事件
|
|
Table.api.bindevent(table);
|
|
},
|
|
|
|
add: function () {
|
|
Controller.api.bindFormInteractions();
|
|
Controller.api.bindevent();
|
|
},
|
|
|
|
edit: function () {
|
|
Controller.api.bindFormInteractions();
|
|
Controller.api.bindevent();
|
|
},
|
|
|
|
api: {
|
|
bindevent: function () {
|
|
Form.api.bindevent($("form[role=form]"));
|
|
},
|
|
|
|
/**
|
|
* 绑定新增与编辑表单的高级可视化交互
|
|
*/
|
|
bindFormInteractions: function () {
|
|
var $iconInput = $('#c-icon_key');
|
|
var $nameInput = $('#c-name');
|
|
var $colorInput = $('#c-color');
|
|
var $colorAddon = $('#color-preview-addon');
|
|
var $previewBadge = $('#preview-badge');
|
|
var $previewIcon = $('#preview-icon');
|
|
var $previewName = $('#preview-name');
|
|
var $currentIconImg = $('#current-icon-img');
|
|
var $iconModal = $('#fc-icon-modal');
|
|
|
|
// 刷新界面预览元素(纯视觉图形,无死板业务文字)
|
|
function updateLivePreview() {
|
|
var name = $nameInput.val() || '未命名分类';
|
|
var key = $iconInput.val() || 'cat-job';
|
|
var color = $colorInput.val() || '#00E070';
|
|
var iconUrl = resolveIconUrl(key);
|
|
|
|
if ($previewName.length) $previewName.text(name);
|
|
if ($previewIcon.length) $previewIcon.attr('src', iconUrl);
|
|
if ($currentIconImg.length) $currentIconImg.attr('src', iconUrl);
|
|
|
|
if ($previewBadge.length) {
|
|
$previewBadge.css({
|
|
'background': color + '22',
|
|
'border-color': color + '55'
|
|
});
|
|
}
|
|
if ($colorAddon.length) $colorAddon.css('background', color);
|
|
}
|
|
|
|
// 确保初始状态完全隐藏
|
|
$iconModal.removeClass('open').attr('style', 'display:none !important;');
|
|
|
|
// 1. 打开纯插画挑选面板(只有用户主动点击按钮才打开)
|
|
$(document).on('click', '#btn-open-icon-picker', function (e) {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
$iconModal.removeAttr('style').addClass('open').fadeIn(150);
|
|
});
|
|
|
|
// 关闭插画挑选面板
|
|
function closeIconModal() {
|
|
$iconModal.removeClass('open').fadeOut(120, function () {
|
|
$(this).attr('style', 'display:none !important;');
|
|
});
|
|
}
|
|
|
|
$(document).on('click', '#btn-close-icon-modal, .fc-modal-overlay', function (e) {
|
|
e.preventDefault();
|
|
closeIconModal();
|
|
});
|
|
|
|
// 2. 点选插画卡片(只设图标,绝不改写用户分类名称)
|
|
$(document).on('click', '.fc-icon-chip', function (e) {
|
|
e.preventDefault();
|
|
var key = $(this).data('key');
|
|
$iconInput.val(key).trigger('change');
|
|
$('.fc-icon-chip').removeClass('active');
|
|
$(this).addClass('active');
|
|
updateLivePreview();
|
|
closeIconModal();
|
|
});
|
|
|
|
// 3. 自定义上传图片监听
|
|
$iconInput.on('input change', function () {
|
|
updateLivePreview();
|
|
});
|
|
|
|
// 4. 监听分类名称输入
|
|
$nameInput.on('input', function () {
|
|
updateLivePreview();
|
|
});
|
|
|
|
// 5. 监听颜色输入与推荐调色板点选
|
|
$colorInput.on('input change', function () {
|
|
updateLivePreview();
|
|
});
|
|
|
|
$(document).on('click', '.fc-color-presets .fc-color-dot', function () {
|
|
var color = $(this).data('color');
|
|
$colorInput.val(color).trigger('change');
|
|
updateLivePreview();
|
|
});
|
|
|
|
// 初始化回显
|
|
var initKey = $iconInput.val() || 'cat-job';
|
|
$('.fc-icon-chip[data-key="' + initKey + '"]').addClass('active');
|
|
updateLivePreview();
|
|
}
|
|
}
|
|
};
|
|
return Controller;
|
|
});
|