/** * 统一统计卡:点击筛选高亮、计数更新、列表状态拖拽到卡片改状态(参照 proj_prps.html) */ (function (global) { 'use strict'; const _registry = {}; const _filterState = {}; function _scopeEl(scopeId) { return document.querySelector('[data-stat-scope="' + scopeId + '"]'); } function _cfg(scopeId) { return _registry[scopeId] || null; } function register(scopeId, cfg) { _registry[scopeId] = Object.assign({ statusField: 'status', defaultFilter: 'all', legacyFilterName: 'filterByStatus', }, cfg || {}); if (_filterState[scopeId] === undefined) { _filterState[scopeId] = _registry[scopeId].defaultFilter || 'all'; } const legacy = _registry[scopeId].legacyFilterName; if (legacy && typeof global[legacy] !== 'function') { global[legacy] = function (status) { onClick(scopeId, status); }; } if (_registry[scopeId].drag) { initDropTargets(scopeId); } } function connect(scopeId, cfg) { const prev = _registry[scopeId] || {}; register(scopeId, Object.assign({}, prev, cfg || {})); } function getFilter(scopeId) { return _filterState[scopeId] !== undefined ? _filterState[scopeId] : 'all'; } function setFilter(scopeId, statusKey) { _filterState[scopeId] = statusKey; } function setActive(scopeId, statusKey) { const scope = _scopeEl(scopeId); if (!scope) return; scope.querySelectorAll('.stat-card').forEach(function (card) { card.classList.remove('active', 'border', 'border-primary'); }); const key = statusKey === undefined || statusKey === null ? 'all' : String(statusKey); let card = scope.querySelector('.stat-card[data-stat-key="' + key.replace(/"/g, '\\"') + '"]'); if (!card && key === 'all') { card = scope.querySelector('.stat-card[data-stat-key="all"]'); } if (card) card.classList.add('active'); } function updateCounts(scopeId, items, statusField) { const scope = _scopeEl(scopeId); if (!scope) return; const cfg = _cfg(scopeId); const field = statusField || (cfg && cfg.statusField) || 'status'; const getStatus = (cfg && cfg.getStatus) || function (item) { return item && item[field] != null ? String(item[field]) : ''; }; const list = items || []; scope.querySelectorAll('.stat-card[data-stat-key]').forEach(function (card) { const key = card.getAttribute('data-stat-key'); const countId = card.getAttribute('data-count-id'); if (!countId) return; const el = document.getElementById(countId); if (!el) return; if (key === 'all') { el.textContent = list.length; } else { el.textContent = list.filter(function (item) { return getStatus(item) === key; }).length; } }); setActive(scopeId, getFilter(scopeId)); } function onClick(scopeId, statusKey) { setFilter(scopeId, statusKey); setActive(scopeId, statusKey); const cfg = _cfg(scopeId); if (cfg && typeof cfg.onFilter === 'function') { cfg.onFilter(statusKey); } } let _clickDelegateBound = false; function _bindGlobalClickDelegate() { if (_clickDelegateBound) return; _clickDelegateBound = true; document.addEventListener('click', function (e) { const card = e.target.closest('.stat-card.is-clickable'); if (!card || card.dataset.statClickBound === '1') return; const scope = card.closest('[data-stat-scope]'); if (!scope) return; const scopeId = scope.getAttribute('data-stat-scope'); if (!scopeId) return; const key = card.getAttribute('data-stat-key'); if (key == null || key === '') return; onClick(scopeId, key); }); } function initDropTargets(scopeId) { const cfg = _cfg(scopeId); if (!cfg || !cfg.drag) return; const drag = cfg.drag; const table = typeof drag.table === 'string' ? document.querySelector(drag.table) : drag.table; if (!table || table.dataset.statDropInit === '1') return; table.dataset.statDropInit = '1'; const dragKind = drag.kind; const onDrop = drag.onDrop || cfg.onStatusDrop; table.addEventListener('dragstart', function (e) { if (e.target && (e.target.tagName === 'SELECT' || e.target.closest('select'))) { e.preventDefault(); return; } const h = e.target.closest('.status-stat-drop-source[data-drag-kind="' + dragKind + '"]'); if (!h) return; const id = parseInt(h.getAttribute('data-drag-id'), 10); if (!id) return; e.dataTransfer.effectAllowed = 'move'; const payload = JSON.stringify({ kind: dragKind, id: id }); e.dataTransfer.setData('application/json', payload); e.dataTransfer.setData('text/plain', dragKind + ':' + id); h.classList.add('status-row-dragging'); }); table.addEventListener('dragend', function (e) { const h = e.target.closest('.status-stat-drop-source[data-drag-kind="' + dragKind + '"]'); if (h) h.classList.remove('status-row-dragging'); }); const scope = _scopeEl(scopeId); if (!scope) return; scope.querySelectorAll('.stat-card[data-drop-status]').forEach(function (card) { if (card.dataset.statDropBound === '1') return; card.dataset.statDropBound = '1'; card.addEventListener('dragover', function (ev) { ev.preventDefault(); ev.dataTransfer.dropEffect = 'move'; card.classList.add('drag-over-drop'); }); card.addEventListener('dragleave', function (ev) { if (!card.contains(ev.relatedTarget)) { card.classList.remove('drag-over-drop'); } }); card.addEventListener('drop', function (ev) { ev.preventDefault(); scope.querySelectorAll('.stat-card.drag-over-drop').forEach(function (c) { c.classList.remove('drag-over-drop'); }); const status = card.getAttribute('data-drop-status'); let parsed = null; try { parsed = JSON.parse(ev.dataTransfer.getData('application/json') || '{}'); } catch (x) { /* ignore */ } if (!parsed || parsed.kind !== dragKind || !parsed.id) { const plain = (ev.dataTransfer.getData('text/plain') || '').trim(); const re = new RegExp('^' + dragKind + ':(\\d+)$'); const m = plain.match(re); if (m) parsed = { kind: dragKind, id: parseInt(m[1], 10) }; } if (!parsed || parsed.kind !== dragKind || !parsed.id) return; if (typeof onDrop === 'function') { onDrop(parsed.id, status); } }); }); } /** 动态渲染统计卡(Scrum 等页按数据生成卡片时复用统一样式与点击筛选) */ function bindDynamicCard(cardEl, scopeId, filterKey, onFilter) { if (!cardEl) return; cardEl.classList.add('stat-card', 'is-clickable'); cardEl.setAttribute('data-stat-key', filterKey); cardEl.dataset.statClickBound = '1'; cardEl.addEventListener('click', function () { if (typeof onFilter === 'function') { onFilter(filterKey); } else { onClick(scopeId, filterKey); } }); } /** 纯展示统计卡:按 count_id 或 data-stat-key 批量写入数值 */ function updateDisplayCounts(scopeId, counts) { const scope = scopeId ? _scopeEl(scopeId) : null; const map = counts || {}; Object.keys(map).forEach(function (ref) { let el = document.getElementById(ref); if (!el && scope) { const card = scope.querySelector('.stat-card[data-stat-key="' + ref.replace(/"/g, '\\"') + '"]') || scope.querySelector('.stat-card[data-count-id="' + ref.replace(/"/g, '\\"') + '"]'); if (card) { const countId = card.getAttribute('data-count-id'); if (countId) el = document.getElementById(countId); } } if (el) el.textContent = map[ref]; }); } function markDynamicActive(containerEl, filterKey) { if (!containerEl) return; containerEl.querySelectorAll('.stat-card').forEach(function (c) { c.classList.remove('active', 'border', 'border-primary'); }); const key = filterKey === undefined || filterKey === null ? 'all' : String(filterKey); const card = containerEl.querySelector('.stat-card[data-stat-key="' + key + '"]') || containerEl.querySelector('.stat-card[data-filter-key="' + key + '"]'); if (card) card.classList.add('active'); } global.IARStatCards = { register: register, connect: connect, onClick: onClick, setActive: setActive, setFilter: setFilter, getFilter: getFilter, updateCounts: updateCounts, updateDisplayCounts: updateDisplayCounts, initDropTargets: initDropTargets, bindDynamicCard: bindDynamicCard, markDynamicActive: markDynamicActive, }; _bindGlobalClickDelegate(); })(window);