/** * 统一实体列表操作列(参照 proj_prps.html 课题列表) * 基本操作:编辑、查看、复制、删除、目录、在线浏览(browse 仅在页面传入时显示) * 四个扩展操作(按页启用):关联的笔记、关联的团队、关联的课题、关联的学位论文 */ (function (global) { 'use strict'; var BASIC_DEFS = [ { key: 'edit', label: '编辑', shortLabel: '编辑', btnClass: 'btn-outline-primary' }, { key: 'view', label: '查看', shortLabel: '查看', btnClass: 'btn-outline-success' }, { key: 'copy', label: '复制', shortLabel: '复制', btnClass: 'btn-outline-info' }, { key: 'delete', label: '删除', shortLabel: '删除', btnClass: 'btn-outline-danger' }, { key: 'directory', label: '目录', shortLabel: '目录', btnClass: 'btn-outline-secondary' }, { key: 'browse', label: '在线浏览', shortLabel: '浏览', btnClass: 'btn-outline-primary' }, ]; var EXT_DEFS = [ { key: 'notes', label: '关联的笔记', shortLabel: '笔记', btnClass: 'btn-outline-info' }, { key: 'teams', label: '关联的团队', shortLabel: '团队', btnClass: 'btn-outline-success' }, { key: 'topics', label: '关联的课题', shortLabel: '课题', btnClass: 'btn-outline-warning' }, { key: 'theses', label: '关联的学位论文', shortLabel: '论文', btnClass: 'btn-outline-secondary' }, ]; function escHtml(s) { if (s == null) return ''; var d = document.createElement('div'); d.textContent = String(s); return d.innerHTML; } function escAttr(s) { return String(s == null ? '' : s) .replace(/&/g, '&') .replace(/"/g, '"') .replace(/'/g, ''') .replace(//g, '>'); } function formatArg(a) { if (a == null) return 'null'; if (typeof a === 'number' && isFinite(a)) return String(a); if (typeof a === 'boolean') return a ? 'true' : 'false'; return JSON.stringify(String(a)); } function resolveOnclick(spec, id) { if (spec == null || spec === false) return null; if (typeof spec === 'string') return spec; if (spec.onclick) return spec.onclick; if (spec.handler) { var args = spec.args != null ? spec.args : [id]; return spec.handler + '(' + args.map(formatArg).join(', ') + ')'; } return null; } function labelHtml(fullLabel, shortLabel) { var full = escHtml(fullLabel || ''); var short = escHtml(shortLabel || fullLabel || ''); if (!shortLabel || shortLabel === fullLabel) { return full; } return '' + full + '' + short + ''; } function renderButton(label, btnClass, onclick, title, disabled, shortLabel) { var titleAttr = title ? ' title="' + escAttr(title) + '"' : (label ? ' title="' + escAttr(label) + '"' : ''); var inner = labelHtml(label, shortLabel); if (disabled) { return ''; } if (!onclick) return ''; return ''; } function pickSpec(group, key, config) { if (group && group[key] != null) return group[key]; if (config[key] != null) return config[key]; return null; } /** * @param {object} config * @param {number|string} config.id * @param {object} [config.basic] 五个基本操作 * @param {object} [config.extended] 扩展操作(未配置的不显示) * @param {Array} [config.extra] 额外按钮 [{label, shortLabel, btnClass, onclick, title}] */ function renderActions(config) { config = config || {}; var id = config.id; var basic = config.basic || {}; var extended = config.extended || {}; var buttons = []; BASIC_DEFS.forEach(function (d) { var spec = pickSpec(basic, d.key, config); if (spec === false || spec == null) return; var onclick = resolveOnclick(spec, id); if (!onclick && !(spec && spec.disabled)) return; buttons.push(renderButton( (spec && spec.label) || d.label, (spec && spec.btnClass) || d.btnClass, onclick, (spec && spec.title) || d.label, spec && spec.disabled, (spec && spec.shortLabel) || d.shortLabel )); }); EXT_DEFS.forEach(function (d) { var spec = pickSpec(extended, d.key, config); if (spec === false || spec == null) return; var onclick = resolveOnclick(spec, id); if (!onclick && !(spec && spec.disabled)) return; buttons.push(renderButton( (spec && spec.label) || d.label, (spec && spec.btnClass) || d.btnClass, onclick, (spec && spec.title) || d.label, spec && spec.disabled, (spec && spec.shortLabel) || d.shortLabel )); }); (config.extra || []).forEach(function (btn) { if (!btn || (!btn.onclick && !btn.disabled)) return; buttons.push(renderButton( btn.label || '', btn.btnClass || 'btn-outline-secondary', btn.onclick, btn.title || btn.label, btn.disabled, btn.shortLabel )); }); if (!buttons.length) return ''; return '
' + buttons.join('') + '
'; } /** * @param {object} config 同 renderActions * @param {string} [tdClass] 追加到 td 的 class(如 'text-end' / 'nowrap') */ function actionsCell(config, tdClass) { var cls = 'iar-entity-list-actions'; if (tdClass) cls += ' ' + String(tdClass).trim(); return '' + renderActions(config) + ''; } function emptyRow(colspan, message) { return '' + escHtml(message || '暂无数据') + ''; } function _tbodyEl(tbodyId) { return typeof tbodyId === 'string' ? document.getElementById(tbodyId) : tbodyId; } function _rowCheckboxes(tbodyId) { var tbody = _tbodyEl(tbodyId); if (!tbody) return []; return Array.from(tbody.querySelectorAll('.iar-entity-list-row-select')); } function _headerCheckbox(tbodyId) { return document.querySelector('.iar-entity-list-select-all[data-iar-select-tbody="' + tbodyId + '"]'); } function renderSelectCell(id, tbodyId) { return '' + '' + ''; } function selectCell(id, tbodyId) { return renderSelectCell(id, tbodyId); } function bindSelection(tbodyId) { var tbody = _tbodyEl(tbodyId); if (!tbody || tbody.__iarSelectBound) return; tbody.__iarSelectBound = true; tbody.addEventListener('change', function (ev) { if (ev.target && ev.target.classList.contains('iar-entity-list-row-select')) { updateSelectAllState(tbodyId); } }); } function initAllSelections() { document.querySelectorAll('.iar-entity-list-select-all[data-iar-select-tbody]').forEach(function (header) { var tbodyId = header.getAttribute('data-iar-select-tbody'); if (tbodyId) bindSelection(tbodyId); }); } function selectAll(tbodyId) { _rowCheckboxes(tbodyId).forEach(function (cb) { cb.checked = true; }); updateSelectAllState(tbodyId); } function deselectAll(tbodyId) { _rowCheckboxes(tbodyId).forEach(function (cb) { cb.checked = false; }); updateSelectAllState(tbodyId); } function toggleSelectAll(headerCb) { if (!headerCb) return; var tbodyId = headerCb.getAttribute('data-iar-select-tbody'); if (!tbodyId) return; _rowCheckboxes(tbodyId).forEach(function (cb) { cb.checked = headerCb.checked; }); headerCb.indeterminate = false; } function onRowSelectChange(tbodyId) { updateSelectAllState(tbodyId); } function updateSelectAllState(tbodyId) { var header = _headerCheckbox(tbodyId); if (!header) return; var all = _rowCheckboxes(tbodyId); if (!all.length) { header.checked = false; header.indeterminate = false; return; } var checked = all.filter(function (cb) { return cb.checked; }); header.checked = checked.length === all.length; header.indeterminate = checked.length > 0 && checked.length < all.length; } function getSelectedIds(tbodyId) { return _rowCheckboxes(tbodyId) .filter(function (cb) { return cb.checked; }) .map(function (cb) { return cb.getAttribute('data-id'); }) .filter(function (id) { return id != null && id !== ''; }); } function syncTableScrollbars(sourceEl, wrapper) { if (!wrapper) return; var container = wrapper.querySelector('.iar-entity-list-table-container'); var top = wrapper.querySelector('.iar-entity-list-hscroll-top'); var bottom = wrapper.querySelector('.iar-entity-list-hscroll-bottom'); if (!container || !top || !bottom) return; if (sourceEl === container) { top.scrollLeft = container.scrollLeft; bottom.scrollLeft = container.scrollLeft; } else if (sourceEl === top) { container.scrollLeft = top.scrollLeft; bottom.scrollLeft = top.scrollLeft; } else if (sourceEl === bottom) { container.scrollLeft = bottom.scrollLeft; top.scrollLeft = bottom.scrollLeft; } } function initTableScrollbars(wrapper) { if (!wrapper || wrapper.__iarScrollInit) return; var container = wrapper.querySelector('.iar-entity-list-table-container'); var table = wrapper.querySelector('.iar-entity-list-table'); var top = wrapper.querySelector('.iar-entity-list-hscroll-top'); var bottom = wrapper.querySelector('.iar-entity-list-hscroll-bottom'); var topInner = top && top.querySelector('.iar-entity-list-hscroll-inner'); var bottomInner = bottom && bottom.querySelector('.iar-entity-list-hscroll-inner'); if (!container || !table || !top || !bottom || !topInner || !bottomInner) return; wrapper.__iarScrollInit = true; function onScroll(ev) { syncTableScrollbars(ev.currentTarget, wrapper); } container.addEventListener('scroll', onScroll); top.addEventListener('scroll', onScroll); bottom.addEventListener('scroll', onScroll); function updateWidth() { var tableWidth = table.scrollWidth; var containerWidth = container.clientWidth; if (tableWidth > containerWidth) { topInner.style.width = tableWidth + 'px'; bottomInner.style.width = tableWidth + 'px'; top.style.display = 'block'; bottom.style.display = 'block'; } else { top.style.display = 'none'; bottom.style.display = 'none'; } } wrapper.__iarScrollUpdate = updateWidth; setTimeout(updateWidth, 50); window.addEventListener('resize', updateWidth); if (typeof ResizeObserver !== 'undefined') { var ro = new ResizeObserver(updateWidth); ro.observe(table); wrapper.__iarScrollRo = ro; } } function refreshScrollbars(root) { var scope = root && root.querySelectorAll ? root : document; var wrappers = scope.querySelectorAll ? scope.querySelectorAll('[data-iar-entity-list-scroll], .iar-entity-list-table-wrapper') : []; wrappers.forEach(function (wrapper) { if (wrapper.__iarScrollUpdate) wrapper.__iarScrollUpdate(); else initTableScrollbars(wrapper); }); document.querySelectorAll('.iar-entity-list-select-all[data-iar-select-tbody]').forEach(function (header) { var tbodyId = header.getAttribute('data-iar-select-tbody'); if (tbodyId) updateSelectAllState(tbodyId); }); } function initAllScrollbars() { document.querySelectorAll('[data-iar-entity-list-scroll], .iar-entity-list-table-wrapper').forEach(initTableScrollbars); } function observeTableBodies() { if (typeof MutationObserver === 'undefined') return; var mo = new MutationObserver(function (mutations) { mutations.forEach(function (m) { var wrapper = m.target.closest && m.target.closest('.iar-entity-list-table-wrapper'); if (wrapper && wrapper.__iarScrollUpdate) { setTimeout(function () { wrapper.__iarScrollUpdate(); }, 50); } }); }); document.querySelectorAll('.iar-entity-list-table-wrapper tbody').forEach(function (tb) { mo.observe(tb, { childList: true, subtree: true }); }); } function bootEntityListScrollbars() { initAllScrollbars(); observeTableBodies(); initAllSelections(); } if (typeof document !== 'undefined') { if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', bootEntityListScrollbars); } else { bootEntityListScrollbars(); } } function ensureDeleteConfirmModal(modalId) { var el = document.getElementById(modalId); if (!el) { el = document.createElement('div'); el.className = 'modal fade'; el.id = modalId; el.tabIndex = -1; el.setAttribute('aria-hidden', 'true'); el.setAttribute('data-bs-backdrop', 'static'); el.setAttribute('data-bs-keyboard', 'false'); el.setAttribute('aria-labelledby', modalId + 'Title'); el.innerHTML = ''; } // 避免被页面局部 stacking context / 高 z-index 悬浮层挡住 el.style.zIndex = '20000'; if (el.parentElement !== document.body) { document.body.appendChild(el); } return el; } function confirmDeleteWithDirNative(options) { options = options || {}; var msg = (options.message || '请选择删除方式') + '\n\n确定 = 删除记录+目录\n取消后再选 = 仅删除记录 / 放弃'; if (!window.confirm(msg + '\n\n是否【删除记录+目录】?\n(点“取消”将继续询问是否仅删除记录)')) { if (!window.confirm('是否【仅删除记录】(保留目录)?\n点“取消”则放弃删除。')) { return Promise.resolve(null); } return Promise.resolve('record'); } return Promise.resolve('both'); } /** * 三按钮删除确认:删除记录+目录 / 仅删除记录 / 取消 * @returns {Promise<'both'|'record'|null>} */ function confirmDeleteWithDir(options) { options = options || {}; var modalId = options.modalId || 'iarEntityListDeleteConfirmModal'; var ModalCtor = (typeof bootstrap !== 'undefined' && bootstrap.Modal) ? bootstrap.Modal : null; if (!ModalCtor) { return confirmDeleteWithDirNative(options); } var modalEl = ensureDeleteConfirmModal(modalId); var titleEl = document.getElementById(modalId + 'Title'); var msgEl = document.getElementById(modalId + 'Message'); if (titleEl && options.title) { titleEl.innerHTML = ' ' + escHtml(options.title); } if (msgEl) { msgEl.textContent = options.message || '请选择删除方式:'; } return new Promise(function (resolve) { var settled = false; var choice = null; var ready = false; function cleanup() { modalEl.removeEventListener('click', onClick, true); modalEl.removeEventListener('hidden.bs.modal', onHidden); modalEl.removeEventListener('shown.bs.modal', onShown); } function finish(result) { if (settled) return; settled = true; cleanup(); resolve(result); } function onClick(ev) { var btn = ev.target && ev.target.closest ? ev.target.closest('[data-iar-delete-choice]') : null; if (!btn || !modalEl.contains(btn)) return; ev.preventDefault(); ev.stopPropagation(); var c = btn.getAttribute('data-iar-delete-choice'); choice = (c === 'both' || c === 'record') ? c : null; try { var inst = ModalCtor.getInstance(modalEl); if (inst) inst.hide(); else finish(choice); } catch (e) { finish(choice); } } function onShown() { ready = true; } function onHidden() { // 仅在真正展示过之后,才把关闭视为最终结果(避免 show 过程中误触发 hidden) if (!ready && choice == null) { // 极少数环境下 show 失败会立刻 hidden:回退原生确认 cleanup(); confirmDeleteWithDirNative(options).then(finish); return; } finish(choice); } modalEl.addEventListener('click', onClick, true); modalEl.addEventListener('shown.bs.modal', onShown); modalEl.addEventListener('hidden.bs.modal', onHidden); try { var inst = ModalCtor.getOrCreateInstance(modalEl, { backdrop: 'static', keyboard: false, focus: true }); inst.show(); // 若动画被禁用 / 未触发 shown,超时兜底标记 ready,避免永远卡住 setTimeout(function () { if (!settled) ready = true; }, 400); } catch (e) { cleanup(); confirmDeleteWithDirNative(options).then(finish); } }); } function _flattenBrowseFiles(nodes, out) { (nodes || []).forEach(function (node) { if (!node) return; if (node.type === 'directory') { _flattenBrowseFiles(node.children, out); } else if (node.type === 'file' && node.path) { out.push(node); } }); return out; } function _browseTabButton(label, active, onClick) { var btn = document.createElement('button'); btn.type = 'button'; btn.className = 'nav-link' + (active ? ' active' : ''); btn.textContent = label; btn.addEventListener('click', onClick); var li = document.createElement('li'); li.className = 'nav-item'; li.appendChild(btn); return li; } /** * 打开在线浏览对话框。 * filesUrl(id, subdir) 须把子目录传给接口。 * fileUrl(id, subdir, path) 返回该文件的在线浏览地址。 * subdirs: [{key, label}] */ function openBrowse(options) { options = options || {}; var modalId = options.modalId || 'iarEntityBrowseModal'; var modalEl = document.getElementById(modalId); if (!modalEl || !global.bootstrap) { alert('在线浏览对话框未加载'); return; } var subdirs = options.subdirs && options.subdirs.length ? options.subdirs : [{ key: '正文', label: '正文' }, { key: '图表', label: '图表' }, { key: '其他', label: '其他' }]; var titleEl = document.getElementById(modalId + 'Title'); if (titleEl) titleEl.textContent = options.title || '在线浏览'; var subdirTabs = document.getElementById(modalId + 'SubdirTabs'); var fileTabs = document.getElementById(modalId + 'FileTabs'); var emptyEl = document.getElementById(modalId + 'Empty'); var frame = document.getElementById(modalId + 'Frame'); var activeSubdir = subdirs[0].key; function showEmpty(message) { if (fileTabs) fileTabs.innerHTML = ''; if (frame) { frame.classList.add('d-none'); frame.removeAttribute('src'); } if (emptyEl) { emptyEl.textContent = message || '该子目录下暂无文件'; emptyEl.classList.remove('d-none'); } } function showFile(subdir, file, tabBtn) { if (!frame || typeof options.fileUrl !== 'function') return; if (fileTabs) { fileTabs.querySelectorAll('.nav-link').forEach(function (el) { el.classList.remove('active'); }); } if (tabBtn) tabBtn.classList.add('active'); if (emptyEl) emptyEl.classList.add('d-none'); frame.classList.remove('d-none'); frame.src = options.fileUrl(options.id, subdir, file.path); } function renderFiles(subdir, nodes) { var files = _flattenBrowseFiles(nodes, []); if (!fileTabs) return; fileTabs.innerHTML = ''; if (!files.length) { showEmpty('该子目录下暂无文件'); return; } if (emptyEl) emptyEl.classList.add('d-none'); files.forEach(function (file, index) { var label = file.path || file.name || ('文件' + (index + 1)); var li = _browseTabButton(label, index === 0, function () { showFile(subdir, file, li.querySelector('.nav-link')); }); fileTabs.appendChild(li); }); showFile(subdir, files[0], fileTabs.querySelector('.nav-link')); } function loadSubdir(subdir) { activeSubdir = subdir; if (subdirTabs) { subdirTabs.querySelectorAll('.nav-link').forEach(function (el) { el.classList.toggle('active', el.getAttribute('data-subdir') === subdir); }); } showEmpty('正在加载…'); if (typeof options.filesUrl !== 'function') { showEmpty('未配置浏览接口'); return; } fetch(options.filesUrl(options.id, subdir), { cache: 'no-store' }) .then(function (res) { return res.json(); }) .then(function (data) { if (activeSubdir !== subdir) return; if (!data || !data.success) { showEmpty((data && data.message) || '加载失败'); return; } var tree = (data.files && data.files[subdir]) || []; renderFiles(subdir, tree); }) .catch(function () { if (activeSubdir === subdir) showEmpty('加载失败'); }); } if (subdirTabs) { subdirTabs.innerHTML = ''; subdirs.forEach(function (item) { var li = _browseTabButton(item.label || item.key, false, function () { loadSubdir(item.key); }); var btn = li.querySelector('.nav-link'); if (btn) btn.setAttribute('data-subdir', item.key); subdirTabs.appendChild(li); }); } bootstrap.Modal.getOrCreateInstance(modalEl).show(); loadSubdir(subdirs[0].key); } global.IAREntityList = { renderActions: renderActions, actionsCell: actionsCell, renderSelectCell: renderSelectCell, selectCell: selectCell, bindSelection: bindSelection, initAllSelections: initAllSelections, selectAll: selectAll, deselectAll: deselectAll, toggleSelectAll: toggleSelectAll, onRowSelectChange: onRowSelectChange, updateSelectAllState: updateSelectAllState, getSelectedIds: getSelectedIds, emptyRow: emptyRow, escAttr: escAttr, escHtml: escHtml, initTableScrollbars: initTableScrollbars, initAllScrollbars: initAllScrollbars, refreshScrollbars: refreshScrollbars, syncTableScrollbars: syncTableScrollbars, confirmDeleteWithDir: confirmDeleteWithDir, openBrowse: openBrowse, }; })(typeof window !== 'undefined' ? window : this);