/** * 选集智能折叠分页 —— 纯函数,无 DOM 依赖,便于单元测试。 * 返回 [{n:number} | {dot:true}] 序列;{dot:true} 表示可点击的省略号。 * * @param {number} total 总集数 * @param {number} current 当前集(1-based) * @param {{threshold?:number, edge?:number, around?:number}} opts */ (function () { 'use strict'; function paginate(total, current, opts) { opts = opts || {}; const threshold = opts.threshold > 0 ? opts.threshold : 12; // 超过则折叠 const edge = opts.edge > 0 ? opts.edge : 3; // 首尾各展示数 const around = opts.around >= 0 ? opts.around : 2; // 当前前后各展示数 total = Math.max(0, Math.floor(total)); current = Math.min(total, Math.max(1, Math.floor(current))); const out = []; if (total === 0) return out; if (total <= threshold) { for (let i = 1; i <= total; i++) out.push({ n: i }); return out; } const set = new Set(); for (let i = 1; i <= edge && i <= total; i++) set.add(i); for (let i = total - edge + 1; i <= total; i++) if (i >= 1) set.add(i); for (let i = current - around; i <= current + around; i++) if (i >= 1 && i <= total) set.add(i); const sorted = Array.from(set).sort((a, b) => a - b); let prev = 0; for (const n of sorted) { const d = n - prev; if (d > 2) out.push({ dot: true }); else if (d === 2) out.push({ n: prev + 1 }); // 只差一个隐藏集,直接展示 out.push({ n }); prev = n; } return out; } window.EpisodePaginator = { paginate: paginate }; })();